[PATCH bpf 3/4] lib/buildid: check the file type before __kernel_read()

From: Chris Mason

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


freader_fetch() consumes a TCP socket's data, or blocks until data arrives,
when a sleepable caller fetches the build ID of a VMA that maps the socket.

In sleepable context freader_fetch() reads with __kernel_read(), which
calls the ->read_iter() of whatever file the caller passes, and nothing
checks the file type. mmap() of a TCP socket gives a VMA whose vm_file
is the socket, and the ->read_iter() of a socket is recvmsg():

do_procmap_query() or bpf_get_stack_sleepable()
build_id_parse_file(vma->vm_file)
freader_fetch(r, 0, 18)
__kernel_read()
sock_read_iter()
sock_recvmsg() /* no MSG_PEEK, blocks unless O_NONBLOCK */

The fix used here is to fail a sleepable fetch unless the file is a
regular file or a block device.

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

diff --git a/lib/buildid.c b/lib/buildid.c
index 22bbac1ee02f4..6ce7745507342 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -99,8 +99,15 @@ const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz)

/* use __kernel_read() for sleepable context */
if (r->may_fault) {
+ umode_t mode = file_inode(r->file)->i_mode;
ssize_t ret;

+ /* ->read_iter() of a stream waits for a peer and consumes data */
+ if (!S_ISREG(mode) && !S_ISBLK(mode)) {
+ r->err = -EINVAL;
+ return NULL;
+ }
+
/* __kernel_read() warns if the file is not open for reading */
if (!(r->file->f_mode & FMODE_READ)) {
r->err = -EINVAL;
--
2.55.0