]> git.kaiwu.me - njs.git/commitdiff
FS: fetch buffer data after argument coercion in read() and write()
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 2 Dec 2026 00:18:20 +0000 (16:18 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 3 Dec 2026 01:27:35 +0000 (17:27 -0800)
Previously, qjs_fs_read() and qjs_fs_write() obtained the buffer pointer
and length before converting the offset, length, and position
arguments.  Because converting those arguments can invoke user code,
such as valueOf(), which can detach or transfer the underlying
ArrayBuffer, the subsequent bounds checks and system calls could access
freed memory.

The njs counterparts, njs_fs_read() and njs_fs_write(), had the same
ordering.  The njs engine does not free the storage of a detached
ArrayBuffer, so there it resulted in using a detached buffer rather
than in a memory safety issue.

Now, all the arguments are converted before the buffer data is fetched,
as in 640d5ab8.

The QuickJS issue was introduced in 1f8f9992 (0.8.9).

external/njs_fs_module.c
external/qjs_fs_module.c
test/fs/methods.t.mjs

index 6f1db88c90fc0e880ea62a24071d87d27c66101d..bf00a828831ebb115c32f6295992e1251fe1d20d 100644 (file)
@@ -1695,6 +1695,7 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     ssize_t             n;
     njs_int_t           ret;
     njs_str_t           data;
+    njs_bool_t          has_length;
     njs_uint_t          fd_offset;
     njs_value_t         *buffer, *value;
     njs_opaque_value_t  result;
@@ -1714,10 +1715,6 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
      */
 
     buffer = njs_arg(args, nargs, fd_offset + 1);
-    ret = njs_value_buffer_get(vm, buffer, &data);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return NJS_ERROR;
-    }
 
     ret = njs_value_to_integer(vm, njs_arg(args, nargs, fd_offset + 2),
                                &offset);
@@ -1725,14 +1722,8 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         return ret;
     }
 
-    if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
-        njs_vm_range_error(vm, "offset is out of range (must be <= %z)",
-                           data.length);
-        return NJS_ERROR;
-    }
-
-    data.length -= offset;
-    data.start += offset;
+    has_length = 0;
+    length = 0;
 
     value = njs_arg(args, nargs, fd_offset + 3);
 
@@ -1742,13 +1733,7 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
             return ret;
         }
 
-        if (njs_slow_path(length < 0 || (size_t) length > data.length)) {
-            njs_vm_range_error(vm, "length is out of range (must be <= %z)",
-                               data.length);
-            return NJS_ERROR;
-        }
-
-        data.length = length;
+        has_length = 1;
     }
 
     value = njs_arg(args, nargs, fd_offset + 4);
@@ -1760,6 +1745,30 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
     }
 
+    ret = njs_value_buffer_get(vm, buffer, &data);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
+        njs_vm_range_error(vm, "offset is out of range (must be <= %z)",
+                           data.length);
+        return NJS_ERROR;
+    }
+
+    data.length -= offset;
+    data.start += offset;
+
+    if (has_length) {
+        if (njs_slow_path(length < 0 || (size_t) length > data.length)) {
+            njs_vm_range_error(vm, "length is out of range (must be <= %z)",
+                               data.length);
+            return NJS_ERROR;
+        }
+
+        data.length = length;
+    }
+
     if (pos == -1) {
         n = read(fd, data.start, data.length);
 
@@ -2569,6 +2578,7 @@ njs_fs_write(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     ssize_t                      n;
     njs_int_t                    ret;
     njs_str_t                    data;
+    njs_bool_t                   has_length;
     njs_uint_t                   fd_offset;
     njs_value_t                  *buffer, *value;
     njs_opaque_value_t           result;
@@ -2623,25 +2633,14 @@ njs_fs_write(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
      * fs.writeSync(fd, buffer, offset[, length[, position]])
      */
 
-    ret = njs_vm_value_to_bytes(vm, &data, buffer);
-    if (njs_slow_path(ret != NJS_OK)) {
-        return NJS_ERROR;
-    }
-
     ret = njs_value_to_integer(vm, njs_arg(args, nargs, fd_offset + 2),
                                &offset);
     if (njs_slow_path(ret != NJS_OK)) {
         return ret;
     }
 
-    if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
-        njs_vm_range_error(vm, "offset is out of range (must be <= %z)",
-                           data.length);
-        return NJS_ERROR;
-    }
-
-    data.length -= offset;
-    data.start += offset;
+    has_length = 0;
+    length = 0;
 
     value = njs_arg(args, nargs, fd_offset + 3);
 
@@ -2651,13 +2650,7 @@ njs_fs_write(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
             return ret;
         }
 
-        if (njs_slow_path(length < 0 || (size_t) length > data.length)) {
-            njs_vm_range_error(vm, "length is out of range (must be <= %z)",
-                               data.length);
-            return NJS_ERROR;
-        }
-
-        data.length = length;
+        has_length = 1;
     }
 
     value = njs_arg(args, nargs, fd_offset + 4);
@@ -2669,6 +2662,30 @@ njs_fs_write(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         }
     }
 
+    ret = njs_vm_value_to_bytes(vm, &data, buffer);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
+    if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
+        njs_vm_range_error(vm, "offset is out of range (must be <= %z)",
+                           data.length);
+        return NJS_ERROR;
+    }
+
+    data.length -= offset;
+    data.start += offset;
+
+    if (has_length) {
+        if (njs_slow_path(length < 0 || (size_t) length > data.length)) {
+            njs_vm_range_error(vm, "length is out of range (must be <= %z)",
+                               data.length);
+            return NJS_ERROR;
+        }
+
+        data.length = length;
+    }
+
 process:
 
     if (pos == -1) {
index 44648bec4a870e5b9978fb5b1ee2fb6a652bad60..de8743b181cc5483c377b043c83eed3816b79f1d 100644 (file)
@@ -751,7 +751,7 @@ static JSValue
 qjs_fs_read(JSContext *cx, JSValueConst this_val, int argc,
     JSValueConst *argv, int calltype)
 {
-    int        fd, fd_offset;
+    int        fd, fd_offset, has_length;
     int64_t    length, pos, offset;
     ssize_t    n;
     JSValue    ret, result;
@@ -775,15 +775,34 @@ qjs_fs_read(JSContext *cx, JSValueConst this_val, int argc,
         }
     }
 
+    if (JS_ToInt64(cx, &offset, argv[fd_offset + 2]) < 0) {
+        return JS_EXCEPTION;
+    }
+
+    has_length = 0;
+    length = 0;
+
+    if (!JS_IsUndefined(argv[fd_offset + 3])) {
+        if (JS_ToInt64(cx, &length, argv[fd_offset + 3]) < 0) {
+            return JS_EXCEPTION;
+        }
+
+        has_length = 1;
+    }
+
+    pos = -1;
+
+    if (!JS_IsNullOrUndefined(argv[fd_offset + 4])) {
+        if (JS_ToInt64(cx, &pos, argv[fd_offset + 4]) < 0) {
+            return JS_EXCEPTION;
+        }
+    }
+
     ret = qjs_typed_array_data(cx, argv[fd_offset + 1], &buffer);
     if (JS_IsException(ret)) {
         return ret;
     }
 
-    if (JS_ToInt64(cx, &offset, argv[fd_offset + 2]) < 0) {
-        return JS_EXCEPTION;
-    }
-
     if (offset < 0 || (size_t) offset > buffer.length) {
         JS_ThrowRangeError(cx, "offset is out of range (must be <= %zu)",
                            buffer.length);
@@ -793,11 +812,7 @@ qjs_fs_read(JSContext *cx, JSValueConst this_val, int argc,
     buffer.length -= offset;
     buffer.start += offset;
 
-    if (!JS_IsUndefined(argv[fd_offset + 3])) {
-        if (JS_ToInt64(cx, &length, argv[fd_offset + 3]) < 0) {
-            return JS_EXCEPTION;
-        }
-
+    if (has_length) {
         if (length < 0 || (size_t) length > buffer.length) {
             JS_ThrowRangeError(cx, "length is out of range (must be <= %zu)",
                                buffer.length);
@@ -807,14 +822,6 @@ qjs_fs_read(JSContext *cx, JSValueConst this_val, int argc,
         buffer.length = length;
     }
 
-    pos = -1;
-
-    if (!JS_IsNullOrUndefined(argv[fd_offset + 4])) {
-        if (JS_ToInt64(cx, &pos, argv[fd_offset + 4]) < 0) {
-            return JS_EXCEPTION;
-        }
-    }
-
     if (pos == -1) {
         n = read(fd, buffer.start, buffer.length);
 
@@ -1993,7 +2000,7 @@ static JSValue
 qjs_fs_write(JSContext *cx, JSValueConst this_val, int argc,
     JSValueConst *argv, int calltype)
 {
-    int                          fd, fd_offset;
+    int                          fd, fd_offset, has_length;
     u_char                       *to_free_content;
     int64_t                      length, pos, offset;
     ssize_t                      n;
@@ -2073,12 +2080,31 @@ qjs_fs_write(JSContext *cx, JSValueConst this_val, int argc,
      * fs.writeSync(fd, buffer, offset[, length[, position]])
      */
 
-    v = qjs_typed_array_data(cx, buffer, &content);
-    if (JS_IsException(v)) {
+    if (JS_ToInt64(cx, &offset, argv[fd_offset + 2]) < 0) {
         return JS_EXCEPTION;
     }
 
-    if (JS_ToInt64(cx, &offset, argv[fd_offset + 2]) < 0) {
+    has_length = 0;
+    length = 0;
+
+    v = argv[fd_offset + 3];
+    if (!JS_IsNullOrUndefined(v)) {
+        if (JS_ToInt64(cx, &length, v) < 0) {
+            return JS_EXCEPTION;
+        }
+
+        has_length = 1;
+    }
+
+    v = argv[fd_offset + 4];
+    if (!JS_IsNullOrUndefined(v)) {
+        if (JS_ToInt64(cx, &pos, v) < 0) {
+            return JS_EXCEPTION;
+        }
+    }
+
+    v = qjs_typed_array_data(cx, buffer, &content);
+    if (JS_IsException(v)) {
         return JS_EXCEPTION;
     }
 
@@ -2091,12 +2117,7 @@ qjs_fs_write(JSContext *cx, JSValueConst this_val, int argc,
     content.length -= offset;
     content.start += offset;
 
-    v = argv[fd_offset + 3];
-    if (!JS_IsNullOrUndefined(v)) {
-        if (JS_ToInt64(cx, &length, v) < 0) {
-            return JS_EXCEPTION;
-        }
-
+    if (has_length) {
         if (length < 0 || (size_t) length > content.length) {
             JS_ThrowRangeError(cx, "length is out of range (must be <= %zu)",
                                content.length);
@@ -2106,13 +2127,6 @@ qjs_fs_write(JSContext *cx, JSValueConst this_val, int argc,
         content.length = length;
     }
 
-    v = argv[fd_offset + 4];
-    if (!JS_IsNullOrUndefined(v)) {
-        if (JS_ToInt64(cx, &pos, v) < 0) {
-            return JS_EXCEPTION;
-        }
-    }
-
 process:
 
     if (pos == -1) {
index 698bb0b069010fe24292a3f3a6afd595c9782e51..1e398947a426c1241c128ca1b55fc8c94318a396 100644 (file)
@@ -1292,6 +1292,46 @@ let options_tsuite = {
     ],
 };
 
+async function detached_buffer_test(params) {
+    let fn = `${test_dir}/fs_detached`;
+    fs.writeFileSync(fn, "ABCD");
+
+    let fd = fs.openSync(fn, params.flags);
+    let buf = new Uint8Array(4096);
+    let offset = {
+        valueOf() {
+            detach(buf.buffer);
+            return 0;
+        }
+    };
+
+    try {
+        fs[params.method](fd, buf, offset, 4, 0);
+        throw Error(`${params.method} accepted detached buffer`);
+
+    } catch (e) {
+        if (!(e instanceof TypeError)) {
+            throw e;
+        }
+
+    } finally {
+        fs.closeSync(fd);
+    }
+
+    return 'SUCCESS';
+}
+
+let detached_buffer_tsuite = {
+    name: "fs readSync/writeSync detached buffer",
+    skip: () => (!has_buffer() || !is_detach_available()),
+    T: detached_buffer_test,
+    prepare_args: (args) => args,
+    tests: [
+        { method: "readSync", flags: "r" },
+        { method: "writeSync", flags: "w" },
+    ],
+};
+
 run([
     readFile_tsuite,
     readFileSync_tsuite,
@@ -1323,5 +1363,6 @@ run([
     writeFh_tsuite,
     closeSync_tsuite,
     options_tsuite,
+    detached_buffer_tsuite,
 ])
 .then($DONE, $DONE);