mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Leizhen Zhang <lzsx618@gmail.com>
To: nathan@kernel.org, nsc@kernel.org
Cc: masahiroy@kernel.org, ardb@kernel.org, rostedt@goodmis.org,
	hpa@zytor.com, linux-kbuild@vger.kernel.org,
	linux-kernel@vger.kernel.org, Leizhen Zhang <lzsx618@gmail.com>
Subject: [PATCH v3 5/6] sorttable: avoid pointer arithmetic overflow when locating sort_needed
Date: Mon,  9 Nov 2026 18:00:02 -0600	[thread overview]
Message-ID: <20261110000003.1719966-6-lzsx618@gmail.com> (raw)
In-Reply-To: <20261110000003.1719966-1-lzsx618@gmail.com>

The location of the main_extable_sort_needed variable is computed as

  (void *)ehdr + shdr_offset(sec) + sym_value(sym) - shdr_addr(sec)

which first adds the symbol's virtual address (e.g. 0xffffffff8...) to
the pointer and only then subtracts the section address. The
intermediate pointer overflows, which is undefined behaviour and is
reported by UBSan.

Subtract the section address from the symbol value first.

The problem was found with UBSan, and the analysis and fix were
prepared with the help of an AI coding assistant; I reviewed and
tested the result.

Fixes: a79f248b9b30 ("scripts: Add sortextable to sort the kernel's exception table.")
Signed-off-by: Leizhen Zhang <lzsx618@gmail.com>
---
 scripts/sorttable.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/sorttable.c b/scripts/sorttable.c
index 88e49a5c4251..0425e00c490f 100644
--- a/scripts/sorttable.c
+++ b/scripts/sorttable.c
@@ -788,7 +788,7 @@ static int do_sort(Elf_Ehdr *ehdr,
 	sort_needed_sec = get_index(shdr_start, shentsize, sort_need_index);
 	sort_needed_loc = (void *)ehdr +
 		shdr_offset(sort_needed_sec) +
-		sym_value(sort_needed_sym) - shdr_addr(sort_needed_sec);
+		(sym_value(sort_needed_sym) - shdr_addr(sort_needed_sec));
 
 	/* extable has been sorted, clear the flag */
 	elf_parser.w(0, sort_needed_loc);
-- 
2.34.1


  parent reply	other threads:[~2026-11-10  0:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 23:59 [PATCH v3 0/6] kbuild: fix UB hit by normal builds in host tools Leizhen Zhang
2026-11-09 23:59 ` [PATCH v3 1/6] genksyms: fix infinite loop on declarations with parameter lists Leizhen Zhang
2026-11-09 23:59 ` [PATCH v3 2/6] kallsyms: do not call qsort() with a NULL table Leizhen Zhang
2026-11-10  0:00 ` [PATCH v3 3/6] modpost: fix handling of short reads in read_text_file() Leizhen Zhang
2026-11-10  0:00 ` [PATCH v3 4/6] modpost: fix pointer arithmetic on NULL in parse_source_files() Leizhen Zhang
2026-11-10  0:00 ` Leizhen Zhang [this message]
2026-11-10  0:00 ` [PATCH v3 6/6] tools/include: fix signed shift overflow in 32-bit unaligned accessors Leizhen Zhang

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=20261110000003.1719966-6-lzsx618@gmail.com \
    --to=lzsx618@gmail.com \
    --cc=ardb@kernel.org \
    --cc=hpa@zytor.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@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®