stack-validation.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. Compile-time stack metadata validation
  2. ======================================
  3. Overview
  4. --------
  5. The kernel CONFIG_STACK_VALIDATION option enables a host tool named
  6. objtool which runs at compile time. It has a "check" subcommand which
  7. analyzes every .o file and ensures the validity of its stack metadata.
  8. It enforces a set of rules on asm code and C inline assembly code so
  9. that stack traces can be reliable.
  10. For each function, it recursively follows all possible code paths and
  11. validates the correct frame pointer state at each instruction.
  12. It also follows code paths involving special sections, like
  13. .altinstructions, __jump_table, and __ex_table, which can add
  14. alternative execution paths to a given instruction (or set of
  15. instructions). Similarly, it knows how to follow switch statements, for
  16. which gcc sometimes uses jump tables.
  17. (Objtool also has an 'orc generate' subcommand which generates debuginfo
  18. for the ORC unwinder. See Documentation/x86/orc-unwinder.txt in the
  19. kernel tree for more details.)
  20. Why do we need stack metadata validation?
  21. -----------------------------------------
  22. Here are some of the benefits of validating stack metadata:
  23. a) More reliable stack traces for frame pointer enabled kernels
  24. Frame pointers are used for debugging purposes. They allow runtime
  25. code and debug tools to be able to walk the stack to determine the
  26. chain of function call sites that led to the currently executing
  27. code.
  28. For some architectures, frame pointers are enabled by
  29. CONFIG_FRAME_POINTER. For some other architectures they may be
  30. required by the ABI (sometimes referred to as "backchain pointers").
  31. For C code, gcc automatically generates instructions for setting up
  32. frame pointers when the -fno-omit-frame-pointer option is used.
  33. But for asm code, the frame setup instructions have to be written by
  34. hand, which most people don't do. So the end result is that
  35. CONFIG_FRAME_POINTER is honored for C code but not for most asm code.
  36. For stack traces based on frame pointers to be reliable, all
  37. functions which call other functions must first create a stack frame
  38. and update the frame pointer. If a first function doesn't properly
  39. create a stack frame before calling a second function, the *caller*
  40. of the first function will be skipped on the stack trace.
  41. For example, consider the following example backtrace with frame
  42. pointers enabled:
  43. [<ffffffff81812584>] dump_stack+0x4b/0x63
  44. [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
  45. [<ffffffff8127f568>] seq_read+0x108/0x3e0
  46. [<ffffffff812cce62>] proc_reg_read+0x42/0x70
  47. [<ffffffff81256197>] __vfs_read+0x37/0x100
  48. [<ffffffff81256b16>] vfs_read+0x86/0x130
  49. [<ffffffff81257898>] SyS_read+0x58/0xd0
  50. [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
  51. It correctly shows that the caller of cmdline_proc_show() is
  52. seq_read().
  53. If we remove the frame pointer logic from cmdline_proc_show() by
  54. replacing the frame pointer related instructions with nops, here's
  55. what it looks like instead:
  56. [<ffffffff81812584>] dump_stack+0x4b/0x63
  57. [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
  58. [<ffffffff812cce62>] proc_reg_read+0x42/0x70
  59. [<ffffffff81256197>] __vfs_read+0x37/0x100
  60. [<ffffffff81256b16>] vfs_read+0x86/0x130
  61. [<ffffffff81257898>] SyS_read+0x58/0xd0
  62. [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
  63. Notice that cmdline_proc_show()'s caller, seq_read(), has been
  64. skipped. Instead the stack trace seems to show that
  65. cmdline_proc_show() was called by proc_reg_read().
  66. The benefit of objtool here is that because it ensures that *all*
  67. functions honor CONFIG_FRAME_POINTER, no functions will ever[*] be
  68. skipped on a stack trace.
  69. [*] unless an interrupt or exception has occurred at the very
  70. beginning of a function before the stack frame has been created,
  71. or at the very end of the function after the stack frame has been
  72. destroyed. This is an inherent limitation of frame pointers.
  73. b) ORC (Oops Rewind Capability) unwind table generation
  74. An alternative to frame pointers and DWARF, ORC unwind data can be
  75. used to walk the stack. Unlike frame pointers, ORC data is out of
  76. band. So it doesn't affect runtime performance and it can be
  77. reliable even when interrupts or exceptions are involved.
  78. For more details, see Documentation/x86/orc-unwinder.txt.
  79. c) Higher live patching compatibility rate
  80. Livepatch has an optional "consistency model", which is needed for
  81. more complex patches. In order for the consistency model to work,
  82. stack traces need to be reliable (or an unreliable condition needs to
  83. be detectable). Objtool makes that possible.
  84. For more details, see the livepatch documentation in the Linux kernel
  85. source tree at Documentation/livepatch/livepatch.txt.
  86. Rules
  87. -----
  88. To achieve the validation, objtool enforces the following rules:
  89. 1. Each callable function must be annotated as such with the ELF
  90. function type. In asm code, this is typically done using the
  91. ENTRY/ENDPROC macros. If objtool finds a return instruction
  92. outside of a function, it flags an error since that usually indicates
  93. callable code which should be annotated accordingly.
  94. This rule is needed so that objtool can properly identify each
  95. callable function in order to analyze its stack metadata.
  96. 2. Conversely, each section of code which is *not* callable should *not*
  97. be annotated as an ELF function. The ENDPROC macro shouldn't be used
  98. in this case.
  99. This rule is needed so that objtool can ignore non-callable code.
  100. Such code doesn't have to follow any of the other rules.
  101. 3. Each callable function which calls another function must have the
  102. correct frame pointer logic, if required by CONFIG_FRAME_POINTER or
  103. the architecture's back chain rules. This can by done in asm code
  104. with the FRAME_BEGIN/FRAME_END macros.
  105. This rule ensures that frame pointer based stack traces will work as
  106. designed. If function A doesn't create a stack frame before calling
  107. function B, the _caller_ of function A will be skipped on the stack
  108. trace.
  109. 4. Dynamic jumps and jumps to undefined symbols are only allowed if:
  110. a) the jump is part of a switch statement; or
  111. b) the jump matches sibling call semantics and the frame pointer has
  112. the same value it had on function entry.
  113. This rule is needed so that objtool can reliably analyze all of a
  114. function's code paths. If a function jumps to code in another file,
  115. and it's not a sibling call, objtool has no way to follow the jump
  116. because it only analyzes a single file at a time.
  117. 5. A callable function may not execute kernel entry/exit instructions.
  118. The only code which needs such instructions is kernel entry code,
  119. which shouldn't be be in callable functions anyway.
  120. This rule is just a sanity check to ensure that callable functions
  121. return normally.
  122. Objtool warnings
  123. ----------------
  124. For asm files, if you're getting an error which doesn't make sense,
  125. first make sure that the affected code follows the above rules.
  126. For C files, the common culprits are inline asm statements and calls to
  127. "noreturn" functions. See below for more details.
  128. Another possible cause for errors in C code is if the Makefile removes
  129. -fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options.
  130. Here are some examples of common warnings reported by objtool, what
  131. they mean, and suggestions for how to fix them.
  132. 1. file.o: warning: objtool: func()+0x128: call without frame pointer save/setup
  133. The func() function made a function call without first saving and/or
  134. updating the frame pointer, and CONFIG_FRAME_POINTER is enabled.
  135. If the error is for an asm file, and func() is indeed a callable
  136. function, add proper frame pointer logic using the FRAME_BEGIN and
  137. FRAME_END macros. Otherwise, if it's not a callable function, remove
  138. its ELF function annotation by changing ENDPROC to END, and instead
  139. use the manual unwind hint macros in asm/unwind_hints.h.
  140. If it's a GCC-compiled .c file, the error may be because the function
  141. uses an inline asm() statement which has a "call" instruction. An
  142. asm() statement with a call instruction must declare the use of the
  143. stack pointer in its output operand. On x86_64, this means adding
  144. the ASM_CALL_CONSTRAINT as an output constraint:
  145. asm volatile("call func" : ASM_CALL_CONSTRAINT);
  146. Otherwise the stack frame may not get created before the call.
  147. 2. file.o: warning: objtool: .text+0x53: unreachable instruction
  148. Objtool couldn't find a code path to reach the instruction.
  149. If the error is for an asm file, and the instruction is inside (or
  150. reachable from) a callable function, the function should be annotated
  151. with the ENTRY/ENDPROC macros (ENDPROC is the important one).
  152. Otherwise, the code should probably be annotated with the unwind hint
  153. macros in asm/unwind_hints.h so objtool and the unwinder can know the
  154. stack state associated with the code.
  155. If you're 100% sure the code won't affect stack traces, or if you're
  156. a just a bad person, you can tell objtool to ignore it. See the
  157. "Adding exceptions" section below.
  158. If it's not actually in a callable function (e.g. kernel entry code),
  159. change ENDPROC to END.
  160. 4. file.o: warning: objtool: func(): can't find starting instruction
  161. or
  162. file.o: warning: objtool: func()+0x11dd: can't decode instruction
  163. Does the file have data in a text section? If so, that can confuse
  164. objtool's instruction decoder. Move the data to a more appropriate
  165. section like .data or .rodata.
  166. 5. file.o: warning: objtool: func()+0x6: unsupported instruction in callable function
  167. This is a kernel entry/exit instruction like sysenter or iret. Such
  168. instructions aren't allowed in a callable function, and are most
  169. likely part of the kernel entry code. They should usually not have
  170. the callable function annotation (ENDPROC) and should always be
  171. annotated with the unwind hint macros in asm/unwind_hints.h.
  172. 6. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame
  173. This is a dynamic jump or a jump to an undefined symbol. Objtool
  174. assumed it's a sibling call and detected that the frame pointer
  175. wasn't first restored to its original state.
  176. If it's not really a sibling call, you may need to move the
  177. destination code to the local file.
  178. If the instruction is not actually in a callable function (e.g.
  179. kernel entry code), change ENDPROC to END and annotate manually with
  180. the unwind hint macros in asm/unwind_hints.h.
  181. 7. file: warning: objtool: func()+0x5c: stack state mismatch
  182. The instruction's frame pointer state is inconsistent, depending on
  183. which execution path was taken to reach the instruction.
  184. Make sure that, when CONFIG_FRAME_POINTER is enabled, the function
  185. pushes and sets up the frame pointer (for x86_64, this means rbp) at
  186. the beginning of the function and pops it at the end of the function.
  187. Also make sure that no other code in the function touches the frame
  188. pointer.
  189. Another possibility is that the code has some asm or inline asm which
  190. does some unusual things to the stack or the frame pointer. In such
  191. cases it's probably appropriate to use the unwind hint macros in
  192. asm/unwind_hints.h.
  193. 8. file.o: warning: objtool: funcA() falls through to next function funcB()
  194. This means that funcA() doesn't end with a return instruction or an
  195. unconditional jump, and that objtool has determined that the function
  196. can fall through into the next function. There could be different
  197. reasons for this:
  198. 1) funcA()'s last instruction is a call to a "noreturn" function like
  199. panic(). In this case the noreturn function needs to be added to
  200. objtool's hard-coded global_noreturns array. Feel free to bug the
  201. objtool maintainer, or you can submit a patch.
  202. 2) funcA() uses the unreachable() annotation in a section of code
  203. that is actually reachable.
  204. 3) If funcA() calls an inline function, the object code for funcA()
  205. might be corrupt due to a gcc bug. For more details, see:
  206. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70646
  207. If the error doesn't seem to make sense, it could be a bug in objtool.
  208. Feel free to ask the objtool maintainer for help.
  209. Adding exceptions
  210. -----------------
  211. If you _really_ need objtool to ignore something, and are 100% sure
  212. that it won't affect kernel stack traces, you can tell objtool to
  213. ignore it:
  214. - To skip validation of a function, use the STACK_FRAME_NON_STANDARD
  215. macro.
  216. - To skip validation of a file, add
  217. OBJECT_FILES_NON_STANDARD_filename.o := n
  218. to the Makefile.
  219. - To skip validation of a directory, add
  220. OBJECT_FILES_NON_STANDARD := y
  221. to the Makefile.