Skip to contentSkip to conversation

net: iosm: Prevent underflow in ipc_chnl_cfg_get()

PATCH Superseded Dan Carpenter 3 participants 5 messages Aug 16, 2021 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CANtMP6qvcE+u61+HUyEFNu15kQ02a1P5_mGRSHuyeCxkf4pQbA@mail.gmail.com

#1 lore raw

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
index 804e6c4f2c78..016c9c3aea8e 100644
@@ -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
#2 lore raw

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.c

drivers/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

#3 lore raw

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
index 804e6c4f2c78..016c9c3aea8e 100644
@@ -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
Reviewed-by on v2 1/1
#4 lore raw
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 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.

Quoted 8 lines

---
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(-)
#5 lore raw
In reply to Dan Carpenter
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 to ipc_chnl_cfg_get() so this patch does not affect runtime.

[...]

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