* [PATCH bpf 1/4] lib/buildid: check FMODE_READ before __kernel_read()
2026-12-03 16:31 [PATCH bpf 0/4] lib/buildid: fix a few bugs Chris Mason
@ 2026-12-03 16:31 ` Chris Mason
2026-12-03 16:31 ` [PATCH bpf 2/4] lib/buildid: check the offset " Chris Mason
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Chris Mason @ 2026-12-03 16:31 UTC (permalink / raw)
To: bpf, ast, daniel, andrii
Cc: akpm, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, linux-kernel, mason
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf 2/4] lib/buildid: check the offset before __kernel_read()
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 ` Chris Mason
2026-12-03 16:31 ` [PATCH bpf 3/4] lib/buildid: check the file type " Chris Mason
2026-12-03 16:31 ` [PATCH bpf 4/4] lib/buildid: limit the number of ELF notes per parse Chris Mason
3 siblings, 0 replies; 7+ messages in thread
From: Chris Mason @ 2026-12-03 16:31 UTC (permalink / raw)
To: bpf, ast, daniel, andrii
Cc: akpm, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, linux-kernel, 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")
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 1331d29b15d16..22bbac1ee02f4 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -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;
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf 3/4] lib/buildid: check the file type before __kernel_read()
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
2026-12-03 18:13 ` sashiko-bot
2026-12-03 16:31 ` [PATCH bpf 4/4] lib/buildid: limit the number of ELF notes per parse Chris Mason
3 siblings, 1 reply; 7+ messages in thread
From: Chris Mason @ 2026-12-03 16:31 UTC (permalink / raw)
To: bpf, ast, daniel, andrii
Cc: akpm, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, linux-kernel, 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")
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf 4/4] lib/buildid: limit the number of ELF notes per parse
2026-12-03 16:31 [PATCH bpf 0/4] lib/buildid: fix a few bugs Chris Mason
` (2 preceding siblings ...)
2026-12-03 16:31 ` [PATCH bpf 3/4] lib/buildid: check the file type " Chris Mason
@ 2026-12-03 16:31 ` Chris Mason
3 siblings, 0 replies; 7+ messages in thread
From: Chris Mason @ 2026-12-03 16:31 UTC (permalink / raw)
To: bpf, ast, daniel, andrii
Cc: akpm, eddyz87, memxor, martin.lau, song, yonghong.song, jolsa,
emil, ihor.solodrai, linux-kernel, 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")
Signed-off-by: Chris Mason <mason@kernel.org>
---
lib/buildid.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/lib/buildid.c b/lib/buildid.c
index 6ce7745507342..b6a6bde544099 100644
--- a/lib/buildid.c
+++ b/lib/buildid.c
@@ -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), ¬e_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), ¬e_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, ¬e_cnt);
freader_cleanup(&r);
return err;
--
2.55.0
^ permalink raw reply related [flat|nested] 7+ messages in thread