Skip to contentSkip to conversation

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

PATCH In review Marek Vasut 2 participants 2 messages Jun 18, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: mt9p031: Check for non-null I2C ID match

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.

Cc Laurent Pinchart <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Stefan Riedmueller <[email protected]>
---
 drivers/media/i2c/mt9p031.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/media/i2c/mt9p031.c +5 -0
index e0f0d87d4cfe9..d0706723e493c 100644
@@ -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");
Signature · 1 line
2.35.1
maintainer
#1 lore raw

Hi Marek,

(CC'ing Wolfram)

Thank you for the patch.

Quoted 21 lines · Marek Vasut

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.

Cc Laurent Pinchart <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Stefan Riedmueller <[email protected]>
---
 drivers/media/i2c/mt9p031.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/media/i2c/mt9p031.c +0 -0
index e0f0d87d4cfe9..d0706723e493c 100644

drivers/media/i2c/mt9p031.c static int mt9p031_probe(struct i2c_client *client, · line 1113

Quoted 8 lines
drivers/media/i2c/mt9p031.c +5 -0
@@ -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/[email protected]/

Quoted 3 lines
drivers/media/i2c/mt9p031.c +0 -0
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
 		dev_warn(&client->dev,
 			"I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
Signature · 3 lines
Regards,

Laurent Pinchart