xref: /linux/tools/objtool/tests/generic/test-jump-label-module-static-key.sh (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1*58b0d663SSong Liu#!/bin/bash
2*58b0d663SSong Liu# SPDX-License-Identifier: GPL-2.0
3*58b0d663SSong Liu#
4*58b0d663SSong Liu# A static branch key owned by a module is rejected whether the key is global
5*58b0d663SSong Liu# or file-local.
6*58b0d663SSong Liu#
7*58b0d663SSong Liu# The rejection matters because late module patching allows the livepatch
8*58b0d663SSong Liu# module to load before the module it depends on: the __jump_table klp reloc is
9*58b0d663SSong Liu# then unresolved, and jump_label_add_module() dereferences an uninitialized
10*58b0d663SSong Liu# pointer.  Catching it at build time is the only defence.
11*58b0d663SSong Liu#
12*58b0d663SSong Liu# test-jump-label-module-key covers the global key.  A file-local one reaches
13*58b0d663SSong Liu# the same check by a different route: the compiler emits the reference against
14*58b0d663SSong Liu# the section symbol plus an addend, so validate_special_section_klp_reloc()
15*58b0d663SSong Liu# has to resolve it to the underlying object before it can see a key at all.
16*58b0d663SSong Liu# Until it did, a static key was passed over as "not STT_OBJECT" and the
17*58b0d663SSong Liu# unsupported reference was emitted with nothing said.
18*58b0d663SSong Liu#
19*58b0d663SSong Liu# Fixed by f9fb44b0ecef ("objtool/klp: Fix detection of corrupt static
20*58b0d663SSong Liu# branch/call entries").
21*58b0d663SSong Liu
22*58b0d663SSong Liu. "$(dirname "$0")/../lib.sh"
23*58b0d663SSong Liu
24*58b0d663SSong Liusetup
25*58b0d663SSong Liubuild_pair jump_label.c -DSTATIC_KEY -DMODNAME='"klp_testmod"'
26*58b0d663SSong Liu
27*58b0d663SSong Liurequire_input_section __jump_table
28*58b0d663SSong Liu
29*58b0d663SSong Liu# The premise: the key is reached through its section symbol, not by name.
30*58b0d663SSong Liu# Without that this is just a second copy of test-jump-label-module-key.
31*58b0d663SSong Liuinput_jump_relocs="$(in_relocs orig.o | awk '/rela__jump_table/,/^$/')"
32*58b0d663SSong Liu
33*58b0d663SSong Liuecho "$input_jump_relocs" | grep -q klp_test_key ||
34*58b0d663SSong Liu	fail "fixture produced no __jump_table reference to the key"
35*58b0d663SSong Liuecho "$input_jump_relocs" | grep -qE '\.(bss|data)\.klp_test_key' ||
36*58b0d663SSong Liu	probe_skip "compiler referenced the static key by name, not through its section"
37*58b0d663SSong Liu
38*58b0d663SSong Liurun_diff 255
39*58b0d663SSong Liu
40*58b0d663SSong Liudiff_log | grep -q 'unsupported static branch key klp_test_key' ||
41*58b0d663SSong Liu	fail "expected rejection, got: $(diff_log | tail -1)"
42*58b0d663SSong Liu[ -e "$workdir/out.o" ] &&
43*58b0d663SSong Liu	fail "output object produced for a rejected input"
44*58b0d663SSong Liu
45*58b0d663SSong Liupass "module-owned file-local static branch key rejected"
46