All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>
Cc: <andrii@kernel.org>, <kernel-team@fb.com>
Subject: [PATCH bpf-next 09/13] selftests/bpf: prevent misaligned memory access in get_stack_raw_tp test
Date: Tue, 23 Nov 2021 16:23:21 -0800	[thread overview]
Message-ID: <20211124002325.1737739-10-andrii@kernel.org> (raw)
In-Reply-To: <20211124002325.1737739-1-andrii@kernel.org>

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.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../selftests/bpf/prog_tests/get_stack_raw_tp.c    | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c b/tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c
index 4184c399d4c6..977ab433a946 100644
--- a/tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c
+++ b/tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c
@@ -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;
 	}
 
-- 
2.30.2


  parent reply	other threads:[~2021-11-24  0:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-24  0:23 [PATCH bpf-next 00/13] Fix sanitizer-reported libbpf and selftest issues Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 01/13] tools/resolve_btf_ids: close ELF file on error Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 02/13] libbpf: fix potential misaligned memory access in btf_ext__new() Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 03/13] libbpf: prevent UBSan from complaining about integer overflow Andrii Nakryiko
2021-11-25 22:21   ` Daniel Borkmann
2021-11-25 23:19     ` Daniel Borkmann
2021-11-26  1:23       ` Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 04/13] libbpf: don't call libc APIs with NULL pointers Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 05/13] libbpf: fix glob_syms memory leak in bpf_linker Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 06/13] libbpf: fix using invalidated memory " Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 07/13] selftests/bpf: fix UBSan complaint about signed __int128 overflow Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 08/13] selftests/bpf: fix possible NULL passed to memcpy() with zero size Andrii Nakryiko
2021-11-24  0:23 ` Andrii Nakryiko [this message]
2021-11-24  0:23 ` [PATCH bpf-next 10/13] selftests/bpf: fix misaligned memory access in queue_stack_map test Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 11/13] selftests/bpf: prevent out-of-bounds stack access in test_bpffs Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 12/13] selftests/bpf: fix misaligned memory accesses in xdp_bonding test Andrii Nakryiko
2021-11-24  0:23 ` [PATCH bpf-next 13/13] selftests/bpf: fix misaligned accesses in xdp and xdp_bpf2bpf tests Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211124002325.1737739-10-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.