Live Patching
 help / color / mirror / Atom feed
From: Longjun Luo <luolongjuna@gmail.com>
To: jpoimboe@kernel.org, peterz@infradead.org
Cc: mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com,
	song@kernel.org, puranjay@kernel.org, nathan@kernel.org,
	ndesaulniers@google.com, morbo@google.com,
	justinstitt@google.com, live-patching@vger.kernel.org,
	llvm@lists.linux.dev, linux-kernel@vger.kernel.org,
	Longjun Luo <luolongjuna@gmail.com>
Subject: [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels
Date: Thu, 26 Nov 2026 18:53:25 +0800	[thread overview]
Message-ID: <20261126105325.2521938-2-luolongjuna@gmail.com> (raw)
In-Reply-To: <20261126105325.2521938-1-luolongjuna@gmail.com>

String relocations can be used for string loads or sentinel checks.
For example, with GCC 16, range_string() in lib/vsprintf.c uses .LC22 + 8
as a loop bound. Treating a sentinel as the start of a string can cause
objtool to read past the end of the referenced string.

Encode each string reference as [byte offset, whole string] in the
checksum to distinguish references at different offsets.

Keep the symbol-based form for sentinel-related relocations to preserve
their offsets from the same string label. Then handle them as normal
data references.

With GCC and Clang, moving the bound from +7 to +8 changes the checksum
even though the string stays the same.

Fixes: 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes: dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Fixes: 873a2208ea31 ("objtool/klp: Calculate object checksums")
Signed-off-by: Longjun Luo <luolongjuna@gmail.com>
---
 tools/objtool/klp-checksum.c                  | 43 ++++++++++++++-----
 tools/objtool/klp-diff.c                      | 42 +++++++++++++++---
 .../tests/generic/fixtures/checksum_insn.c    | 15 +++++++
 .../tests/generic/test-checksum-insn.sh       | 21 +++++++--
 4 files changed, 101 insertions(+), 20 deletions(-)

diff --git a/tools/objtool/klp-checksum.c b/tools/objtool/klp-checksum.c
index 2547e142794b..05437c52c26a 100644
--- a/tools/objtool/klp-checksum.c
+++ b/tools/objtool/klp-checksum.c
@@ -67,6 +67,36 @@ static bool is_anonymous_const_data(struct symbol *sym)
 	       (is_sec_sym(sym) || strstarts(sym->name, ".LC"));
 }
 
+/*
+ * Encode a string reference as [byte offset, whole string]. This can express
+ * differences in either the offset or the string.
+ *
+ * - For a string sentinel, the compiler uses this relocation for a boundary
+ *   check without reading from the referenced address.
+ * - The referenced part stays the same but the whole string changes, for
+ *   example when the code uses &s[1] and s changes from "kfree" to "vfree".
+ */
+static void checksum_update_string(struct symbol *sym, struct instruction *insn,
+				   struct reloc *reloc, s64 offset)
+{
+	struct symbol *target = reloc->sym;
+	unsigned long start = is_sec_sym(target) ? offset : target->offset;
+	s64 addend = is_sec_sym(target) ? 0 : offset;
+	const char *str = target->sec->data->d_buf + start;
+	size_t len = elf_strlen(target->sec, start);
+
+	if (insn) {
+		__checksum_update_insn(sym, insn, &addend, sizeof(addend));
+		__checksum_update_insn(sym, insn, str, len);
+	} else {
+		unsigned long sym_offset = reloc_offset(reloc) - sym->offset;
+
+		__checksum_update_object(sym, sym_offset, "reloc addend",
+					 &addend, sizeof(addend));
+		__checksum_update_object(sym, sym_offset, "reloc string", str, len);
+	}
+}
+
 static void checksum_update_insn(struct objtool_file *file, struct symbol *func,
 				 struct instruction *insn)
 {
@@ -135,11 +165,7 @@ static void checksum_update_insn(struct objtool_file *file, struct symbol *func,
 	offset = arch_insn_adjusted_addend(insn, reloc);
 
 	if (is_string_sec(sym->sec)) {
-		char *str;
-
-		str = sym->sec->data->d_buf + sym->offset + offset;
-		__checksum_update_insn(func, insn, str,
-				       elf_strlen(sym->sec, sym->offset + offset));
+		checksum_update_string(func, insn, reloc, offset);
 		goto alts;
 	}
 
@@ -212,12 +238,7 @@ static void checksum_update_object(struct objtool_file *file, struct symbol *sym
 		offset = reloc_addend(reloc);
 
 		if (is_string_sec(target->sec)) {
-			char *str;
-
-			str = target->sec->data->d_buf + target->offset + offset;
-			__checksum_update_object(sym, sym_offset,
-						 "reloc string", str,
-						 elf_strlen(target->sec, target->offset + offset));
+			checksum_update_string(sym, NULL, reloc, offset);
 			continue;
 		}
 
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index dd5c4d604be3..c32054bff84e 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -923,6 +923,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 
 	if (data_too && !is_undef_sym(patched_sym)) {
 		struct section *patched_sec = patched_sym->sec;
+		size_t sym_size = patched_sym->len;
 
 		out_sec = find_section_by_name(elf, patched_sec->name);
 		if (!out_sec) {
@@ -935,7 +936,12 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 				return NULL;
 		}
 
-		if (is_string_sec(patched_sym->sec)) {
+		/* String at the label will be copied as normal data. */
+		if (is_string_sec(patched_sec) && !is_sec_sym(patched_sym) && !sym_size)
+			sym_size = elf_strlen(patched_sec, patched_sym->offset) +
+				   (patched_sec->sh.sh_entsize ?: 1);
+
+		if (is_string_sec(patched_sec) && is_sec_sym(patched_sym)) {
 			out_sym = elf_create_section_symbol(elf, out_sec);
 			if (!out_sym)
 				return NULL;
@@ -946,7 +952,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 		if (!is_sec_sym(patched_sym))
 			offset = ALIGN(sec_size(out_sec), out_sec->sh.sh_addralign);
 
-		if (patched_sym->len || is_sec_sym(patched_sym)) {
+		if (sym_size || is_sec_sym(patched_sym)) {
 			void *data = NULL;
 			size_t size;
 
@@ -957,7 +963,7 @@ static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym
 			if (is_sec_sym(patched_sym))
 				size = sec_size(patched_sym->sec);
 			else
-				size = patched_sym->len;
+				size = sym_size;
 
 			if (!elf_add_data(elf, out_sec, data, size))
 				return NULL;
@@ -1258,6 +1264,26 @@ static bool klp_reloc_needed(struct reloc *patched_reloc)
 	return true;
 }
 
+/*
+ * String relocations can be used for string loads or sentinel checks.
+ * For a string sentinel, we must keep its offset from the string's start.
+ * To achieve this, keep both relocations relative to the same string label
+ * when it can be identified.
+ */
+static struct symbol *find_string_label(struct reloc *reloc)
+{
+	struct symbol *sym = reloc->sym;
+	struct section *sec = sym->sec;
+
+	if (!is_string_sec(sec) || !(sec->sh.sh_flags & SHF_MERGE))
+		return NULL;
+
+	if (is_sec_sym(sym))
+		sym = find_symbol_by_offset(sec, arch_adjusted_addend(reloc));
+
+	return sym && is_local_label(sym) ? sym : NULL;
+}
+
 /* Return -1 error, 0 success, 1 skip */
 static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc)
 {
@@ -1290,6 +1316,10 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 	if (sym && sym->len == sec_size(sec))
 		goto found_sym;
 
+	sym = find_string_label(reloc);
+	if (sym)
+		goto found_sym;
+
 	/* No dedicated section; find the symbol manually */
 	sym = find_symbol_containing_inclusive(sec, arch_adjusted_addend(reloc));
 	if (!sym) {
@@ -1327,7 +1357,7 @@ static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc)
 
 /*
  * Sections with anonymous or uncorrelated data (strings, UBSAN data, Clang
- * anonymous constants) need section symbol references.
+ * anonymous constants) normally use section symbol references.
  */
 static bool is_uncorrelated_section(struct section *sec)
 {
@@ -1348,7 +1378,7 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
 	if (reloc_type(reloc) == R_NONE)
 		return 1;
 
-	if (is_uncorrelated_section(sec))
+	if (is_uncorrelated_section(sec) && !find_string_label(reloc))
 		return convert_reloc_sym_to_secsym(elf, reloc);
 
 	/* Everything else: references should use named symbols. */
@@ -1599,7 +1629,7 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 	 * "offset is outside the section".
 	 * So for this case, create temp symbol to use symbol-based relocation.
 	 */
-	if (is_string_sec(patched_sym->sec)) {
+	if (is_string_sec(patched_sym->sec) && is_sec_sym(patched_sym)) {
 		long str_off = arch_adjusted_addend(patched_reloc);
 		const char *str = patched_sym->sec->data->d_buf + str_off;
 		struct section *out_sec = out_sym->sec;
diff --git a/tools/objtool/tests/generic/fixtures/checksum_insn.c b/tools/objtool/tests/generic/fixtures/checksum_insn.c
index 70c2e9106f8a..5e794df2c7de 100644
--- a/tools/objtool/tests/generic/fixtures/checksum_insn.c
+++ b/tools/objtool/tests/generic/fixtures/checksum_insn.c
@@ -50,6 +50,21 @@ void set_priv(int i, int v)
 #define MESSAGE "original"
 #endif
 
+#ifdef STRING_END
+int walk(const char *start, const char *end);
+
+int range_string(void)
+{
+	const char *p = "[range ";
+	const char *end = p + sizeof("[range ");
+
+#if defined(MOVE_END) && !defined(PATCHED)
+	end--;
+#endif
+	return walk(p, end);
+}
+#endif
+
 int target(int x)
 {
 	int r;
diff --git a/tools/objtool/tests/generic/test-checksum-insn.sh b/tools/objtool/tests/generic/test-checksum-insn.sh
index 692ab1b174b2..08b9dafc2088 100755
--- a/tools/objtool/tests/generic/test-checksum-insn.sh
+++ b/tools/objtool/tests/generic/test-checksum-insn.sh
@@ -4,9 +4,10 @@
 # What a function's checksum has to cover beyond its instruction bytes.
 #
 # checksum_update_insn() hashes the raw bytes, and then what any relocation on
-# the instruction refers to: a string section contributes the string's
-# contents, anything else the target symbol's name and the adjusted addend,
-# with a reference to a static resolved back through its section symbol first.
+# the instruction refers to: a string section contributes the byte offset
+# within the string and its full contents, anything else the target symbol's
+# name and the adjusted addend, with a reference to a static resolved back
+# through its section symbol first.
 #
 # None of these show up in the bytes.  A rel32 operand is zero in the object
 # and supplied by the relocation, so every change below leaves the encoded
@@ -64,4 +65,18 @@ if $READELF -h "$workdir/patched.o" | grep -q 'Machine:.*X86-64'; then
 		fail "wide_string() lost its PC-relative string reference"
 fi
 
+# Place MESSAGE after "[range " to catch hashing the next literal at the loop bound.
+string_flags=(-fno-pic -fno-data-sections)
+build_pair checksum_insn.c -DSTRING_END -DSTR_CONTENT "${string_flags[@]}"
+assert_checksum_matches range_string
+
+# Move the bound from the terminating NUL to one past it.
+build_pair checksum_insn.c -DSTRING_END -DMOVE_END "${string_flags[@]}"
+assert_checksum_differs range_string
+run_diff
+section_relocs .text.range_string | awk '
+	$5 ~ /^\.L/ && $6 == "+" { base[$7] = $4 " " $5 }
+	END { exit !(0 in base && 8 in base && base[0] == base[8]) }' ||
+	fail "range_string() lost the common base of its two references"
+
 pass "instruction checksums cover reloc targets, addends and string contents"
-- 
2.53.0


      reply	other threads:[~2026-11-26 10:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 10:53 [PATCH 1/2] objtool/klp: Fix the offset and length used to read SHF_STRINGS data Longjun Luo
2026-11-26 10:53 ` Longjun Luo [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=20261126105325.2521938-2-luolongjuna@gmail.com \
    --to=luolongjuna@gmail.com \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mbenes@suse.cz \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=puranjay@kernel.org \
    --cc=song@kernel.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