Skip to content

Commit 3bb2a01

Browse files
Wang Haigregkh
authored andcommitted
kobject: Fix slab-out-of-bounds in fill_kobj_path()
In kobject_get_path(), if kobj->name is changed between calls get_kobj_path_length() and fill_kobj_path() and the length becomes longer, then fill_kobj_path() will have an out-of-bounds bug. The actual current problem occurs when the ixgbe probe. In ixgbe_mii_bus_init(), if the length of netdev->dev.kobj.name length becomes longer, out-of-bounds will occur. cpu0 cpu1 ixgbe_probe register_netdev(netdev) netdev_register_kobject device_add kobject_uevent // Sending ADD events systemd-udevd // rename netdev dev_change_name device_rename kobject_rename ixgbe_mii_bus_init | mdiobus_register | __mdiobus_register | device_register | device_add | kobject_uevent | kobject_get_path | len = get_kobj_path_length // old name | path = kzalloc(len, gfp_mask); | kobj->name = name; /* name length becomes * longer */ fill_kobj_path /* kobj path length is * longer than path, * resulting in out of * bounds when filling path */ This is the kasan report: ================================================================== BUG: KASAN: slab-out-of-bounds in fill_kobj_path+0x50/0xc0 Write of size 7 at addr ff1100090573d1fd by task kworker/28:1/673 Workqueue: events work_for_cpu_fn Call Trace: <TASK> dump_stack_lvl+0x34/0x48 print_address_description.constprop.0+0x86/0x1e7 print_report+0x36/0x4f kasan_report+0xad/0x130 kasan_check_range+0x35/0x1c0 memcpy+0x39/0x60 fill_kobj_path+0x50/0xc0 kobject_get_path+0x5a/0xc0 kobject_uevent_env+0x140/0x460 device_add+0x5c7/0x910 __mdiobus_register+0x14e/0x490 ixgbe_probe.cold+0x441/0x574 [ixgbe] local_pci_probe+0x78/0xc0 work_for_cpu_fn+0x26/0x40 process_one_work+0x3b6/0x6a0 worker_thread+0x368/0x520 kthread+0x165/0x1a0 ret_from_fork+0x1f/0x30 This reproducer triggers that bug: while: do rmmod ixgbe sleep 0.5 modprobe ixgbe sleep 0.5 When calling fill_kobj_path() to fill path, if the name length of kobj becomes longer, return failure and retry. This fixes the problem. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Wang Hai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 504fa21 commit 3bb2a01

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/kobject.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int get_kobj_path_length(const struct kobject *kobj)
112112
return length;
113113
}
114114

115-
static void fill_kobj_path(const struct kobject *kobj, char *path, int length)
115+
static int fill_kobj_path(const struct kobject *kobj, char *path, int length)
116116
{
117117
const struct kobject *parent;
118118

@@ -121,12 +121,16 @@ static void fill_kobj_path(const struct kobject *kobj, char *path, int length)
121121
int cur = strlen(kobject_name(parent));
122122
/* back up enough to print this name with '/' */
123123
length -= cur;
124+
if (length <= 0)
125+
return -EINVAL;
124126
memcpy(path + length, kobject_name(parent), cur);
125127
*(path + --length) = '/';
126128
}
127129

128130
pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
129131
kobj, __func__, path);
132+
133+
return 0;
130134
}
131135

132136
/**
@@ -141,13 +145,17 @@ char *kobject_get_path(const struct kobject *kobj, gfp_t gfp_mask)
141145
char *path;
142146
int len;
143147

148+
retry:
144149
len = get_kobj_path_length(kobj);
145150
if (len == 0)
146151
return NULL;
147152
path = kzalloc(len, gfp_mask);
148153
if (!path)
149154
return NULL;
150-
fill_kobj_path(kobj, path, len);
155+
if (fill_kobj_path(kobj, path, len)) {
156+
kfree(path);
157+
goto retry;
158+
}
151159

152160
return path;
153161
}

0 commit comments

Comments
 (0)