drivers: spi: fix deref ater null.might in spi-uclass.c
PATCH 1/1 drivers: spi: fix deref ater null.might in spi-uclass.c
From: Anton Moryakov <[email protected]>
The static analyzer (Svace) reported
After having been compared to a NULL value at spi-uclass.c:465, pointer 'dev' is passed as 1st parameter in call to function 'dev_get_flags' at spi-uclass.c:469, where it is dereferenced at device.h:240.
Correct explained:
1. Added dev && !device_active(dev) check before calling device_active()
2. Added explicit if (!dev) check with ret = -ENODEV setting
3. Protected logging in error block with if(dev) check
--- drivers/spi/spi-uclass.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/spi/spi-uclass.c +11 -3
@@ -345,7 +345,7 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,return ret; } -if (!device_active (dev)) { +if (dev && !device_active (dev)) {struct spi_slave *slave; ret =device_probe (dev);
@@ -355,6 +355,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp, slave->dev = dev; } +if (!dev) { + ret = -ENODEV ; + goto err; + } + slave =dev_get_parent_priv (dev); bus_data =dev_get_uclass_priv (bus);
@@ -373,8 +378,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,return 0 ; err: -log_debug ("%s: Error path, device '%s'\n" , __func__, dev->name); - +if (dev) +log_debug ("%s: Error path, device '%s'\n" , __func__, dev->name); +else +log_debug ("%s: Error path, NULL device\n" , __func__); +return ret; }
Signature · 1 line
2.30.2
Quoted 22 lines · On Fri, May 16, 2025 at 03:50:53PM +0300, [email protected] wrote:
From: Anton Moryakov <[email protected]>
The static analyzer (Svace) reported
After having been compared to a NULL value at spi-uclass.c:465, pointer 'dev' is passed as 1st parameter in call to function 'dev_get_flags' at spi-uclass.c:469, where it is dereferenced at device.h:240.Correct explained:
1. Added dev && !device_active(dev) check before callingdevice_active()
2. Added explicit if (!dev) check with ret =-ENODEVsetting
3. Protected logging in error block with if(dev) checkSigned-off-by Anton Moryakov <[email protected]>--- drivers/spi/spi-uclass.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/spi/spi-uclass.c +0
-0
drivers/spi/spi-uclass.c int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp, · line 351
Quoted 8 lines
drivers/spi/spi-uclass.c +1
-1@@ -345,7 +345,7 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,return ret; } -if (!device_active (dev)) { +if (dev && !device_active (dev)) {struct spi_slave *slave; ret =device_probe (dev);
Given that it looks like from a quick glance most places do not check for dev before dereferencing perhaps this should be done in dev_get_flags or so, and a follow-up for the places which had already been checking.
Signature · 1 line
Tom
Hi,
Quoted 58 lines · On Fri, 16 May 2025 at 14:51, <[email protected]> wrote:
From: Anton Moryakov <[email protected]>
The static analyzer (Svace) reported
After having been compared to a NULL value at spi-uclass.c:465, pointer 'dev' is passed as 1st parameter in call to function 'dev_get_flags' at spi-uclass.c:469, where it is dereferenced at device.h:240.Correct explained:
1. Added dev && !device_active(dev) check before callingdevice_active()
2. Added explicit if (!dev) check with ret =-ENODEVsetting
3. Protected logging in error block with if(dev) checkSigned-off-by Anton Moryakov <[email protected]>--- drivers/spi/spi-uclass.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/spi/spi-uclass.c +11
-4@@ -345,7 +345,7 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,return ret; } -if (!device_active (dev)) { +if (dev && !device_active (dev)) {struct spi_slave *slave; ret =device_probe (dev);@@ -355,6 +355,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp, slave->dev = dev; } +if (!dev) { + ret = -ENODEV ; + goto err; + } + slave =dev_get_parent_priv (dev); bus_data =dev_get_uclass_priv (bus);@@ -373,8 +378,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,return 0 ; err: -log_debug ("%s: Error path, device '%s'\n" , __func__, dev->name); - +if (dev) +log_debug ("%s: Error path, device '%s'\n" , __func__, dev->name); +else +log_debug ("%s: Error path, NULL device\n" , __func__); +return ret; } --
Quoted 2 lines
2.30.2
You are not allowed to pass NULL as dev to driver model functions. So the bug here is in the SPI code.
There is a code-size cost to checking arguments and we tend to be reluctant to pay it in U-Boot, where code size is often critical.
We do have assert() but it is fairly intrusive and people seldom enable it. If you have a better way, please suggest it!
Regards,
Simon
I can then leave only
+ if (!dev) {
+ ret = -ENODEV;
+ goto err;
+ }
+
but make it higher
How do you like this option?
пт, 16 мая 2025 г. в 19:20, Simon Glass <[email protected]>:
Quoted 81 lines
Hi,
Quoted 6 lines · On Fri, 16 May 2025 at 14:51, <[email protected]> wrote:
From: Anton Moryakov <[email protected]>
The static analyzer (Svace) reported
After having been compared to a NULL value at spi-uclass.c:465, pointer 'dev' is passed as 1st parameter in call to function'
dev_get_flags'drivers/spi/spi-uclass.c int spi_get_bus_and_cs(int busnum, int cs, struct
Quoted 17 lines
at spi-uclass.c:469, where it is dereferenced at device.h:240.
Correct explained:
1. Added dev && !device_active(dev) check before callingdevice_active()
2. Added explicit if (!dev) check with ret =-ENODEVsetting
3. Protected logging in error block with if(dev) checkSigned-off-by Anton Moryakov <[email protected]>--- drivers/spi/spi-uclass.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/spi/spi-uclass.c +0
-0@@ -345,7 +345,7 @@ int spi_get_bus_and_cs(int busnum, int cs, structudevice **busp,
drivers/spi/spi-uclass.c int spi_get_bus_and_cs(int busnum, int cs, struct
Quoted 9 lines
drivers/spi/spi-uclass.c +1
-1return ret; } -if (!device_active (dev)) { +if (dev && !device_active (dev)) {struct spi_slave *slave; ret =device_probe (dev);@@ -355,6 +355,11 @@ int spi_get_bus_and_cs(int busnum, int cs, structudevice **busp,
drivers/spi/spi-uclass.c int spi_get_bus_and_cs(int busnum, int cs, struct
Quoted 12 lines
drivers/spi/spi-uclass.c +5
-0slave->dev = dev; } +if (!dev) { + ret = -ENODEV ; + goto err; + } + slave =dev_get_parent_priv (dev); bus_data =dev_get_uclass_priv (bus);@@ -373,8 +378,11 @@ int spi_get_bus_and_cs(int busnum, int cs, structudevice **busp,
drivers/spi/spi-uclass.c int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
Quoted 7 lines
drivers/spi/spi-uclass.c +2
-2return 0 ; err: -log_debug ("%s: Error path, device '%s'\n" , __func__, dev->name); - +if (dev) + log_debug("%s: Error path, device '%s'\n", __func__,
dev->name);Quoted 9 lines
drivers/spi/spi-uclass.c +3
-1+else +log_debug ("%s: Error path, NULL device\n" , __func__); +return ret; } --2.30.2
You are not allowed to pass NULL as dev to driver model functions. So the bug here is in the SPI code.
There is a code-size cost to checking arguments and we tend to be reluctant to pay it in U-Boot, where code size is often critical.
We do have
assert()but it is fairly intrusive and people seldom enable it. If you have a better way, please suggest it!Regards,
Simon