@@ -1213,6 +1213,9 @@ int btf_struct_access(struct bpf_verifier_log *log,
const struct btf_type *t, int off, int size,
enum bpf_access_type atype,
u32 *next_btf_id);
+int btf_struct_address(struct bpf_verifier_log *log,
+ const struct btf_type *t,
+ u32 off, u32 exp_id);
int btf_resolve_helper_id(struct bpf_verifier_log *log,
const struct bpf_func_proto *fn, int);
@@ -4004,6 +4004,75 @@ int btf_struct_access(struct bpf_verifier_log *log,
return -EINVAL;
}
+int btf_struct_address(struct bpf_verifier_log *log,
+ const struct btf_type *t,
+ u32 off, u32 exp_id)
+{
+ u32 i, moff, mtrue_end, msize = 0;
+ const struct btf_member *member;
+ const struct btf_type *mtype;
+ const char *tname, *mname;
+
+again:
+ tname = __btf_name_by_offset(btf_vmlinux, t->name_off);
+ if (!btf_type_is_struct(t)) {
+ bpf_log(log, "Type '%s' is not a struct\n", tname);
+ return -EINVAL;
+ }
+
+ if (off > t->size) {
+ bpf_log(log, "address beyond struct %s at off %u size %u\n",
+ tname, off, t->size);
+ return -EACCES;
+ }
+
+ for_each_member(i, t, member) {
+ /* offset of the field in bytes */
+ moff = btf_member_bit_offset(t, member) / 8;
+ if (off < moff)
+ /* won't find anything, field is already too far */
+ break;
+
+ /* we found the member */
+ if (off == moff && member->type == exp_id)
+ return 0;
+
+ /* type of the field */
+ mtype = btf_type_by_id(btf_vmlinux, member->type);
+ mname = __btf_name_by_offset(btf_vmlinux, member->name_off);
+
+ mtype = btf_resolve_size(btf_vmlinux, mtype, &msize,
+ NULL, NULL);
+ if (IS_ERR(mtype)) {
+ bpf_log(log, "field %s doesn't have size\n", mname);
+ return -EFAULT;
+ }
+
+ mtrue_end = moff + msize;
+ if (off >= mtrue_end)
+ /* no overlap with member, keep iterating */
+ continue;
+
+ /* the 'off' we're looking for is either equal to start
+ * of this field or inside of this struct
+ */
+ if (btf_type_is_struct(mtype)) {
+ /* our field must be inside that union or struct */
+ t = mtype;
+
+ /* adjust offset we're looking for */
+ off -= moff;
+ goto again;
+ }
+
+ bpf_log(log, "struct %s doesn't have struct field at offset %d\n", tname, off);
+ return -EACCES;
+ }
+
+ bpf_log(log, "struct %s doesn't have field at offset %d\n", tname, off);
+ return -EACCES;
+}
+
static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
int arg)
{
@@ -3665,6 +3665,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
{
struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
enum bpf_reg_type expected_type, type = reg->type;
+ const struct btf_type *btf_type;
int err = 0;
if (arg_type == ARG_DONTCARE)
@@ -3743,17 +3744,28 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
expected_type = PTR_TO_BTF_ID;
if (type != expected_type)
goto err_type;
- if (reg->btf_id != meta->btf_id) {
- verbose(env, "Helper has type %s got %s in R%d\n",
- kernel_type_name(meta->btf_id),
- kernel_type_name(reg->btf_id), regno);
+ if (reg->off) {
+ btf_type = btf_type_by_id(btf_vmlinux, reg->btf_id);
+ if (btf_struct_address(&env->log, btf_type, reg->off, meta->btf_id)) {
+ verbose(env, "Helper has type %s got %s in R%d, off %d\n",
+ kernel_type_name(meta->btf_id),
+ kernel_type_name(reg->btf_id), regno, reg->off);
- return -EACCES;
- }
- if (!tnum_is_const(reg->var_off) || reg->var_off.value || reg->off) {
- verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n",
- regno);
- return -EACCES;
+ return -EACCES;
+ }
+ } else {
+ if (reg->btf_id != meta->btf_id) {
+ verbose(env, "Helper has type %s got %s in R%d\n",
+ kernel_type_name(meta->btf_id),
+ kernel_type_name(reg->btf_id), regno);
+
+ return -EACCES;
+ }
+ if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
+ verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n",
+ regno);
+ return -EACCES;
+ }
}
} else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
if (meta->func_id == BPF_FUNC_spin_lock) {
Adding btf_struct_address function that takes 2 BTF objects and offset as arguments and checks wether object A is nested in object B on given offset. This function is be used when checking the helper function PTR_TO_BTF_ID arguments. If the argument has an offset value, the btf_struct_address will check if the final address is the expected BTF ID. This way we can access nested BTF objects under PTR_TO_BTF_ID pointer type and pass them to helpers, while they still point to valid kernel BTF objects. Signed-off-by: Jiri Olsa <jolsa@kernel.org> --- include/linux/bpf.h | 3 ++ kernel/bpf/btf.c | 69 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 32 +++++++++++++------- 3 files changed, 94 insertions(+), 10 deletions(-)