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: 9738211 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 5CE7F601C2 for ; Fri, 19 May 2017 21:35:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3677C284FE for ; Fri, 19 May 2017 21:35:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 292FB28501; Fri, 19 May 2017 21:35:04 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA70D284FE for ; Fri, 19 May 2017 21:35:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753375AbdESVfD (ORCPT ); Fri, 19 May 2017 17:35:03 -0400 Received: from shelob.surriel.com ([96.67.55.147]:35578 "EHLO shelob.surriel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752472AbdESVfC (ORCPT ); Fri, 19 May 2017 17:35:02 -0400 X-Greylist: delayed 496 seconds by postgrey-1.27 at vger.kernel.org; Fri, 19 May 2017 17:35:02 EDT 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 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 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> Sender: linux-sh-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.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);