Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp4918551rwb; Tue, 8 Aug 2023 16:39:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHr8P6kv6njDSeEJwytaU5tKoRUfWbEVe8SkGTWPY2JAvZPz6dAnDoHggQBuTGhvUG+ZoqS X-Received: by 2002:a05:6a20:969b:b0:134:e14c:851b with SMTP id hp27-20020a056a20969b00b00134e14c851bmr955024pzc.23.1691537973105; Tue, 08 Aug 2023 16:39:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691537973; cv=none; d=google.com; s=arc-20160816; b=FUA4QnoWbOf7LSdKCt5cgsE4lTpLQDftCUvDVCOXPnPUIS/AxbJeVdboi9iCfn4zGQ viieg8LigcVifZJMnfkW/jfoWoYP371UhFjFQLwEabFVRDwIQpyXu/ufwgU8+RdON1d/ knLejfqxHDJm7fdLDbtdxVHNJjwu8vn5wV/WB2kn9VQ3/XAl2Cgvg550FujsReBg3Fo4 Jz0tUufNFTQRBcKKHCkum+I34wCU3PR1QSbjT3lPTTW6fw9/Djn2Kjj55nleL83zCbyX z0ETWbMJlemEQ5lyAr33x2fEPbR4fnHEpF0s8TVpFRbn5USJtJwHb+zLfyYGAwjFflVF g8vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature; bh=lWc4w0X+KkjRmN5DgqpHONdI5ESqhYxfcg8cG60uZv8=; fh=XKLIStInM4FhcGNXZBO1ToDEdzJeO2NG6tkivRIaEVM=; b=EHfaE6wyUd/u6AT6rzBgBd8m85lpbGFbhVrqDjOMsj4QxtFknRXbn9Ihg3b0fCLls7 GOcNVLdqD7cVcWAGETN2Gv/+lq7Uju9tQdnck6fY2VNGHLZ//utUC47+heIqN9D/xqVA S2UvLhZGTr7pSORhn1jWMSt1nQkO4Nhh70qP0I/YHQxhSM3RizSHkctRIewMCWvzQ06T cotgnV9w58GbcvN7xu/DfWlmaVywXDqcaaIQCedaD6UCy+25f3aAE5Y5T5DIKWiCHifh nlNWDFJ6EfCuOyGsA7ZisVU48WxAMaJ5hmb5lYe/AlbpealmgNv/ljQDHdQB6hYRk2fD RegA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HV74zwmQ; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z12-20020a1709027e8c00b001b9d800b487si7452357pla.87.2023.08.08.16.39.21; Tue, 08 Aug 2023 16:39:33 -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=@kernel.org header.s=k20201202 header.b=HV74zwmQ; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230512AbjHHXAk (ORCPT + 99 others); Tue, 8 Aug 2023 19:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230027AbjHHXAj (ORCPT ); Tue, 8 Aug 2023 19:00:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CD7E19A1; Tue, 8 Aug 2023 16:00:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E106462DF0; Tue, 8 Aug 2023 23:00:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 40E44C433C9; Tue, 8 Aug 2023 23:00:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1691535637; bh=1+fp21MqF6KntDdFbgZ1oMwBrAWE3yROyeT/Yf4gLaA=; h=Date:From:To:CC:Subject:In-Reply-To:References:From; b=HV74zwmQ9KMzQVRuCI4qOWFBpxm1whPstAE6VTcXGI27a836eve4BHUhDQ6ZFrC0a BQ4Mkjy1mkMXU2VdMCDfg7ic7NBPPKveWSt4+lVQuUQKm2MoLKFcUA8DB5oH33kLfM L/DcI98+DBMx3Rc84SFCnwXVlbwP64W1fvJM75p8D41letvCfh9GRvKmNGXOfXq60G F3uOq1RfFPgi0Vw0EO+rfzD6MQTLqEYpaPz05J9cLCA/pU222VzXpYyCc60BKB6vUq VlZVnAzr7GmDDSZlpeNu1h+pHGpjZRNhYCeOE51giyiD8OmL2jIpFVLOQUBjwQyo2n 2GqkwP7vVLt1w== Date: Tue, 08 Aug 2023 16:00:36 -0700 From: Kees Cook To: "Gustavo A. R. Silva" , "GONG, Ruiqi" CC: Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , Roopa Prabhu , Nikolay Aleksandrov , Kees Cook , netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Wang Weiyang , Xiu Jianfeng , gongruiqi1@huawei.com Subject: Re: [PATCH v2] netfilter: ebtables: fix fortify warnings User-Agent: K-9 Mail for Android In-Reply-To: References: <20230808133038.771316-1-gongruiqi@huaweicloud.com> Message-ID: <45DEF7A6-093D-4517-8CD8-D86D1671BE48@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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_BLOCKED,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 August 8, 2023 9:32:50 AM PDT, "Gustavo A=2E R=2E Silva" wrote: >On Tue, Aug 08, 2023 at 09:30:38PM +0800, GONG, Ruiqi wrote: >> From: "GONG, Ruiqi" >>=20 >> When compiling with gcc 13 and CONFIG_FORTIFY_SOURCE=3Dy, the following >> warning appears: >>=20 >> In function =E2=80=98fortify_memcpy_chk=E2=80=99, >> inlined from =E2=80=98size_entry_mwt=E2=80=99 at net/bridge/netfilt= er/ebtables=2Ec:2118:2: >> =2E/include/linux/fortify-string=2Eh:592:25: error: call to =E2=80=98__= read_overflow2_field=E2=80=99 >> declared with attribute warning: detected read beyond size of field (2n= d parameter); >> maybe use struct_group()? [-Werror=3Dattribute-warning] >> 592 | __read_overflow2_field(q_size_field, si= ze); >> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~= ~~~ >>=20 >> The compiler is complaining: >>=20 >> memcpy(&offsets[1], &entry->watchers_offset, >> sizeof(offsets) - sizeof(offsets[0])); >>=20 >> where memcpy reads beyong &entry->watchers_offset to copy >> {watchers,target,next}_offset altogether into offsets[]=2E Silence the >> warning by wrapping these three up via struct_group()=2E >>=20 >> Signed-off-by: GONG, Ruiqi >> --- >>=20 >> v2: fix HDRTEST error by replacing struct_group() with __struct_group()= , >> since it's a uapi header=2E >>=20 >> include/uapi/linux/netfilter_bridge/ebtables=2Eh | 14 ++++++++------ >> net/bridge/netfilter/ebtables=2Ec | 3 +-- >> 2 files changed, 9 insertions(+), 8 deletions(-) >>=20 >> diff --git a/include/uapi/linux/netfilter_bridge/ebtables=2Eh b/include= /uapi/linux/netfilter_bridge/ebtables=2Eh >> index a494cf43a755=2E=2Eb0caad82b693 100644 >> --- a/include/uapi/linux/netfilter_bridge/ebtables=2Eh >> +++ b/include/uapi/linux/netfilter_bridge/ebtables=2Eh >> @@ -182,12 +182,14 @@ struct ebt_entry { >> unsigned char sourcemsk[ETH_ALEN]; >> unsigned char destmac[ETH_ALEN]; >> unsigned char destmsk[ETH_ALEN]; >> - /* sizeof ebt_entry + matches */ >> - unsigned int watchers_offset; >> - /* sizeof ebt_entry + matches + watchers */ >> - unsigned int target_offset; >> - /* sizeof ebt_entry + matches + watchers + target */ >> - unsigned int next_offset; >> + __struct_group(/* no tag */, offsets, /* no attrs */, >> + /* sizeof ebt_entry + matches */ >> + unsigned int watchers_offset; >> + /* sizeof ebt_entry + matches + watchers */ >> + unsigned int target_offset; >> + /* sizeof ebt_entry + matches + watchers + target */ >> + unsigned int next_offset; >> + ); >> unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct eb= t_replace)))); >> }; Actually, looking at what size_entry_mwt() is doing, I think you probably = DO want a tag for this and to use a real structure for the manipulations in= stead of doing array indexing? I dunno=2E This is a weird function! :) -Kees >> =20 >> diff --git a/net/bridge/netfilter/ebtables=2Ec b/net/bridge/netfilter/e= btables=2Ec >> index 757ec46fc45a=2E=2E5ec66b1ebb64 100644 >> --- a/net/bridge/netfilter/ebtables=2Ec >> +++ b/net/bridge/netfilter/ebtables=2Ec >> @@ -2115,8 +2115,7 @@ static int size_entry_mwt(const struct ebt_entry = *entry, const unsigned char *ba >> return ret; >> =20 >> offsets[0] =3D sizeof(struct ebt_entry); /* matches come first */ >> - memcpy(&offsets[1], &entry->watchers_offset, >> - sizeof(offsets) - sizeof(offsets[0])); >> + memcpy(&offsets[1], &entry->offsets, sizeof(offsets) - sizeof(offsets= [0])); > ^^^^^^^^^^^^ >You now can replace this ____________________________________| >with just `sizeof(entry->offsets)` > >With that change you can add my >Reviewed-by: Gustavo A=2E R=2E Silva > >Thank you >-- >Gustavo > >> =20 >> if (state->buf_kern_start) { >> buf_start =3D state->buf_kern_start + state->buf_kern_offset; >> --=20 >> 2=2E41=2E0 >>=20 --=20 Kees Cook