Skip to contentSkip to conversation

samples/bpf: syscall_tp_user: Refactor and fix array index out-of-bounds bug

PATCH In review Jinghao Jia 3 participants 6 messages Aug 18, 2023 – Aug 28, 2023 BPF

lore
Share & tools
lore
#0 lore raw

cover samples/bpf: syscall_tp_user: Refactor and fix array index out-of-bounds bug

There are currently 6 BPF programs in syscall_tp_kern but the array to hold the corresponding bpf_links in syscall_tp_user only has space for 4 programs, given the array size is hardcoded. This causes the sample program to fail due to an out-of-bound access that corrupts other stack variables:

  # ./syscall_tp
  prog #0: map ids 4 5
  verify map:4 val: 5
  map_lookup failed: Bad file descriptor

This patch series aims to solve this issue for now and for the future. It first adds the -fsanitize=bounds flag to make similar bugs more obvious at runtime. It then refactors syscall_tp_user to retrieve the number of programs from the bpf_object and dynamically allocate the array of bpf_links to avoid inconsistencies from hardcoding.

Jinghao Jia (3):
samples/bpf: Add -fsanitize=bounds to userspace programs samples/bpf: syscall_tp_user: Rename num_progs into nr_tests samples/bpf: syscall_tp_user: Fix array out-of-bound access

 samples/bpf/Makefile          |  1 +
 samples/bpf/syscall_tp_user.c | 44 ++++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 14 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 samples/bpf: Add -fsanitize=bounds to userspace programs

The sanitizer flag, which is supported by both clang and gcc, would make it easier to debug array index out-of-bounds problems in these programs.

---
 samples/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)
samples/bpf/Makefile +1 -0
index 595b98d825ce..340972ed1582 100644
@@ -193,6 +193,7 @@ endif
 TPROGS_CFLAGS += -Wall -O2
 TPROGS_CFLAGS += -Wmissing-prototypes
 TPROGS_CFLAGS += -Wstrict-prototypes
+TPROGS_CFLAGS += -fsanitize=bounds
 
 TPROGS_CFLAGS += -I$(objtree)/usr/include
 TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/3 samples/bpf: syscall_tp_user: Rename num_progs into nr_tests

In reply to Jinghao Jia

The variable name num_progs causes confusion because that variable really controls the number of rounds the test should be executed.

Rename num_progs into nr_tests for the sake of clarity.

---
 samples/bpf/syscall_tp_user.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
samples/bpf/syscall_tp_user.c +12 -12
index 7a788bb837fc..18c94c7e8a40 100644
@@ -17,9 +17,9 @@
 
 static void usage(const char *cmd)
 {
-	printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
-	printf("       -i num_progs      # number of progs of the test\n");
-	printf("       -h                # help\n");
+	printf("USAGE: %s [-i nr_tests] [-h]\n", cmd);
+	printf("       -i nr_tests      # rounds of test to run\n");
+	printf("       -h               # help\n");
 }
 
 static void verify_map(int map_id)
@@ -45,14 +45,14 @@ static void verify_map(int map_id)
 	}
 }
 
-static int test(char *filename, int num_progs)
+static int test(char *filename, int nr_tests)
 {
-	int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0;
-	struct bpf_link *links[num_progs * 4];
-	struct bpf_object *objs[num_progs];
+	int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
+	struct bpf_link *links[nr_tests * 4];
+	struct bpf_object *objs[nr_tests];
 	struct bpf_program *prog;
 
-	for (i = 0; i < num_progs; i++) {
+	for (i = 0; i < nr_tests; i++) {
 		objs[i] = bpf_object__open_file(filename, NULL);
 		if (libbpf_get_error(objs[i])) {
 			fprintf(stderr, "opening BPF object file failed\n");
@@ -101,7 +101,7 @@ static int test(char *filename, int num_progs)
 	close(fd);
 
 	/* verify the map */
-	for (i = 0; i < num_progs; i++) {
+	for (i = 0; i < nr_tests; i++) {
 		verify_map(map0_fds[i]);
 		verify_map(map1_fds[i]);
 	}
@@ -117,13 +117,13 @@ static int test(char *filename, int num_progs)
 
 int main(int argc, char **argv)
 {
-	int opt, num_progs = 1;
+	int opt, nr_tests = 1;
 	char filename[256];
 
 	while ((opt = getopt(argc, argv, "i:h")) != -1) {
 		switch (opt) {
 		case 'i':
-			num_progs = atoi(optarg);
+			nr_tests = atoi(optarg);
 			break;
 		case 'h':
 		default:
@@ -134,5 +134,5 @@ int main(int argc, char **argv)
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	return test(filename, num_progs);
+	return test(filename, nr_tests);
 }
Signature · 1 line
2.41.0
#3 lore raw

PATCH 3/3 samples/bpf: syscall_tp_user: Fix array out-of-bound access

In reply to Jinghao Jia

Commit 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample") added two more eBPF programs to support the openat2() syscall. However, it did not increase the size of the array that holds the corresponding bpf_links. This leads to an out-of-bound access on that array in the bpf_object__for_each_program loop and could corrupt other variables on the stack. On our testing QEMU, it corrupts the map1_fds array and causes the sample to fail:

  # ./syscall_tp
  prog #0: map ids 4 5
  verify map:4 val: 5
  map_lookup failed: Bad file descriptor

Dynamically allocate the array based on the number of programs reported by libbpf to prevent similar inconsistencies in the future

Fixes 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample")
---
 samples/bpf/syscall_tp_user.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
samples/bpf/syscall_tp_user.c +19 -3
index 18c94c7e8a40..8855d2c1290d 100644
@@ -48,7 +48,7 @@ static void verify_map(int map_id)
 static int test(char *filename, int nr_tests)
 {
 	int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
-	struct bpf_link *links[nr_tests * 4];
+	struct bpf_link **links = NULL;
 	struct bpf_object *objs[nr_tests];
 	struct bpf_program *prog;
 
@@ -60,6 +60,17 @@ static int test(char *filename, int nr_tests)
 			goto cleanup;
 		}
 
+		/* One-time initialization */
+		if (!links) {
+			int nr_progs = 0;
+
+			bpf_object__for_each_program(prog, objs[i])
+				nr_progs += 1;
+
+			links = calloc(nr_progs * nr_tests,
+				       sizeof(struct bpf_link *));
+		}
+
 		/* load BPF program */
 		if (bpf_object__load(objs[i])) {
 			fprintf(stderr, "loading BPF object file failed\n");
@@ -107,8 +118,13 @@ static int test(char *filename, int nr_tests)
 	}
 
 cleanup:
-	for (j--; j >= 0; j--)
-		bpf_link__destroy(links[j]);
+	if (links) {
+		for (j--; j >= 0; j--)
+			bpf_link__destroy(links[j]);
+
+		free(links);
+		links = NULL;
+	}
 
 	for (i--; i >= 0; i--)
 		bpf_object__close(objs[i]);
Signature · 1 line
2.41.0
maintainer
#4 lore raw

Re: [PATCH bpf 3/3] samples/bpf: syscall_tp_user: Fix array out-of-bound access

Quoted 38 lines · Jinghao Jia

Commit 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample") added two more eBPF programs to support the openat2() syscall. However, it did not increase the size of the array that holds the corresponding bpf_links. This leads to an out-of-bound access on that array in the bpf_object__for_each_program loop and could corrupt other variables on the stack. On our testing QEMU, it corrupts the map1_fds array and causes the sample to fail:

   # ./syscall_tp
   prog #0: map ids 4 5
   verify map:4 val: 5
   map_lookup failed: Bad file descriptor

Dynamically allocate the array based on the number of programs reported by libbpf to prevent similar inconsistencies in the future

Fixes 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample")
---
  samples/bpf/syscall_tp_user.c | 22 +++++++++++++++++++---
  1 file changed, 19 insertions(+), 3 deletions(-)
samples/bpf/syscall_tp_user.c +1 -1
index 18c94c7e8a40..8855d2c1290d 100644
@@ -48,7 +48,7 @@ static void verify_map(int map_id)
  static int test(char *filename, int nr_tests)
  {
  	int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
-	struct bpf_link *links[nr_tests * 4];
+	struct bpf_link **links = NULL;
  	struct bpf_object *objs[nr_tests];
  	struct bpf_program *prog;
 
@@ -60,6 +60,17 @@ static int test(char *filename, int nr_tests)
  			goto cleanup;
  		}

samples/bpf/syscall_tp_user.c static int test(char *filename, int nr_tests) · line 71

Quoted 10 lines
samples/bpf/syscall_tp_user.c +9 -0
 
+		/* One-time initialization */
+		if (!links) {
+			int nr_progs = 0;
+
+			bpf_object__for_each_program(prog, objs[i])
+				nr_progs += 1;
+
+			links = calloc(nr_progs * nr_tests,
+				       sizeof(struct bpf_link *));

NULL check is missing

Quoted 7 lines
samples/bpf/syscall_tp_user.c +2 -0
+		}
+
  		/* load BPF program */
  		if (bpf_object__load(objs[i])) {
  			fprintf(stderr, "loading BPF object file failed\n");
@@ -107,8 +118,13 @@ static int test(char *filename, int nr_tests)
  	}

samples/bpf/syscall_tp_user.c static int test(char *filename, int nr_tests) · line 126

Quoted 10 lines
samples/bpf/syscall_tp_user.c +6 -2
 
  cleanup:
-	for (j--; j >= 0; j--)
-		bpf_link__destroy(links[j]);
+	if (links) {
+		for (j--; j >= 0; j--)
+			bpf_link__destroy(links[j]);
+
+		free(links);
+		links = NULL;

why is this explicit links = NULL needed?

Quoted 5 lines
samples/bpf/syscall_tp_user.c +1 -0
+	}
 
  	for (i--; i >= 0; i--)
  		bpf_object__close(objs[i]);
#5 lore raw

Re: [PATCH bpf 3/3] samples/bpf: syscall_tp_user: Fix array out-of-bound access

Quoted 41 lines · Daniel Borkmann
Quoted 40 lines · Jinghao Jia

Commit 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample") added two more eBPF programs to support the openat2() syscall. However, it did not increase the size of the array that holds the corresponding bpf_links. This leads to an out-of-bound access on that array in the bpf_object__for_each_program loop and could corrupt other variables on the stack. On our testing QEMU, it corrupts the map1_fds array and causes the sample to fail:

   # ./syscall_tp
   prog #0: map ids 4 5
   verify map:4 val: 5
   map_lookup failed: Bad file descriptor

Dynamically allocate the array based on the number of programs reported by libbpf to prevent similar inconsistencies in the future

Fixes 06744f24696e ("samples/bpf: Add openat2() enter/exit tracepoint to syscall_tp sample")

---
  samples/bpf/syscall_tp_user.c | 22 +++++++++++++++++++---

  1 file changed, 19 insertions(+), 3 deletions(-)
samples/bpf/syscall_tp_user.c +1 -1
index 18c94c7e8a40..8855d2c1290d 100644
@@ -48,7 +48,7 @@ static void verify_map(int map_id)
   static int test(char *filename, int nr_tests)
   {
       int map0_fds[nr_tests], map1_fds[nr_tests], fd, i, j = 0;
-    struct bpf_link *links[nr_tests * 4];
+    struct bpf_link **links = NULL;
       struct bpf_object *objs[nr_tests];
       struct bpf_program *prog;
   @@ -60,6 +60,17 @@ static int test(char *filename, int nr_tests)

              goto cleanup;
          }
  +        /* One-time initialization */
+        if (!links) {
+            int nr_progs = 0;

Quoted 8 lines

samples/bpf/syscall_tp_user.c static int test(char *filename, int nr_tests) · line 71

Quoted 6 lines
samples/bpf/syscall_tp_user.c +6 -0
+
+            bpf_object__for_each_program(prog, objs[i])
+                nr_progs += 1;
+
+            links = calloc(nr_progs * nr_tests,
+                       sizeof(struct bpf_link *));

NULL check is missing

Oh, apparently I missed that, will fix in v2.

Quoted 11 lines
Quoted 11 lines

+        }
+
          /* load BPF program */
          if (bpf_object__load(objs[i])) { fprintf(stderr, "loading BPF object file failed\n");

samples/bpf/syscall_tp_user.c +0 -0
@@ -107,8 +118,13 @@ static int test(char *filename, int nr_tests)
       }

    cleanup:
-    for (j--; j >= 0; j--)
-        bpf_link__destroy(links[j]);

Quoted 8 lines
Quoted 6 lines

+    if (links) {
+        for (j--; j >= 0; j--)
+            bpf_link__destroy(links[j]);
+
+        free(links);
+        links = NULL;

why is this explicit links = NULL needed?

Yeah I agree this is redundant, since links is not used later.

Quoted 8 lines
Quoted 5 lines

+    }
        for (i--; i >= 0; i--)
          bpf_object__close(objs[i]);

I wonder if there are further comments before I roll out a v2?

--Jinghao