Skip to content

Commit bae1cd3

Browse files
svens-s390hcahca
authored andcommitted
s390/entry: add support for syscall stack randomization
This adds support for adding a random offset to the stack while handling syscalls. The patch uses get_tod_clock_fast() as this is considered good enough and has much less performance penalty compared to using get_random_int(). The patch also adds randomization in pgm_check_handler() as the sigreturn/rt_sigreturn system calls might be called from there. Signed-off-by: Sven Schnelle <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Heiko Carstens <[email protected]>
1 parent f5b474d commit bae1cd3

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

arch/s390/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ config S390
137137
select HAVE_ARCH_JUMP_LABEL_RELATIVE
138138
select HAVE_ARCH_KASAN
139139
select HAVE_ARCH_KASAN_VMALLOC
140+
select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
140141
select HAVE_ARCH_SECCOMP_FILTER
141142
select HAVE_ARCH_SOFT_DIRTY
142143
select HAVE_ARCH_TRACEHOOK

arch/s390/include/asm/entry-common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
#include <linux/sched.h>
66
#include <linux/audit.h>
7+
#include <linux/randomize_kstack.h>
78
#include <linux/tracehook.h>
89
#include <linux/processor.h>
910
#include <linux/uaccess.h>
11+
#include <asm/timex.h>
1012
#include <asm/fpu/api.h>
1113

1214
#define ARCH_EXIT_TO_USER_MODE_WORK (_TIF_GUARDED_STORAGE | _TIF_PER_TRAP)
@@ -48,6 +50,14 @@ static __always_inline void arch_exit_to_user_mode(void)
4850

4951
#define arch_exit_to_user_mode arch_exit_to_user_mode
5052

53+
static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
54+
unsigned long ti_work)
55+
{
56+
choose_random_kstack_offset(get_tod_clock_fast() & 0xff);
57+
}
58+
59+
#define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_prepare
60+
5161
static inline bool on_thread_stack(void)
5262
{
5363
return !(((unsigned long)(current->stack) ^ current_stack_pointer()) & ~(THREAD_SIZE - 1));

arch/s390/kernel/syscall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ void do_syscall(struct pt_regs *regs)
142142

143143
void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
144144
{
145+
add_random_kstack_offset();
145146
enter_from_user_mode(regs);
146147

147148
memcpy(&regs->gprs[8], S390_lowcore.save_area_sync, 8 * sizeof(unsigned long));

arch/s390/kernel/traps.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "asm/ptrace.h"
1818
#include <linux/kprobes.h>
1919
#include <linux/kdebug.h>
20+
#include <linux/randomize_kstack.h>
2021
#include <linux/extable.h>
2122
#include <linux/ptrace.h>
2223
#include <linux/sched.h>
@@ -301,6 +302,7 @@ void noinstr __do_pgm_check(struct pt_regs *regs)
301302
unsigned int trapnr, syscall_redirect = 0;
302303
irqentry_state_t state;
303304

305+
add_random_kstack_offset();
304306
regs->int_code = *(u32 *)&S390_lowcore.pgm_ilc;
305307
regs->int_parm_long = S390_lowcore.trans_exc_code;
306308

0 commit comments

Comments
 (0)