Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5285965rwb; Wed, 9 Aug 2023 01:23:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEv9lHgT1v9LmmyfOBEugrq5nUnR2WqE1mcZZtURrmdAv0jgyk5B5tDrEi+Q7iKxml5B7rw X-Received: by 2002:a05:6402:1495:b0:523:48d7:70b1 with SMTP id e21-20020a056402149500b0052348d770b1mr1791355edv.19.1691569439595; Wed, 09 Aug 2023 01:23:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691569439; cv=none; d=google.com; s=arc-20160816; b=Q9B6jAOPlCw66VyRKCYWOBwHxBo/MgLO4dPhmXXxTJd5zTv3ea4Ao5xWkNtpBGkiLa F4eNzNQ96Pe14ssHE3smF+STeFqeBwCrr54+DKTTey/JvJFsd04vPfu/9OJ/Ntl/Z/iz TSb9gPMJe+v2O6FVOHpATr4F2QE7jYvyu8JD81ZvmCLw8JyKmJyDZlhljVwBJPSbO88i h9UX1KAnwgGvI5gYXzaVp2zKyl/roUYsH7dJ+KOtsMEP0HLVlo/cTPOx7ptCqUDw6dXI v9ZQgKmudoGlqi4GsJWu1Dy3Za02QsnhS4kiu3k7lN00amcNEcmJzqNnYruXqQ/7CqGd pmfA== 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:date:subject:cc:to:from; bh=v2CXWeKoUDK2AVE1Jye9ovIL8+HJZ0Hw8eNteGFF94Y=; fh=O63IryxzGsBPvfSEuBQrY4gQO4Dt5kdLA7nkl36gPo8=; b=wLfcbETnY+r2AyBvb4xBpFUnvy+iJPKmtyGbJh+Xj3d0ztrQ/ukvulkf3VdVONuomS f/DbT7y0joN4Bhee9JxavJGtq0YKxUIYZFXVSBI4YnZQdlo0VbppdhLjBMXCZSGdBd8v lQmYbk9XFdQO/5cIaG6kz30/WSXQeMwVI2YfOv8b5lgS/06sJCKk7M09j0tX87OC62vt HgsKrhUJ+xMKsKpzY6B2fBfre4jeY9mkWSa32JsyT6Cud9IkDpDsnZSf8TdLSNvxTMnd ClVBgPBQcqANunT74GlJZQjOxzmd0joA4uzGAD0Miw+6q3z1XxLv1ehV2b/YUCAFcEvJ s3Kg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z23-20020aa7d417000000b005222ca6f9a0si8436010edq.560.2023.08.09.01.23.34; Wed, 09 Aug 2023 01:23:59 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229992AbjHIHrP (ORCPT + 99 others); Wed, 9 Aug 2023 03:47:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229504AbjHIHrN (ORCPT ); Wed, 9 Aug 2023 03:47:13 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F8A8173A; Wed, 9 Aug 2023 00:47:12 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4RLMZx49tjz4f3jJK; Wed, 9 Aug 2023 15:47:05 +0800 (CST) Received: from vm-fedora-38.huawei.com (unknown [10.67.174.164]) by APP4 (Coremail) with SMTP id gCh0CgBn9qh1RNNkH4knAQ--.26977S2; Wed, 09 Aug 2023 15:47:08 +0800 (CST) From: "GONG, Ruiqi" To: Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , Roopa Prabhu , Nikolay Aleksandrov , Kees Cook , "Gustavo A . R . Silva" Cc: 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: [PATCH v3] netfilter: ebtables: fix fortify warnings in size_entry_mwt() Date: Wed, 9 Aug 2023 15:45:03 +0800 Message-ID: <20230809074503.1323102-1-gongruiqi@huaweicloud.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgBn9qh1RNNkH4knAQ--.26977S2 X-Coremail-Antispam: 1UD129KBjvJXoWxXF1xJw4UCw4xZw1DZrWfAFb_yoW5Zr48pF 1qka45trWrJ3y3Kw48J34vvr1ruw1kWa43ArW7C34FkFyjqFyDXa9akryjka4kJws09F43 tFZ0vFWfGrWDJaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkIb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7MxAIw28I cxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2 IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI 42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42 IY6xAIw20EY4v20xvaj40_Wr1j6rW3Jr1lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2 z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1zuWJUUUUU== X-CM-SenderInfo: pjrqw2pxltxq5kxd4v5lfo033gof0z/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE 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 From: "GONG, Ruiqi" When compiling with gcc 13 and CONFIG_FORTIFY_SOURCE=y, the following warning appears: In function ‘fortify_memcpy_chk’, inlined from ‘size_entry_mwt’ at net/bridge/netfilter/ebtables.c:2118:2: ./include/linux/fortify-string.h:592:25: error: call to ‘__read_overflow2_field’ declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror=attribute-warning] 592 | __read_overflow2_field(q_size_field, size); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The compiler is complaining: memcpy(&offsets[1], &entry->watchers_offset, sizeof(offsets) - sizeof(offsets[0])); where memcpy reads beyong &entry->watchers_offset to copy {watchers,target,next}_offset altogether into offsets[]. Silence the warning by wrapping these three up via struct_group(). Signed-off-by: GONG, Ruiqi Reviewed-by: Gustavo A. R. Silva --- v3: use sizeof(entry->offsets) as memcpy()'s size, as Gustavo suggested. v2: fix HDRTEST error by replacing struct_group() with __struct_group(), since it's a uapi header. include/uapi/linux/netfilter_bridge/ebtables.h | 14 ++++++++------ net/bridge/netfilter/ebtables.c | 3 +-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/uapi/linux/netfilter_bridge/ebtables.h b/include/uapi/linux/netfilter_bridge/ebtables.h index a494cf43a755..b0caad82b693 100644 --- a/include/uapi/linux/netfilter_bridge/ebtables.h +++ b/include/uapi/linux/netfilter_bridge/ebtables.h @@ -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 ebt_replace)))); }; diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c index 757ec46fc45a..aa23479b20b2 100644 --- a/net/bridge/netfilter/ebtables.c +++ b/net/bridge/netfilter/ebtables.c @@ -2115,8 +2115,7 @@ static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *ba return ret; offsets[0] = sizeof(struct ebt_entry); /* matches come first */ - memcpy(&offsets[1], &entry->watchers_offset, - sizeof(offsets) - sizeof(offsets[0])); + memcpy(&offsets[1], &entry->offsets, sizeof(entry->offsets)); if (state->buf_kern_start) { buf_start = state->buf_kern_start + state->buf_kern_offset; -- 2.41.0