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 4/4] lib/buildid: limit the number of ELF notes per parse
Date: Thu,  3 Dec 2026 11:31:26 -0500	[thread overview]
Message-ID: <20261203163126.48697-5-mason@kernel.org> (raw)
In-Reply-To: <20261203163126.48697-1-mason@kernel.org>

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), &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;
-- 
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 ` [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 ` Chris Mason [this message]

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-5-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