Message ID | 20190529011426.1328736-2-andriin@fb.com |
---|---|
State | Changes Requested |
Delegated to: | BPF Maintainers |
Headers | show |
Series | [bpf-next,1/9] libbpf: fix detection of corrupted BPF instructions section | expand |
On Tue, May 28, 2019 at 6:14 PM Andrii Nakryiko <andriin@fb.com> wrote: > > 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> > --- > tools/lib/bpf/libbpf.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index ca4432f5b067..05a73223e524 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 < bpf_insn_sz || size % bpf_insn_sz) { how about if (!size || 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, prog->insns_cnt * bpf_insn_sz); Given the check above, we can just use size in memcpy, right? Thanks, Song > prog->idx = idx; > prog->instances.fds = NULL; > prog->instances.nr = -1; > -- > 2.17.1 >
On Wed, May 29, 2019 at 10:01 AM Song Liu <liu.song.a23@gmail.com> wrote: > > On Tue, May 28, 2019 at 6:14 PM Andrii Nakryiko <andriin@fb.com> wrote: > > > > 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> > > --- > > tools/lib/bpf/libbpf.c | 12 +++++++----- > > 1 file changed, 7 insertions(+), 5 deletions(-) > > > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > index ca4432f5b067..05a73223e524 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 < bpf_insn_sz || size % bpf_insn_sz) { > > how about > if (!size || size % bpf_insn_sz) sure, why not. > > > + 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, prog->insns_cnt * bpf_insn_sz); > > Given the check above, we can just use size in memcpy, right? yep, good point, will update > > Thanks, > Song > > > prog->idx = idx; > > prog->instances.fds = NULL; > > prog->instances.nr = -1; > > -- > > 2.17.1 > >
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index ca4432f5b067..05a73223e524 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 < bpf_insn_sz || 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, prog->insns_cnt * bpf_insn_sz); prog->idx = idx; prog->instances.fds = NULL; prog->instances.nr = -1;
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> --- tools/lib/bpf/libbpf.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)