diff mbox series

media: mt9p031: Check for non-null I2C ID match

Message ID 20220618222232.478138-1-marex@denx.de
State New
Headers show
Series media: mt9p031: Check for non-null I2C ID match | expand

Commit Message

Marek Vasut June 18, 2022, 10:22 p.m. UTC
The const struct i2c_device_id *did passed to probe may be NULL e.g.
in case DT contains the following compatible string:
compatible = "aptina,mt9p006", "aptina,mt9p031";

Check the variable for non-NULL and fail gracefully instead of crashing
the kernel outright with NULL pointer dereference splat.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
Cc: Stefan Riedmueller <s.riedmueller@phytec.de>
---
 drivers/media/i2c/mt9p031.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Laurent Pinchart June 18, 2022, 10:59 p.m. UTC | #1
Hi Marek,

(CC'ing Wolfram)

Thank you for the patch.

On Sun, Jun 19, 2022 at 12:22:32AM +0200, Marek Vasut wrote:
> The const struct i2c_device_id *did passed to probe may be NULL e.g.
> in case DT contains the following compatible string:
> compatible = "aptina,mt9p006", "aptina,mt9p031";
> 
> Check the variable for non-NULL and fail gracefully instead of crashing
> the kernel outright with NULL pointer dereference splat.
> 
> Signed-off-by: Marek Vasut <marex@denx.de>
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
> Cc: Stefan Riedmueller <s.riedmueller@phytec.de>
> ---
>  drivers/media/i2c/mt9p031.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
> index e0f0d87d4cfe9..d0706723e493c 100644
> --- a/drivers/media/i2c/mt9p031.c
> +++ b/drivers/media/i2c/mt9p031.c
> @@ -1106,6 +1106,11 @@ static int mt9p031_probe(struct i2c_client *client,
>  		return -EINVAL;
>  	}
>  
> +	if (did == NULL) {
> +		dev_err(&client->dev, "Invalid I2C device ID\n");
> +		return -EINVAL;
> +	}
> +

Is there a way we could fix this in the I2C core ? See
https://lore.kernel.org/all/20210723183114.26017-1-laurent.pinchart@ideasonboard.com/

>  	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
>  		dev_warn(&client->dev,
>  			"I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
diff mbox series

Patch

diff --git a/drivers/media/i2c/mt9p031.c b/drivers/media/i2c/mt9p031.c
index e0f0d87d4cfe9..d0706723e493c 100644
--- a/drivers/media/i2c/mt9p031.c
+++ b/drivers/media/i2c/mt9p031.c
@@ -1106,6 +1106,11 @@  static int mt9p031_probe(struct i2c_client *client,
 		return -EINVAL;
 	}
 
+	if (did == NULL) {
+		dev_err(&client->dev, "Invalid I2C device ID\n");
+		return -EINVAL;
+	}
+
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
 		dev_warn(&client->dev,
 			"I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");