From patchwork Wed Nov 29 23:38:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobin Harding X-Patchwork-Id: 10083899 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 CFC126020B for ; Wed, 29 Nov 2017 23:39:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C13D229D55 for ; Wed, 29 Nov 2017 23:39:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B5F4F29D58; Wed, 29 Nov 2017 23:39:23 +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 B454D29D55 for ; Wed, 29 Nov 2017 23:39:21 +0000 (UTC) Received: (qmail 4040 invoked by uid 550); 29 Nov 2017 23:39:09 -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 3941 invoked from network); 29 Nov 2017 23:39:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tobin.cc; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ejvtU4+wX9zV7uSdk S95QGwdFtdzhNLb7A6YBpHNEmk=; b=RTadynS6d+4fGfUTn4eZ0Xqb/X06Z0jFo ToPQo27FdIuk0v6O4XMOalbmYZ0ZhP8nMxRi7UIg1yJ7OqEopCmTh9xjgN8TS6ji KqVwAmir4sGUn1CZOKdLMoCAOYzYMiZ6WbU5bAcZRkeZ2Eh3t9PVAhPLipj7hj2m vx4F4z5w9mb5q+lSjSvLbhXXRbqczCG+y2EkTcvES20NFJmcy17PrQCXVihjJFDk 4aWYjPsFjN6jmj7yHJh+6E2eGygCBktP3xqsMgajaGlywlf/g6An/EJ131ISwyu+ RWZtbpSUARTBmRd30SH7RXds0kRJzDz7lA3iz6XTAYRZ2DN1vp72g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=ejvtU4+wX9zV7uSdkS95QGwdFtdzhNLb7A6YBpHNEmk=; b=NK27xd13 B4HdzEQIk4pxlSwYOK9g2RQ5DmuHVxeOnwwfb4QLLlh3icnYByPkevu7hN7Pk/DU 7mney16yiKs1+Tcu8mDlQLvQShJgo3jxv4v9d1zLDqZX/J5jdmotli3r02TK/AdE c7s5j05cISP7Ihn7dNhw+IwMYObYF/vsmdMg4q210lT+K/0pI9Bgmdc7WfiyHd+P OM7HTvaYwCfcwiiSfRykrqAEj8cM0AHeAnHa8eolez4EH4DgvfJUf9R0n3KIQ9QA BNf/ue5h4BbRmU/75P8kaRvCgh6ja28q4Ou07nsjOcUxzYzj0qaG+r4/OXIA2pET 9RPZBzSscULGKw== X-ME-Sender: From: "Tobin C. Harding" To: Linus Torvalds Cc: "Tobin C. Harding" , David Laight , Kees Cook , "Jason A. Donenfeld" , Theodore Ts'o , 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 , =?UTF-8?q?Radim=20Krcm=C3=A1r?= , David Miller , Stephen Rothwell , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , kernel-hardening@lists.openwall.com, Linux Kernel Mailing List , Network Development Date: Thu, 30 Nov 2017 10:38:38 +1100 Message-Id: <1511998718-4158-3-git-send-email-me@tobin.cc> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511998718-4158-1-git-send-email-me@tobin.cc> References: <1511998718-4158-1-git-send-email-me@tobin.cc> Subject: [kernel-hardening] [PATCH 2/2] printk: add specifier %pz, for zeroed address X-Virus-Scanned: ClamAV using ClamSMTP Currently %pK [at times] zeros addresses. It would be nice to remove %pK entirely. Printing zero addresses is useful if we want to sanitize an address but there may be userland tools that currently rely on the address format (i.e the correct width). Add printk specifier %pz. Signed-off-by: Tobin C. Harding --- Documentation/printk-formats.txt | 11 +++++++++++ lib/vsprintf.c | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt index aa0a776c817a..f88b06485378 100644 --- a/Documentation/printk-formats.txt +++ b/Documentation/printk-formats.txt @@ -122,6 +122,17 @@ uniquely grep'able. If, in the future, we need to modify the way the Kernel handles printing pointers it will be nice to be able to find the call sites. +Zeroed Addresses +================ + +:: + + %pz 00000000 or 0000000000000000 + +For printing zeroed addresses. This is useful if you are want to sanitize +an address but there may be userland tools that depend on the correct width +of the address for parsing. + Struct Resources ================ diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 81e9ce8f52f9..ebf911618858 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1727,6 +1727,19 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) return number(buf, end, hashval, spec); } +static noinline_for_stack +char *zero_string(char *buf, char *end, struct printf_spec spec) +{ + spec.base = 16; + spec.flags |= SMALL; + if (spec.field_width == -1) { + spec.field_width = 2 * sizeof(void *); + spec.flags |= ZEROPAD; + } + + return number(buf, end, 0, spec); +} + /* * Show a '%p' thing. A kernel extension is that the '%p' is followed * by an extra set of alphanumeric characters that are extended format @@ -1833,6 +1846,9 @@ static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) * C full compatible string * * - 'x' For printing the address. Equivalent to "%lx". + * - 'z' For printing zeroed addresses. This is useful if you are want to + * sanitize an address but there may be userland tools that depend on the + * correct width of the address for parsing. * * ** Please update also Documentation/printk-formats.txt when making changes ** * @@ -1960,6 +1976,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr, } case 'x': return pointer_string(buf, end, ptr, spec); + case 'z': + return zero_string(buf, end, spec); } /* default is to _not_ leak addresses, hash before printing */