History

bpf: Check attach type at prog load time

== The problem == There are use-cases when a program of some type can be attached to multiple attach points and those attach points must have different permissions to access context or to call helpers. E.g. context structure may have fields for both IPv4 and IPv6 but it doesn't make sense to read from / write to IPv6 field when attach point is somewhere in IPv4 stack. Same applies to BPF-helpers: it may make sense to call some helper from some attach point, but not from other for same prog type. == The solution == Introduce `expected_attach_type` field in in `struct bpf_attr` for `BPF_PROG_LOAD` command. If scenario described in "The problem" section is the case for some prog type, the field will be checked twice: 1) At load time prog type is checked to see if attach type for it must be known to validate program permissions correctly. Prog will be rejected with EINVAL if it's the case and `expected_attach_type` is not specified or has invalid value. 2) At attach time `attach_type` is compared with `expected_attach_type`, if prog type requires to have one, and, if they differ, attach will be rejected with EINVAL. The `expected_attach_type` is now available as part of `struct bpf_prog` in both `bpf_verifier_ops->is_valid_access()` and `bpf_verifier_ops->get_func_proto()` () and can be used to check context accesses and calls to helpers correspondingly. Initially the idea was discussed by Alexei Starovoitov <ast@fb.com> and Daniel Borkmann <daniel@iogearbox.net> here: https://marc.info/?l=linux-netdev&m=152107378717201&w=2

Andrey Ignatov committed 8 years ago

Commit 5e43f891 parent 807ae7d8 files · +88 −29Browse files

Showing the changes to include/uapi/linux (1 of 8 files). Show all

Unified · Split

include/uapi/linux/bpf.h+5−0
@@ -296,6 +296,11 @@ union bpf_attr { · 295 unmodified lines
296296 __u32 prog_flags;
297297 char prog_name[BPF_OBJ_NAME_LEN];
298298 __u32 prog_ifindex; /* ifindex of netdev to prep for */
299+ /* For some prog types expected attach type must be known at
300+ * load time to verify attach type specific parts of prog
301+ * (context accesses, allowed helpers, etc).
302+ */
303+ __u32 expected_attach_type;
299304 };
300305
301306 struct { /* anonymous struct used by BPF_OBJ_* commands */