bpf.vger.kernel.org archive mirror
 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>,
	Evgeny Vereshchagin <evvers@ya.ru>
Subject: [PATCH bpf-next 02/13] libbpf: fix potential misaligned memory access in btf_ext__new()
Date: Tue, 23 Nov 2021 16:23:14 -0800	[thread overview]
Message-ID: <20211124002325.1737739-3-andrii@kernel.org> (raw)
In-Reply-To: <20211124002325.1737739-1-andrii@kernel.org>

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

Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Fixes: 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 tools/lib/bpf/btf.c | 10 +++++-----
 tools/lib/bpf/btf.h |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index e97217a77196..8024fe355ca8 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -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;
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 5c73a5b0a044..742a2bf71c5e 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -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);
-- 
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 ` Andrii Nakryiko [this message]
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 ` [PATCH bpf-next 09/13] selftests/bpf: prevent misaligned memory access in get_stack_raw_tp test Andrii Nakryiko
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-3-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=evvers@ya.ru \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).