Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp435567rbb; Sat, 24 Feb 2024 06:59:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUXimdOsg4NM+f3H4dUKblihWGB45isDjZ0nK5R9IxH4e2ETHSjNsEruk8cVx4nG7oxpdWNsvfKzKEd1tXrsuDNBDp3DDQzspbLBTwQgw== X-Google-Smtp-Source: AGHT+IFANpk5Bx7CabLAXU5Y6fRU+ICe0UBb1KLrUNlr56r46pkn7Co4aPMhQR95JlRl5f883vay X-Received: by 2002:a17:906:8802:b0:a3f:40ac:1e0e with SMTP id zh2-20020a170906880200b00a3f40ac1e0emr1680673ejb.67.1708786799254; Sat, 24 Feb 2024 06:59:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708786799; cv=pass; d=google.com; s=arc-20160816; b=kZFYNpTcs4xHtElPu1Yqlxqr8hBjkfySfQZ8zQG+rQtWY+55rELb0mssNO0ILgyDS7 5SY3oW8GflETFAm9f3XOfXmBTDDGxuQBC6brFl9yiQCKoJ6xZWszvxMD96rKEzIZchPG uy1Ylztu6wB2h8Z4ehUij2WUSpWxozSyJzMPB+ZmL31ZVm5VOqCjXMLOujcoIc+Nz+rt FcNLxnY3LrGTNLmqu1ceHhA61VsawK090fVLMvcAGZobN7V3Izf1FnjjYePQsA6pUROx lnjtCBQyWF4PzWcZ2vcqS2GITQ3vh7p4KCGLL/ADzVI704LqOKzKkvFZ4xdNk8xEGZvA qVdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from:dkim-signature; bh=/cwjvg9+u7ZlrLMkHUpJ95aVxmWV89rVx4iHmJaYaHo=; fh=XqbhKM6+62AJKqUXDzjWbPw1sYNb9O4emxySFhF9Qzc=; b=bKH7cCf88SC+/R6omlvazW0FU3ByXzW49Ty0xrLlNbtwQAXLcimVyFTTXepLWlZQ/p wteoTxVa29qFTreK/6oxG9YJCJ2ZC89ns03OfEYFWkN5nVrDfE609HZqUWHK0B+cJFRe 1lJereZaqWbYX51qhaRUnfOXSXEVQhDxi4RoQFGJYpPRJKMmvU+EhV4bKfJ14v4ITfrS KPD2DX58zOxsoatQT6gOJnFnxRGE09LAVeKvNJEzMYbV1ZI8LMYh03eS0uB83rGK22MI pg37RcUsCbJ3DTb8akJMH/VdQ8MBG5TWsGVRU2islTLn9Ab3Lmo6M8fGghftsqyH+e6U w4tg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=ZtoPfK2K; arc=pass (i=1 spf=pass spfdomain=gmx.com dkim=pass dkdomain=gmx.com dmarc=pass fromdomain=gmx.com); spf=pass (google.com: domain of linux-kernel+bounces-79708-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79708-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id t13-20020a1709063e4d00b00a3f554f9c57si592143eji.481.2024.02.24.06.59.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Feb 2024 06:59:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79708-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.com header.s=s31663417 header.b=ZtoPfK2K; arc=pass (i=1 spf=pass spfdomain=gmx.com dkim=pass dkdomain=gmx.com dmarc=pass fromdomain=gmx.com); spf=pass (google.com: domain of linux-kernel+bounces-79708-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79708-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 0925E1F22A4C for ; Sat, 24 Feb 2024 14:59:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 25A6945C08; Sat, 24 Feb 2024 14:59:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.com header.i=erick.archer@gmx.com header.b="ZtoPfK2K" Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDA5145028; Sat, 24 Feb 2024 14:59:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708786791; cv=none; b=L7UoN+YTR1F/X1YbRDaQRib4/8GWaGYME6esbgGl8OQ8IT0YaEuqP+caE7XKhcxWfqOZtNmqhErKwX+ZLgHXzp1E+VaMK4WF9RLDCWLTU6hwYjIP8UTdAYBPswsO5nSG64Cn2/czg1cfZja6SQfijjTmxH1w1ljsVI8tLEalfw8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708786791; c=relaxed/simple; bh=GXwJdQKczfCri7kuRFyPAfWFwpv9CLz1xjPSkkrGDBE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=t+1uYnxo3vtmbikTMNvj6PmRoowD6sOrqc0n1+paCRpZiklkudkuZJ2zyICkuCNwyo+fSRXb9DLKFgRlZ00JZkVK9Xidrd0J9Gb7WcfMQK5USDjAtrWsLPVIUiLyOr7mXIbOF9bovz4WgEPGU9XtoRD17ruH2v/Jmyc2oXLgi1U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com; spf=pass smtp.mailfrom=gmx.com; dkim=pass (2048-bit key) header.d=gmx.com header.i=erick.archer@gmx.com header.b=ZtoPfK2K; arc=none smtp.client-ip=212.227.17.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.com; s=s31663417; t=1708786779; x=1709391579; i=erick.archer@gmx.com; bh=GXwJdQKczfCri7kuRFyPAfWFwpv9CLz1xjPSkkrGDBE=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=ZtoPfK2KUcgeUx7BrZozE9lFn3/vLA49E1ChYV/y6jnopnfYH+tOI8iazTwx//Td wM0/HJLVUjXkGN/+C+o+ov2121Pe30hfUWJLsMOo1bgJMcKsb1danFAni1PZUn0ZS 8JqKFjYQ5bd3v6J2YYVVCd62ZWNUg35zEtJGFPdXnKJC8P5sl/GTmI1J83umU7wVd bGxzy/EFjvdkIgL71nmgiw2rOWjxfhAskzyRAO974PnZ6q7lHD5OOtfSsur29MSC1 qYFbxRW7jFxhNDCdMVWObC85tHESs+z/6leKvf9JNrxO3kRIb7151W6minzbWZxAF YNkpgm6TTlk2dyxwNA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from localhost.localdomain ([79.157.194.183]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1N7i8Y-1qrCvp1g1K-014ieB; Sat, 24 Feb 2024 15:59:39 +0100 From: Erick Archer To: Kent Overstreet , Brian Foster , "Gustavo A. R. Silva" , Kees Cook Cc: Erick Archer , linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] bcachefs: Prefer struct_size over open coded arithmetic Date: Sat, 24 Feb 2024 15:59:24 +0100 Message-Id: <20240224145924.7468-1-erick.archer@gmx.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:JUoiwKLF8APLvcFjy9rt3Tj1Yh62YBlzwLpZ/tX95GiVy7Ykwsz YwEEQ6AfKP+TnH3k4Rp7Qi6vq3AZ1859s1mPKceixyerZ41+mVf0FRhXy5DqKPTcMPR0Pkw e2qb9Hks9yUrs3pnRzHu3VbBfwC0u9B64K4xvsZJbSsJJD2NFmYNBD+ONjdkIWaQ1c4eMZP jhDTGAGZ5VbB2pNVHUXiw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:u5njqjBC44s=;+Up8iJIUClOpGuh2fvPIJnlxf1q y/NJQQvQ8X6Meif7skAMa8oZOjB1bjBc76N5VMaZDyNUMU0ZiMvXxkLtEvmLWq6O8Thzp38zp hfuCcdDsOrbN5Y3E+Rsefo9JfeihJ9zuMmOC7UKABoSAKqVdqPamv0PiyoNGwaisAPc7iQVeL vqTBOptLQoFYOljJpF3m+vnybexTdNCSB5H+Yr3GuRtA6CE623dUcgQW/TjoZ7fVb7pa+mRtC aQBfI5V19rj63Gs/h48Mn0yrfdYHk7feTt5gpGH8Gv3KRC+JuVmHBAmNx42dFniqkpaCofH89 GBpApAtghWNtjlhGKXlurYu4UM9p2DrqLpmmu8LXoIFOXkbG1dUWSUdRamuoCUA6Yb0YXPsnN SgWtUFj7wRpLaXo3rpNF3cw+dnIrwc/oNiXAvGSZ8S8boNCj2uS4ir4aiChmRYF53+vzO44YU Q6qdcOLRMj7U5zDUGw6H0/14v3MnL3GL43ZSoE7AZU4y0jlJSs9k1O0r7/0bQiOuXlHnIm3m+ IKrF1KvVjfhaAt/4XY6tbDiPq4RXJH708HT3JLvp5IsnisDcwPv/TQH+FMYUvPzEx8iQZ1PmH ff5xQLe2nkZVcEG8iz7CbBqrwTHPyaqrEdWq7L6/fYxzx0iEEbKiZXUtMJkRRdFam3g8pTCzw NaYH8Zm5vpBGI1/o6p7eLrunrQFIvUmKAuTOkIoTC8kkuOIlc2Zo7auKb+bl6H7Mx06VZMdMz UPIJI8Hv1WtFvtVxHMFC0V245dcgCZXOOJttmsUjo5WGvl5d88wadNZEIq9TP/SvyDWef6D39 mJ2YtKlTKBeyxtZ7yQWhSpkVYIgq/UfGR5pxF6lDH/AVs= This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2]. As the "op" variable is a pointer to "struct promote_op" and this structure ends in a flexible array: struct promote_op { [...] struct bio_vec bi_inline_vecs[]; }; the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the argument "size + size * count" in the kzalloc() function. This way, the code is more readable and safer. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-= coded-arithmetic-in-allocator-arguments [1] Link: https://github.com/KSPP/linux/issues/160 [2] Signed-off-by: Erick Archer =2D-- fs/bcachefs/io_read.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/bcachefs/io_read.c b/fs/bcachefs/io_read.c index dce136cd2271..01beab55c6b3 100644 =2D-- a/fs/bcachefs/io_read.c +++ b/fs/bcachefs/io_read.c @@ -174,7 +174,7 @@ static struct promote_op *__promote_alloc(struct btree= _trans *trans, if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_promote)) return ERR_PTR(-BCH_ERR_nopromote_no_writes); - op =3D kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_KERNEL)= ; + op =3D kzalloc(struct_size(op, bi_inline_vecs, pages), GFP_KERNEL); if (!op) { ret =3D -BCH_ERR_nopromote_enomem; goto err; =2D- 2.25.1