Re: [PATCH] i2c: mux: harden i2c_mux_alloc() against integer overflows

From: Dan Carpenter
Date: Fri Sep 16 2022 - 04:21:10 EST


On Fri, Sep 16, 2022 at 01:01:42AM -0700, Kees Cook wrote:
> > 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)
>
> The new variable makes it more readable, but beyond that, do you see any
> reason not to just directly compose the calls?
>

You could do that too.

You pointed this out in your other email but the one thing that people
have to be careful of when assigning struct_size() is that the
"mux_size" variable has to be size_t.

The math in submit_create() from drivers/gpu/drm/msm/msm_gem_submit.c
is so terribly unreadable. It works but it's so ugly. Unfortunately,
I'm the person who wrote it.

regards,
dan carpenter