special.c 4.9 KB

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