From: Chris Mason <mason@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
andrii@kernel.org
Cc: akpm@linux-foundation.org, eddyz87@gmail.com, memxor@gmail.com,
martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev,
linux-kernel@vger.kernel.org, mason@kernel.org
Subject: [PATCH bpf 1/4] lib/buildid: check FMODE_READ before __kernel_read()
Date: Thu, 3 Dec 2026 11:31:23 -0500 [thread overview]
Message-ID: <20261203163126.48697-2-mason@kernel.org> (raw)
In-Reply-To: <20261203163126.48697-1-mason@kernel.org>
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@kernel.org>
---
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
next prev parent reply other threads:[~2026-12-03 16:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 16:31 [PATCH bpf 0/4] lib/buildid: fix a few bugs Chris Mason
2026-12-03 16:31 ` Chris Mason [this message]
2026-12-03 16:31 ` [PATCH bpf 2/4] lib/buildid: check the offset before __kernel_read() Chris Mason
2026-12-03 16:31 ` [PATCH bpf 3/4] lib/buildid: check the file type " Chris Mason
2026-12-03 18:13 ` sashiko-bot
2026-12-03 18:58 ` Chris Mason
2026-12-03 16:31 ` [PATCH bpf 4/4] lib/buildid: limit the number of ELF notes per parse Chris Mason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203163126.48697-2-mason@kernel.org \
--to=mason@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=song@kernel.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox