All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Longjun Luo" <luolongjuna@gmail.com>,
	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
Subject: Re: [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings
Date: Wed, 04 Nov 2026 14:03:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-197388@kernel.org> (raw)
In-Reply-To: <20261104133158.3187525-2-luolongjuna@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] objtool/klp: unadjusted PC-relative string addend in clone_reloc()
--

commit fd88c78f1c913d4c955b6342d4e45b4274643124
Author: Longjun Luo <luolongjuna@gmail.com>

objtool/klp: Fix checksums and copying of wide strings

This commit adds elf_string_len() to compute string lengths in SHF_STRINGS
sections using sh_entsize so that wide strings with embedded zero bytes are
not truncated by strlen() during checksum calculation and livepatch relocation
cloning.

> diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
> index 34da55e456097..6321f691adbbd 100644
> --- a/tools/objtool/klp-diff.c
> +++ b/tools/objtool/klp-diff.c
> @@ -1548,12 +1548,18 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
>  	 * version of the string section.  Now copy the string itself.
>  	 */
>  	if (is_string_sec(patched_sym->sec)) {
> -		const char *str = patched_sym->sec->data->d_buf + addend;
> +		ssize_t len = elf_string_len(patched_sym->sec, addend);
> +		const char *str;
>  
> -		__dbg_clone("\"%s\"", escape_str(str));
> +		if (len < 0)
> +			return -1;
> +		str = patched_sym->sec->data->d_buf + addend;
> +		if (patched_sym->sec->sh.sh_entsize == 1)
> +			__dbg_clone("\"%s\"", escape_str(str));
>  
> -		addend = elf_add_string(e->out, out_sym->sec, str);
> -		if (addend == -1)
> +		addend = ALIGN(sec_size(out_sym->sec), out_sym->sec->sh.sh_addralign);
> +		if (!elf_add_data(e->out, out_sym->sec, str,
> +				  len + patched_sym->sec->sh.sh_entsize))
>  			return -1;
>  	}

[Severity: Medium]
What happens in clone_reloc() when a patched function on x86_64 references a
string section via a PC-relative relocation such as R_X86_64_PC32?

In checksum_update_insn(), the relocation addend is adjusted before calling
elf_string_len():

tools/objtool/klp-checksum.c:checksum_update_insn() {
	...
	offset = arch_insn_adjusted_addend(insn, reloc);

	if (is_string_sec(sym->sec)) {
		ssize_t size = elf_string_len(sym->sec, sym->offset + offset);
	...
}

However, in clone_sym_relocs() -> convert_reloc_sym() ->
convert_reloc_sym_to_secsym() -> clone_reloc(), addend comes directly from
reloc_addend(patched_reloc), which still carries the -4 PC-relative bias on
x86_64.

When the target string is at offset 0 of the section (so addend is -4):
- For wide strings (sh_entsize > 1), passing addend as unsigned long offset to
  elf_string_len() wraps to 0xfffffffffffffffcUL, which triggers the
  offset >= sec_size(sec) error and aborts klp diff.
- For single-byte strings (sh_entsize == 1), elf_string_len() and
  elf_add_data() read 4 bytes before patched_sym->sec->data->d_buf.

When the target string is at offset >= 4, elf_string_len() starts 4 bytes
before the string (inside the preceding string's terminator, returning 0 for
4-byte wide strings), copies the wrong bytes, and then overwrites addend with
the aligned output section offset without preserving the PC-relative bias for
elf_create_reloc().

Should clone_reloc() use arch_adjusted_addend(patched_reloc) to locate and
measure the string in patched_sym->sec, and preserve the bias
(reloc_addend(patched_reloc) - arch_adjusted_addend(patched_reloc)) when
computing the new addend for elf_create_reloc()?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261104133158.3187525-1-luolongjuna@gmail.com?part=1

  reply	other threads:[~2026-11-04 14:03 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 13:31 [RFC PATCH v2 00/11] objtool/klp: DWARF-based cross-build symbol correlation Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings Longjun Luo
2026-11-04 14:03   ` sashiko-bot [this message]
2026-11-04 13:31 ` [RFC PATCH v2 02/11] objtool/klp: Hash compound literals by content Longjun Luo
2026-11-04 13:55   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 03/11] objtool/klp: Preserve identifier digits when stripping suffixes Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 04/11] objtool/klp: Read symbol identities from DWARF Longjun Luo
2026-11-04 13:51   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 05/11] objtool/klp: Support reduced and split DWARF Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 06/11] klp-build: Require CONFIG_DEBUG_INFO Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 07/11] objtool/klp: Correlate symbols using DWARF identities Longjun Luo
2026-11-04 14:09   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 08/11] objtool/klp: Include symbol identities in reference checksums Longjun Luo
2026-11-04 14:06   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 09/11] objtool/klp: Extend twins through unchanged function references Longjun Luo
2026-11-04 14:05   ` sashiko-bot
2026-11-04 13:31 ` [RFC PATCH v2 10/11] objtool/klp: Check unmatched data references Longjun Luo
2026-11-04 13:31 ` [RFC PATCH v2 11/11] objtool/klp: Test symbol identity and state preservation Longjun Luo
2026-11-04 13:50   ` sashiko-bot

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=sashiko-outbox-197388@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=live-patching@vger.kernel.org \
    --cc=luolongjuna@gmail.com \
    --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=sashiko-reviews@lists.linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.