From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796315498; b=TleKZXJ0kt6U22XKyByTLIv8ydx1bU+YhF1Ay6bGos1Gea+sbNyxjJ9HvzFMn+t1X60P 0M6BQ6GvGVOJGdbWRfMC40BlrZZAqAPl+c/z+FH22FxLDeC1vngRqjvONSO3vEGx0sNBH 5C+zelAMjAFzMcUERTwB22vNWKaJtOCuv3n9bNKnHXHPHiO04ifULuBP3kA+mDnk9Rgkn EB7kRcVJF4yeZG8FiKyd+x6aNXDDiE/Mwqo2zNXqUnxDaAEI9+dXGDUsRP25oEeKbNvpD 6oHNVyDnNDCuPPD7euwXbWCZM6uQmANGv56m3RNN+w9FUcp7sCNze0TEYzVvsmq4zwA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796315498; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=gntHj6R5BZ+LdI0Bs9aU5m3f7+KxR78j7B6BLWfQYN0=; b=jwatfiFm2x6lLQ33TGsKQ6TFtciIc94umQwF25+7T4pxkQSta1PqpxYSgq25r0d0xvPD g87gH4D0Mnuf72P6JwL0pya5SQe59+WGJqnzI1RjDmww69aHNnH7PPgq8xff9fGIykPzw wfUKCkgDzkUR+/xnAu7H147FiC60aMTkaajYgjb0ttLwulk4+yBwAE7iGVqEbtcIOjoGt ncKPb+qkB7VZOzjeYeidSkkPqVWVDFtyfHXEYuQRGxMc6g0VITLoajSiiHnwuiT/XD8LT jesWfFWHwinp0wxmGidH7kiPzP4ybKMVhPMdSSlIc3ISoXuRPK/C9fBg7CsOnOywtqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="DfK6EZC/"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD01D606A338; Thu, 3 Dec 2026 16:31:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DfK6EZC/ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C8401F00898; Thu, 3 Dec 2026 16:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796315498; bh=gntHj6R5BZ+LdI0Bs9aU5m3f7+KxR78j7B6BLWfQYN0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DfK6EZC/8+u0sVJ4hkJywUGd7qw7uL1BAz/RiD+sABHQF46/cCs16QMm7NZVTkKwl fMhodB359GWgAouDiqvP6rhij2UTb2krkJDfv7XEenm6yeWaB2J51x3ofUx9xXdHvi 6w9g6uAAstyuzW1alEykbpaxFqGZiphPd4GAUy66fs3z/AgWVeATcKWVqf3YIvmC/N lEVLftF85nZeT2ZtdAeBsWQZq0qPHNwWgxxXtXyNrmcSV0lEwNKofp7Kz2FpPih0cg pcR8ItpIZmCMJfIk28pF2ovmLoLgFzKHKckH3oo9juvFjVT+u/7FHGqKGggp7IbQJf Jng1Zf/b39vOw== From: Chris Mason 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 Message-ID: <20261203163126.48697-5-mason@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203163126.48697-1-mason@kernel.org> References: <20261203163126.48697-1-mason@kernel.org> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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