From patchwork Fri May 19 21:26:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 9738193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 56D01601C2 for ; Fri, 19 May 2017 21:27:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3AC43206AC for ; Fri, 19 May 2017 21:27:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2C471284AF; Fri, 19 May 2017 21:27:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 20C5F206AC for ; Fri, 19 May 2017 21:27:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=VIy3QTuayXRYX0pSza52Ktu95354Z6N5i0dE9RieBMo=; b=YQD80xx07E+UseUcAx3g7SZF3J MPjTpIVBVEKAqI9CMjQVskqefu1ieqfRYIpzAjksbtRD+kjYORlISuJBF2NNe3dHW7cWWSH7uIgyo X1krjjWbwbr2vjO+FmZc3Ukn4cAnZOIDN4Eq6rzJ3bg/bx4HaPXF4MKN8LDz+I8L2gbAcIhLWxNSM k8Ucs/C6jbKy7Bja79Pyg3mgJal8R0mdt1I7MyiJ4BNTblUVQoEXHWPcKxU/zsvwte/+/yiGtC7yX b4MXHMXUDXZ68XxEah2hyQN4P4v7l7hn7q8Gza4a1JlmxoewMUbv4h5D1OrIPLRtghWLxxrBVhqvx dIynMJJg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dBpR2-0001U8-TK; Fri, 19 May 2017 21:27:44 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dBpQz-0001TE-Sw for linux-arm-kernel@bombadil.infradead.org; Fri, 19 May 2017 21:27:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=BAYmnNpFKPDuOJvyeMSEUK2A/DIoKEUl547vEFiBRKY=; b=ZOTJnEl0mm9BkRG8n7MmjnYMF CenCVm1TVBB2hFM3+yCOpTddByret+bz+BXNmHmW7aftlNaoDLBFJZ4xpVKn4bE5UQGtTCoysSXvU L8HOAsoCsHNwWpwBzAZwfTSfM+W09OBqmrTgtkwlBCzL4UL7W6JmXutxfQ4XfapgOFlrzY4XDNSMM UYOot6h7PIBrxfRT0WAodUmKN/5fPYchZT+8Ga5nM7IBz2sLlA09Ep1mqrI9Tu/WAy4r9c1QvSLoW qmrg+zUNRNp1zwi0GMTuIZ/lLDclSHxEGT5ouDvADPRK5k/VUbuZ2nJ0akn8V77KWbBJt74fkl5SM nG2EcdOQQ==; Received: from shelob.surriel.com ([96.67.55.147]) by merlin.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dBpQy-00006N-Bv for linux-arm-kernel@lists.infradead.org; Fri, 19 May 2017 21:27:41 +0000 Received: from [2603:3005:d05:2b00:224:e8ff:fe38:995c] (helo=annuminas.surriel.com) by shelob.surriel.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1dBpQ0-00048q-G9; Fri, 19 May 2017 17:26:40 -0400 Received: from annuminas.surriel.com (localhost.localdomain [127.0.0.1]) by annuminas.surriel.com (8.15.2/8.14.5) with ESMTP id v4JLQeXN000750; Fri, 19 May 2017 17:26:40 -0400 Received: (from riel@localhost) by annuminas.surriel.com (8.15.2/8.15.2/Submit) id v4JLQen7000745; Fri, 19 May 2017 17:26:40 -0400 X-Authentication-Warning: annuminas.surriel.com: riel set sender to riel@redhat.com using -f From: riel@redhat.com To: linux-kernel@vger.kernel.org Subject: [PATCH 1/5] random, stackprotect: introduce get_random_canary function Date: Fri, 19 May 2017 17:26:32 -0400 Message-Id: <20170519212636.30440-2-riel@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170519212636.30440-1-riel@redhat.com> References: <20170519212636.30440-1-riel@redhat.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170519_172740_497675_8B7F7FED X-CRM114-Status: UNSURE ( 9.10 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tytso@mit.edu, keescook@chromium.org, ysato@users.sourceforge.jp, linux-sh@vger.kernel.org, catalin.marinas@arm.com, x86@kernel.org, luto@amacapital.net, danielmicay@gmail.com, hpa@zytor.com, mingo@kernel.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 + * 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);