Skip to contentSkip to conversation

event/octeontx: resolve possible integer overflow

PATCH Superseded Hanumanth Pothula 5 participants 11 messages Oct 18, 2024 – Oct 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 event/octeontx: resolve possible integer overflow

The last argument passed to ssovf_parsekv() is an unsigned char*, but it is accessed as an integer. This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

---
 drivers/event/octeontx/ssovf_evdev.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +10 -2
index 3a933b1db7..ccb447d33a 100644
@@ -719,8 +719,16 @@ ssovf_close(struct rte_eventdev *dev)
 static int
 ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = (uint8_t *)opaque;
+	char *end;
+
+	errno = 0;
+	*flag = (uint8_t)strtol(value, &end, 2);
+	if ((errno != 0) || (value == end)) {
+		ssovf_log_err("fail to get key val ret:%d err:%d", *flag, errno);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines

The last argument passed to ssovf_parsekv() is an unsigned char*, but it is accessed as an integer. This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

Quoted 31 lines
---
 drivers/event/octeontx/ssovf_evdev.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +10 -3
index 3a933b1db7..ccb447d33a 100644
@@ -719,8 +719,16 @@ ssovf_close(struct rte_eventdev *dev)
 static int
 ssovf_parsekv(const char *key __rte_unused, const char *value, void
 *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = (uint8_t *)opaque;
+	char *end;
+
+	errno = 0;
+	*flag = (uint8_t)strtol(value, &end, 2);
+	if ((errno != 0) || (value == end)) {
+		ssovf_log_err("fail to get key val ret:%d err:%d", *flag, errno);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
--

2.25.1

#2 lore raw
In reply to Hanumanth Pothula
Quoted 24 lines · On Fri, 18 Oct 2024 13:29:03 +0530 Hanumanth Pothula <[email protected]>…

The last argument passed to ssovf_parsekv() is an unsigned char*, but it is accessed as an integer. This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

---
 drivers/event/octeontx/ssovf_evdev.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +2 -2
index 3a933b1db7..ccb447d33a 100644
@@ -719,8 +719,16 @@ ssovf_close(struct rte_eventdev *dev)
 static int
 ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = (uint8_t *)opaque;
+	char *end;

drivers/event/octeontx/ssovf_evdev.c ssovf_close(struct rte_eventdev *dev) · line 733

Quoted 10 lines
drivers/event/octeontx/ssovf_evdev.c +8 -0
+
+	errno = 0;
+	*flag = (uint8_t)strtol(value, &end, 2);
+	if ((errno != 0) || (value == end)) {
+		ssovf_log_err("fail to get key val ret:%d err:%d", *flag, errno);
+		return -EINVAL;
+	}
+
 	return 0;
 }

Cast of opaque is unnecessary in C.
Use strtoul to avoid allowing negative numbers.
Passing 2 as argument makes it assume binary so 101 is legal value and returns 5 and it is not helping.

Why not:

drivers/event/octeontx/ssovf_evdev.c +13 -3
index 3a933b1db7..9804f5bc59 100644
@@ -717,10 +717,20 @@ ssovf_close(struct rte_eventdev *dev)
 }
 
 static int
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-       int *flag = opaque;
-       *flag = !!atoi(value);
+       uint8_t *flag = opaque;
+       unsigned long v;
+       char *end;
+
+       errno = 0;
+       v = strtoul(value, &end, 0);
+       if (errno != 0 || end == value || *end != '\0') {
+               ssvf_log_err("invalid %s value %s", key, value);
+               return -EINVAL;
+       }
+
+       *flag = !!v;
        return 0;
 }
 
#3 lore raw

PATCH v2 1/1 event/octeontx: resolve possible integer overflow

In reply to Hanumanth Pothula

The last argument passed to ssovf_parsekv() is an unsigned char*, but it is accessed as an integer. This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

---

v2: use strtoul instead of strtol

---
 drivers/event/octeontx/ssovf_evdev.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +13 -3
index 3a933b1db7..d2ab8011e1 100644
@@ -717,10 +717,20 @@ ssovf_close(struct rte_eventdev *dev)
 }
 
 static int
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = opaque;
+	uint64_t v;
+	char *end;
+
+	errno = 0;
+	v = (uint8_t)strtoul(value, &end, 0);
+	if ((errno != 0) || (value == end) || *end != '\0') {
+		ssovf_log_err("invalid %s value %s", key, value);
+		return -EINVAL;
+	}
+
+	*flag = !!v;
 	return 0;
 }
 
Signature · 1 line
2.25.1
#4 lore raw

drivers/event/octeontx/ssovf_evdev.c ssovf_close(struct rte_eventdev *dev) · line 727

Quoted 12 lines · On Wed, 23 Oct 2024 12:45:46 +0530 Hanumanth Pothula <[email protected]>…

static int

drivers/event/octeontx/ssovf_evdev.c +7 -3
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = opaque;
+	uint64_t v;
+	char *end;
+
+	errno = 0;
+	v = (uint8_t)strtoul(value, &end, 0);

Cast will cause truncation of large values.

Maybe:

	v = strtoul(value, &end, 0);
	if (errno != 0 || value == end || *end != '\0' || v > UINT8_MAX) {

...

Quoted 8 lines
drivers/event/octeontx/ssovf_evdev.c +6 -0
+	if ((errno != 0) || (value == end) || *end != '\0') {
+		ssovf_log_err("invalid %s value %s", key, value);
+		return -EINVAL;
+	}
+
+	*flag = !!v;
 	return 0;
 }
#5 lore raw

From: Stephen Hemminger <[email protected]>
Sent: Wednesday, October 23, 2024 9:46 PM
To: Hanumanth Reddy Pothula <[email protected]>
Cc: Jerin Jacob <[email protected]>; [email protected]; Harman Kalra <[email protected]>
Subject: [EXTERNAL] Re: [PATCH v2 1/1] event/octeontx: resolve possible integer overflow

On Wed, 23 Oct 2024 12: 45: 46 +0530 Hanumanth Pothula <hpothula@ marvell. com> wrote: > static int > -ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque) > +ssovf_parsekv(const char *key, const char *value,

On Wed, 23 Oct 2024 12:45:46 +0530

Quoted 13 lines · Hanumanth Pothula <[email protected]<mailto:[email protected]>> wrote:

static int

(quoted diff) +2 -3
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
 
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 
 {
 
-         int *flag = opaque;
 
-         *flag = !!atoi(value);
 
+        uint8_t *flag = opaque;

Review

Quoted 10 lines
(quoted diff) +5 -0
 
+        uint64_t v;
 
+        char *end;
 
+
 
+        errno = 0;
 
+        v = (uint8_t)strtoul(value, &end, 0);

Cast will cause truncation of large values.

Maybe:

              v = strtoul(value, &end, 0);

              if (errno != 0 || value == end || *end != '\0' || v > UINT8_MAX) {

...

Thanks for the review/comment.
Here, the value can only be ‘0’ or ‘1’, so truncation won’t be an issue.

Quoted 15 lines
(quoted diff) +6 -0
+        if ((errno != 0) || (value == end) || *end != '\0') {
 
+                       ssovf_log_err("invalid %s value %s", key, value);
 
+                       return -EINVAL;
 
+        }
 
+
 
+        *flag = !!v;
 
          return 0;
 
 }
#6 lore raw

PATCH v3 1/1 event/octeontx: resolve possible integer overflow

In reply to Hanumanth Pothula

The last argument passed to ssovf_parsekv() is an unsigned char*, but it is accessed as an integer. This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

---

v2: use strtoul instead of strtol
v3: Add value boundry check. Here, value can be either 0 or 1.

---
 drivers/event/octeontx/ssovf_evdev.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +13 -3
index 3a933b1db7..957fcab04e 100644
@@ -717,10 +717,20 @@ ssovf_close(struct rte_eventdev *dev)
 }
 
 static int
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = opaque;
+	uint64_t v;
+	char *end;
+
+	errno = 0;
+	v = strtoul(value, &end, 0);
+	if ((errno != 0) || (value == end) || *end != '\0' || v > 1) {
+		ssovf_log_err("invalid %s value %s", key, value);
+		return -EINVAL;
+	}
+
+	*flag = !!v;
 	return 0;
 }
 
Signature · 1 line
2.25.1
Tested-by on v3 1/1
#7 lore raw
Quoted 15 lines

-----Original Message-----
From: Hanumanth Pothula <[email protected]>
Sent: Thursday, October 24, 2024 6:55 AM
To: Jerin Jacob <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 1/1] event/octeontx: resolve possible integer overflow

The last argument passed to ssovf_parsekv() is an
unsigned char*, but it is accessed as an integer.
This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

Quoted 6 lines

---

v2: use strtoul instead of strtol
v3: Add value boundry check. Here, value can be either 0 or 1.
---

Confirmed that it resolves https://bugs.dpdk.org/show_bug.cgi?id=1512.

Thanks,
Ali

#8 lore raw
Quoted 9 lines · Ali Alnubani
Quoted 9 lines

-----Original Message-----
From: Hanumanth Pothula <[email protected]>
Sent: Thursday, October 24, 2024 6:55 AM
To: Jerin Jacob <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: [PATCH v3 1/1] event/octeontx: resolve possible integer overflow

Quoted 7 lines
Quoted 7 lines

The last argument passed to ssovf_parsekv() is an
unsigned char*, but it is accessed as an integer.
This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

Add
Bugzilla ID: 1512

And add Fixes: tag

please start the git commit subject with event/octeontx: fix ...

Good to merge next version

Quoted 16 lines
Quoted 6 lines

---

v2: use strtoul instead of strtol
v3: Add value boundry check. Here, value can be either 0 or 1.
---

Confirmed that it resolves https://bugs.dpdk.org/show_bug.cgi?id=1512.

Thanks,
Ali

#9 lore raw

PATCH v4 1/1 event/octeontx: fix possible integer overflow

In reply to Hanumanth Pothula

The last argument passed to ssovf_parsekv() is an
unsigned char*, but it is accessed as an integer.
This can lead to an integer overflow.

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

Bugzilla ID: 1512

Fixes 3516327e00fd ("event/octeontx: add selftest to device arguments")

---

v2: Use strtoul instead of strtol
v3: Add value boundry check. Here, value can be either 0 or 1.
v4: Commit text update

---
 drivers/event/octeontx/ssovf_evdev.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +13 -3
index 3a933b1db7..957fcab04e 100644
@@ -717,10 +717,20 @@ ssovf_close(struct rte_eventdev *dev)
 }
 
 static int
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-	int *flag = opaque;
-	*flag = !!atoi(value);
+	uint8_t *flag = opaque;
+	uint64_t v;
+	char *end;
+
+	errno = 0;
+	v = strtoul(value, &end, 0);
+	if ((errno != 0) || (value == end) || *end != '\0' || v > 1) {
+		ssovf_log_err("invalid %s value %s", key, value);
+		return -EINVAL;
+	}
+
+	*flag = !!v;
 	return 0;
 }
 
Signature · 1 line
2.25.1
Tested-by Ali Alnubani on v4 1/1 Applied
#10 lore raw

Re: [PATCH v4 1/1] event/octeontx: fix possible integer overflow

Quoted 5 lines · Hanumanth Pothula

The last argument passed to ssovf_parsekv() is an
unsigned char*, but it is accessed as an integer.
This can lead to an integer overflow.

Quoted 7 lines

Hence, make ensure the argument is accessed as a char and for better error handling use strtol instead of atoi.

Bugzilla ID: 1512

Fixes 3516327e00fd ("event/octeontx: add selftest to device arguments")

Missed following tag

Applied to dpdk-next-net-mrvl/for-main. Thanks

Quoted 40 lines

---

v2: Use strtoul instead of strtol
v3: Add value boundry check. Here, value can be either 0 or 1.
v4: Commit text update

---
 drivers/event/octeontx/ssovf_evdev.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/event/octeontx/ssovf_evdev.c +13 -4
index 3a933b1db7..957fcab04e 100644
@@ -717,10 +717,20 @@ ssovf_close(struct rte_eventdev *dev)
 }
 
 static int
-ssovf_parsekv(const char *key __rte_unused, const char *value, void *opaque)
+ssovf_parsekv(const char *key, const char *value, void *opaque)
 {
-       int *flag = opaque;
-       *flag = !!atoi(value);
+       uint8_t *flag = opaque;
+       uint64_t v;
+       char *end;
+
+       errno = 0;
+       v = strtoul(value, &end, 0);
+       if ((errno != 0) || (value == end) || *end != '\0' || v > 1) {
+               ssovf_log_err("invalid %s value %s", key, value);
+               return -EINVAL;
+       }
+
+       *flag = !!v;
        return 0;
 }
 
--

2.25.1