“69d8c25b145214f224c434f3692015e92fe2d795”上不存在“...sun/awt/git@gitcode.net:openanolis/dragonwell8_jdk.git”
提交 72c1c346 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

libbpf: Fix potential misaligned memory access in btf_ext__new()

mainline inclusion
from mainline-5.17-rc1
commit 401891a9
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=401891a9debaf0a684502f2aaecf53448cee9414

-------------------------------------------------

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: 2993e051 ("tools/bpf: add support to read .BTF.ext sections")
Reported-by: NEvgeny Vereshchagin <evvers@ya.ru>
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211124002325.1737739-3-andrii@kernel.org
(cherry picked from commit 401891a9)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 715cb080
...@@ -2723,15 +2723,11 @@ void btf_ext__free(struct btf_ext *btf_ext) ...@@ -2723,15 +2723,11 @@ void btf_ext__free(struct btf_ext *btf_ext)
free(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; struct btf_ext *btf_ext;
int err; int err;
err = btf_ext_parse_hdr(data, size);
if (err)
return libbpf_err_ptr(err);
btf_ext = calloc(1, sizeof(struct btf_ext)); btf_ext = calloc(1, sizeof(struct btf_ext));
if (!btf_ext) if (!btf_ext)
return libbpf_err_ptr(-ENOMEM); return libbpf_err_ptr(-ENOMEM);
...@@ -2744,6 +2740,10 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size) ...@@ -2744,6 +2740,10 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
} }
memcpy(btf_ext->data, data, 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)) { if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, line_info_len)) {
err = -EINVAL; err = -EINVAL;
goto done; goto done;
......
...@@ -83,7 +83,7 @@ LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, ...@@ -83,7 +83,7 @@ LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
__u32 expected_value_size, __u32 expected_value_size,
__u32 *key_type_id, __u32 *value_type_id); __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 void btf_ext__free(struct btf_ext *btf_ext);
LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
__u32 *size); __u32 *size);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册