Skip to contentSkip to conversation

i2c: Fix NULL pointer dereference in npcm_i2c_reg_slave

PATCH In review Rand Deeb 4 participants 7 messages Jan 9, 2024 – Feb 5, 2024

Subsystems (2)
lore
Share & tools
lore

Rand Deeb

author
#0 lore raw

PATCH 1/1 i2c: Fix NULL pointer dereference in npcm_i2c_reg_slave

In the npcm_i2c_reg_slave function, a potential NULL pointer dereference issue occurs when 'client' is NULL. This patch adds a proper NULL check for 'client' at the beginning of the function to prevent undefined behavior.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 drivers/i2c/busses/i2c-npcm7xx.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-npcm7xx.c +5 -4
index c1b679737240..cfabfb50211d 100644
@@ -1243,13 +1243,14 @@ static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
 static int npcm_i2c_reg_slave(struct i2c_client *client)
 {
 	unsigned long lock_flags;
-	struct npcm_i2c *bus = i2c_get_adapdata(client->adapter);
-
-	bus->slave = client;
+	struct npcm_i2c *bus;
 
-	if (!bus->slave)
+	if (!client)
 		return -EINVAL;
 
+	bus = i2c_get_adapdata(client->adapter);
+	bus->slave = client;
+
 	if (client->flags & I2C_CLIENT_TEN)
 		return -EAFNOSUPPORT;
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Rand Deeb

In the npcm_i2c_reg_slave function, a potential NULL pointer dereference issue occurs when 'client' is NULL. This patch adds a proper NULL check for 'client' at the beginning of the function to prevent undefined behavior.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 drivers/i2c/busses/i2c-npcm7xx.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-npcm7xx.c +5 -5
index c1b679737240..cfabfb50211d 100644
@@ -1243,13 +1243,14 @@ static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
 static int npcm_i2c_reg_slave(struct i2c_client *client)
 {
        unsigned long lock_flags;
-       struct npcm_i2c *bus = i2c_get_adapdata(client->adapter);
-
-       bus->slave = client;
+       struct npcm_i2c *bus;
 
-       if (!bus->slave)
+       if (!client)
                return -EINVAL;
 
+       bus = i2c_get_adapdata(client->adapter);
+       bus->slave = client;
+
        if (client->flags & I2C_CLIENT_TEN)
                return -EAFNOSUPPORT;
 
--
Quoted 2 lines

2.34.1

Thanks for the patch!

Reviewed-by [email protected]
#2 lore raw
Quoted 36 lines · Tali Perry
Quoted 35 lines · Rand Deeb

In the npcm_i2c_reg_slave function, a potential NULL pointer dereference issue occurs when 'client' is NULL. This patch adds a proper NULL check for 'client' at the beginning of the function to prevent undefined behavior.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 drivers/i2c/busses/i2c-npcm7xx.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-npcm7xx.c +5 -4
index c1b679737240..cfabfb50211d 100644
@@ -1243,13 +1243,14 @@ static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus)
 static int npcm_i2c_reg_slave(struct i2c_client *client)
 {
        unsigned long lock_flags;
-       struct npcm_i2c *bus = i2c_get_adapdata(client->adapter);
-
-       bus->slave = client;
+       struct npcm_i2c *bus;
 
-       if (!bus->slave)
+       if (!client)
                return -EINVAL;
 
+       bus = i2c_get_adapdata(client->adapter);
+       bus->slave = client;
+
        if (client->flags & I2C_CLIENT_TEN)
                return -EAFNOSUPPORT;
 
Quoted 8 lines
Quoted 3 lines

--
2.34.1

Thanks for the patch!

Reviewed-by [email protected]

If I'm not missing something, npcm_i2c_reg_slave() is called via a function pointer ->reg_slave here [1]. And seems client can't be NULL there. Other drivers implementing ->reg_slave function don't check its argument.

Maybe we should just drop if (!bus->slave) check?

[1]: https://elixir.bootlin.com/linux/latest/source/drivers/i2c/i2c-core-slave.c#L48

#3 lore raw
Quoted 5 lines

If I'm not missing something, npcm_i2c_reg_slave() is called via a function pointer ->reg_slave here [1]. And seems client can't be NULL there. Other drivers implementing ->reg_slave function don't check its argument.

Correct, we trust ourselves here.

Quoted 1 line

Maybe we should just drop if (!bus->slave) check?

Yes.

#4 lore raw
Quoted 6 lines · Wolfram Sang
Quoted 5 lines

If I'm not missing something, npcm_i2c_reg_slave() is called via a function pointer ->reg_slave here [1]. And seems client can't be NULL there. Other drivers implementing ->reg_slave function don't check its argument.

Quoted 6 lines

Correct, we trust ourselves here.

Quoted 1 line

Maybe we should just drop if (!bus->slave) check?

Yes.

Okay, thanks for confirmation.

Rand, would you like to prepare the patch, please?

author
#5 lore raw
Quoted 10 lines · Fedor Pchelkin
Quoted 8 lines · Wolfram Sang
Quoted 5 lines

If I'm not missing something, npcm_i2c_reg_slave() is called via a function pointer ->reg_slave here [1]. And seems client can't be NULL there. Other drivers implementing ->reg_slave function don't check its argument.

Correct, we trust ourselves here.

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Maybe we should just drop if (!bus->slave) check?

Yes.

Okay, thanks for confirmation.

Rand, would you like to prepare the patch, please?

Hi Fedor!,

Sure, In fact, there were two scenarios from the beginning, either redundant condition or potential NULL pointer dereference.I relied on the condition to determine the type of issue because I did not find it logical to add a useless condition, but based on the Wolfram Sang words "we trust ourselves here." then the scenario will change to redundant condition, so i'll write a new patch and send it in new thread.

author
#6 lore raw
In reply to Fedor Pchelkin

Hi Fedor!,

Sure, In fact, there were two scenarios from the beginning, either redundant condition or potential NULL pointer dereference.I relied on the condition to determine the type of issue because I did not find it logical to add a useless condition, but based on the words Wolfram Sang "we trust ourselves here." then the scenario will change to redundant condition, so i'll write a new patch and send it in new thread.

Quoted 19 lines · Fedor Pchelkin
Quoted 6 lines · Wolfram Sang
Quoted 5 lines

If I'm not missing something, npcm_i2c_reg_slave() is called via a function pointer ->reg_slave here [1]. And seems client can't be NULL there. Other drivers implementing ->reg_slave function don't check its argument.

Quoted 6 lines

Correct, we trust ourselves here.

Quoted 1 line

Maybe we should just drop if (!bus->slave) check?

Yes.

Okay, thanks for confirmation.

Rand, would you like to prepare the patch, please?