From patchwork Mon Mar 12 22:55:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 10277631 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 A942D602BD for ; Mon, 12 Mar 2018 22:55:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C2E328DB0 for ; Mon, 12 Mar 2018 22:55:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9071D28DD5; Mon, 12 Mar 2018 22:55: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.2 required=2.0 tests=BAYES_00, 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 9032A28DB0 for ; Mon, 12 Mar 2018 22:55:42 +0000 (UTC) Received: (qmail 17604 invoked by uid 550); 12 Mar 2018 22:55:40 -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 17573 invoked from network); 12 Mar 2018 22:55:39 -0000 Date: Mon, 12 Mar 2018 15:55:24 -0700 From: Andrew Morton To: Kees Cook Cc: Linus Torvalds , Linux Kernel Mailing List , Josh Poimboeuf , Rasmus Villemoes , "Gustavo A. R. Silva" , "Tobin C. Harding" , Steven Rostedt , Jonathan Corbet , Chris Mason , Josef Bacik , David Sterba , "David S. Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Masahiro Yamada , Borislav Petkov , Randy Dunlap , Ian Abbott , Sergey Senozhatsky , Petr Mladek , Andy Shevchenko , Pantelis Antoniou , Linux Btrfs , Network Development , Kernel Hardening Subject: Re: [PATCH v3] kernel.h: Skip single-eval logic on literals in min()/max() Message-Id: <20180312155524.b421f07d7f08f24c57bd1887@linux-foundation.org> In-Reply-To: References: <20180309200536.GA5670@beast> <20180309160719.154a3158e2d8ee56e43a918f@linux-foundation.org> <20180309163241.a421e216999bd0b1f43a64c2@linux-foundation.org> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 X-Virus-Scanned: ClamAV using ClamSMTP On Fri, 9 Mar 2018 17:30:15 -0800 Kees Cook wrote: > > It's one reason why I wondered if simplifying the expression to have > > just that single __builtin_constant_p() might not end up working.. > > Yeah, it seems like it doesn't bail out as "false" for complex > expressions given to __builtin_constant_p(). > > If no magic solution, then which of these? > > - go back to my original "multi-eval max only for constants" macro (meh) > - add gcc version checks around this and similarly for -Wvla in the future (eww) > - raise gcc version (yikes) Replacing the __builtin_choose_expr() with ?: works of course. What will be the runtime effects? I tried replacing __builtin_choose_expr(__builtin_constant_p(x) && __builtin_constant_p(y), with __builtin_choose_expr(__builtin_constant_p(x + y), but no success. I'm not sure what else to try to trick gcc into working. --- a/include/linux/kernel.h~kernelh-skip-single-eval-logic-on-literals-in-min-max-v3-fix +++ a/include/linux/kernel.h @@ -804,13 +804,10 @@ static inline void ftrace_dump(enum ftra * accidental VLA. */ #define __min(t1, t2, x, y) \ - __builtin_choose_expr(__builtin_constant_p(x) && \ - __builtin_constant_p(y), \ - (t1)(x) < (t2)(y) ? (t1)(x) : (t2)(y), \ - __single_eval_min(t1, t2, \ - __UNIQUE_ID(min1_), \ - __UNIQUE_ID(min2_), \ - x, y)) + ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? \ + ((t1)(x) < (t2)(y) ? (t1)(x) : (t2)(y)) : \ + (__single_eval_min(t1, t2, __UNIQUE_ID(min1_), \ + __UNIQUE_ID(min2_), x, y))) /** * min - return minimum of two values of the same or compatible types @@ -826,13 +823,11 @@ static inline void ftrace_dump(enum ftra max1 > max2 ? max1 : max2; }) #define __max(t1, t2, x, y) \ - __builtin_choose_expr(__builtin_constant_p(x) && \ - __builtin_constant_p(y), \ - (t1)(x) > (t2)(y) ? (t1)(x) : (t2)(y), \ - __single_eval_max(t1, t2, \ - __UNIQUE_ID(max1_), \ - __UNIQUE_ID(max2_), \ - x, y)) + ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? \ + ((t1)(x) > (t2)(y) ? (t1)(x) : (t2)(y)) : \ + (__single_eval_max(t1, t2, __UNIQUE_ID(max1_), \ + __UNIQUE_ID(max2_), x, y))) + /** * max - return maximum of two values of the same or compatible types * @x: first value