User stack pointer randomisation
RFC 1/1 User stack pointer randomisation
This patch adds a page size range randomisation to the user stack pointer.
---
Hi Ralf,
This is taken from the x86 architecture. I modified it a bit so the randomisation range is only a page size range. Since the top of the stack is already randomised, I don't see any point to make the range bigger as this is the case in x86 arch. I'm surely missing something obvious and that's the reason this patch is a RFC.
I tested it and it works fine so far.
Please try to have a look,
Franck
arch/mips/kernel/process.c | 13 +++++++++++++ include/asm-mips/system.h | 2 +- 2 files changed, 14 insertions(+), 1 deletions(-)
arch/mips/kernel/process.c +13 -0
@@ -25,6 +25,7 @@#include <linux/init.h> #include <linux/completion.h> #include <linux/kallsyms.h> +#include <linux/random.h> #include <asm/bootinfo.h> #include <asm/cpu.h>
@@ -460,3 +461,15 @@ unsigned long get_wchan(struct task_struct *task) out:return pc; } + +/* +* Don't forget that the stack pointer must be aligned on a 8 bytes +* boundary at least. +*/ +unsigned long arch_align_stack (unsigned long sp) +{ +if (!(current->personality &ADDR_NO_RANDOMIZE ) && randomize_va_space) + sp -=get_random_int () & ~PAGE_MASK ; + +return sp & ~7 ; +}
include/asm-mips/system.h +1 -1
@@ -355,6 +355,6 @@ extern int stop_a_enabled;*/ #define __ARCH_WANT_UNLOCKED_CTXSW -#define arch_align_stack (x) (x) +extern unsigned long arch_align_stack (unsigned long sp);#endif /* _ASM_SYSTEM_H */
Signature · 1 line
1.5.2.2
arch/mips/kernel/process.c unsigned long get_wchan(struct task_struct *task) · line 475
Quoted 12 lines · Franck Bui-Huu
arch/mips/kernel/process.c +11
-0+/* +* Don't forget that the stack pointer must be aligned on a 8 bytes +* boundary at least. +*/ +unsigned long arch_align_stack (unsigned long sp) +{ +if (!(current->personality &ADDR_NO_RANDOMIZE ) && randomize_va_space) + sp -=get_random_int () & ~PAGE_MASK ; + +return sp & ~7 ; +}
For the 64-bit ABIs (N32 & N64) the stack must be 16 byte aligned.
Nigel
Hi,
Quoted 3 lines · Nigel Stephens
For the 64-bit ABIs (N32 & N64) the stack must be 16 byte aligned.
Thanks for this, I'll cook up a new patch.
Signature · 1 line
Franck
Quoted 2 lines · Franck Bui-Huu
This patch adds a page size range randomisation to the user stack pointer.
Looks fine to me aside of the issue Nigel raised.
There is a constant defining the ABI-specific alignment in <asm/asm.h>:
#if (_MIPS_SIM == _MIPS_SIM_ABI32)
#define ALSZ 7
#define ALMASK ~7
#endif
#if (_MIPS_SIM == _MIPS_SIM_NABI32) || (_MIPS_SIM == _MIPS_SIM_ABI64)
#define ALSZ 15
#define ALMASK ~15
#endif
This will unnecessarily increase the alignment of the stack wasting a few bytes of memory for O32 binaries running on 64-bit kernels but I'd just ignore this artefact; the cure would be uglier than the disease ;-)
Ralf
Hi Ralf,
Quoted 7 lines · Ralf Baechle
Quoted 2 lines · Franck Bui-Huu
This patch adds a page size range randomisation to the user stack pointer.
Looks fine to me aside of the issue Nigel raised.
I'll fix it.
Quoted 2 lines
There is a constant defining the ABI-specific alignment in <
asm/asm.h>:
I didn't know about them. ALSZ name is not really self speaking, don't you think ?
Quoted 9 lines
#if (
_MIPS_SIM==_MIPS_SIM_ABI32)
#define ALSZ 7
#define ALMASK ~7
#endif
#if (_MIPS_SIM==_MIPS_SIM_NABI32) || (_MIPS_SIM==_MIPS_SIM_ABI64)
#define ALSZ 15
#define ALMASK ~15
#endif
this is weird I would have defined them like this instead:
#if (_MIPS_SIM == _MIPS_SIM_ABI32)
#define ALSZ 8
#elif (_MIPS_SIM == _MIPS_SIM_NABI32) || (_MIPS_SIM == _MIPS_SIM_ABI64) #define ALSZ 16
#endif
#define ALMASK (~(ALSZ-1))
Quoted 4 lines
This will unnecessarily increase the alignment of the stack wasting a few bytes of memory for O32 binaries running on 64-bit kernels but I'd just ignore this artefact; the cure would be uglier than the disease ;-)
specially that we don't care to waste a couple of bytes in this case...
Thanks
Signature · 1 line
Franck
Quoted 9 lines · Franck Bui-Huu
this is weird I would have defined them like this instead:
#if (
_MIPS_SIM==_MIPS_SIM_ABI32)
#define ALSZ 8
#elif (_MIPS_SIM==_MIPS_SIM_NABI32) || (_MIPS_SIM==_MIPS_SIM_ABI64) #define ALSZ 16
#endif#define ALMASK (~(ALSZ-1))
<asm/asm.h> which is fairly similar to it's userspace equivalent <sys/asm.h> contains definitions which are some sort of pseudo-standard in the MIPS world, including ALSZ and ALMASK. If I had choosen them I'd have set ALSZ to 8 rsp. 16, just like you ... Anyway, having similar macros makes porting of assembler code easier. This also is why <asm/regdef.h> and <asm/fpregdef.h> are as they are. RISC/os, IRIX, some of the BSD variants, even the non-Linux SDE variants for example for baremetal use a similar set of macros and headers.
Ralf
Quoted 13 lines · Ralf Baechle
Quoted 9 lines · Franck Bui-Huu
this is weird I would have defined them like this instead:
#if (
_MIPS_SIM==_MIPS_SIM_ABI32)
#define ALSZ 8
#elif (_MIPS_SIM==_MIPS_SIM_NABI32) || (_MIPS_SIM==_MIPS_SIM_ABI64) #define ALSZ 16
#endif#define ALMASK (~(ALSZ-1))
Quoted 9 lines
<
asm/asm.h> which is fairly similar to it's userspace equivalent <sys/asm.h> contains definitions which are some sort of pseudo-standard in the MIPS world, including ALSZ and ALMASK. If I had choosen them I'd have set ALSZ to 8 rsp. 16, just like you ... Anyway, having similar macros makes porting of assembler code easier. This also is why <asm/regdef.h> and <asm/fpregdef.h> are as they are. RISC/os, IRIX, some of the BSD variants, even the non-Linux SDE variants for example for baremetal use a similar set of macros and headers.
Thanks for explanations.
That makes me think that we may have the same alignement issue in include/asm-mips/ptrace.h header file:
struct pt_regs {
#ifdef CONFIG_32BIT
/* Pad bytes for argument save space on the stack. */
unsigned long pad0[6];
#endif
/* Saved main processor registers. */
unsigned long regs[32];
/* Saved special registers. */
unsigned long cp0_status;
unsigned long hi;
unsigned long lo;
#ifdef CONFIG_CPU_HAS_SMARTMIPS
unsigned long acx;
#endif
unsigned long cp0_badvaddr;
unsigned long cp0_cause;
unsigned long cp0_epc;
#ifdef CONFIG_MIPS_MT_SMTC
unsigned long cp0_tcstatus;
#endif /* CONFIG_MIPS_MT_SMTC */
} __attribute__ ((aligned (8)));Note that the structure is aligned on a 8 bytes boundary which is not correct for a 64 bit kernel, is it ?
Thanks
Franck