Skip to content

fix(encodes): fix heap buffer overflow read in detectCharset - #756

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/384151-charset-oob-read-master
Nov 30, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/384151-charset-oob-read-master

Conversation

@pppanghu77

@pppanghu77 pppanghu77 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

修复编码探测 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-overflow READ of size 1,恰在分配区末尾之后)。等价最小输入为以 ESC 结尾的任意文本(如单字节 \x1B 即可复现同签名越界)。

修复方式:

  • NUL 指针扫描改为按 byteArray.size() 的有界索引循环,每处字节读取前补 pos < size 边界检查
  • 不再依赖隐式终止符;ISO-2022 各分支的字符集判定与扫描终止行为保持不变
  • 移除开头对 *text 的死读取(空 raw 缓冲保护)

本 PR 为 master(V25)分支修复,与 release/eagle 的 #755 同源同内容(#753/#754 为同批 384159 单的负值左移修复,互不重叠)。

验证:

  • 等价复现(模拟 QArrayData 堆布局 + NUL 结尾):修复前 ASan 报 heap-buffer-overflow READ 1 字节(与提单同签名);修复后 12 组语料全部干净且输出逐行一致
  • V25 构建机(10.8.15.56 x86_64)编译通过,DetectCode 相关单测 73/73 PASSED

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:

  • Prevent heap-buffer-overflow reads in charset detection for truncated input, including data ending with an ESC byte.

Enhancements:

  • Make charset detection scan QByteArray contents using explicit size bounds rather than relying on implicit NUL termination.

@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Bound detectCharset’s input scan and ISO-2022 escape reads by the QByteArray size to prevent out-of-bounds reads.

  • src/common/encoding.cpp L766-774 (+4 small edits)

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/common/encoding.cpp
{
qCDebug(appLog) << "validateUTF8 is true";
while ((c = *text++) != '\0')
while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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`.

Comment thread src/common/encoding.cpp
{
qCDebug(appLog) << "validateUTF8 is true";
while ((c = *text++) != '\0')
while (pos < size && (c = static_cast<uint8_t>(text[pos++])) != '\0')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread src/common/encoding.cpp
const char* text = byteArray.constData();
uint8_t c = *text;
const qsizetype size = byteArray.size();
int pos = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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`.

@deepin-bot

deepin-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.11
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #757

- 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
@pppanghu77
pppanghu77 force-pushed the fix/384151-charset-oob-read-master branch from 15310d4 to d9a0c76 Compare November 30, 2026 08:03
@deepin-ci-robot

Copy link
Copy Markdown

[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.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@pppanghu77

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 4a16a5d into linuxdeepin:master Nov 30, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants