From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796138079; b=vXl/Sr7s/soGix7KB7jU18qElOayWYJ1CPLmfP9oycyvGznvl069BLDfSC2Flx5XPNiS GAeqIKpIJBDMZ9wJK8cfpRqapHJMrAzU77Ehr2/y3xv/YxD1gA3QTt4X0YcySbHPRek1u cBpMSi07kh3pctGV5nuTXWnXMu0P1BapjIro4EOW8KbGYDXmFwJ731J43jnhPll4wcFlQ X8sHUxDU6cH7gn+MMjsDRqOTUjZ+UyOUvyv1ZPx7R6eJCNjtLOUjPXkNNM5vjL2HnLdyG MGM36UR79cqj7kSfrmTe5yk3Cr0PjuAIm+tARfsoFRJQB0NCWtpCbEP9BhrLoCATWzQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796138079; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=g+DcWrzAnXcgHdg0DyzVhVRA/NboSiyFp6V7bZRcJ2I=; b=Ceh6kD0jpDx3Np+FwoWriT8EsPfvkMdeFu6EMteH+WXSw8yp9HgFOz+S4wyJ3BVtD79B 78qyuXHMD9yz/ogHKwJVy7CiS0PgxYd5Ncq5PqxslmZgsZL+2LGutRTURFai1j9DMMstH vQxzvzmHlSO/Pljoc0eBILQpOhk2Y+hD4Dq5I+0vG0CznPURMNlgKOfoyatqPRat/XUle cZExSegIbdnnM6uXGn66aom5kq5dqD+DfeoKikyCoZzycl158aB0qQivV2R7szf44CrcG gZYrVGSDRu3uSi0+69qgSk8ZUqSY47RgDQN7h3zKRyWYyM3weDXfB0JK59JDU7ph95w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=v5IusnvV; arc=none smtp.remote-ip=209.85.221.49 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C122606A322 for ; Tue, 1 Dec 2026 15:14:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3C122606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=v5IusnvV Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso5309944f8f.2 for ; Tue, 01 Dec 2026 07:14:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796138077; x=1796742877; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=g+DcWrzAnXcgHdg0DyzVhVRA/NboSiyFp6V7bZRcJ2I=; b=v5IusnvVp2tgZEY4A4o5V2L7f6AZpPmajYp8nSKX1/JJiupD0kEv8wvbspA9k4iJs9 NN52zf70ImwBTRU80UJ09BBCHofpsb1jbArGAIi/v5sg3vt52hGs3hXIDFUQT9M2HH8T VonSoPlfsAWvVdJ2rebE8ZcBEK3pR+zguDChF8ZLC1WhBP5UQ+I+suIUBkGAID0seRCF 3wF0sV4ogRCeShVp5v7twNuvFXstwxqi0Tqq/15dmNG9f1lO1h2Qn8yy4SoQl9Q4sz/M /8O1Qu5MvKGzMgYZOMzyQRgp41qFcptgpdIQNPOB35hZuyJuC3vYYZDnXbeHcs5WJprs zffQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796138077; x=1796742877; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=g+DcWrzAnXcgHdg0DyzVhVRA/NboSiyFp6V7bZRcJ2I=; b=dERVUjeJY67uFP5UnR87BycHmye7Y8rq4dBrex/WmvANh7O0XV9z2pRW5fPCeLZOmV UbhmugcgRbiZflr88GkCHOh+ThaDol3+Csk5DbDNsZtvtMcz9lOvpkbL0g1vgNeK+Y1/ 98nqVw7Ox8r6VtnLfnrt+uQuqh/fGk3+3GDu7rXBs1y79+GYsbvYTShOLqWphY3RCfaW Yj3ISTDVeTF5wxgRhicQAaJXFiNjgTe+Q1J5Pkkm8cqVejZXfvmTG78GU6ePR1TZI9l9 +NoX05CJXBK1Pz3C9qGwtQ+w+2lzYAlHZDysN0mfsP62cFEwkgHj/XWQaDrEtLVji6Ht mGWw== X-Gm-Message-State: APuU4Z+YkI/WWlJupvd8o84Y3iD+qpPHMXGmZ0MEd0qFoHZeeNPtDpSJ 99h0h5nla/nzlDlHQw7P4fLGEgnh+aq5xKepZNhb2R0T0W9HVEwvkd8z X-Gm-Gg: AZFdTZc/c3FafhyWnsVqkX5Xh1ita3sFpUHPFHC2YXTuQb7B3hbVGL2Gydm3f0vRyZL 8wVHT+iIz8Xx+Ua3I9v7kiDH1I9y47PxqgamiUyNqEEM925JfRlzd0go8tF/2nCUbB61MMxEAWC L0rGbH/xzbcloMjWL2Rww4gzqMyRj6o7bF3pQtjKpaUg06BciMSBr6pz2nX66FeKrsGOjs26C2+ jIUxPE6OVZ4zRxCDA4CRH1+eNdQyXwqswX/A94SGLR0n5ur/6ZPwsPrCpgSjIS4grUw/vz0Zgdr 68maNRwVef1/Z2lqNxpu5wZ69HGlZgqXy/A47TlgfDGrfpFC0DUJ6AeWwp52ogHaLjUXncTyjcB /9k1aEH82wc/sp0sEsOw+n5MsHhnEggqP6CXU/lmvSa+bT+zMr2WV9My7FNbaIJ7eeE4+5nz42c P39bEqEGqxCd3dKTp8yvr3rVM6KmWz8Zj8Yua0pCoUWXE9te5MK/BNvDYjqNYgbx/PRvgLtWzJu yGs1JuTOPbK4Ylmc0X6fzqUqWC0jNmVjrWuVl7opy5GG2S8cwgUYjDDH5GMlZrTQdgoZLrVOqs3 cN2GD2eJHIBc6JZI06jOd0INyNbqkI3QXigqGS2pzB5y8hiueiY+ELX5LCA9iBuZO4nRGynLK3F jlC/ZNj6u7+qH38TtdI3BfWngH/ziMUyMzCtGHmlFWR/zu0cmBo9ptg== X-Received: by 2002:a05:6000:186f:b0:4ca:79b7:2a5d with SMTP id ffacd0b85a97d-4ca79b72cc4mr4067344f8f.26.1796138077355; Tue, 01 Dec 2026 07:14:37 -0800 (PST) Received: from Developer ([216.234.217.221]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b2052c8sm72209346f8f.41.2026.12.01.07.14.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 07:14:36 -0800 (PST) From: Seke Kazuru To: joe.lawrence@redhat.com Cc: live-patching@vger.kernel.org, jpoimboe@kernel.org, mbenes@suse.cz, pmladek@suse.com, song@kernel.org, peterz@infradead.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] objtool/klp: Add test for computed-goto jump tables Date: Tue, 1 Dec 2026 17:14:30 +0200 Message-ID: <20261201151430.1712-1-kazuruuni@gmail.com> X-Mailer: git-send-email 2.51.0.windows.1 In-Reply-To: References: X-Mailing-List: live-patching@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ___bpf_prog_run() dispatches through a static local table of label addresses. Every entry points into the function itself, so the table describes one build of the code rather than state shared with the running kernel, but klp diff correlates it like any other static local. That goes wrong in two ways. When a patch leaves every label in place the table is unchanged, the patched function is pointed at the running kernel's copy, and every dispatch lands in the original handlers: the module builds, loads and reports "patching complete", and the fix never runs. When a patch moves a label the correlated table has changed, and klp diff rejects the patch with "changed data", so no fix to such a function can be built. The fixture is an interpreter in the shape of ___bpf_prog_run(), its table filled with a default first as BPF_INSN_MAP() does, built with the table in ordinary read-only data and in .data.rel.ro.c_jump_table, where __annotate_jump_table puts it. The test runs five segments, labels moving and labels staying, the latter also in a module, and asserts the premise of each before the result: the patched function carries its own copy of the table, relocated into the new code; an ordinary static local of the same function is still correlated; and the table of an unchanged function is not pulled in. The segments where no label moves come last, as only they depend on the compiler's layout. The change they make is in the last block: an edit to an earlier handler that looked the same size still moved the labels after it under Clang. Verified with gcc 11 to 16 and Clang 15 to 19 and 21 to 23 on x86_64, and with aarch64 gcc 13 and Clang 18 on Josh Poimboeuf's klp-build-arm64-latest: the test fails on mainline; passes with "jumptable" added to is_uncorrelated_static_local(), and with a rule that leaves uncorrelated any static local whose relocations point inside a function rather than at its start; fails at the correlated-static check when no static local is correlated; and reports a probe skip, never a pass, when the fixture's labels are removed. The same holds with the flags of -Os, IBT, retpoline, call padding, kCFI and UBSAN bounds, except that Clang with retpolines lowers the computed goto to a switch and keeps no code address in the table, which the test reports as a probe skip. On kernel/bpf/core.o from 7.4-rc5 both forms show the same way: a change to the ALU_NEG handler is rejected with "changed data", and a change to the text of the pr_warn() in default_label builds with ___bpf_prog_run() pointing at the running kernel's jumptable. Link: https://lore.kernel.org/live-patching/CAJATrkz6CgsaAFjeGT_1TuUTxrpVYEo=9rGuobnhpU-LPxv_dA@mail.gmail.com/ Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Seke Kazuru --- .../generic/fixtures/computed_goto_table.c | 115 +++++++++++++++ .../tests/generic/test-computed-goto-table.sh | 137 ++++++++++++++++++ 2 files changed, 252 insertions(+) create mode 100644 tools/objtool/tests/generic/fixtures/computed_goto_table.c create mode 100755 tools/objtool/tests/generic/test-computed-goto-table.sh diff --git a/tools/objtool/tests/generic/fixtures/computed_goto_table.c b/tools/objtool/tests/generic/fixtures/computed_goto_table.c new file mode 100644 index 0000000000..e64cb2ff4a --- /dev/null +++ b/tools/objtool/tests/generic/fixtures/computed_goto_table.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A computed-goto interpreter in the shape of ___bpf_prog_run(): a static + * table of label addresses, filled with a default and then set per opcode, + * and dispatched by "goto *jumptable[op]". + * + * Every entry is the address of a label inside interp() itself, so the table + * describes this build of the function rather than holding state the running + * kernel has accumulated. + * + * The patch changes interp() in one of two ways: + * + * - by default it changes the op_div handler, which moves the labels laid + * out after it, so the table's contents change too; + * - with SAME_SIZE it changes only the value default_label returns, in the + * last block, so no label moves and the table is identical in both builds. + * + * ANNOTATE puts the tables where the kernel's __annotate_jump_table does with + * CONFIG_OBJTOOL. Without it they land in ordinary read-only data, as they + * do on a kernel built without objtool. + * + * runs is an ordinary static local of the same function, which must go on + * being the running kernel's copy. other() has a table of its own and the + * patch does not touch it. + */ + +#ifndef MODNAME +#define MODNAME "vmlinux" +#endif + +/* klp diff takes the object's module name from .modinfo */ +static const char __modinfo[] + __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=" MODNAME; + +#ifdef ANNOTATE +#define __jump_table_sec __attribute__((section(".data.rel.ro.c_jump_table"))) +#else +#define __jump_table_sec +#endif + +struct insn { + unsigned char code; + int imm; +}; + +enum { OP_ADD = 1, OP_SUB, OP_DIV, OP_EXIT }; + +long interp(const struct insn *insn, long acc) +{ + static unsigned long runs; + /* + * Filled with a default first, as BPF_INSN_MAP() does, so that every + * entry carries a relocation to a label of this function. + */ + static const void * const jumptable[256] __jump_table_sec = { + [0 ... 255] = &&default_label, + [OP_ADD] = &&op_add, + [OP_SUB] = &&op_sub, + [OP_DIV] = &&op_div, + [OP_EXIT] = &&op_exit, + }; + + runs++; + +select_insn: + goto *jumptable[insn->code]; + +op_add: + acc += insn->imm; + insn++; + goto select_insn; +op_sub: + acc -= insn->imm; + insn++; + goto select_insn; +op_div: +#if defined(PATCHED) && !defined(SAME_SIZE) + acc = insn->imm ? acc / insn->imm : 0; +#else + acc = acc / insn->imm; +#endif + insn++; + goto select_insn; +op_exit: + return acc + runs; +default_label: + /* + * The SAME_SIZE change has to be here, in the last block: an edit to + * an earlier handler that looks the same size can still move the labels + * after it (Clang laid out "+ 1" and "+ 2" in op_add differently). + */ +#if defined(PATCHED) && defined(SAME_SIZE) + return -2; +#else + return -1; +#endif +} + +long other(const unsigned char *op, long acc) +{ + static const void * const table[4] __jump_table_sec = { + &&o_end, &&o_inc, &&o_dec, &&o_end, + }; + +next: + goto *table[*op++ & 3]; +o_inc: + acc++; + goto next; +o_dec: + acc--; + goto next; +o_end: + return acc; +} diff --git a/tools/objtool/tests/generic/test-computed-goto-table.sh b/tools/objtool/tests/generic/test-computed-goto-table.sh new file mode 100755 index 0000000000..46789654c9 --- /dev/null +++ b/tools/objtool/tests/generic/test-computed-goto-table.sh @@ -0,0 +1,137 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# A computed-goto table travels with the function it belongs to. +# +# ___bpf_prog_run() dispatches through a static local table of label +# addresses. Those addresses point into the function itself, so the table +# describes one build of the code; it is not state to share with the running +# kernel. klp diff correlates it like any other static local, and that goes +# wrong in two ways: +# +# - When the patch leaves every label where it was, the table's contents +# are unchanged, the patched function is pointed at the running kernel's +# copy, and every dispatch lands in the original handlers. The module +# builds, loads and reports "patching complete", but the fix never runs. +# - When the patch moves a label, the correlated table has changed, and klp +# diff rejects the patch with "changed data". No fix to such a function +# can be built. +# +# A correct patch carries the table with the function, its entries pointing +# into the new code, in both cases. Nothing fails at build or load time in the +# first, so only a test notices. +# +# Report: https://lore.kernel.org/live-patching/CAJATrkz6CgsaAFjeGT_1TuUTxrpVYEo=9rGuobnhpU-LPxv_dA@mail.gmail.com/ +# +# Load-bearing in the fixture: the table is filled with a default first so +# that every entry has a relocation into interp(); the SAME_SIZE change is in +# the last block, because an edit to an earlier handler moved the labels after +# it under Clang although it looked the same size; and the table is named +# jumptable, as in the kernel. Both directions are checked: an ordinary +# static local of the same function must still be correlated, and a table of +# an unchanged function must not be pulled in. + +. "$(dirname "$0")/../lib.sh" + +# gcc names a static local ., Clang .. +local_sym() +{ + in_symbols orig.o | awk -v v="$1" -v f="$2" ' + $4 == "OBJECT" && ($NF ~ ("^" v "[.][0-9]+$") || $NF == f "." v) { + print $NF + exit + }' +} + +# The section a symbol of out.o is defined in. +out_sym_section() +{ + local ndx + + ndx="$(out_symbols | awk -v n="$1" '$NF == n && $7 ~ /^[0-9]+$/ { print $7; exit }')" + [ -n "$ndx" ] || return 0 + out_sections | sed 's/^ *\[ *//; s/\]//' | awk -v i="$ndx" '$1 == i { print $2; exit }' +} + +# check [cflags...]: one build of the fixture, judged on both sides. +check() +{ + local same_size= table runs other_table sec + + case " $* " in *" -DSAME_SIZE "*) same_size=1 ;; esac + + # -Wno-override-init stands in for kernel/bpf/Makefile, which builds + # core.o with it: the table is filled with a default and then + # overridden per opcode, which Clang warns about by default. + setup + if [ -n "${MODULE:-}" ]; then + build_module_pair computed_goto_table.c "$MODULE" -Wno-override-init "$@" + else + build_pair computed_goto_table.c -Wno-override-init "$@" + fi + run_checksum + + table="$(local_sym jumptable interp)" + runs="$(local_sym runs interp)" + other_table="$(local_sym table other)" + [ -n "$table" ] && [ -n "$runs" ] && [ -n "$other_table" ] || + fail "[$*] fixture produced no static local symbols" \ + "(jumptable '$table', runs '$runs', table '$other_table')" + assert_input_symbol "$table" + + # The premise: the table's entries are relocations into interp's own + # text, and the build pair differs the way this segment needs. Not + # every configuration keeps a table of code addresses: with retpolines + # Clang lowers the computed goto to a switch, and the table holds no + # address at all, so there is nothing for this test to check. + in_relocs orig.o | + awk '/^Relocation section/ { p = 0 } p; /^Relocation section/ && /data/ { p = 1 }' | + grep -q '[[:space:]]\.text\.interp[[:space:]]' || + probe_skip "[$*] this compiler built the table without code addresses" \ + "(Clang does with retpolines)" + [ "$(checksum_of orig.o interp)" != "$(checksum_of patched.o interp)" ] || + fail "[$*] the patch did not change interp" + if [ -n "$same_size" ]; then + [ "$(checksum_of orig.o "$table")" = "$(checksum_of patched.o "$table")" ] || + probe_skip "[$*] the SAME_SIZE patch moved a label under this compiler" + else + [ "$(checksum_of orig.o "$table")" != "$(checksum_of patched.o "$table")" ] || + fail "[$*] the patch moved no label, so the table did not change" + fi + + run_diff + + assert_patched interp + assert_not_patched other + + # The table is the patch's own copy, pointing into the new interp + # (klp diff names it by the function symbol, interp + ). + assert_no_klp_sym "$table" + assert_symbol "$table" + sec="$(out_sym_section "$table")" + [ -n "$sec" ] || fail "[$*] '$table' is not defined in the output" + section_relocs "$sec" | awk '$5 == "interp" || $5 == ".text.interp"' | grep -q . || + fail "[$*] '$table' in the output does not point into the patched interp" + + # The other direction: state is still shared, and the table of a + # function the patch does not change stays out. + assert_klp_sym "$runs" "${MODULE:-vmlinux}" + assert_no_symbol "$other_table" + + cleanup +} + +# Labels move: rejected as "changed data" today. Unconditional, so the test +# can never skip as a whole. +check +check -DANNOTATE + +# Labels stay: the silent case. Built last, since a compiler which moves a +# label anyway can only skip it. +check -DSAME_SIZE +check -DANNOTATE -DSAME_SIZE + +# The same in a module: the state it shares is the module's. +MODULE=klp_test_mod check -DANNOTATE -DSAME_SIZE + +pass "computed-goto table carried with its function, whether or not its labels move" -- 2.43.0