Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2241775lqo; Mon, 13 May 2024 11:58:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBpale1WqkGesF+Y0taRAWpxDl6vUY0KVtky6qSmjsk4pnDq3fGqWJuDW39X80AP/pk7cnjw30rfaDcOC02jCa+cuBZxdN/M0RUaPaxQ== X-Google-Smtp-Source: AGHT+IGU5pV4baxYy/PoALmE3WAetjFIbUH9W/LpOe8QRvxAI49ZaR7np5EANmvLGWlqG75sGpy6 X-Received: by 2002:a17:902:fc4f:b0:1e8:c962:4f6e with SMTP id d9443c01a7336-1ef43d27f6fmr120769805ad.20.1715626724011; Mon, 13 May 2024 11:58:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715626723; cv=pass; d=google.com; s=arc-20160816; b=Uq/WMRFQMjhWVyvjeuulCFJmGKxLiRTYVt9IR8WGywmt9prO1tQWn4NWPJOYUHqtgi APLXgEv4H8BAg0QoJjf+5YYwbtXViNQpiQBzYWjkFtWxdJwxXNlqVbR81i/oepC6aQb8 lrGQXK+95bn+jqsfuMNsnBhzVSam/JZACFhRIVBhRxd+A4mTKLmhIF4KrESaAxxLsw0C QHKZoaRxg7h2qsO5em7REWKGVCKZ+O0Mmc+Wj4JrC2c+PRo+Krd1pspSLaYC+JFfLzQN hSbF0kqgoN0CAQPSD8cnZfEUKz66ROG1+STJjwL9+kaXEos/Fz7uUBanziJYyz/hlraR Y9fw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=9Sr9kb5P0bdJ6cwmhx72J5XTZqy5p16gtf0vFcQMPyY=; fh=tjpOsYle9VvsSQplZYeyC4nNg0Yg903UfWff15uncOE=; b=WrQV68OnKM5eFMJPYcSffWd4E46pkWZ2nzYMQCX3yEzsSFWLhO5fBX5Yco5oApF6kI fikjTlSd85xlZ2GByGTg6NpkSNM1b3n/t1YZylcbX6W1Eg9CR7vENDCHpDUg2ig7//jW fxswfgxWTmUYliv8skVqT8V3aVC6OZqi4xBLoYB8HLh8qhwkAFOlX00niAc4vshwJRvV kQUTVy9Lqh+Cv/ESv5eic+gzJdTDhFZp7jo6OSU7r85PG41KkcJbtKo+7+LIGMe2AO5j VKi81MEIEhotiDxEtLkNBioSEIt9m/KKEvqCF0AyXkBS16gUFSsCCONsKha342PH2kuB Zfng==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WH5YCYqI; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-177996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177996-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c038741si92138575ad.343.2024.05.13.11.58.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 11:58:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-177996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WH5YCYqI; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-177996-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177996-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id EBE6EB22C95 for ; Mon, 13 May 2024 18:57:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2674F446DE; Mon, 13 May 2024 18:57:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="WH5YCYqI" Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A0993D0A4 for ; Mon, 13 May 2024 18:57:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715626628; cv=none; b=JxKG6cGgNRJ+G8Qsl7FDXgMNlYQ6hEHbHP80l5i3LQJzZ4UeMNeEMNzLlrx+F72uzm8t7Kkf82AVAo1yM9EC5R3E7IVCNH2Oe48kt6N+AKMwDhyTZcGNBSHp5pJ2wTM1iqPVtTjGKTDhw2M+mYYeTkHjg279qAo5WUSA6J7s1rM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715626628; c=relaxed/simple; bh=q5wPdG/4nQ7huyx+fHjYRyb5vaRB6g0n6Fi2oJpjudg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=thS6h3zQelDXd7Fz1h1+ivIUP5Chv1XhJuIcZ8KA5OLfTylpOPvWPzTj2vzYX0bU4bnuWLXw76hp7Bx7YLp03PxRTNZuLSedt6ekc83NrYObW3Zb+sQI97de0pHrzZ9lL0W4JBV4xE71F7d6U2MQ0LLPi4Z2hqj4Qi0eVYhhTHk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=WH5YCYqI; arc=none smtp.client-ip=209.85.215.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-61be599ab77so2100010a12.1 for ; Mon, 13 May 2024 11:57:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715626625; x=1716231425; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=9Sr9kb5P0bdJ6cwmhx72J5XTZqy5p16gtf0vFcQMPyY=; b=WH5YCYqIc8Jge3V/TDdAYdAP9FxsCdf0v/8mvIAgJkeEcji+qwKYD4Qk2p3wdKKANp Pvg/RJlUMxDO68KxfJL93NYkgY+GN3AwySDULVBqXtEK02qja+Bo/i0B6YUJ5zjiVihk MjMc+uUJl97mvLUsNNG4GO4ic3eFgFQ6wJhm8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715626625; x=1716231425; 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 :message-id:reply-to; bh=9Sr9kb5P0bdJ6cwmhx72J5XTZqy5p16gtf0vFcQMPyY=; b=IrEBYvuqZ3FIK020cCgYUAKxIANQdFVeK9rTg8y668pE7V2QHhTrZ2hcXFDHk16rnI hN3hfFkweGrYvQa6l1I6aooXODzVAwx4O11vHaxhnQF4A2ijqkGo3saDVtNMaxoC0CNu 9VlmSWRjvMLqyCQWKeYJJ85+ZBkSeE0R68yv8GW+j4BF8/PARELzkdZ5ubcaIiCU1fd3 KlSD/os155LQj4RSxxA4QF8MWtB6YmLM23A3wrsiAz4tUzOlclYzRoJ66GXDTXC9MgUl EhAT+Le27WJELVxjFFqK8cZAgwHt3YSftDv4chfqwERTqlSIANp89q5cpgBhFWRGh/GI FbkA== X-Forwarded-Encrypted: i=1; AJvYcCUAeg91SPVM/7VHnKM7ey4Sp9XAjPjllpK/rzzvudrVAciKXJTqqP8bNAtlY+EUr0VzwR5nD6iOzfn77/UT1kSGcvtteLXk3oVj2OJp X-Gm-Message-State: AOJu0YxLX5/dboEaK6/MGl1KhPi+buOtWKxIzzlrp9l2gZMShUNch/Pu VC3NEVdP13cZsBuCEURrgsi8TMO48dDI7L6Fj2kjijvpeuL0ZZ+cr6OGz73vDQ== X-Received: by 2002:a05:6a21:2b0a:b0:1af:cd4a:1e1d with SMTP id adf61e73a8af0-1afde1c554bmr8986093637.40.1715626625368; Mon, 13 May 2024 11:57:05 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ef0bf31a7asm84964695ad.140.2024.05.13.11.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 11:57:04 -0700 (PDT) Date: Mon, 13 May 2024 11:57:04 -0700 From: Kees Cook To: Erick Archer Cc: Taras Chornyi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH] net: prestera: Add flex arrays to some structs Message-ID: <202405131154.BABC943C@keescook> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Sun, May 12, 2024 at 06:10:27PM +0200, Erick Archer wrote: > The "struct prestera_msg_vtcam_rule_add_req" uses a dynamically sized > set of trailing elements. Specifically, it uses an array of structures > of type "prestera_msg_acl_action actions_msg". > > The "struct prestera_msg_flood_domain_ports_set_req" also uses a > dynamically sized set of trailing elements. Specifically, it uses an > array of structures of type "prestera_msg_acl_action actions_msg". > > So, use the preferred way in the kernel declaring flexible arrays [1]. > > At the same time, 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 via > CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for > strcpy/memcpy-family functions). In this case, it is important to note > that the attribute used is specifically __counted_by_le since the > counters are of type __le32. > > The logic does not need to change since the counters for the flexible > arrays are asigned before any access to the arrays. > > The order in which the structure prestera_msg_vtcam_rule_add_req and the > structure prestera_msg_flood_domain_ports_set_req are defined must be > changed to avoid incomplete type errors. > > Also, avoid the open-coded arithmetic in memory allocator functions [2] > using the "struct_size" macro. > > Moreover, the new structure members also allow us to avoid the open- > coded arithmetic on pointers. So, take advantage of this refactoring > accordingly. > > This code was detected with the help of Coccinelle, and audited and > modified manually. > > Link: https://www.kernel.org/doc/html/next/process/deprecated.html#zero-length-and-one-element-arrays [1] > Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [2] > Signed-off-by: Erick Archer This is a really nice cleanup. :) Fewer lines of code, more readable, and protected by __counted_by! Reviewed-by: Kees Cook -- Kees Cook