Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Add attach_btf_id attribute to program load

Add attach_btf_id attribute to prog_load command.
It's similar to existing expected_attach_type attribute which is
used in several cgroup based program types.
Unfortunately expected_attach_type is ignored for
tracing programs and cannot be reused for new purpose.
Hence introduce attach_btf_id to verify bpf programs against
given in-kernel BTF type id at load time.
It is strictly checked to be valid for raw_tp programs only.
In a later patches it will become:
btf_id == 0 semantics of existing raw_tp progs.
btd_id > 0 raw_tp with BTF and additional type safety.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191016032505.2089704-5-ast@kernel.org

authored by

Alexei Starovoitov and committed by
Daniel Borkmann
ccfe29eb 8580ac94

+17 -4
+1
include/linux/bpf.h
··· 375 375 u32 id; 376 376 u32 func_cnt; /* used by non-func prog as the number of func progs */ 377 377 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */ 378 + u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 378 379 bool verifier_zext; /* Zero extensions has been inserted by verifier. */ 379 380 bool offload_requested; 380 381 struct bpf_prog **func;
+1
include/uapi/linux/bpf.h
··· 420 420 __u32 line_info_rec_size; /* userspace bpf_line_info size */ 421 421 __aligned_u64 line_info; /* line info */ 422 422 __u32 line_info_cnt; /* number of bpf_line_info records */ 423 + __u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 423 424 }; 424 425 425 426 struct { /* anonymous struct used by BPF_OBJ_* commands */
+14 -4
kernel/bpf/syscall.c
··· 23 23 #include <linux/timekeeping.h> 24 24 #include <linux/ctype.h> 25 25 #include <linux/nospec.h> 26 + #include <uapi/linux/btf.h> 26 27 27 28 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ 28 29 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ ··· 1566 1565 } 1567 1566 1568 1567 static int 1569 - bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, 1570 - enum bpf_attach_type expected_attach_type) 1568 + bpf_prog_load_check_attach(enum bpf_prog_type prog_type, 1569 + enum bpf_attach_type expected_attach_type, 1570 + u32 btf_id) 1571 1571 { 1572 1572 switch (prog_type) { 1573 1573 case BPF_PROG_TYPE_CGROUP_SOCK: ··· 1610 1608 default: 1611 1609 return -EINVAL; 1612 1610 } 1611 + case BPF_PROG_TYPE_RAW_TRACEPOINT: 1612 + if (btf_id > BTF_MAX_TYPE) 1613 + return -EINVAL; 1614 + return 0; 1613 1615 default: 1616 + if (btf_id) 1617 + return -EINVAL; 1614 1618 return 0; 1615 1619 } 1616 1620 } 1617 1621 1618 1622 /* last field in 'union bpf_attr' used by this command */ 1619 - #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt 1623 + #define BPF_PROG_LOAD_LAST_FIELD attach_btf_id 1620 1624 1621 1625 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) 1622 1626 { ··· 1664 1656 return -EPERM; 1665 1657 1666 1658 bpf_prog_load_fixup_attach_type(attr); 1667 - if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type)) 1659 + if (bpf_prog_load_check_attach(type, attr->expected_attach_type, 1660 + attr->attach_btf_id)) 1668 1661 return -EINVAL; 1669 1662 1670 1663 /* plain bpf_prog allocation */ ··· 1674 1665 return -ENOMEM; 1675 1666 1676 1667 prog->expected_attach_type = attr->expected_attach_type; 1668 + prog->aux->attach_btf_id = attr->attach_btf_id; 1677 1669 1678 1670 prog->aux->offload_requested = !!attr->prog_ifindex; 1679 1671
+1
tools/include/uapi/linux/bpf.h
··· 420 420 __u32 line_info_rec_size; /* userspace bpf_line_info size */ 421 421 __aligned_u64 line_info; /* line info */ 422 422 __u32 line_info_cnt; /* number of bpf_line_info records */ 423 + __u32 attach_btf_id; /* in-kernel BTF type id to attach to */ 423 424 }; 424 425 425 426 struct { /* anonymous struct used by BPF_OBJ_* commands */