Skip to contentSkip to conversation

Fix sanitizer-reported libbpf and selftest issues

PATCH In review Andrii Nakryiko 3 participants 17 messages Nov 24, 2021 – Nov 26, 2021

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

cover Fix sanitizer-reported libbpf and selftest issues

After doing another run with ASAN, LSAN, and UBSAN, they turned up a bunch of new issues not noticed before. Fix all of them in preparation for sanitized test_progs runs in BPF CI in the near future.

Andrii Nakryiko (13):
tools/resolve_btf_ids: close ELF file on error
libbpf: fix potential misaligned memory access in btf_ext__new()
libbpf: prevent UBSan from complaining about integer overflow
libbpf: don't call libc APIs with NULL pointers
libbpf: fix glob_syms memory leak in bpf_linker
libbpf: fix using invalidated memory in bpf_linker selftests/bpf: fix UBSan complaint about signed __int128 overflow selftests/bpf: fix possible NULL passed to memcpy() with zero size selftests/bpf: prevent misaligned memory access in get_stack_raw_tp
test
selftests/bpf: fix misaligned memory access in queue_stack_map test selftests/bpf: prevent out-of-bounds stack access in test_bpffs selftests/bpf: fix misaligned memory accesses in xdp_bonding test selftests/bpf: fix misaligned accesses in xdp and xdp_bpf2bpf tests

 tools/bpf/resolve_btfids/main.c               |  5 +--
 tools/lib/bpf/btf.c                           | 11 +++---
 tools/lib/bpf/btf.h                           |  2 +-
 tools/lib/bpf/libbpf.c                        | 10 ++++--
 tools/lib/bpf/linker.c                        |  6 +++-
 .../selftests/bpf/prog_tests/btf_dump.c       |  2 +-
 .../selftests/bpf/prog_tests/core_reloc.c     |  3 +-
 .../bpf/prog_tests/get_stack_raw_tp.c         | 14 +++++---
 .../bpf/prog_tests/queue_stack_map.c          | 12 ++++---
 .../selftests/bpf/prog_tests/test_bpffs.c     |  4 ++-
 tools/testing/selftests/bpf/prog_tests/xdp.c  | 11 +++---
 .../selftests/bpf/prog_tests/xdp_bonding.c    | 36 ++++++++++---------
 .../selftests/bpf/prog_tests/xdp_bpf2bpf.c    |  6 ++--
 13 files changed, 74 insertions(+), 48 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/13 tools/resolve_btf_ids: close ELF file on error

Fix one case where we don't do explicit clean up.

Fixes fbbb68de80a4 ("bpf: Add resolve_btfids tool to resolve BTF IDs in ELF object")
---
 tools/bpf/resolve_btfids/main.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/bpf/resolve_btfids/main.c +3 -2
index a59cb0ee609c..e9e6166c3f28 100644
@@ -167,7 +167,7 @@ static struct btf_id *btf_id__find(struct rb_root *root, const char *name)
 	return NULL;
 }
 
-static struct btf_id*
+static struct btf_id *
 btf_id__add(struct rb_root *root, char *name, bool unique)
 {
 	struct rb_node **p = &root->rb_node;
@@ -730,7 +730,8 @@ int main(int argc, const char **argv)
 	if (obj.efile.idlist_shndx == -1 ||
 	    obj.efile.symbols_shndx == -1) {
 		pr_debug("Cannot find .BTF_ids or symbols sections, nothing to do\n");
-		return 0;
+		err = 0;
+		goto out;
 	}
 
 	if (symbols_collect(&obj))
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 2/13 libbpf: fix potential misaligned memory access in btf_ext__new()

In reply to Andrii Nakryiko

Perform a memory copy before we do the sanity checks of btf_ext_hdr. This prevents misaligned memory access if raw btf_ext data is not 4-byte aligned ([0]).

While at it, also add missing const qualifier.

[0] Closes: https://github.com/libbpf/libbpf/issues/391

Fixes 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections")
---
 tools/lib/bpf/btf.c | 10 +++++-----
 tools/lib/bpf/btf.h |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
tools/lib/bpf/btf.c +5 -5
index e97217a77196..8024fe355ca8 100644
@@ -2731,15 +2731,11 @@ void btf_ext__free(struct btf_ext *btf_ext)
 	free(btf_ext);
 }
 
-struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
+struct btf_ext *btf_ext__new(const __u8 *data, __u32 size)
 {
 	struct btf_ext *btf_ext;
 	int err;
 
-	err = btf_ext_parse_hdr(data, size);
-	if (err)
-		return libbpf_err_ptr(err);
-
 	btf_ext = calloc(1, sizeof(struct btf_ext));
 	if (!btf_ext)
 		return libbpf_err_ptr(-ENOMEM);
@@ -2752,6 +2748,10 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
 	}
 	memcpy(btf_ext->data, data, size);
 
+	err = btf_ext_parse_hdr(btf_ext->data, size);
+	if (err)
+		goto done;
+
 	if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) {
 		err = -EINVAL;
 		goto done;
tools/lib/bpf/btf.h +1 -1
index 5c73a5b0a044..742a2bf71c5e 100644
@@ -157,7 +157,7 @@ LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
 				    __u32 expected_value_size,
 				    __u32 *key_type_id, __u32 *value_type_id);
 
-LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
+LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size);
 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
 LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
 					     __u32 *size);
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH 3/13 libbpf: prevent UBSan from complaining about integer overflow

In reply to Andrii Nakryiko

Integer overflow is intentional, silence the sanitizer. It works completely reliably on sane compilers and architectures.

---
 tools/lib/bpf/btf.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/btf.c +1 -0
index 8024fe355ca8..be1dafd56a13 100644
@@ -3127,6 +3127,7 @@ struct btf_dedup {
 	struct strset *strs_set;
 };
 
+__attribute__((no_sanitize("signed-integer-overflow")))
 static long hash_combine(long h, long value)
 {
 	return h * 31 + value;
Signature · 1 line
2.30.2
maintainer
#4 lore raw

Re: [PATCH bpf-next 03/13] libbpf: prevent UBSan from complaining about integer overflow

Quoted 14 lines · Andrii Nakryiko

Integer overflow is intentional, silence the sanitizer. It works completely reliably on sane compilers and architectures.

---
  tools/lib/bpf/btf.c | 1 +
  1 file changed, 1 insertion(+)
tools/lib/bpf/btf.c +0 -0
index 8024fe355ca8..be1dafd56a13 100644

tools/lib/bpf/btf.c struct btf_dedup { · line 3133

Quoted 7 lines
tools/lib/bpf/btf.c +1 -0
@@ -3127,6 +3127,7 @@ struct btf_dedup {
  	struct strset *strs_set;
  };
 
+__attribute__((no_sanitize("signed-integer-overflow")))
  static long hash_combine(long h, long value)
  {
  	return h * 31 + value;

Sgtm, I guess my only question, was there a reason for not using e.g. __u64 in the first place? Meaning, __u64 hash_combine(__u64 h, __u64 value) plus the call-sites where you have h variable re-feeding into hash_combine().

maintainer
#5 lore raw

Re: [PATCH bpf-next 03/13] libbpf: prevent UBSan from complaining about integer overflow

Quoted 9 lines · Daniel Borkmann
Quoted 8 lines · Andrii Nakryiko

Integer overflow is intentional, silence the sanitizer. It works completely reliably on sane compilers and architectures.

---
  tools/lib/bpf/btf.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 16 lines

tools/lib/bpf/btf.c struct btf_dedup { · line 3133

Quoted 12 lines
tools/lib/bpf/btf.c +1 -0
index 8024fe355ca8..be1dafd56a13 100644
@@ -3127,6 +3127,7 @@ struct btf_dedup {
       struct strset *strs_set;
   };
+__attribute__((no_sanitize("signed-integer-overflow")))
   static long hash_combine(long h, long value)
   {
       return h * 31 + value;
 

Sgtm, I guess my only question, was there a reason for not using e.g. __u64 in the first place? Meaning, __u64 hash_combine(__u64 h, __u64 value) plus the call-sites where you have h variable re-feeding into hash_combine().

Given the remainder of the series is all straight forward, I took that in already, but would still be nice if we can silence the sanitizer complaint w/o such attribute workaround.

Thanks,
Daniel

#6 lore raw

Re: [PATCH bpf-next 03/13] libbpf: prevent UBSan from complaining about integer overflow

Quoted 28 lines · Daniel Borkmann
Quoted 9 lines · Daniel Borkmann
Quoted 8 lines · Andrii Nakryiko

Integer overflow is intentional, silence the sanitizer. It works completely reliably on sane compilers and architectures.

---
  tools/lib/bpf/btf.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 16 lines

tools/lib/bpf/btf.c struct btf_dedup { · line 3133

Quoted 12 lines
tools/lib/bpf/btf.c +1 -0
index 8024fe355ca8..be1dafd56a13 100644
@@ -3127,6 +3127,7 @@ struct btf_dedup {
      struct strset *strs_set;
  };
+__attribute__((no_sanitize("signed-integer-overflow")))
  static long hash_combine(long h, long value)
  {
      return h * 31 + value;
 

Sgtm, I guess my only question, was there a reason for not using e.g. __u64 in the first place? Meaning, __u64 hash_combine(__u64 h, __u64 value) plus the call-sites where you have h variable re-feeding into hash_combine().

Quoted 3 lines

Given the remainder of the series is all straight forward, I took that in already, but would still be nice if we can silence the sanitizer complaint w/o such attribute workaround.

You are right, I'll follow up with u64 conversion and will drop the attribute.

Quoted 3 lines

Thanks,
Daniel

author maintainer
#7 lore raw

PATCH 4/13 libbpf: don't call libc APIs with NULL pointers

In reply to Andrii Nakryiko

Sanitizer complains about qsort(), bsearch(), and memcpy() being called with NULL pointer. This can only happen when the associated number of elements is zero, so no harm should be done. But still prevent this from happening to keep sanitizer runs clean from extra noise.

---
 tools/lib/bpf/libbpf.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +7 -3
index af405c38aadc..23f84757c806 100644
@@ -3369,7 +3369,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
 
 	/* sort BPF programs by section name and in-section instruction offset
 	 * for faster search */
-	qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
+	if (obj->nr_programs)
+		qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
 
 	return bpf_object__init_btf(obj, btf_data, btf_ext_data);
 }
@@ -5816,6 +5817,8 @@ static int cmp_relo_by_insn_idx(const void *key, const void *elem)
 
 static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
 {
+	if (!prog->nr_reloc)
+		return NULL;
 	return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
 		       sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
 }
@@ -5831,8 +5834,9 @@ static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_progra
 	relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
 	if (!relos)
 		return -ENOMEM;
-	memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
-	       sizeof(*relos) * subprog->nr_reloc);
+	if (subprog->nr_reloc)
+		memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
+		       sizeof(*relos) * subprog->nr_reloc);
 
 	for (i = main_prog->nr_reloc; i < new_cnt; i++)
 		relos[i].insn_idx += subprog->sub_insn_off;
Signature · 1 line
2.30.2
author maintainer
#8 lore raw

PATCH 5/13 libbpf: fix glob_syms memory leak in bpf_linker

In reply to Andrii Nakryiko

glob_syms array wasn't freed on bpf_link__free(). Fix that.

Fixes a46349227cd8 ("libbpf: Add linker extern resolution support for functions and global variables")
---
 tools/lib/bpf/linker.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/linker.c +1 -0
index 594b206fa674..3e1b2a15fdc7 100644
@@ -210,6 +210,7 @@ void bpf_linker__free(struct bpf_linker *linker)
 	}
 	free(linker->secs);
 
+	free(linker->glob_syms);
 	free(linker);
 }
 
Signature · 1 line
2.30.2
author maintainer
#9 lore raw

PATCH 6/13 libbpf: fix using invalidated memory in bpf_linker

In reply to Andrii Nakryiko

add_dst_sec() can invalidate bpf_linker's section index making dst_symtab pointer pointing into unallocated memory. Reinitialize dst_symtab pointer on each iteration to make sure it's always valid.

Fixes faf6ed321cf6 ("libbpf: Add BPF static linker APIs")
---
 tools/lib/bpf/linker.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/linker.c +4 -1
index 3e1b2a15fdc7..9aa016fb55aa 100644
@@ -2000,7 +2000,7 @@ static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj)
 {
 	struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx];
-	struct dst_sec *dst_symtab = &linker->secs[linker->symtab_sec_idx];
+	struct dst_sec *dst_symtab;
 	int i, err;
 
 	for (i = 1; i < obj->sec_cnt; i++) {
@@ -2033,6 +2033,9 @@ static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *ob
 			return -1;
 		}
 
+		/* add_dst_sec() above could have invalidated linker->secs */
+		dst_symtab = &linker->secs[linker->symtab_sec_idx];
+
 		/* shdr->sh_link points to SYMTAB */
 		dst_sec->shdr->sh_link = linker->symtab_sec_idx;
 
Signature · 1 line
2.30.2
author maintainer
#10 lore raw

PATCH 7/13 selftests/bpf: fix UBSan complaint about signed __int128 overflow

In reply to Andrii Nakryiko

Test is using __int128 variable as unsigned and highest order bit can be set to 1 after bit shift. Use unsigned __int128 explicitly and prevent UBSan from complaining.

---
 tools/testing/selftests/bpf/prog_tests/btf_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/btf_dump.c +1 -1
index af47aeb211e7..9e26903f9170 100644
@@ -323,7 +323,7 @@ static void test_btf_dump_int_data(struct btf *btf, struct btf_dump *d,
 				   char *str)
 {
 #ifdef __SIZEOF_INT128__
-	__int128 i = 0xffffffffffffffff;
+	unsigned __int128 i = 0xffffffffffffffff;
 
 	/* this dance is required because we cannot directly initialize
 	 * a 128-bit value to anything larger than a 64-bit value.
Signature · 1 line
2.30.2
author maintainer
#11 lore raw

PATCH 8/13 selftests/bpf: fix possible NULL passed to memcpy() with zero size

In reply to Andrii Nakryiko

Prevent sanitizer from complaining about passing NULL into memcpy(), even if it happens with zero size.

---
 tools/testing/selftests/bpf/prog_tests/core_reloc.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/core_reloc.c +2 -1
index 1041d0c593f6..44a9868c70ea 100644
@@ -881,7 +881,8 @@ void test_core_reloc(void)
 		data = mmap_data;
 
 		memset(mmap_data, 0, sizeof(*data));
-		memcpy(data->in, test_case->input, test_case->input_len);
+		if (test_case->input_len)
+			memcpy(data->in, test_case->input, test_case->input_len);
 		data->my_pid_tgid = my_pid_tgid;
 
 		link = bpf_program__attach_raw_tracepoint(prog, tp_name);
Signature · 1 line
2.30.2
author maintainer
#12 lore raw

PATCH 9/13 selftests/bpf: prevent misaligned memory access in get_stack_raw_tp test

In reply to Andrii Nakryiko

Perfbuf doesn't guarantee 8-byte alignment of the data like BPF ringbuf does, so struct get_stack_trace_t can arrive not properly aligned for subsequent u64 accesses. Easiest fix is to just copy data locally.

---
 .../selftests/bpf/prog_tests/get_stack_raw_tp.c    | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c +10 -4
index 4184c399d4c6..977ab433a946 100644
@@ -24,13 +24,19 @@ static void get_stack_print_output(void *ctx, int cpu, void *data, __u32 size)
 {
 	bool good_kern_stack = false, good_user_stack = false;
 	const char *nonjit_func = "___bpf_prog_run";
-	struct get_stack_trace_t *e = data;
+	/* perfbuf-submitted data is 4-byte aligned, but we need 8-byte
+	 * alignment, so copy data into a local variable, for simplicity
+	 */
+	struct get_stack_trace_t e;
 	int i, num_stack;
 	static __u64 cnt;
 	struct ksym *ks;
 
 	cnt++;
 
+	memset(&e, 0, sizeof(e));
+	memcpy(&e, data, size <= sizeof(e) ? size : sizeof(e));
+
 	if (size < sizeof(struct get_stack_trace_t)) {
 		__u64 *raw_data = data;
 		bool found = false;
@@ -57,19 +63,19 @@ static void get_stack_print_output(void *ctx, int cpu, void *data, __u32 size)
 			good_user_stack = true;
 		}
 	} else {
-		num_stack = e->kern_stack_size / sizeof(__u64);
+		num_stack = e.kern_stack_size / sizeof(__u64);
 		if (env.jit_enabled) {
 			good_kern_stack = num_stack > 0;
 		} else {
 			for (i = 0; i < num_stack; i++) {
-				ks = ksym_search(e->kern_stack[i]);
+				ks = ksym_search(e.kern_stack[i]);
 				if (ks && (strcmp(ks->name, nonjit_func) == 0)) {
 					good_kern_stack = true;
 					break;
 				}
 			}
 		}
-		if (e->user_stack_size > 0 && e->user_stack_buildid_size > 0)
+		if (e.user_stack_size > 0 && e.user_stack_buildid_size > 0)
 			good_user_stack = true;
 	}
 
Signature · 1 line
2.30.2
author maintainer
#13 lore raw

PATCH 10/13 selftests/bpf: fix misaligned memory access in queue_stack_map test

In reply to Andrii Nakryiko

Copy over iphdr into a local variable before accessing its fields.

---
 .../selftests/bpf/prog_tests/queue_stack_map.c       | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c +7 -5
index 8ccba3ab70ee..b9822f914eeb 100644
@@ -14,7 +14,7 @@ static void test_queue_stack_map_by_type(int type)
 	int i, err, prog_fd, map_in_fd, map_out_fd;
 	char file[32], buf[128];
 	struct bpf_object *obj;
-	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
+	struct iphdr iph;
 
 	/* Fill test values to be used */
 	for (i = 0; i < MAP_SIZE; i++)
@@ -60,15 +60,17 @@ static void test_queue_stack_map_by_type(int type)
 
 		err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
 					buf, &size, &retval, &duration);
-		if (err || retval || size != sizeof(pkt_v4) ||
-		    iph->daddr != val)
+		if (err || retval || size != sizeof(pkt_v4))
+			break;
+		memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
+		if (iph.daddr != val)
 			break;
 	}
 
-	CHECK(err || retval || size != sizeof(pkt_v4) || iph->daddr != val,
+	CHECK(err || retval || size != sizeof(pkt_v4) || iph.daddr != val,
 	      "bpf_map_pop_elem",
 	      "err %d errno %d retval %d size %d iph->daddr %u\n",
-	      err, errno, retval, size, iph->daddr);
+	      err, errno, retval, size, iph.daddr);
 
 	/* Queue is empty, program should return TC_ACT_SHOT */
 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
Signature · 1 line
2.30.2
author maintainer
#14 lore raw

PATCH 11/13 selftests/bpf: prevent out-of-bounds stack access in test_bpffs

In reply to Andrii Nakryiko

Buf can be not zero-terminated leading to strstr() to access data beyond the intended buf[] array. Fix by forcing zero termination.

---
 tools/testing/selftests/bpf/prog_tests/test_bpffs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/test_bpffs.c +3 -1
index d29ebfeef9c5..e36782c8ec49 100644
@@ -19,11 +19,13 @@ static int read_iter(char *file)
 	fd = open(file, 0);
 	if (fd < 0)
 		return -1;
-	while ((len = read(fd, buf, sizeof(buf))) > 0)
+	while ((len = read(fd, buf, sizeof(buf))) > 0) {
+		buf[sizeof(buf) - 1] = '\0';
 		if (strstr(buf, "iter")) {
 			close(fd);
 			return 0;
 		}
+	}
 	close(fd);
 	return -1;
 }
Signature · 1 line
2.30.2
author maintainer
#15 lore raw

PATCH 12/13 selftests/bpf: fix misaligned memory accesses in xdp_bonding test

In reply to Andrii Nakryiko

Construct packet buffer explicitly for each packet to avoid unaligned memory accesses.

---
 .../selftests/bpf/prog_tests/xdp_bonding.c    | 36 ++++++++++---------
 1 file changed, 20 insertions(+), 16 deletions(-)
tools/testing/selftests/bpf/prog_tests/xdp_bonding.c +20 -16
index faa22b84f2ee..5e3a26b15ec6 100644
@@ -218,9 +218,9 @@ static int send_udp_packets(int vary_dst_ip)
 		.h_dest = BOND2_MAC,
 		.h_proto = htons(ETH_P_IP),
 	};
-	uint8_t buf[128] = {};
-	struct iphdr *iph = (struct iphdr *)(buf + sizeof(eh));
-	struct udphdr *uh = (struct udphdr *)(buf + sizeof(eh) + sizeof(*iph));
+	struct iphdr iph = {};
+	struct udphdr uh = {};
+	uint8_t buf[128];
 	int i, s = -1;
 	int ifindex;
 
@@ -232,17 +232,16 @@ static int send_udp_packets(int vary_dst_ip)
 	if (!ASSERT_GT(ifindex, 0, "get bond1 ifindex"))
 		goto err;
 
-	memcpy(buf, &eh, sizeof(eh));
-	iph->ihl = 5;
-	iph->version = 4;
-	iph->tos = 16;
-	iph->id = 1;
-	iph->ttl = 64;
-	iph->protocol = IPPROTO_UDP;
-	iph->saddr = 1;
-	iph->daddr = 2;
-	iph->tot_len = htons(sizeof(buf) - ETH_HLEN);
-	iph->check = 0;
+	iph.ihl = 5;
+	iph.version = 4;
+	iph.tos = 16;
+	iph.id = 1;
+	iph.ttl = 64;
+	iph.protocol = IPPROTO_UDP;
+	iph.saddr = 1;
+	iph.daddr = 2;
+	iph.tot_len = htons(sizeof(buf) - ETH_HLEN);
+	iph.check = 0;
 
 	for (i = 1; i <= NPACKETS; i++) {
 		int n;
@@ -253,10 +252,15 @@ static int send_udp_packets(int vary_dst_ip)
 		};
 
 		/* vary the UDP destination port for even distribution with roundrobin/xor modes */
-		uh->dest++;
+		uh.dest++;
 
 		if (vary_dst_ip)
-			iph->daddr++;
+			iph.daddr++;
+
+		/* construct a packet */
+		memcpy(buf, &eh, sizeof(eh));
+		memcpy(buf + sizeof(eh), &iph, sizeof(iph));
+		memcpy(buf + sizeof(eh) + sizeof(iph), &uh, sizeof(uh));
 
 		n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&saddr_ll, sizeof(saddr_ll));
 		if (!ASSERT_EQ(n, sizeof(buf), "sendto"))
Signature · 1 line
2.30.2
author maintainer
#16 lore raw

PATCH 13/13 selftests/bpf: fix misaligned accesses in xdp and xdp_bpf2bpf tests

In reply to Andrii Nakryiko

Similar to previous patch, just copy over necessary struct into local stack variable before checking its fields.

---
 tools/testing/selftests/bpf/prog_tests/xdp.c         | 11 ++++++-----
 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c |  6 +++---
 2 files changed, 9 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/xdp.c +6 -5
index 7a7ef9d4e151..ac65456b7ab8 100644
@@ -11,8 +11,8 @@ void test_xdp(void)
 	const char *file = "./test_xdp.o";
 	struct bpf_object *obj;
 	char buf[128];
-	struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr);
-	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
+	struct ipv6hdr iph6;
+	struct iphdr iph;
 	__u32 duration, retval, size;
 	int err, prog_fd, map_fd;
 
@@ -28,16 +28,17 @@ void test_xdp(void)
 
 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
 				buf, &size, &retval, &duration);
-
+	memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
 	CHECK(err || retval != XDP_TX || size != 74 ||
-	      iph->protocol != IPPROTO_IPIP, "ipv4",
+	      iph.protocol != IPPROTO_IPIP, "ipv4",
 	      "err %d errno %d retval %d size %d\n",
 	      err, errno, retval, size);
 
 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
 				buf, &size, &retval, &duration);
+	memcpy(&iph6, buf + sizeof(struct ethhdr), sizeof(iph6));
 	CHECK(err || retval != XDP_TX || size != 114 ||
-	      iph6->nexthdr != IPPROTO_IPV6, "ipv6",
+	      iph6.nexthdr != IPPROTO_IPV6, "ipv6",
 	      "err %d errno %d retval %d size %d\n",
 	      err, errno, retval, size);
 out:
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c +3 -3
index f99386d1dc4c..c98a897ad692 100644
@@ -42,7 +42,7 @@ void test_xdp_bpf2bpf(void)
 	char buf[128];
 	int err, pkt_fd, map_fd;
 	bool passed = false;
-	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
+	struct iphdr iph;
 	struct iptnl_info value4 = {.family = AF_INET};
 	struct test_xdp *pkt_skel = NULL;
 	struct test_xdp_bpf2bpf *ftrace_skel = NULL;
@@ -93,9 +93,9 @@ void test_xdp_bpf2bpf(void)
 	/* Run test program */
 	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
 				buf, &size, &retval, &duration);
-
+	memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
 	if (CHECK(err || retval != XDP_TX || size != 74 ||
-		  iph->protocol != IPPROTO_IPIP, "ipv4",
+		  iph.protocol != IPPROTO_IPIP, "ipv4",
 		  "err %d errno %d retval %d size %d\n",
 		  err, errno, retval, size))
 		goto out;
Signature · 1 line
2.30.2