1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun #include <string.h>
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include "../../special.h"
5*4882a593Smuzhiyun #include "../../builtin.h"
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #define X86_FEATURE_POPCNT (4 * 32 + 23)
8*4882a593Smuzhiyun #define X86_FEATURE_SMAP (9 * 32 + 20)
9*4882a593Smuzhiyun
arch_handle_alternative(unsigned short feature,struct special_alt * alt)10*4882a593Smuzhiyun void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
11*4882a593Smuzhiyun {
12*4882a593Smuzhiyun switch (feature) {
13*4882a593Smuzhiyun case X86_FEATURE_SMAP:
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * If UACCESS validation is enabled; force that alternative;
16*4882a593Smuzhiyun * otherwise force it the other way.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * What we want to avoid is having both the original and the
19*4882a593Smuzhiyun * alternative code flow at the same time, in that case we can
20*4882a593Smuzhiyun * find paths that see the STAC but take the NOP instead of
21*4882a593Smuzhiyun * CLAC and the other way around.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun if (uaccess)
24*4882a593Smuzhiyun alt->skip_orig = true;
25*4882a593Smuzhiyun else
26*4882a593Smuzhiyun alt->skip_alt = true;
27*4882a593Smuzhiyun break;
28*4882a593Smuzhiyun case X86_FEATURE_POPCNT:
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun * It has been requested that we don't validate the !POPCNT
31*4882a593Smuzhiyun * feature path which is a "very very small percentage of
32*4882a593Smuzhiyun * machines".
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun alt->skip_orig = true;
35*4882a593Smuzhiyun break;
36*4882a593Smuzhiyun default:
37*4882a593Smuzhiyun break;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
arch_support_alt_relocation(struct special_alt * special_alt,struct instruction * insn,struct reloc * reloc)41*4882a593Smuzhiyun bool arch_support_alt_relocation(struct special_alt *special_alt,
42*4882a593Smuzhiyun struct instruction *insn,
43*4882a593Smuzhiyun struct reloc *reloc)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * The x86 alternatives code adjusts the offsets only when it
47*4882a593Smuzhiyun * encounters a branch instruction at the very beginning of the
48*4882a593Smuzhiyun * replacement group.
49*4882a593Smuzhiyun */
50*4882a593Smuzhiyun return insn->offset == special_alt->new_off &&
51*4882a593Smuzhiyun (insn->type == INSN_CALL || is_jump(insn));
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun * There are 3 basic jump table patterns:
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * 1. jmpq *[rodata addr](,%reg,8)
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * This is the most common case by far. It jumps to an address in a simple
60*4882a593Smuzhiyun * jump table which is stored in .rodata.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * 2. jmpq *[rodata addr](%rip)
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * This is caused by a rare GCC quirk, currently only seen in three driver
65*4882a593Smuzhiyun * functions in the kernel, only with certain obscure non-distro configs.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * As part of an optimization, GCC makes a copy of an existing switch jump
68*4882a593Smuzhiyun * table, modifies it, and then hard-codes the jump (albeit with an indirect
69*4882a593Smuzhiyun * jump) to use a single entry in the table. The rest of the jump table and
70*4882a593Smuzhiyun * some of its jump targets remain as dead code.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * In such a case we can just crudely ignore all unreachable instruction
73*4882a593Smuzhiyun * warnings for the entire object file. Ideally we would just ignore them
74*4882a593Smuzhiyun * for the function, but that would require redesigning the code quite a
75*4882a593Smuzhiyun * bit. And honestly that's just not worth doing: unreachable instruction
76*4882a593Smuzhiyun * warnings are of questionable value anyway, and this is such a rare issue.
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * 3. mov [rodata addr],%reg1
79*4882a593Smuzhiyun * ... some instructions ...
80*4882a593Smuzhiyun * jmpq *(%reg1,%reg2,8)
81*4882a593Smuzhiyun *
82*4882a593Smuzhiyun * This is a fairly uncommon pattern which is new for GCC 6. As of this
83*4882a593Smuzhiyun * writing, there are 11 occurrences of it in the allmodconfig kernel.
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * As of GCC 7 there are quite a few more of these and the 'in between' code
86*4882a593Smuzhiyun * is significant. Esp. with KASAN enabled some of the code between the mov
87*4882a593Smuzhiyun * and jmpq uses .rodata itself, which can confuse things.
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
90*4882a593Smuzhiyun * ensure the same register is used in the mov and jump instructions.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
93*4882a593Smuzhiyun */
arch_find_switch_table(struct objtool_file * file,struct instruction * insn)94*4882a593Smuzhiyun struct reloc *arch_find_switch_table(struct objtool_file *file,
95*4882a593Smuzhiyun struct instruction *insn)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct reloc *text_reloc, *rodata_reloc;
98*4882a593Smuzhiyun struct section *table_sec;
99*4882a593Smuzhiyun unsigned long table_offset;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* look for a relocation which references .rodata */
102*4882a593Smuzhiyun text_reloc = find_reloc_by_dest_range(file->elf, insn->sec,
103*4882a593Smuzhiyun insn->offset, insn->len);
104*4882a593Smuzhiyun if (!text_reloc || text_reloc->sym->type != STT_SECTION ||
105*4882a593Smuzhiyun !text_reloc->sym->sec->rodata)
106*4882a593Smuzhiyun return NULL;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun table_offset = text_reloc->addend;
109*4882a593Smuzhiyun table_sec = text_reloc->sym->sec;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (text_reloc->type == R_X86_64_PC32)
112*4882a593Smuzhiyun table_offset += 4;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * Make sure the .rodata address isn't associated with a
116*4882a593Smuzhiyun * symbol. GCC jump tables are anonymous data.
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * Also support C jump tables which are in the same format as
119*4882a593Smuzhiyun * switch jump tables. For objtool to recognize them, they
120*4882a593Smuzhiyun * need to be placed in the C_JUMP_TABLE_SECTION section. They
121*4882a593Smuzhiyun * have symbols associated with them.
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun if (find_symbol_containing(table_sec, table_offset) &&
124*4882a593Smuzhiyun strcmp(table_sec->name, C_JUMP_TABLE_SECTION))
125*4882a593Smuzhiyun return NULL;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Each table entry has a rela associated with it. The rela
129*4882a593Smuzhiyun * should reference text in the same function as the original
130*4882a593Smuzhiyun * instruction.
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun rodata_reloc = find_reloc_by_dest(file->elf, table_sec, table_offset);
133*4882a593Smuzhiyun if (!rodata_reloc)
134*4882a593Smuzhiyun return NULL;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * Use of RIP-relative switch jumps is quite rare, and
138*4882a593Smuzhiyun * indicates a rare GCC quirk/bug which can leave dead
139*4882a593Smuzhiyun * code behind.
140*4882a593Smuzhiyun */
141*4882a593Smuzhiyun if (text_reloc->type == R_X86_64_PC32)
142*4882a593Smuzhiyun file->ignore_unreachables = true;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun return rodata_reloc;
145*4882a593Smuzhiyun }
146