1 /*
2 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "builtin.h"
22 #include "check.h"
23 #include "elf.h"
24 #include "special.h"
25 #include "arch.h"
26 #include "warn.h"
27
28 #include <linux/hashtable.h>
29 #include <linux/kernel.h>
30
31 #define FAKE_JUMP_OFFSET -1
32
33 struct alternative {
34 struct list_head list;
35 struct instruction *insn;
36 };
37
38 const char *objname;
39 struct cfi_state initial_func_cfi;
40
find_insn(struct objtool_file * file,struct section * sec,unsigned long offset)41 struct instruction *find_insn(struct objtool_file *file,
42 struct section *sec, unsigned long offset)
43 {
44 struct instruction *insn;
45
46 hash_for_each_possible(file->insn_hash, insn, hash, offset)
47 if (insn->sec == sec && insn->offset == offset)
48 return insn;
49
50 return NULL;
51 }
52
next_insn_same_sec(struct objtool_file * file,struct instruction * insn)53 static struct instruction *next_insn_same_sec(struct objtool_file *file,
54 struct instruction *insn)
55 {
56 struct instruction *next = list_next_entry(insn, list);
57
58 if (!next || &next->list == &file->insn_list || next->sec != insn->sec)
59 return NULL;
60
61 return next;
62 }
63
next_insn_same_func(struct objtool_file * file,struct instruction * insn)64 static struct instruction *next_insn_same_func(struct objtool_file *file,
65 struct instruction *insn)
66 {
67 struct instruction *next = list_next_entry(insn, list);
68 struct symbol *func = insn->func;
69
70 if (!func)
71 return NULL;
72
73 if (&next->list != &file->insn_list && next->func == func)
74 return next;
75
76 /* Check if we're already in the subfunction: */
77 if (func == func->cfunc)
78 return NULL;
79
80 /* Move to the subfunction: */
81 return find_insn(file, func->cfunc->sec, func->cfunc->offset);
82 }
83
84 #define func_for_each_insn_all(file, func, insn) \
85 for (insn = find_insn(file, func->sec, func->offset); \
86 insn; \
87 insn = next_insn_same_func(file, insn))
88
89 #define func_for_each_insn(file, func, insn) \
90 for (insn = find_insn(file, func->sec, func->offset); \
91 insn && &insn->list != &file->insn_list && \
92 insn->sec == func->sec && \
93 insn->offset < func->offset + func->len; \
94 insn = list_next_entry(insn, list))
95
96 #define func_for_each_insn_continue_reverse(file, func, insn) \
97 for (insn = list_prev_entry(insn, list); \
98 &insn->list != &file->insn_list && \
99 insn->sec == func->sec && insn->offset >= func->offset; \
100 insn = list_prev_entry(insn, list))
101
102 #define sec_for_each_insn_from(file, insn) \
103 for (; insn; insn = next_insn_same_sec(file, insn))
104
105 #define sec_for_each_insn_continue(file, insn) \
106 for (insn = next_insn_same_sec(file, insn); insn; \
107 insn = next_insn_same_sec(file, insn))
108
109 /*
110 * Check if the function has been manually whitelisted with the
111 * STACK_FRAME_NON_STANDARD macro, or if it should be automatically whitelisted
112 * due to its use of a context switching instruction.
113 */
ignore_func(struct objtool_file * file,struct symbol * func)114 static bool ignore_func(struct objtool_file *file, struct symbol *func)
115 {
116 struct rela *rela;
117
118 /* check for STACK_FRAME_NON_STANDARD */
119 if (file->whitelist && file->whitelist->rela)
120 list_for_each_entry(rela, &file->whitelist->rela->rela_list, list) {
121 if (rela->sym->type == STT_SECTION &&
122 rela->sym->sec == func->sec &&
123 rela->addend == func->offset)
124 return true;
125 if (rela->sym->type == STT_FUNC && rela->sym == func)
126 return true;
127 }
128
129 return false;
130 }
131
132 /*
133 * This checks to see if the given function is a "noreturn" function.
134 *
135 * For global functions which are outside the scope of this object file, we
136 * have to keep a manual list of them.
137 *
138 * For local functions, we have to detect them manually by simply looking for
139 * the lack of a return instruction.
140 *
141 * Returns:
142 * -1: error
143 * 0: no dead end
144 * 1: dead end
145 */
__dead_end_function(struct objtool_file * file,struct symbol * func,int recursion)146 static int __dead_end_function(struct objtool_file *file, struct symbol *func,
147 int recursion)
148 {
149 int i;
150 struct instruction *insn;
151 bool empty = true;
152
153 /*
154 * Unfortunately these have to be hard coded because the noreturn
155 * attribute isn't provided in ELF data.
156 */
157 static const char * const global_noreturns[] = {
158 "__stack_chk_fail",
159 "panic",
160 "do_exit",
161 "do_task_dead",
162 "__module_put_and_exit",
163 "complete_and_exit",
164 "kvm_spurious_fault",
165 "__reiserfs_panic",
166 "lbug_with_loc",
167 "fortify_panic",
168 "usercopy_abort",
169 "machine_real_restart",
170 "rewind_stack_do_exit",
171 };
172
173 if (func->bind == STB_WEAK)
174 return 0;
175
176 if (func->bind == STB_GLOBAL)
177 for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
178 if (!strcmp(func->name, global_noreturns[i]))
179 return 1;
180
181 if (!func->len)
182 return 0;
183
184 insn = find_insn(file, func->sec, func->offset);
185 if (!insn->func)
186 return 0;
187
188 func_for_each_insn_all(file, func, insn) {
189 empty = false;
190
191 if (insn->type == INSN_RETURN)
192 return 0;
193 }
194
195 if (empty)
196 return 0;
197
198 /*
199 * A function can have a sibling call instead of a return. In that
200 * case, the function's dead-end status depends on whether the target
201 * of the sibling call returns.
202 */
203 func_for_each_insn_all(file, func, insn) {
204 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
205 struct instruction *dest = insn->jump_dest;
206
207 if (!dest)
208 /* sibling call to another file */
209 return 0;
210
211 if (dest->func && dest->func->pfunc != insn->func->pfunc) {
212
213 /* local sibling call */
214 if (recursion == 5) {
215 /*
216 * Infinite recursion: two functions
217 * have sibling calls to each other.
218 * This is a very rare case. It means
219 * they aren't dead ends.
220 */
221 return 0;
222 }
223
224 return __dead_end_function(file, dest->func,
225 recursion + 1);
226 }
227 }
228
229 if (insn->type == INSN_JUMP_DYNAMIC && list_empty(&insn->alts))
230 /* sibling call */
231 return 0;
232 }
233
234 return 1;
235 }
236
dead_end_function(struct objtool_file * file,struct symbol * func)237 static int dead_end_function(struct objtool_file *file, struct symbol *func)
238 {
239 return __dead_end_function(file, func, 0);
240 }
241
clear_insn_state(struct insn_state * state)242 static void clear_insn_state(struct insn_state *state)
243 {
244 int i;
245
246 memset(state, 0, sizeof(*state));
247 state->cfa.base = CFI_UNDEFINED;
248 for (i = 0; i < CFI_NUM_REGS; i++) {
249 state->regs[i].base = CFI_UNDEFINED;
250 state->vals[i].base = CFI_UNDEFINED;
251 }
252 state->drap_reg = CFI_UNDEFINED;
253 state->drap_offset = -1;
254 }
255
256 /*
257 * Call the arch-specific instruction decoder for all the instructions and add
258 * them to the global instruction list.
259 */
decode_instructions(struct objtool_file * file)260 static int decode_instructions(struct objtool_file *file)
261 {
262 struct section *sec;
263 struct symbol *func;
264 unsigned long offset;
265 struct instruction *insn;
266 int ret;
267
268 for_each_sec(file, sec) {
269
270 if (!(sec->sh.sh_flags & SHF_EXECINSTR))
271 continue;
272
273 if (strcmp(sec->name, ".altinstr_replacement") &&
274 strcmp(sec->name, ".altinstr_aux") &&
275 strncmp(sec->name, ".discard.", 9))
276 sec->text = true;
277
278 for (offset = 0; offset < sec->len; offset += insn->len) {
279 insn = malloc(sizeof(*insn));
280 if (!insn) {
281 WARN("malloc failed");
282 return -1;
283 }
284 memset(insn, 0, sizeof(*insn));
285 INIT_LIST_HEAD(&insn->alts);
286 clear_insn_state(&insn->state);
287
288 insn->sec = sec;
289 insn->offset = offset;
290
291 ret = arch_decode_instruction(file->elf, sec, offset,
292 sec->len - offset,
293 &insn->len, &insn->type,
294 &insn->immediate,
295 &insn->stack_op);
296 if (ret)
297 goto err;
298
299 if (!insn->type || insn->type > INSN_LAST) {
300 WARN_FUNC("invalid instruction type %d",
301 insn->sec, insn->offset, insn->type);
302 ret = -1;
303 goto err;
304 }
305
306 hash_add(file->insn_hash, &insn->hash, insn->offset);
307 list_add_tail(&insn->list, &file->insn_list);
308 }
309
310 list_for_each_entry(func, &sec->symbol_list, list) {
311 if (func->type != STT_FUNC)
312 continue;
313
314 if (!find_insn(file, sec, func->offset)) {
315 WARN("%s(): can't find starting instruction",
316 func->name);
317 return -1;
318 }
319
320 func_for_each_insn(file, func, insn)
321 if (!insn->func)
322 insn->func = func;
323 }
324 }
325
326 return 0;
327
328 err:
329 free(insn);
330 return ret;
331 }
332
333 /*
334 * Mark "ud2" instructions and manually annotated dead ends.
335 */
add_dead_ends(struct objtool_file * file)336 static int add_dead_ends(struct objtool_file *file)
337 {
338 struct section *sec;
339 struct rela *rela;
340 struct instruction *insn;
341 bool found;
342
343 /*
344 * By default, "ud2" is a dead end unless otherwise annotated, because
345 * GCC 7 inserts it for certain divide-by-zero cases.
346 */
347 for_each_insn(file, insn)
348 if (insn->type == INSN_BUG)
349 insn->dead_end = true;
350
351 /*
352 * Check for manually annotated dead ends.
353 */
354 sec = find_section_by_name(file->elf, ".rela.discard.unreachable");
355 if (!sec)
356 goto reachable;
357
358 list_for_each_entry(rela, &sec->rela_list, list) {
359 if (rela->sym->type != STT_SECTION) {
360 WARN("unexpected relocation symbol type in %s", sec->name);
361 return -1;
362 }
363 insn = find_insn(file, rela->sym->sec, rela->addend);
364 if (insn)
365 insn = list_prev_entry(insn, list);
366 else if (rela->addend == rela->sym->sec->len) {
367 found = false;
368 list_for_each_entry_reverse(insn, &file->insn_list, list) {
369 if (insn->sec == rela->sym->sec) {
370 found = true;
371 break;
372 }
373 }
374
375 if (!found) {
376 WARN("can't find unreachable insn at %s+0x%x",
377 rela->sym->sec->name, rela->addend);
378 return -1;
379 }
380 } else {
381 WARN("can't find unreachable insn at %s+0x%x",
382 rela->sym->sec->name, rela->addend);
383 return -1;
384 }
385
386 insn->dead_end = true;
387 }
388
389 reachable:
390 /*
391 * These manually annotated reachable checks are needed for GCC 4.4,
392 * where the Linux unreachable() macro isn't supported. In that case
393 * GCC doesn't know the "ud2" is fatal, so it generates code as if it's
394 * not a dead end.
395 */
396 sec = find_section_by_name(file->elf, ".rela.discard.reachable");
397 if (!sec)
398 return 0;
399
400 list_for_each_entry(rela, &sec->rela_list, list) {
401 if (rela->sym->type != STT_SECTION) {
402 WARN("unexpected relocation symbol type in %s", sec->name);
403 return -1;
404 }
405 insn = find_insn(file, rela->sym->sec, rela->addend);
406 if (insn)
407 insn = list_prev_entry(insn, list);
408 else if (rela->addend == rela->sym->sec->len) {
409 found = false;
410 list_for_each_entry_reverse(insn, &file->insn_list, list) {
411 if (insn->sec == rela->sym->sec) {
412 found = true;
413 break;
414 }
415 }
416
417 if (!found) {
418 WARN("can't find reachable insn at %s+0x%x",
419 rela->sym->sec->name, rela->addend);
420 return -1;
421 }
422 } else {
423 WARN("can't find reachable insn at %s+0x%x",
424 rela->sym->sec->name, rela->addend);
425 return -1;
426 }
427
428 insn->dead_end = false;
429 }
430
431 return 0;
432 }
433
434 /*
435 * Warnings shouldn't be reported for ignored functions.
436 */
add_ignores(struct objtool_file * file)437 static void add_ignores(struct objtool_file *file)
438 {
439 struct instruction *insn;
440 struct section *sec;
441 struct symbol *func;
442
443 for_each_sec(file, sec) {
444 list_for_each_entry(func, &sec->symbol_list, list) {
445 if (func->type != STT_FUNC)
446 continue;
447
448 if (!ignore_func(file, func))
449 continue;
450
451 func_for_each_insn_all(file, func, insn)
452 insn->ignore = true;
453 }
454 }
455 }
456
457 /*
458 * FIXME: For now, just ignore any alternatives which add retpolines. This is
459 * a temporary hack, as it doesn't allow ORC to unwind from inside a retpoline.
460 * But it at least allows objtool to understand the control flow *around* the
461 * retpoline.
462 */
add_nospec_ignores(struct objtool_file * file)463 static int add_nospec_ignores(struct objtool_file *file)
464 {
465 struct section *sec;
466 struct rela *rela;
467 struct instruction *insn;
468
469 sec = find_section_by_name(file->elf, ".rela.discard.nospec");
470 if (!sec)
471 return 0;
472
473 list_for_each_entry(rela, &sec->rela_list, list) {
474 if (rela->sym->type != STT_SECTION) {
475 WARN("unexpected relocation symbol type in %s", sec->name);
476 return -1;
477 }
478
479 insn = find_insn(file, rela->sym->sec, rela->addend);
480 if (!insn) {
481 WARN("bad .discard.nospec entry");
482 return -1;
483 }
484
485 insn->ignore_alts = true;
486 }
487
488 return 0;
489 }
490
491 /*
492 * Find the destination instructions for all jumps.
493 */
add_jump_destinations(struct objtool_file * file)494 static int add_jump_destinations(struct objtool_file *file)
495 {
496 struct instruction *insn;
497 struct rela *rela;
498 struct section *dest_sec;
499 unsigned long dest_off;
500
501 for_each_insn(file, insn) {
502 if (insn->type != INSN_JUMP_CONDITIONAL &&
503 insn->type != INSN_JUMP_UNCONDITIONAL)
504 continue;
505
506 if (insn->offset == FAKE_JUMP_OFFSET)
507 continue;
508
509 rela = find_rela_by_dest_range(insn->sec, insn->offset,
510 insn->len);
511 if (!rela) {
512 dest_sec = insn->sec;
513 dest_off = insn->offset + insn->len + insn->immediate;
514 } else if (rela->sym->type == STT_SECTION) {
515 dest_sec = rela->sym->sec;
516 dest_off = rela->addend + 4;
517 } else if (rela->sym->sec->idx) {
518 dest_sec = rela->sym->sec;
519 dest_off = rela->sym->sym.st_value + rela->addend + 4;
520 } else if (strstr(rela->sym->name, "_indirect_thunk_")) {
521 /*
522 * Retpoline jumps are really dynamic jumps in
523 * disguise, so convert them accordingly.
524 */
525 insn->type = INSN_JUMP_DYNAMIC;
526 insn->retpoline_safe = true;
527 continue;
528 } else {
529 /* sibling call */
530 insn->jump_dest = 0;
531 continue;
532 }
533
534 insn->jump_dest = find_insn(file, dest_sec, dest_off);
535 if (!insn->jump_dest) {
536
537 /*
538 * This is a special case where an alt instruction
539 * jumps past the end of the section. These are
540 * handled later in handle_group_alt().
541 */
542 if (!strcmp(insn->sec->name, ".altinstr_replacement"))
543 continue;
544
545 WARN_FUNC("can't find jump dest instruction at %s+0x%lx",
546 insn->sec, insn->offset, dest_sec->name,
547 dest_off);
548 return -1;
549 }
550
551 /*
552 * For GCC 8+, create parent/child links for any cold
553 * subfunctions. This is _mostly_ redundant with a similar
554 * initialization in read_symbols().
555 *
556 * If a function has aliases, we want the *first* such function
557 * in the symbol table to be the subfunction's parent. In that
558 * case we overwrite the initialization done in read_symbols().
559 *
560 * However this code can't completely replace the
561 * read_symbols() code because this doesn't detect the case
562 * where the parent function's only reference to a subfunction
563 * is through a switch table.
564 */
565 if (insn->func && insn->jump_dest->func &&
566 insn->func != insn->jump_dest->func &&
567 !strstr(insn->func->name, ".cold.") &&
568 strstr(insn->jump_dest->func->name, ".cold.")) {
569 insn->func->cfunc = insn->jump_dest->func;
570 insn->jump_dest->func->pfunc = insn->func;
571 }
572 }
573
574 return 0;
575 }
576
577 /*
578 * Find the destination instructions for all calls.
579 */
add_call_destinations(struct objtool_file * file)580 static int add_call_destinations(struct objtool_file *file)
581 {
582 struct instruction *insn;
583 unsigned long dest_off;
584 struct rela *rela;
585
586 for_each_insn(file, insn) {
587 if (insn->type != INSN_CALL)
588 continue;
589
590 rela = find_rela_by_dest_range(insn->sec, insn->offset,
591 insn->len);
592 if (!rela) {
593 dest_off = insn->offset + insn->len + insn->immediate;
594 insn->call_dest = find_symbol_by_offset(insn->sec,
595 dest_off);
596
597 if (!insn->call_dest && !insn->ignore) {
598 WARN_FUNC("unsupported intra-function call",
599 insn->sec, insn->offset);
600 if (retpoline)
601 WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE.");
602 return -1;
603 }
604
605 } else if (rela->sym->type == STT_SECTION) {
606 insn->call_dest = find_symbol_by_offset(rela->sym->sec,
607 rela->addend+4);
608 if (!insn->call_dest ||
609 insn->call_dest->type != STT_FUNC) {
610 WARN_FUNC("can't find call dest symbol at %s+0x%x",
611 insn->sec, insn->offset,
612 rela->sym->sec->name,
613 rela->addend + 4);
614 return -1;
615 }
616 } else
617 insn->call_dest = rela->sym;
618 }
619
620 return 0;
621 }
622
623 /*
624 * The .alternatives section requires some extra special care, over and above
625 * what other special sections require:
626 *
627 * 1. Because alternatives are patched in-place, we need to insert a fake jump
628 * instruction at the end so that validate_branch() skips all the original
629 * replaced instructions when validating the new instruction path.
630 *
631 * 2. An added wrinkle is that the new instruction length might be zero. In
632 * that case the old instructions are replaced with noops. We simulate that
633 * by creating a fake jump as the only new instruction.
634 *
635 * 3. In some cases, the alternative section includes an instruction which
636 * conditionally jumps to the _end_ of the entry. We have to modify these
637 * jumps' destinations to point back to .text rather than the end of the
638 * entry in .altinstr_replacement.
639 *
640 * 4. It has been requested that we don't validate the !POPCNT feature path
641 * which is a "very very small percentage of machines".
642 */
handle_group_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)643 static int handle_group_alt(struct objtool_file *file,
644 struct special_alt *special_alt,
645 struct instruction *orig_insn,
646 struct instruction **new_insn)
647 {
648 struct instruction *last_orig_insn, *last_new_insn, *insn, *fake_jump = NULL;
649 unsigned long dest_off;
650
651 last_orig_insn = NULL;
652 insn = orig_insn;
653 sec_for_each_insn_from(file, insn) {
654 if (insn->offset >= special_alt->orig_off + special_alt->orig_len)
655 break;
656
657 if (special_alt->skip_orig)
658 insn->type = INSN_NOP;
659
660 insn->alt_group = true;
661 last_orig_insn = insn;
662 }
663
664 if (next_insn_same_sec(file, last_orig_insn)) {
665 fake_jump = malloc(sizeof(*fake_jump));
666 if (!fake_jump) {
667 WARN("malloc failed");
668 return -1;
669 }
670 memset(fake_jump, 0, sizeof(*fake_jump));
671 INIT_LIST_HEAD(&fake_jump->alts);
672 clear_insn_state(&fake_jump->state);
673
674 fake_jump->sec = special_alt->new_sec;
675 fake_jump->offset = FAKE_JUMP_OFFSET;
676 fake_jump->type = INSN_JUMP_UNCONDITIONAL;
677 fake_jump->jump_dest = list_next_entry(last_orig_insn, list);
678 fake_jump->func = orig_insn->func;
679 }
680
681 if (!special_alt->new_len) {
682 if (!fake_jump) {
683 WARN("%s: empty alternative at end of section",
684 special_alt->orig_sec->name);
685 return -1;
686 }
687
688 *new_insn = fake_jump;
689 return 0;
690 }
691
692 last_new_insn = NULL;
693 insn = *new_insn;
694 sec_for_each_insn_from(file, insn) {
695 if (insn->offset >= special_alt->new_off + special_alt->new_len)
696 break;
697
698 last_new_insn = insn;
699
700 insn->ignore = orig_insn->ignore_alts;
701
702 if (insn->type != INSN_JUMP_CONDITIONAL &&
703 insn->type != INSN_JUMP_UNCONDITIONAL)
704 continue;
705
706 if (!insn->immediate)
707 continue;
708
709 dest_off = insn->offset + insn->len + insn->immediate;
710 if (dest_off == special_alt->new_off + special_alt->new_len) {
711 if (!fake_jump) {
712 WARN("%s: alternative jump to end of section",
713 special_alt->orig_sec->name);
714 return -1;
715 }
716 insn->jump_dest = fake_jump;
717 }
718
719 if (!insn->jump_dest) {
720 WARN_FUNC("can't find alternative jump destination",
721 insn->sec, insn->offset);
722 return -1;
723 }
724 }
725
726 if (!last_new_insn) {
727 WARN_FUNC("can't find last new alternative instruction",
728 special_alt->new_sec, special_alt->new_off);
729 return -1;
730 }
731
732 if (fake_jump)
733 list_add(&fake_jump->list, &last_new_insn->list);
734
735 return 0;
736 }
737
738 /*
739 * A jump table entry can either convert a nop to a jump or a jump to a nop.
740 * If the original instruction is a jump, make the alt entry an effective nop
741 * by just skipping the original instruction.
742 */
handle_jump_alt(struct objtool_file * file,struct special_alt * special_alt,struct instruction * orig_insn,struct instruction ** new_insn)743 static int handle_jump_alt(struct objtool_file *file,
744 struct special_alt *special_alt,
745 struct instruction *orig_insn,
746 struct instruction **new_insn)
747 {
748 if (orig_insn->type == INSN_NOP)
749 return 0;
750
751 if (orig_insn->type != INSN_JUMP_UNCONDITIONAL) {
752 WARN_FUNC("unsupported instruction at jump label",
753 orig_insn->sec, orig_insn->offset);
754 return -1;
755 }
756
757 *new_insn = list_next_entry(orig_insn, list);
758 return 0;
759 }
760
761 /*
762 * Read all the special sections which have alternate instructions which can be
763 * patched in or redirected to at runtime. Each instruction having alternate
764 * instruction(s) has them added to its insn->alts list, which will be
765 * traversed in validate_branch().
766 */
add_special_section_alts(struct objtool_file * file)767 static int add_special_section_alts(struct objtool_file *file)
768 {
769 struct list_head special_alts;
770 struct instruction *orig_insn, *new_insn;
771 struct special_alt *special_alt, *tmp;
772 struct alternative *alt;
773 int ret;
774
775 ret = special_get_alts(file->elf, &special_alts);
776 if (ret)
777 return ret;
778
779 list_for_each_entry_safe(special_alt, tmp, &special_alts, list) {
780
781 orig_insn = find_insn(file, special_alt->orig_sec,
782 special_alt->orig_off);
783 if (!orig_insn) {
784 WARN_FUNC("special: can't find orig instruction",
785 special_alt->orig_sec, special_alt->orig_off);
786 ret = -1;
787 goto out;
788 }
789
790 new_insn = NULL;
791 if (!special_alt->group || special_alt->new_len) {
792 new_insn = find_insn(file, special_alt->new_sec,
793 special_alt->new_off);
794 if (!new_insn) {
795 WARN_FUNC("special: can't find new instruction",
796 special_alt->new_sec,
797 special_alt->new_off);
798 ret = -1;
799 goto out;
800 }
801 }
802
803 if (special_alt->group) {
804 if (!special_alt->orig_len) {
805 WARN_FUNC("empty alternative entry",
806 orig_insn->sec, orig_insn->offset);
807 continue;
808 }
809
810 ret = handle_group_alt(file, special_alt, orig_insn,
811 &new_insn);
812 if (ret)
813 goto out;
814 } else if (special_alt->jump_or_nop) {
815 ret = handle_jump_alt(file, special_alt, orig_insn,
816 &new_insn);
817 if (ret)
818 goto out;
819 }
820
821 alt = malloc(sizeof(*alt));
822 if (!alt) {
823 WARN("malloc failed");
824 ret = -1;
825 goto out;
826 }
827
828 alt->insn = new_insn;
829 list_add_tail(&alt->list, &orig_insn->alts);
830
831 list_del(&special_alt->list);
832 free(special_alt);
833 }
834
835 out:
836 return ret;
837 }
838
add_switch_table(struct objtool_file * file,struct instruction * insn,struct rela * table,struct rela * next_table)839 static int add_switch_table(struct objtool_file *file, struct instruction *insn,
840 struct rela *table, struct rela *next_table)
841 {
842 struct rela *rela = table;
843 struct instruction *alt_insn;
844 struct alternative *alt;
845 struct symbol *pfunc = insn->func->pfunc;
846 unsigned int prev_offset = 0;
847
848 list_for_each_entry_from(rela, &table->rela_sec->rela_list, list) {
849 if (rela == next_table)
850 break;
851
852 /* Make sure the switch table entries are consecutive: */
853 if (prev_offset && rela->offset != prev_offset + 8)
854 break;
855
856 /* Detect function pointers from contiguous objects: */
857 if (rela->sym->sec == pfunc->sec &&
858 rela->addend == pfunc->offset)
859 break;
860
861 alt_insn = find_insn(file, rela->sym->sec, rela->addend);
862 if (!alt_insn)
863 break;
864
865 /* Make sure the jmp dest is in the function or subfunction: */
866 if (alt_insn->func->pfunc != pfunc)
867 break;
868
869 alt = malloc(sizeof(*alt));
870 if (!alt) {
871 WARN("malloc failed");
872 return -1;
873 }
874
875 alt->insn = alt_insn;
876 list_add_tail(&alt->list, &insn->alts);
877 prev_offset = rela->offset;
878 }
879
880 if (!prev_offset) {
881 WARN_FUNC("can't find switch jump table",
882 insn->sec, insn->offset);
883 return -1;
884 }
885
886 return 0;
887 }
888
889 /*
890 * find_switch_table() - Given a dynamic jump, find the switch jump table in
891 * .rodata associated with it.
892 *
893 * There are 3 basic patterns:
894 *
895 * 1. jmpq *[rodata addr](,%reg,8)
896 *
897 * This is the most common case by far. It jumps to an address in a simple
898 * jump table which is stored in .rodata.
899 *
900 * 2. jmpq *[rodata addr](%rip)
901 *
902 * This is caused by a rare GCC quirk, currently only seen in three driver
903 * functions in the kernel, only with certain obscure non-distro configs.
904 *
905 * As part of an optimization, GCC makes a copy of an existing switch jump
906 * table, modifies it, and then hard-codes the jump (albeit with an indirect
907 * jump) to use a single entry in the table. The rest of the jump table and
908 * some of its jump targets remain as dead code.
909 *
910 * In such a case we can just crudely ignore all unreachable instruction
911 * warnings for the entire object file. Ideally we would just ignore them
912 * for the function, but that would require redesigning the code quite a
913 * bit. And honestly that's just not worth doing: unreachable instruction
914 * warnings are of questionable value anyway, and this is such a rare issue.
915 *
916 * 3. mov [rodata addr],%reg1
917 * ... some instructions ...
918 * jmpq *(%reg1,%reg2,8)
919 *
920 * This is a fairly uncommon pattern which is new for GCC 6. As of this
921 * writing, there are 11 occurrences of it in the allmodconfig kernel.
922 *
923 * As of GCC 7 there are quite a few more of these and the 'in between' code
924 * is significant. Esp. with KASAN enabled some of the code between the mov
925 * and jmpq uses .rodata itself, which can confuse things.
926 *
927 * TODO: Once we have DWARF CFI and smarter instruction decoding logic,
928 * ensure the same register is used in the mov and jump instructions.
929 *
930 * NOTE: RETPOLINE made it harder still to decode dynamic jumps.
931 */
find_switch_table(struct objtool_file * file,struct symbol * func,struct instruction * insn)932 static struct rela *find_switch_table(struct objtool_file *file,
933 struct symbol *func,
934 struct instruction *insn)
935 {
936 struct rela *text_rela, *rodata_rela;
937 struct instruction *orig_insn = insn;
938 struct section *rodata_sec;
939 unsigned long table_offset;
940
941 /*
942 * Backward search using the @first_jump_src links, these help avoid
943 * much of the 'in between' code. Which avoids us getting confused by
944 * it.
945 */
946 for (;
947 &insn->list != &file->insn_list && insn->func && insn->func->pfunc == func;
948 insn = insn->first_jump_src ?: list_prev_entry(insn, list)) {
949
950 if (insn != orig_insn && insn->type == INSN_JUMP_DYNAMIC)
951 break;
952
953 /* allow small jumps within the range */
954 if (insn->type == INSN_JUMP_UNCONDITIONAL &&
955 insn->jump_dest &&
956 (insn->jump_dest->offset <= insn->offset ||
957 insn->jump_dest->offset > orig_insn->offset))
958 break;
959
960 /* look for a relocation which references .rodata */
961 text_rela = find_rela_by_dest_range(insn->sec, insn->offset,
962 insn->len);
963 if (!text_rela || text_rela->sym->type != STT_SECTION ||
964 !text_rela->sym->sec->rodata)
965 continue;
966
967 table_offset = text_rela->addend;
968 rodata_sec = text_rela->sym->sec;
969
970 if (text_rela->type == R_X86_64_PC32)
971 table_offset += 4;
972
973 /*
974 * Make sure the .rodata address isn't associated with a
975 * symbol. gcc jump tables are anonymous data.
976 */
977 if (find_symbol_containing(rodata_sec, table_offset))
978 continue;
979
980 rodata_rela = find_rela_by_dest(rodata_sec, table_offset);
981 if (rodata_rela) {
982 /*
983 * Use of RIP-relative switch jumps is quite rare, and
984 * indicates a rare GCC quirk/bug which can leave dead
985 * code behind.
986 */
987 if (text_rela->type == R_X86_64_PC32)
988 file->ignore_unreachables = true;
989
990 return rodata_rela;
991 }
992 }
993
994 return NULL;
995 }
996
997
add_func_switch_tables(struct objtool_file * file,struct symbol * func)998 static int add_func_switch_tables(struct objtool_file *file,
999 struct symbol *func)
1000 {
1001 struct instruction *insn, *last = NULL, *prev_jump = NULL;
1002 struct rela *rela, *prev_rela = NULL;
1003 int ret;
1004
1005 func_for_each_insn_all(file, func, insn) {
1006 if (!last)
1007 last = insn;
1008
1009 /*
1010 * Store back-pointers for unconditional forward jumps such
1011 * that find_switch_table() can back-track using those and
1012 * avoid some potentially confusing code.
1013 */
1014 if (insn->type == INSN_JUMP_UNCONDITIONAL && insn->jump_dest &&
1015 insn->offset > last->offset &&
1016 insn->jump_dest->offset > insn->offset &&
1017 !insn->jump_dest->first_jump_src) {
1018
1019 insn->jump_dest->first_jump_src = insn;
1020 last = insn->jump_dest;
1021 }
1022
1023 if (insn->type != INSN_JUMP_DYNAMIC)
1024 continue;
1025
1026 rela = find_switch_table(file, func, insn);
1027 if (!rela)
1028 continue;
1029
1030 /*
1031 * We found a switch table, but we don't know yet how big it
1032 * is. Don't add it until we reach the end of the function or
1033 * the beginning of another switch table in the same function.
1034 */
1035 if (prev_jump) {
1036 ret = add_switch_table(file, prev_jump, prev_rela, rela);
1037 if (ret)
1038 return ret;
1039 }
1040
1041 prev_jump = insn;
1042 prev_rela = rela;
1043 }
1044
1045 if (prev_jump) {
1046 ret = add_switch_table(file, prev_jump, prev_rela, NULL);
1047 if (ret)
1048 return ret;
1049 }
1050
1051 return 0;
1052 }
1053
1054 /*
1055 * For some switch statements, gcc generates a jump table in the .rodata
1056 * section which contains a list of addresses within the function to jump to.
1057 * This finds these jump tables and adds them to the insn->alts lists.
1058 */
add_switch_table_alts(struct objtool_file * file)1059 static int add_switch_table_alts(struct objtool_file *file)
1060 {
1061 struct section *sec;
1062 struct symbol *func;
1063 int ret;
1064
1065 if (!file->rodata)
1066 return 0;
1067
1068 for_each_sec(file, sec) {
1069 list_for_each_entry(func, &sec->symbol_list, list) {
1070 if (func->type != STT_FUNC)
1071 continue;
1072
1073 ret = add_func_switch_tables(file, func);
1074 if (ret)
1075 return ret;
1076 }
1077 }
1078
1079 return 0;
1080 }
1081
read_unwind_hints(struct objtool_file * file)1082 static int read_unwind_hints(struct objtool_file *file)
1083 {
1084 struct section *sec, *relasec;
1085 struct rela *rela;
1086 struct unwind_hint *hint;
1087 struct instruction *insn;
1088 struct cfi_reg *cfa;
1089 int i;
1090
1091 sec = find_section_by_name(file->elf, ".discard.unwind_hints");
1092 if (!sec)
1093 return 0;
1094
1095 relasec = sec->rela;
1096 if (!relasec) {
1097 WARN("missing .rela.discard.unwind_hints section");
1098 return -1;
1099 }
1100
1101 if (sec->len % sizeof(struct unwind_hint)) {
1102 WARN("struct unwind_hint size mismatch");
1103 return -1;
1104 }
1105
1106 file->hints = true;
1107
1108 for (i = 0; i < sec->len / sizeof(struct unwind_hint); i++) {
1109 hint = (struct unwind_hint *)sec->data->d_buf + i;
1110
1111 rela = find_rela_by_dest(sec, i * sizeof(*hint));
1112 if (!rela) {
1113 WARN("can't find rela for unwind_hints[%d]", i);
1114 return -1;
1115 }
1116
1117 insn = find_insn(file, rela->sym->sec, rela->addend);
1118 if (!insn) {
1119 WARN("can't find insn for unwind_hints[%d]", i);
1120 return -1;
1121 }
1122
1123 cfa = &insn->state.cfa;
1124
1125 if (hint->type == UNWIND_HINT_TYPE_SAVE) {
1126 insn->save = true;
1127 continue;
1128
1129 } else if (hint->type == UNWIND_HINT_TYPE_RESTORE) {
1130 insn->restore = true;
1131 insn->hint = true;
1132 continue;
1133 }
1134
1135 insn->hint = true;
1136
1137 switch (hint->sp_reg) {
1138 case ORC_REG_UNDEFINED:
1139 cfa->base = CFI_UNDEFINED;
1140 break;
1141 case ORC_REG_SP:
1142 cfa->base = CFI_SP;
1143 break;
1144 case ORC_REG_BP:
1145 cfa->base = CFI_BP;
1146 break;
1147 case ORC_REG_SP_INDIRECT:
1148 cfa->base = CFI_SP_INDIRECT;
1149 break;
1150 case ORC_REG_R10:
1151 cfa->base = CFI_R10;
1152 break;
1153 case ORC_REG_R13:
1154 cfa->base = CFI_R13;
1155 break;
1156 case ORC_REG_DI:
1157 cfa->base = CFI_DI;
1158 break;
1159 case ORC_REG_DX:
1160 cfa->base = CFI_DX;
1161 break;
1162 default:
1163 WARN_FUNC("unsupported unwind_hint sp base reg %d",
1164 insn->sec, insn->offset, hint->sp_reg);
1165 return -1;
1166 }
1167
1168 cfa->offset = hint->sp_offset;
1169 insn->state.type = hint->type;
1170 insn->state.end = hint->end;
1171 }
1172
1173 return 0;
1174 }
1175
read_retpoline_hints(struct objtool_file * file)1176 static int read_retpoline_hints(struct objtool_file *file)
1177 {
1178 struct section *sec;
1179 struct instruction *insn;
1180 struct rela *rela;
1181
1182 sec = find_section_by_name(file->elf, ".rela.discard.retpoline_safe");
1183 if (!sec)
1184 return 0;
1185
1186 list_for_each_entry(rela, &sec->rela_list, list) {
1187 if (rela->sym->type != STT_SECTION) {
1188 WARN("unexpected relocation symbol type in %s", sec->name);
1189 return -1;
1190 }
1191
1192 insn = find_insn(file, rela->sym->sec, rela->addend);
1193 if (!insn) {
1194 WARN("bad .discard.retpoline_safe entry");
1195 return -1;
1196 }
1197
1198 if (insn->type != INSN_JUMP_DYNAMIC &&
1199 insn->type != INSN_CALL_DYNAMIC) {
1200 WARN_FUNC("retpoline_safe hint not an indirect jump/call",
1201 insn->sec, insn->offset);
1202 return -1;
1203 }
1204
1205 insn->retpoline_safe = true;
1206 }
1207
1208 return 0;
1209 }
1210
mark_rodata(struct objtool_file * file)1211 static void mark_rodata(struct objtool_file *file)
1212 {
1213 struct section *sec;
1214 bool found = false;
1215
1216 /*
1217 * This searches for the .rodata section or multiple .rodata.func_name
1218 * sections if -fdata-sections is being used. The .str.1.1 and .str.1.8
1219 * rodata sections are ignored as they don't contain jump tables.
1220 */
1221 for_each_sec(file, sec) {
1222 if (!strncmp(sec->name, ".rodata", 7) &&
1223 !strstr(sec->name, ".str1.")) {
1224 sec->rodata = true;
1225 found = true;
1226 }
1227 }
1228
1229 file->rodata = found;
1230 }
1231
decode_sections(struct objtool_file * file)1232 static int decode_sections(struct objtool_file *file)
1233 {
1234 int ret;
1235
1236 mark_rodata(file);
1237
1238 ret = decode_instructions(file);
1239 if (ret)
1240 return ret;
1241
1242 ret = add_dead_ends(file);
1243 if (ret)
1244 return ret;
1245
1246 add_ignores(file);
1247
1248 ret = add_nospec_ignores(file);
1249 if (ret)
1250 return ret;
1251
1252 ret = add_jump_destinations(file);
1253 if (ret)
1254 return ret;
1255
1256 ret = add_special_section_alts(file);
1257 if (ret)
1258 return ret;
1259
1260 ret = add_call_destinations(file);
1261 if (ret)
1262 return ret;
1263
1264 ret = add_switch_table_alts(file);
1265 if (ret)
1266 return ret;
1267
1268 ret = read_unwind_hints(file);
1269 if (ret)
1270 return ret;
1271
1272 ret = read_retpoline_hints(file);
1273 if (ret)
1274 return ret;
1275
1276 return 0;
1277 }
1278
is_fentry_call(struct instruction * insn)1279 static bool is_fentry_call(struct instruction *insn)
1280 {
1281 if (insn->type == INSN_CALL &&
1282 insn->call_dest->type == STT_NOTYPE &&
1283 !strcmp(insn->call_dest->name, "__fentry__"))
1284 return true;
1285
1286 return false;
1287 }
1288
has_modified_stack_frame(struct insn_state * state)1289 static bool has_modified_stack_frame(struct insn_state *state)
1290 {
1291 int i;
1292
1293 if (state->cfa.base != initial_func_cfi.cfa.base ||
1294 state->cfa.offset != initial_func_cfi.cfa.offset ||
1295 state->stack_size != initial_func_cfi.cfa.offset ||
1296 state->drap)
1297 return true;
1298
1299 for (i = 0; i < CFI_NUM_REGS; i++)
1300 if (state->regs[i].base != initial_func_cfi.regs[i].base ||
1301 state->regs[i].offset != initial_func_cfi.regs[i].offset)
1302 return true;
1303
1304 return false;
1305 }
1306
has_valid_stack_frame(struct insn_state * state)1307 static bool has_valid_stack_frame(struct insn_state *state)
1308 {
1309 if (state->cfa.base == CFI_BP && state->regs[CFI_BP].base == CFI_CFA &&
1310 state->regs[CFI_BP].offset == -16)
1311 return true;
1312
1313 if (state->drap && state->regs[CFI_BP].base == CFI_BP)
1314 return true;
1315
1316 return false;
1317 }
1318
update_insn_state_regs(struct instruction * insn,struct insn_state * state)1319 static int update_insn_state_regs(struct instruction *insn, struct insn_state *state)
1320 {
1321 struct cfi_reg *cfa = &state->cfa;
1322 struct stack_op *op = &insn->stack_op;
1323
1324 if (cfa->base != CFI_SP && cfa->base != CFI_SP_INDIRECT)
1325 return 0;
1326
1327 /* push */
1328 if (op->dest.type == OP_DEST_PUSH)
1329 cfa->offset += 8;
1330
1331 /* pop */
1332 if (op->src.type == OP_SRC_POP)
1333 cfa->offset -= 8;
1334
1335 /* add immediate to sp */
1336 if (op->dest.type == OP_DEST_REG && op->src.type == OP_SRC_ADD &&
1337 op->dest.reg == CFI_SP && op->src.reg == CFI_SP)
1338 cfa->offset -= op->src.offset;
1339
1340 return 0;
1341 }
1342
save_reg(struct insn_state * state,unsigned char reg,int base,int offset)1343 static void save_reg(struct insn_state *state, unsigned char reg, int base,
1344 int offset)
1345 {
1346 if (arch_callee_saved_reg(reg) &&
1347 state->regs[reg].base == CFI_UNDEFINED) {
1348 state->regs[reg].base = base;
1349 state->regs[reg].offset = offset;
1350 }
1351 }
1352
restore_reg(struct insn_state * state,unsigned char reg)1353 static void restore_reg(struct insn_state *state, unsigned char reg)
1354 {
1355 state->regs[reg].base = CFI_UNDEFINED;
1356 state->regs[reg].offset = 0;
1357 }
1358
1359 /*
1360 * A note about DRAP stack alignment:
1361 *
1362 * GCC has the concept of a DRAP register, which is used to help keep track of
1363 * the stack pointer when aligning the stack. r10 or r13 is used as the DRAP
1364 * register. The typical DRAP pattern is:
1365 *
1366 * 4c 8d 54 24 08 lea 0x8(%rsp),%r10
1367 * 48 83 e4 c0 and $0xffffffffffffffc0,%rsp
1368 * 41 ff 72 f8 pushq -0x8(%r10)
1369 * 55 push %rbp
1370 * 48 89 e5 mov %rsp,%rbp
1371 * (more pushes)
1372 * 41 52 push %r10
1373 * ...
1374 * 41 5a pop %r10
1375 * (more pops)
1376 * 5d pop %rbp
1377 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1378 * c3 retq
1379 *
1380 * There are some variations in the epilogues, like:
1381 *
1382 * 5b pop %rbx
1383 * 41 5a pop %r10
1384 * 41 5c pop %r12
1385 * 41 5d pop %r13
1386 * 41 5e pop %r14
1387 * c9 leaveq
1388 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1389 * c3 retq
1390 *
1391 * and:
1392 *
1393 * 4c 8b 55 e8 mov -0x18(%rbp),%r10
1394 * 48 8b 5d e0 mov -0x20(%rbp),%rbx
1395 * 4c 8b 65 f0 mov -0x10(%rbp),%r12
1396 * 4c 8b 6d f8 mov -0x8(%rbp),%r13
1397 * c9 leaveq
1398 * 49 8d 62 f8 lea -0x8(%r10),%rsp
1399 * c3 retq
1400 *
1401 * Sometimes r13 is used as the DRAP register, in which case it's saved and
1402 * restored beforehand:
1403 *
1404 * 41 55 push %r13
1405 * 4c 8d 6c 24 10 lea 0x10(%rsp),%r13
1406 * 48 83 e4 f0 and $0xfffffffffffffff0,%rsp
1407 * ...
1408 * 49 8d 65 f0 lea -0x10(%r13),%rsp
1409 * 41 5d pop %r13
1410 * c3 retq
1411 */
update_insn_state(struct instruction * insn,struct insn_state * state)1412 static int update_insn_state(struct instruction *insn, struct insn_state *state)
1413 {
1414 struct stack_op *op = &insn->stack_op;
1415 struct cfi_reg *cfa = &state->cfa;
1416 struct cfi_reg *regs = state->regs;
1417
1418 /* stack operations don't make sense with an undefined CFA */
1419 if (cfa->base == CFI_UNDEFINED) {
1420 if (insn->func) {
1421 WARN_FUNC("undefined stack state", insn->sec, insn->offset);
1422 return -1;
1423 }
1424 return 0;
1425 }
1426
1427 if (state->type == ORC_TYPE_REGS || state->type == ORC_TYPE_REGS_IRET)
1428 return update_insn_state_regs(insn, state);
1429
1430 switch (op->dest.type) {
1431
1432 case OP_DEST_REG:
1433 switch (op->src.type) {
1434
1435 case OP_SRC_REG:
1436 if (op->src.reg == CFI_SP && op->dest.reg == CFI_BP &&
1437 cfa->base == CFI_SP &&
1438 regs[CFI_BP].base == CFI_CFA &&
1439 regs[CFI_BP].offset == -cfa->offset) {
1440
1441 /* mov %rsp, %rbp */
1442 cfa->base = op->dest.reg;
1443 state->bp_scratch = false;
1444 }
1445
1446 else if (op->src.reg == CFI_SP &&
1447 op->dest.reg == CFI_BP && state->drap) {
1448
1449 /* drap: mov %rsp, %rbp */
1450 regs[CFI_BP].base = CFI_BP;
1451 regs[CFI_BP].offset = -state->stack_size;
1452 state->bp_scratch = false;
1453 }
1454
1455 else if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1456
1457 /*
1458 * mov %rsp, %reg
1459 *
1460 * This is needed for the rare case where GCC
1461 * does:
1462 *
1463 * mov %rsp, %rax
1464 * ...
1465 * mov %rax, %rsp
1466 */
1467 state->vals[op->dest.reg].base = CFI_CFA;
1468 state->vals[op->dest.reg].offset = -state->stack_size;
1469 }
1470
1471 else if (op->src.reg == CFI_BP && op->dest.reg == CFI_SP &&
1472 cfa->base == CFI_BP) {
1473
1474 /*
1475 * mov %rbp, %rsp
1476 *
1477 * Restore the original stack pointer (Clang).
1478 */
1479 state->stack_size = -state->regs[CFI_BP].offset;
1480 }
1481
1482 else if (op->dest.reg == cfa->base) {
1483
1484 /* mov %reg, %rsp */
1485 if (cfa->base == CFI_SP &&
1486 state->vals[op->src.reg].base == CFI_CFA) {
1487
1488 /*
1489 * This is needed for the rare case
1490 * where GCC does something dumb like:
1491 *
1492 * lea 0x8(%rsp), %rcx
1493 * ...
1494 * mov %rcx, %rsp
1495 */
1496 cfa->offset = -state->vals[op->src.reg].offset;
1497 state->stack_size = cfa->offset;
1498
1499 } else {
1500 cfa->base = CFI_UNDEFINED;
1501 cfa->offset = 0;
1502 }
1503 }
1504
1505 break;
1506
1507 case OP_SRC_ADD:
1508 if (op->dest.reg == CFI_SP && op->src.reg == CFI_SP) {
1509
1510 /* add imm, %rsp */
1511 state->stack_size -= op->src.offset;
1512 if (cfa->base == CFI_SP)
1513 cfa->offset -= op->src.offset;
1514 break;
1515 }
1516
1517 if (op->dest.reg == CFI_SP && op->src.reg == CFI_BP) {
1518
1519 /* lea disp(%rbp), %rsp */
1520 state->stack_size = -(op->src.offset + regs[CFI_BP].offset);
1521 break;
1522 }
1523
1524 if (op->src.reg == CFI_SP && cfa->base == CFI_SP) {
1525
1526 /* drap: lea disp(%rsp), %drap */
1527 state->drap_reg = op->dest.reg;
1528
1529 /*
1530 * lea disp(%rsp), %reg
1531 *
1532 * This is needed for the rare case where GCC
1533 * does something dumb like:
1534 *
1535 * lea 0x8(%rsp), %rcx
1536 * ...
1537 * mov %rcx, %rsp
1538 */
1539 state->vals[op->dest.reg].base = CFI_CFA;
1540 state->vals[op->dest.reg].offset = \
1541 -state->stack_size + op->src.offset;
1542
1543 break;
1544 }
1545
1546 if (state->drap && op->dest.reg == CFI_SP &&
1547 op->src.reg == state->drap_reg) {
1548
1549 /* drap: lea disp(%drap), %rsp */
1550 cfa->base = CFI_SP;
1551 cfa->offset = state->stack_size = -op->src.offset;
1552 state->drap_reg = CFI_UNDEFINED;
1553 state->drap = false;
1554 break;
1555 }
1556
1557 if (op->dest.reg == state->cfa.base) {
1558 WARN_FUNC("unsupported stack register modification",
1559 insn->sec, insn->offset);
1560 return -1;
1561 }
1562
1563 break;
1564
1565 case OP_SRC_AND:
1566 if (op->dest.reg != CFI_SP ||
1567 (state->drap_reg != CFI_UNDEFINED && cfa->base != CFI_SP) ||
1568 (state->drap_reg == CFI_UNDEFINED && cfa->base != CFI_BP)) {
1569 WARN_FUNC("unsupported stack pointer realignment",
1570 insn->sec, insn->offset);
1571 return -1;
1572 }
1573
1574 if (state->drap_reg != CFI_UNDEFINED) {
1575 /* drap: and imm, %rsp */
1576 cfa->base = state->drap_reg;
1577 cfa->offset = state->stack_size = 0;
1578 state->drap = true;
1579 }
1580
1581 /*
1582 * Older versions of GCC (4.8ish) realign the stack
1583 * without DRAP, with a frame pointer.
1584 */
1585
1586 break;
1587
1588 case OP_SRC_POP:
1589 if (!state->drap && op->dest.type == OP_DEST_REG &&
1590 op->dest.reg == cfa->base) {
1591
1592 /* pop %rbp */
1593 cfa->base = CFI_SP;
1594 }
1595
1596 if (state->drap && cfa->base == CFI_BP_INDIRECT &&
1597 op->dest.type == OP_DEST_REG &&
1598 op->dest.reg == state->drap_reg &&
1599 state->drap_offset == -state->stack_size) {
1600
1601 /* drap: pop %drap */
1602 cfa->base = state->drap_reg;
1603 cfa->offset = 0;
1604 state->drap_offset = -1;
1605
1606 } else if (regs[op->dest.reg].offset == -state->stack_size) {
1607
1608 /* pop %reg */
1609 restore_reg(state, op->dest.reg);
1610 }
1611
1612 state->stack_size -= 8;
1613 if (cfa->base == CFI_SP)
1614 cfa->offset -= 8;
1615
1616 break;
1617
1618 case OP_SRC_REG_INDIRECT:
1619 if (state->drap && op->src.reg == CFI_BP &&
1620 op->src.offset == state->drap_offset) {
1621
1622 /* drap: mov disp(%rbp), %drap */
1623 cfa->base = state->drap_reg;
1624 cfa->offset = 0;
1625 state->drap_offset = -1;
1626 }
1627
1628 if (state->drap && op->src.reg == CFI_BP &&
1629 op->src.offset == regs[op->dest.reg].offset) {
1630
1631 /* drap: mov disp(%rbp), %reg */
1632 restore_reg(state, op->dest.reg);
1633
1634 } else if (op->src.reg == cfa->base &&
1635 op->src.offset == regs[op->dest.reg].offset + cfa->offset) {
1636
1637 /* mov disp(%rbp), %reg */
1638 /* mov disp(%rsp), %reg */
1639 restore_reg(state, op->dest.reg);
1640 }
1641
1642 break;
1643
1644 default:
1645 WARN_FUNC("unknown stack-related instruction",
1646 insn->sec, insn->offset);
1647 return -1;
1648 }
1649
1650 break;
1651
1652 case OP_DEST_PUSH:
1653 state->stack_size += 8;
1654 if (cfa->base == CFI_SP)
1655 cfa->offset += 8;
1656
1657 if (op->src.type != OP_SRC_REG)
1658 break;
1659
1660 if (state->drap) {
1661 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1662
1663 /* drap: push %drap */
1664 cfa->base = CFI_BP_INDIRECT;
1665 cfa->offset = -state->stack_size;
1666
1667 /* save drap so we know when to restore it */
1668 state->drap_offset = -state->stack_size;
1669
1670 } else if (op->src.reg == CFI_BP && cfa->base == state->drap_reg) {
1671
1672 /* drap: push %rbp */
1673 state->stack_size = 0;
1674
1675 } else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1676
1677 /* drap: push %reg */
1678 save_reg(state, op->src.reg, CFI_BP, -state->stack_size);
1679 }
1680
1681 } else {
1682
1683 /* push %reg */
1684 save_reg(state, op->src.reg, CFI_CFA, -state->stack_size);
1685 }
1686
1687 /* detect when asm code uses rbp as a scratch register */
1688 if (!no_fp && insn->func && op->src.reg == CFI_BP &&
1689 cfa->base != CFI_BP)
1690 state->bp_scratch = true;
1691 break;
1692
1693 case OP_DEST_REG_INDIRECT:
1694
1695 if (state->drap) {
1696 if (op->src.reg == cfa->base && op->src.reg == state->drap_reg) {
1697
1698 /* drap: mov %drap, disp(%rbp) */
1699 cfa->base = CFI_BP_INDIRECT;
1700 cfa->offset = op->dest.offset;
1701
1702 /* save drap offset so we know when to restore it */
1703 state->drap_offset = op->dest.offset;
1704 }
1705
1706 else if (regs[op->src.reg].base == CFI_UNDEFINED) {
1707
1708 /* drap: mov reg, disp(%rbp) */
1709 save_reg(state, op->src.reg, CFI_BP, op->dest.offset);
1710 }
1711
1712 } else if (op->dest.reg == cfa->base) {
1713
1714 /* mov reg, disp(%rbp) */
1715 /* mov reg, disp(%rsp) */
1716 save_reg(state, op->src.reg, CFI_CFA,
1717 op->dest.offset - state->cfa.offset);
1718 }
1719
1720 break;
1721
1722 case OP_DEST_LEAVE:
1723 if ((!state->drap && cfa->base != CFI_BP) ||
1724 (state->drap && cfa->base != state->drap_reg)) {
1725 WARN_FUNC("leave instruction with modified stack frame",
1726 insn->sec, insn->offset);
1727 return -1;
1728 }
1729
1730 /* leave (mov %rbp, %rsp; pop %rbp) */
1731
1732 state->stack_size = -state->regs[CFI_BP].offset - 8;
1733 restore_reg(state, CFI_BP);
1734
1735 if (!state->drap) {
1736 cfa->base = CFI_SP;
1737 cfa->offset -= 8;
1738 }
1739
1740 break;
1741
1742 case OP_DEST_MEM:
1743 if (op->src.type != OP_SRC_POP) {
1744 WARN_FUNC("unknown stack-related memory operation",
1745 insn->sec, insn->offset);
1746 return -1;
1747 }
1748
1749 /* pop mem */
1750 state->stack_size -= 8;
1751 if (cfa->base == CFI_SP)
1752 cfa->offset -= 8;
1753
1754 break;
1755
1756 default:
1757 WARN_FUNC("unknown stack-related instruction",
1758 insn->sec, insn->offset);
1759 return -1;
1760 }
1761
1762 return 0;
1763 }
1764
insn_state_match(struct instruction * insn,struct insn_state * state)1765 static bool insn_state_match(struct instruction *insn, struct insn_state *state)
1766 {
1767 struct insn_state *state1 = &insn->state, *state2 = state;
1768 int i;
1769
1770 if (memcmp(&state1->cfa, &state2->cfa, sizeof(state1->cfa))) {
1771 WARN_FUNC("stack state mismatch: cfa1=%d%+d cfa2=%d%+d",
1772 insn->sec, insn->offset,
1773 state1->cfa.base, state1->cfa.offset,
1774 state2->cfa.base, state2->cfa.offset);
1775
1776 } else if (memcmp(&state1->regs, &state2->regs, sizeof(state1->regs))) {
1777 for (i = 0; i < CFI_NUM_REGS; i++) {
1778 if (!memcmp(&state1->regs[i], &state2->regs[i],
1779 sizeof(struct cfi_reg)))
1780 continue;
1781
1782 WARN_FUNC("stack state mismatch: reg1[%d]=%d%+d reg2[%d]=%d%+d",
1783 insn->sec, insn->offset,
1784 i, state1->regs[i].base, state1->regs[i].offset,
1785 i, state2->regs[i].base, state2->regs[i].offset);
1786 break;
1787 }
1788
1789 } else if (state1->type != state2->type) {
1790 WARN_FUNC("stack state mismatch: type1=%d type2=%d",
1791 insn->sec, insn->offset, state1->type, state2->type);
1792
1793 } else if (state1->drap != state2->drap ||
1794 (state1->drap && state1->drap_reg != state2->drap_reg) ||
1795 (state1->drap && state1->drap_offset != state2->drap_offset)) {
1796 WARN_FUNC("stack state mismatch: drap1=%d(%d,%d) drap2=%d(%d,%d)",
1797 insn->sec, insn->offset,
1798 state1->drap, state1->drap_reg, state1->drap_offset,
1799 state2->drap, state2->drap_reg, state2->drap_offset);
1800
1801 } else
1802 return true;
1803
1804 return false;
1805 }
1806
1807 /*
1808 * Follow the branch starting at the given instruction, and recursively follow
1809 * any other branches (jumps). Meanwhile, track the frame pointer state at
1810 * each instruction and validate all the rules described in
1811 * tools/objtool/Documentation/stack-validation.txt.
1812 */
validate_branch(struct objtool_file * file,struct instruction * first,struct insn_state state)1813 static int validate_branch(struct objtool_file *file, struct instruction *first,
1814 struct insn_state state)
1815 {
1816 struct alternative *alt;
1817 struct instruction *insn, *next_insn;
1818 struct section *sec;
1819 struct symbol *func = NULL;
1820 int ret;
1821
1822 insn = first;
1823 sec = insn->sec;
1824
1825 if (insn->alt_group && list_empty(&insn->alts)) {
1826 WARN_FUNC("don't know how to handle branch to middle of alternative instruction group",
1827 sec, insn->offset);
1828 return 1;
1829 }
1830
1831 while (1) {
1832 next_insn = next_insn_same_sec(file, insn);
1833
1834 if (file->c_file && func && insn->func && func != insn->func->pfunc) {
1835 WARN("%s() falls through to next function %s()",
1836 func->name, insn->func->name);
1837 return 1;
1838 }
1839
1840 if (insn->func)
1841 func = insn->func->pfunc;
1842
1843 if (func && insn->ignore) {
1844 WARN_FUNC("BUG: why am I validating an ignored function?",
1845 sec, insn->offset);
1846 return 1;
1847 }
1848
1849 if (insn->visited) {
1850 if (!insn->hint && !insn_state_match(insn, &state))
1851 return 1;
1852
1853 return 0;
1854 }
1855
1856 if (insn->hint) {
1857 if (insn->restore) {
1858 struct instruction *save_insn, *i;
1859
1860 i = insn;
1861 save_insn = NULL;
1862 func_for_each_insn_continue_reverse(file, insn->func, i) {
1863 if (i->save) {
1864 save_insn = i;
1865 break;
1866 }
1867 }
1868
1869 if (!save_insn) {
1870 WARN_FUNC("no corresponding CFI save for CFI restore",
1871 sec, insn->offset);
1872 return 1;
1873 }
1874
1875 if (!save_insn->visited) {
1876 /*
1877 * Oops, no state to copy yet.
1878 * Hopefully we can reach this
1879 * instruction from another branch
1880 * after the save insn has been
1881 * visited.
1882 */
1883 if (insn == first)
1884 return 0;
1885
1886 WARN_FUNC("objtool isn't smart enough to handle this CFI save/restore combo",
1887 sec, insn->offset);
1888 return 1;
1889 }
1890
1891 insn->state = save_insn->state;
1892 }
1893
1894 state = insn->state;
1895
1896 } else
1897 insn->state = state;
1898
1899 insn->visited = true;
1900
1901 if (!insn->ignore_alts) {
1902 list_for_each_entry(alt, &insn->alts, list) {
1903 ret = validate_branch(file, alt->insn, state);
1904 if (ret)
1905 return 1;
1906 }
1907 }
1908
1909 switch (insn->type) {
1910
1911 case INSN_RETURN:
1912 if (func && has_modified_stack_frame(&state)) {
1913 WARN_FUNC("return with modified stack frame",
1914 sec, insn->offset);
1915 return 1;
1916 }
1917
1918 if (state.bp_scratch) {
1919 WARN("%s uses BP as a scratch register",
1920 insn->func->name);
1921 return 1;
1922 }
1923
1924 return 0;
1925
1926 case INSN_CALL:
1927 if (is_fentry_call(insn))
1928 break;
1929
1930 ret = dead_end_function(file, insn->call_dest);
1931 if (ret == 1)
1932 return 0;
1933 if (ret == -1)
1934 return 1;
1935
1936 /* fallthrough */
1937 case INSN_CALL_DYNAMIC:
1938 if (!no_fp && func && !has_valid_stack_frame(&state)) {
1939 WARN_FUNC("call without frame pointer save/setup",
1940 sec, insn->offset);
1941 return 1;
1942 }
1943 break;
1944
1945 case INSN_JUMP_CONDITIONAL:
1946 case INSN_JUMP_UNCONDITIONAL:
1947 if (insn->jump_dest &&
1948 (!func || !insn->jump_dest->func ||
1949 insn->jump_dest->func->pfunc == func)) {
1950 ret = validate_branch(file, insn->jump_dest,
1951 state);
1952 if (ret)
1953 return 1;
1954
1955 } else if (func && has_modified_stack_frame(&state)) {
1956 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1957 sec, insn->offset);
1958 return 1;
1959 }
1960
1961 if (insn->type == INSN_JUMP_UNCONDITIONAL)
1962 return 0;
1963
1964 break;
1965
1966 case INSN_JUMP_DYNAMIC:
1967 if (func && list_empty(&insn->alts) &&
1968 has_modified_stack_frame(&state)) {
1969 WARN_FUNC("sibling call from callable instruction with modified stack frame",
1970 sec, insn->offset);
1971 return 1;
1972 }
1973
1974 return 0;
1975
1976 case INSN_CONTEXT_SWITCH:
1977 if (func && (!next_insn || !next_insn->hint)) {
1978 WARN_FUNC("unsupported instruction in callable function",
1979 sec, insn->offset);
1980 return 1;
1981 }
1982 return 0;
1983
1984 case INSN_STACK:
1985 if (update_insn_state(insn, &state))
1986 return 1;
1987
1988 break;
1989
1990 default:
1991 break;
1992 }
1993
1994 if (insn->dead_end)
1995 return 0;
1996
1997 if (!next_insn) {
1998 if (state.cfa.base == CFI_UNDEFINED)
1999 return 0;
2000 WARN("%s: unexpected end of section", sec->name);
2001 return 1;
2002 }
2003
2004 insn = next_insn;
2005 }
2006
2007 return 0;
2008 }
2009
validate_unwind_hints(struct objtool_file * file)2010 static int validate_unwind_hints(struct objtool_file *file)
2011 {
2012 struct instruction *insn;
2013 int ret, warnings = 0;
2014 struct insn_state state;
2015
2016 if (!file->hints)
2017 return 0;
2018
2019 clear_insn_state(&state);
2020
2021 for_each_insn(file, insn) {
2022 if (insn->hint && !insn->visited) {
2023 ret = validate_branch(file, insn, state);
2024 warnings += ret;
2025 }
2026 }
2027
2028 return warnings;
2029 }
2030
validate_retpoline(struct objtool_file * file)2031 static int validate_retpoline(struct objtool_file *file)
2032 {
2033 struct instruction *insn;
2034 int warnings = 0;
2035
2036 for_each_insn(file, insn) {
2037 if (insn->type != INSN_JUMP_DYNAMIC &&
2038 insn->type != INSN_CALL_DYNAMIC)
2039 continue;
2040
2041 if (insn->retpoline_safe)
2042 continue;
2043
2044 /*
2045 * .init.text code is ran before userspace and thus doesn't
2046 * strictly need retpolines, except for modules which are
2047 * loaded late, they very much do need retpoline in their
2048 * .init.text
2049 */
2050 if (!strcmp(insn->sec->name, ".init.text") && !module)
2051 continue;
2052
2053 WARN_FUNC("indirect %s found in RETPOLINE build",
2054 insn->sec, insn->offset,
2055 insn->type == INSN_JUMP_DYNAMIC ? "jump" : "call");
2056
2057 warnings++;
2058 }
2059
2060 return warnings;
2061 }
2062
is_kasan_insn(struct instruction * insn)2063 static bool is_kasan_insn(struct instruction *insn)
2064 {
2065 return (insn->type == INSN_CALL &&
2066 !strcmp(insn->call_dest->name, "__asan_handle_no_return"));
2067 }
2068
is_ubsan_insn(struct instruction * insn)2069 static bool is_ubsan_insn(struct instruction *insn)
2070 {
2071 return (insn->type == INSN_CALL &&
2072 !strcmp(insn->call_dest->name,
2073 "__ubsan_handle_builtin_unreachable"));
2074 }
2075
ignore_unreachable_insn(struct instruction * insn)2076 static bool ignore_unreachable_insn(struct instruction *insn)
2077 {
2078 int i;
2079
2080 if (insn->ignore || insn->type == INSN_NOP)
2081 return true;
2082
2083 /*
2084 * Ignore any unused exceptions. This can happen when a whitelisted
2085 * function has an exception table entry.
2086 *
2087 * Also ignore alternative replacement instructions. This can happen
2088 * when a whitelisted function uses one of the ALTERNATIVE macros.
2089 */
2090 if (!strcmp(insn->sec->name, ".fixup") ||
2091 !strcmp(insn->sec->name, ".altinstr_replacement") ||
2092 !strcmp(insn->sec->name, ".altinstr_aux"))
2093 return true;
2094
2095 if (!insn->func)
2096 return false;
2097
2098 /*
2099 * CONFIG_UBSAN_TRAP inserts a UD2 when it sees
2100 * __builtin_unreachable(). The BUG() macro has an unreachable() after
2101 * the UD2, which causes GCC's undefined trap logic to emit another UD2
2102 * (or occasionally a JMP to UD2).
2103 */
2104 if (list_prev_entry(insn, list)->dead_end &&
2105 (insn->type == INSN_BUG ||
2106 (insn->type == INSN_JUMP_UNCONDITIONAL &&
2107 insn->jump_dest && insn->jump_dest->type == INSN_BUG)))
2108 return true;
2109
2110 /*
2111 * Check if this (or a subsequent) instruction is related to
2112 * CONFIG_UBSAN or CONFIG_KASAN.
2113 *
2114 * End the search at 5 instructions to avoid going into the weeds.
2115 */
2116 for (i = 0; i < 5; i++) {
2117
2118 if (is_kasan_insn(insn) || is_ubsan_insn(insn))
2119 return true;
2120
2121 if (insn->type == INSN_JUMP_UNCONDITIONAL) {
2122 if (insn->jump_dest &&
2123 insn->jump_dest->func == insn->func) {
2124 insn = insn->jump_dest;
2125 continue;
2126 }
2127
2128 break;
2129 }
2130
2131 if (insn->offset + insn->len >= insn->func->offset + insn->func->len)
2132 break;
2133
2134 insn = list_next_entry(insn, list);
2135 }
2136
2137 return false;
2138 }
2139
validate_functions(struct objtool_file * file)2140 static int validate_functions(struct objtool_file *file)
2141 {
2142 struct section *sec;
2143 struct symbol *func;
2144 struct instruction *insn;
2145 struct insn_state state;
2146 int ret, warnings = 0;
2147
2148 clear_insn_state(&state);
2149
2150 state.cfa = initial_func_cfi.cfa;
2151 memcpy(&state.regs, &initial_func_cfi.regs,
2152 CFI_NUM_REGS * sizeof(struct cfi_reg));
2153 state.stack_size = initial_func_cfi.cfa.offset;
2154
2155 for_each_sec(file, sec) {
2156 list_for_each_entry(func, &sec->symbol_list, list) {
2157 if (func->type != STT_FUNC || func->pfunc != func)
2158 continue;
2159
2160 insn = find_insn(file, sec, func->offset);
2161 if (!insn || insn->ignore)
2162 continue;
2163
2164 ret = validate_branch(file, insn, state);
2165 warnings += ret;
2166 }
2167 }
2168
2169 return warnings;
2170 }
2171
validate_reachable_instructions(struct objtool_file * file)2172 static int validate_reachable_instructions(struct objtool_file *file)
2173 {
2174 struct instruction *insn;
2175
2176 if (file->ignore_unreachables)
2177 return 0;
2178
2179 for_each_insn(file, insn) {
2180 if (insn->visited || ignore_unreachable_insn(insn))
2181 continue;
2182
2183 WARN_FUNC("unreachable instruction", insn->sec, insn->offset);
2184 return 1;
2185 }
2186
2187 return 0;
2188 }
2189
cleanup(struct objtool_file * file)2190 static void cleanup(struct objtool_file *file)
2191 {
2192 struct instruction *insn, *tmpinsn;
2193 struct alternative *alt, *tmpalt;
2194
2195 list_for_each_entry_safe(insn, tmpinsn, &file->insn_list, list) {
2196 list_for_each_entry_safe(alt, tmpalt, &insn->alts, list) {
2197 list_del(&alt->list);
2198 free(alt);
2199 }
2200 list_del(&insn->list);
2201 hash_del(&insn->hash);
2202 free(insn);
2203 }
2204 elf_close(file->elf);
2205 }
2206
2207 static struct objtool_file file;
2208
check(const char * _objname,bool orc)2209 int check(const char *_objname, bool orc)
2210 {
2211 int ret, warnings = 0;
2212
2213 objname = _objname;
2214
2215 file.elf = elf_open(objname, orc ? O_RDWR : O_RDONLY);
2216 if (!file.elf)
2217 return 1;
2218
2219 INIT_LIST_HEAD(&file.insn_list);
2220 hash_init(file.insn_hash);
2221 file.whitelist = find_section_by_name(file.elf, ".discard.func_stack_frame_non_standard");
2222 file.c_file = find_section_by_name(file.elf, ".comment");
2223 file.ignore_unreachables = no_unreachable;
2224 file.hints = false;
2225
2226 arch_initial_func_cfi_state(&initial_func_cfi);
2227
2228 ret = decode_sections(&file);
2229 if (ret < 0)
2230 goto out;
2231 warnings += ret;
2232
2233 if (list_empty(&file.insn_list))
2234 goto out;
2235
2236 if (retpoline) {
2237 ret = validate_retpoline(&file);
2238 if (ret < 0)
2239 return ret;
2240 warnings += ret;
2241 }
2242
2243 ret = validate_functions(&file);
2244 if (ret < 0)
2245 goto out;
2246 warnings += ret;
2247
2248 ret = validate_unwind_hints(&file);
2249 if (ret < 0)
2250 goto out;
2251 warnings += ret;
2252
2253 if (!warnings) {
2254 ret = validate_reachable_instructions(&file);
2255 if (ret < 0)
2256 goto out;
2257 warnings += ret;
2258 }
2259
2260 if (orc) {
2261 ret = create_orc(&file);
2262 if (ret < 0)
2263 goto out;
2264
2265 ret = create_orc_sections(&file);
2266 if (ret < 0)
2267 goto out;
2268
2269 ret = elf_write(file.elf);
2270 if (ret < 0)
2271 goto out;
2272 }
2273
2274 out:
2275 cleanup(&file);
2276
2277 /* ignore warnings for now until we get all the code cleaned up */
2278 if (ret || warnings)
2279 return 0;
2280 return 0;
2281 }
2282