From patchwork Sun Oct 1 11:07:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 147082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp841479vqb; Sun, 1 Oct 2023 05:17:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQx+x862lJlCcx7ybe06lC0Ym5WV3JFGrW9Hd8aeDCngSGPD8mm1fPjuJpjskJdtEkNLRg X-Received: by 2002:a05:6870:a706:b0:196:45b7:9385 with SMTP id g6-20020a056870a70600b0019645b79385mr10609224oam.27.1696162675738; Sun, 01 Oct 2023 05:17:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696162675; cv=none; d=google.com; s=arc-20160816; b=owX8VdYL2y5maeXm30zKJKSIj7xa4Ne7QlGArrVhCHW4aTVTE9qC6L5IpBBqYJp8aB mgnfUqoWnenqlYEQ8RrxxD3kpHsTgQAmE3kzGs8BjirJjzy66lZCIpP7t8/7pccTROaW 3n4QSKa+eq035qBi6Oy/mPd+wyayC2D9YBfvesSmoFeVDOB4xVdMLixmk2UWc6hyBSGg fb8AYh/j8kXm3opRrQAeLL5PnmGmrnbzQR0762nZ2J+op96FF7IpQ6LH20XVVlcizquX jTRs4G5eyRiISya6gcrgMOiQ5qaG3UokAD2cF+N+GCScyRo3Q1pATKMCJibM0MQV4WGR 1xRg== 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:dkim-signature; bh=vnDdg5+DzhgdjgHe7taJV26QihvlT8zptPXcuelUK94=; fh=KxHMk//6qYJ++e6pngwlhB64GPqdNhTrWpurz2QyY6Y=; b=CIkFSL4t+Mntw6k7p+BXnZpnOYxzIJWF+5KZyHFu1i7kKI6Ke1ha0i66p7Rr38nhTg ehsQl8D4+uotblNHfK1HjkXXsqjUMLIVykwiS9C6oynOs3utpkoANLe8KgPH7bsm0mlh mwqwTQaZwEM+ZdyhzMbOinx09zQ0tz2/1XgYO+9o4P445P9Z0vJy9J2wLnz5t1VZg3rj kG75OujNzNCwm1KvgElgpNgOZxSlvIa6EAOJ2RwPRyNhLVW4tAsCPg4scgoD8K60wxCr uWIHFvVv8a/KHZbg+n+ODqJK90qvYw0znCsYfo9PDbct6VjxDdFJfA/JGCZSXSPiEIcI iuMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=nPaerTM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id e5-20020a636905000000b0057754fd6270si22609668pgc.651.2023.10.01.05.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 05:17:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=nPaerTM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 6B271802FBA8; Sun, 1 Oct 2023 04:10:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234871AbjJALHj (ORCPT + 19 others); Sun, 1 Oct 2023 07:07:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234821AbjJALHh (ORCPT ); Sun, 1 Oct 2023 07:07:37 -0400 Received: from smtp.smtpout.orange.fr (smtp-25.smtpout.orange.fr [80.12.242.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE033C2 for ; Sun, 1 Oct 2023 04:07:32 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id muIKqTqNAm6tamuIKq8U2z; Sun, 01 Oct 2023 13:07:30 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1696158450; bh=vnDdg5+DzhgdjgHe7taJV26QihvlT8zptPXcuelUK94=; h=From:To:Cc:Subject:Date; b=nPaerTM624Q5KEjxgJswqeFpq5OhcBol67sFbIhkVF77GVRMUBXpY0DwuSsqJOOTn wVoZtLnsn5JrD9WKvVwo47eqbRqe37LfBdvE372lstTMcBReVX5Ne/KYW0ECHitHIG BiMgTnyMEZlatR/fk9sP4bO8zUmQE/nRZaUza69inlUDs29S+7LqSv5TUVY2+byAZC /Qt14ebRducV3Or2yaSeyn1BR8eYoPair4dsek646jESx9lLw/nO4p8QGHI/x4YLqX ER03VapFfVsNtESQi5WqNOJlKhxDMhW6E9sBbuojDeRTBQzTP5nH/O0hBrRrgtmmf1 iNgvxfBcgFv+A== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 01 Oct 2023 13:07:30 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: keescook@chromium.org, Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , netdev@vger.kernel.org, dev@openvswitch.org Subject: [PATCH net-next 1/2] net: openvswitch: Use struct_size() Date: Sun, 1 Oct 2023 13:07:22 +0200 Message-Id: <8be59c9e06fca8eff2f264abb4c2f74db0b19a9e.1696156198.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 01 Oct 2023 04:10:47 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778555473386036612 X-GMAIL-MSGID: 1778555473386036612 Use struct_size() instead of hand writing it. This is less verbose and more robust. Signed-off-by: Christophe JAILLET --- This is IMHO more readable, even if not perfect. However (untested): + new = kzalloc(size_add(struct_size(new, masks, size), size_mul(sizeof(u64), size)), GFP_KERNEL); looks completely unreadable to me. --- net/openvswitch/flow_table.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c index 4f3b1798e0b2..d108ae0bd0ee 100644 --- a/net/openvswitch/flow_table.c +++ b/net/openvswitch/flow_table.c @@ -220,16 +220,13 @@ static struct mask_array *tbl_mask_array_alloc(int size) struct mask_array *new; size = max(MASK_ARRAY_SIZE_MIN, size); - new = kzalloc(sizeof(struct mask_array) + - sizeof(struct sw_flow_mask *) * size + + new = kzalloc(struct_size(new, masks, size) + sizeof(u64) * size, GFP_KERNEL); if (!new) return NULL; new->masks_usage_zero_cntr = (u64 *)((u8 *)new + - sizeof(struct mask_array) + - sizeof(struct sw_flow_mask *) * - size); + struct_size(new, masks, size)); new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) + sizeof(u64) * size, From patchwork Sun Oct 1 11:07:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe JAILLET X-Patchwork-Id: 147135 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp958431vqb; Sun, 1 Oct 2023 09:35:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHMSsE4bKg+tZ+SLYaRI5uuiFmgWPual1yjSnJSZ/+jEQeXZFGIqkQYsagQCk9IJyaYstWm X-Received: by 2002:a05:6a21:9995:b0:15a:4c23:870b with SMTP id ve21-20020a056a21999500b0015a4c23870bmr14092299pzb.23.1696178143843; Sun, 01 Oct 2023 09:35:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696178143; cv=none; d=google.com; s=arc-20160816; b=T//SXcm7WVxuo6JXKivT98AJQUCu+fC3i1kq6bQVTVKs9ifns9qwW4xlzPwPC7BPYf JY/IUfrVsfyk8EUSl42fzLfC0H/uGYrNwsVPSjgHosICKlz9zvvm9Oz/aOv3ttvZj507 HG2CDu9eapeWRmxPZSs8I1ApRn0hMrGPm8GsPIPS6aE0Dq2jrsdITNQOl0mJvsWqrWZn faodpvex01Fwsl+DusZX6VEAg/Jnos9g24NIyUqlKf4h9hcR+underjGeFP+ZYF5F8Wi F3kiyKFkrA7hOwf8S+L62JYna5EwoTl/jvf4P8uu2/8V82JiJBOdyJJoO7RqJPg4mu9J gESA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=v9P7JS8Eo6gg0JgpQ/a5dnlXc7ZeGy/7g3pcTmVCJ18=; fh=144h59Trx5ef5R0VkTN5OfufadNCskPabTmJJAa01IQ=; b=W3HOZPmCImIm3vvqnCRp1A/D1/Vrh2p30ikLh9RCTb21qHs40mE2i3ShFFdkV1CTLJ gGXS3TaXdNf5wqZTeCPalUpJ3qoXwSsCo63PI7754HDv9BSwdM3Lqxa4JcIohhXEBDxf hDye2VMfqLBzQOJdV+mBL3YbHIer9ILSSUaURtXAmLMfocw8jfsssxQO2aSFZ5MZNB/S JmiGE4raFGHG2G27xkrHOJU6ttAayovCp+yWjSIIt70Rrsb5SSZGHde8jSkHS29oG3Zj 4C6OCJUj+kmKEZsuJpMxNvSb4E9VxWrZtvrS1N+TlAtabVxe0aMiWFmBFDkVzHYa933+ pfqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=OcrHXPEw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u23-20020a056a00099700b00687008df88dsi28311717pfg.52.2023.10.01.09.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 09:35:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=OcrHXPEw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0D3D7802887A; Sun, 1 Oct 2023 04:11:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234886AbjJALHn (ORCPT + 19 others); Sun, 1 Oct 2023 07:07:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234873AbjJALHk (ORCPT ); Sun, 1 Oct 2023 07:07:40 -0400 Received: from smtp.smtpout.orange.fr (smtp-26.smtpout.orange.fr [80.12.242.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A66EDD for ; Sun, 1 Oct 2023 04:07:37 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id muIKqTqNAm6tamuIRq8U3c; Sun, 01 Oct 2023 13:07:36 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1696158456; bh=v9P7JS8Eo6gg0JgpQ/a5dnlXc7ZeGy/7g3pcTmVCJ18=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OcrHXPEwJq5ccOGM2WWXMREYaA5x0o3Tt5f28aR5RqTrPTdP8zPl6Hd6n0mtT99Tv 1KHlmqCZPA7tCaA0QYbYkbQ+SmOJPAc2RDlcL6TtG0gyKE8KZP4+U9/GKRDBW+GCp6 VzWecaU8rn3uaqyZ8qYdCITjFOtAqHPeX3w6zoM+d5R8cWW0FBa1TCFlMIuXAoNNSv yNqQoese2sMY/E4nuu0QocTSjow/jUUN7brb7p0UkkKckCpc7lPXVcsS6Fk3d0bA9m tpBLPy4DIdYTZKJVl6e1HeXg7sdsUnpTyrqCh2IyL22dIUZesYXZbSffSWzSu9wvfM IkgecWhFDL3cQ== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 01 Oct 2023 13:07:36 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET To: keescook@chromium.org, Pravin B Shelar , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Tom Rix Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET , netdev@vger.kernel.org, dev@openvswitch.org, llvm@lists.linux.dev Subject: [PATCH net-next 2/2] net: openvswitch: Annotate struct mask_array with __counted_byUse struct_size() Date: Sun, 1 Oct 2023 13:07:23 +0200 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: <8be59c9e06fca8eff2f264abb4c2f74db0b19a9e.1696156198.git.christophe.jaillet@wanadoo.fr> References: <8be59c9e06fca8eff2f264abb4c2f74db0b19a9e.1696156198.git.christophe.jaillet@wanadoo.fr> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 01 Oct 2023 04:11:01 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778571693591514396 X-GMAIL-MSGID: 1778571693591514396 Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). Signed-off-by: Christophe JAILLET --- This patch is part of a work done in parallel of what is currently worked on by Kees Cook. My patches are only related to corner cases that do NOT match the semantic of his Coccinelle script[1]. In this case, in tbl_mask_array_alloc(), several things are allocated with a single allocation. Then, some pointer arithmetic computes the address of the memory after the flex-array. [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci --- net/openvswitch/flow_table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/openvswitch/flow_table.h b/net/openvswitch/flow_table.h index 9e659db78c05..8d9e83b4d62c 100644 --- a/net/openvswitch/flow_table.h +++ b/net/openvswitch/flow_table.h @@ -48,7 +48,7 @@ struct mask_array { int count, max; struct mask_array_stats __percpu *masks_usage_stats; u64 *masks_usage_zero_cntr; - struct sw_flow_mask __rcu *masks[]; + struct sw_flow_mask __rcu *masks[] __counted_by(size); }; struct table_instance {