special.c 4.6 KB

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