public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] i2c: mux: harden i2c_mux_alloc() against integer overflows
@ 2022-09-15 11:30 Dan Carpenter
  2022-09-15 13:36 ` Peter Rosin
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Dan Carpenter @ 2022-09-15 11:30 UTC (permalink / raw)
  To: Peter Rosin
  Cc: Wolfram Sang, Gustavo A. R. Silva, linux-i2c, linux-kernel,
	kernel-janitors

A couple years back we went through the kernel an automatically
converted size calculations to use struct_size() instead.  The
struct_size() calculation is protected against integer overflows.

However it does not make sense to use the result from struct_size()
for additional math operations as that would negate any safeness.

Fixes: 1f3b69b6b939 ("i2c: mux: Use struct_size() in devm_kzalloc()")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/i2c/i2c-mux.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index 774507b54b57..313904be5f3b 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -243,9 +243,10 @@ struct i2c_mux_core *i2c_mux_alloc(struct i2c_adapter *parent,
 				   int (*deselect)(struct i2c_mux_core *, u32))
 {
 	struct i2c_mux_core *muxc;
+	size_t mux_size;
 
-	muxc = devm_kzalloc(dev, struct_size(muxc, adapter, max_adapters)
-			    + sizeof_priv, GFP_KERNEL);
+	mux_size = struct_size(muxc, adapter, max_adapters);
+	muxc = devm_kzalloc(dev, size_add(mux_size, sizeof_priv), GFP_KERNEL);
 	if (!muxc)
 		return NULL;
 	if (sizeof_priv)
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-09-21 20:13 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-15 11:30 [PATCH] i2c: mux: harden i2c_mux_alloc() against integer overflows Dan Carpenter
2022-09-15 13:36 ` Peter Rosin
2022-09-15 13:51 ` Gustavo A. R. Silva
2022-09-15 14:09   ` Dan Carpenter
2022-09-16  8:07     ` Kees Cook
2022-09-16  8:23       ` Dan Carpenter
2022-09-16 13:31         ` Kees Cook
2022-09-16 14:55           ` Dan Carpenter
2022-09-16 21:31             ` Kees Cook
2022-09-16  8:01 ` Kees Cook
2022-09-16  8:20   ` Dan Carpenter
2022-09-16 19:31     ` Wolfram Sang
2022-09-19  6:35       ` Dan Carpenter
2022-09-21 20:13 ` Wolfram Sang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox