Skip to content

Commit 048e290

Browse files
cuiyunhuipalmer-dabbelt
authored andcommitted
riscv: Randomize lower bits of stack address
Implement arch_align_stack() to randomize the lower bits of the stack address. Signed-off-by: Yunhui Cui <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 11c2dbd commit 048e290

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

arch/riscv/include/asm/exec.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
3+
#ifndef __ASM_EXEC_H
4+
#define __ASM_EXEC_H
5+
6+
extern unsigned long arch_align_stack(unsigned long sp);
7+
8+
#endif /* __ASM_EXEC_H */

arch/riscv/kernel/process.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/tick.h>
1616
#include <linux/ptrace.h>
1717
#include <linux/uaccess.h>
18+
#include <linux/personality.h>
1819

1920
#include <asm/unistd.h>
2021
#include <asm/processor.h>
@@ -26,6 +27,7 @@
2627
#include <asm/cpuidle.h>
2728
#include <asm/vector.h>
2829
#include <asm/cpufeature.h>
30+
#include <asm/exec.h>
2931

3032
#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_STACKPROTECTOR_PER_TASK)
3133
#include <linux/stackprotector.h>
@@ -99,6 +101,13 @@ void show_regs(struct pt_regs *regs)
99101
dump_backtrace(regs, NULL, KERN_DEFAULT);
100102
}
101103

104+
unsigned long arch_align_stack(unsigned long sp)
105+
{
106+
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
107+
sp -= get_random_u32_below(PAGE_SIZE);
108+
return sp & ~0xf;
109+
}
110+
102111
#ifdef CONFIG_COMPAT
103112
static bool compat_mode_supported __read_mostly;
104113

0 commit comments

Comments
 (0)