From: Leizhen Zhang <lzsx618@gmail.com>
To: nathan@kernel.org, nsc@kernel.org
Cc: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org,
rostedt@goodmis.org, ljs@kernel.org,
Leizhen Zhang <lzsx618@gmail.com>
Subject: [PATCH 1/5] elf-parse: avoid an undefined shift when checking the ELF type
Date: Sun, 18 Oct 2026 19:02:14 -0500 [thread overview]
Message-ID: <20261019000218.3977398-2-lzsx618@gmail.com> (raw)
In-Reply-To: <20261019000218.3977398-1-lzsx618@gmail.com>
elf_parse() takes a bitmap of permitted ELF types and tests it with
type = elf_parser.r2(&ehdr->e32.e_type);
if (!((1 << type) & types)) {
e_type is a 16-bit field read straight out of the file, so a type of 32
or more shifts by at least the width of int and the result is undefined;
31 shifts into the sign bit. All three users of the parser -- kallsyms,
sorttable and tracepoint-update -- reach this with e_type unvalidated.
Found by fuzzing the host ELF tools with mutated object files under
ASan/UBSan. Reproducer, on any vmlinux (or any ET_REL object for
tracepoint-update), setting only e_type:
$ python3 -c 'import struct
d = bytearray(open("vmlinux","rb").read())
struct.pack_into("<H", d, 0x10, 40)
open("t.elf","wb").write(bytes(d))'
$ ./kallsyms --sysmap=/dev/null t.elf
scripts/elf-parse.c:94:11: runtime error: shift exponent 40 is too
large for 32-bit type 'int'
#0 in elf_parse scripts/elf-parse.c:94
(host tools built with -fsanitize=address,undefined)
Reject a type that cannot be in the bitmap, and shift an unsigned 1 so
that a type of 31 stays defined. No valid object changes behaviour: every
ET_* value the callers put in the bitmap is far below 32, and a file with
a larger e_type already took the error path -- it just took it through
undefined behaviour.
Fixes: b055f4c431e3 ("sorttable: Move ELF parsing into scripts/elf-parse.[ch]")
Assisted-by: LLM ASan UBSan
Signed-off-by: Leizhen Zhang <lzsx618@gmail.com>
---
scripts/elf-parse.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/scripts/elf-parse.c b/scripts/elf-parse.c
index ce1c2a670244..9e7d1a4f263f 100644
--- a/scripts/elf-parse.c
+++ b/scripts/elf-parse.c
@@ -91,7 +91,8 @@ static int elf_parse(const char *fname, void *addr, uint32_t types)
}
type = elf_parser.r2(&ehdr->e32.e_type);
- if (!((1 << type) & types)) {
+ /* e_type comes from the file and the bitmap only has 32 slots. */
+ if (type >= 32 || !((1U << type) & types)) {
fprintf(stderr, "Invalid ELF type file %s\n", fname);
return -1;
}
--
2.34.1
next prev parent reply other threads:[~2026-10-19 0:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-19 0:02 [PATCH 0/5] elf-parse: validate what the host ELF tools read from the file Leizhen Zhang
2026-10-19 0:02 ` Leizhen Zhang [this message]
2026-10-19 12:47 ` [PATCH 1/5] elf-parse: avoid an undefined shift when checking the ELF type Steven Rostedt
2026-10-19 0:02 ` [PATCH 2/5] elf-parse: check that the section header table is inside the file Leizhen Zhang
2026-10-19 12:55 ` Steven Rostedt
2026-10-19 0:02 ` [PATCH 3/5] elf-parse: check the section indices that elf_open_ro() takes from " Leizhen Zhang
2026-10-19 0:02 ` [PATCH 4/5] elf-parse: check the symbol table before walking it Leizhen Zhang
2026-10-19 0:02 ` [PATCH 5/5] elf-parse: bound symbol names by the string table Leizhen Zhang
2026-10-19 12:57 ` [PATCH 0/5] elf-parse: validate what the host ELF tools read from the file Steven Rostedt
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=20261019000218.3977398-2-lzsx618@gmail.com \
--to=lzsx618@gmail.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=rostedt@goodmis.org \
/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
all inboxes | Powered by JetHome®