net: iosm: Prevent underflow in ipc_chnl_cfg_get()
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): CANtMP6qvcE+u61+HUyEFNu15kQ02a1P5_mGRSHuyeCxkf4pQbA@mail.gmail.com
PATCH 1/1 net: iosm: Prevent underflow in ipc_chnl_cfg_get()
The bounds check on "index" doesn't catch negative values. Using ARRAY_SIZE() directly is more readable and more robust because it prevents negative values for "index". Fortunately we only pass valid values to ipc_chnl_cfg_get() so this patch does not affect runtime.
---
I suspect this is bug report was based on static analysis. I had a Smatch check that used to print a warning, but then I modified it to only complain when the index was user controlled.
drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c +3 -4
@@ -64,10 +64,9 @@ static struct ipc_chnl_cfg modem_cfg[] = {int ipc_chnl_cfg_get (struct ipc_chnl_cfg *chnl_cfg,int index) { -int array_size =ARRAY_SIZE (modem_cfg); - -if (index >= array_size) { -pr_err ("index: %d and array_size %d" , index, array_size); +if (index >=ARRAY_SIZE (modem_cfg)) { +pr_err ("index: %d and array_size %lu" , index, +ARRAY_SIZE (modem_cfg));return -ECHRNG ; }
Signature · 1 line
2.20.1
Hi Dan,
drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c static struct ipc_chnl_cfg modem_cfg[] = { · line 67
Quoted 10 lines
+++
b/drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.cdrivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c +2
-4@@ -64,10 +64,9 @@ static struct ipc_chnl_cfg modem_cfg[] = {int ipc_chnl_cfg_get (struct ipc_chnl_cfg *chnl_cfg,int index) { -int array_size =ARRAY_SIZE (modem_cfg); - -if (index >= array_size) { -pr_err ("index: %d and array_size %d" , index, array_size); +if (index >=ARRAY_SIZE (modem_cfg)) { + pr_err("index: %d and array_size %lu", index,
array_size is removed so please change array_size in pr_err to array size (remove _).
Also change in pr_err array size format "%lu" is throwing warning [1] in 32bit env.
[1]
from ../drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c:6:
../drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c: In function 'ipc_chnl_cfg_get':../include/linux/kern_levels.h:5:18: warning: format '%lu' expects argument of type
'long unsigned int', but argument 3 has type 'unsigned int' [-Wformat=]
Regards,
Chetan
PATCH v2 1/1 net: iosm: Prevent underflow in ipc_chnl_cfg_get()
The bounds check on "index" doesn't catch negative values. Using ARRAY_SIZE() directly is more readable and more robust because it prevents negative values for "index". Fortunately we only pass valid values to ipc_chnl_cfg_get() so this patch does not affect runtime.
---
v2: Remove underscore between "array" and "size".
Use %zu print format specifier to fix a compile warning on 32 bit.
drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c +3 -4
@@ -64,10 +64,9 @@ static struct ipc_chnl_cfg modem_cfg[] = {int ipc_chnl_cfg_get (struct ipc_chnl_cfg *chnl_cfg,int index) { -int array_size =ARRAY_SIZE (modem_cfg); - -if (index >= array_size) { -pr_err ("index: %d and array_size %d" , index, array_size); +if (index >=ARRAY_SIZE (modem_cfg)) { +pr_err ("index: %d and array size %zu" , index, +ARRAY_SIZE (modem_cfg));return -ECHRNG ; }
Signature · 1 line
2.20.1
Quoted 18 lines
-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: Monday, August 16, 2021 4:44 PM
To: Kumar, M Chetan <[email protected]>; Solomon Ucko <[email protected]>
Cc: linuxwwan <[email protected]>; Loic Poulain <[email protected]>; Sergey Ryazanov <[email protected]>; Johannes Berg <[email protected]>; David S. Miller <[email protected]>; Jakub Kicinski <[email protected]>; [email protected]; [email protected]
Subject: [PATCH v2 net] net: iosm: Prevent underflow inipc_chnl_cfg_get()The bounds check on "index" doesn't catch negative values. Using
ARRAY_SIZE()directly is more readable and more robust because it prevents negative values for "index". Fortunately we only pass valid values toipc_chnl_cfg_get()so this patch does not affect runtime.
Quoted 8 lines
Reported-by Solomon Ucko <[email protected]>Signed-off-by Dan Carpenter <[email protected]>---
v2: Remove underscore between "array" and "size".
Use %zu print format specifier to fix a compile warning on 32 bit.drivers/net/wwan/iosm/iosm_ipc_chnl_cfg.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
This patch was applied to netdev/net.git (refs/heads/master):
Hello:
This patch was applied to netdev/net.git (refs/heads/master):
Quoted 10 lines · On Mon, 16 Aug 2021 14:13:33 +0300 you wrote:
The bounds check on "index" doesn't catch negative values. Using
ARRAY_SIZE()directly is more readable and more robust because it prevents negative values for "index". Fortunately we only pass valid values toipc_chnl_cfg_get()so this patch does not affect runtime.Reported-by Solomon Ucko <[email protected]>Signed-off-by Dan Carpenter <[email protected]>[...]
Here is the summary with links:
- [v2,net] net: iosm: Prevent underflow in ipc_chnl_cfg_get()
https://git.kernel.org/netdev/net/c/4f3f2e3fa043
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html