Skip to content

Commit

Permalink
libbpf: fix potential misaligned memory access in btf_ext__new()
Browse files Browse the repository at this point in the history
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: libbpf/libbpf#391

Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Fixes: 2993e05 ("tools/bpf: add support to read .BTF.ext sections")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
  • Loading branch information
anakryiko authored and Nobody committed Nov 24, 2021
1 parent f0cd536 commit 210d1ef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions tools/lib/bpf/btf.c
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tools/lib/bpf/btf.h
Expand Up @@ -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);
Expand Down

0 comments on commit 210d1ef

Please sign in to comment.