Skip to content

bcachefs: guard dirent to_text against unvalidated names - #1202

Open
matthiasgoergens wants to merge 2 commits into
koverstreet:masterfrom
matthiasgoergens:fix/dirent-to-text-guards
Open

bcachefs: guard dirent to_text against unvalidated names#1202
matthiasgoergens wants to merge 2 commits into
koverstreet:masterfrom
matthiasgoergens:fix/dirent-to-text-guards

Conversation

@matthiasgoergens

Copy link
Copy Markdown

bch2_dirent_to_text() may be called on unvalidated keys: the sb
validation error path prints a corrupt clean section's journal entries,
and a dirent key with a truncated value or an inflated name length read
past the key in prt_bytes().

Return early with an invalid marker for truncated keys, and check the
name and lookup-name extents against the value bounds before printing.
The extent guards check the length before computing the sum, so the sum
cannot wrap (qstr.len is u32 and bch2_dirent_name_bytes() can underflow).

Adds kunit tests for the truncated, underflowed-name, and casefold
overrun paths, passing under UML.

Found by fuzzing the offline tools with AFL++/ASAN.

The bcachefs-tools tree has the same fix in
koverstreet/bcachefs-tools#877.

bch2_dirent_to_text() may be called on unvalidated keys: the sb
validation error path prints a corrupt clean section's journal entries,
and a dirent key with a truncated value or an inflated name length
reached the printer, which then read past the key in prt_bytes().

Return early with an invalid marker when the key header is truncated
(bkey_val_u64s() underflows when u64s < BKEY_U64s, so the header length
is checked explicitly) or the value is smaller than struct bch_dirent,
and check the name and lookup-name extents against the value bounds
before printing. The extent guards check the length against the value
size before computing the sum: qstr.len is u32 and
bch2_dirent_name_bytes() can underflow to 0xffffffff, so the sum would
wrap in 32-bit arithmetic. The guards bound extents against the key's
declared u64s; the callers' walkers bound the declared extent to
actual storage.

Found by fuzzing the offline tools with AFL++/ASAN.
Tests the truncated-key, underflowed-name, and casefold overrun paths,
all of which previously crashed.
@matthiasgoergens
matthiasgoergens force-pushed the fix/dirent-to-text-guards branch from eec0523 to 2a53d33 Compare August 18, 2026 09:28
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