Skip to contentSkip to conversation

mfd: ene-kb3930: Fix potential NULL pointer dereference

PATCH Superseded Chenyuan Yang 3 participants 9 messages Jan 20, 2025 – Feb 13, 2025 Multifunction devices (MFD)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: ene-kb3930: Fix potential NULL pointer dereference

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
b1ba8bcb2d1ffce11b308ce166c9cc28d989e3b9 ("backlight: hx8357:Fix potential NULL pointer dereference").

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mfd/ene-kb3930.c +2 -0
index fa0ad2f14a39..60824d847bf0 100644
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
 			devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
 		if (IS_ERR(ddata->off_gpios))
 			return PTR_ERR(ddata->off_gpios);
+		if (!ddata->off_gpios)
+			return -ENOMEM;
 		if (ddata->off_gpios->ndescs < 2) {
 			dev_err(dev, "invalid off-gpios property\n");
 			return -EINVAL;
Signature · 1 line
2.34.1
#1 lore raw

Dear Developers,

I am writing to check if there has been any update on this issue. Given that it involves a potential null pointer dereference, I believe we should address it as soon as possible

Best,
Chenyuan

Quoted 27 lines · Chenyuan Yang

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
b1ba8bcb2d1ffce11b308ce166c9cc28d989e3b9 ("backlight: hx8357:Fix potential NULL pointer dereference").

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mfd/ene-kb3930.c +2 -1
index fa0ad2f14a39..60824d847bf0 100644
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
                        devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
                if (IS_ERR(ddata->off_gpios))
                        return PTR_ERR(ddata->off_gpios);
+               if (!ddata->off_gpios)
+                       return -ENOMEM;
                if (ddata->off_gpios->ndescs < 2) {
                        dev_err(dev, "invalid off-gpios property\n");
                        return -EINVAL;
--

2.34.1

#2 lore raw
Quoted 6 lines · Chenyuan Yang

Dear Developers,

I am writing to check if there has been any update on this issue. Given that it involves a potential null pointer dereference, I believe we should address it as soon as possible

How did you test it or reproduce the issue? You claim that it is ASAP and ping during the merge window, so I assume this is real issue and tested on device?

Best regards,
Krzysztof

#3 lore raw

Hi Krzysztof,

This was caught by our static analysis tool because devm_gpiod_get_array_optional() can return a NULL pointer in low-memory situations. It's essentially the same scenario described here:
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/.

Best,
Chenyuan

Quoted 14 lines · Krzysztof Kozlowski
Quoted 6 lines · Chenyuan Yang

Dear Developers,

I am writing to check if there has been any update on this issue. Given that it involves a potential null pointer dereference, I believe we should address it as soon as possible

How did you test it or reproduce the issue? You claim that it is ASAP and ping during the merge window, so I assume this is real issue and tested on device?

Best regards,
Krzysztof

#4 lore raw
Quoted 5 lines · Chenyuan Yang

Hi Krzysztof,

This was caught by our static analysis tool because devm_gpiod_get_array_optional() can return a NULL pointer in low-memory situations. It's essentially the same scenario described

Don't top post.

So you did not reproduce it, did not test it, thus I suggest not to ping us as we missed anything. Try to get this tested first.

Best regards,
Krzysztof

maintainer
#5 lore raw
In reply to Chenyuan Yang
Quoted 15 lines · Chenyuan Yang

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
b1ba8bcb2d1ffce11b308ce166c9cc28d989e3b9 ("backlight: hx8357:Fix potential NULL pointer dereference").

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/mfd/ene-kb3930.c +0 -0
index fa0ad2f14a39..60824d847bf0 100644

drivers/mfd/ene-kb3930.c static int kb3930_probe(struct i2c_client *client) · line 166

Quoted 5 lines
drivers/mfd/ene-kb3930.c +2 -0
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
 			devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
 		if (IS_ERR(ddata->off_gpios))
 			return PTR_ERR(ddata->off_gpios);
+		if (!ddata->off_gpios)
+			return -ENOMEM;

I don't see many other call sites checking for NULL - why is this different?

What about IS_ERR_OR_NULL() instead?

Quoted 6 lines
drivers/mfd/ene-kb3930.c +0 -0
 		if (ddata->off_gpios->ndescs < 2) {
 			dev_err(dev, "invalid off-gpios property\n");
 			return -EINVAL;
Signature · 1 line
2.34.1
Signature · 1 line
Lee Jones [李琼斯]
#6 lore raw
Quoted 12 lines · Lee Jones
Quoted 9 lines · Chenyuan Yang

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/> >

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 13 lines

drivers/mfd/ene-kb3930.c static int kb3930_probe(struct i2c_client *client) · line 166

Quoted 10 lines
drivers/mfd/ene-kb3930.c +2 -0
index fa0ad2f14a39..60824d847bf0 100644
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
                      devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
              if (IS_ERR(ddata->off_gpios))
                      return PTR_ERR(ddata->off_gpios);
+             if (!ddata->off_gpios)
+                     return -ENOMEM;

I don't see many other call sites checking for NULL - why is this different?

It looks like other places using devm_gpiod_get_array_optional do check for a NULL return value, such as in drivers/iio/resolver/ad2s1210.c, drivers/mtd/maps/physmap-core.c, and this patch commit:
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/.

Quoted 1 line

What about IS_ERR_OR_NULL() instead?

Yes, that sounds good. However, I have a question—what error number should be returned in this case?

Quoted 9 lines
Quoted 6 lines
drivers/mfd/ene-kb3930.c +0 -1
              if (ddata->off_gpios->ndescs < 2) {
                      dev_err(dev, "invalid off-gpios property\n");
                      return -EINVAL;
--

2.34.1

--
Lee Jones [李琼斯]

-Chenyuan

maintainer
#7 lore raw
Quoted 33 lines · Chenyuan Yang
Quoted 12 lines · Lee Jones
Quoted 9 lines · Chenyuan Yang

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/> >

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 13 lines

drivers/mfd/ene-kb3930.c static int kb3930_probe(struct i2c_client *client) · line 166

Quoted 10 lines
drivers/mfd/ene-kb3930.c +2 -0
index fa0ad2f14a39..60824d847bf0 100644
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
                      devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
              if (IS_ERR(ddata->off_gpios))
                      return PTR_ERR(ddata->off_gpios);
+             if (!ddata->off_gpios)
+                     return -ENOMEM;

I don't see many other call sites checking for NULL - why is this different?

It looks like other places using devm_gpiod_get_array_optional do check for a NULL return value, such as in drivers/iio/resolver/ad2s1210.c, drivers/mtd/maps/physmap-core.c, and this patch commit:
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/.

Quoted 4 lines
Quoted 1 line

What about IS_ERR_OR_NULL() instead?

Yes, that sounds good. However, I have a question—what error number should be returned in this case?

I just took a closer look and don't thing returning on NULL is correct.

The call is *_optional() meaning that NULL is an acceptable value.

I believe that this is the correct solution:

drivers/mfd/ene-kb3930.c +1 -1
index fa0ad2f14a39..9460a67acb0b 100644
@@ -162,7 +162,7 @@ static int kb3930_probe(struct i2c_client *client)
                        devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
                if (IS_ERR(ddata->off_gpios))
                        return PTR_ERR(ddata->off_gpios);
-               if (ddata->off_gpios->ndescs < 2) {
+               if (ddata->off_gpios && ddata->off_gpios->ndescs < 2) {
                        dev_err(dev, "invalid off-gpios property\n");
                        return -EINVAL;
                }
Signature · 1 line
Lee Jones [李琼斯]
#8 lore raw

Thanks so much for your insights!
I will resubmit the patch.

-Chenyuan

Quoted 63 lines · Lee Jones
Quoted 33 lines · Chenyuan Yang
Quoted 12 lines · Lee Jones
Quoted 9 lines · Chenyuan Yang

The off_gpios could be NULL. Add missing check in the kb3930_probe().
This is similar to the issue fixed in commit
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/> >

Fixes ede6b2d1dfc0 ("mfd: ene-kb3930: Add driver for ENE KB3930 Embedded Controller")
---
 drivers/mfd/ene-kb3930.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 13 lines

drivers/mfd/ene-kb3930.c static int kb3930_probe(struct i2c_client *client) · line 166

Quoted 10 lines
drivers/mfd/ene-kb3930.c +2 -0
index fa0ad2f14a39..60824d847bf0 100644
@@ -162,6 +162,8 @@ static int kb3930_probe(struct i2c_client *client)
                      devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
              if (IS_ERR(ddata->off_gpios))
                      return PTR_ERR(ddata->off_gpios);
+             if (!ddata->off_gpios)
+                     return -ENOMEM;

I don't see many other call sites checking for NULL - why is this different?

It looks like other places using devm_gpiod_get_array_optional do check for a NULL return value, such as in drivers/iio/resolver/ad2s1210.c, drivers/mtd/maps/physmap-core.c, and this patch commit:
https://patchwork.kernel.org/project/linux-fbdev/patch/[email protected]/.

Quoted 4 lines
Quoted 1 line

What about IS_ERR_OR_NULL() instead?

Yes, that sounds good. However, I have a question—what error number should be returned in this case?

I just took a closer look and don't thing returning on NULL is correct.

The call is *_optional() meaning that NULL is an acceptable value.

I believe that this is the correct solution:

drivers/mfd/ene-kb3930.c +1 -2
index fa0ad2f14a39..9460a67acb0b 100644
@@ -162,7 +162,7 @@ static int kb3930_probe(struct i2c_client *client)
                        devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
                if (IS_ERR(ddata->off_gpios))
                        return PTR_ERR(ddata->off_gpios);
-               if (ddata->off_gpios->ndescs < 2) {
+               if (ddata->off_gpios && ddata->off_gpios->ndescs < 2) {
                        dev_err(dev, "invalid off-gpios property\n");
                        return -EINVAL;
                }
 
--

Lee Jones [李琼斯]