Skip to contentSkip to conversation

selftests/bpf: Fix issues in parse_num_list()

PATCH Superseded Yuntao Wang 3 participants 6 messages Apr 4, 2022 – Apr 6, 2022

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Fix issues in parse_num_list()

There are some issues in parse_num_list():

1. The end variable is assigned twice when parsing_end is true.
2. The function does not check that parsing_end should finally be false.

Clean up parse_num_list() and fix these issues.

---
 tools/testing/selftests/bpf/testing_helpers.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/testing_helpers.c +9 -9
index 795b6798ccee..82f0e2d99c23 100644
@@ -20,16 +20,16 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 		if (errno)
 			return -errno;
 
-		if (parsing_end)
-			end = num;
-		else
+		if (!parsing_end) {
 			start = num;
+			if (*next == '-') {
+				s = next + 1;
+				parsing_end = true;
+				continue;
+			}
+		}
 
-		if (!parsing_end && *next == '-') {
-			s = next + 1;
-			parsing_end = true;
-			continue;
-		} else if (*next == ',') {
+		if (*next == ',') {
 			parsing_end = false;
 			s = next + 1;
 			end = num;
@@ -60,7 +60,7 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 			set[i] = true;
 	}
 
-	if (!set)
+	if (!set || parsing_end)
 		return -EINVAL;
 
 	*num_set = set;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 7 lines · Yuntao Wang

There are some issues in parse_num_list():

1. The end variable is assigned twice when parsing_end is true.
2. The function does not check that parsing_end should finally be false.

Clean up parse_num_list() and fix these issues.

It would be great to also explain user-visible bug. What do you do to trigger bugs? Can you please put that into the commit message, in a before/after fashion? Thanks!

Quoted 47 lines
---
 tools/testing/selftests/bpf/testing_helpers.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/testing_helpers.c +9 -10
index 795b6798ccee..82f0e2d99c23 100644
@@ -20,16 +20,16 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
                if (errno)
                        return -errno;
 
-               if (parsing_end)
-                       end = num;
-               else
+               if (!parsing_end) {
                        start = num;
+                       if (*next == '-') {
+                               s = next + 1;
+                               parsing_end = true;
+                               continue;
+                       }
+               }
 
-               if (!parsing_end && *next == '-') {
-                       s = next + 1;
-                       parsing_end = true;
-                       continue;
-               } else if (*next == ',') {
+               if (*next == ',') {
                        parsing_end = false;
                        s = next + 1;
                        end = num;
@@ -60,7 +60,7 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
                        set[i] = true;
        }
 
-       if (!set)
+       if (!set || parsing_end)
                return -EINVAL;
 
        *num_set = set;
--

2.35.1

#2 lore raw

PATCH v2 1/1 selftests/bpf: Fix issues in parse_num_list()

There are some issues in parse_num_list():

First, the end variable is assigned twice when parsing_end is true, it is unnecessary.

Second, the function does not check that parsing_end is false after parsing argument. Thus, if the final part of the argument is something like '4-', parse_num_list() will discard it instead of returning -EINVAL.

Clean up parse_num_list() and fix these issues.

Before:

 $ ./test_progs -n 2,4-
 #2 atomic_bounds:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

After:

 $ ./test_progs -n 2,4-
 Failed to parse test numbers.

---
v1 -> v2: add more details to commit message

 tools/testing/selftests/bpf/testing_helpers.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/testing_helpers.c +9 -9
index 795b6798ccee..82f0e2d99c23 100644
@@ -20,16 +20,16 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 		if (errno)
 			return -errno;
 
-		if (parsing_end)
-			end = num;
-		else
+		if (!parsing_end) {
 			start = num;
+			if (*next == '-') {
+				s = next + 1;
+				parsing_end = true;
+				continue;
+			}
+		}
 
-		if (!parsing_end && *next == '-') {
-			s = next + 1;
-			parsing_end = true;
-			continue;
-		} else if (*next == ',') {
+		if (*next == ',') {
 			parsing_end = false;
 			s = next + 1;
 			end = num;
@@ -60,7 +60,7 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 			set[i] = true;
 	}
 
-	if (!set)
+	if (!set || parsing_end)
 		return -EINVAL;
 
 	*num_set = set;
Signature · 1 line
2.35.1
#3 lore raw
Quoted 45 lines · Yuntao Wang

There are some issues in parse_num_list():

First, the end variable is assigned twice when parsing_end is true, it is unnecessary.

Second, the function does not check that parsing_end is false after parsing argument. Thus, if the final part of the argument is something like '4-', parse_num_list() will discard it instead of returning -EINVAL.

Clean up parse_num_list() and fix these issues.

Before:

 $ ./test_progs -n 2,4-
 #2 atomic_bounds:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

After:

 $ ./test_progs -n 2,4-
 Failed to parse test numbers.

---
v1 -> v2: add more details to commit message

 tools/testing/selftests/bpf/testing_helpers.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/testing_helpers.c +3 -3
index 795b6798ccee..82f0e2d99c23 100644
@@ -20,16 +20,16 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
                if (errno)
                        return -errno;
 
-               if (parsing_end)
-                       end = num;
-               else
+               if (!parsing_end) {
                        start = num;
+                       if (*next == '-') {
+                               s = next + 1;

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 32

Quoted 10 lines
tools/testing/selftests/bpf/testing_helpers.c +4 -5
+                               parsing_end = true;
+                               continue;
+                       }
+               }
 
-               if (!parsing_end && *next == '-') {
-                       s = next + 1;
-                       parsing_end = true;
-                       continue;
-               } else if (*next == ',') {

I think the new structure of the code is actually harder to follow and there is no need to change this code in the first place just to optimize away parsing_end assignmet.

tools/testing/selftests/bpf/testing_helpers.c int parse_num_list(const char *s, bool **num_set, int *num_set_len) · line 65

Quoted 12 lines
tools/testing/selftests/bpf/testing_helpers.c +2 -1
+               if (*next == ',') {
                        parsing_end = false;
                        s = next + 1;
                        end = num;
@@ -60,7 +60,7 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
                        set[i] = true;
        }
 
-       if (!set)
+       if (!set || parsing_end)
                return -EINVAL;
 

this is a real fix, please submit just and drop the first part of the patch

Quoted 4 lines
tools/testing/selftests/bpf/testing_helpers.c +0 -1
        *num_set = set;
--

2.35.1

#4 lore raw

PATCH v3 1/1 selftests/bpf: Fix issues in parse_num_list()

The function does not check that parsing_end is false after parsing argument. Thus, if the final part of the argument is something like '4-', which is invalid, parse_num_list() will discard it instead of returning -EINVAL.

Before:

 $ ./test_progs -n 2,4-
 #2 atomic_bounds:OK
 Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED

After:

 $ ./test_progs -n 2,4-
 Failed to parse test numbers.

---
v1 -> v2: add more details to commit message
v2 -> v3: remove the first part of the patch

 tools/testing/selftests/bpf/testing_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/testing_helpers.c +1 -1
index 795b6798ccee..87867f7a78c3 100644
@@ -60,7 +60,7 @@ int parse_num_list(const char *s, bool **num_set, int *num_set_len)
 			set[i] = true;
 	}
 
-	if (!set)
+	if (!set || parsing_end)
 		return -EINVAL;
 
 	*num_set = set;
Signature · 1 line
2.35.1
#5 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Wed, 6 Apr 2022 08:36:22 +0800 you wrote:

The function does not check that parsing_end is false after parsing argument. Thus, if the final part of the argument is something like '4-', which is invalid, parse_num_list() will discard it instead of returning -EINVAL.

Before:

[...]

Here is the summary with links:
- [bpf-next,v3] selftests/bpf: Fix issues in parse_num_list()

    https://git.kernel.org/bpf/bpf-next/c/958ddfd75d83

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html