Re: [PATCH] intel_menlow: set cdev after null device check to avoid null pointer dereference

From: Darren Hart
Date: Mon Mar 28 2016 - 14:21:13 EST


On Mon, Mar 28, 2016 at 05:18:39PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@xxxxxxxxxxxxx>
>
> intel_menlow_memory_remove sanity checks to see if device is null, however,
> this check is performed after we have already passed device into a call
> to acpi_driver_data. If device is null, then acpi_driver_data will produce
> a null pointer dereference on device. The correct action is to sanity check
> device, then assign cdev, then check if cdev is null.
>

Hrm, looking at this locally, that all makes sense.

Taking a step back however, I notice that intel_menlow_memory_remove is an ops
function pointer inside the acpi_driver structure itself, which is called from
acpi_device_remove() (and probe) (drivers/acpi/bus.c). This already verifies
acpi_driver is not NULL and can't get acpi_driver if acpi_device is NULL. So
unless there is some other use case for this callback I'm unaware of (certainly
possible) it appears to be totally redundant to do this checking here.

+Rafael - is there a best practices for these acpi callbacks with respect to
input validation?

> Signed-off-by: Colin Ian King <colin.king@xxxxxxxxxxxxx>
> ---
> drivers/platform/x86/intel_menlow.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/platform/x86/intel_menlow.c b/drivers/platform/x86/intel_menlow.c
> index 0a919d8..185a1bd 100644
> --- a/drivers/platform/x86/intel_menlow.c
> +++ b/drivers/platform/x86/intel_menlow.c
> @@ -196,9 +196,13 @@ static int intel_menlow_memory_add(struct acpi_device *device)
>
> static int intel_menlow_memory_remove(struct acpi_device *device)
> {
> - struct thermal_cooling_device *cdev = acpi_driver_data(device);
> + struct thermal_cooling_device *cdev;
> +
> + if (!device)
> + return -EINVAL;
>
> - if (!device || !cdev)
> + cdev = acpi_driver_data(device);
> + if (!cdev)
> return -EINVAL;
>
> sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
> --
> 2.7.4
>
>

--
Darren Hart
Intel Open Source Technology Center