Repository navigation
fix: prevent integer overflow and division by zero in size utils - #262
Conversation
1. Root cause: ceilSize() and formatSize() performed unchecked arithmetic on 64-bit signed integers, causing CWE-190 overflow; floorSize() and ceilSize() divided by roundingSize without a zero-guard, causing CWE-369 division by zero (SIGFPE) 2. Fix: add roundingSize <= 0 early return in floorSize()/ceilSize(); use __builtin_add_overflow in ceilSize() and __builtin_mul_overflow in formatSize() to detect overflow, capping to LLONG_MAX on overflow 3. Impact: normal-value paths unchanged; extreme/zero-value inputs now return safely instead of crashing or producing undefined behavior Influence: 1. Test disk size display with normal values across all unit thresholds 2. Test partition alignment with standard rounding sizes (e.g. 4096) 3. Verify no crash on extreme values (near LLONG_MAX) and zero roundingSize fix: 修复尺寸工具函数中的整数溢出与除零问题 1. 根因:ceilSize() 和 formatSize() 对 64 位有符号整数执行未检查的 算术运算,导致 CWE-190 溢出;floorSize() 和 ceilSize() 未对 roundingSize 做零值守卫即执行除法,导致 CWE-369 除零(SIGFPE) 2. 方案:在 floorSize()/ceilSize() 中添加 roundingSize <= 0 提前返回; ceilSize() 使用 __builtin_add_overflow、formatSize() 使用 __builtin_mul_overflow 检测溢出,溢出时 cap 到 LLONG_MAX 3. 影响:正常值路径行为不变;极值/零值输入从崩溃或未定义行为 变为安全返回 Influence: 1. 测试各单位阈值档位下的正常磁盘容量显示 2. 测试标准对齐粒度(如 4096)下的分区大小取整 3. 验证极值(接近 LLONG_MAX)和零值 roundingSize 下不崩溃 PMS: BUG-383517
Reviewer's GuideImplementationReview the overflow-safe byte calculation in
Review the zero-or-negative rounding guards and overflow-safe numerator calculation in
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="basestruct/utils.cpp" line_range="556" />
<code_context>
+ if (__builtin_add_overflow(value, roundingSize - 1LL, &numerator)) {
+ numerator = LLONG_MAX;
+ }
+ return numerator / roundingSize * roundingSize;
}
</code_context>
<issue_to_address>
**Trailing bytes remain unerased**
When the numerator addition overflows and `roundingSize` does not divide `LLONG_MAX`, `ceilSize` substitutes `LLONG_MAX` for the overflowing numerator, then division and multiplication round it down to a multiple below the requested value. When `PartedCore::eraseFilesystemSignatures()` uses that result to calculate the end of a write range, trailing bytes are not erased.
Detect an unrepresentable rounded-up size and report failure instead of returning a smaller multiple.
</issue_to_address>| if (__builtin_add_overflow(value, roundingSize - 1LL, &numerator)) { | ||
| numerator = LLONG_MAX; | ||
| } | ||
| return numerator / roundingSize * roundingSize; |
There was a problem hiding this comment.
🟡 Medium · Trailing bytes remain unerased
When the numerator addition overflows and roundingSize does not divide LLONG_MAX, ceilSize substitutes LLONG_MAX for the overflowing numerator, then division and multiplication round it down to a multiple below the requested value. When PartedCore::eraseFilesystemSignatures() uses that result to calculate the end of a write range, trailing bytes are not erased.
Detect an unrepresentable rounded-up size and report failure instead of returning a smaller multiple.
Prompt for AI agents
In `basestruct/utils.cpp` at line 556:
**Trailing bytes remain unerased**
When the numerator addition overflows and `roundingSize` does not divide `LLONG_MAX`, `ceilSize` substitutes `LLONG_MAX` for the overflowing numerator, then division and multiplication round it down to a multiple below the requested value. When `PartedCore::eraseFilesystemSignatures()` uses that result to calculate the end of a write range, trailing bytes are not erased.
Detect an unrepresentable rounded-up size and report failure instead of returning a smaller multiple.|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: liyigang1, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
ac95791
into
linuxdeepin:release/1071
Root Cause Analysis
Three integer safety defects exist in
basestruct/utils.cpp:ceilSize()(utils.cpp:541) — CWE-190 + CWE-369: The expression(value + roundingSize - 1LL) / roundingSize * roundingSizeoverflows whenvalueis nearLLONG_MAX, and divides by zero whenroundingSize=0.floorSize()(utils.cpp:536) — CWE-369:value / roundingSize * roundingSizedivides by zero whenroundingSize=0, causing SIGFPE.formatSize()(utils.cpp:473) — CWE-190:sectors * sectorSizemultiplication overflows for large sector counts, evaluated 5 times in if/else branches.Both
SectorandByte_Valuearetypedef long long(64-bit signed). UBSan detected 5 signed integer overflow points; divide-by-zero test exited with SIGFPE (code 136).Fix Approach
floorSize()/ceilSize(): AddedroundingSize <= 0early return (returnsvalue), eliminating CWE-369.ceilSize(): Used__builtin_add_overflowto safely computevalue + roundingSize - 1LL, capping toLLONG_MAXon overflow.formatSize(): Used__builtin_mul_overflowto safely computesectors * sectorSizeonce, capping toLLONG_MAXon overflow, replacing 5 inline overflow-prone multiplications.Change Safety Assessment
Code Safety
Business Impact Scope
formatSize()formats sector count + sector size to human-readable string. Normal display behavior unchanged.floorSize()/ceilSize()round partition sizes to alignment boundaries. Normal alignment behavior unchanged.Verification Suggestion
Test disk size display across all unit thresholds (B/KiB/MiB/GiB/TiB) and partition alignment with standard rounding sizes. Verify no crash on extreme values and zero roundingSize.
根因分析
basestruct/utils.cpp中存在三个整数安全缺陷:ceilSize()(utils.cpp:541) — CWE-190 + CWE-369:表达式(value + roundingSize - 1LL) / roundingSize * roundingSize在value接近LLONG_MAX时加法溢出,roundingSize=0时除零。floorSize()(utils.cpp:536) — CWE-369:value / roundingSize * roundingSize在roundingSize=0时除零,导致 SIGFPE。formatSize()(utils.cpp:473) — CWE-190:sectors * sectorSize乘法在大扇区数时溢出,在 if/else 分支中重复求值 5 次。Sector和Byte_Value均为typedef long long(64 位有符号)。UBSan 检出 5 处有符号整数溢出;除零测试退出码 136(SIGFPE)。修复方案
floorSize()/ceilSize():添加roundingSize <= 0提前返回(返回value),消除 CWE-369。ceilSize():使用__builtin_add_overflow安全计算value + roundingSize - 1LL,溢出时 cap 到LLONG_MAX。formatSize():使用__builtin_mul_overflow安全计算sectors * sectorSize,溢出时 cap 到LLONG_MAX,替换 5 处内联溢出乘法。改动安全评估
代码安全评估
业务影响范围
formatSize()将扇区数+扇区大小格式化为可读字符串,正常显示行为不变。floorSize()/ceilSize()对分区大小取整对齐,正常对齐行为不变。验证建议
测试各单位阈值档位下的磁盘容量显示和标准对齐粒度下的分区取整。验证极值和零值 roundingSize 下不崩溃。
Summary by Sourcery
Harden size formatting and alignment utilities against integer overflow and invalid rounding inputs.
Bug Fixes:
Enhancements: