public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ACPI: bus: Avoid non-ACPI device objects in walks over children
@ 2022-04-22 15:13 Rafael J. Wysocki
  2022-04-22 15:26 ` Mika Westerberg
  0 siblings, 1 reply; 3+ messages in thread
From: Rafael J. Wysocki @ 2022-04-22 15:13 UTC (permalink / raw)
  To: Linux ACPI; +Cc: LKML, Mika Westerberg, Bjorn Helgaas, Linux PM

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

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: b7dd6298db81 ("ACPI: PM: Introduce acpi_dev_power_up_children_with_adr()")
Reported-by: kernel test robot <oliver.sang@intel.com>
BugLink: https://lore.kernel.org/lkml/20220420064725.GB16310@xsang-OptiPlex-9020/
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

The commit being fixed is present in linux-next.

---
 drivers/acpi/bus.c       |   24 ++++++++++++++++++++++--
 drivers/acpi/device_pm.c |    5 +----
 include/acpi/acpi_bus.h  |    2 +-
 3 files changed, 24 insertions(+), 7 deletions(-)

Index: linux-pm/drivers/acpi/bus.c
===================================================================
--- linux-pm.orig/drivers/acpi/bus.c
+++ linux-pm/drivers/acpi/bus.c
@@ -1070,10 +1070,30 @@ int acpi_bus_for_each_dev(int (*fn)(stru
 }
 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
 
+struct acpi_dev_walk_context {
+	int (*fn)(struct acpi_device *, void *);
+	void *data;
+};
+
+static int acpi_dev_for_one_check(struct device *dev, void *context)
+{
+	struct acpi_dev_walk_context *adwc = context;
+
+	if (dev->bus != &acpi_bus_type)
+		return 0;
+
+	return adwc->fn(to_acpi_device(dev), adwc->data);
+}
+
 int acpi_dev_for_each_child(struct acpi_device *adev,
-			    int (*fn)(struct device *, void *), void *data)
+			    int (*fn)(struct acpi_device *, void *), void *data)
 {
-	return device_for_each_child(&adev->dev, data, fn);
+	struct acpi_dev_walk_context adwc = {
+		.fn = fn,
+		.data = data,
+	};
+
+	return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
 }
 
 /* --------------------------------------------------------------------------
Index: linux-pm/include/acpi/acpi_bus.h
===================================================================
--- linux-pm.orig/include/acpi/acpi_bus.h
+++ linux-pm/include/acpi/acpi_bus.h
@@ -482,7 +482,7 @@ extern struct bus_type acpi_bus_type;
 
 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data);
 int acpi_dev_for_each_child(struct acpi_device *adev,
-			    int (*fn)(struct device *, void *), void *data);
+			    int (*fn)(struct acpi_device *, void *), void *data);
 
 /*
  * Events
Index: linux-pm/drivers/acpi/device_pm.c
===================================================================
--- linux-pm.orig/drivers/acpi/device_pm.c
+++ linux-pm/drivers/acpi/device_pm.c
@@ -429,11 +429,8 @@ bool acpi_bus_power_manageable(acpi_hand
 }
 EXPORT_SYMBOL(acpi_bus_power_manageable);
 
-static int acpi_power_up_if_adr_present(struct device *dev, void *not_used)
+static int acpi_power_up_if_adr_present(struct acpi_device *adev, void *not_used)
 {
-	struct acpi_device *adev;
-
-	adev = to_acpi_device(dev);
 	if (!(adev->flags.power_manageable && adev->pnp.type.bus_address))
 		return 0;
 




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ACPI: bus: Avoid non-ACPI device objects in walks over children
  2022-04-22 15:13 [PATCH] ACPI: bus: Avoid non-ACPI device objects in walks over children Rafael J. Wysocki
@ 2022-04-22 15:26 ` Mika Westerberg
  2022-04-22 16:00   ` Rafael J. Wysocki
  0 siblings, 1 reply; 3+ messages in thread
From: Mika Westerberg @ 2022-04-22 15:26 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux ACPI, LKML, Bjorn Helgaas, Linux PM

Hi Rafael,

On Fri, Apr 22, 2022 at 05:13:48PM +0200, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> 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: b7dd6298db81 ("ACPI: PM: Introduce acpi_dev_power_up_children_with_adr()")
> Reported-by: kernel test robot <oliver.sang@intel.com>
> BugLink: https://lore.kernel.org/lkml/20220420064725.GB16310@xsang-OptiPlex-9020/
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
> 
> The commit being fixed is present in linux-next.
> 
> ---
>  drivers/acpi/bus.c       |   24 ++++++++++++++++++++++--
>  drivers/acpi/device_pm.c |    5 +----
>  include/acpi/acpi_bus.h  |    2 +-
>  3 files changed, 24 insertions(+), 7 deletions(-)
> 
> Index: linux-pm/drivers/acpi/bus.c
> ===================================================================
> --- linux-pm.orig/drivers/acpi/bus.c
> +++ linux-pm/drivers/acpi/bus.c
> @@ -1070,10 +1070,30 @@ int acpi_bus_for_each_dev(int (*fn)(stru
>  }
>  EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
>  
> +struct acpi_dev_walk_context {
> +	int (*fn)(struct acpi_device *, void *);
> +	void *data;
> +};
> +
> +static int acpi_dev_for_one_check(struct device *dev, void *context)
> +{
> +	struct acpi_dev_walk_context *adwc = context;
> +
> +	if (dev->bus != &acpi_bus_type)
> +		return 0;

I wonder if it make sense to add dev_is_acpi() that does the above
analoguos to dev_is_pci()?

Regardless of that,

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ACPI: bus: Avoid non-ACPI device objects in walks over children
  2022-04-22 15:26 ` Mika Westerberg
@ 2022-04-22 16:00   ` Rafael J. Wysocki
  0 siblings, 0 replies; 3+ messages in thread
From: Rafael J. Wysocki @ 2022-04-22 16:00 UTC (permalink / raw)
  To: Mika Westerberg
  Cc: Rafael J. Wysocki, Linux ACPI, LKML, Bjorn Helgaas, Linux PM

On Fri, Apr 22, 2022 at 5:27 PM Mika Westerberg
<mika.westerberg@linux.intel.com> wrote:
>
> Hi Rafael,
>
> On Fri, Apr 22, 2022 at 05:13:48PM +0200, Rafael J. Wysocki wrote:
> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >
> > 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: b7dd6298db81 ("ACPI: PM: Introduce acpi_dev_power_up_children_with_adr()")
> > Reported-by: kernel test robot <oliver.sang@intel.com>
> > BugLink: https://lore.kernel.org/lkml/20220420064725.GB16310@xsang-OptiPlex-9020/
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
> >
> > The commit being fixed is present in linux-next.
> >
> > ---
> >  drivers/acpi/bus.c       |   24 ++++++++++++++++++++++--
> >  drivers/acpi/device_pm.c |    5 +----
> >  include/acpi/acpi_bus.h  |    2 +-
> >  3 files changed, 24 insertions(+), 7 deletions(-)
> >
> > Index: linux-pm/drivers/acpi/bus.c
> > ===================================================================
> > --- linux-pm.orig/drivers/acpi/bus.c
> > +++ linux-pm/drivers/acpi/bus.c
> > @@ -1070,10 +1070,30 @@ int acpi_bus_for_each_dev(int (*fn)(stru
> >  }
> >  EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
> >
> > +struct acpi_dev_walk_context {
> > +     int (*fn)(struct acpi_device *, void *);
> > +     void *data;
> > +};
> > +
> > +static int acpi_dev_for_one_check(struct device *dev, void *context)
> > +{
> > +     struct acpi_dev_walk_context *adwc = context;
> > +
> > +     if (dev->bus != &acpi_bus_type)
> > +             return 0;
>
> I wonder if it make sense to add dev_is_acpi() that does the above
> analoguos to dev_is_pci()?

I thought about that, but this is the only place where it would be
needed ATM, so for now this isn't necessary.

> Regardless of that,
>
> Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-22 16:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-22 15:13 [PATCH] ACPI: bus: Avoid non-ACPI device objects in walks over children Rafael J. Wysocki
2022-04-22 15:26 ` Mika Westerberg
2022-04-22 16:00   ` Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox