Heap buffer overflow in JS_ReadFunctionTag when cpool_count overflows function_size
Los mantenedores suelen responder en 2 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 48/100
Línea de trabajo
Start in quickjs.c at JS_ReadFunctionTag around the reported size computation and inspect free_function_bytecode, which releases the constant pool on failure. Check how bytecode counts and allocation sizes are validated elsewhere, then run the existing bytecode reader tests with the supplied reproducer. Done means malformed input is rejected without overflow or an out-of-bounds read.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
JS_ReadFunctionTag uses attacker-controlled cpool_count, local_count and byte_code_len directly in an int size computation, with no validation, then allocates that size and later walks cpool_count entries.
plaintext
// quickjs.c:39704
if (bc_get_leb128_int(s, &bc.cpool_count)) goto fail;
if (bc_get_leb128_int(s, &bc.byte_code_len)) goto fail;
if (bc_get_leb128_int(s, &local_count)) goto fail;
function_size = cpool_offset + bc.cpool_count * sizeof(*bc.cpool); // int overflow
function_size += local_count * sizeof(*bc.vardefs); // int overflow
function_size += bc.byte_code_len; // int overflow
b = js_mallocz(ctx, function_size);
sizeof(JSValue) is 16, so cpool_count > 134,217,728 wraps the multiplication. cpool_count = 0x10000001 gives * 16 = 16: a 16-byte allocation, while b->cpool_count stays at 268 million.
On the error path free_function_bytecode() then walks the full range:
plaintext
for (i = 0; i < b->cpool_count; i++)
JS_FreeValueRT(rt, b->cpool[i]); // past the end of the block
Reproducer:
plaintext
#include <stdio.h>
#include "quickjs.h"
int main(void) {
unsigned char buf[1 << 20];
size_t n = fread(buf, 1, sizeof(buf), stdin);
if (!n) return 0;
JSRuntime *rt = JS_NewRuntime();
JSContext *ctx = JS_NewContext(rt);
JSValue v = JS_ReadObject(ctx, buf, n, JS_READ_OBJ_BYTECODE);
if (JS_IsException(v)) { JSValue e = JS_GetException(ctx); JS_FreeValue(ctx, e); }
JS_FreeValue(ctx, v);
JS_FreeContext(ctx); JS_FreeRuntime(rt);
return 0;
}
65-byte input:
plaintext
1d ff ff ff ff 01 01 13 30 30 30 30 30 30 30 30
30 30 30 30 30 30 30 30 30 30 16 30 0d 01 01 01
01 00 30 30 01 30 01 30 30 30 30 07 07 30 30 30
30 30 30 0c 30 30 30 30 30 30 30 30 30 30 30 30
30
ff ff ff ff at offset 1 is the documented UINT32_MAX checksum escape hatch, so the checksum does not need to be recomputed.
Result:
plaintext
==165748==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7abb0bde0980
READ of size 4
#0 free_function_bytecode quickjs.c:37168:45
#1 free_gc_object quickjs.c:7104:9
#2 free_zero_refcount quickjs.c:7123:9
#3 js_free_value_rt quickjs.c:7169:21
#6 JS_ReadFunctionTag quickjs.c:39938:5
#7 JS_ReadObjectRec quickjs.c:40414:15
#8 js_map_read quickjs.c:54055:19
#12 JS_ReadObject quickjs.c:40617:12
Also reproduces as a plain SIGSEGV (exit 139) on a -O2 build with no sanitizers, so it is not an ASAN artifact.
Tested on commit 7a23d8e (0.17.0), x86-64 Linux.
Suggested fix: validate before the size computation and do the arithmetic in size_t with overflow checks:
plaintext
if (bc.cpool_count < 0 || bc.cpool_count > JS_MAX_CPOOL_COUNT ||
local_count < 0 || local_count > JS_MAX_LOCAL_COUNT ||
bc.byte_code_len < 0 || bc.byte_code_len > JS_MAX_BYTECODE_LEN)
goto fail;
and free only the cpool entries actually initialised (the buffer is js_mallocz'd, so track the filled count or clamp b->cpool_count before any goto fail that reaches the destructor).
I know bytecode is out of scope per SECURITY.md — filing as a plain bug report, not a security report.
- Lenguaje dominante
- C
- Estrellas
- 3.9k
- Forks
- 389
- Merge medio
- 2 d 9 h
- PR fusionados (30 d)
- 19
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de quickjs-ng/quickjs
-
JS_SetPropertyInternal2 loses the value when a lazy property on the prototype runs out of memoryAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
quickjs-ng/quickjs#1852 ·
Los mantenedores suelen responder en 2 días
-
Array.prototype.find frees an element twice when interrupted between iterationsPosiblemente ocupada @josefguenther la tomó hace 23 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 73/100
quickjs-ng/quickjs#1792 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
Object.prototype.propertyIsEnumerable converts `this` before the property keyPosiblemente ocupada @SomSamantray la tomó hace 19 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
quickjs-ng/quickjs#1772 ·
Los mantenedores suelen responder en 2 días
-
Deserialization corrupts data or returns `undefined` when a RegExp precedes a shared object referencePosiblemente ocupada @Sree24-ui la tomó hace 22 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
quickjs-ng/quickjs#1771 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
More native loops never consult the interrupt handler (TypedArray, String, JSON, change-array-by-copy, and some harder cases)Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
quickjs-ng/quickjs#1856 ·
Los mantenedores suelen responder en 2 días
Todos los issues de quickjs-ng/quickjs
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 70/100
ly05010419/MacNativeGames#1340 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
-
area:stdlib bug found-by:cold-read kind:bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 83/100
InauguralSystems/EigenScript#2197 ·
Los mantenedores suelen responder en 1 día
-
Hide the cursorAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 63/100
seedds/nxnative#675 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
Los mantenedores suelen responder en 2 días