BPF List
 help / color / mirror / Atom feed
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 3/4] lib/buildid: check the file type before __kernel_read()
Date: Thu,  3 Dec 2026 11:31:25 -0500	[thread overview]
Message-ID: <20261203163126.48697-4-mason@kernel.org> (raw)
In-Reply-To: <20261203163126.48697-1-mason@kernel.org>

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@kernel.org>
---
 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


  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 ` [PATCH bpf 1/4] lib/buildid: check FMODE_READ before __kernel_read() Chris Mason
2026-12-03 16:31 ` [PATCH bpf 2/4] lib/buildid: check the offset " Chris Mason
2026-12-03 16:31 ` Chris Mason [this message]
2026-12-03 18:13   ` [PATCH bpf 3/4] lib/buildid: check the file type " 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-4-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