blob: 17c1195f11f428b4e21c783543c3826186b2777a [file] [log] [blame]
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06001Compile-time stack metadata validation
2======================================
3
4
5Overview
6--------
7
8The kernel CONFIG_STACK_VALIDATION option enables a host tool named
9objtool which runs at compile time. It has a "check" subcommand which
10analyzes every .o file and ensures the validity of its stack metadata.
11It enforces a set of rules on asm code and C inline assembly code so
12that stack traces can be reliable.
13
14Currently it only checks frame pointer usage, but there are plans to add
15CFI validation for C files and CFI generation for asm files.
16
17For each function, it recursively follows all possible code paths and
18validates the correct frame pointer state at each instruction.
19
20It also follows code paths involving special sections, like
21.altinstructions, __jump_table, and __ex_table, which can add
22alternative execution paths to a given instruction (or set of
23instructions). Similarly, it knows how to follow switch statements, for
24which gcc sometimes uses jump tables.
25
26
27Why do we need stack metadata validation?
28-----------------------------------------
29
30Here are some of the benefits of validating stack metadata:
31
32a) More reliable stack traces for frame pointer enabled kernels
33
34 Frame pointers are used for debugging purposes. They allow runtime
35 code and debug tools to be able to walk the stack to determine the
36 chain of function call sites that led to the currently executing
37 code.
38
39 For some architectures, frame pointers are enabled by
40 CONFIG_FRAME_POINTER. For some other architectures they may be
41 required by the ABI (sometimes referred to as "backchain pointers").
42
43 For C code, gcc automatically generates instructions for setting up
44 frame pointers when the -fno-omit-frame-pointer option is used.
45
46 But for asm code, the frame setup instructions have to be written by
47 hand, which most people don't do. So the end result is that
48 CONFIG_FRAME_POINTER is honored for C code but not for most asm code.
49
50 For stack traces based on frame pointers to be reliable, all
51 functions which call other functions must first create a stack frame
52 and update the frame pointer. If a first function doesn't properly
53 create a stack frame before calling a second function, the *caller*
54 of the first function will be skipped on the stack trace.
55
56 For example, consider the following example backtrace with frame
57 pointers enabled:
58
59 [<ffffffff81812584>] dump_stack+0x4b/0x63
60 [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
61 [<ffffffff8127f568>] seq_read+0x108/0x3e0
62 [<ffffffff812cce62>] proc_reg_read+0x42/0x70
63 [<ffffffff81256197>] __vfs_read+0x37/0x100
64 [<ffffffff81256b16>] vfs_read+0x86/0x130
65 [<ffffffff81257898>] SyS_read+0x58/0xd0
66 [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
67
68 It correctly shows that the caller of cmdline_proc_show() is
69 seq_read().
70
71 If we remove the frame pointer logic from cmdline_proc_show() by
72 replacing the frame pointer related instructions with nops, here's
73 what it looks like instead:
74
75 [<ffffffff81812584>] dump_stack+0x4b/0x63
76 [<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30
77 [<ffffffff812cce62>] proc_reg_read+0x42/0x70
78 [<ffffffff81256197>] __vfs_read+0x37/0x100
79 [<ffffffff81256b16>] vfs_read+0x86/0x130
80 [<ffffffff81257898>] SyS_read+0x58/0xd0
81 [<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76
82
83 Notice that cmdline_proc_show()'s caller, seq_read(), has been
84 skipped. Instead the stack trace seems to show that
85 cmdline_proc_show() was called by proc_reg_read().
86
87 The benefit of objtool here is that because it ensures that *all*
88 functions honor CONFIG_FRAME_POINTER, no functions will ever[*] be
89 skipped on a stack trace.
90
91 [*] unless an interrupt or exception has occurred at the very
92 beginning of a function before the stack frame has been created,
93 or at the very end of the function after the stack frame has been
94 destroyed. This is an inherent limitation of frame pointers.
95
96b) 100% reliable stack traces for DWARF enabled kernels
97
98 (NOTE: This is not yet implemented)
99
100 As an alternative to frame pointers, DWARF Call Frame Information
101 (CFI) metadata can be used to walk the stack. Unlike frame pointers,
102 CFI metadata is out of band. So it doesn't affect runtime
103 performance and it can be reliable even when interrupts or exceptions
104 are involved.
105
106 For C code, gcc automatically generates DWARF CFI metadata. But for
107 asm code, generating CFI is a tedious manual approach which requires
108 manually placed .cfi assembler macros to be scattered throughout the
109 code. It's clumsy and very easy to get wrong, and it makes the real
110 code harder to read.
111
112 Stacktool will improve this situation in several ways. For code
113 which already has CFI annotations, it will validate them. For code
114 which doesn't have CFI annotations, it will generate them. So an
115 architecture can opt to strip out all the manual .cfi annotations
116 from their asm code and have objtool generate them instead.
117
118 We might also add a runtime stack validation debug option where we
119 periodically walk the stack from schedule() and/or an NMI to ensure
120 that the stack metadata is sane and that we reach the bottom of the
121 stack.
122
123 So the benefit of objtool here will be that external tooling should
124 always show perfect stack traces. And the same will be true for
125 kernel warning/oops traces if the architecture has a runtime DWARF
126 unwinder.
127
128c) Higher live patching compatibility rate
129
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500130 Livepatch has an optional "consistency model", which is needed for
131 more complex patches. In order for the consistency model to work,
132 stack traces need to be reliable (or an unreliable condition needs to
133 be detectable). Objtool makes that possible.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600134
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500135 For more details, see the livepatch documentation in the Linux kernel
136 source tree at Documentation/livepatch/livepatch.txt.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600137
138Rules
139-----
140
141To achieve the validation, objtool enforces the following rules:
142
1431. Each callable function must be annotated as such with the ELF
144 function type. In asm code, this is typically done using the
145 ENTRY/ENDPROC macros. If objtool finds a return instruction
146 outside of a function, it flags an error since that usually indicates
147 callable code which should be annotated accordingly.
148
149 This rule is needed so that objtool can properly identify each
150 callable function in order to analyze its stack metadata.
151
1522. Conversely, each section of code which is *not* callable should *not*
153 be annotated as an ELF function. The ENDPROC macro shouldn't be used
154 in this case.
155
156 This rule is needed so that objtool can ignore non-callable code.
157 Such code doesn't have to follow any of the other rules.
158
1593. Each callable function which calls another function must have the
160 correct frame pointer logic, if required by CONFIG_FRAME_POINTER or
161 the architecture's back chain rules. This can by done in asm code
162 with the FRAME_BEGIN/FRAME_END macros.
163
164 This rule ensures that frame pointer based stack traces will work as
165 designed. If function A doesn't create a stack frame before calling
166 function B, the _caller_ of function A will be skipped on the stack
167 trace.
168
1694. Dynamic jumps and jumps to undefined symbols are only allowed if:
170
171 a) the jump is part of a switch statement; or
172
173 b) the jump matches sibling call semantics and the frame pointer has
174 the same value it had on function entry.
175
176 This rule is needed so that objtool can reliably analyze all of a
177 function's code paths. If a function jumps to code in another file,
178 and it's not a sibling call, objtool has no way to follow the jump
179 because it only analyzes a single file at a time.
180
1815. A callable function may not execute kernel entry/exit instructions.
182 The only code which needs such instructions is kernel entry code,
183 which shouldn't be be in callable functions anyway.
184
185 This rule is just a sanity check to ensure that callable functions
186 return normally.
187
188
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500189Objtool warnings
190----------------
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600191
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500192For asm files, if you're getting an error which doesn't make sense,
193first make sure that the affected code follows the above rules.
194
195For C files, the common culprits are inline asm statements and calls to
196"noreturn" functions. See below for more details.
197
198Another possible cause for errors in C code is if the Makefile removes
199-fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600200
201Here are some examples of common warnings reported by objtool, what
202they mean, and suggestions for how to fix them.
203
204
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002051. file.o: warning: objtool: func()+0x128: call without frame pointer save/setup
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600206
207 The func() function made a function call without first saving and/or
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500208 updating the frame pointer, and CONFIG_FRAME_POINTER is enabled.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600209
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500210 If the error is for an asm file, and func() is indeed a callable
211 function, add proper frame pointer logic using the FRAME_BEGIN and
212 FRAME_END macros. Otherwise, if it's not a callable function, remove
213 its ELF function annotation by changing ENDPROC to END, and instead
214 use the manual CFI hint macros in asm/undwarf.h.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600215
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500216 If it's a GCC-compiled .c file, the error may be because the function
217 uses an inline asm() statement which has a "call" instruction. An
218 asm() statement with a call instruction must declare the use of the
219 stack pointer in its output operand. For example, on x86_64:
220
221 register void *__sp asm("rsp");
222 asm volatile("call func" : "+r" (__sp));
223
224 Otherwise the stack frame may not get created before the call.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600225
226
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002272. file.o: warning: objtool: .text+0x53: unreachable instruction
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600228
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500229 Objtool couldn't find a code path to reach the instruction.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600230
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500231 If the error is for an asm file, and the instruction is inside (or
232 reachable from) a callable function, the function should be annotated
233 with the ENTRY/ENDPROC macros (ENDPROC is the important one).
234 Otherwise, the code should probably be annotated with the CFI hint
235 macros in asm/undwarf.h so objtool and the unwinder can know the
236 stack state associated with the code.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600237
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500238 If you're 100% sure the code won't affect stack traces, or if you're
239 a just a bad person, you can tell objtool to ignore it. See the
240 "Adding exceptions" section below.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600241
242 If it's not actually in a callable function (e.g. kernel entry code),
243 change ENDPROC to END.
244
245
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002464. file.o: warning: objtool: func(): can't find starting instruction
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600247 or
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500248 file.o: warning: objtool: func()+0x11dd: can't decode instruction
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600249
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500250 Does the file have data in a text section? If so, that can confuse
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600251 objtool's instruction decoder. Move the data to a more appropriate
252 section like .data or .rodata.
253
254
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002555. file.o: warning: objtool: func()+0x6: unsupported instruction in callable function
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600256
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500257 This is a kernel entry/exit instruction like sysenter or iret. Such
258 instructions aren't allowed in a callable function, and are most
259 likely part of the kernel entry code. They should usually not have
260 the callable function annotation (ENDPROC) and should always be
261 annotated with the CFI hint macros in asm/undwarf.h.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600262
263
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002646. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600265
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500266 This is a dynamic jump or a jump to an undefined symbol. Objtool
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600267 assumed it's a sibling call and detected that the frame pointer
268 wasn't first restored to its original state.
269
270 If it's not really a sibling call, you may need to move the
271 destination code to the local file.
272
273 If the instruction is not actually in a callable function (e.g.
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500274 kernel entry code), change ENDPROC to END and annotate manually with
275 the CFI hint macros in asm/undwarf.h.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600276
277
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002787. file: warning: objtool: func()+0x5c: stack state mismatch
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600279
280 The instruction's frame pointer state is inconsistent, depending on
281 which execution path was taken to reach the instruction.
282
Josh Poimboeufbaa41462017-06-28 10:11:07 -0500283 Make sure that, when CONFIG_FRAME_POINTER is enabled, the function
284 pushes and sets up the frame pointer (for x86_64, this means rbp) at
285 the beginning of the function and pops it at the end of the function.
286 Also make sure that no other code in the function touches the frame
287 pointer.
288
289 Another possibility is that the code has some asm or inline asm which
290 does some unusual things to the stack or the frame pointer. In such
291 cases it's probably appropriate to use the CFI hint macros in
292 asm/undwarf.h.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600293
294
Josh Poimboeufbaa41462017-06-28 10:11:07 -05002958. file.o: warning: objtool: funcA() falls through to next function funcB()
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600296
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500297 This means that funcA() doesn't end with a return instruction or an
298 unconditional jump, and that objtool has determined that the function
299 can fall through into the next function. There could be different
300 reasons for this:
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600301
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500302 1) funcA()'s last instruction is a call to a "noreturn" function like
303 panic(). In this case the noreturn function needs to be added to
304 objtool's hard-coded global_noreturns array. Feel free to bug the
305 objtool maintainer, or you can submit a patch.
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600306
Josh Poimboeufb1547d32016-04-15 09:17:10 -0500307 2) funcA() uses the unreachable() annotation in a section of code
308 that is actually reachable.
309
310 3) If funcA() calls an inline function, the object code for funcA()
311 might be corrupt due to a gcc bug. For more details, see:
312 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70646
313
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600314
315If the error doesn't seem to make sense, it could be a bug in objtool.
316Feel free to ask the objtool maintainer for help.
317
318
319Adding exceptions
320-----------------
321
322If you _really_ need objtool to ignore something, and are 100% sure
323that it won't affect kernel stack traces, you can tell objtool to
324ignore it:
325
326- To skip validation of a function, use the STACK_FRAME_NON_STANDARD
327 macro.
328
329- To skip validation of a file, add
330
331 OBJECT_FILES_NON_STANDARD_filename.o := n
332
333 to the Makefile.
334
335- To skip validation of a directory, add
336
337 OBJECT_FILES_NON_STANDARD := y
338
339 to the Makefile.