Skip to content

Commit f4a0318

Browse files
Xiu Jianfengmpe
authored andcommitted
powerpc: add support for syscall stack randomization
Add support for adding a random offset to the stack while handling syscalls. This patch uses mftb() instead of get_random_int() for better performance. In order to avoid unconditional stack canaries on syscall entry (due to the use of alloca()), also disable stack protector to avoid triggering needless checks and slowing down the entry path. As there is no general way to control stack protector coverage with a function attribute, this must be disabled at the compilation unit level. Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/20220701082435.126596-3-xiujianfeng@huawei.com
1 parent 1547db7 commit f4a0318

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ config PPC
196196
select HAVE_ARCH_KASAN if PPC_BOOK3E_64
197197
select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
198198
select HAVE_ARCH_KFENCE if PPC_BOOK3S_32 || PPC_8xx || 40x
199+
select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
199200
select HAVE_ARCH_KGDB
200201
select HAVE_ARCH_MMAP_RND_BITS
201202
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT

arch/powerpc/kernel/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ CFLAGS_cputable.o += -DDISABLE_BRANCH_PROFILING
5454
CFLAGS_btext.o += -DDISABLE_BRANCH_PROFILING
5555
endif
5656

57+
#ifdef CONFIG_RANDOMIZE_KSTACK_OFFSET
58+
# Remove stack protector to avoid triggering unneeded stack canary
59+
# checks due to randomize_kstack_offset.
60+
CFLAGS_REMOVE_syscall.o = -fstack-protector -fstack-protector-strong
61+
CFLAGS_syscall.o += -fno-stack-protector
62+
#endif
63+
5764
obj-y := cputable.o syscalls.o \
5865
irq.o align.o signal_$(BITS).o pmc.o vdso.o \
5966
process.o systbl.o idle.o \

arch/powerpc/kernel/syscall.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <linux/compat.h>
44
#include <linux/context_tracking.h>
5+
#include <linux/randomize_kstack.h>
56

67
#include <asm/interrupt.h>
78
#include <asm/kup.h>
@@ -18,10 +19,12 @@ notrace long system_call_exception(long r3, long r4, long r5,
1819
long r6, long r7, long r8,
1920
unsigned long r0, struct pt_regs *regs)
2021
{
22+
long ret;
2123
syscall_fn f;
2224

2325
kuap_lock();
2426

27+
add_random_kstack_offset();
2528
regs->orig_gpr3 = r3;
2629

2730
if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))
@@ -169,5 +172,19 @@ notrace long system_call_exception(long r3, long r4, long r5,
169172
f = (void *)sys_call_table[r0];
170173
}
171174

172-
return f(r3, r4, r5, r6, r7, r8);
175+
ret = f(r3, r4, r5, r6, r7, r8);
176+
177+
/*
178+
* Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
179+
* so the maximum stack offset is 1k bytes (10 bits).
180+
*
181+
* The actual entropy will be further reduced by the compiler when
182+
* applying stack alignment constraints: the powerpc architecture
183+
* may have two kinds of stack alignment (16-bytes and 8-bytes).
184+
*
185+
* So the resulting 6 or 7 bits of entropy is seen in SP[9:4] or SP[9:3].
186+
*/
187+
choose_random_kstack_offset(mftb());
188+
189+
return ret;
173190
}

0 commit comments

Comments
 (0)