Skip to contentSkip to conversation

lib/buildid: fix a few bugs

PATCH Applied Chris Mason 3 participants 8 messages Dec 3 – Dec 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover lib/buildid: fix a few bugs

Hi everyone,

These are AI generated fixes for a few buildid bugs.

Patches 1-3 add the checks that freader_fetch() needs before it calls __kernel_read(). Without them, we can trigger a warning, overflow the kernfs read buffer, or read from a socket.

Patch 4 limits the number of ELF notes we'll walk, so a bad file can't lock up the CPU.

This is on top of v7.4-rc5.

Chris Mason (4):
lib/buildid: check FMODE_READ before __kernel_read() lib/buildid: check the offset before __kernel_read() lib/buildid: check the file type before __kernel_read() lib/buildid: limit the number of ELF notes per parse

 lib/buildid.c | 37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/4 lib/buildid: check FMODE_READ before __kernel_read()

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")
---
 lib/buildid.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/buildid.c +6 -0
index 995a327f46ae7..1331d29b15d16 100644
@@ -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;
Signature · 1 line
2.55.0
#2 lore raw

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

In reply to Chris Mason

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")
---
 lib/buildid.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/buildid.c +6 -0
index 1331d29b15d16..22bbac1ee02f4 100644
@@ -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;
Signature · 1 line
2.55.0
#3 lore raw

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

In reply to Chris Mason

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")
---
 lib/buildid.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/buildid.c +7 -0
index 22bbac1ee02f4..6ce7745507342 100644
@@ -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;
Signature · 1 line
2.55.0
#6 lore raw

PATCH 4/4 lib/buildid: limit the number of ELF notes per parse

In reply to Chris Mason

parse_build_id() causes a hard lockup if an ELF file's PT_NOTE segments are full of empty notes.

The note loop can walk all of p_filesz if those pages are in the page cache, and bpf_get_stackid() on a BPF_F_STACK_BUILD_ID map can run it in NMI context or with IRQs off. A note with zero n_namesz and n_descsz advances the loop by just sizeof(Elf32_Nhdr), and all MAX_PHDR_CNT program headers can be PT_NOTE and cover the same range of the file:

    U32_MAX / sizeof(Elf32_Nhdr) = U32_MAX / 12 ~ 358M notes per segment
    MAX_PHDR_CNT * 358M          = 256 * 358M   ~  92G notes per parse

The fix used here is to cap the number of notes walked per parse, as MAX_PHDR_CNT does for program headers.

Fixes cdbb44f9a74f ("lib/buildid: don't limit .note.gnu.build-id to the first page in ELF")
---
 lib/buildid.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
lib/buildid.c +12 -6
index 6ce7745507342..b6a6bde544099 100644
@@ -11,6 +11,7 @@
 #define BUILD_ID 3
 
 #define MAX_PHDR_CNT 256
+#define MAX_NOTE_CNT 1024
 
 void freader_init_from_file(struct freader *r, void *buf, u32 buf_sz,
 			    struct file *file, bool may_fault)
@@ -176,7 +177,7 @@ void freader_cleanup(struct freader *r)
  * identical.
  */
 static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *size,
-			  loff_t note_off, Elf32_Word note_size)
+			  loff_t note_off, Elf32_Word note_size, u32 *note_cnt)
 {
 	const char note_name[] = "GNU";
 	const size_t note_name_sz = sizeof(note_name);
@@ -188,6 +189,10 @@ static int parse_build_id(struct freader *r, unsigned char *build_id, __u32 *siz
 		return -EINVAL;
 
 	while (note_end - note_off > sizeof(Elf32_Nhdr) + note_name_sz) {
+		/* set upper bound on amount of notes we iterate, over all segments */
+		if (++*note_cnt > MAX_NOTE_CNT)
+			break;
+
 		nhdr = freader_fetch(r, note_off, sizeof(Elf32_Nhdr) + note_name_sz);
 		if (!nhdr)
 			return r->err;
@@ -230,7 +235,7 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si
 {
 	const Elf32_Ehdr *ehdr;
 	const Elf32_Phdr *phdr;
-	__u32 phnum, phoff, i;
+	__u32 phnum, phoff, i, note_cnt = 0;
 
 	ehdr = freader_fetch(r, 0, sizeof(Elf32_Ehdr));
 	if (!ehdr)
@@ -255,7 +260,7 @@ static int get_build_id_32(struct freader *r, unsigned char *build_id, __u32 *si
 
 		if (phdr->p_type == PT_NOTE &&
 		    !parse_build_id(r, build_id, size, READ_ONCE(phdr->p_offset),
-				    READ_ONCE(phdr->p_filesz)))
+				    READ_ONCE(phdr->p_filesz), &note_cnt))
 			return 0;
 	}
 	return -EINVAL;
@@ -266,7 +271,7 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si
 {
 	const Elf64_Ehdr *ehdr;
 	const Elf64_Phdr *phdr;
-	__u32 phnum, i;
+	__u32 phnum, i, note_cnt = 0;
 	__u64 phoff;
 
 	ehdr = freader_fetch(r, 0, sizeof(Elf64_Ehdr));
@@ -292,7 +297,7 @@ static int get_build_id_64(struct freader *r, unsigned char *build_id, __u32 *si
 
 		if (phdr->p_type == PT_NOTE &&
 		    !parse_build_id(r, build_id, size, READ_ONCE(phdr->p_offset),
-				    READ_ONCE(phdr->p_filesz)))
+				    READ_ONCE(phdr->p_filesz), &note_cnt))
 			return 0;
 	}
 
@@ -403,11 +408,12 @@ int build_id_parse_file(struct file *file, unsigned char *build_id, __u32 *size)
 int build_id_parse_buf(const void *buf, unsigned char *build_id, u32 buf_size)
 {
 	struct freader r;
+	u32 note_cnt = 0;
 	int err;
 
 	freader_init_from_mem(&r, buf, buf_size);
 
-	err = parse_build_id(&r, build_id, NULL, 0, buf_size);
+	err = parse_build_id(&r, build_id, NULL, 0, buf_size, &note_cnt);
 
 	freader_cleanup(&r);
 	return err;
Signature · 1 line
2.55.0
#4 lore raw

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

In reply to Chris Mason
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] lib/buildid: freader_fetch() allows __kernel_read() on S_IFREG stream files --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] lib/buildid: freader_fetch() allows __kernel_read() on S_IFREG stream files
--

commit 2b2bbc9fbc6dc8ab602d62d9f09a6baccfe7d1de
Author: Chris Mason <[email protected]>

lib/buildid: check the file type before __kernel_read()

This commit rejects non-regular, non-block files before calling __kernel_read() in freader_fetch() so that sleepable build ID lookups do not read from sockets or other streams.

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

lib/buildid.c const void *freader_fetch(struct freader *r, loff_t file_off, size_t sz) · line 109

Quoted 10 lines
lib/buildid.c +6 -0
 	/* 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;
+		}

[Severity: Medium]
Can S_IFREG stream files still reach __kernel_read() here?

Inode mode alone misses regular files opened with stream_open() or nonseekable_open(). FUSE files opened with FOPEN_STREAM still allow mmap() in fuse_file_mmap(), so do_procmap_query() -> build_id_parse_file() still calls fuse_file_read_iter(), which can block or consume stream data.

Rejecting files with FMODE_STREAM or without FMODE_LSEEK in r->file->f_mode would also catch stream regular files while still allowing seekable pseudo files like shmem and memfd.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#5 lore raw

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

Quoted 2 lines · On Thu, 03 Dec 2026 18:13:35 +0000, [email protected] wrote:

[Severity: Medium]
Can S_IFREG stream files still reach __kernel_read() here?

Yes, a FUSE file with FOPEN_STREAM still gets here. But page faults on that mapping already send reads to the FUSE server, and this patch doesn't change anything for regular files, so I'd leave that one alone.

-chris

#7 lore raw
In reply to Chris Mason
This series was applied to bpf/bpf.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · On Thu, 3 Dec 2026 11:31:22 -0500 you wrote:

Hi everyone,

These are AI generated fixes for a few buildid bugs.

Patches 1-3 add the checks that freader_fetch() needs before it calls __kernel_read(). Without them, we can trigger a warning, overflow the kernfs read buffer, or read from a socket.

[...]

Here is the summary with links:
- [bpf,1/4] lib/buildid: check FMODE_READ before __kernel_read()

    https://git.kernel.org/bpf/bpf/c/cb634df2b91e
  - [bpf,2/4] lib/buildid: check the offset before __kernel_read()
    https://git.kernel.org/bpf/bpf/c/adfc32b685e8
  - [bpf,3/4] lib/buildid: check the file type before __kernel_read()
    https://git.kernel.org/bpf/bpf/c/03b365971313
  - [bpf,4/4] lib/buildid: limit the number of ELF notes per parse
    https://git.kernel.org/bpf/bpf/c/12af212c4ffa

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html