Re: [PATCH net-next] net: ethtool: Fix out-of-bounds copy to user

From: Alexander Duyck
Date: Fri Jun 02 2023 - 12:02:18 EST


On Fri, Jun 2, 2023 at 8:42 AM Andrew Lunn <andrew@xxxxxxx> wrote:
>
> > > Also, RTNL should be held during the time both calls are made into the
> > > driver. So nothing from userspace should be able to get in the middle
> > > of these calls to change the number of queues.
> > >
> >
> > The RTNL lock is already be held during every each ioctl in dev_ethtool().
> >
> > rtnl_lock();
> > rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
> > rtnl_unlock();
>
> Yes, exactly. So the kernel should be safe from buffer overruns.
>
> Userspace will not get more than it asked for. It might get less, and
> it could be different to the previous calls. But i'm not aware of
> anything which says anything about the consistency between different
> invocations of ethtool -S.

The problem is the userspace allocation ends up requiring we make two
calls into the stack. So it takes the lock once to get the count,
performs the allocation, and then calls into ethtool again taking the
lock and by then the value may have changed.

Within each call it is held the entire time, but the userspace has to
make two calls. So in between the two the number of rings could
potentially change.

What this change is essentially doing is clamping the copied data to
the lesser of the current value versus the value when the userspace
was allocated. However I am wondering now if we shouldn't just update
the size value and return that as some sort of error for the userspace
to potentially reallocate and repeat until it has the right size.