stackprotector: ascii armor the stack canary
Zero out the first byte of the stack canary value on 64 bit systems, in order to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if an attacker somehow guessed or obtained the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
Thanks to Daniel Micay for extracting code of similar functionality from PaX/grsecurity and making it easy to find in his linux-hardened git tree on https://github.com/thestinger/linux-hardened/
PATCH 1/5 random,stackprotect: introduce get_random_canary function
From: Rik van Riel <[email protected]>
Introduce the get_random_canary function, which provides a random unsigned long canary value with the first byte zeroed out on 64 bit architectures, in order to mitigate non-terminated C string overflows.
Inspired by the "ascii armor" code in the old execshield patches, and the current PaX/grsecurity code base.
--- include/linux/random.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/random.h +20 -0
@@ -57,6 +57,26 @@ static inline unsigned long get_random_long(void)#endif } +/* +* On 64 bit architectures, protect against non-terminated C string overflows +* by zeroing out the first byte of the canary; this leaves 56 bits of entropy. +*/ +#ifdef CONFIG_64BIT +#ifdef __LITTLE_ENDIAN +#define CANARY_MASK 0xffffffffffffff00UL +#else /* big endian 64 bits */ +#define CANARY_MASK 0x00ffffffffffffffUL +#endif +#else /* 32 bits */ +#define CANARY_MASK 0xffffffffUL +#endif +static inline unsigned long get_random_canary (void ) +{ +unsigned long val =get_random_long (); + +return val &CANARY_MASK ; +} +unsigned long randomize_page (unsigned long start,unsigned long range);u32 prandom_u32 (void );
Signature · 1 line
2.9.3
Re: [PATCH 1/5] random,stackprotect: introduce get_random_canary function
Quoted 20 lines · riel at redhat.com
From: Rik van Riel <[email protected]>
Introduce the
get_random_canaryfunction, which provides a random unsigned long canary value with the first byte zeroed out on 64 bit architectures, in order to mitigate non-terminated C string overflows.Inspired by the "ascii armor" code in the old execshield patches, and the current PaX/grsecurity code base.
Signed-off-by Rik van Riel <[email protected]>--- include/linux/random.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)include/linux/random.h +0
-0
include/linux/random.h static inline unsigned long get_random_long(void) · line 61
Quoted 5 lines
include/linux/random.h +2
-0@@ -57,6 +57,26 @@ static inline unsigned long get_random_long(void)#endif } +/* +* On 64 bit architectures, protect against non-terminated C string overflows
s/64 bit/64-bit
Quoted 7 lines
include/linux/random.h +7
-0+* by zeroing out the first byte of the canary; this leaves 56 bits of entropy. +*/ +#ifdef CONFIG_64BIT +#ifdef __LITTLE_ENDIAN +#define CANARY_MASK 0xffffffffffffff00UL +#else /* big endian 64 bits */ +#define CANARY_MASK 0x00ffffffffffffffUL
include/linux/random.h static inline unsigned long get_random_long(void) · line 78
Quoted 10 lines
include/linux/random.h +10
-0+#endif +#else /* 32 bits */ +#define CANARY_MASK 0xffffffffUL +#endif +static inline unsigned long get_random_canary (void ) +{ +unsigned long val =get_random_long (); + +return val &CANARY_MASK ; +}
Please separate function definitions from macros by a separate empty line.
Also, a bit of structure and organization would make the macro easier to read:
#ifdef CONFIG_64BIT
# ifdef __LITTLE_ENDIAN # define CANARY_MASK 0xffffffffffffff00UL # else /* big endian, 64 bits: */ # define CANARY_MASK 0x00ffffffffffffffUL # endif #else /* 32 bits: */ # define CANARY_MASK 0x00000000ffffffffUL #endif
Thanks,
Ingo
PATCH 2/5 fork,random: use get_random_canary to set tsk->stack_canary
In reply to [email protected]From: Rik van Riel <[email protected]>
Use the ascii-armor canary to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if they somehow obtain the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
--- kernel/fork.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/fork.c +1 -1
@@ -560,7 +560,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)set_task_stack_end_magic (tsk);#ifdef CONFIG_CC_STACKPROTECTOR - tsk->stack_canary =get_random_long (); + tsk->stack_canary =get_random_canary ();#endif /*
Signature · 1 line
2.9.3
PATCH 3/5 x86: ascii armor the x86_64 boot init stack canary
In reply to [email protected]From: Rik van Riel <[email protected]>
Use the ascii-armor canary to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if they somehow obtain the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
--- arch/x86/include/asm/stackprotector.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/stackprotector.h +1 -0
@@ -74,6 +74,7 @@ static __always_inline void boot_init_stack_canary(void)get_random_bytes (&canary,sizeof (canary)); tsc =rdtsc (); canary += tsc + (tsc <<32UL ); + canary &=CANARY_MASK ; current->stack_canary = canary;#ifdef CONFIG_X86_64
Signature · 1 line
2.9.3
PATCH 4/5 arm64: ascii armor the arm64 boot init stack canary
In reply to [email protected]From: Rik van Riel <[email protected]>
Use the ascii-armor canary to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if they somehow obtain the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
--- arch/arm64/include/asm/stackprotector.h | 1 + 1 file changed, 1 insertion(+)
arch/arm64/include/asm/stackprotector.h +1 -0
@@ -30,6 +30,7 @@ static __always_inline void boot_init_stack_canary(void)/* Try to get a semi random initial value. */ get_random_bytes (&canary,sizeof (canary)); canary ^=LINUX_VERSION_CODE ; + canary &=CANARY_MASK ; current->stack_canary = canary; __stack_chk_guard = current->stack_canary;
Signature · 1 line
2.9.3
PATCH 5/5 sh64: ascii armor the sh64 boot init stack canary
In reply to [email protected]From: Rik van Riel <[email protected]>
Use the ascii-armor canary to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if they somehow obtain the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
--- arch/sh/include/asm/stackprotector.h | 1 + 1 file changed, 1 insertion(+)
arch/sh/include/asm/stackprotector.h +1 -0
@@ -19,6 +19,7 @@ static __always_inline void boot_init_stack_canary(void)/* Try to get a semi random initial value. */ get_random_bytes (&canary,sizeof (canary)); canary ^=LINUX_VERSION_CODE ; + canary &=CANARY_MASK ; current->stack_canary = canary; __stack_chk_guard = current->stack_canary;
Signature · 1 line
2.9.3
Kees Cook
Acked-byQuoted 4 lines · On Fri, May 19, 2017 at 2:26 PM, <[email protected]> wrote:
Zero out the first byte of the stack canary value on 64 bit systems, in order to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if an attacker somehow guessed or obtained the canary value.
This also stops string functions from being able to read the canary.
It might also be worth mentioning that the reduction in entropy for 64-bit to gain this corner-case protection is worth it, but on 32-bit, it is not. (Which is especially true given that the 64-bit canary was only 32-bits in some cases until recently.)
Quoted 5 lines
Inspired by execshield ascii-armor and PaX/grsecurity.
Thanks to Daniel Micay for extracting code of similar functionality from PaX/grsecurity and making it easy to find in his linux-hardened git tree on https://github.com/thestinger/linux-hardened/
Thanks!
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 6 lines · Kees Cook
Quoted 4 lines · On Fri, May 19, 2017 at 2:26 PM, <[email protected]> wrote:
Zero out the first byte of the stack canary value on 64 bit systems, in order to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if an attacker somehow guessed or obtained the canary value.
Quoted 6 lines
This also stops string functions from being able to read the canary.
It might also be worth mentioning that the reduction in entropy for 64-bit to gain this corner-case protection is worth it, but on 32-bit, it is not. (Which is especially true given that the 64-bit canary was only 32-bits in some cases until recently.)
+1
It took me a while to deduce that myself, when I started wondering why this was not done for 32-bit.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert at linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 10 lines · riel at redhat.com
Zero out the first byte of the stack canary value on 64 bit systems, in order to prevent unterminated C string overflows from being able to successfully overwrite the canary, even if an attacker somehow guessed or obtained the canary value.
Inspired by execshield ascii-armor and PaX/grsecurity.
Thanks to Daniel Micay for extracting code of similar functionality from PaX/grsecurity and making it easy to find in his linux-hardened git tree on https://github.com/thestinger/linux-hardened/
To clarify something this part isn't from PaX / grsecurity. I marked the commits from PaX / grsecurity as such in their commit messages and these are among the ones that aren't from there.
This is from a set of changes that I did for CopperheadOS and forward ported to mainline recently to start linux-hardened. It was only arm64 for CopperheadOS. The overlap with PaX is that when adding the leading zero byte for x86, I needed to first fix get_random_int being used for the per-task canary value. I didn't know PaX fixed it way back in 2007.
I implemented heap canaries for our userspace malloc implementation and then later did the same thing for slub in the kernel. I added a leading zero byte to both of those heap canaries later on and then did the SSP implementation in Bionic and the kernel's arm64 code. I took the idea from glibc but limited it to 64-bit where there's entropy to spare. The glibc leading zero might have come from execshield, but I don't know.