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