Skip to content

Commit 1ff2975

Browse files
committed
randomize_kstack: Improve docs on requirements/rationale
There were some recent questions about where and why to use the random_kstack routines when applying them to new architectures[1]. Update the header comments to reflect the design choices for the routines. [1] https://lore.kernel.org/lkml/[email protected] Cc: Nicholas Piggin <[email protected]> Cc: Xiu Jianfeng <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 8c6a490 commit 1ff2975

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

include/linux/randomize_kstack.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ DECLARE_PER_CPU(u32, kstack_offset);
4040
*/
4141
#define KSTACK_OFFSET_MAX(x) ((x) & 0x3FF)
4242

43-
/*
44-
* These macros must be used during syscall entry when interrupts and
43+
/**
44+
* add_random_kstack_offset - Increase stack utilization by previously
45+
* chosen random offset
46+
*
47+
* This should be used in the syscall entry path when interrupts and
4548
* preempt are disabled, and after user registers have been stored to
46-
* the stack.
49+
* the stack. For testing the resulting entropy, please see:
50+
* tools/testing/selftests/lkdtm/stack-entropy.sh
4751
*/
4852
#define add_random_kstack_offset() do { \
4953
if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \
@@ -55,6 +59,23 @@ DECLARE_PER_CPU(u32, kstack_offset);
5559
} \
5660
} while (0)
5761

62+
/**
63+
* choose_random_kstack_offset - Choose the random offset for the next
64+
* add_random_kstack_offset()
65+
*
66+
* This should only be used during syscall exit when interrupts and
67+
* preempt are disabled. This position in the syscall flow is done to
68+
* frustrate attacks from userspace attempting to learn the next offset:
69+
* - Maximize the timing uncertainty visible from userspace: if the
70+
* offset is chosen at syscall entry, userspace has much more control
71+
* over the timing between choosing offsets. "How long will we be in
72+
* kernel mode?" tends to be more difficult to predict than "how long
73+
* will we be in user mode?"
74+
* - Reduce the lifetime of the new offset sitting in memory during
75+
* kernel mode execution. Exposure of "thread-local" memory content
76+
* (e.g. current, percpu, etc) tends to be easier than arbitrary
77+
* location memory exposure.
78+
*/
5879
#define choose_random_kstack_offset(rand) do { \
5980
if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT, \
6081
&randomize_kstack_offset)) { \

0 commit comments

Comments
 (0)