JS_GetTypedArrayBuffer / JS_GetUint8Array report a stale length for length-tracking views after ArrayBuffer.prototype.resize()

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

メンテナーはふだん 1 日以内に返信

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

評価

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

調査の方向性

Start in quickjs.c at JS_GetTypedArrayBuffer and JS_GetUint8Array, then compare their length handling with js_typed_array_get_byteLength() and js_array_buffer_update_typed_arrays(). Reproduce with the proposed api-test.c case: resize a buffer after creating length-tracking views and check the reported lengths. Done when both APIs report the views' current lengths after resizing, including the offset Uint16Array case.

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

説明

(found when fuzzing quickjab; agentic)


Version: quickjs-ng master at e22ce4d; also v0.16.2.

Summary: for a length-tracking typed array, new Uint8Array(rab) over a resizable ArrayBuffer, both C API calls keep returning the length from when the view was created after the buffer is shrunk. Script code sees the correct byteLength. An embedder that trusts the API reads or writes past the end of the shrunk buffer; in our embedder this showed up as an ASan heap-buffer-overflow.

Repro: a test for api-test.c.
static void typed_array_track_rab_length(void)
{
JSValue u8, u16, ret;
size_t off, len, bpe;
uint8_t *p;

JSRuntime *rt = new_runtime();
JSContext *ctx = JS_NewContext(rt);
ret = eval(ctx, "ab = new ArrayBuffer(64, {maxByteLength: 1 << 20});"
                "u8 = new Uint8Array(ab); u16 = new Uint16Array(ab, 2);"
                "ab.resize(7)");
assert(!JS_IsException(ret));
JS_FreeValue(ctx, ret);
u8 = eval(ctx, "u8");
u16 = eval(ctx, "u16");

ret = JS_GetTypedArrayBuffer(ctx, u8, &off, &len, &bpe);
assert(!JS_IsException
JS_FreeValue(ctx, ret)
assert(off == 0 && len == 7 && bpe == 1);   /* fails: len == 64 */
p = JS_GetUint8Array(c
assert(p);
assert(len == 7);     s: len == 64 */

ret = JS_GetTypedArray&bpe);
assert(!JS_IsException
JS_FreeValue(ctx, ret)
assert(off == 2 && lens: len == 62 */

JS_FreeValue(ctx, u8);
JS_FreeValue(ctx, u16)
JS_FreeContext(ctx);
JS_FreeRuntime(rt);

}

Cause:

  • Both functions return ta->length, the byte length when the view was created.
  • On resize, js_array_buffer_update_typed_arrays() refreshes ta->length only for
    DataView. For typed arrat and leaves ta->lengthalone.
  • js_typed_array_get_byteLength() gets it right: for track_rab it uses count << size_log2.

Proposed fix:
--- a/quickjs.c (JS_GetTypedArrayBuffer)

  • if (pbyte_length)
  •    *pbyte_length = t
    
  • if (pbyte_length)
  •    *pbyte_length = (size_t)p->u.array.count <<
    

typed_array_size_log2(p->c
--- a/quickjs.c (JS_GetUint8Array)

  • *psize = ta->length;
  • *psize = p->u.array.c
  • Fixed-length views are unchanged: once typed_array_is_oob() is false, count == ta->length >> size_log2.
  • Tested: with the fix, apn+UBSan, and run-test262 -c tests.conf reports 0 errors.

Possibly related: the inteo reads ta->length; it mayneed the same treatment.

Found by: fuzzing an embedder (quickjab) with libFuzzer + ASan.

主要言語
C
スター
3.9k
フォーク
389
平均マージ
4日 5時間
マージ済み PR(30日)
16

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

quickjs-ng/quickjs のほかの issue

quickjs-ng/quickjs の issue をすべて見る

似ている issue

C の issue をもっと見る

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

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