Investigate CWE-369, Divide by Zero
まだ誰も着手していません。
評価
調査の方向性
Read the CWE-369 guidance and review the proposed Python example against the discussion linked in the issue. Decide which compliant solutions should be documented and clarify how integer division, Decimal overflow, and the ctypes case apply. Done means the guide presents accurate, agreed examples and explains when each approach is appropriate.
索引モデルが issue の本文から書いたものです。
説明
When trying to demonstrate a compliant solution for CWE-369, we ran into a lot of questions on how to handle this.
When performing division or remainder calculations on integers in Python, a ZeroDivisionError can occur if the divisor is zero. To prevent this, the divisor should be checked to ensure it is not zero before proceeding, or handle the ZeroDivisionError exception if the divisor is zero. This rule only applies to integers, not floating-point numbers. In floating-point arithmetic, dividing a positive non-zero value by zero yields positive infinity, while dividing a negative non-zero value by zero produces negative infinity. However, attempting to divide zero by zero leads to a ZeroDivisionError, as the outcome is undefined.
'Decimal' is used in division calculations, as it ensures the accuracy of the math, and it also prevents float overflow from occurring.
Emax and Emin are the upper and lower bounds of the Decimal class. Overflow can occur in decimal calculation if the calculated number is above or below the Emax or Emin. The default precision of decimal numbers is 28. So all integers up to 999999999999999999999999999999 (28 nines) can be represented exactly, and higher numbers might be rounded.
We have this potentially compliant code example:
Which tries to include error handling for division by zero and potential overflow errors:
""" Compliant Code Example """
from decimal import Decimal, ROUND_HALF_UP, Overflow
def divide(first_number: int, second_number: int):
'''Function to divide 2 numbers'''
if second_number == 0:
raise ZeroDivisionError("TODO: implement zero division error handling here")
# This operation may cause an overflow due to a large exponent
try:
result = Decimal(first_number / second_number)
except Overflow as e:
raise Overflow("TODO: implement overflow error handling here") from e
rounded_result = Decimal(result).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
# Rounding result to 2 decimal place
return rounded_result
#####################
# Trying to exploit above code example
#####################
for number in (10, 41, 200, -10, 0):
# Dividing 100 by each number 10, 41, 200, -10, and 0 separately.
print("-" * 20 + f"divide by {number}" + "-" * 20 + "\n")
print(f"100 / {number} = ", end="")
print(divide(100, number))
However, after more thought and discussion with the python community on this issue, perhaps since dividing two integers already raises ZeroDivisionError. You don’t need to check if the divisor is zero and raise ZeroDivisionError because it happens automatically.
Hence the only risk may come from using ctypes... in which case the following compliant solutions a possibilities:
Maybe there could just be numerous examples of compliant solutions:
- running the code in a safe environment that raises a divide by zero exception, and handles exceptions gracefully.
- user input checking.
- a Maybe Monad.
- return float('nan') (or requiring zeros to be signed and returning + or - float('inf') accordingly)
- intrinsically non-zero denominators, e.g. len(...) + 1.
- converting the numerator and denominator to a class T on which T(1)/T(0) is valid, (e.g. the Projectively Extended Real Line).
Full discussion with python.org can be seen here:
https://discuss.python.org/t/is-this-a-compliant-solution-for-cwe-369/86953/8
- 主要言語
- Python
- スター
- 2
- フォーク
- 2
- 平均マージ
- 11分
- マージ済み PR(30日)
- 2
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ossf/python-secure-coding-guide のほかの issue
-
pyscg-0051 should use a context manager対応中かも @s19110 が 8 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ossf/python-secure-coding-guide#1 · コメント 4 件 · 担当者 1 名 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 65/100
ossf/python-secure-coding-guide#29 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
ossf/python-secure-coding-guide の issue をすべて見る
似ている issue
-
documentation type/bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
add entry to blockオープン
難易度 1/5 1時間未満 初心者へのやさしさ 65/100
StevenBlack/hosts#3299 ·
-
`garak --report` overwrites the input report when its name has no ".report"対応中かも @0z5a が今日担当しました。 オープンneeds-triage
難易度 2/5 半日 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
trace: Pi's 1-hour cache writes are priced at 1.25x, but billed at 2x対応中かも @Shizoqua が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/TokenEconomizer#221 ·
メンテナーはふだん 1 日以内に返信
-
plot_graph crashes when a mesh node has no m2m edge対応中かも @tygvi が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
mllam/neural-lam#883 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信