From: Vivien Didelot <[email protected]>
[ Upstream commit 0ee4e76937d69128a6a66861ba393ebdc2ffc8a2 ]
ethtool_get_regs() allocates a buffer of size ops->get_regs_len(),
and pass it to the kernel driver via ops->get_regs() for filling.
There is no restriction about what the kernel drivers can or cannot do
with the open ethtool_regs structure. They usually set regs->version
and ignore regs->len or set it to the same size as ops->get_regs_len().
But if userspace allocates a smaller buffer for the registers dump,
we would cause a userspace buffer overflow in the final copy_to_user()
call, which uses the regs.len value potentially reset by the driver.
To fix this, make this case obvious and store regs.len before calling
ops->get_regs(), to only copy as much data as requested by userspace,
up to the value returned by ops->get_regs_len().
While at it, remove the redundant check for non-null regbuf.
Signed-off-by: Vivien Didelot <[email protected]>
Reviewed-by: Michal Kubecek <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
net/core/ethtool.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
--- a/net/core/ethtool.c
+++ b/net/core/ethtool.c
@@ -1434,13 +1434,16 @@ static int ethtool_get_regs(struct net_d
return -ENOMEM;
}
+ if (regs.len < reglen)
+ reglen = regs.len;
+
ops->get_regs(dev, ®s, regbuf);
ret = -EFAULT;
if (copy_to_user(useraddr, ®s, sizeof(regs)))
goto out;
useraddr += offsetof(struct ethtool_regs, data);
- if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
+ if (copy_to_user(useraddr, regbuf, reglen))
goto out;
ret = 0;
Hi!
> From: Vivien Didelot <[email protected]>
>
> [ Upstream commit 0ee4e76937d69128a6a66861ba393ebdc2ffc8a2 ]
>
> ethtool_get_regs() allocates a buffer of size ops->get_regs_len(),
> and pass it to the kernel driver via ops->get_regs() for filling.
>
> There is no restriction about what the kernel drivers can or cannot do
> with the open ethtool_regs structure. They usually set regs->version
> and ignore regs->len or set it to the same size as ops->get_regs_len().
>
> But if userspace allocates a smaller buffer for the registers dump,
> we would cause a userspace buffer overflow in the final copy_to_user()
> call, which uses the regs.len value potentially reset by the driver.
>
> To fix this, make this case obvious and store regs.len before calling
> ops->get_regs(), to only copy as much data as requested by userspace,
> up to the value returned by ops->get_regs_len().
>
> While at it, remove the redundant check for non-null regbuf.
Mainline differs from 4.19-stable here, and while the non-null check
is redundant in -mainline, it does not seem to be redundant in
-stable.
In stable, if get_regs_len() returns < 0, we'll pass it to vzalloc.
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
On Mon, Jun 10, 2019 at 10:21:12AM +0200, Pavel Machek wrote:
> Hi!
>
> > From: Vivien Didelot <[email protected]>
> >
> > [ Upstream commit 0ee4e76937d69128a6a66861ba393ebdc2ffc8a2 ]
> >
> > ethtool_get_regs() allocates a buffer of size ops->get_regs_len(),
> > and pass it to the kernel driver via ops->get_regs() for filling.
> >
> > There is no restriction about what the kernel drivers can or cannot do
> > with the open ethtool_regs structure. They usually set regs->version
> > and ignore regs->len or set it to the same size as ops->get_regs_len().
> >
> > But if userspace allocates a smaller buffer for the registers dump,
> > we would cause a userspace buffer overflow in the final copy_to_user()
> > call, which uses the regs.len value potentially reset by the driver.
> >
> > To fix this, make this case obvious and store regs.len before calling
> > ops->get_regs(), to only copy as much data as requested by userspace,
> > up to the value returned by ops->get_regs_len().
> >
> > While at it, remove the redundant check for non-null regbuf.
>
> Mainline differs from 4.19-stable here, and while the non-null check
> is redundant in -mainline, it does not seem to be redundant in
> -stable.
>
> In stable, if get_regs_len() returns < 0, we'll pass it to vzalloc.
Right. :-(
I guess we should also pick commit f9fc54d313fa ("ethtool: check the
return value of get_regs_len") to stable branches before 5.0.
Michal
On Mon, Jun 10, 2019 at 10:42:29AM +0200, Michal Kubecek wrote:
> On Mon, Jun 10, 2019 at 10:21:12AM +0200, Pavel Machek wrote:
> > Hi!
> >
> > > From: Vivien Didelot <[email protected]>
> > >
> > > [ Upstream commit 0ee4e76937d69128a6a66861ba393ebdc2ffc8a2 ]
> > >
> > > ethtool_get_regs() allocates a buffer of size ops->get_regs_len(),
> > > and pass it to the kernel driver via ops->get_regs() for filling.
> > >
> > > There is no restriction about what the kernel drivers can or cannot do
> > > with the open ethtool_regs structure. They usually set regs->version
> > > and ignore regs->len or set it to the same size as ops->get_regs_len().
> > >
> > > But if userspace allocates a smaller buffer for the registers dump,
> > > we would cause a userspace buffer overflow in the final copy_to_user()
> > > call, which uses the regs.len value potentially reset by the driver.
> > >
> > > To fix this, make this case obvious and store regs.len before calling
> > > ops->get_regs(), to only copy as much data as requested by userspace,
> > > up to the value returned by ops->get_regs_len().
> > >
> > > While at it, remove the redundant check for non-null regbuf.
> >
> > Mainline differs from 4.19-stable here, and while the non-null check
> > is redundant in -mainline, it does not seem to be redundant in
> > -stable.
> >
> > In stable, if get_regs_len() returns < 0, we'll pass it to vzalloc.
>
> Right. :-(
>
> I guess we should also pick commit f9fc54d313fa ("ethtool: check the
> return value of get_regs_len") to stable branches before 5.0.
Now queued up, thanks.
greg k-h