Skip to contentSkip to conversation

xenbus: fix possible crash in xenbus_uevent_backend

PATCH In review Olaf Hering 4 participants 6 messages Jul 18, 2011 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenbus: fix possible crash in xenbus_uevent_backend

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)
 		return -ENODEV;
 
 	/* stuff we want to pass to /sbin/hotplug */

Index: linux-3.0-rc7-xen-kexec/include/linux/compiler.h

#1 lore raw
Quoted 10 lines · Olaf Hering

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================

drivers/xen/xenbus/xenbus_probe_backend.c static int xenbus_uevent_backend(struct · line 108

Quoted 6 lines
drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)
 		return -ENODEV;

Is this fixing an actual crash which you observed of just something you noticed looking at the code?

container_of is pure pointer arithmetic without dereferencing so to get bus == NULL you'd need xdev == offsetof(struct xen_bus_type, bus) or some such.

I think the check of xdev is correct, although it might be clearer if it preceded the "bus = ... " it's not actively harmful where it is since container_of doesn't dereference the pointer.

Ian.

#2 lore raw
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 10 lines · Olaf Hering

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================

drivers/xen/xenbus/xenbus_probe_backend.c static int xenbus_uevent_backend(struct · line 108

Quoted 6 lines
drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)
 		return -ENODEV;

Is this fixing an actual crash which you observed of just something you noticed looking at the code?

Quoted 3 lines

container_of is pure pointer arithmetic without dereferencing so to get bus == NULL you'd need xdev == offsetof(struct xen_bus_type, bus) or some such.

-offsetof(struct xen_bus_type, bus)

Quoted 1 line

I think the check of xdev is correct, although it might be clearer if it

Not really, as it similarly is the result of a container_of().

Quoted 2 lines

preceded the "bus = ... " it's not actively harmful where it is since container_of doesn't dereference the pointer.

Doesn't? "xdev->dev.bus" very much looks like a de-reference to me.

Jan

#3 lore raw
Quoted 26 lines · Jan Beulich
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 10 lines · Olaf Hering

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================

drivers/xen/xenbus/xenbus_probe_backend.c static int xenbus_uevent_backend(struct · line 108

Quoted 6 lines
drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)
 		return -ENODEV;

Is this fixing an actual crash which you observed of just something you noticed looking at the code?

Quoted 3 lines

container_of is pure pointer arithmetic without dereferencing so to get bus == NULL you'd need xdev == offsetof(struct xen_bus_type, bus) or some such.

Quoted 5 lines

-offsetof(struct xen_bus_type, bus)

Quoted 1 line

I think the check of xdev is correct, although it might be clearer if it

Not really, as it similarly is the result of a container_of().

So it is, didn't spot that.

Quoted 4 lines
Quoted 2 lines

preceded the "bus = ... " it's not actively harmful where it is since container_of doesn't dereference the pointer.

Doesn't? "xdev->dev.bus" very much looks like a de-reference to me.

Oh, right. I'll argue that that's the parameter to container_of de-referencing, not the macro itself, to make myself look less dumb ;-P

Ian.

#4 lore raw
In reply to Ian Campbell
Quoted 12 lines · Ian Campbell
Quoted 11 lines · Olaf Hering

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================

drivers/xen/xenbus/xenbus_probe_backend.c +0 -0
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
Quoted 8 lines

drivers/xen/xenbus/xenbus_probe_backend.c static int xenbus_uevent_backend(struct · line 108

Quoted 5 lines
drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)
 		return -ENODEV;

Is this fixing an actual crash which you observed of just something you noticed looking at the code?

I was browsing the code.

Thanks to you and Jan for reviewing my attempt to fix something thats not broken. I will prepare a better patch.

Olaf

#5 lore raw
In reply to Olaf Hering
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Olaf Hering

Fix possible NULL pointer crash in xenbus_uevent_backend(). The variable to check for should probably be bus.

Index: linux-3.0-rc7-xen-kexec/drivers/xen/xenbus/xenbus_probe_backend.c
===================================================================

drivers/xen/xenbus/xenbus_probe_backend.c static int xenbus_uevent_backend(struct · line 107

Quoted 5 lines
drivers/xen/xenbus/xenbus_probe_backend.c +1 -1
@@ -104,7 +104,7 @@ static int xenbus_uevent_backend(struct
 
 	xdev = to_xenbus_device(dev);
 	bus = container_of(xdev->dev.bus, struct xen_bus_type, bus);
-	if (xdev == NULL)
+	if (bus == NULL)

How can the result of a container_of() be NULL if the passed in value is in any way meaningful (i.e. valid or NULL)? If any such check is really necessary, wouldn't you rather want to check xdev->dev.bus here?

Looking at the code (and its 2.6.18 tree's counterpart xenbus_uevent_frontend()), I would rather suspect that this wasn't meant to check bus in the first place, but instead needlessly tries to check the to_xenbus_device() result, which likewise can't reasonably be NULL (as that's again the result of a container_of()).

Jan

Quoted 3 lines
drivers/xen/xenbus/xenbus_probe_backend.c +0 -0
 		return -ENODEV;
 
 	/* stuff we want to pass to /sbin/hotplug */