Signed overflow to heap out of bounds write in dynamic IO context (appendDynamic)

オープン 初心者向け
#1,053 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
74/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
backend, security

調査の方向性

src/gd_io_dp.cから始め、特に359行目付近のappendDynamic()と、報告に記載されているdynamicSeek()の動作を確認してください。bytesNeededの計算方法とバッファを拡張するかどうかの判断方法を調べてから、UBSanまたはASanで再現コードを実行してください。INT_MAXの近くへのシークと16バイトの書き込みで、オーバーフローやバッファ外への書き込みが発生しなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

appendDynamic() in the dynamic IO context has a signed integer overflow that leads to a heap out of bounds write.

Location: src/gd_io_dp.c:359. bytesNeeded = dp->pos + size overflows signed int when pos is near INT_MAX. The grow check at line 361 (bytesNeeded > dp->realSize) then passes with a negative value, the realloc is skipped, and line 383 does memcpy(tmp + dp->pos, src, size) about 2 GB past a small heap buffer.

Root cause: dynamicSeek() (lines 177-221) only grows the buffer once (realSize * 2) when seeking past EOF, then happily sets pos and logicalSize to the huge value.

Reproduce (needs gdNewDynamicCtx(1024, NULL), seek to 2147483640, write 16 bytes):

gdIOCtx *ctx = gdNewDynamicCtx(1024, NULL);
ctx->seek(ctx, 2147483640);
ctx->putBuf(ctx, buf, 16);

Confirmed: UBSan reports signed integer overflow: 16 + 2147483640 at gd_io_dp.c:359. An ASan-only build segfaults on WRITE in memcpy from appendDynamic (gd_io_dp.c:383) via dynamicPutbuf.

To be clear about the scope: this is NOT reachable from a crafted image file through libgd's own decoders or encoders. Libgd's own seeks on a dynamic context are small constants (BMP header patch-ups) or image derived GD2 chunk offsets that cannot approach INT_MAX without a multi-GB image already in memory. It needs a caller that seeks a dynamic context from untrusted offsets.

Severity is low. The write offset is always about 2 GB past the buffer, so in practice it crashes rather than giving controlled heap corruption. appendDynamic should check pos > INT_MAX - size before adding. Tested against libgd 2.3.3.

主要言語
C
スター
985
フォーク
289
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

libgd/libgd のほかの issue

libgd/libgd の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。