Skip to content

Commit 0f60a8e

Browse files
committed
mm: Implement stack frame object validation
This creates per-architecture function arch_within_stack_frames() that should validate if a given object is contained by a kernel stack frame. Initial implementation is on x86. This is based on code from PaX. Signed-off-by: Kees Cook <[email protected]>
1 parent 7c15d9b commit 0f60a8e

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

arch/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,15 @@ config CC_STACKPROTECTOR_STRONG
424424

425425
endchoice
426426

427+
config HAVE_ARCH_WITHIN_STACK_FRAMES
428+
bool
429+
help
430+
An architecture should select this if it can walk the kernel stack
431+
frames to determine if an object is part of either the arguments
432+
or local variables (i.e. that it excludes saved return addresses,
433+
and similar) by implementing an inline arch_within_stack_frames(),
434+
which is used by CONFIG_HARDENED_USERCOPY.
435+
427436
config HAVE_CONTEXT_TRACKING
428437
bool
429438
help

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ config X86
9191
select HAVE_ARCH_SOFT_DIRTY if X86_64
9292
select HAVE_ARCH_TRACEHOOK
9393
select HAVE_ARCH_TRANSPARENT_HUGEPAGE
94+
select HAVE_ARCH_WITHIN_STACK_FRAMES
9495
select HAVE_EBPF_JIT if X86_64
9596
select HAVE_CC_STACKPROTECTOR
9697
select HAVE_CMPXCHG_DOUBLE

arch/x86/include/asm/thread_info.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,50 @@ static inline unsigned long current_stack_pointer(void)
180180
return sp;
181181
}
182182

183+
/*
184+
* Walks up the stack frames to make sure that the specified object is
185+
* entirely contained by a single stack frame.
186+
*
187+
* Returns:
188+
* 1 if within a frame
189+
* -1 if placed across a frame boundary (or outside stack)
190+
* 0 unable to determine (no frame pointers, etc)
191+
*/
192+
static inline int arch_within_stack_frames(const void * const stack,
193+
const void * const stackend,
194+
const void *obj, unsigned long len)
195+
{
196+
#if defined(CONFIG_FRAME_POINTER)
197+
const void *frame = NULL;
198+
const void *oldframe;
199+
200+
oldframe = __builtin_frame_address(1);
201+
if (oldframe)
202+
frame = __builtin_frame_address(2);
203+
/*
204+
* low ----------------------------------------------> high
205+
* [saved bp][saved ip][args][local vars][saved bp][saved ip]
206+
* ^----------------^
207+
* allow copies only within here
208+
*/
209+
while (stack <= frame && frame < stackend) {
210+
/*
211+
* If obj + len extends past the last frame, this
212+
* check won't pass and the next frame will be 0,
213+
* causing us to bail out and correctly report
214+
* the copy as invalid.
215+
*/
216+
if (obj + len <= frame)
217+
return obj >= oldframe + 2 * sizeof(void *) ? 1 : -1;
218+
oldframe = frame;
219+
frame = *(const void * const *)frame;
220+
}
221+
return -1;
222+
#else
223+
return 0;
224+
#endif
225+
}
226+
183227
#else /* !__ASSEMBLY__ */
184228

185229
#ifdef CONFIG_X86_64

include/linux/thread_info.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ static inline bool test_and_clear_restore_sigmask(void)
146146
#error "no set_restore_sigmask() provided and default one won't work"
147147
#endif
148148

149+
#ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
150+
static inline int arch_within_stack_frames(const void * const stack,
151+
const void * const stackend,
152+
const void *obj, unsigned long len)
153+
{
154+
return 0;
155+
}
156+
#endif
157+
149158
#endif /* __KERNEL__ */
150159

151160
#endif /* _LINUX_THREAD_INFO_H */

0 commit comments

Comments
 (0)