system/nxinit: fix stack-buffer-overflow in init_parse_config_buffer

init_parse_config_buffer() computed the per-refill copy length as
MIN(len - off, sizeof(tmp)) without subtracting the 'n' leftover bytes
already held at the front of 'tmp' from a previous refill, so
memcpy(&tmp[n], ..., r) could write past the end of tmp[]. The
file-based twin, init_parse_config_file(), already gets this right
(read(fd, &buf[n], sizeof(buf) - n)).

Reproduced locally with an AddressSanitizer host harness feeding the
real 95-byte builtin "preset" rc content through
init_parse_config_buffer() at CONFIG_SYSTEM_NXINIT_RC_LINE_MAX=32/48:
ASan reports a stack-buffer-overflow on the 'tmp' array. Fixed to
MIN(len - off, sizeof(tmp) - n) and reverified clean at
RC_LINE_MAX=32/48/64/128.

The default config never hits this (the builtin preset is 95 bytes and
the default RC_LINE_MAX is 128), but SYSTEM_NXINIT_RC_LINE_MAX had no
lower bound, so lowering it towards 32/48 for a smaller build would
silently corrupt the stack while parsing the preset during boot. Add a
"range 64 4096" bound so the value can no longer be set below the
builtin preset's needs.

Assisted-by: opencode:mimo-v2.5-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
This commit is contained in:
wangjianyu3 2026-08-27 10:49:04 +08:00 committed by Alan C. Assis
parent a31fe191fa
commit 7a1c6aaa7c
2 changed files with 2 additions and 1 deletions

View file

@ -40,6 +40,7 @@ config SYSTEM_NXINIT_RC_FILE_PATH
config SYSTEM_NXINIT_RC_LINE_MAX
int "Max line length of RC file"
default 128
range 64 4096
---help---
Maximum line length of RC file.
More details: https://android.googlesource.com/platform/system/core/+/master/init/README.md

View file

@ -109,7 +109,7 @@ static int init_parse_config_buffer(FAR const struct parser_s *parser,
for (; ; )
{
r = MIN(len - off, sizeof(tmp));
r = MIN(len - off, sizeof(tmp) - n);
memcpy(&tmp[n], &buf[off], r);
if (r == 0)
{