From patchwork Tue Oct 17 04:52:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobin Harding X-Patchwork-Id: 10010755 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 28B02601E7 for ; Tue, 17 Oct 2017 04:53:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A45C2875B for ; Tue, 17 Oct 2017 04:53:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0EE5B28762; Tue, 17 Oct 2017 04:53:43 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id BDAED2875B for ; Tue, 17 Oct 2017 04:53:40 +0000 (UTC) Received: (qmail 18110 invoked by uid 550); 17 Oct 2017 04:53:37 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 18083 invoked from network); 17 Oct 2017 04:53:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tobin.cc; h=cc :date:from:message-id:subject:to:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=5tY+JYjfpeX5E4upZk6ZCHLhDFOJr+IRxinTdRLrO mA=; b=RYiQ5oH+OkJeKnSs/DnlcQ9f9z57JjB/ceotsEGSQMftYtqWVADcdYSRM 9VXwf6YwlqSnTNXs65VhKobZtqyKfHeWXX6qgVKmhPfrSYCP2Y3MUvquYGVrRUrA 2cRxtN9HJZIBPSMJgy4cInIGyLy5i0uS44Lr3+RfIMDU/PfG/ErI6SCbUp2eBJuw cn/82ni8tipgStLjxbzLxZhFzn1TXnPJa0ix2/E5sSdX3HYh6ojyVvZNoKJvwF3j fc/eGddxSGqz97A0pt64HENL203eRZ94ReDhvJaApEo6dFOAX1kRW3VPcqLJL7c5 eoJ29v9iGdVt9nJ84E/Ukm3Fk7M8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:message-id:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=5tY+JYjfpeX5E4upZ k6ZCHLhDFOJr+IRxinTdRLrOmA=; b=PRNzz+0pMCe9BXit2K1pQTCs/Dt+8E63/ PPQw8mEMMF43X1YxYHsPDQaeWlhx5MwI9NfeZWrN0EgMy2dOzzpiIDZads68UFbJ pqcPCQoIOmxurqq88GnWTSeNTTAiI7AgPUGgSLXeeJkYZ2TDRmHOCdhY//IzBdff mmgQoleax2uBJDayxH7I9LDGIx2UyHYa96dQ91Qndyf4pPlKGdpF2Sw3/4x+YUWz UOvDhLdHB8f8Vb8LOYFvpwhxjuwLzpOVYHlqP08R1LVFXqSRraalXvW6gOEMNk6b Xul8TGfP57c6lzNOOm18oqb9iDk4Kdn0YaPBmeT5pzDTabxFn+NKg== X-ME-Sender: From: "Tobin C. Harding" To: kernel-hardening@lists.openwall.com Cc: "Tobin C. Harding" , Linus Torvalds , Kees Cook , Paolo Bonzini , Tycho Andersen , "Roberts, William C" , Tejun Heo , Jordan Glover , Greg KH , Petr Mladek , Joe Perches , Ian Campbell , Sergey Senozhatsky , Catalin Marinas , Will Deacon , Steven Rostedt , Chris Fries , Dave Weinstein , Daniel Micay , Djalal Harouni , linux-kernel@vger.kernel.org Date: Tue, 17 Oct 2017 15:52:51 +1100 Message-Id: <1508215972-7769-1-git-send-email-me@tobin.cc> X-Mailer: git-send-email 2.7.4 Subject: [kernel-hardening] [PATCH v2] printk: hash addresses printed with %p X-Virus-Scanned: ClamAV using ClamSMTP Currently there are many places in the kernel where addresses are being printed using an unadorned %p. Kernel pointers should be printed using %pK allowing some control via the kptr_restrict sysctl. Exposing addresses gives attackers sensitive information about the kernel layout in memory. We can reduce the attack surface by hashing all addresses printed with %p. This will of course break some users, forcing code printing needed addresses to be updated. For what it's worth, usage of unadorned %p can be broken down as follows git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l arch: 2512 block: 20 crypto: 12 fs: 1221 include: 147 kernel: 109 lib: 77 mm: 120 net: 1516 security: 11 sound: 168 virt: 2 drivers: 8420 Add helper function siphash_1ulong(). Add function ptr_to_id() to map an address to a 32 bit unique identifier. Signed-off-by: Tobin C. Harding --- V2: - Use SipHash to do the hashing The discussion related to this patch has been fragmented. There are three other threads associated with this patch. Email threads by subject: [PATCH] printk: hash addresses printed with %p [PATCH 0/3] add %pX specifier [kernel-hardening] [RFC V2 0/6] add more kernel pointer filter options include/linux/siphash.h | 2 ++ lib/siphash.c | 13 +++++++++++++ lib/vsprintf.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/linux/siphash.h b/include/linux/siphash.h index fa7a6b9cedbf..a9392568c8b8 100644 --- a/include/linux/siphash.h +++ b/include/linux/siphash.h @@ -26,6 +26,8 @@ u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); #endif +unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t *key); + u64 siphash_1u64(const u64 a, const siphash_key_t *key); u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); u64 siphash_3u64(const u64 a, const u64 b, const u64 c, diff --git a/lib/siphash.c b/lib/siphash.c index 3ae58b4edad6..63f4ff57c9ce 100644 --- a/lib/siphash.c +++ b/lib/siphash.c @@ -116,6 +116,19 @@ EXPORT_SYMBOL(__siphash_unaligned); #endif /** + * siphash_1ulong - computes siphash PRF value + * @first: value to hash + * @key: the siphash key + */ +unsigned long siphash_1ulong(const unsigned long first, const siphash_key_t *key) +{ +#ifdef CONFIG_64BIT + return (unsigned long)siphash_1u64((u64)first, key); +#endif + return (unsigned long)siphash_1u32((u32)first, key); +} + +/** * siphash_1u64 - compute 64-bit siphash PRF value of a u64 * @first: first u64 * @key: the siphash key diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 86c3385b9eb3..afd1c835b0f6 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef CONFIG_BLOCK #include #endif @@ -503,6 +504,7 @@ char *number(char *buf, char *end, unsigned long long num, *buf = '0'; ++buf; } + /* actual digits of result */ while (--i >= 0) { if (buf < end) @@ -1591,6 +1593,28 @@ char *device_node_string(char *buf, char *end, struct device_node *dn, return widen_string(buf, buf - buf_start, end, spec); } +/* Maps a pointer to a 32 bit unique identifier. */ +static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) +{ + static siphash_key_t ptr_secret __read_mostly; + static bool have_key = false; + unsigned long hashval; + + /* Kernel doesn't boot if we use get_random_once() */ + if (!have_key) { + get_random_bytes(&ptr_secret, sizeof(ptr_secret)); + have_key = true; + } + + hashval = siphash_1ulong((unsigned long)ptr, &ptr_secret); + + spec.field_width = 2 + 2 * sizeof(unsigned int); /* 0x + hex */ + spec.flags = SPECIAL | SMALL | ZEROPAD; + spec.base = 16; + + return number(buf, end, (u32)hashval, spec); +} + int kptr_restrict __read_mostly; /* @@ -1703,6 +1727,9 @@ int kptr_restrict __read_mostly; * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 * function pointers are really function descriptors, which contain a * pointer to the real address. + * + * Default behaviour (unadorned %p) is to hash the address, rendering it useful + * as a unique identifier. */ static noinline_for_stack char *pointer(const char *fmt, char *buf, char *end, void *ptr, @@ -1858,14 +1885,13 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr, return device_node_string(buf, end, ptr, spec, fmt + 1); } } - spec.flags |= SMALL; + if (spec.field_width == -1) { spec.field_width = default_width; spec.flags |= ZEROPAD; } - spec.base = 16; - return number(buf, end, (unsigned long) ptr, spec); + return ptr_to_id(buf, end, ptr, spec); } /*