Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp219406rwb; Wed, 21 Sep 2022 21:00:09 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4Td5iButr8pN8QuwTGFNIBiVj33a/xQDN6hwbe9boZiFOTIwofTRZfrbY1/9afs5vGHW/U X-Received: by 2002:a17:902:bf08:b0:178:90fb:8cda with SMTP id bi8-20020a170902bf0800b0017890fb8cdamr1326952plb.9.1663819209063; Wed, 21 Sep 2022 21:00:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663819209; cv=none; d=google.com; s=arc-20160816; b=tYKy7ioQ3j/Psw6WzT7rSwfy7VdoKxRRwyHMPE9LDHSWXwonUGXx50cQ1IWXRzpy/G /LrXM9/1LTsmxeEUZXxJYrq3F5E74wTLJKikU19PBIWM1GUJC45qOZ73CECpO4PKPKsK cJPCfvQOHKg0Vcel2aqUZQbAqvK2i8FRBdjZLo8ZehUr7z2awJ+LtW0LA18DTBMF7fXb 9hvFqpxoCpC7JiruaquXswMs4olvhP3jHyR4v2yPx2168Fcjg3pLVbfym9XjvphczDF4 u5TevCX4GZs9EsLMu1Xh0tRhbZv4NGjBAGAL+vuIzjR3fhZhCMDVsD59OXLVlJfbo6SR e5AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=qyywbofJe0eyuZ1xhJp6fm2W5D3XouXfRrTFYq94Fp4=; b=yUzYwnGYqSEu6+tgx/ZpFbzqwlMjA//61fa0QdFqNDKNw7OEchbdYIaH23Hx+LAANW rw85jkqFxPkAfUQu4QGtsZTT4rL5T2bbyK8t9bmLuFtiowTcGIcjZ8eDAK6Jm9Zm0u3o k/fvMEe/RpWmSLYOUSePadCn2te+gh5Mm4NkQfId5wslzsrOKSQOf1hoFz86AhE/IsgT nDkD0p4XSbcsWXekiL6IlU+r482AwsgEee+cBzxGdPB83L5qN92q9C/QT8aMR5maTq2y bsNZPAgmaYw5sTzSYJWx5/q5en6vGDpkPSqi+jqkSRkTP/dQi6ur80nFBMNLUJ5kwHHn 9bgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Pw/f8KQs"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r2-20020aa79882000000b0054289be8a17si4418395pfl.328.2022.09.21.20.59.17; Wed, 21 Sep 2022 21:00:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="Pw/f8KQs"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229771AbiIVDq4 (ORCPT + 99 others); Wed, 21 Sep 2022 23:46:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229519AbiIVDqv (ORCPT ); Wed, 21 Sep 2022 23:46:51 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B5AFAB07F for ; Wed, 21 Sep 2022 20:46:50 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id v4so7882416pgi.10 for ; Wed, 21 Sep 2022 20:46:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=qyywbofJe0eyuZ1xhJp6fm2W5D3XouXfRrTFYq94Fp4=; b=Pw/f8KQsfXL0i7dI2WsLILA87LJmHWktVmMcNk02AdJw7OYwyfgFBp6OV5I7L2z+jK VT+jdCOhM59bxVoafw7uz7OyCyVKqc0ozDJVlZBiaOzy9ANYBjVgH5FdxsycfdmNd5Kb FCweeMeThswnUmgdalb60X24AzKQnx8Ym9Exw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=qyywbofJe0eyuZ1xhJp6fm2W5D3XouXfRrTFYq94Fp4=; b=uZPlM/JJz8UsyLhacEgBlN/eIZ9cwSkZguNwwAogsLBZqqRoQy58rtzPXWNG8inR97 sLmowWsB5Tpwdro6Q6ORRahXf2OniS1MU6pTUtNRrGpOJi6mgXply+aCc1ymDjBF5AZq ++yLKkE0HXqkdiqI8d4GQ2oqWEqk2idnAxX4idEmfdALgAgWuLXBpwO/ippzhQpy2och k5fejbp8Of4S8Ba1F5iVbfYdye+cXZ5l6PZ/EjrWT56qVATNvuNoJXsjfloWOWiLvTFL 83y9DyIXps0CNOvvI0zJqI6L5yfBjhAldCmgZXA42K7dWUeNI2RQliMQR6XJ9mhqR6w+ PmUg== X-Gm-Message-State: ACrzQf0XJ1TQe8Aa0TAK1mIn84Qbbye3R4/gfBsAO0MnnimQkUvP+AYA xLxusM5zmXQGSLpFDPyD3PqFzg== X-Received: by 2002:a63:ba05:0:b0:43b:e89b:da2b with SMTP id k5-20020a63ba05000000b0043be89bda2bmr1299435pgf.209.1663818409568; Wed, 21 Sep 2022 20:46:49 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id u7-20020a17090341c700b00174fa8cbf31sm2837874ple.303.2022.09.21.20.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 20:46:48 -0700 (PDT) Date: Wed, 21 Sep 2022 20:46:47 -0700 From: Kees Cook To: Siddhesh Poyarekar Cc: linux-hardening@vger.kernel.org, Nathan Chancellor , Nick Desaulniers , Arnd Bergmann , Juergen Gross , Boris Ostrovsky , Tom Rix , Miguel Ojeda , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH 2/4] fortify: Explicitly check bounds are compile-time constants Message-ID: <202209212034.16D9025882@keescook> References: <20220920192202.190793-1-keescook@chromium.org> <20220920192202.190793-3-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 21, 2022 at 07:48:44AM -0400, Siddhesh Poyarekar wrote: > On 2022-09-20 15:22, Kees Cook wrote: > > In preparation for replacing __builtin_object_size() with > > __builtin_dynamic_object_size(), all the compile-time size checks need > > to check that the bounds variables are, in fact, known at compile-time. > > Enforce what was guaranteed with __bos(). In other words, since all uses > > of __bos() were constant expressions, it was not required to test for > > this. When these change to __bdos(), they _may_ be constant expressions, > > and the checks are only valid when the prior condition holds. This > > results in no binary differences. > > > > Signed-off-by: Kees Cook > > --- > > include/linux/fortify-string.h | 50 +++++++++++++++++++++------------- > > 1 file changed, 31 insertions(+), 19 deletions(-) > > > > diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h > > index ff879efe94ed..71c0a432c638 100644 > > --- a/include/linux/fortify-string.h > > +++ b/include/linux/fortify-string.h > > @@ -80,6 +80,12 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size) > > #define POS __pass_object_size(1) > > #define POS0 __pass_object_size(0) > > +#define __compiletime_lessthan(bounds, length) ( \ > > + __builtin_constant_p(length) && \ > > + __builtin_constant_p(bounds) && \ > > + bounds < length \ > > +) > > So with the gcc ranger, the compiler has lately been quite successful at > computing a constant `bounds < length` even though bounds and length are not > constant. So perhaps this: > > #define __compiletime_lessthan (bounds, length) ( \ > __builtin_constant (bounds < length) && \ > bounds < length \ > ) > > might succeed in a few more cases. Oh, interesting! That's very cool -- I never considered tossing a full expression into __bcp. Yeah, that seems to work just fine: https://godbolt.org/z/xrchErEx1 -- Kees Cook