bpf: Support bitfield read access in btf_struct_access

This patch allows bitfield access as a scalar.

It checks "off + size > t->size" to avoid accessing bitfield
end up accessing beyond the struct.  This check is done
outside of the loop since it is applicable to all access.

It also takes this chance to break early on the "off < moff" case.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200109003501.3855427-1-kafai@fb.com
This commit is contained in:
Martin KaFai Lau 2020-01-08 16:35:01 -08:00 коммит произвёл Alexei Starovoitov
Родитель 218b3f65f9
Коммит 976aba002f
1 изменённых файлов: 39 добавлений и 5 удалений

Просмотреть файл

@ -3744,19 +3744,53 @@ again:
return -EINVAL; return -EINVAL;
} }
for_each_member(i, t, member) { if (off + size > t->size) {
if (btf_member_bitfield_size(t, member)) bpf_log(log, "access beyond struct %s at off %u size %u\n",
/* bitfields are not supported yet */ tname, off, size);
continue; return -EACCES;
}
for_each_member(i, t, member) {
/* offset of the field in bytes */ /* offset of the field in bytes */
moff = btf_member_bit_offset(t, member) / 8; moff = btf_member_bit_offset(t, member) / 8;
if (off + size <= moff) if (off + size <= moff)
/* won't find anything, field is already too far */ /* won't find anything, field is already too far */
break; break;
if (btf_member_bitfield_size(t, member)) {
u32 end_bit = btf_member_bit_offset(t, member) +
btf_member_bitfield_size(t, member);
/* off <= moff instead of off == moff because clang
* does not generate a BTF member for anonymous
* bitfield like the ":16" here:
* struct {
* int :16;
* int x:8;
* };
*/
if (off <= moff &&
BITS_ROUNDUP_BYTES(end_bit) <= off + size)
return SCALAR_VALUE;
/* off may be accessing a following member
*
* or
*
* Doing partial access at either end of this
* bitfield. Continue on this case also to
* treat it as not accessing this bitfield
* and eventually error out as field not
* found to keep it simple.
* It could be relaxed if there was a legit
* partial access case later.
*/
continue;
}
/* In case of "off" is pointing to holes of a struct */ /* In case of "off" is pointing to holes of a struct */
if (off < moff) if (off < moff)
continue; break;
/* type of the field */ /* type of the field */
mtype = btf_type_by_id(btf_vmlinux, member->type); mtype = btf_type_by_id(btf_vmlinux, member->type);