syzbot
KASAN: use-after-free Read in ocfs2_search_dirblock
Status:
upstream: reported C repro on 2024/08/16 09:49
Subsystems:
ocfs2
Labels:
prio:high
[Documentation on labels]
Reported-by: syzbot+b9704899e166798d57c9@syzkaller.appspotmail.com
Fix commit:
ocfs2: fix directory entry check in ocfs2_search_dirblock()
Patched on: [ci-upstream-linux-next-kasan-gce-root], missing on: [ci-qemu-gce-upstream-auto ci-qemu-native-arm64-kvm ci-qemu-upstream ci-qemu-upstream-386 ci-qemu2-arm32 ci-qemu2-arm64 ci-qemu2-arm64-compat ci-qemu2-arm64-mte ci-qemu2-riscv64 ci-snapshot-upstream-root ci-upstream-bpf-kasan-gce ci-upstream-bpf-next-kasan-gce ci-upstream-gce-arm64 ci-upstream-gce-leak ci-upstream-kasan-badwrites-root ci-upstream-kasan-gce ci-upstream-kasan-gce-386 ci-upstream-kasan-gce-root ci-upstream-kasan-gce-selinux-root ci-upstream-kasan-gce-smack-root ci-upstream-kmsan-gce-386-root ci-upstream-kmsan-gce-root ci-upstream-net-kasan-gce ci-upstream-net-this-kasan-gce ci-upstream-rust-kasan-gce ci2-upstream-fs ci2-upstream-kcsan-gce ci2-upstream-usb]
First crash: 823d, last: 676d
✨ AI-generated root cause summary:
When mounting a corrupted OCFS2 filesystem image, a directory inode with the OCFS2_INLINE_DATA_FL dynamic feature can have an on-disk size (fe->i_size) larger than the maximum inline data capacity of the dinode block. Neither ocfs2_validate_inode_block()/ocfs2_populate_inode() nor ocfs2_find_entry_id() validates that i_size does not exceed the inline data limit. During directory lookup (ocfs2_lookup() -> ocfs2_find_entry_id()), ocfs2_search_dirblock() receives i_size_read(dir) as the search byte limit, causing dlimit to extend far beyond the buffer_head boundary (di_bh->b_data). As the search loop iterates using de->rec_len, it accesses memory beyond the buffer into an adjacent or freed page, triggering an out-of-bounds read reported by KASAN as use-after-free in ocfs2_match().
▶
▼
✨ Full AI root cause analysis
Root Cause Analysis
Involved Functions, Structs, and Files
- Files:
fs/ocfs2/dir.c, fs/ocfs2/inode.c, fs/ocfs2/ocfs2_fs.h
- Functions:
ocfs2_find_entry_id(), ocfs2_search_dirblock(), ocfs2_match(), ocfs2_populate_inode()
- Structs & Fields:
struct ocfs2_dinode: fe->i_size, di->i_dyn_features, di->id2.i_data.id_data, di->id2.i_data.id_count
struct ocfs2_dir_entry: de->rec_len, de->name_len, de->inode, de->name
struct inode: dir->i_size (i_size_read(dir))
Mechanism of the Failure
-
Inode Population and Missing Validation:
In OCFS2, files or directories can have inline data embedded directly within their inode block buffer (di_bh->b_data) at di->id2.i_data.id_data when the dynamic feature flag OCFS2_INLINE_DATA_FL is set. The maximum possible inline data size is bounded by the block size minus the dinode structure header offset:
$$\text{max_inline_data} = \text{sb->s_blocksize} - \text{offsetof}(\text{struct ocfs2_dinode}, \text{id2.i_data.id_data}) - \text{xattrsize}$$
(as computed by ocfs2_max_inline_data_with_xattr()).
While regular file read paths (such as ocfs2_read_inline_data() in fs/ocfs2/aops.c) check whether i_size > ocfs2_max_inline_data_with_xattr(...), neither ocfs2_validate_inode_block() nor ocfs2_populate_inode() in fs/ocfs2/inode.c validates that fe->i_size of a directory inode with OCFS2_INLINE_DATA_FL fits within this limit.
-
Triggering Directory Search (ocfs2_find_entry_id()):
When looking up a path entry inside an inline directory (ocfs2_lookup() $\to$ ocfs2_find_entry() $\to$ ocfs2_find_entry_id()), the kernel reads the inode block di_bh and passes the start of inline data and the inode size to ocfs2_search_dirblock():
di = (struct ocfs2_dinode *)di_bh->b_data;
data = &di->id2.i_data;
found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
data->id_data, i_size_read(dir), res_dir);
-
Out-of-Bounds Traversal in ocfs2_search_dirblock():
In ocfs2_search_dirblock(), the search limit is calculated as:
de_buf = first_de;
dlimit = de_buf + bytes; /* bytes == i_size_read(dir) */
Because bytes is taken directly from the unvalidated i_size, dlimit extends far beyond the bounds of the di_bh->b_data block (typically a single buffer/page).
As the loop iterates:
while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
de = (struct ocfs2_dir_entry *) de_buf;
if (de->name + namelen <= dlimit &&
ocfs2_match(namelen, name, de)) {
...
}
de_len = le16_to_cpu(de->rec_len);
if (de_len <= 0) ...
de_buf += de_len;
}
For non-matching entries, ocfs2_check_dir_entry() is intentionally skipped for performance ("minimal checking by hand"). When de_buf advances past the allocated di_bh buffer, it walks off the end of the page and enters adjacent physical memory in the kernel direct mapping.
-
Crash Manifestation:
When the out-of-bounds pointer de_buf enters an adjacent physical page that is marked as freed in the page allocator, accessing de->name_len inside inline ocfs2_match():
if (len != de->name_len)
return 0;
triggers a KASAN use-after-free report (or an unhandled kernel paging request if an unmapped page boundary is crossed).
▶
▼
Last patch testing requests (7)
Sample crash report:
* Struck through repros no longer work on HEAD.