Repository navigation
fix: fix 5 fuzz test security issues in devicemanager - #988
deepin-bot[bot] merged 1 commit into
Conversation
1. Root cause: gcd() only guarded b==0 so gcd(0,N) still divided by zero after swap, while negative/INT_MIN inputs recursed infinitely or overflowed in a % b; hexToDec() used int accumulation causing signed overflow on long hex; convertToBytes() cast unchecked double to quint64 causing UB on large values; loadSgSmartCtlInfoToCache() accessed words[2] without size check 2. Fix: guard zero arguments and iterate on 64-bit absolute values in gcd(), plus zero-value filter in caculateScreenRatio(); widen hexToDec() to qint64 with 15-char cap; validate double range before static_cast in convertToBytes(); add words.size()<3 guard in loadSgSmartCtlInfoToCache() 3. Add regression cases for every fuzz entry under tests/, verified with ASan/UBSan: the same inputs abort on pre-fix builds and pass after fix 4. Impact: eliminates divide-by-zero, integer overflow, and OOB read paths under fuzz input; no behavior change for valid inputs Influence: 1. Test screen ratio calculation with zero/empty resolution strings 2. Test hexToDec with long hex strings exceeding INT_MAX 3. Test convertToBytes with extremely large size values 4. Test loadSgSmartCtlInfoToCache with malformed line input 5. Test gcd() with zero, negative and INT_MIN arguments fix: 修复设备管理器5个模糊测试安全问题 1. 根因:gcd()仅判断b==0,a==0时交换后仍会除零,负数/INT_MIN入参会 无限递归且a % b溢出;hexToDec()使用int累加长十六进制字符串溢出; convertToBytes()未校验double转quint64越界; loadSgSmartCtlInfoToCache()未检查words[2]越界 2. 方案:gcd()同时守卫零参数并改用64位绝对值迭代实现, caculateScreenRatio()过滤零值;hexToDec()扩宽为qint64并限制15字符; convertToBytes()转换前校验数值范围;loadSgSmartCtlInfoToCache()增加 words.size()<3检查 3. 补充:为上述模糊测试入口新增回归用例,已用ASan/UBSan验证(修复前 同一输入崩溃或触发UB,修复后通过) 4. 影响:消除模糊测试输入下的除零、整数溢出、越界读取路径,对正常输入 无行为变化 Influence: 1. 测试零值/空分辨率字符串的屏幕比例计算 2. 测试超长十六进制字符串的hexToDec转换 3. 测试超大数值的convertToBytes转换 4. 测试格式异常行的loadSgSmartCtlInfoToCache处理 5. 测试零值、负数与INT_MIN入参的gcd()计算 PMS: BUG-383515
Reviewer's GuideImplementationGuard malformed smartctl path lines and make screen-ratio and GCD handling safe for zero, negative, and extreme integer inputs.
Reject out-of-range byte-size conversions and cap hexadecimal parsing to avoid overflow in the accumulator.
Add regression coverage for malformed smartctl lines, storage size overflow, and huge disk-size strings.
Add focused regression cases for GCD and screen-ratio edge inputs introduced by the fuzz findings.
Add regression cases for hexadecimal conversion beyond the former integer range and for long or invalid input.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 8 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp" line_range="157" />
<code_context>
}
- diskBytesSize = static_cast<quint64>(diskSizeFloat * multiplier);
+ double result = diskSizeFloat * multiplier;
+ if (result < 0.0 || result > static_cast<double>(std::numeric_limits<quint64>::max())) {
+ qCDebug(appLog) << "convertToBytes, result out of range, clamping to 0";
+ return 0;
</code_context>
<issue_to_address>
**Oversized capacities reach undefined conversion**
When the size and unit produce a `double` result of exactly 2^64, such as `16 EB` with the 1024 scale, `static_cast<double>(quint64::max())` rounds up to 2^64, so the range check accepts that result and `static_cast<quint64>(result)` converts an out-of-range value, causing undefined behavior instead of clamping it.
Reject results greater than or equal to 2^64 before converting them to `quint64`.
</issue_to_address>
### Comment 2
<location path="deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp" line_range="497" />
<code_context>
int width = re.cap(1).toInt();
int height = re.cap(2).toInt();
+ if (width == 0 || height == 0) {
+ return;
+ }
int gys = gcd(width, height);
</code_context>
<issue_to_address>
**Old display ratio remains visible**
When a monitor with a calculated ratio receives a zero-dimension resolution, `caculateScreenRatio()` returns without clearing `m_AspectRatio`, so callers and `loadOtherDeviceInfo()` continue to expose or persist the previous resolution’s ratio as current display data.
Clear `m_AspectRatio` before returning from the zero-dimension guard.
Also at `deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp:496`.
</issue_to_address>
### Comment 3
<location path="deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp" line_range="159" />
<code_context>
+ double result = diskSizeFloat * multiplier;
+ if (result < 0.0 || result > static_cast<double>(std::numeric_limits<quint64>::max())) {
+ qCDebug(appLog) << "convertToBytes, result out of range, clamping to 0";
+ return 0;
+ }
+ diskBytesSize = static_cast<quint64>(result);
</code_context>
<issue_to_address>
**Displayed capacity contradicts bytes**
When `getInfoFromLshw()` receives an oversized capacity that `convertToBytes()` rejects, `getInfoFromLshw()` keeps the raw oversized size in `m_Size` while `convertToBytes()` sets `m_SizeBytes` to zero, so the device can be shown as having an enormous capacity while numeric consumers treat it as zero.
Keep the displayed size consistent with the rejected byte count, or reject the capacity/device instead of retaining the unrepresentable size.
</issue_to_address>
### Comment 4
<location path="deepin-devicemanager/tests/src/DeviceManager/ut_fuzz_383515.cpp" line_range="42-78" />
<code_context>
+ EXPECT_EQ(0, m_deviceMonitor->gcd(0, 0));
</code_context>
<issue_to_address>
**Regression tests fail to compile**
When the test executable is built, the test target compiles these added tests, but they directly call private methods and access private fields without a test-access declaration or visibility workaround, so compilation fails.
Use public entry points or add an explicit test-access mechanism for the private methods and fields used by these tests.
Also at `deepin-devicemanager-server/tests/src/LoadInfo/ut_ThreadPoolTask.cpp:84-87`.
</issue_to_address>
### Comment 5
<location path="deepin-devicemanager/src/Tool/EDIDParser.cpp" line_range="461" />
<code_context>
int i;
- int v = 0;
- for (i = 0; i < strHex.length(); i++) {
+ qint64 v = 0;
+ int len = strHex.length();
+ if (len > 15) {
</code_context>
<issue_to_address>
**Large hex-to-binary conversions collapse**
When `hexToBin()` receives a hexadecimal value whose decimal representation exceeds `INT_MAX`, `hexToDec()` now returns values above `INT_MAX`, but `hexToBin()` passes that decimal string to `decToBin()`, whose `toInt()` fails on those values and yields zero; the binary conversion therefore returns `0000` for inputs such as `FFFFFFFF`.
Make `decToBin()` accept a 64-bit value, or implement hex-to-binary conversion directly without narrowing through `int`.
</issue_to_address>
### Comment 6
<location path="deepin-devicemanager/src/Tool/EDIDParser.cpp" line_range="463-465" />
<code_context>
- for (i = 0; i < strHex.length(); i++) {
+ qint64 v = 0;
+ int len = strHex.length();
+ if (len > 15) {
+ len = 15;
+ }
+ for (i = 0; i < len; i++) {
v *= 16;
</code_context>
<issue_to_address>
**Long hex strings lose trailing digits**
When a hexadecimal input longer than 15 characters has significant digits after the first 15, `hexToDec()` processes only the first 15 characters, so a valid 16-character value such as `000000000000000F` converts to zero instead of 15. The length cap silently changes the represented value even when the input fits in `qint64`.
Handle the entire input while checking for overflow, or reject values that cannot be represented rather than silently dropping trailing digits.
</issue_to_address>
### Comment 7
<location path="deepin-devicemanager/tests/src/DeviceManager/ut_devicestorage.cpp" line_range="431-433" />
<code_context>
+// m_Size 中的数字部分曾被强转为 int 导致越界,当前实现只做容量区间比较
+TEST_F(UT_DeviceStorage, UT_DeviceStorage_checkDiskSize_huge_383515)
+{
+ m_deviceStorage->m_Size = "999999999999999999999999999999 GB";
+ m_deviceStorage->m_SizeBytes = 999999999999999999ULL;
+ EXPECT_NO_FATAL_FAILURE(m_deviceStorage->checkDiskSize());
+}
</code_context>
<issue_to_address>
**Oversized-size test misses the input**
When the regression suite runs the oversized-size case, `checkDiskSize()` bases its comparisons on `m_SizeBytes` and never reads the oversized `m_Size` string, while the test only checks for fatal failures; it therefore passes without exercising the claimed oversized-string handling.
Exercise the oversized string through the code that parses it and assert the expected handling.
</issue_to_address>
### Comment 8
<location path="deepin-devicemanager/tests/src/DeviceManager/ut_devicestorage.cpp" line_range="417" />
<code_context>
+ mapinfo.insert("size", "1ZB (1ZB)");
+ m_deviceStorage->getInfoFromLshw(mapinfo);
+ // 超出 quint64 表示范围,钳位为 0,不再产生未定义行为
+ EXPECT_EQ(0ULL, m_deviceStorage->m_SizeBytes);
+
+ // 正常容量仍能正确换算 1TB = 1000^4
</code_context>
<issue_to_address>
**Overflow assertion accepts undefined result**
When the unchecked floating-point-to-`quint64` conversion produces zero, when the unchecked out-of-range cast yields `0` on the test platform, `UT_DeviceStorage_convertToBytes_overflow_383515` passes its assertion at line 417 even though the range guard is absent and the cast has undefined behavior.
Run this case with a sanitizer that instruments floating-point cast overflow, or otherwise assert that the range-check path was taken rather than relying on the converted value.
</issue_to_address>| } | ||
| diskBytesSize = static_cast<quint64>(diskSizeFloat * multiplier); | ||
| double result = diskSizeFloat * multiplier; | ||
| if (result < 0.0 || result > static_cast<double>(std::numeric_limits<quint64>::max())) { |
There was a problem hiding this comment.
🟡 Medium · Oversized capacities reach undefined conversion
When the size and unit produce a double result of exactly 2^64, such as 16 EB with the 1024 scale, static_cast<double>(quint64::max()) rounds up to 2^64, so the range check accepts that result and static_cast<quint64>(result) converts an out-of-range value, causing undefined behavior instead of clamping it.
Reject results greater than or equal to 2^64 before converting them to quint64.
Prompt for AI agents
In `deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp` at line 157:
**Oversized capacities reach undefined conversion**
When the size and unit produce a `double` result of exactly 2^64, such as `16 EB` with the 1024 scale, `static_cast<double>(quint64::max())` rounds up to 2^64, so the range check accepts that result and `static_cast<quint64>(result)` converts an out-of-range value, causing undefined behavior instead of clamping it.
Reject results greater than or equal to 2^64 before converting them to `quint64`.| int width = re.cap(1).toInt(); | ||
| int height = re.cap(2).toInt(); | ||
| if (width == 0 || height == 0) { | ||
| return; |
There was a problem hiding this comment.
🟡 Medium · Old display ratio remains visible
When a monitor with a calculated ratio receives a zero-dimension resolution, caculateScreenRatio() returns without clearing m_AspectRatio, so callers and loadOtherDeviceInfo() continue to expose or persist the previous resolution’s ratio as current display data.
Clear m_AspectRatio before returning from the zero-dimension guard.
Also at deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp:496.
Prompt for AI agents
In `deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp` at line 497:
**Old display ratio remains visible**
When a monitor with a calculated ratio receives a zero-dimension resolution, `caculateScreenRatio()` returns without clearing `m_AspectRatio`, so callers and `loadOtherDeviceInfo()` continue to expose or persist the previous resolution’s ratio as current display data.
Clear `m_AspectRatio` before returning from the zero-dimension guard.
Also at `deepin-devicemanager/src/DeviceManager/DeviceMonitor.cpp:496`.| double result = diskSizeFloat * multiplier; | ||
| if (result < 0.0 || result > static_cast<double>(std::numeric_limits<quint64>::max())) { | ||
| qCDebug(appLog) << "convertToBytes, result out of range, clamping to 0"; | ||
| return 0; |
There was a problem hiding this comment.
🟡 Medium · Displayed capacity contradicts bytes
When getInfoFromLshw() receives an oversized capacity that convertToBytes() rejects, getInfoFromLshw() keeps the raw oversized size in m_Size while convertToBytes() sets m_SizeBytes to zero, so the device can be shown as having an enormous capacity while numeric consumers treat it as zero.
Keep the displayed size consistent with the rejected byte count, or reject the capacity/device instead of retaining the unrepresentable size.
Prompt for AI agents
In `deepin-devicemanager/src/DeviceManager/DeviceStorage.cpp` at line 159:
**Displayed capacity contradicts bytes**
When `getInfoFromLshw()` receives an oversized capacity that `convertToBytes()` rejects, `getInfoFromLshw()` keeps the raw oversized size in `m_Size` while `convertToBytes()` sets `m_SizeBytes` to zero, so the device can be shown as having an enormous capacity while numeric consumers treat it as zero.
Keep the displayed size consistent with the rejected byte count, or reject the capacity/device instead of retaining the unrepresentable size.| EXPECT_EQ(0, m_deviceMonitor->gcd(0, 0)); | ||
| EXPECT_EQ(100, m_deviceMonitor->gcd(0, 100)); | ||
| EXPECT_EQ(100, m_deviceMonitor->gcd(100, 0)); | ||
| EXPECT_EQ(-5, m_deviceMonitor->gcd(-5, 0)); | ||
| EXPECT_EQ(1920, m_deviceMonitor->gcd(0, 1920)); | ||
|
|
||
| // 正常输入结果不变 | ||
| EXPECT_EQ(120, m_deviceMonitor->gcd(1920, 1080)); | ||
| EXPECT_EQ(10, m_deviceMonitor->gcd(10, 10)); | ||
| EXPECT_EQ(1, m_deviceMonitor->gcd(10, 11)); | ||
| } | ||
|
|
||
| // PMS BUG-383515 用例:负数参数会导致无限递归/取模溢出 | ||
| TEST_F(UT_DeviceMonitorFuzz, UT_DeviceMonitorFuzz_gcd_negative_383515) | ||
| { | ||
| EXPECT_EQ(4, m_deviceMonitor->gcd(-8, 12)); | ||
| EXPECT_EQ(4, m_deviceMonitor->gcd(8, -12)); | ||
| EXPECT_EQ(4, m_deviceMonitor->gcd(-8, -12)); | ||
| } | ||
|
|
||
| // PMS BUG-383515 用例:INT_MIN 取反与 a % b 属于未定义行为 | ||
| TEST_F(UT_DeviceMonitorFuzz, UT_DeviceMonitorFuzz_gcd_extremum_383515) | ||
| { | ||
| const int intMin = std::numeric_limits<int>::min(); | ||
|
|
||
| EXPECT_EQ(1, m_deviceMonitor->gcd(intMin, -1)); | ||
| EXPECT_EQ(1, m_deviceMonitor->gcd(-1, intMin)); | ||
| EXPECT_EQ(std::numeric_limits<int>::max(), m_deviceMonitor->gcd(intMin, intMin)); | ||
| EXPECT_EQ(intMin, m_deviceMonitor->gcd(intMin, 0)); | ||
| } | ||
|
|
||
| // PMS BUG-383515 用例:分辨率为 0x0 时 caculateScreenRatio() 以零参数调用 gcd() | ||
| TEST_F(UT_DeviceMonitorFuzz, UT_DeviceMonitorFuzz_caculateScreenRatio_zero_383515) | ||
| { | ||
| m_deviceMonitor->m_AspectRatio = ""; | ||
| m_deviceMonitor->m_CurrentResolution = "0x0"; | ||
| m_deviceMonitor->caculateScreenRatio(); |
There was a problem hiding this comment.
🔴 Critical · Regression tests fail to compile
When the test executable is built, the test target compiles these added tests, but they directly call private methods and access private fields without a test-access declaration or visibility workaround, so compilation fails.
Use public entry points or add an explicit test-access mechanism for the private methods and fields used by these tests.
Also at deepin-devicemanager-server/tests/src/LoadInfo/ut_ThreadPoolTask.cpp:84-87.
Prompt for AI agents
In `deepin-devicemanager/tests/src/DeviceManager/ut_fuzz_383515.cpp` at lines 42-78:
**Regression tests fail to compile**
When the test executable is built, the test target compiles these added tests, but they directly call private methods and access private fields without a test-access declaration or visibility workaround, so compilation fails.
Use public entry points or add an explicit test-access mechanism for the private methods and fields used by these tests.
Also at `deepin-devicemanager-server/tests/src/LoadInfo/ut_ThreadPoolTask.cpp:84-87`.| int i; | ||
| int v = 0; | ||
| for (i = 0; i < strHex.length(); i++) { | ||
| qint64 v = 0; |
There was a problem hiding this comment.
🟡 Medium · Large hex-to-binary conversions collapse
When hexToBin() receives a hexadecimal value whose decimal representation exceeds INT_MAX, hexToDec() now returns values above INT_MAX, but hexToBin() passes that decimal string to decToBin(), whose toInt() fails on those values and yields zero; the binary conversion therefore returns 0000 for inputs such as FFFFFFFF.
Make decToBin() accept a 64-bit value, or implement hex-to-binary conversion directly without narrowing through int.
Prompt for AI agents
In `deepin-devicemanager/src/Tool/EDIDParser.cpp` at line 461:
**Large hex-to-binary conversions collapse**
When `hexToBin()` receives a hexadecimal value whose decimal representation exceeds `INT_MAX`, `hexToDec()` now returns values above `INT_MAX`, but `hexToBin()` passes that decimal string to `decToBin()`, whose `toInt()` fails on those values and yields zero; the binary conversion therefore returns `0000` for inputs such as `FFFFFFFF`.
Make `decToBin()` accept a 64-bit value, or implement hex-to-binary conversion directly without narrowing through `int`.| if (len > 15) { | ||
| len = 15; | ||
| } |
There was a problem hiding this comment.
🟡 Medium · Long hex strings lose trailing digits
When a hexadecimal input longer than 15 characters has significant digits after the first 15, hexToDec() processes only the first 15 characters, so a valid 16-character value such as 000000000000000F converts to zero instead of 15. The length cap silently changes the represented value even when the input fits in qint64.
Handle the entire input while checking for overflow, or reject values that cannot be represented rather than silently dropping trailing digits.
Prompt for AI agents
In `deepin-devicemanager/src/Tool/EDIDParser.cpp` at lines 463-465:
**Long hex strings lose trailing digits**
When a hexadecimal input longer than 15 characters has significant digits after the first 15, `hexToDec()` processes only the first 15 characters, so a valid 16-character value such as `000000000000000F` converts to zero instead of 15. The length cap silently changes the represented value even when the input fits in `qint64`.
Handle the entire input while checking for overflow, or reject values that cannot be represented rather than silently dropping trailing digits.| m_deviceStorage->m_Size = "999999999999999999999999999999 GB"; | ||
| m_deviceStorage->m_SizeBytes = 999999999999999999ULL; | ||
| EXPECT_NO_FATAL_FAILURE(m_deviceStorage->checkDiskSize()); |
There was a problem hiding this comment.
🟡 Medium · Oversized-size test misses the input
When the regression suite runs the oversized-size case, checkDiskSize() bases its comparisons on m_SizeBytes and never reads the oversized m_Size string, while the test only checks for fatal failures; it therefore passes without exercising the claimed oversized-string handling.
Exercise the oversized string through the code that parses it and assert the expected handling.
Prompt for AI agents
In `deepin-devicemanager/tests/src/DeviceManager/ut_devicestorage.cpp` at lines 431-433:
**Oversized-size test misses the input**
When the regression suite runs the oversized-size case, `checkDiskSize()` bases its comparisons on `m_SizeBytes` and never reads the oversized `m_Size` string, while the test only checks for fatal failures; it therefore passes without exercising the claimed oversized-string handling.
Exercise the oversized string through the code that parses it and assert the expected handling.| mapinfo.insert("size", "1ZB (1ZB)"); | ||
| m_deviceStorage->getInfoFromLshw(mapinfo); | ||
| // 超出 quint64 表示范围,钳位为 0,不再产生未定义行为 | ||
| EXPECT_EQ(0ULL, m_deviceStorage->m_SizeBytes); |
There was a problem hiding this comment.
🟡 Medium · Overflow assertion accepts undefined result
When the unchecked floating-point-to-quint64 conversion produces zero, when the unchecked out-of-range cast yields 0 on the test platform, UT_DeviceStorage_convertToBytes_overflow_383515 passes its assertion at line 417 even though the range guard is absent and the cast has undefined behavior.
Run this case with a sanitizer that instruments floating-point cast overflow, or otherwise assert that the range-check path was taken rather than relying on the converted value.
Prompt for AI agents
In `deepin-devicemanager/tests/src/DeviceManager/ut_devicestorage.cpp` at line 417:
**Overflow assertion accepts undefined result**
When the unchecked floating-point-to-`quint64` conversion produces zero, when the unchecked out-of-range cast yields `0` on the test platform, `UT_DeviceStorage_convertToBytes_overflow_383515` passes its assertion at line 417 even though the range guard is absent and the cast has undefined behavior.
Run this case with a sanitizer that instruments floating-point cast overflow, or otherwise assert that the range-check path was taken rather than relying on the converted value.|
[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) |
8059b31
into
linuxdeepin:develop/eagle
Influence:
fix: 修复设备管理器5个模糊测试安全问题
Influence:
PMS: BUG-383515
Summary by Sourcery
Harden device-manager input handling against five fuzz-discovered security issues and add regression coverage for the affected paths.
Bug Fixes:
Enhancements:
Tests: