History

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.

Alexei Starovoitov committed 7 years ago

Commit ccfe29e1 parent 8580ac94 files · +17 −4Browse files

Unified · Split

4 files
  1. include/linux/bpf.h +1 −0
  2. include/uapi/linux/bpf.h +1 −0
  3. kernel/bpf/syscall.c +14 −4
  4. tools/include/uapi/linux/bpf.h +1 −0
include/linux/bpf.h+1−0
@@ -375,6 +375,7 @@ struct bpf_prog_aux { · 374 unmodified lines
375375 u32 id;
376376 u32 func_cnt; /* used by non-func prog as the number of func progs */
377377 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 */
378379 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
379380 bool offload_requested;
380381 struct bpf_prog **func;
include/uapi/linux/bpf.h+1−0
@@ -420,6 +420,7 @@ union bpf_attr { · 419 unmodified lines
420420 __u32 line_info_rec_size; /* userspace bpf_line_info size */
421421 __aligned_u64 line_info; /* line info */
422422 __u32 line_info_cnt; /* number of bpf_line_info records */
423+ __u32 attach_btf_id; /* in-kernel BTF type id to attach to */
423424 };
424425
425426 struct { /* anonymous struct used by BPF_OBJ_* commands */
kernel/bpf/syscall.c+14−4
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323 #include <linux/timekeeping.h>
2424 #include <linux/ctype.h>
2525 #include <linux/nospec.h>
26+#include <uapi/linux/btf.h>
2627
2728 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
2829 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
@@ -1565,8 +1566,9 @@ static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr) · 1536 unmodified lines
15651566 }
15661567
15671568 static int
1568−bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1569− enum bpf_attach_type expected_attach_type)
1569+bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1570+ enum bpf_attach_type expected_attach_type,
1571+ u32 btf_id)
15701572 {
15711573 switch (prog_type) {
15721574 case BPF_PROG_TYPE_CGROUP_SOCK:
@@ -1608,13 +1610,19 @@ bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type, · 35 unmodified lines
16081610 default:
16091611 return -EINVAL;
16101612 }
1613+ case BPF_PROG_TYPE_RAW_TRACEPOINT:
1614+ if (btf_id > BTF_MAX_TYPE)
1615+ return -EINVAL;
1616+ return 0;
16111617 default:
1618+ if (btf_id)
1619+ return -EINVAL;
16121620 return 0;
16131621 }
16141622 }
16151623
16161624 /* last field in 'union bpf_attr' used by this command */
1617−#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1625+#define BPF_PROG_LOAD_LAST_FIELD attach_btf_id
16181626
16191627 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
16201628 {
@@ -1656,7 +1664,8 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · 35 unmodified lines
16561664 return -EPERM;
16571665
16581666 bpf_prog_load_fixup_attach_type(attr);
1659− if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1667+ if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
1668+ attr->attach_btf_id))
16601669 return -EINVAL;
16611670
16621671 /* plain bpf_prog allocation */
@@ -1665,6 +1674,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · 2 unmodified lines
16651674 return -ENOMEM;
16661675
16671676 prog->expected_attach_type = attr->expected_attach_type;
1677+ prog->aux->attach_btf_id = attr->attach_btf_id;
16681678
16691679 prog->aux->offload_requested = !!attr->prog_ifindex;
16701680
tools/include/uapi/linux/bpf.h+1−0
@@ -420,6 +420,7 @@ union bpf_attr { · 419 unmodified lines
420420 __u32 line_info_rec_size; /* userspace bpf_line_info size */
421421 __aligned_u64 line_info; /* line info */
422422 __u32 line_info_cnt; /* number of bpf_line_info records */
423+ __u32 attach_btf_id; /* in-kernel BTF type id to attach to */
423424 };
424425
425426 struct { /* anonymous struct used by BPF_OBJ_* commands */