[PATCH bpf 1/4] lib/buildid: check FMODE_READ before __kernel_read()
From: Chris Mason
Date: Thu Dec 03 2026 - 11:32:00 EST
freader_fetch() hits the WARN_ON_ONCE() in __kernel_read() if a sleepable
program reads a file dynptr whose file is not open for reading.
__kernel_read() expects its caller to check FMODE_READ: without the flag
it warns and returns -EINVAL. The sleepable branch of freader_fetch()
passes r->file to it and doesn't check f_mode. Neither does
bpf_dynptr_from_file(), which sets up a freader for any file the program
passes in.
The fix used here is to check FMODE_READ in freader_fetch() and fail the
fetch.
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 995a327f46ae7..1331d29b15d16 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -101,6 +101,12 @@ const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz)
if (r->may_fault) {
ssize_t ret;
+ /* __kernel_read() warns if the file is not open for reading */
+ if (!(r->file->f_mode & FMODE_READ)) {
+ 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