Re: Container_of considered harmful - was Re: usb storage sddr09

From: Kevin O'Connor (kevin@koconnor.net)
Date: Tue Oct 22 2002 - 20:43:45 EST


On Sun, Oct 20, 2002 at 08:24:36PM +0200, Andries Brouwer wrote:
> The oops is a dereference of fffffff0 in base/bus.c:driver_attach().
> I have seen several such oopses lately, various places in the kernel.
> The cause here is a NULL pointer that is turned into fffffff0 by
> container_of() and then fed to get_device(). And get_device() tests
> that it gets a non-NULL pointer, but that does not protect against
> fffffff0.

Just as an anecdote - I built a variant of container_of to protect against
cases where NULL can creep in:

#define test_container_of(ptr, type, member) ({ \
        const typeof( ((type *)0)->member ) *__p = (ptr); \
        __p ? container_of(__p, type, member) : NULL;})

It calls the real container_of only if 'ptr' is not NULL.

-Kevin

-- 
 ------------------------------------------------------------------------
 | Kevin O'Connor                     "BTW, IMHO we need a FAQ for      |
 | kevin@koconnor.net                  'IMHO', 'FAQ', 'BTW', etc. !"    |
 ------------------------------------------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Oct 23 2002 - 22:01:01 EST