Repository navigation
fix(encodes): fix heap buffer overflow read in detectCharset - #756
deepin-bot[bot] merged 1 commit into
Conversation
Reviewer's GuideImplementationBound detectCharset’s input scan and ISO-2022 escape reads by the QByteArray size to prevent out-of-bounds reads.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/common/encoding.cpp" line_range="755" />
<code_context>
{
qCDebug(appLog) << "validateUTF8 is true";
- while ((c = *text++) != '\0')
+ while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0')
{
if (c > 0x7F)
</code_context>
<issue_to_address>
**Malformed input reads past buffer**
When `validateUTF8` returns false and a locale-specific fallback scanner receives a truncated multibyte sequence, the locale-specific fallback scanners consume the implicit terminator and then read past the `QByteArray` allocation, so charset detection can crash.
Bound reads in every locale-specific fallback scanner by the `QByteArray` size.
Also at `src/common/encoding.cpp:747-748`.
</issue_to_address>
### Comment 2
<location path="src/common/encoding.cpp" line_range="755" />
<code_context>
{
qCDebug(appLog) << "validateUTF8 is true";
- while ((c = *text++) != '\0')
+ while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0')
{
if (c > 0x7F)
</code_context>
<issue_to_address>
**Raw byte arrays still overread input**
When the caller passes a `QByteArray::fromRawData` whose backing bytes have no NUL terminator, `detectCharset` calls `validateUTF8` before this bounded scan, and `validateUTF8` reads `constData()` until it finds a NUL without checking the array size. A non-NUL-terminated `QByteArray::fromRawData` therefore causes an out-of-bounds read before this loop runs.
Make `validateUTF8` scan by length and check the remaining length before reading UTF-8 continuation bytes.
</issue_to_address>
### Comment 3
<location path="src/common/encoding.cpp" line_range="748" />
<code_context>
const char* text = byteArray.constData();
- uint8_t c = *text;
+ const qsizetype size = byteArray.size();
+ int pos = 0;
+ uint8_t c = 0;
std::string charset;
</code_context>
<issue_to_address>
**Large inputs corrupt scan position**
When a valid input exceeds `INT_MAX` bytes and the scan does not stop earlier, `pos` overflows its `int` range while the loop compares it with the larger `size`, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.
Declare `pos` as `qsizetype` so it can represent the full range of `size`.
</issue_to_address>| { | ||
| qCDebug(appLog) << "validateUTF8 is true"; | ||
| while ((c = *text++) != '\0') | ||
| while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0') |
There was a problem hiding this comment.
🟡 Medium · Malformed input reads past buffer
When validateUTF8 returns false and a locale-specific fallback scanner receives a truncated multibyte sequence, the locale-specific fallback scanners consume the implicit terminator and then read past the QByteArray allocation, so charset detection can crash.
Bound reads in every locale-specific fallback scanner by the QByteArray size.
Also at src/common/encoding.cpp:747-748.
Prompt for AI agents
In `src/common/encoding.cpp` at line 755:
**Malformed input reads past buffer**
When `validateUTF8` returns false and a locale-specific fallback scanner receives a truncated multibyte sequence, the locale-specific fallback scanners consume the implicit terminator and then read past the `QByteArray` allocation, so charset detection can crash.
Bound reads in every locale-specific fallback scanner by the `QByteArray` size.
Also at `src/common/encoding.cpp:747-748`.| { | ||
| qCDebug(appLog) << "validateUTF8 is true"; | ||
| while ((c = *text++) != '\0') | ||
| while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0') |
There was a problem hiding this comment.
🟡 Medium · Raw byte arrays still overread input
When the caller passes a QByteArray::fromRawData whose backing bytes have no NUL terminator, detectCharset calls validateUTF8 before this bounded scan, and validateUTF8 reads constData() until it finds a NUL without checking the array size. A non-NUL-terminated QByteArray::fromRawData therefore causes an out-of-bounds read before this loop runs.
Make validateUTF8 scan by length and check the remaining length before reading UTF-8 continuation bytes.
Prompt for AI agents
In `src/common/encoding.cpp` at line 755:
**Raw byte arrays still overread input**
When the caller passes a `QByteArray::fromRawData` whose backing bytes have no NUL terminator, `detectCharset` calls `validateUTF8` before this bounded scan, and `validateUTF8` reads `constData()` until it finds a NUL without checking the array size. A non-NUL-terminated `QByteArray::fromRawData` therefore causes an out-of-bounds read before this loop runs.
Make `validateUTF8` scan by length and check the remaining length before reading UTF-8 continuation bytes.| const char* text = byteArray.constData(); | ||
| uint8_t c = *text; | ||
| const qsizetype size = byteArray.size(); | ||
| int pos = 0; |
There was a problem hiding this comment.
🟡 Medium · Large inputs corrupt scan position
When a valid input exceeds INT_MAX bytes and the scan does not stop earlier, pos overflows its int range while the loop compares it with the larger size, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.
Declare pos as qsizetype so it can represent the full range of size.
Prompt for AI agents
In `src/common/encoding.cpp` at line 748:
**Large inputs corrupt scan position**
When a valid input exceeds `INT_MAX` bytes and the scan does not stop earlier, `pos` overflows its `int` range while the loop compares it with the larger `size`, so subsequent indexing reads outside the buffer and charset detection can crash or behave incorrectly.
Declare `pos` as `qsizetype` so it can represent the full range of `size`.|
TAG Bot New tag: 6.6.11 |
- Replace the NUL-terminator pointer scan in detectCharset with a size-bounded index loop; the ESC branch could consume the terminator via unbounded reads and then read one byte beyond the heap buffer (CWE-125, reported as heap-buffer-overflow-read by ASan) - Add bounds checks before every byte read in the ISO-2022 escape handling; charset detection results stay identical for all in-bounds inputs - Drop the dead initial read of *text which could touch an empty raw buffer 修复(encodes): 修复 detectCharset 堆缓冲区越界读 - detectCharset 由 NUL 终止符指针扫描改为按 size 的有界索引循环;原 ESC 分支可能经无界读取耗尽终止符后越过堆缓冲区尾再读 1 字节(CWE-125,ASan 报 heap-buffer-overflow-read) - ISO-2022 转义处理每处字节读取前补边界检查;所有合法输入的字符集判定结果保持不变 - 移除开头对 *text 的死读取,避免触碰空 raw 缓冲 Log: 修复编码探测 detectCharset 因 NUL 扫描与 ESC 转义分支无界读导致的堆缓冲区越界读(CWE-125) Bug: https://pms.uniontech.com/bug-view-384151.html PMS: BUG-384151
15310d4 to
d9a0c76
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: lzwind, pppanghu77 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 |
|
/merge |
修复编码探测 detectCharset 堆缓冲区越界读(CWE-125)
WingFuzz 模糊测试(ASan 插桩)发现:
detectCharset(QByteArray const&)(src/common/encoding.cpp)对输入做while ((c = *text++) != '\0')NUL 扫描,且 ISO-2022 ESC 转义分支存在多处无界*text++。当输入以 ESC(0x1B)结尾时,ESC 分支先消耗掉 QByteArray 隐式终止符,外层循环继续解引用即越过堆缓冲区尾部(ASan:heap-buffer-overflowREAD of size 1,恰在分配区末尾之后)。等价最小输入为以 ESC 结尾的任意文本(如单字节\x1B即可复现同签名越界)。修复方式:
byteArray.size()的有界索引循环,每处字节读取前补pos < size边界检查*text的死读取(空 raw 缓冲保护)本 PR 为 master(V25)分支修复,与 release/eagle 的 #755 同源同内容(#753/#754 为同批 384159 单的负值左移修复,互不重叠)。
验证:
heap-buffer-overflowREAD 1 字节(与提单同签名);修复后 12 组语料全部干净且输出逐行一致fix(encodes): fix heap buffer overflow read in detectCharset
Log: 修复编码探测 detectCharset 因 NUL 扫描与 ESC 转义分支无界读导致的堆缓冲区越界读(CWE-125)
Bug: https://pms.uniontech.com/bug-view-384151.html
PMS: BUG-384151
Summary by Sourcery
Fix detectCharset to safely handle truncated byte sequences without reading beyond the input buffer.
Bug Fixes:
Enhancements: