Thread (56 messages) 56 messages, 6 authors, 2020-07-17

Re: [PATCH v4 bpf-next 07/14] bpf: Allow nested BTF object to be refferenced by BTF object + offset

From: Andrii Nakryiko <hidden>
Date: 2020-06-30 20:07:32
Also in: bpf

On Tue, Jun 30, 2020 at 1:05 PM Andrii Nakryiko
[off-list ref] wrote:
On Thu, Jun 25, 2020 at 4:49 PM Jiri Olsa [off-list ref] wrote:
quoted
Adding btf_struct_address function that takes 2 BTF objects
and offset as arguments and checks whether object A is nested
in object B on given offset.

This function will 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.

Using btf_struct_access to implement new btf_struct_address
function, because it already walks down the given BTF object.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 include/linux/bpf.h   |  3 ++
 kernel/bpf/btf.c      | 67 ++++++++++++++++++++++++++++++++++++++-----
 kernel/bpf/verifier.c | 37 +++++++++++++++---------
 3 files changed, 87 insertions(+), 20 deletions(-)
[...]
quoted
 error:
                bpf_log(log, "access beyond struct %s at off %u size %u\n",
@@ -4043,9 +4054,21 @@ int btf_struct_access(struct bpf_verifier_log *log,

                        /* adjust offset we're looking for */
                        off -= moff;
+
+                       /* We are nexting into another struct,
+                        * check if we are crossing expected ID.
+                        */
+                       if (data->op == ACCESS_EXPECT && !off && t == data->exp_type)
before you can do this type check, you need to btf_type_skip_modifiers() first.
Ignore this part, btf_resolve_size() (somewhat unexpectedly) already does that.
quoted
+                               return 0;
                        goto again;
                }

+               /* We are interested only in structs for expected ID,
+                * bail out.
+                */
+               if (data->op == ACCESS_EXPECT)
+                       return -EINVAL;
+
                if (btf_type_is_ptr(mtype)) {
                        const struct btf_type *stype;
                        u32 id;
[...]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help