Skip to content

Commit 10fa1b2

Browse files
committed
ACPI: bus: Avoid non-ACPI device objects in walks over children
When walking the children of an ACPI device, take extra care to avoid using to_acpi_device() on the ones that are not ACPI devices, because that may lead to out-of-bounds access and memory corruption. While at it, make the function passed to acpi_dev_for_each_child() take a struct acpi_device pointer argument (instead of a struct device one), so it is more straightforward to use. Fixes: b7dd629 ("ACPI: PM: Introduce acpi_dev_power_up_children_with_adr()") Reported-by: kernel test robot <[email protected]> BugLink: https://lore.kernel.org/lkml/20220420064725.GB16310@xsang-OptiPlex-9020/ Signed-off-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Mika Westerberg <[email protected]>
1 parent 62d5287 commit 10fa1b2

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

drivers/acpi/bus.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,10 +1070,30 @@ int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
10701070
}
10711071
EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
10721072

1073+
struct acpi_dev_walk_context {
1074+
int (*fn)(struct acpi_device *, void *);
1075+
void *data;
1076+
};
1077+
1078+
static int acpi_dev_for_one_check(struct device *dev, void *context)
1079+
{
1080+
struct acpi_dev_walk_context *adwc = context;
1081+
1082+
if (dev->bus != &acpi_bus_type)
1083+
return 0;
1084+
1085+
return adwc->fn(to_acpi_device(dev), adwc->data);
1086+
}
1087+
10731088
int acpi_dev_for_each_child(struct acpi_device *adev,
1074-
int (*fn)(struct device *, void *), void *data)
1089+
int (*fn)(struct acpi_device *, void *), void *data)
10751090
{
1076-
return device_for_each_child(&adev->dev, data, fn);
1091+
struct acpi_dev_walk_context adwc = {
1092+
.fn = fn,
1093+
.data = data,
1094+
};
1095+
1096+
return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
10771097
}
10781098

10791099
/* --------------------------------------------------------------------------

drivers/acpi/device_pm.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,8 @@ bool acpi_bus_power_manageable(acpi_handle handle)
425425
}
426426
EXPORT_SYMBOL(acpi_bus_power_manageable);
427427

428-
static int acpi_power_up_if_adr_present(struct device *dev, void *not_used)
428+
static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
429429
{
430-
struct acpi_device *adev;
431-
432-
adev = to_acpi_device(dev);
433430
if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
434431
return 0;
435432

include/acpi/acpi_bus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ extern struct bus_type acpi_bus_type;
482482

483483
int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data);
484484
int acpi_dev_for_each_child(struct acpi_device *adev,
485-
int (*fn)(struct device *, void *), void *data);
485+
int (*fn)(struct acpi_device *, void *), void *data);
486486

487487
/*
488488
* Events

0 commit comments

Comments
 (0)