From patchwork Fri Mar 16 04:25:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10286383 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 7135F601BE for ; Fri, 16 Mar 2018 04:26:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6ABBB28D24 for ; Fri, 16 Mar 2018 04:26:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F5BC28D34; Fri, 16 Mar 2018 04:26:37 +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 8910728D24 for ; Fri, 16 Mar 2018 04:26:36 +0000 (UTC) Received: (qmail 13676 invoked by uid 550); 16 Mar 2018 04:26:20 -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 13613 invoked from network); 16 Mar 2018 04:26:18 -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=eNbhngV0sTnspfoX+8GgHF02vVphrEUurDkfyZCcFFU=; b=JQDZayrv1B0bFOrineuoV3C69YHZtKa1BTIzjBS36C75DqlC8WROOFkZFKMSBsUNcw nYQDP4XG1nEkr64TUt4gndcSBEloCW0en92UPnaaXn6cH0QyJE4amtBsccuwoiNnQe8/ Jca1icVgiWsn8C6nlF9toPPZ2duAuVvhb1Oe0= 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=eNbhngV0sTnspfoX+8GgHF02vVphrEUurDkfyZCcFFU=; b=WsK2EGknlYAPhABbj4YIgjDGjoMHvvS+adZmoTS60fuDbX8/MjANeXyxPuOzuXcN+N 7aRc3pUeb85yBgGoXDsEJrcx+5Hp0Q47fshDTqMUSfxT4sX/J3YpX9M5+Oe7xVOwLa6s cRWtgjnJOdmx5xnovb2m2CN16PErQWlaFy+6h9lynP2g1NWs/rEj/RCEPaLyRcZSprmV umfRCtG89H0yV2z1edg/sSZxq5XF4CvtycsKraZzi2TKTisGtfFhvNvPavB3eejnKeJm 3dzeESAzsaVLNJwx+b6rjXbD62ftLJ0uwyywjx/e+Xsjf91VjZV1/mUw6dmZ8pVxql9e Qz/g== X-Gm-Message-State: AElRT7FdpVVVOah8DBeJZpHUopIf4/tU7q4f2e9ZST+vj7IWJjaCscLJ L5VBSgmHlXkSM7q3T5AtJzWQMg== X-Google-Smtp-Source: AG47ELvfRtErUV0+GPtQJOy+8qquv5l2SeENkFF214yWZtE8hj3fALz5r57H4ASXqbiJq/hL7LaGhg== X-Received: by 10.99.120.138 with SMTP id t132mr342233pgc.280.1521174365952; Thu, 15 Mar 2018 21:26:05 -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 v5 1/2] kernel.h: Introduce const_max_t() for VLA removal Date: Thu, 15 Mar 2018 21:25:58 -0700 Message-Id: <1521174359-46392-2-git-send-email-keescook@chromium.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1521174359-46392-1-git-send-email-keescook@chromium.org> References: <1521174359-46392-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_t(), 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_non_const_arg’ declared with attribute error: const_max_t() used with non-constant expression To gain the ability to compare differing types, the desired type must be explicitly declared, as with the existing max_t() macro. This is needed when comparing different enum types and to allow things like: int foo[const_max_t(size_t, 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..e14531781568 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_t - return maximum of two compile-time constant expressions + * @type: type used for evaluation + * @x: first compile-time constant expression + * @y: second compile-time constant expression + * + * This has no multi-evaluation defenses, and must only ever be used with + * compile-time constant expressions (for example when calculating a stack + * array size). + */ +size_t __error_non_const_arg(void) \ +__compiletime_error("const_max_t() used with non-constant expression"); +#define const_max_t(type, x, y) \ + __builtin_choose_expr(__builtin_constant_p(x) && \ + __builtin_constant_p(y), \ + (type)(x) > (type)(y) ? \ + (type)(x) : (type)(y), \ + __error_non_const_arg()) + +/** * min3 - return minimum of three values * @x: first value * @y: second value