Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2153303pxa; Mon, 17 Aug 2020 02:13:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzv69sGbObzFioEP1jF0elhfhLAgvgztDv6ckKB2LElSxKnI4t0vZo5ifpv7tLGmZpljCas X-Received: by 2002:a17:907:7090:: with SMTP id yj16mr13782175ejb.73.1597655637944; Mon, 17 Aug 2020 02:13:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597655637; cv=none; d=google.com; s=arc-20160816; b=v2GPsSwoT9QLulrRWQsqBCAPf5UnnCluwxNfFVhNfT3jugYh+0iiPu60md52z+muJI hcB3wu2IQhMSim8IUQUH9BW7H/0XluDp+Jd/rsmCoyjef9bqU9nlpWpJ+qytbaF5MfBj WYfkiwfX7T1Y9KTzGuVMLP12Xdr1x2tcLw2xjTgMzaFI0q0/LyrxJEjQqioOteTNDgK7 /YZyfdV5PphHc2cUKFla/R9Lwy/9Zj4uFlnHhi34FH20ClleBELrIhgmQceba1Roary1 DB/3cvBOqb/oSGyyPFKDUYP09cVH5BosY3PtYDEjlSccJXjGxYPAmdWMtCL2W2pCvXJH H5+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:mail-followup-to :reply-to:message-id:subject:cc:to:from:date; bh=GARyJq03DiBqMoGFc4uCHjxCPpG+2Kh3D3vIhfTEsmM=; b=MrOcnNN/i+hn0XBUeUq6WBwIbjh124ebW71hfo+le/KonB726tkPaiw4mSsrfkViR1 TMlEewQU2SI3E9TZvu/2hGqpZTNJlc4IwDA7yHDGX9WC3rJG34hnfiJ0VeNkA773AJ8+ bxAPc5llawYPng28WQXf+Ef4SHA4nb+w2GIg+yBcWlAtgy7Tg2feYWPyGSDalbT8NTeC kNwBE8aev9GP6hZcCd1OF8N+8xuI/pUQxsL8z25JnVcSo3GmOXM73iedrLHmSlM6lJXF brD439AGeDFcttWvPXofsxoo5bamHE2CKoWPd61q+ZbelvZPfImHfNp1mUZ9dqU3huVB ikBQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bd21si10925198edb.117.2020.08.17.02.13.35; Mon, 17 Aug 2020 02:13:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726788AbgHQJKK (ORCPT + 99 others); Mon, 17 Aug 2020 05:10:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:49688 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728352AbgHQJKA (ORCPT ); Mon, 17 Aug 2020 05:10:00 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C3C82B67A; Mon, 17 Aug 2020 09:10:23 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 82A9BDA6EF; Mon, 17 Aug 2020 11:08:54 +0200 (CEST) Date: Mon, 17 Aug 2020 11:08:54 +0200 From: David Sterba To: Kees Cook Cc: Rasmus Villemoes , "Gustavo A. R. Silva" , Jason Gunthorpe , Leon Romanovsky , Matthew Wilcox , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com Subject: Re: [PATCH v2] overflow: Add __must_check attribute to check_*() helpers Message-ID: <20200817090854.GA2026@twin.jikos.cz> Reply-To: dsterba@suse.cz Mail-Followup-To: dsterba@suse.cz, Kees Cook , Rasmus Villemoes , "Gustavo A. R. Silva" , Jason Gunthorpe , Leon Romanovsky , Matthew Wilcox , linux-kernel@vger.kernel.org, kernel-hardening@lists.openwall.com References: <202008151007.EF679DF@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202008151007.EF679DF@keescook> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Aug 15, 2020 at 10:09:24AM -0700, Kees Cook wrote: > Since the destination variable of the check_*_overflow() helpers will > contain a wrapped value on failure, it would be best to make sure callers > really did check the return result of the helper. Adjust the macros to use > a bool-wrapping static inline that is marked with __must_check. This means > the macros can continue to have their type-agnostic behavior while gaining > the function attribute (that cannot be applied directly to macros). > > Suggested-by: Rasmus Villemoes > Signed-off-by: Kees Cook > --- > v2: > - de-generalized __must_check_overflow() from being named "bool" (willy) > - fix comment typos (rasmus) > v1: https://lore.kernel.org/lkml/202008121450.405E4A3@keescook > --- > include/linux/overflow.h | 39 ++++++++++++++++++++++++--------------- > 1 file changed, 24 insertions(+), 15 deletions(-) > > diff --git a/include/linux/overflow.h b/include/linux/overflow.h > index 93fcef105061..f1c4e7b56bd9 100644 > --- a/include/linux/overflow.h > +++ b/include/linux/overflow.h > @@ -43,6 +43,16 @@ > #define is_non_negative(a) ((a) > 0 || (a) == 0) > #define is_negative(a) (!(is_non_negative(a))) > > +/* > + * Allows for effectively applying __must_check to a macro so we can have > + * both the type-agnostic benefits of the macros while also being able to > + * enforce that the return value is, in fact, checked. > + */ > +static inline bool __must_check __must_check_overflow(bool overflow) > +{ > + return unlikely(overflow); How does the 'unlikely' hint propagate through return? It is in a static inline so compiler has complete information in order to use it, but I'm curious if it actually does. > +} > + > #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW > /* > * For simplicity and code hygiene, the fallback code below insists on > @@ -52,32 +62,32 @@ > * alias for __builtin_add_overflow, but add type checks similar to > * below. > */ > -#define check_add_overflow(a, b, d) ({ \ > +#define check_add_overflow(a, b, d) __must_check_overflow(({ \ > typeof(a) __a = (a); \ > typeof(b) __b = (b); \ > typeof(d) __d = (d); \ > (void) (&__a == &__b); \ > (void) (&__a == __d); \ > __builtin_add_overflow(__a, __b, __d); \ > -}) > +})) In case the hint gets dropped, the fix would probably be #define check_add_overflow(a, b, d) unlikely(__must_check_overflow(({ \ typeof(a) __a = (a); \ typeof(b) __b = (b); \ typeof(d) __d = (d); \ (void) (&__a == &__b); \ (void) (&__a == __d); \ __builtin_add_overflow(__a, __b, __d); \ })))