Message ID | 20230428010839.1328507-1-martin.lau@linux.dev (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | [bpf-next] libbpf: btf_dump_type_data_check_overflow needs to consider BTF_MEMBER_BITFIELD_SIZE | expand |
On 4/27/23 6:08 PM, Martin KaFai Lau wrote: > diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c > index 580985ee5545..8f659ec8798d 100644 > --- a/tools/lib/bpf/btf_dump.c > +++ b/tools/lib/bpf/btf_dump.c > @@ -2250,9 +2250,19 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d, > const struct btf_type *t, > __u32 id, > const void *data, > - __u8 bits_offset) > + __u8 bits_offset, > + __u8 bit_sz) > { > - __s64 size = btf__resolve_size(d->btf, id); > + __s64 size; > + > + if (bit_sz) { > + /* bits_offset is at most 7. bit_sz is at most 128. */ > + __u8 nr_bytes = (bits_offset + bit_sz + 7) / 8; > + > + return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : 0; hmm...returning 0 here is not very correct. nr_bytes should be returned instead, although does not seem anyone is using the value. will post v2. > + } > + > + size = btf__resolve_size(d->btf, id);
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 580985ee5545..8f659ec8798d 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -2250,9 +2250,19 @@ static int btf_dump_type_data_check_overflow(struct btf_dump *d, const struct btf_type *t, __u32 id, const void *data, - __u8 bits_offset) + __u8 bits_offset, + __u8 bit_sz) { - __s64 size = btf__resolve_size(d->btf, id); + __s64 size; + + if (bit_sz) { + /* bits_offset is at most 7. bit_sz is at most 128. */ + __u8 nr_bytes = (bits_offset + bit_sz + 7) / 8; + + return data + nr_bytes > d->typed_dump->data_end ? -E2BIG : 0; + } + + size = btf__resolve_size(d->btf, id); if (size < 0 || size >= INT_MAX) { pr_warn("unexpected size [%zu] for id [%u]\n", @@ -2407,7 +2417,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d, { int size, err = 0; - size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset); + size = btf_dump_type_data_check_overflow(d, t, id, data, bits_offset, bit_sz); if (size < 0) return size; err = btf_dump_type_data_check_zero(d, t, id, data, bits_offset, bit_sz);