Skip to contentSkip to conversation

BPF refactor and add bpf_prog05

PATCH New Richard Palethorpe 2 participants 7 messages May 5, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover BPF refactor and add bpf_prog05

Hello,

V3:

* constify and add attributes

* replace insn builder with BPF_MAP_ARRAY_STX macro to insert multiple instructions.

V2:

* Added a number of helper functions to bpf_common and used them on all tests.

* Added some instruction concatenation helpers, but only used these on bpf_prog05 as they don't seem to make the other tests better to read.

* Further shorten bpf_prog05 by using a generic expect function

Richard Palethorpe (4):
lapi/bpf: Add /= and %=
bpf: Add map_array helper functions/macro and constify
bpf: Add helper to run socket programs
bpf: Check truncation on 32bit div/mod by zero

 include/lapi/bpf.h                         |   2 +
 runtest/cve                                |   1 +
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/bpf/.gitignore   |   1 +
 testcases/kernel/syscalls/bpf/bpf_common.c |  64 ++++++--
 testcases/kernel/syscalls/bpf/bpf_common.h |  53 +++++-
 testcases/kernel/syscalls/bpf/bpf_prog01.c |  32 +---
 testcases/kernel/syscalls/bpf/bpf_prog02.c |  54 ++-----
 testcases/kernel/syscalls/bpf/bpf_prog03.c |  32 +---
 testcases/kernel/syscalls/bpf/bpf_prog04.c |  17 +-
 testcases/kernel/syscalls/bpf/bpf_prog05.c | 177 +++++++++++++++++++++
 11 files changed, 308 insertions(+), 126 deletions(-)
 create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog05.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/4 lapi/bpf: Add /= and %=

Add div and mod instructions.

---
 include/lapi/bpf.h | 2 ++
 1 file changed, 2 insertions(+)
include/lapi/bpf.h +2 -0
index f27a92146..5ae25293b 100644
@@ -37,8 +37,10 @@
 #define BPF_OP(code)    ((code) & 0xf0)
 #define		BPF_ADD		0x00
 #define		BPF_SUB		0x10
+#define		BPF_DIV		0x30
 #define		BPF_LSH		0x60
 #define		BPF_RSH		0x70
+#define		BPF_MOD		0x90
 
 #define		BPF_JEQ		0x10
 
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/4 bpf: Add map_array helper functions/macro and constify

In reply to Richard Palethorpe

We usually just create an array map with standard key and value size. Then fetch some values from the array. So we can create some standard functions for this.

The byte code which stores the values in the map can also be extracted into a macro. So a macro has been added, however it is only used in the new bpf_prog05 test. The old tests do some pointer arithmetic on the map call return value to trigger bugs. They could be rewritten, but then would need verifying.

This also adds some extra const checks and attributes.

---
 testcases/kernel/syscalls/bpf/bpf_common.c | 49 ++++++++++++++++----
 testcases/kernel/syscalls/bpf/bpf_common.h | 53 ++++++++++++++++++++--
 testcases/kernel/syscalls/bpf/bpf_prog01.c | 25 +++-------
 testcases/kernel/syscalls/bpf/bpf_prog02.c | 47 +++++--------------
 testcases/kernel/syscalls/bpf/bpf_prog03.c | 25 ++--------
 testcases/kernel/syscalls/bpf/bpf_prog04.c |  8 +---
 6 files changed, 112 insertions(+), 95 deletions(-)
testcases/kernel/syscalls/bpf/bpf_common.c +40 -9
index fd299b73d..42c72b975 100644
@@ -28,7 +28,7 @@ void rlimit_bump_memlock(void)
 	}
 }
 
-int bpf_map_create(union bpf_attr *attr)
+int bpf_map_create(union bpf_attr *const attr)
 {
 	int ret;
 
@@ -48,12 +48,45 @@ int bpf_map_create(union bpf_attr *attr)
 	return ret;
 }
 
-void bpf_init_prog_attr(union bpf_attr *attr, const struct bpf_insn *prog,
-	size_t prog_size, char *log_buf, size_t log_size)
+int bpf_map_array_create(const uint32_t max_entries)
+{
+	union bpf_attr map_attr = {
+		.map_type = BPF_MAP_TYPE_ARRAY,
+		.key_size = 4,
+		.value_size = 8,
+		.max_entries = max_entries,
+		.map_flags = 0
+	};
+
+	return bpf_map_create(&map_attr);
+}
+
+void bpf_map_array_get(const int map_fd,
+		       const uint32_t *const array_indx,
+		       uint64_t *const array_val)
+{
+	union bpf_attr elem_attr = {
+		.map_fd = map_fd,
+		.key = ptr_to_u64(array_indx),
+		.value = ptr_to_u64(array_val),
+		.flags = 0
+	};
+	const int ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));
+
+        if (ret) {
+		tst_brk(TBROK | TTERRNO,
+			"Failed array map lookup: [%"PRIu32, *array_indx);
+	}
+}
+
+void bpf_init_prog_attr(union bpf_attr *const attr,
+			const struct bpf_insn *const prog,
+			const size_t prog_size, char *const log_buf,
+			const size_t log_size)
 {
 	static struct bpf_insn *buf;
 	static size_t buf_size;
-	size_t prog_len = prog_size / sizeof(*prog);
+	const size_t prog_len = prog_size / sizeof(*prog);
 
 	/* all guarded buffers will be free()d automatically by LTP library */
 	if (!buf || prog_size > buf_size) {
@@ -72,12 +105,10 @@ void bpf_init_prog_attr(union bpf_attr *attr, const struct bpf_insn *prog,
 	attr->log_level = 1;
 }
 
-int bpf_load_prog(union bpf_attr *attr, const char *log)
+int bpf_load_prog(union bpf_attr *const attr, const char *const log)
 {
-	int ret;
-
-	ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
-		TST_RETVAL_GE0);
+	const int ret = TST_RETRY_FUNC(bpf(BPF_PROG_LOAD, attr, sizeof(*attr)),
+				       TST_RETVAL_GE0);
 
 	if (ret >= 0) {
 		tst_res(TPASS, "Loaded program");
testcases/kernel/syscalls/bpf/bpf_common.h +49 -4

61 lines not on this page · raw message

testcases/kernel/syscalls/bpf/bpf_prog01.c +6 -19
index 966bf2092..ac57b24b5 100644
@@ -83,14 +83,7 @@ void run(void)
 	uint32_t key = 0;
 	uint64_t val;
 
-	memset(attr, 0, sizeof(*attr));
-	attr->map_type = BPF_MAP_TYPE_ARRAY;
-	attr->key_size = 4;
-	attr->value_size = 8;
-	attr->max_entries = 1;
-
-	map_fd = bpf_map_create(attr);
-
+	map_fd = bpf_map_array_create(1);
 	prog_fd = load_prog(map_fd);
 
 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
@@ -99,15 +92,12 @@ void run(void)
 
 	SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
 
-	memset(attr, 0, sizeof(*attr));
-	attr->map_fd = map_fd;
-	attr->key = ptr_to_u64(&key);
-	attr->value = ptr_to_u64(&val);
+	SAFE_CLOSE(prog_fd);
+	SAFE_CLOSE(sk[0]);
+	SAFE_CLOSE(sk[1]);
 
-	TEST(bpf(BPF_MAP_LOOKUP_ELEM, attr, sizeof(*attr)));
-	if (TST_RET == -1) {
-		tst_res(TFAIL | TTERRNO, "array map lookup");
-	} else if (val != 1) {
+        bpf_map_array_get(map_fd, &key, &val);
+	if (val != 1) {
 		tst_res(TFAIL,
 			"val = %lu, but should be val = 1",
 			val);
@@ -115,10 +105,7 @@ void run(void)
 	        tst_res(TPASS, "val = 1");
 	}
 
-	SAFE_CLOSE(prog_fd);
 	SAFE_CLOSE(map_fd);
-	SAFE_CLOSE(sk[0]);
-	SAFE_CLOSE(sk[1]);
 }
 
 static struct tst_test test = {
testcases/kernel/syscalls/bpf/bpf_prog02.c +11 -36

74 lines not on this page · raw message

testcases/kernel/syscalls/bpf/bpf_prog03.c +5 -20
index 5b8a394e8..1195ddc2c 100644
@@ -119,13 +119,7 @@ static void run(void)
 	int map_fd, prog_fd;
 	int sk[2];
 
-	memset(attr, 0, sizeof(*attr));
-	attr->map_type = BPF_MAP_TYPE_ARRAY;
-	attr->key_size = 4;
-	attr->value_size = 8;
-	attr->max_entries = 32;
-
-	map_fd = bpf_map_create(attr);
+	map_fd = bpf_map_array_create(32);
 
 	memset(attr, 0, sizeof(*attr));
 	attr->map_fd = map_fd;
@@ -148,22 +142,13 @@ static void run(void)
 			&prog_fd, sizeof(prog_fd));
 
 	SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
-
-	memset(attr, 0, sizeof(*attr));
-	attr->map_fd = map_fd;
-	attr->key = ptr_to_u64(key);
-	attr->value = ptr_to_u64(val);
-	*key = 0;
-
-	TEST(bpf(BPF_MAP_LOOKUP_ELEM, attr, sizeof(*attr)));
-	if (TST_RET == -1)
-		tst_res(TFAIL | TTERRNO, "array map lookup");
-	else
-		tst_res(TINFO, "Pointer offset was 0x%"PRIx64, *val);
-
 	SAFE_CLOSE(sk[0]);
 	SAFE_CLOSE(sk[1]);
 	SAFE_CLOSE(prog_fd);
+
+	*key = 0;
+	bpf_map_array_get(map_fd, key, val);
+	tst_res(TINFO, "Pointer offset was 0x%"PRIx64, *val);
 exit:
 	SAFE_CLOSE(map_fd);
 }
testcases/kernel/syscalls/bpf/bpf_prog04.c +1 -7
index 799fcb52d..1a1ee0f04 100644
@@ -94,13 +94,7 @@ static void run(void)
 	int map_fd, prog_fd;
 	int sk[2];
 
-	memset(attr, 0, sizeof(*attr));
-	attr->map_type = BPF_MAP_TYPE_ARRAY;
-	attr->key_size = 4;
-	attr->value_size = 8;
-	attr->max_entries = 1;
-
-	map_fd = bpf_map_create(attr);
+	map_fd = bpf_map_array_create(1);
 	prog_fd = load_prog(map_fd);
 
 	if (prog_fd >= 0) {
Signature · 1 line
2.31.1
#3 lore raw

PATCH v3 2/4 bpf: Add map_array helper functions/macro and constify

Hi!

testcases/kernel/syscalls/bpf/bpf_common.c int bpf_map_create(union bpf_attr *attr) · line 76

Quoted 3 lines
testcases/kernel/syscalls/bpf/bpf_common.c +3 -0
+	const int ret = bpf(BPF_MAP_LOOKUP_ELEM, &elem_attr, sizeof(elem_attr));
+
+        if (ret) {

Fixed these spaces wrongly used for indentation.

testcases/kernel/syscalls/bpf/bpf_common.c int bpf_map_create(union bpf_attr *attr) · line 78

Quoted 2 lines
testcases/kernel/syscalls/bpf/bpf_common.c +2 -0
+		tst_brk(TBROK | TTERRNO,
+			"Failed array map lookup: [%"PRIu32, *array_indx);

And also this message. There was missing ] at the end of the string and I've also added the fd=%i to the message as well.

And pushed along with the rest of the patchset, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]
#4 lore raw

PATCH v3 3/4 bpf: Add helper to run socket programs

In reply to Richard Palethorpe

So far we always trigger the BPF programs the same way.

---
 testcases/kernel/syscalls/bpf/bpf_common.c | 15 +++++++++++++++
 testcases/kernel/syscalls/bpf/bpf_prog01.c | 11 +----------
 testcases/kernel/syscalls/bpf/bpf_prog02.c | 11 +----------
 testcases/kernel/syscalls/bpf/bpf_prog03.c |  9 +--------
 testcases/kernel/syscalls/bpf/bpf_prog04.c |  9 +--------
 5 files changed, 19 insertions(+), 36 deletions(-)
testcases/kernel/syscalls/bpf/bpf_common.c +15 -0
index 42c72b975..8a324a8fa 100644
@@ -124,3 +124,18 @@ int bpf_load_prog(union bpf_attr *const attr, const char *const log)
 	tst_brk(TBROK | TERRNO, "Failed to load program");
 	return ret;
 }
+
+void bpf_run_prog(const int prog_fd,
+		  const char *const msg, const size_t msg_len)
+{
+	int sk[2];
+
+	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
+	SAFE_SETSOCKOPT(sk[1], SOL_SOCKET, SO_ATTACH_BPF,
+			&prog_fd, sizeof(prog_fd));
+
+	SAFE_WRITE(1, sk[0], msg, msg_len);
+
+	SAFE_CLOSE(sk[0]);
+	SAFE_CLOSE(sk[1]);
+}
testcases/kernel/syscalls/bpf/bpf_prog01.c +1 -10
index ac57b24b5..1d5d04556 100644
@@ -79,22 +79,13 @@ void setup(void)
 void run(void)
 {
 	int map_fd, prog_fd;
-	int sk[2];
 	uint32_t key = 0;
 	uint64_t val;
 
 	map_fd = bpf_map_array_create(1);
 	prog_fd = load_prog(map_fd);
-
-	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
-	SAFE_SETSOCKOPT(sk[1], SOL_SOCKET, SO_ATTACH_BPF,
-			&prog_fd, sizeof(prog_fd));
-
-	SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
-
+	bpf_run_prog(prog_fd, msg, sizeof(MSG));
 	SAFE_CLOSE(prog_fd);
-	SAFE_CLOSE(sk[0]);
-	SAFE_CLOSE(sk[1]);
 
         bpf_map_array_get(map_fd, &key, &val);
 	if (val != 1) {
testcases/kernel/syscalls/bpf/bpf_prog02.c +1 -10
index 4558153ea..9f6acca60 100644
@@ -79,20 +79,11 @@ static void setup(void)
 static void run(void)
 {
 	int map_fd, prog_fd;
-	int sk[2];
 
 	map_fd = bpf_map_array_create(2);
 	prog_fd = load_prog(map_fd);
-
-	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
-	SAFE_SETSOCKOPT(sk[1], SOL_SOCKET, SO_ATTACH_BPF,
-			&prog_fd, sizeof(prog_fd));
-
-	SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
-
+	bpf_run_prog(prog_fd, msg, sizeof(MSG));
 	SAFE_CLOSE(prog_fd);
-	SAFE_CLOSE(sk[0]);
-	SAFE_CLOSE(sk[1]);
 
         *key = 0;
 	bpf_map_array_get(map_fd, key, val);
testcases/kernel/syscalls/bpf/bpf_prog03.c +1 -8
index 1195ddc2c..9a7af7f4c 100644
@@ -117,7 +117,6 @@ static void setup(void)
 static void run(void)
 {
 	int map_fd, prog_fd;
-	int sk[2];
 
 	map_fd = bpf_map_array_create(32);
 
@@ -137,13 +136,7 @@ static void run(void)
 
 	tst_res(TFAIL, "Loaded bad eBPF, now we will run it and maybe crash");
 
-	SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
-	SAFE_SETSOCKOPT(sk[1], SOL_SOCKET, SO_ATTACH_BPF,
-			&prog_fd, sizeof(prog_fd));
-
-	SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
-	SAFE_CLOSE(sk[0]);
-	SAFE_CLOSE(sk[1]);
+	bpf_run_prog(prog_fd, msg, sizeof(MSG));
 	SAFE_CLOSE(prog_fd);
 
 	*key = 0;
testcases/kernel/syscalls/bpf/bpf_prog04.c +1 -8
index 1a1ee0f04..09d0cc468 100644
@@ -92,7 +92,6 @@ static void setup(void)
 static void run(void)
 {
 	int map_fd, prog_fd;
-	int sk[2];
 
 	map_fd = bpf_map_array_create(1);
 	prog_fd = load_prog(map_fd);
@@ -100,13 +99,7 @@ static void run(void)
 	if (prog_fd >= 0) {
 		tst_res(TFAIL, "Malicious eBPF code passed verification. "
 			"Now let's try crashing the kernel.");
-		SAFE_SOCKETPAIR(AF_UNIX, SOCK_DGRAM, 0, sk);
-		SAFE_SETSOCKOPT(sk[1], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
-			sizeof(prog_fd));
-
-		SAFE_WRITE(1, sk[0], msg, sizeof(MSG));
-		SAFE_CLOSE(sk[0]);
-		SAFE_CLOSE(sk[1]);
+		bpf_run_prog(prog_fd, msg, sizeof(MSG));
 	}
 
 	if (prog_fd >= 0)
Signature · 1 line
2.31.1
#5 lore raw

PATCH v3 4/4 bpf: Check truncation on 32bit div/mod by zero

In reply to Richard Palethorpe

Add a test which checks for a number of issues surrounding division by zero.

---
 runtest/cve                                |   1 +
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/bpf/.gitignore   |   1 +
 testcases/kernel/syscalls/bpf/bpf_prog05.c | 177 +++++++++++++++++++++
 4 files changed, 180 insertions(+)
 create mode 100644 testcases/kernel/syscalls/bpf/bpf_prog05.c
runtest/cve +1 -0
index f650854f9..3beb88bb0 100644
@@ -61,3 +61,4 @@ cve-2020-11494 pty04
 cve-2020-14386 sendto03
 cve-2020-14416 pty03
 cve-2020-29373 io_uring02
+cve-2021-3444 bpf_prog05
runtest/syscalls +1 -0
index 816c01bf6..aa7fa24dd 100644
@@ -42,6 +42,7 @@ bpf_prog01 bpf_prog01
 bpf_prog02 bpf_prog02
 bpf_prog03 bpf_prog03
 bpf_prog04 bpf_prog04
+bpf_prog05 bpf_prog05
 
 brk01 brk01
 brk02 brk02
testcases/kernel/syscalls/bpf/.gitignore +1 -0
index 74742c0cd..42365cef5 100644
@@ -3,3 +3,4 @@ bpf_prog01
 bpf_prog02
 bpf_prog03
 bpf_prog04
+bpf_prog05
testcases/kernel/syscalls/bpf/bpf_prog05.c new file +177 -0

177 lines not on this page · raw message

Signature · 1 line
2.31.1
#6 lore raw

PATCH v3 4/4 bpf: Check truncation on 32bit div/mod by zero

Hi!

testcases/kernel/syscalls/bpf/bpf_prog05.c line 139

Quoted 4 lines
testcases/kernel/syscalls/bpf/bpf_prog05.c new file +4 -0
+	map_fd = bpf_map_array_create(4);
+	prog_fd = load_prog();
+	bpf_run_prog(prog_fd, msg, sizeof(MSG));
+	SAFE_CLOSE(prog_fd);

I've added *key = 0; here so that the test does not break on invalid map index on subsequent iterations (-i 2).

And also fixed some spaces mixed between tabs used for indentation and pushed, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]