Skip to contentSkip to conversation

iio: hmc: fix a potential NULL pointer dereference

PATCH In review Kangjie Lu 4 participants 7 messages Mar 9, 2019 – Mar 24, 2019 1 message not in the archive IIO

lore
Share & tools
lore

Kangjie Lu

author
#0 lore raw

PATCH 1/1 iio: hmc: fix a potential NULL pointer dereference

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/magnetometer/hmc5843_i2c.c +6 -1
index 3de7f4426ac4..c0cd0823f8d5 100644
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *devm_regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(devm_regmap))
+		return PTR_ERR(devm_regmap);
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			devm_regmap,
 			id->driver_data, id->name);
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 13 lines · Kangjie Lu

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/magnetometer/hmc5843_i2c.c +0 -0
index 3de7f4426ac4..c0cd0823f8d5 100644

drivers/iio/magnetometer/hmc5843_i2c.c static const struct regmap_config hmc5843_i2c_regmap_config = { · line 64

Quoted 7 lines
drivers/iio/magnetometer/hmc5843_i2c.c +4 -0
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *devm_regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(devm_regmap))
+		return PTR_ERR(devm_regmap);

This fixes only one part of the problem leaving identical spi issue behind. I guess this check should be part of *common_probe().

Quoted 10 lines
drivers/iio/magnetometer/hmc5843_i2c.c +2 -2
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			devm_regmap,
 			id->driver_data, id->name);
 }
 
--

2.17.1

maintainer
#2 lore raw
Quoted 9 lines · On Sat, 9 Mar 2019 14:42:09 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 8 lines · Kangjie Lu

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
Quoted 15 lines

drivers/iio/magnetometer/hmc5843_i2c.c static const struct regmap_config hmc5843_i2c_regmap_config = { · line 64

Quoted 12 lines
drivers/iio/magnetometer/hmc5843_i2c.c +4 -0
index 3de7f4426ac4..c0cd0823f8d5 100644
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *devm_regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(devm_regmap))
+		return PTR_ERR(devm_regmap);  

This fixes only one part of the problem leaving identical spi issue behind. I guess this check should be part of *common_probe().

It could be, but that somewhat hides the error checking.

I'd prefer to see it fixed like this, but in both here and the spi file. Both this and spi in one patch would be great!

Thanks,

Jonathan

Quoted 11 lines
Quoted 11 lines
drivers/iio/magnetometer/hmc5843_i2c.c +2 -2
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			devm_regmap,
 			id->driver_data, id->name);
 }
 
--

2.17.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#4 lore raw
Quoted 29 lines · On Tue, 12 Mar 2019 02:19:25 -0500 Kangjie Lu <[email protected]> wrote:
Quoted 29 lines · Jonathan Cameron
Quoted 24 lines · On Sat, 9 Mar 2019 14:42:09 +0100 Tomasz Duszynski <[email protected]…
Quoted 13 lines · Kangjie Lu

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

---
drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iio/magnetometer/hmc5843_i2c.c +0 -0
index 3de7f4426ac4..c0cd0823f8d5 100644

drivers/iio/magnetometer/hmc5843_i2c.c static const struct regmap_config hmc5843_i2c_regmap_config = { · line 64

Quoted 7 lines
drivers/iio/magnetometer/hmc5843_i2c.c +4 -0
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *devm_regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(devm_regmap))
+		return PTR_ERR(devm_regmap);    

This fixes only one part of the problem leaving identical spi issue behind. I guess this check should be part of *common_probe().

Quoted 7 lines
Quoted 4 lines

It could be, but that somewhat hides the error checking.

I'd prefer to see it fixed like this, but in both here and the spi file. Both this and spi in one patch would be great!

I’ve submitted the patch for spi as well.

Ah. I missed it because of inconsistent patch naming. Anyhow, there were some minor suggestions for that patch so please send a v2 with the two combined.

Thanks

Jonathan

Quoted 16 lines
Quoted 15 lines

Thanks,

Jonathan

Quoted 10 lines
Quoted 10 lines
drivers/iio/magnetometer/hmc5843_i2c.c +2 -2
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			devm_regmap,
 			id->driver_data, id->name);
 }
 
--

2.17.1

author
#5 lore raw

PATCH v2 1/1 iio: hmc5843: fix potential NULL pointer dereferences

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

---
V2: fix the two together

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 drivers/iio/magnetometer/hmc5843_spi.c | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/hmc5843_i2c.c +6 -1
index 3de7f4426ac4..86abba5827a2 100644
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(regmap);
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }
 
drivers/iio/magnetometer/hmc5843_spi.c +6 -1
index 535f03a70d63..8355713651d4 100644
@@ -58,6 +58,7 @@ static const struct regmap_config hmc5843_spi_regmap_config = {
 static int hmc5843_spi_probe(struct spi_device *spi)
 {
 	int ret;
+	struct regmap *regmap;
 	const struct spi_device_id *id = spi_get_device_id(spi);
 
 	spi->mode = SPI_MODE_3;
@@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	regmap = devm_regmap_init(spi, &hmc5843_spi_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(devm_regmap);
+
 	return hmc5843_common_probe(&spi->dev,
-			devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }
 
Signature · 1 line
2.17.1
maintainer
#6 lore raw

Re: [PATCH v2] iio: hmc5843: fix potential NULL pointer dereferences

Quoted 4 lines · On Sat, 16 Mar 2019 17:08:33 -0500 Kangjie Lu <[email protected]> wrote:

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.
Thanks.

Jonathan

Quoted 52 lines

---
V2: fix the two together

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 drivers/iio/magnetometer/hmc5843_spi.c | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/hmc5843_i2c.c +6 -1
index 3de7f4426ac4..86abba5827a2 100644
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(regmap);
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }
 
drivers/iio/magnetometer/hmc5843_spi.c +6 -1
index 535f03a70d63..8355713651d4 100644
@@ -58,6 +58,7 @@ static const struct regmap_config hmc5843_spi_regmap_config = {
 static int hmc5843_spi_probe(struct spi_device *spi)
 {
 	int ret;
+	struct regmap *regmap;
 	const struct spi_device_id *id = spi_get_device_id(spi);
 
 	spi->mode = SPI_MODE_3;
@@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	regmap = devm_regmap_init(spi, &hmc5843_spi_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(devm_regmap);
+
 	return hmc5843_common_probe(&spi->dev,
-			devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }
 
#7 lore raw

Re: [PATCH v2] iio: hmc5843: fix potential NULL pointer dereferences

Quoted 10 lines · On Sun, 24 Mar 2019 10:48:56 +0000 Jonathan Cameron <[email protected]> wrote:
Quoted 4 lines · On Sat, 16 Mar 2019 17:08:33 -0500 Kangjie Lu <[email protected]> wrote:

devm_regmap_init_i2c may fail and return NULL. The fix returns the error when it fails.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.
Thanks.

Ah, how the eye jumps over the obvious...

Good thing this one was caught by build tests.

See below.

I've fixed up in the tree.

Jonathan

Quoted 40 lines

Jonathan

Quoted 37 lines

---
V2: fix the two together

---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++++++-
 drivers/iio/magnetometer/hmc5843_spi.c | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/iio/magnetometer/hmc5843_i2c.c +6 -1
index 3de7f4426ac4..86abba5827a2 100644
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config = {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 			     const struct i2c_device_id *id)
 {
+	struct regmap *regmap = devm_regmap_init_i2c(cli,
+			&hmc5843_i2c_regmap_config);
+	if (IS_ERR(regmap))
+		return PTR_ERR(regmap);
+
 	return hmc5843_common_probe(&cli->dev,
-			devm_regmap_init_i2c(cli, &hmc5843_i2c_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }
 
drivers/iio/magnetometer/hmc5843_spi.c +1 -0
index 535f03a70d63..8355713651d4 100644
@@ -58,6 +58,7 @@ static const struct regmap_config hmc5843_spi_regmap_config = {
 static int hmc5843_spi_probe(struct spi_device *spi)
 {
 	int ret;
+	struct regmap *regmap;
 	const struct spi_device_id *id = spi_get_device_id(spi);
 
Quoted 6 lines

drivers/iio/magnetometer/hmc5843_spi.c static int hmc5843_spi_probe(struct spi_device *spi) · line 71

Quoted 6 lines
drivers/iio/magnetometer/hmc5843_spi.c +1 -0
 	spi->mode = SPI_MODE_3;
@@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	regmap = devm_regmap_init(spi, &hmc5843_spi_regmap_config);

devm_regmap_init_spi

Quoted 2 lines

drivers/iio/magnetometer/hmc5843_spi.c static int hmc5843_spi_probe(struct spi_device *spi) · line 73

Quoted 2 lines
drivers/iio/magnetometer/hmc5843_spi.c +2 -0
+	if (IS_ERR(regmap))
+		return PTR_ERR(devm_regmap);

It's called regmap.

Quoted 8 lines
Quoted 7 lines
drivers/iio/magnetometer/hmc5843_spi.c +2 -1
+
 	return hmc5843_common_probe(&spi->dev,
-			devm_regmap_init_spi(spi, &hmc5843_spi_regmap_config),
+			regmap,
 			id->driver_data, id->name);
 }