Skip to content

Fix NULL/OOB read parsing an unmatched bare comp-extra/defaults option on macOS - #141

Open
renard wants to merge 1 commit into
AgentD:masterfrom
renard:fix-args-parsing
Open

Fix NULL/OOB read parsing an unmatched bare comp-extra/defaults option on macOS#141
renard wants to merge 1 commit into
AgentD:masterfrom
renard:fix-args-parsing

Conversation

@renard

@renard renard commented Sep 5, 2026

Copy link
Copy Markdown

getsubopt() only guarantees returning -1 for an option that matches none of the given names. It does not guarantee fixing *valuep in that case. glibc sets it to the option's own text, but macOS/BSD libc sets it to NULL. Both
compressor_cfg_init_options() (comp_opt.c) and
parse_fstree_defaults() (fstree_cli.c) relied on the glibc behavior to look up bare flags such as "extreme" or "hc", or to report an unknown key.

On macOS this crashes tar2sqfs/gensquashfs with a NULL strcmp() argument for any --comp-extra flag with no "=value" part (e.g. "extreme", "hc"), and makes --defaults read one element before the start of its option name table on an unrecognized key.

Fixed by saving a pointer to the token before each getsubopt() call and using it to recover the option name when getsubopt() returns -1, instead of relying on *valuep. getsubopt() always NUL terminates the consumed token in place before returning, matched or not, so this works the same on every platform.

Also dropped parse_fstree_defaults()'s switch "default" case: moving the unrecognized-option check earlier makes it unreachable, and it referenced *valuep the same unsafe way this commit removes everywhere else.

…n on macOS

getsubopt() only guarantees returning -1 for an option that matches
none of the given names. It does not guarantee fixing
*valuep in that case. glibc sets it to the option's own text,
but macOS/BSD libc sets it to NULL. Both
compressor_cfg_init_options() (comp_opt.c) and
parse_fstree_defaults() (fstree_cli.c) relied on the glibc
behavior to look up bare flags such as "extreme" or "hc", or to
report an unknown key.

On macOS this crashes tar2sqfs/gensquashfs with a NULL strcmp()
argument for any --comp-extra flag with no "=value" part (e.g.
"extreme", "hc"), and makes --defaults read one element before the
start of its option name table on an unrecognized key.

Fixed by saving a pointer to the token before each getsubopt() call
and using it to recover the option name when getsubopt() returns -1,
instead of relying on *valuep. getsubopt() always NUL terminates the
consumed token in place before returning, matched or not, so this
works the same on every platform.

Also dropped parse_fstree_defaults()'s switch "default" case: moving
the unrecognized-option check earlier makes it unreachable, and it
referenced *valuep the same unsafe way this commit removes everywhere
else.

Signed-off-by: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant