Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1645968

[PATCH 1/5] random,stackprotect: introduce get_random_canary function

Path csiph.com!news.mixmin.net!aioe.org!news.servidellagleba.it!bofh.it!news.nic.it!robomod
From riel@redhat.com
Newsgroups linux.kernel
Subject [PATCH 1/5] random,stackprotect: introduce get_random_canary function
Date Fri, 19 May 2017 23:40:02 +0200
Message-ID <tIXMS-86v-23@gated-at.bofh.it> (permalink)
References <tIXMR-86v-5@gated-at.bofh.it>
X-Original-To linux-kernel@vger.kernel.org
X-Greylist delayed 496 seconds by postgrey-1.27 at vger.kernel.org; Fri, 19 May 2017 17:35:02 EDT
X-Mailer git-send-email 2.9.3
Sender robomod@news.nic.it
List-ID <linux-kernel.vger.kernel.org>
X-Mailing-List linux-kernel@vger.kernel.org
Approved robomod@news.nic.it
Lines 48
Organization linux.* mail to news gateway
X-Original-Cc danielmicay@gmail.com, tytso@mit.edu, keescook@chromium.org, hpa@zytor.com, luto@amacapital.net, mingo@kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com, linux-sh@vger.kernel.org, ysato@users.sourceforge.jp
X-Original-Date Fri, 19 May 2017 17:26:32 -0400
X-Original-Message-ID <20170519212636.30440-2-riel@redhat.com>
X-Original-References <20170519212636.30440-1-riel@redhat.com>
X-Original-Sender linux-kernel-owner@vger.kernel.org
Xref csiph.com linux.kernel:1645968

Show key headers only | View raw


From: Rik van Riel <riel@redhat.com>

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 <riel@redhat.com>
---
 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
+ * 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);
-- 
2.9.3

Back to linux.kernel | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

[PATCH 1/5] random,stackprotect: introduce get_random_canary function riel@redhat.com - 2017-05-19 23:40 +0200
  Re: [PATCH 1/5] random,stackprotect: introduce get_random_canary  function Ingo Molnar <mingo@kernel.org> - 2017-05-24 10:40 +0200

csiph-web