Skip to content

Commit a58015d

Browse files
dcuirafaeljw
authored andcommitted
ACPI: scan: Harden acpi_device_add() against device ID overflows
Linux VM on Hyper-V crashes with the latest mainline: [ 4.069624] detected buffer overflow in strcpy [ 4.077733] kernel BUG at lib/string.c:1149! .. [ 4.085819] RIP: 0010:fortify_panic+0xf/0x11 ... [ 4.085819] Call Trace: [ 4.085819] acpi_device_add.cold.15+0xf2/0xfb [ 4.085819] acpi_add_single_object+0x2a6/0x690 [ 4.085819] acpi_bus_check_add+0xc6/0x280 [ 4.085819] acpi_ns_walk_namespace+0xda/0x1aa [ 4.085819] acpi_walk_namespace+0x9a/0xc2 [ 4.085819] acpi_bus_scan+0x78/0x90 [ 4.085819] acpi_scan_init+0xfa/0x248 [ 4.085819] acpi_init+0x2c1/0x321 [ 4.085819] do_one_initcall+0x44/0x1d0 [ 4.085819] kernel_init_freeable+0x1ab/0x1f4 This is because of the recent buffer overflow detection in the commit 6a39e62 ("lib: string.h: detect intra-object overflow in fortified string functions") Here acpi_device_bus_id->bus_id can only hold 14 characters, while the the acpi_device_hid(device) returns a 22-char string "HYPER_V_GEN_COUNTER_V1". Per ACPI Spec v6.2, Section 6.1.5 _HID (Hardware ID), if the ID is a string, it must be of the form AAA#### or NNNN####, i.e. 7 chars or 8 chars. The field bus_id in struct acpi_device_bus_id was originally defined as char bus_id[9], and later was enlarged to char bus_id[15] in 2007 in the commit bb09585 ("ACPI: use more understandable bus_id for ACPI devices") Fix the issue by changing the field bus_id to const char *, and use kstrdup_const() to initialize it. Signed-off-by: Dexuan Cui <decui@microsoft.com> Tested-By: Jethro Beekman <jethro@fortanix.com> [ rjw: Subject change, whitespace adjustment ] Cc: All applicable <stable@vger.kernel.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 7c53f6b commit a58015d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

drivers/acpi/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void acpi_scan_table_handler(u32 event, void *table, void *context);
9797
extern struct list_head acpi_bus_id_list;
9898

9999
struct acpi_device_bus_id {
100-
char bus_id[15];
100+
const char *bus_id;
101101
unsigned int instance_no;
102102
struct list_head node;
103103
};

drivers/acpi/scan.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ static void acpi_device_del(struct acpi_device *device)
486486
acpi_device_bus_id->instance_no--;
487487
else {
488488
list_del(&acpi_device_bus_id->node);
489+
kfree_const(acpi_device_bus_id->bus_id);
489490
kfree(acpi_device_bus_id);
490491
}
491492
break;
@@ -674,7 +675,14 @@ int acpi_device_add(struct acpi_device *device,
674675
}
675676
if (!found) {
676677
acpi_device_bus_id = new_bus_id;
677-
strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
678+
acpi_device_bus_id->bus_id =
679+
kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
680+
if (!acpi_device_bus_id->bus_id) {
681+
pr_err(PREFIX "Memory allocation error for bus id\n");
682+
result = -ENOMEM;
683+
goto err_free_new_bus_id;
684+
}
685+
678686
acpi_device_bus_id->instance_no = 0;
679687
list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
680688
}
@@ -709,6 +717,11 @@ int acpi_device_add(struct acpi_device *device,
709717
if (device->parent)
710718
list_del(&device->node);
711719
list_del(&device->wakeup_list);
720+
721+
err_free_new_bus_id:
722+
if (!found)
723+
kfree(new_bus_id);
724+
712725
mutex_unlock(&acpi_device_lock);
713726

714727
err_detach:

0 commit comments

Comments
 (0)