[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <4BF26926.4070507@free.fr>
Date: Tue, 18 May 2010 12:17:10 +0200
From: Daniel Lezcano <daniel.lezcano@...e.fr>
To: Octavian Purdila <opurdila@...acom.com>
CC: Linux Netdev List <netdev@...r.kernel.org>
Subject: dev_get_valid_name buggy with hash collision
Hi all,
the commit:
commit d90310243fd750240755e217c5faa13e24f41536
Author: Octavian Purdila <opurdila@...acom.com>
Date: Wed Nov 18 02:36:59 2009 +0000
net: device name allocation cleanups
introduced a bug when there is a hash collision making impossible to
rename a device with eth%d
This bug is very hard to reproduce and appears rarely, but finally I
succeed to reproduce it with the program in attachment which fail to
rename a device with errno ENFILE.
The test program creates a new network namespace in order to avoid
messing the real network and to be sure we don't have any ethernet
devices. Hence, we know if we create one ethernet device with the name
eth%d the result will be 'eth0'.
The first step is to find a conflicting name with 'eth0':
1) We compute the hash of 'eth0' with the hashing functions imported
from the kernel
2) We create a temporary name, compute its hash
3) We compare the hash with the one we found for 'eth0'.
We loop until the hashes are different. When they are the same, then the
temporary name is a conflicting name.
We create a dummy device with the temporary conflicting name and then we
try to rename it with 'eth%d' (expecting 'eth0'), that fails with ENFILE
due to the kernel bug.
From the kernel POV, this is what happen:
dev_change_name does:
---------------------
[ ... ]
dev_get_valid_name(net, newname, dev->name, 1);
[ ... ]
Note the dev->name parameter and newname is 'eth%d'.
dev_get_valid_name does:
------------------------
[ ... ]
if (fmt && strchr(name, '%'))
return __dev_alloc_name(net, name, buf);
[ ... ]
Note the 'buf' parameter is the 'dev->name' parameter and 'name' is "eth%d"
__dev_alloc_name does:
----------------------
[ ... ]
for_each_netdev(net, d) {
if (!sscanf(d->name, name, &i))
continue;
if (i < 0 || i >= max_netdevices)
continue;
/* avoid cases where sscanf is not exact inverse of printf */
snprintf(buf, IFNAMSIZ, name, i);
if (!strncmp(buf, d->name, IFNAMSIZ))
set_bit(i, inuse);
}
[ ... ]
Here the buf parameter is 'dev->name', so while we are browsing the
network devices, we change the name of our eth device we want to rename.
But in the context of our test program, that does not happen because
there is no "eth[0-9]" network devices in the namespace, then we exit
the loop with 'i == 0'.
Right after we do:
if (buf != name)
snprintf(buf, IFNAMSIZ, name, i);
Here buf and name pointers are different, so we modify 'buf' which is
'dev->name', that is the network device name directly. So we have
'dev->name' == "eth0" after this line.
So right after we are trying to find ourself :)
[ ... ]
if (!__dev_get_by_name(net, buf))
return i;
[ ... ]
When hashing are the same for the oldname and the newname, the function
'__dev_get_by_name':
[ ... ]
struct hlist_head *head = dev_name_hash(net, name);
hlist_for_each_entry(dev, p, head, name_hlist)
if (!strncmp(dev->name, name, IFNAMSIZ))
return dev;
[ ... ]
will find the network device because we do __dev_get_by_name(net,
"eth0"), the dev->name is already modified with "eth0" and the hashing
of the temporary name and "eth0" are the same so returning the same hash
entry.
We are lucky, most of the time, because the name of the network device
and the new name have different hash entry, so we compare to ourself
very rarely.
IMO, the bug is to pass the 'dev->name' parameter to __dev_alloc_name
directly instead of a temporary name.
The patch in attachment fix the problem but I am not sure we shouldn't
go further and do more cleanup around this bug, so you may consider it
as a RFC more than a fix. If it is acceptable, I will send it as a patch
against net-2.6.
Thanks
-- Daniel
View attachment "myethash.c" of type "text/x-csrc" (2623 bytes)
View attachment "fix-dev_get_valid_name.patch" of type "text/x-diff" (2316 bytes)
Powered by blists - more mailing lists