core: dev: don't call BUG() on bad input
Subsystems (2)
PATCH 1/1 core: dev: don't call BUG() on bad input
alloc_netdev() may be called with too long name (more that IFNAMSIZ bytes). Currently this leads to BUG(). Other insane inputs (bad txqs, rxqs) and even OOM don't lead to BUG(). Made alloc_netdev() return NULL, like on other errors.
---
Compile tested.
net/core/dev.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
net/core/dev.c +4 -1
@@ -5761,7 +5761,10 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,size_t alloc_size;struct net_device *p; -BUG_ON (strlen (name) >=sizeof (dev->name)); +if (strnlen (name,sizeof (dev->name)) >=sizeof (dev->name)) { +pr_err ("alloc_netdev: Too long device name \n" ); +return NULL ; + }if (txqs <1 ) { pr_err("alloc_netdev: Unable to allocate device "
Signature · 1 line
1.7.0.4
Quoted 17 lines · Vasiliy Kulikov
alloc_netdev()may be called with too long name (more that IFNAMSIZ bytes). Currently this leads toBUG(). Other insane inputs (bad txqs, rxqs) and even OOM don't lead toBUG(). Madealloc_netdev()return NULL, like on other errors.Signed-off-by Vasiliy Kulikov<[email protected]>---
Compile tested.net/core/dev.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)net/core/dev.c +0
-0
net/core/dev.c struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, · line 5764
Quoted 5 lines
net/core/dev.c +1
-1@@ -5761,7 +5761,10 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,size_t alloc_size;struct net_device *p; -BUG_ON (strlen (name)>=sizeof (dev->name)); +if (strnlen (name,sizeof (dev->name))>=sizeof (dev->name)) {
"size_t strnlen(const char *s, size_t maxlen) : The strnlen() function returns strlen(s), if that is less than maxlen, or maxlen if there is no '\0' character among the first maxlen characters pointed to by s."
How can strnlen(name, sizeof(dev->name)) be greater than sizeof(dev->name)?
Shouldn't it be "if (strnlen(name, sizeof(dev->name)) == sizeof(dev->name))" instead?
Nicolas.
Quoted 6 lines
net/core/dev.c +3
-0+pr_err ("alloc_netdev: Too long device name \n" ); +return NULL ; + }if (txqs<1 ) { pr_err("alloc_netdev: Unable to allocate device "
Hi Nicolas,
Quoted 2 lines · Nicolas de Pesloüan
Review
Quoted 2 lines
(quoted diff) +1
-1- BUG_ON(strlen(name)>= sizeof(dev->name)); + if (strnlen(name, sizeof(dev->name))>= sizeof(dev->name)) {
Ehh... Space after ")" is needed :)
Quoted 8 lines
"
size_tstrnlen(const char *s, size_t maxlen): Thestrnlen()function returns strlen(s), if that is less than maxlen, or maxlen if there is no '\0' character among the first maxlen characters pointed to by s."How can
strnlen(name, sizeof(dev->name))be greater thansizeof(dev->name)?Shouldn't it be "if (
strnlen(name, sizeof(dev->name))==sizeof(dev->name))" instead?
Not a big deal, but MO it's better to guard from everything that is not a good input by negating the check. strnlen() < sizeof() is OK, strnlen() >= sizeof() is bad. Is "==" more preferable for net/ coding style?
Signature · 2 lines
Vasiliy Kulikov http://www.openwall.com - bringing security into open computing environments
Quoted 1 line · Vasiliy Kulikov
Hi Nicolas,
Hi Vasiliy,
Quoted 5 lines · Nicolas de Pesloüan
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- BUG_ON(strlen(name)>= sizeof(dev->name)); + if (strnlen(name, sizeof(dev->name))>= sizeof(dev->name)) {Ehh... Space after ")" is needed :)
:-D
Quoted 5 lines
Quoted 5 lines
"
size_tstrnlen(const char *s, size_t maxlen): Thestrnlen()function returns strlen(s), if that is less than maxlen, or maxlen if there is no '\0' character among the first maxlen characters pointed to by s."
Quoted 7 lines
Quoted 3 lines
How can
strnlen(name, sizeof(dev->name))be greater thansizeof(dev->name)?Shouldn't it be "if (
strnlen(name, sizeof(dev->name))==sizeof(dev->name))" instead?Not a big deal, but MO it's better to guard from everything that is not a good input by negating the check. strnlen()<
sizeof()is OK, strnlen()>=sizeof()is bad. Is "==" more preferable fornet/coding style?
Agreed, both cannot cause any troubles. == is supposed to be better from the API point of view, but >= is probably more readable.
Nicolas.
Quoted 4 lines · On Mon, 14 Feb 2011 13:56:06 +0300 Vasiliy Kulikov <[email protected]> wrote:
alloc_netdev()may be called with too long name (more that IFNAMSIZ bytes). Currently this leads toBUG(). Other insane inputs (bad txqs, rxqs) and even OOM don't lead toBUG(). Madealloc_netdev()return NULL, like on other errors.
The only way alloc_netdev could be called with a name too long was if some driver was incorrectly written. It is not something that can be exercised by user space.
Please leave the BUG() so the driver will show up in kernel oops logs etc.