Problems with setting devices' bus_id on sparc32
Hi,
While investigating Debian bug #394697 [0] on sparc32, I've found that in a couple of places the bus_id field of struct device is set by copying the dp->path_component_name (obtained from prom) without any length checking. In most cases it happens to work, however on SparcStation5 for some devices this string is over 20 chars long (like 'power-management@4,a0000000'), and that exceeds the amount of memory allocated for bus_id (which is BUS_ID_SIZE == 20). So, blindly copying the names into bus_id using strcpy() leads to all kinds of bad things. The attached hackish patch replaces strcpy() by strlcpy() in two places, making it possible to boot SS5 again (tested in QEMU, where vanilla 2.6.18 fails to boot with symptoms identical to the ones described in the bug).
[0] http://bugs.debian.org/394697
Best regards,
Signature · 2 lines
Jurij Smakov [email protected] Key: http://www.wooyd.org/pgpkey/ KeyID: C99E03CC
arch/sparc/kernel/of_device.c +1 -1
@@ -652,7 +652,7 @@if (!parent)strcpy (op->dev.bus_id,"root" );else -strcpy (op->dev.bus_id, dp->path_component_name); +strlcpy (op->dev.bus_id, dp->path_component_name,BUS_ID_SIZE );if (of_device_register (op)) { printk("%s: Could not register of device.\n",
drivers/sbus/sbus.c +1 -1
@@ -61,7 +61,7 @@else sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev; sdev->ofdev.dev.bus = &sbus_bus_type; -strcpy (sdev->ofdev.dev.bus_id, dp->path_component_name); +strlcpy (sdev->ofdev.dev.bus_id, dp->path_component_name,BUS_ID_SIZE );if (of_device_register (&sdev->ofdev) !=0 ) printk(KERN_DEBUG "sbus: device registration error for %s!\n",
David Miller
maintainerFrom: Jurij Smakov <[email protected]>
Date: Thu, 26 Oct 2006 23:55:03 -0700
Quoted 13 lines
While investigating Debian bug #394697 [0] on sparc32, I've found that in a couple of places the
bus_idfield of struct device is set by copying thedp->path_component_name(obtained from prom) without any length checking. In most cases it happens to work, however on SparcStation5 for some devices this string is over 20 chars long (like 'power-management@4,a0000000'), and that exceeds the amount of memory allocated forbus_id(which isBUS_ID_SIZE= 20). So, blindly copying the names intobus_idusingstrcpy()leads to all kinds of bad things. The attached hackish patch replacesstrcpy()bystrlcpy()in two places, making it possible to boot SS5 again (tested in QEMU, where vanilla 2.6.18 fails to boot with symptoms identical to the ones described in the bug).
Quoted 1 line
I happened to be playing around in this area recently.
I recently fixed the problem on sparc64 that the bus_id's being generated by of_device.c were not globally unique (the device layer requires that they are), so I changed it to "%s@%08x" using dp->name and dp->node.
But that will run into the same length issue.
So let's just use "%08x" and dp->node, with option fixed sized string prefix for ebus/isa/sbus, for all of these things.
arch/sparc/kernel/ebus.c +2 -2
@@ -237,12 +237,12 @@ void __init fill_ebus_device(struct devi dev->ofdev.node = dp; dev->ofdev.dev.parent = &dev->bus->ofdev.dev; dev->ofdev.dev.bus = &ebus_bus_type; -strcpy (dev->ofdev.dev.bus_id, dp->path_component_name); +sprintf (dev->ofdev.dev.bus_id,"ebus[%08x]" , dp->node);/* Register with core */ if (of_device_register (&dev->ofdev) !=0 )printk (KERN_DEBUG "ebus: device registration error for %s!\n" , - dev->ofdev.dev.bus_id); + dp->path_component_name);if ((dp = dp->child) !=NULL ) { dev->children = (struct linux_ebus_child *)
arch/sparc/kernel/of_device.c +1 -1
@@ -651,7 +651,7 @@ build_resources:if (!parent)strcpy (op->dev.bus_id,"root" );else -strcpy (op->dev.bus_id, dp->path_component_name); +sprintf (op->dev.bus_id,"%08x" , dp->node);if (of_device_register (op)) { printk("%s: Could not register of device.\n",
arch/sparc64/kernel/ebus.c +2 -2
@@ -389,12 +389,12 @@ static void __init fill_ebus_device(stru dev->ofdev.node = dp; dev->ofdev.dev.parent = &dev->bus->ofdev.dev; dev->ofdev.dev.bus = &ebus_bus_type; -strcpy (dev->ofdev.dev.bus_id, dp->path_component_name); +sprintf (dev->ofdev.dev.bus_id,"ebus[%08x]" , dp->node);/* Register with core */ if (of_device_register (&dev->ofdev) !=0 )printk (KERN_DEBUG "ebus: device registration error for %s!\n" , - dev->ofdev.dev.bus_id); + dp->path_component_name); dp = dp->child;if (dp) {
arch/sparc64/kernel/isa.c +2 -2
@@ -115,12 +115,12 @@ static void __init isa_fill_devices(stru isa_dev->ofdev.node = dp; isa_dev->ofdev.dev.parent = &isa_br->ofdev.dev; isa_dev->ofdev.dev.bus = &isa_bus_type; -strcpy (isa_dev->ofdev.dev.bus_id, dp->path_component_name); +sprintf (isa_dev->ofdev.dev.bus_id,"isa[%08x]" , dp->node);/* Register with core */ if (of_device_register (&isa_dev->ofdev) !=0 ) {printk (KERN_DEBUG "isa: device registration error for %s!\n" , - isa_dev->ofdev.dev.bus_id); + dp->path_component_name);kfree (isa_dev); goto next_sibling; }
arch/sparc64/kernel/of_device.c +1 -1
@@ -861,7 +861,7 @@ static struct of_device * __init scan_onif (!parent)strcpy (op->dev.bus_id,"root" );else -sprintf (op->dev.bus_id,"%s@%08x" , dp->name, dp->node); +sprintf (op->dev.bus_id,"%08x" , dp->node);if (of_device_register (op)) { printk("%s: Could not register of device.\n",
drivers/sbus/sbus.c +2 -2
@@ -61,11 +61,11 @@ static void __init fill_sbus_device(struelse sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev; sdev->ofdev.dev.bus = &sbus_bus_type; -strcpy (sdev->ofdev.dev.bus_id, dp->path_component_name); +sprintf (sdev->ofdev.dev.bus_id,"sbus[%08x]" , dp->node);if (of_device_register (&sdev->ofdev) !=0 )printk (KERN_DEBUG "sbus: device registration error for %s!\n" , - sdev->ofdev.dev.bus_id); + dp->path_component_name); }static void __init sbus_bus_ranges_init (struct device_node *dp,struct sbus_bus *sbus)