objtool.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. Objtool
  2. =======
  3. The kernel CONFIG_OBJTOOL option enables a host tool named 'objtool'
  4. which runs at compile time. It can do various validations and
  5. transformations on .o files.
  6. Objtool has become an integral part of the x86-64 kernel toolchain. The
  7. kernel depends on it for a variety of security and performance features
  8. (and other types of features as well).
  9. Features
  10. --------
  11. Objtool has the following features:
  12. - Stack unwinding metadata validation -- useful for helping to ensure
  13. stack traces are reliable for live patching
  14. - ORC unwinder metadata generation -- a faster and more precise
  15. alternative to frame pointer based unwinding
  16. - Retpoline validation -- ensures that all indirect calls go through
  17. retpoline thunks, for Spectre v2 mitigations
  18. - Retpoline call site annotation -- annotates all retpoline thunk call
  19. sites, enabling the kernel to patch them inline, to prevent "thunk
  20. funneling" for both security and performance reasons
  21. - Non-instrumentation validation -- validates non-instrumentable
  22. ("noinstr") code rules, preventing instrumentation in low-level C
  23. entry code
  24. - Static call annotation -- annotates static call sites, enabling the
  25. kernel to implement inline static calls, a faster alternative to some
  26. indirect branches
  27. - Uaccess validation -- validates uaccess rules for a proper
  28. implementation of Supervisor Mode Access Protection (SMAP)
  29. - Straight Line Speculation validation -- validates certain SLS
  30. mitigations
  31. - Indirect Branch Tracking validation -- validates Intel CET IBT rules
  32. to ensure that all functions referenced by function pointers have
  33. corresponding ENDBR instructions
  34. - Indirect Branch Tracking annotation -- annotates unused ENDBR
  35. instruction sites, enabling the kernel to "seal" them (replace them
  36. with NOPs) to further harden IBT
  37. - Function entry annotation -- annotates function entries, enabling
  38. kernel function tracing
  39. - Other toolchain hacks which will go unmentioned at this time...
  40. Each feature can be enabled individually or in combination using the
  41. objtool cmdline.
  42. Objects
  43. -------
  44. Typically, objtool runs on every translation unit (TU, aka ".o file") in
  45. the kernel. If a TU is part of a kernel module, the '--module' option
  46. is added.
  47. However:
  48. - If noinstr validation is enabled, it also runs on vmlinux.o, with all
  49. options removed and '--noinstr' added.
  50. - If IBT or LTO is enabled, it doesn't run on TUs at all. Instead it
  51. runs on vmlinux.o and linked modules, with all options.
  52. In summary:
  53. A) Legacy mode:
  54. TU: objtool [--module] <options>
  55. vmlinux: N/A
  56. module: N/A
  57. B) CONFIG_NOINSTR_VALIDATION=y && !(CONFIG_X86_KERNEL_IBT=y || CONFIG_LTO=y):
  58. TU: objtool [--module] <options> // no --noinstr
  59. vmlinux: objtool --noinstr // other options removed
  60. module: N/A
  61. C) CONFIG_X86_KERNEL_IBT=y || CONFIG_LTO=y:
  62. TU: N/A
  63. vmlinux: objtool --noinstr <options>
  64. module: objtool --module --noinstr <options>
  65. Stack validation
  66. ----------------
  67. Objtool's stack validation feature analyzes every .o file and ensures
  68. the validity of its stack metadata. It enforces a set of rules on asm
  69. code and C inline assembly code so that stack traces can be reliable.
  70. For each function, it recursively follows all possible code paths and
  71. validates the correct frame pointer state at each instruction.
  72. It also follows code paths involving special sections, like
  73. .altinstructions, __jump_table, and __ex_table, which can add
  74. alternative execution paths to a given instruction (or set of
  75. instructions). Similarly, it knows how to follow switch statements, for
  76. which gcc sometimes uses jump tables.
  77. Here are some of the benefits of validating stack metadata:
  78. a) More reliable stack traces for frame pointer enabled kernels
  79. Frame pointers are used for debugging purposes. They allow runtime
  80. code and debug tools to be able to walk the stack to determine the
  81. chain of function call sites that led to the currently executing
  82. code.
  83. For some architectures, frame pointers are enabled by
  84. CONFIG_FRAME_POINTER. For some other architectures they may be
  85. required by the ABI (sometimes referred to as "backchain pointers").
  86. For C code, gcc automatically generates instructions for setting up
  87. frame pointers when the -fno-omit-frame-pointer option is used.
  88. But for asm code, the frame setup instructions have to be written by
  89. hand, which most people don't do. So the end result is that
  90. CONFIG_FRAME_POINTER is honored for C code but not for most asm code.
  91. For stack traces based on frame pointers to be reliable, all
  92. functions which call other functions must first create a stack frame
  93. and update the frame pointer. If a first function doesn't properly
  94. create a stack frame before calling a second function, the *caller*
  95. of the first function will be skipped on the stack trace.
  96. For example, consider the following example backtrace with frame
  97. pointers enabled:
  98. [<ffffffff81812584>] dump_stack+0x4b/0x63
  99. [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
  100. [<ffffffff8127f568>] seq_read+0x108/0x3e0
  101. [<ffffffff812cce62>] proc_reg_read+0x42/0x70
  102. [<ffffffff81256197>] __vfs_read+0x37/0x100
  103. [<ffffffff81256b16>] vfs_read+0x86/0x130
  104. [<ffffffff81257898>] SyS_read+0x58/0xd0
  105. [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
  106. It correctly shows that the caller of cmdline_proc_show() is
  107. seq_read().
  108. If we remove the frame pointer logic from cmdline_proc_show() by
  109. replacing the frame pointer related instructions with nops, here's
  110. what it looks like instead:
  111. [<ffffffff81812584>] dump_stack+0x4b/0x63
  112. [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
  113. [<ffffffff812cce62>] proc_reg_read+0x42/0x70
  114. [<ffffffff81256197>] __vfs_read+0x37/0x100
  115. [<ffffffff81256b16>] vfs_read+0x86/0x130
  116. [<ffffffff81257898>] SyS_read+0x58/0xd0
  117. [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
  118. Notice that cmdline_proc_show()'s caller, seq_read(), has been
  119. skipped. Instead the stack trace seems to show that
  120. cmdline_proc_show() was called by proc_reg_read().
  121. The benefit of objtool here is that because it ensures that *all*
  122. functions honor CONFIG_FRAME_POINTER, no functions will ever[*] be
  123. skipped on a stack trace.
  124. [*] unless an interrupt or exception has occurred at the very
  125. beginning of a function before the stack frame has been created,
  126. or at the very end of the function after the stack frame has been
  127. destroyed. This is an inherent limitation of frame pointers.
  128. b) ORC (Oops Rewind Capability) unwind table generation
  129. An alternative to frame pointers and DWARF, ORC unwind data can be
  130. used to walk the stack. Unlike frame pointers, ORC data is out of
  131. band. So it doesn't affect runtime performance and it can be
  132. reliable even when interrupts or exceptions are involved.
  133. For more details, see Documentation/x86/orc-unwinder.rst.
  134. c) Higher live patching compatibility rate
  135. Livepatch has an optional "consistency model", which is needed for
  136. more complex patches. In order for the consistency model to work,
  137. stack traces need to be reliable (or an unreliable condition needs to
  138. be detectable). Objtool makes that possible.
  139. For more details, see the livepatch documentation in the Linux kernel
  140. source tree at Documentation/livepatch/livepatch.rst.
  141. To achieve the validation, objtool enforces the following rules:
  142. 1. Each callable function must be annotated as such with the ELF
  143. function type. In asm code, this is typically done using the
  144. ENTRY/ENDPROC macros. If objtool finds a return instruction
  145. outside of a function, it flags an error since that usually indicates
  146. callable code which should be annotated accordingly.
  147. This rule is needed so that objtool can properly identify each
  148. callable function in order to analyze its stack metadata.
  149. 2. Conversely, each section of code which is *not* callable should *not*
  150. be annotated as an ELF function. The ENDPROC macro shouldn't be used
  151. in this case.
  152. This rule is needed so that objtool can ignore non-callable code.
  153. Such code doesn't have to follow any of the other rules.
  154. 3. Each callable function which calls another function must have the
  155. correct frame pointer logic, if required by CONFIG_FRAME_POINTER or
  156. the architecture's back chain rules. This can by done in asm code
  157. with the FRAME_BEGIN/FRAME_END macros.
  158. This rule ensures that frame pointer based stack traces will work as
  159. designed. If function A doesn't create a stack frame before calling
  160. function B, the _caller_ of function A will be skipped on the stack
  161. trace.
  162. 4. Dynamic jumps and jumps to undefined symbols are only allowed if:
  163. a) the jump is part of a switch statement; or
  164. b) the jump matches sibling call semantics and the frame pointer has
  165. the same value it had on function entry.
  166. This rule is needed so that objtool can reliably analyze all of a
  167. function's code paths. If a function jumps to code in another file,
  168. and it's not a sibling call, objtool has no way to follow the jump
  169. because it only analyzes a single file at a time.
  170. 5. A callable function may not execute kernel entry/exit instructions.
  171. The only code which needs such instructions is kernel entry code,
  172. which shouldn't be be in callable functions anyway.
  173. This rule is just a sanity check to ensure that callable functions
  174. return normally.
  175. Objtool warnings
  176. ----------------
  177. For asm files, if you're getting an error which doesn't make sense,
  178. first make sure that the affected code follows the above rules.
  179. For C files, the common culprits are inline asm statements and calls to
  180. "noreturn" functions. See below for more details.
  181. Another possible cause for errors in C code is if the Makefile removes
  182. -fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options.
  183. Here are some examples of common warnings reported by objtool, what
  184. they mean, and suggestions for how to fix them. When in doubt, ping
  185. the objtool maintainers.
  186. 1. file.o: warning: objtool: func()+0x128: call without frame pointer save/setup
  187. The func() function made a function call without first saving and/or
  188. updating the frame pointer, and CONFIG_FRAME_POINTER is enabled.
  189. If the error is for an asm file, and func() is indeed a callable
  190. function, add proper frame pointer logic using the FRAME_BEGIN and
  191. FRAME_END macros. Otherwise, if it's not a callable function, remove
  192. its ELF function annotation by changing ENDPROC to END, and instead
  193. use the manual unwind hint macros in asm/unwind_hints.h.
  194. If it's a GCC-compiled .c file, the error may be because the function
  195. uses an inline asm() statement which has a "call" instruction. An
  196. asm() statement with a call instruction must declare the use of the
  197. stack pointer in its output operand. On x86_64, this means adding
  198. the ASM_CALL_CONSTRAINT as an output constraint:
  199. asm volatile("call func" : ASM_CALL_CONSTRAINT);
  200. Otherwise the stack frame may not get created before the call.
  201. 2. file.o: warning: objtool: .text+0x53: unreachable instruction
  202. Objtool couldn't find a code path to reach the instruction.
  203. If the error is for an asm file, and the instruction is inside (or
  204. reachable from) a callable function, the function should be annotated
  205. with the ENTRY/ENDPROC macros (ENDPROC is the important one).
  206. Otherwise, the code should probably be annotated with the unwind hint
  207. macros in asm/unwind_hints.h so objtool and the unwinder can know the
  208. stack state associated with the code.
  209. If you're 100% sure the code won't affect stack traces, or if you're
  210. a just a bad person, you can tell objtool to ignore it. See the
  211. "Adding exceptions" section below.
  212. If it's not actually in a callable function (e.g. kernel entry code),
  213. change ENDPROC to END.
  214. 4. file.o: warning: objtool: func(): can't find starting instruction
  215. or
  216. file.o: warning: objtool: func()+0x11dd: can't decode instruction
  217. Does the file have data in a text section? If so, that can confuse
  218. objtool's instruction decoder. Move the data to a more appropriate
  219. section like .data or .rodata.
  220. 5. file.o: warning: objtool: func()+0x6: unsupported instruction in callable function
  221. This is a kernel entry/exit instruction like sysenter or iret. Such
  222. instructions aren't allowed in a callable function, and are most
  223. likely part of the kernel entry code. They should usually not have
  224. the callable function annotation (ENDPROC) and should always be
  225. annotated with the unwind hint macros in asm/unwind_hints.h.
  226. 6. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame
  227. This is a dynamic jump or a jump to an undefined symbol. Objtool
  228. assumed it's a sibling call and detected that the frame pointer
  229. wasn't first restored to its original state.
  230. If it's not really a sibling call, you may need to move the
  231. destination code to the local file.
  232. If the instruction is not actually in a callable function (e.g.
  233. kernel entry code), change ENDPROC to END and annotate manually with
  234. the unwind hint macros in asm/unwind_hints.h.
  235. 7. file: warning: objtool: func()+0x5c: stack state mismatch
  236. The instruction's frame pointer state is inconsistent, depending on
  237. which execution path was taken to reach the instruction.
  238. Make sure that, when CONFIG_FRAME_POINTER is enabled, the function
  239. pushes and sets up the frame pointer (for x86_64, this means rbp) at
  240. the beginning of the function and pops it at the end of the function.
  241. Also make sure that no other code in the function touches the frame
  242. pointer.
  243. Another possibility is that the code has some asm or inline asm which
  244. does some unusual things to the stack or the frame pointer. In such
  245. cases it's probably appropriate to use the unwind hint macros in
  246. asm/unwind_hints.h.
  247. 8. file.o: warning: objtool: funcA() falls through to next function funcB()
  248. This means that funcA() doesn't end with a return instruction or an
  249. unconditional jump, and that objtool has determined that the function
  250. can fall through into the next function. There could be different
  251. reasons for this:
  252. 1) funcA()'s last instruction is a call to a "noreturn" function like
  253. panic(). In this case the noreturn function needs to be added to
  254. objtool's hard-coded global_noreturns array. Feel free to bug the
  255. objtool maintainer, or you can submit a patch.
  256. 2) funcA() uses the unreachable() annotation in a section of code
  257. that is actually reachable.
  258. 3) If funcA() calls an inline function, the object code for funcA()
  259. might be corrupt due to a gcc bug. For more details, see:
  260. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70646
  261. 9. file.o: warning: objtool: funcA() call to funcB() with UACCESS enabled
  262. This means that an unexpected call to a non-whitelisted function exists
  263. outside of arch-specific guards.
  264. X86: SMAP (stac/clac): __uaccess_begin()/__uaccess_end()
  265. ARM: PAN: uaccess_enable()/uaccess_disable()
  266. These functions should be called to denote a minimal critical section around
  267. access to __user variables. See also: https://lwn.net/Articles/517475/
  268. The intention of the warning is to prevent calls to funcB() from eventually
  269. calling schedule(), potentially leaking the AC flags state, and not
  270. restoring them correctly.
  271. It also helps verify that there are no unexpected calls to funcB() which may
  272. access user space pages with protections against doing so disabled.
  273. To fix, either:
  274. 1) remove explicit calls to funcB() from funcA().
  275. 2) add the correct guards before and after calls to low level functions like
  276. __get_user_size()/__put_user_size().
  277. 3) add funcB to uaccess_safe_builtin whitelist in tools/objtool/check.c, if
  278. funcB obviously does not call schedule(), and is marked notrace (since
  279. function tracing inserts additional calls, which is not obvious from the
  280. sources).
  281. 10. file.o: warning: func()+0x5c: stack layout conflict in alternatives
  282. This means that in the use of the alternative() or ALTERNATIVE()
  283. macro, the code paths have conflicting modifications to the stack.
  284. The problem is that there is only one ORC unwind table, which means
  285. that the ORC unwind entries must be consistent for all possible
  286. instruction boundaries regardless of which code has been patched.
  287. This limitation can be overcome by massaging the alternatives with
  288. NOPs to shift the stack changes around so they no longer conflict.
  289. 11. file.o: warning: unannotated intra-function call
  290. This warning means that a direct call is done to a destination which
  291. is not at the beginning of a function. If this is a legit call, you
  292. can remove this warning by putting the ANNOTATE_INTRA_FUNCTION_CALL
  293. directive right before the call.
  294. If the error doesn't seem to make sense, it could be a bug in objtool.
  295. Feel free to ask the objtool maintainer for help.
  296. Adding exceptions
  297. -----------------
  298. If you _really_ need objtool to ignore something, and are 100% sure
  299. that it won't affect kernel stack traces, you can tell objtool to
  300. ignore it:
  301. - To skip validation of a function, use the STACK_FRAME_NON_STANDARD
  302. macro.
  303. - To skip validation of a file, add
  304. OBJECT_FILES_NON_STANDARD_filename.o := y
  305. to the Makefile.
  306. - To skip validation of a directory, add
  307. OBJECT_FILES_NON_STANDARD := y
  308. to the Makefile.
  309. NOTE: OBJECT_FILES_NON_STANDARD doesn't work for link time validation of
  310. vmlinux.o or a linked module. So it should only be used for files which
  311. aren't linked into vmlinux or a module.