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