node icon indicating copy to clipboard operation
node copied to clipboard

child_process: advanced serialization segfaults on a host object with an index property

Open ketd opened this issue 1 week ago • 1 comments

Version

v26.13.1 (v24.22.3 crashes the same way; v22.23.5 does not)

Platform

Linux x64 (official node:26.13.1-bookworm-slim image) and macOS arm64 (official v26.13.1 binary)

Subsystem

child_process

What steps will reproduce the bug?

// repro.js
const { fork } = require('node:child_process');
const { MessageChannel } = require('node:worker_threads');
if (process.argv[2] === 'child') return;
const { port1 } = new MessageChannel();
port1[0] = 'x';
fork(__filename, ['child'], { serialization: 'advanced' }).send(port1);
$ node repro.js
Segmentation fault (core dumped)

With a property that is not an index (port1.named = 'x'), the child receives {"named":"x"}.

How often does it reproduce? Is there a required condition?

Always, when the sent value is a host object (here a MessagePort) with an own enumerable index property.

What is the expected behavior? Why is that the expected behavior?

The child receives { '0': 'x' }. The C++ code is documented to send the same shallow copy as writeValue({ ...object }) in serialization.js. A JS error would also be acceptable, but not a crash.

What do you see instead?

Segmentation fault (exit code 139).

Additional information

SerializerDelegate::WriteHostObject in src/node_ipc_serdes.cc (the non-view branch) calls object->GetOwnPropertyNames(context). Its default KeyConversionMode is kKeepNumbers, so index keys come back as Smis. The loop then calls copy->CreateDataProperty(context, key.As<Name>(), val). As<Name>() is an unchecked cast, and V8 dereferences the Smi as a Name.

Requesting string keys fixes it: object->GetOwnPropertyNames(context, static_cast<PropertyFilter>(ONLY_ENUMERABLE | SKIP_SYMBOLS), KeyConversionMode::kConvertToString). Another option is to branch on key->IsUint32() and use the CreateDataProperty(context, uint32_t, value) overload.

The code is the same on main (be0f17cfe4).

ketd avatar Nov 25 '26 02:11 ketd

Reproduced on Linux x64: it crashes with SIGSEGV on v24.22.3 and main (built from d2812af0), and works on v20.20.2, v22.20.0 and v25.6.1, where the child receives {"0":"x"}. With a non-index property (port1.named = 'x') every version works.

The backtrace on main ends in v8::Object::CreateDataProperty called from node::ipc_serdes::IPCSerializerDelegate::WriteHostObject, which matches the analysis above. src/node_ipc_serdes.cc was added in #63933 (first released in v26.4.0 and v24.19.0), so this is a regression from that change. cc @anonrig

yohannesjoya avatar Nov 26 '26 18:11 yohannesjoya