From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ingo Molnar Date: Wed, 24 May 2017 08:30:43 +0000 Subject: Re: [PATCH 1/5] random,stackprotect: introduce get_random_canary function Message-Id: <20170524083043.hpbpuracjqj2bbv2@gmail.com> List-Id: References: <20170519212636.30440-1-riel@redhat.com> <20170519212636.30440-2-riel@redhat.com> In-Reply-To: <20170519212636.30440-2-riel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: linux-arm-kernel@lists.infradead.org * riel@redhat.com wrote: > From: Rik van Riel > > 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. > > Signed-off-by: Rik van Riel > --- > include/linux/random.h | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/include/linux/random.h b/include/linux/random.h > index ed5c3838780d..765a992c6774 100644 > --- a/include/linux/random.h > +++ b/include/linux/random.h > @@ -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 > + * 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; > +} 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 From mboxrd@z Thu Jan 1 00:00:00 1970 From: mingo@kernel.org (Ingo Molnar) Date: Wed, 24 May 2017 10:30:43 +0200 Subject: [PATCH 1/5] random,stackprotect: introduce get_random_canary function In-Reply-To: <20170519212636.30440-2-riel@redhat.com> References: <20170519212636.30440-1-riel@redhat.com> <20170519212636.30440-2-riel@redhat.com> Message-ID: <20170524083043.hpbpuracjqj2bbv2@gmail.com> To: linux-arm-kernel@lists.infradead.org List-Id: linux-arm-kernel.lists.infradead.org * riel at redhat.com wrote: > From: Rik van Riel > > 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. > > Signed-off-by: Rik van Riel > --- > include/linux/random.h | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/include/linux/random.h b/include/linux/random.h > index ed5c3838780d..765a992c6774 100644 > --- a/include/linux/random.h > +++ b/include/linux/random.h > @@ -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 > + * 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; > +} 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 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936822AbdEXIa6 (ORCPT ); Wed, 24 May 2017 04:30:58 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:33227 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934658AbdEXIaw (ORCPT ); Wed, 24 May 2017 04:30:52 -0400 Date: Wed, 24 May 2017 10:30:43 +0200 From: Ingo Molnar To: riel@redhat.com Cc: linux-kernel@vger.kernel.org, danielmicay@gmail.com, tytso@mit.edu, keescook@chromium.org, hpa@zytor.com, luto@amacapital.net, x86@kernel.org, linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com, linux-sh@vger.kernel.org, ysato@users.sourceforge.jp Subject: Re: [PATCH 1/5] random,stackprotect: introduce get_random_canary function Message-ID: <20170524083043.hpbpuracjqj2bbv2@gmail.com> References: <20170519212636.30440-1-riel@redhat.com> <20170519212636.30440-2-riel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170519212636.30440-2-riel@redhat.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * riel@redhat.com wrote: > From: Rik van Riel > > 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. > > Signed-off-by: Rik van Riel > --- > include/linux/random.h | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/include/linux/random.h b/include/linux/random.h > index ed5c3838780d..765a992c6774 100644 > --- a/include/linux/random.h > +++ b/include/linux/random.h > @@ -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 > + * 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; > +} 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