summaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/libbpf.c
diff options
context:
space:
mode:
authorAndrii Nakryiko2019-05-29 19:36:03 +0200
committerDaniel Borkmann2019-05-30 01:23:34 +0200
commit8ca990ce0d402d5aaf05f7a33ff025fcbcbb5f93 (patch)
tree4a5d05ae4f807e0a9ac4a9ab5599f57e2adac9ff /tools/lib/bpf/libbpf.c
parentlibbpf: prevent overwriting of log_level in bpf_object__load_progs() (diff)
downloadkernel-qcow2-linux-8ca990ce0d402d5aaf05f7a33ff025fcbcbb5f93.tar.gz
kernel-qcow2-linux-8ca990ce0d402d5aaf05f7a33ff025fcbcbb5f93.tar.xz
kernel-qcow2-linux-8ca990ce0d402d5aaf05f7a33ff025fcbcbb5f93.zip
libbpf: fix detection of corrupted BPF instructions section
Ensure that size of a section w/ BPF instruction is exactly a multiple of BPF instruction size. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Song Liu <songliubraving@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r--tools/lib/bpf/libbpf.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 30cb08e2eb75..c985a7916e35 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -349,8 +349,11 @@ static int
bpf_program__init(void *data, size_t size, char *section_name, int idx,
struct bpf_program *prog)
{
- if (size < sizeof(struct bpf_insn)) {
- pr_warning("corrupted section '%s'\n", section_name);
+ const size_t bpf_insn_sz = sizeof(struct bpf_insn);
+
+ if (size == 0 || size % bpf_insn_sz) {
+ pr_warning("corrupted section '%s', size: %zu\n",
+ section_name, size);
return -EINVAL;
}
@@ -376,9 +379,8 @@ bpf_program__init(void *data, size_t size, char *section_name, int idx,
section_name);
goto errout;
}
- prog->insns_cnt = size / sizeof(struct bpf_insn);
- memcpy(prog->insns, data,
- prog->insns_cnt * sizeof(struct bpf_insn));
+ prog->insns_cnt = size / bpf_insn_sz;
+ memcpy(prog->insns, data, size);
prog->idx = idx;
prog->instances.fds = NULL;
prog->instances.nr = -1;