From patchwork Thu Mar 15 19:47:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10285703 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 83D9360291 for ; Thu, 15 Mar 2018 19:48:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 79B1028B60 for ; Thu, 15 Mar 2018 19:48:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6E4D828BFA; Thu, 15 Mar 2018 19:48:21 +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 9135F28BAF for ; Thu, 15 Mar 2018 19:48:20 +0000 (UTC) Received: (qmail 18251 invoked by uid 550); 15 Mar 2018 19:48:05 -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 18226 invoked from network); 15 Mar 2018 19:48:04 -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 :mime-version:content-transfer-encoding; bh=lj4zQ/k/yu01ARgkyBeTOKuSoVDiR5D4J5KvwtDC7qI=; b=KKX74YJSzyqvgg9URplHd7ngxPe+lRa8UxeXSnBDv7rQEKTru1A8hV6OvlLmlchbGD bZhhaX278PRe1Hq3EI9/itDha0K+dyDFM3e0nqfLEALGPrf0GsngOshKwyIJXTaUuRX9 3XYxZdi3n3clR8R+e5rQVDVpG4QR7I+9yAY48= 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:mime-version:content-transfer-encoding; bh=lj4zQ/k/yu01ARgkyBeTOKuSoVDiR5D4J5KvwtDC7qI=; b=lUVJvn9yfNCKK8JZXG3n/JT3WkmOjDHckuF2IapIFx7lqILpDAxmsKJ6sjtoT352QM 4A69aMdGYwxTu7fZx9MPiFizoQY/EPjoXSKYHfbm4GgcxplL1al1PK1TMro2lrVJm9Ht szOQqs8Ka5HUZ81Kj9OSnt6zm2Fekof8VQEvnBpqBP3nHGIAs5TqvXeRCH4GCIWp6Mws VmyPanjTs2tQXoVsVCZ/367t7TEKNpcrb/fwoEBemHS/IZ9ERPOgCv0OKPzLza5zov7/ rYmLuSqIalxWPGj8JoK4vO/lWMvK9zJRB/LPzfYOxPnClNc/WncflUDYwLSvLRn5LISX yc6w== X-Gm-Message-State: AElRT7GxKPE4Zd78vzgM6+Y8zOZeTU5cgnmSJl0nHv7fuOqEZgwEE9MX rTMEVk4GDWkqVb19A8EaLbjj7A== X-Google-Smtp-Source: AG47ELuiIr+cAwEvRNGq6dM4tp9gYGstmJNZCs3yxH6XyH/axRko5aK188XBv7I4efppE6+3lryk0w== X-Received: by 2002:a17:902:983:: with SMTP id 3-v6mr9572077pln.278.1521143272889; Thu, 15 Mar 2018 12:47:52 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Linus Torvalds , Josh Poimboeuf , Rasmus Villemoes , Randy Dunlap , Miguel Ojeda , Ingo Molnar , David Laight , Ian Abbott , linux-input@vger.kernel.org, linux-btrfs@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: [PATCH v4 1/2] kernel.h: Introduce const_max() for VLA removal Date: Thu, 15 Mar 2018 12:47:45 -0700 Message-Id: <1521143266-31350-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521143266-31350-1-git-send-email-keescook@chromium.org> References: <1521143266-31350-1-git-send-email-keescook@chromium.org> MIME-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP In the effort to remove all VLAs from the kernel[1], it is desirable to build with -Wvla. However, this warning is overly pessimistic, in that it is only happy with stack array sizes that are declared as constant expressions, and not constant values. One case of this is the evaluation of the max() macro which, due to its construction, ends up converting constant expression arguments into a constant value result. Attempts to adjust the behavior of max() ran afoul of version-dependent compiler behavior[2]. To work around this and still gain -Wvla coverage, this patch introduces a new macro, const_max(), for use in these cases of stack array size declaration, where the constant expressions are retained. Since this means losing the double-evaluation protections of the max() macro, this macro is designed to explicitly fail if used on non-constant arguments. Older compilers will fail with the unhelpful message: error: first argument to ‘__builtin_choose_expr’ not a constant Newer compilers will fail with a hopefully more helpful message: error: call to ‘__error_not_const_arg’ declared with attribute error: const_max() used with non-compile-time constant arg To gain the ability to compare differing types, the arguments are explicitly cast to size_t. Without this, some compiler versions will fail when comparing different enum types or similar constant expression cases. With the casting, it's possible to do things like: int foo[const_max(6, sizeof(something))]; [1] https://lkml.org/lkml/2018/3/7/621 [2] https://lkml.org/lkml/2018/3/10/170 Signed-off-by: Kees Cook --- include/linux/kernel.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 3fd291503576..012f588b5a25 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -820,6 +820,25 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } x, y) /** + * const_max - return maximum of two positive compile-time constant values + * @x: first compile-time constant value + * @y: second compile-time constant value + * + * This has no type checking nor multi-evaluation defenses, and must + * only ever be used with positive compile-time constant values (for + * example when calculating a stack array size). + */ +size_t __error_not_const_arg(void) \ +__compiletime_error("const_max() used with non-compile-time constant arg"); +#define const_max(x, y) \ + __builtin_choose_expr(__builtin_constant_p(x) && \ + __builtin_constant_p(y), \ + (size_t)(x) > (size_t)(y) ? \ + (size_t)(x) : \ + (size_t)(y), \ + __error_not_const_arg()) + +/** * min3 - return minimum of three values * @x: first value * @y: second value