Skip to contentSkip to conversation

selftests/bpf: fix invalid pointer check in get_xlated_program()

PATCH In review Eduard Zingerman 2 participants 4 messages Jun 9, 2023 – Jun 12, 2023

Subsystems (3)
lore
Share & tools
lore

Eduard Zingerman

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: fix invalid pointer check in get_xlated_program()

Dan Carpenter reported invalid check for calloc() result in test_verifier.c:get_xlated_program():

./tools/testing/selftests/bpf/test_verifier.c:1365 get_xlated_program()
warn: variable dereferenced before check 'buf' (see line 1364)

  ./tools/testing/selftests/bpf/test_verifier.c
    1363		*cnt = xlated_prog_len / buf_element_size;
    1364		*buf = calloc(*cnt, buf_element_size);
    1365		if (!buf) {

  This should be if (!*buf) {
    1366			perror("can't allocate xlated program buffer");
    1367			return -ENOMEM;

This commit refactors the get_xlated_program() to avoid using double pointer type.

Fixes 933ff53191eb ("selftests/bpf: specify expected instructions in test_verifier tests")
---
 tools/testing/selftests/bpf/test_verifier.c | 26 ++++++++++++---------
 1 file changed, 15 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +15 -11
index 71704a38cac3..c6bc9e26d333 100644
@@ -1341,45 +1341,48 @@ static bool cmp_str_seq(const char *log, const char *exp)
 	return true;
 }
 
-static int get_xlated_program(int fd_prog, struct bpf_insn **buf, int *cnt)
+static struct bpf_insn *get_xlated_program(int fd_prog, int *cnt)
 {
 	struct bpf_prog_info info = {};
 	__u32 info_len = sizeof(info);
+	__u32 buf_element_size;
 	__u32 xlated_prog_len;
-	__u32 buf_element_size = sizeof(struct bpf_insn);
+	struct bpf_insn *buf;
+
+	buf_element_size = sizeof(struct bpf_insn);
 
 	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
 		perror("bpf_prog_get_info_by_fd failed");
-		return -1;
+		return NULL;
 	}
 
 	xlated_prog_len = info.xlated_prog_len;
 	if (xlated_prog_len % buf_element_size) {
 		printf("Program length %d is not multiple of %d\n",
 		       xlated_prog_len, buf_element_size);
-		return -1;
+		return NULL;
 	}
 
 	*cnt = xlated_prog_len / buf_element_size;
-	*buf = calloc(*cnt, buf_element_size);
+	buf = calloc(*cnt, buf_element_size);
 	if (!buf) {
 		perror("can't allocate xlated program buffer");
-		return -ENOMEM;
+		return NULL;
 	}
 
 	bzero(&info, sizeof(info));
 	info.xlated_prog_len = xlated_prog_len;
-	info.xlated_prog_insns = (__u64)(unsigned long)*buf;
+	info.xlated_prog_insns = (__u64)(unsigned long)buf;
 	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
 		perror("second bpf_prog_get_info_by_fd failed");
 		goto out_free_buf;
 	}
 
-	return 0;
+	return buf;
 
 out_free_buf:
-	free(*buf);
-	return -1;
+	free(buf);
+	return NULL;
 }
 
 static bool is_null_insn(struct bpf_insn *insn)
@@ -1512,7 +1515,8 @@ static bool check_xlated_program(struct bpf_test *test, int fd_prog)
 	if (!check_expected && !check_unexpected)
 		goto out;
 
-	if (get_xlated_program(fd_prog, &buf, &cnt)) {
+	buf = get_xlated_program(fd_prog, &cnt);
+	if (!buf) {
 		printf("FAIL: can't get xlated program\n");
 		result = false;
 		goto out;
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 16 lines · Eduard Zingerman

Dan Carpenter reported invalid check for calloc() result in test_verifier.c:get_xlated_program():

   ./tools/testing/selftests/bpf/test_verifier.c:1365 get_xlated_program()
   warn: variable dereferenced before check 'buf' (see line 1364)

   ./tools/testing/selftests/bpf/test_verifier.c
     1363		*cnt = xlated_prog_len / buf_element_size;
     1364		*buf = calloc(*cnt, buf_element_size);
     1365		if (!buf) {

   This should be if (!*buf) {

     1366			perror("can't allocate xlated program buffer");
     1367			return -ENOMEM;
Quoted 2 lines

This commit refactors the get_xlated_program() to avoid using double pointer type.

Isn't the small reported fix above sufficient? (Either is fine with me though.)

Quoted 17 lines
Fixes 933ff53191eb ("selftests/bpf: specify expected instructions in test_verifier tests")
---
  tools/testing/selftests/bpf/test_verifier.c | 26 ++++++++++++---------
  1 file changed, 15 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +0 -1
index 71704a38cac3..c6bc9e26d333 100644
@@ -1341,45 +1341,48 @@ static bool cmp_str_seq(const char *log, const char *exp)
  	return true;
  }
 
-static int get_xlated_program(int fd_prog, struct bpf_insn **buf, int *cnt)

tools/testing/selftests/bpf/test_verifier.c static bool cmp_str_seq(const char *log, const char *exp) · line 1352

Quoted 10 lines
tools/testing/selftests/bpf/test_verifier.c +5 -1
+static struct bpf_insn *get_xlated_program(int fd_prog, int *cnt)
  {
  	struct bpf_prog_info info = {};
  	__u32 info_len = sizeof(info);
+	__u32 buf_element_size;
  	__u32 xlated_prog_len;
-	__u32 buf_element_size = sizeof(struct bpf_insn);
+	struct bpf_insn *buf;
+
+	buf_element_size = sizeof(struct bpf_insn);

Just small nit: the __u32 buf_element_size = sizeof(struct bpf_insn); could have stayed as is.

Quoted 54 lines
tools/testing/selftests/bpf/test_verifier.c +10 -9
  	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
  		perror("bpf_prog_get_info_by_fd failed");
-		return -1;
+		return NULL;
  	}
 
  	xlated_prog_len = info.xlated_prog_len;
  	if (xlated_prog_len % buf_element_size) {
  		printf("Program length %d is not multiple of %d\n",
  		       xlated_prog_len, buf_element_size);
-		return -1;
+		return NULL;
  	}
 
  	*cnt = xlated_prog_len / buf_element_size;
-	*buf = calloc(*cnt, buf_element_size);
+	buf = calloc(*cnt, buf_element_size);
  	if (!buf) {
  		perror("can't allocate xlated program buffer");
-		return -ENOMEM;
+		return NULL;
  	}
 
  	bzero(&info, sizeof(info));
  	info.xlated_prog_len = xlated_prog_len;
-	info.xlated_prog_insns = (__u64)(unsigned long)*buf;
+	info.xlated_prog_insns = (__u64)(unsigned long)buf;
  	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
  		perror("second bpf_prog_get_info_by_fd failed");
  		goto out_free_buf;
  	}
 
-	return 0;
+	return buf;
 
  out_free_buf:
-	free(*buf);
-	return -1;
+	free(buf);
+	return NULL;
  }
 
  static bool is_null_insn(struct bpf_insn *insn)
@@ -1512,7 +1515,8 @@ static bool check_xlated_program(struct bpf_test *test, int fd_prog)
  	if (!check_expected && !check_unexpected)
  		goto out;
 
-	if (get_xlated_program(fd_prog, &buf, &cnt)) {
+	buf = get_xlated_program(fd_prog, &cnt);
+	if (!buf) {
  		printf("FAIL: can't get xlated program\n");
  		result = false;
  		goto out;
author maintainer
#2 lore raw
Quoted 14 lines · Daniel Borkmann
Quoted 13 lines · Eduard Zingerman

Dan Carpenter reported invalid check for calloc() result in test_verifier.c:get_xlated_program():

   ./tools/testing/selftests/bpf/test_verifier.c:1365 get_xlated_program()
   warn: variable dereferenced before check 'buf' (see line 1364)

   ./tools/testing/selftests/bpf/test_verifier.c
     1363		*cnt = xlated_prog_len / buf_element_size;
     1364		*buf = calloc(*cnt, buf_element_size);
     1365		if (!buf) {

   This should be if (!*buf) {
Quoted 7 lines
Quoted 5 lines
     1366			perror("can't allocate xlated program buffer");
     1367			return -ENOMEM;

This commit refactors the get_xlated_program() to avoid using double pointer type.

Isn't the small reported fix above sufficient? (Either is fine with me though.)

I think it is less prone to mechanical mistakes without double pointers (in case if this function would be modified sometimes in the future). But I can rollback to a small fix if you insist.

Quoted 17 lines
Quoted 17 lines
Fixes 933ff53191eb ("selftests/bpf: specify expected instructions in test_verifier tests")
---
  tools/testing/selftests/bpf/test_verifier.c | 26 ++++++++++++---------
  1 file changed, 15 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +0 -0
index 71704a38cac3..c6bc9e26d333 100644
@@ -1341,45 +1341,48 @@ static bool cmp_str_seq(const char *log, const char *exp)
  	return true;
  }
 
Quoted 14 lines

tools/testing/selftests/bpf/test_verifier.c static bool cmp_str_seq(const char *log, const char *exp) · line 1352

Quoted 11 lines
tools/testing/selftests/bpf/test_verifier.c +5 -2
-static int get_xlated_program(int fd_prog, struct bpf_insn **buf, int *cnt)
+static struct bpf_insn *get_xlated_program(int fd_prog, int *cnt)
  {
  	struct bpf_prog_info info = {};
  	__u32 info_len = sizeof(info);
+	__u32 buf_element_size;
  	__u32 xlated_prog_len;
-	__u32 buf_element_size = sizeof(struct bpf_insn);
+	struct bpf_insn *buf;
+
+	buf_element_size = sizeof(struct bpf_insn);

Just small nit: the __u32 buf_element_size = sizeof(struct bpf_insn); could have stayed as is.

Moved it to have "inverse Christmas tree" for declarations, can send V2 undoing this.

Quoted 56 lines
Quoted 55 lines
tools/testing/selftests/bpf/test_verifier.c +10 -9
  	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
  		perror("bpf_prog_get_info_by_fd failed");
-		return -1;
+		return NULL;
  	}
 
  	xlated_prog_len = info.xlated_prog_len;
  	if (xlated_prog_len % buf_element_size) {
  		printf("Program length %d is not multiple of %d\n",
  		       xlated_prog_len, buf_element_size);
-		return -1;
+		return NULL;
  	}
 
  	*cnt = xlated_prog_len / buf_element_size;
-	*buf = calloc(*cnt, buf_element_size);
+	buf = calloc(*cnt, buf_element_size);
  	if (!buf) {
  		perror("can't allocate xlated program buffer");
-		return -ENOMEM;
+		return NULL;
  	}
 
  	bzero(&info, sizeof(info));
  	info.xlated_prog_len = xlated_prog_len;
-	info.xlated_prog_insns = (__u64)(unsigned long)*buf;
+	info.xlated_prog_insns = (__u64)(unsigned long)buf;
  	if (bpf_prog_get_info_by_fd(fd_prog, &info, &info_len)) {
  		perror("second bpf_prog_get_info_by_fd failed");
  		goto out_free_buf;
  	}
 
-	return 0;
+	return buf;
 
  out_free_buf:
-	free(*buf);
-	return -1;
+	free(buf);
+	return NULL;
  }
 
  static bool is_null_insn(struct bpf_insn *insn)
@@ -1512,7 +1515,8 @@ static bool check_xlated_program(struct bpf_test *test, int fd_prog)
  	if (!check_expected && !check_unexpected)
  		goto out;
 
-	if (get_xlated_program(fd_prog, &buf, &cnt)) {
+	buf = get_xlated_program(fd_prog, &cnt);
+	if (!buf) {
  		printf("FAIL: can't get xlated program\n");
  		result = false;
  		goto out;
maintainer
#3 lore raw
Quoted 18 lines · Eduard Zingerman
Quoted 17 lines · Daniel Borkmann
Quoted 16 lines · Eduard Zingerman

Dan Carpenter reported invalid check for calloc() result in test_verifier.c:get_xlated_program():

    ./tools/testing/selftests/bpf/test_verifier.c:1365 get_xlated_program()
    warn: variable dereferenced before check 'buf' (see line 1364)

    ./tools/testing/selftests/bpf/test_verifier.c
      1363		*cnt = xlated_prog_len / buf_element_size;
      1364		*buf = calloc(*cnt, buf_element_size);
      1365		if (!buf) {

    This should be if (!*buf) {

      1366			perror("can't allocate xlated program buffer");
      1367			return -ENOMEM;
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This commit refactors the get_xlated_program() to avoid using double pointer type.

Isn't the small reported fix above sufficient? (Either is fine with me though.)

I think it is less prone to mechanical mistakes without double pointers (in case if this function would be modified sometimes in the future).

Ok.

Quoted 33 lines

But I can rollback to a small fix if you insist.

Quoted 16 lines
Quoted 16 lines
Fixes 933ff53191eb ("selftests/bpf: specify expected instructions in test_verifier tests")
---
   tools/testing/selftests/bpf/test_verifier.c | 26 ++++++++++++---------
   1 file changed, 15 insertions(+), 11 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +0 -0
index 71704a38cac3..c6bc9e26d333 100644
@@ -1341,45 +1341,48 @@ static bool cmp_str_seq(const char *log, const char *exp)
   	return true;
   }
 
Quoted 14 lines

tools/testing/selftests/bpf/test_verifier.c static bool cmp_str_seq(const char *log, const char *exp) · line 1352

Quoted 11 lines
tools/testing/selftests/bpf/test_verifier.c +5 -2
-static int get_xlated_program(int fd_prog, struct bpf_insn **buf, int *cnt)
+static struct bpf_insn *get_xlated_program(int fd_prog, int *cnt)
   {
   	struct bpf_prog_info info = {};
   	__u32 info_len = sizeof(info);
+	__u32 buf_element_size;
   	__u32 xlated_prog_len;
-	__u32 buf_element_size = sizeof(struct bpf_insn);
+	struct bpf_insn *buf;
+
+	buf_element_size = sizeof(struct bpf_insn);

Just small nit: the __u32 buf_element_size = sizeof(struct bpf_insn); could have stayed as is.

Quoted 2 lines

Moved it to have "inverse Christmas tree" for declarations, can send V2 undoing this.

Nah, it's fine. Fixed up while applying. Thanks!