[PATCH bpf 2/4] lib/buildid: check the offset before __kernel_read()

From: Chris Mason

Date: Thu Dec 03 2026 - 11:31:40 EST


sysfs overflows the kernfs read buffer when a file dynptr reads a binary
attribute at a negative offset.

__kernel_read() skips rw_verify_area(), so its caller has to reject a
negative offset, and the sleepable branch of freader_fetch() doesn't. A
file dynptr has a size of U64_MAX, so an offset of 2^63 or more can pass
its bounds check. sysfs then grows count past the buffer that kernfs
allocated:

kernfs_file_read_iter()
buf = kmalloc(8)
sysfs_kf_bin_read() pos = -16, count = 8, size = 84
pos >= size signed, false
pos + count > size unsigned, true
count = size - pos 100
sysfs_bin_attr_simple_read()
memcpy(buf, attr->private + off, count)

The fix used here is to check the offset in the sleepable branch, as
rw_verify_area() does.

Fixes: 777a8560fd29 ("lib/buildid: use __kernel_read() for sleepable context")
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
---
lib/buildid.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/lib/buildid.c b/lib/buildid.c
index 1331d29b15d16..22bbac1ee02f4 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -107,6 +107,12 @@ const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz)
return NULL;
}

+ /* __kernel_read() skips the offset checks of rw_verify_area() */
+ if (file_off < 0 || (loff_t)(file_off + sz) < 0) {
+ r->err = -EINVAL;
+ return NULL;
+ }
+
ret = __kernel_read(r->file, r->buf, sz, &file_off);
if (ret != sz) {
r->err = (ret < 0) ? ret : -EIO;
--
2.55.0