* [PATCH 1/2] objtool/klp: Fix the offset and length used to read SHF_STRINGS data
@ 2026-11-26 10:53 Longjun Luo
2026-11-26 10:53 ` [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels Longjun Luo
0 siblings, 1 reply; 2+ messages in thread
From: Longjun Luo @ 2026-11-26 10:53 UTC (permalink / raw)
To: jpoimboe, peterz
Cc: mbenes, pmladek, joe.lawrence, song, puranjay, nathan,
ndesaulniers, morbo, justinstitt, live-patching, llvm,
linux-kernel, Longjun Luo
For offset, handle PC-relative references, like the one in
proc_ns_dir_lookup() with Clang ThinLTO. Use arch_adjusted_addend()
to read the string, and generate symbol-based relocations when needed.
For length, add support for wide strings. The u16 psr_setup_time_us[]
table is placed in a wide-string section by Clang. The table
occupies 14 bytes, while strlen() gives objtool a length of only 5 bytes.
With GCC and Clang, the test catches both wide string truncation and a
missing PC-relative adjustment.
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>
---
Ran the test suite on x86-64 (tip/objtool/core, b99a5337da3c).
Same results with patch 1 alone and with both patches.
GCC 12.2.0 (-fno-pie) 49 passed, 4 skipped
Clang 17.0.6 51 passed, 2 skipped
With this GCC's default PIE, the same 35 tests fail with or without
the patches.
Codex helped write the new tests.
tools/objtool/elf.c | 27 +++++++++++++-
tools/objtool/include/objtool/elf.h | 1 +
tools/objtool/klp-checksum.c | 6 ++-
tools/objtool/klp-diff.c | 37 ++++++++++++++++---
.../tests/generic/fixtures/checksum_insn.c | 11 ++++++
.../tests/generic/test-checksum-insn.sh | 18 +++++++++
6 files changed, 91 insertions(+), 9 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index a791f4ea6ec1..4a8698114666 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1382,9 +1382,32 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
return elf;
}
+static size_t __elf_strlen(const char *str, size_t width, size_t size)
+{
+ size_t len;
+
+ for (len = 0; size - len >= width; len += width) {
+ if (!memchr_inv(str + len, 0, width))
+ return len;
+ }
+ return 0;
+}
+
+/*
+ * Same as strlen, add support for wide strings like:
+ * u16 psr_setup_time_us[] from the DRM DisplayPort helpers.
+ * Clang puts this data into a wide string section with sh_entsize = 2.
+ */
+size_t elf_strlen(struct section *sec, unsigned long offset)
+{
+ return __elf_strlen(sec->data->d_buf + offset, sec->sh.sh_entsize ?: 1,
+ sec_size(sec) - offset);
+}
+
unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
{
unsigned int offset;
+ size_t width, len;
if (!strtab)
strtab = find_section_by_name(elf, ".strtab");
@@ -1399,8 +1422,10 @@ unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char
}
offset = ALIGN(sec_size(strtab), strtab->sh.sh_addralign);
+ width = strtab->sh.sh_entsize ?: 1;
+ len = __elf_strlen(str, width, SIZE_MAX);
- if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
+ if (!elf_add_data(elf, strtab, str, len + width))
return -1;
return offset;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index f7e6a652ee78..50d5b17907c8 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -180,6 +180,7 @@ struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec);
void *elf_add_data(struct elf *elf, struct section *sec, const void *data,
size_t size);
+size_t elf_strlen(struct section *sec, unsigned long offset);
unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
diff --git a/tools/objtool/klp-checksum.c b/tools/objtool/klp-checksum.c
index ebe25f9c5260..2547e142794b 100644
--- a/tools/objtool/klp-checksum.c
+++ b/tools/objtool/klp-checksum.c
@@ -138,7 +138,8 @@ static void checksum_update_insn(struct objtool_file *file, struct symbol *func,
char *str;
str = sym->sec->data->d_buf + sym->offset + offset;
- __checksum_update_insn(func, insn, str, strlen(str));
+ __checksum_update_insn(func, insn, str,
+ elf_strlen(sym->sec, sym->offset + offset));
goto alts;
}
@@ -215,7 +216,8 @@ static void checksum_update_object(struct objtool_file *file, struct symbol *sym
str = target->sec->data->d_buf + target->offset + offset;
__checksum_update_object(sym, sym_offset,
- "reloc string", str, strlen(str));
+ "reloc string", str,
+ elf_strlen(target->sec, target->offset + offset));
continue;
}
diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 95d0d86666d7..dd5c4d604be3 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -1584,18 +1584,43 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
return -1;
/*
- * For strings, all references use section symbols, thanks to
- * convert_reloc_sym(). clone_symbol() has cloned an empty
- * version of the string section. Now copy the string itself.
+ * For string relocations, in some cases, compilers generate
+ * PC-relative code like:
+ *
+ * xorw 0(%rip), %dx # "mnt", R_X86_64_PC32, addend -4
+ * (from proc_ns_dir_lookup(), Clang ThinLTO).
+ *
+ * In that case, str_off is not equal to the addend.
+ * To read the string, use arch_adjusted_addend instead.
+ *
+ * To write the string, we could use out_off + (addend - str_off)
+ * as the section-based addend.
+ * The addend may become negative, and make lld report
+ * "offset is outside the section".
+ * So for this case, create temp symbol to use symbol-based relocation.
*/
if (is_string_sec(patched_sym->sec)) {
- const char *str = patched_sym->sec->data->d_buf + addend;
+ 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;
+ unsigned int out_off;
__dbg_clone("\"%s\"", escape_str(str));
- addend = elf_add_string(e->out, out_sym->sec, str);
- if (addend == -1)
+ out_off = elf_add_string(e->out, out_sec, str);
+ if (out_off == -1)
return -1;
+
+ if (addend != str_off) {
+ out_sym = elf_create_symbol(e->out, ".L.str", out_sec,
+ STB_LOCAL, STT_NOTYPE, out_off, 0);
+ if (!out_sym)
+ return -1;
+
+ addend -= str_off;
+ } else {
+ addend = out_off;
+ }
}
if (!elf_create_reloc(e->out, sec, offset, out_sym, addend,
diff --git a/tools/objtool/tests/generic/fixtures/checksum_insn.c b/tools/objtool/tests/generic/fixtures/checksum_insn.c
index 10f70a74a976..70c2e9106f8a 100644
--- a/tools/objtool/tests/generic/fixtures/checksum_insn.c
+++ b/tools/objtool/tests/generic/fixtures/checksum_insn.c
@@ -76,3 +76,14 @@ int target(int x)
return r;
}
+
+#ifdef WIDE_STRING
+unsigned short wide_string(void)
+{
+#ifdef PATCHED
+ return *(const volatile unsigned short *)u"\u0200";
+#else
+ return *(const volatile unsigned short *)u"\u0100";
+#endif
+}
+#endif
diff --git a/tools/objtool/tests/generic/test-checksum-insn.sh b/tools/objtool/tests/generic/test-checksum-insn.sh
index e1c04a1f518a..692ab1b174b2 100755
--- a/tools/objtool/tests/generic/test-checksum-insn.sh
+++ b/tools/objtool/tests/generic/test-checksum-insn.sh
@@ -46,4 +46,22 @@ build_pair checksum_insn.c
run_checksum
assert_checksum_matches target
+# The wide character changes while its low byte stays zero.
+build_pair checksum_insn.c -DWIDE_STRING -fno-pic
+assert_checksum_differs wide_string
+run_diff
+for obj in patched out; do
+ $OBJCOPY -O binary --only-section='*.str2.*' "$workdir/$obj.o" \
+ "$workdir/$obj.bin" || fail "cannot extract wide string from $obj.o"
+done
+[ -s "$workdir/patched.bin" ] || fail "fixture produced no two-byte string section"
+cmp -s "$workdir/patched.bin" "$workdir/out.bin" ||
+ fail "wide string was copied incorrectly"
+
+# The x86-64 RIP-relative load needs a string label with addend -4.
+if $READELF -h "$workdir/patched.o" | grep -q 'Machine:.*X86-64'; then
+ section_relocs .text.wide_string | grep -Eq 'R_X86_64_PC32.* \.L[^ ]* - 4$' ||
+ fail "wide_string() lost its PC-relative string reference"
+fi
+
pass "instruction checksums cover reloc targets, addends and string contents"
--
2.53.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels
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
0 siblings, 0 replies; 2+ messages in thread
From: Longjun Luo @ 2026-11-26 10:53 UTC (permalink / raw)
To: jpoimboe, peterz
Cc: mbenes, pmladek, joe.lawrence, song, puranjay, nathan,
ndesaulniers, morbo, justinstitt, live-patching, llvm,
linux-kernel, Longjun Luo
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-26 10:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] objtool/klp: Fix string sentinels referenced through .L labels Longjun Luo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox