From patchwork Thu Mar 8 03:30:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10265965 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 65F94602C8 for ; Thu, 8 Mar 2018 03:32:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 58E87296B7 for ; Thu, 8 Mar 2018 03:32:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4D5D3296ED; Thu, 8 Mar 2018 03:32:10 +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.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_MED 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 75224296B7 for ; Thu, 8 Mar 2018 03:32:09 +0000 (UTC) Received: (qmail 19599 invoked by uid 550); 8 Mar 2018 03:31:47 -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 18219 invoked from network); 8 Mar 2018 03:31:43 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=RNDM7GZdsDPxYnFcKPqvjTjxnD8widEXNa1zIpYjArk=; b=eCIUkMuBYmv0Iq/3nbZSA0iY0NhkXvC1L/VIW9TvgB1Mp8rTAN7Fwvni+2HBljOOwC mXaM+nRYx5zMdGVPXgEm3Nqsts5u80BLloHcdWit91mfpKcq+dbB5/jny6Z+1uvPJv11 N4ZRVNOppGQh40/678XwK6xIFMf5LczlZ1Cz0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=RNDM7GZdsDPxYnFcKPqvjTjxnD8widEXNa1zIpYjArk=; b=g2eT0/Yg31Y+RYGBUG3YQk8NmEsXB+rERl2vWTX+BuoHVMTDCc19NwuVKNDZjTIFNl LtzItnPAPaZQYUtbM5RcPRMN+RNWRrK8UtoMxAXgT4EO0Or7Mi9QiPjw9l3NmyhOzhUe zIs9rHqVbCi1Vl/e4IZ792a05UIpIliNSla5GJJk4QKgBu3LALOTbmWDQ9dEQlXn+uWs 8Sidzl19srGC/3LrxK0lO0iAS5fAD1ess/q+gTssfMr6Bpf6Hpoj8kuEC8i5u7GSLwth zVt4YZ2xl0fopcVNUzRW+5tZcQY10RmShfcJwqofQ3hopvWIbMeWgWo+n7lGeOfMLv0c OuaQ== X-Gm-Message-State: APf1xPCnSaKgycGnX3BgWSIPeny57iIHy700QjjUayzR7pLf+2k5JabL kenz+bS0w4Oh42Bot5Q0cMJHTg== X-Google-Smtp-Source: AG47ELtWM14soWSmsG2eAR2K5Ck0zGJd3swSN/FVtXvEwYmEoay2Xu2WWKsvIF4fgpOxY9j8wEnwMA== X-Received: by 10.98.18.143 with SMTP id 15mr25129033pfs.104.1520479891926; Wed, 07 Mar 2018 19:31:31 -0800 (PST) From: Kees Cook To: Andrew Morton Cc: Kees Cook , linux-kernel@vger.kernel.org, corbet@lwn.net, gustavo@embeddedor.com, rostedt@goodmis.org, Chris Mason , Josef Bacik , David Sterba , "David S. Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Masahiro Yamada , Borislav Petkov , Josh Poimboeuf , Randy Dunlap , Ian Abbott , "Tobin C. Harding" , Sergey Senozhatsky , Petr Mladek , Andy Shevchenko , Pantelis Antoniou , linux-btrfs@vger.kernel.org, netdev@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH v2 1/3] vsprintf: Remove accidental VLA usage Date: Wed, 7 Mar 2018 19:30:45 -0800 Message-Id: <1520479847-39174-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520479847-39174-1-git-send-email-keescook@chromium.org> References: <1520479847-39174-1-git-send-email-keescook@chromium.org> X-Virus-Scanned: ClamAV using ClamSMTP In the quest to remove all stack VLAs from the kernel[1], this introduces a new "simple max" macro, and changes the "sym" array size calculation to use it. The value is actually a fixed size, but since the max() macro uses some extensive tricks for safety, it ends up looking like a variable size to the compiler. [1] https://lkml.org/lkml/2018/3/7/621 Signed-off-by: Kees Cook --- include/linux/kernel.h | 11 +++++++++++ lib/vsprintf.c | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 3fd291503576..1da554e9997f 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -820,6 +820,17 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } x, y) /** + * SIMPLE_MAX - return maximum of two values without any type checking + * @x: first value + * @y: second value + * + * This should only be used in stack array sizes, since the type-checking + * from max() confuses the compiler into thinking a VLA is being used. + */ +#define SIMPLE_MAX(x, y) ((size_t)(x) > (size_t)(y) ? (size_t)(x) \ + : (size_t)(y)) + +/** * min3 - return minimum of three values * @x: first value * @y: second value diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d7a708f82559..50cce36e1cdc 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -744,8 +744,8 @@ char *resource_string(char *buf, char *end, struct resource *res, #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") - char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, - 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; + char sym[SIMPLE_MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, + 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; char *p = sym, *pend = sym + sizeof(sym); int decode = (fmt[0] == 'R') ? 1 : 0;