History

bpf: Implement CAP_BPF

Implement permissions as stated in uapi/linux/capability.h In order to do that the verifier allow_ptr_leaks flag is split into four flags and they are set as: env->allow_ptr_leaks = bpf_allow_ptr_leaks(); env->bypass_spec_v1 = bpf_bypass_spec_v1(); env->bypass_spec_v4 = bpf_bypass_spec_v4(); env->bpf_capable = bpf_capable(); The first three currently equivalent to perfmon_capable(), since leaking kernel pointers and reading kernel memory via side channel attacks is roughly equivalent to reading kernel memory with cap_perfmon. 'bpf_capable' enables bounded loops, precision tracking, bpf to bpf calls and other verifier features. 'allow_ptr_leaks' enable ptr leaks, ptr conversions, subtraction of pointers. 'bypass_spec_v1' disables speculative analysis in the verifier, run time mitigations in bpf array, and enables indirect variable access in bpf programs. 'bypass_spec_v4' disables emission of sanitation code by the verifier. That means that the networking BPF program loaded with CAP_BPF + CAP_NET_ADMIN will have speculative checks done by the verifier and other spectre mitigation applied. Such networking BPF program will not be able to leak kernel pointers and will not be able to access arbitrary kernel memory.

Alexei Starovoitov committed 6 years ago

Commit 2c78ee81 parent a17b53c19 files · +134 −60Browse files

Unified · Split

19 files
  1. drivers/media/rc/bpf-lirc.c +1 −1
  2. include/linux/bpf.h +17 −1
  3. include/linux/bpf_verifier.h +3 −0
  4. kernel/bpf/arraymap.c +5 −5
  5. kernel/bpf/bpf_struct_ops.c +1 −1
  6. kernel/bpf/core.c +1 −1
  7. kernel/bpf/cpumap.c +1 −1
  8. kernel/bpf/hashtab.c +2 −2
  9. kernel/bpf/helpers.c +3 −1
  10. kernel/bpf/lpm_trie.c +1 −1
  11. kernel/bpf/map_in_map.c +1 −1
  12. kernel/bpf/queue_stack_maps.c +1 −1
  13. kernel/bpf/reuseport_array.c +1 −1
  14. kernel/bpf/stackmap.c +1 −1
  15. kernel/bpf/syscall.c +68 −21
  16. kernel/bpf/verifier.c +20 −17
  17. kernel/trace/bpf_trace.c +3 −0
  18. net/core/bpf_sk_storage.c +2 −2
  19. net/core/filter.c +2 −2
drivers/media/rc/bpf-lirc.c+1−1
@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 109 unmodified lines
110110 case BPF_FUNC_get_prandom_u32:
111111 return &bpf_get_prandom_u32_proto;
112112 case BPF_FUNC_trace_printk:
113− if (capable(CAP_SYS_ADMIN))
113+ if (perfmon_capable())
114114 return bpf_get_trace_printk_proto();
115115 /* fall through */
116116 default:
include/linux/bpf.h+17−1
@@ -19,6 +19,7 @@ · 18 unmodified lines
1919 #include <linux/mutex.h>
2020 #include <linux/module.h>
2121 #include <linux/kallsyms.h>
22+#include <linux/capability.h>
2223
2324 struct bpf_verifier_env;
2425 struct bpf_verifier_log;
@@ -119,7 +120,7 @@ struct bpf_map { · 94 unmodified lines
119120 struct bpf_map_memory memory;
120121 char name[BPF_OBJ_NAME_LEN];
121122 u32 btf_vmlinux_value_type_id;
122− bool unpriv_array;
123+ bool bypass_spec_v1;
123124 bool frozen; /* write-once; write-protected by freeze_mutex */
124125 /* 22 bytes hole */
125126
@@ -1095,6 +1096,21 @@ struct bpf_map *bpf_map_get_curr_or_next(u32 *id); · 969 unmodified lines
10951096
10961097 extern int sysctl_unprivileged_bpf_disabled;
10971098
1099+static inline bool bpf_allow_ptr_leaks(void)
1100+{
1101+ return perfmon_capable();
1102+}
1103+
1104+static inline bool bpf_bypass_spec_v1(void)
1105+{
1106+ return perfmon_capable();
1107+}
1108+
1109+static inline bool bpf_bypass_spec_v4(void)
1110+{
1111+ return perfmon_capable();
1112+}
1113+
10981114 int bpf_map_new_fd(struct bpf_map *map, int flags);
10991115 int bpf_prog_new_fd(struct bpf_prog *prog);
11001116
include/linux/bpf_verifier.h+3−0
@@ -375,6 +375,9 @@ struct bpf_verifier_env { · 374 unmodified lines
375375 u32 used_map_cnt; /* number of used maps */
376376 u32 id_gen; /* used to generate unique reg IDs */
377377 bool allow_ptr_leaks;
378+ bool bpf_capable;
379+ bool bypass_spec_v1;
380+ bool bypass_spec_v4;
378381 bool seen_direct_write;
379382 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
380383 const struct bpf_line_info *prev_linfo;
kernel/bpf/arraymap.c+5−5
@@ -77,7 +77,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 76 unmodified lines
7777 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
7878 int ret, numa_node = bpf_map_attr_numa_node(attr);
7979 u32 elem_size, index_mask, max_entries;
80− bool unpriv = !capable(CAP_SYS_ADMIN);
80+ bool bypass_spec_v1 = bpf_bypass_spec_v1();
8181 u64 cost, array_size, mask64;
8282 struct bpf_map_memory mem;
8383 struct bpf_array *array;
@@ -95,7 +95,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 11 unmodified lines
9595 mask64 -= 1;
9696
9797 index_mask = mask64;
98− if (unpriv) {
98+ if (!bypass_spec_v1) {
9999 /* round up array size to nearest power of 2,
100100 * since cpu will speculate within index_mask limits
101101 */
@@ -149,7 +149,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 47 unmodified lines
149149 return ERR_PTR(-ENOMEM);
150150 }
151151 array->index_mask = index_mask;
152− array->map.unpriv_array = unpriv;
152+ array->map.bypass_spec_v1 = bypass_spec_v1;
153153
154154 /* copy mandatory map attributes */
155155 bpf_map_init_from_attr(&array->map, attr);
@@ -219,7 +219,7 @@ static u32 array_map_gen_lookup(struct bpf_map *map, struct bpf_insn *insn_buf) · 63 unmodified lines
219219
220220 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
221221 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
222− if (map->unpriv_array) {
222+ if (!map->bypass_spec_v1) {
223223 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
224224 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
225225 } else {
@@ -1053,7 +1053,7 @@ static u32 array_of_map_gen_lookup(struct bpf_map *map, · 827 unmodified lines
10531053
10541054 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
10551055 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
1056− if (map->unpriv_array) {
1056+ if (!map->bypass_spec_v1) {
10571057 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
10581058 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
10591059 } else {
kernel/bpf/bpf_struct_ops.c+1−1
@@ -557,7 +557,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · 556 unmodified lines
557557 struct bpf_map *map;
558558 int err;
559559
560− if (!capable(CAP_SYS_ADMIN))
560+ if (!bpf_capable())
561561 return ERR_PTR(-EPERM);
562562
563563 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
kernel/bpf/core.c+1−1
@@ -646,7 +646,7 @@ static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp) · 645 unmodified lines
646646 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
647647 {
648648 if (!bpf_prog_kallsyms_candidate(fp) ||
649− !capable(CAP_SYS_ADMIN))
649+ !bpf_capable())
650650 return;
651651
652652 bpf_prog_ksym_set_addr(fp);
kernel/bpf/cpumap.c+1−1
@@ -85,7 +85,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr) · 84 unmodified lines
8585 u64 cost;
8686 int ret;
8787
88− if (!capable(CAP_SYS_ADMIN))
88+ if (!bpf_capable())
8989 return ERR_PTR(-EPERM);
9090
9191 /* check sanity of attributes */
kernel/bpf/hashtab.c+2−2
@@ -359,9 +359,9 @@ static int htab_map_alloc_check(union bpf_attr *attr) · 358 unmodified lines
359359 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
360360 offsetof(struct htab_elem, hash_node.pprev));
361361
362− if (lru && !capable(CAP_SYS_ADMIN))
362+ if (lru && !bpf_capable())
363363 /* LRU implementation is much complicated than other
364− * maps. Hence, limit to CAP_SYS_ADMIN for now.
364+ * maps. Hence, limit to CAP_BPF.
365365 */
366366 return -EPERM;
367367
kernel/bpf/helpers.c+3−1
@@ -633,7 +633,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 632 unmodified lines
633633 break;
634634 }
635635
636− if (!capable(CAP_SYS_ADMIN))
636+ if (!bpf_capable())
637637 return NULL;
638638
639639 switch (func_id) {
@@ -642,6 +642,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 2 unmodified lines
642642 case BPF_FUNC_spin_unlock:
643643 return &bpf_spin_unlock_proto;
644644 case BPF_FUNC_trace_printk:
645+ if (!perfmon_capable())
646+ return NULL;
645647 return bpf_get_trace_printk_proto();
646648 case BPF_FUNC_jiffies64:
647649 return &bpf_jiffies64_proto;
kernel/bpf/lpm_trie.c+1−1
@@ -543,7 +543,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr) · 542 unmodified lines
543543 u64 cost = sizeof(*trie), cost_per_node;
544544 int ret;
545545
546− if (!capable(CAP_SYS_ADMIN))
546+ if (!bpf_capable())
547547 return ERR_PTR(-EPERM);
548548
549549 /* check sanity of attributes */
kernel/bpf/map_in_map.c+1−1
@@ -60,7 +60,7 @@ struct bpf_map *bpf_map_meta_alloc(int inner_map_ufd) · 59 unmodified lines
6060 /* Misc members not needed in bpf_map_meta_equal() check. */
6161 inner_map_meta->ops = inner_map->ops;
6262 if (inner_map->ops == &array_map_ops) {
63− inner_map_meta->unpriv_array = inner_map->unpriv_array;
63+ inner_map_meta->bypass_spec_v1 = inner_map->bypass_spec_v1;
6464 container_of(inner_map_meta, struct bpf_array, map)->index_mask =
6565 container_of(inner_map, struct bpf_array, map)->index_mask;
6666 }
kernel/bpf/queue_stack_maps.c+1−1
@@ -45,7 +45,7 @@ static bool queue_stack_map_is_full(struct bpf_queue_stack *qs) · 44 unmodified lines
4545 /* Called from syscall */
4646 static int queue_stack_map_alloc_check(union bpf_attr *attr)
4747 {
48− if (!capable(CAP_SYS_ADMIN))
48+ if (!bpf_capable())
4949 return -EPERM;
5050
5151 /* check sanity of attributes */
kernel/bpf/reuseport_array.c+1−1
@@ -154,7 +154,7 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr) · 153 unmodified lines
154154 struct bpf_map_memory mem;
155155 u64 array_size;
156156
157− if (!capable(CAP_SYS_ADMIN))
157+ if (!bpf_capable())
158158 return ERR_PTR(-EPERM);
159159
160160 array_size = sizeof(*array);
kernel/bpf/stackmap.c+1−1
@@ -93,7 +93,7 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr) · 92 unmodified lines
9393 u64 cost, n_buckets;
9494 int err;
9595
96− if (!capable(CAP_SYS_ADMIN))
96+ if (!bpf_capable())
9797 return ERR_PTR(-EPERM);
9898
9999 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)
kernel/bpf/syscall.c+68−21
@@ -1534,7 +1534,7 @@ static int map_freeze(const union bpf_attr *attr) · 1533 unmodified lines
15341534 err = -EBUSY;
15351535 goto err_put;
15361536 }
1537− if (!capable(CAP_SYS_ADMIN)) {
1537+ if (!bpf_capable()) {
15381538 err = -EPERM;
15391539 goto err_put;
15401540 }
@@ -2009,6 +2009,55 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, · 468 unmodified lines
20092009 }
20102010 }
20112011
2012+static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2013+{
2014+ switch (prog_type) {
2015+ case BPF_PROG_TYPE_SCHED_CLS:
2016+ case BPF_PROG_TYPE_SCHED_ACT:
2017+ case BPF_PROG_TYPE_XDP:
2018+ case BPF_PROG_TYPE_LWT_IN:
2019+ case BPF_PROG_TYPE_LWT_OUT:
2020+ case BPF_PROG_TYPE_LWT_XMIT:
2021+ case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2022+ case BPF_PROG_TYPE_SK_SKB:
2023+ case BPF_PROG_TYPE_SK_MSG:
2024+ case BPF_PROG_TYPE_LIRC_MODE2:
2025+ case BPF_PROG_TYPE_FLOW_DISSECTOR:
2026+ case BPF_PROG_TYPE_CGROUP_DEVICE:
2027+ case BPF_PROG_TYPE_CGROUP_SOCK:
2028+ case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2029+ case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2030+ case BPF_PROG_TYPE_CGROUP_SYSCTL:
2031+ case BPF_PROG_TYPE_SOCK_OPS:
2032+ case BPF_PROG_TYPE_EXT: /* extends any prog */
2033+ return true;
2034+ case BPF_PROG_TYPE_CGROUP_SKB:
2035+ /* always unpriv */
2036+ case BPF_PROG_TYPE_SK_REUSEPORT:
2037+ /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2038+ default:
2039+ return false;
2040+ }
2041+}
2042+
2043+static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2044+{
2045+ switch (prog_type) {
2046+ case BPF_PROG_TYPE_KPROBE:
2047+ case BPF_PROG_TYPE_TRACEPOINT:
2048+ case BPF_PROG_TYPE_PERF_EVENT:
2049+ case BPF_PROG_TYPE_RAW_TRACEPOINT:
2050+ case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2051+ case BPF_PROG_TYPE_TRACING:
2052+ case BPF_PROG_TYPE_LSM:
2053+ case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2054+ case BPF_PROG_TYPE_EXT: /* extends any prog */
2055+ return true;
2056+ default:
2057+ return false;
2058+ }
2059+}
2060+
20122061 /* last field in 'union bpf_attr' used by this command */
20132062 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
20142063
@@ -2031,7 +2080,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · 16 unmodified lines
20312080
20322081 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
20332082 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2034− !capable(CAP_SYS_ADMIN))
2083+ !bpf_capable())
20352084 return -EPERM;
20362085
20372086 /* copy eBPF program license from user space */
@@ -2044,11 +2093,16 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · 6 unmodified lines
20442093 is_gpl = license_is_gpl_compatible(license);
20452094
20462095 if (attr->insn_cnt == 0 ||
2047− attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2096+ attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
20482097 return -E2BIG;
20492098 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
20502099 type != BPF_PROG_TYPE_CGROUP_SKB &&
2051− !capable(CAP_SYS_ADMIN))
2100+ !bpf_capable())
2101+ return -EPERM;
2102+
2103+ if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN))
2104+ return -EPERM;
2105+ if (is_perfmon_prog_type(type) && !perfmon_capable())
20522106 return -EPERM;
20532107
20542108 bpf_prog_load_fixup_attach_type(attr);
@@ -2682,6 +2736,11 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, · 627 unmodified lines
26822736 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
26832737 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
26842738 case BPF_PROG_TYPE_CGROUP_SKB:
2739+ if (!capable(CAP_NET_ADMIN))
2740+ /* cg-skb progs can be loaded by unpriv user.
2741+ * check permissions at attach time.
2742+ */
2743+ return -EPERM;
26852744 return prog->enforce_expected_attach_type &&
26862745 prog->expected_attach_type != attach_type ?
26872746 -EINVAL : 0;
@@ -2747,9 +2806,6 @@ static int bpf_prog_attach(const union bpf_attr *attr) · 59 unmodified lines
27472806 struct bpf_prog *prog;
27482807 int ret;
27492808
2750− if (!capable(CAP_NET_ADMIN))
2751− return -EPERM;
2752−
27532809 if (CHECK_ATTR(BPF_PROG_ATTACH))
27542810 return -EINVAL;
27552811
@@ -2804,9 +2860,6 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 48 unmodified lines
28042860 {
28052861 enum bpf_prog_type ptype;
28062862
2807− if (!capable(CAP_NET_ADMIN))
2808− return -EPERM;
2809−
28102863 if (CHECK_ATTR(BPF_PROG_DETACH))
28112864 return -EINVAL;
28122865
@@ -2819,6 +2872,8 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 6 unmodified lines
28192872 case BPF_PROG_TYPE_LIRC_MODE2:
28202873 return lirc_prog_detach(attr);
28212874 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2875+ if (!capable(CAP_NET_ADMIN))
2876+ return -EPERM;
28222877 return skb_flow_dissector_bpf_prog_detach(attr);
28232878 case BPF_PROG_TYPE_CGROUP_DEVICE:
28242879 case BPF_PROG_TYPE_CGROUP_SKB:
@@ -2882,8 +2937,6 @@ static int bpf_prog_test_run(const union bpf_attr *attr, · 57 unmodified lines
28822937 struct bpf_prog *prog;
28832938 int ret = -ENOTSUPP;
28842939
2885− if (!capable(CAP_SYS_ADMIN))
2886− return -EPERM;
28872940 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
28882941 return -EINVAL;
28892942
@@ -3184,7 +3237,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, · 294 unmodified lines
31843237 info.run_time_ns = stats.nsecs;
31853238 info.run_cnt = stats.cnt;
31863239
3187− if (!capable(CAP_SYS_ADMIN)) {
3240+ if (!bpf_capable()) {
31883241 info.jited_prog_len = 0;
31893242 info.xlated_prog_len = 0;
31903243 info.nr_jited_ksyms = 0;
@@ -3543,7 +3596,7 @@ static int bpf_btf_load(const union bpf_attr *attr) · 352 unmodified lines
35433596 if (CHECK_ATTR(BPF_BTF_LOAD))
35443597 return -EINVAL;
35453598
3546− if (!capable(CAP_SYS_ADMIN))
3599+ if (!bpf_capable())
35473600 return -EPERM;
35483601
35493602 return btf_new_fd(attr);
@@ -3766,9 +3819,6 @@ static int link_create(union bpf_attr *attr) · 216 unmodified lines
37663819 struct bpf_prog *prog;
37673820 int ret;
37683821
3769− if (!capable(CAP_NET_ADMIN))
3770− return -EPERM;
3771−
37723822 if (CHECK_ATTR(BPF_LINK_CREATE))
37733823 return -EINVAL;
37743824
@@ -3817,9 +3867,6 @@ static int link_update(union bpf_attr *attr) · 42 unmodified lines
38173867 u32 flags;
38183868 int ret;
38193869
3820− if (!capable(CAP_NET_ADMIN))
3821− return -EPERM;
3822−
38233870 if (CHECK_ATTR(BPF_LINK_UPDATE))
38243871 return -EINVAL;
38253872
@@ -3988,7 +4035,7 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz · 162 unmodified lines
39884035 union bpf_attr attr;
39894036 int err;
39904037
3991− if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
4038+ if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
39924039 return -EPERM;
39934040
39944041 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
kernel/bpf/verifier.c+20−17
@@ -1295,7 +1295,7 @@ static void __mark_reg_unknown(const struct bpf_verifier_env *env, · 1294 unmodified lines
12951295 reg->type = SCALAR_VALUE;
12961296 reg->var_off = tnum_unknown;
12971297 reg->frameno = 0;
1298− reg->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks;
1298+ reg->precise = env->subprog_cnt > 1 || !env->bpf_capable;
12991299 __mark_reg_unbounded(reg);
13001300 }
13011301
@@ -1427,8 +1427,9 @@ static int check_subprogs(struct bpf_verifier_env *env) · 125 unmodified lines
14271427 continue;
14281428 if (insn[i].src_reg != BPF_PSEUDO_CALL)
14291429 continue;
1430− if (!env->allow_ptr_leaks) {
1431− verbose(env, "function calls to other bpf functions are allowed for root only\n");
1430+ if (!env->bpf_capable) {
1431+ verbose(env,
1432+ "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n");
14321433 return -EPERM;
14331434 }
14341435 ret = add_subprog(env, i + insn[i].imm + 1);
@@ -1962,8 +1963,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, · 527 unmodified lines
19621963 bool new_marks = false;
19631964 int i, err;
19641965
1965− if (!env->allow_ptr_leaks)
1966− /* backtracking is root only for now */
1966+ if (!env->bpf_capable)
19671967 return 0;
19681968
19691969 func = st->frame[st->curframe];
@@ -2211,7 +2211,7 @@ static int check_stack_write(struct bpf_verifier_env *env, · 241 unmodified lines
22112211 reg = &cur->regs[value_regno];
22122212
22132213 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&
2214− !register_is_null(reg) && env->allow_ptr_leaks) {
2214+ !register_is_null(reg) && env->bpf_capable) {
22152215 if (dst_reg != BPF_REG_FP) {
22162216 /* The backtracking logic can only recognize explicit
22172217 * stack slot address like [fp - 8]. Other spill of
@@ -2237,7 +2237,7 @@ static int check_stack_write(struct bpf_verifier_env *env, · 19 unmodified lines
22372237 return -EINVAL;
22382238 }
22392239
2240− if (!env->allow_ptr_leaks) {
2240+ if (!env->bypass_spec_v4) {
22412241 bool sanitize = false;
22422242
22432243 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
@@ -3432,7 +3432,7 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, · 1188 unmodified lines
34323432 * Spectre masking for stack ALU.
34333433 * See also retrieve_ptr_limit().
34343434 */
3435− if (!env->allow_ptr_leaks) {
3435+ if (!env->bypass_spec_v1) {
34363436 char tn_buf[48];
34373437
34383438 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
@@ -4435,10 +4435,10 @@ record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, · 996 unmodified lines
44354435
44364436 if (!BPF_MAP_PTR(aux->map_ptr_state))
44374437 bpf_map_ptr_store(aux, meta->map_ptr,
4438− meta->map_ptr->unpriv_array);
4438+ !meta->map_ptr->bypass_spec_v1);
44394439 else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr)
44404440 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
4441− meta->map_ptr->unpriv_array);
4441+ !meta->map_ptr->bypass_spec_v1);
44424442 return 0;
44434443 }
44444444
@@ -4807,7 +4807,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, · 362 unmodified lines
48074807 static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
48084808 const struct bpf_insn *insn)
48094809 {
4810− return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
4810+ return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K;
48114811 }
48124812
48134813 static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
@@ -5117,7 +5117,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · 303 unmodified lines
51175117 /* For unprivileged we require that resulting offset must be in bounds
51185118 * in order to be able to sanitize access later on.
51195119 */
5120− if (!env->allow_ptr_leaks) {
5120+ if (!env->bypass_spec_v1) {
51215121 if (dst_reg->type == PTR_TO_MAP_VALUE &&
51225122 check_map_access(env, dst, dst_reg->off, 1, false)) {
51235123 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
@@ -7244,7 +7244,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, · 2120 unmodified lines
72447244 insn_stack[env->cfg.cur_stack++] = w;
72457245 return 1;
72467246 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
7247− if (loop_ok && env->allow_ptr_leaks)
7247+ if (loop_ok && env->bpf_capable)
72487248 return 0;
72497249 verbose_linfo(env, t, "%d: ", t);
72507250 verbose_linfo(env, w, "%d: ", w);
@@ -8353,7 +8353,7 @@ next: · 1102 unmodified lines
83538353 if (env->max_states_per_insn < states_cnt)
83548354 env->max_states_per_insn = states_cnt;
83558355
8356− if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
8356+ if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
83578357 return push_jmp_history(env, cur);
83588358
83598359 if (!add_new_state)
@@ -10014,7 +10014,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) · 1654 unmodified lines
1001410014 insn->code = BPF_JMP | BPF_TAIL_CALL;
1001510015
1001610016 aux = &env->insn_aux_data[i + delta];
10017− if (env->allow_ptr_leaks && !expect_blinding &&
10017+ if (env->bpf_capable && !expect_blinding &&
1001810018 prog->jit_requested &&
1001910019 !bpf_map_key_poisoned(aux) &&
1002010020 !bpf_map_ptr_poisoned(aux) &&
@@ -10758,7 +10758,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · 737 unmodified lines
1075810758 env->insn_aux_data[i].orig_idx = i;
1075910759 env->prog = *prog;
1076010760 env->ops = bpf_verifier_ops[env->prog->type];
10761− is_priv = capable(CAP_SYS_ADMIN);
10761+ is_priv = bpf_capable();
1076210762
1076310763 if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
1076410764 mutex_lock(&bpf_verifier_lock);
@@ -10799,7 +10799,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · 34 unmodified lines
1079910799 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
1080010800 env->strict_alignment = false;
1080110801
10802− env->allow_ptr_leaks = is_priv;
10802+ env->allow_ptr_leaks = bpf_allow_ptr_leaks();
10803+ env->bypass_spec_v1 = bpf_bypass_spec_v1();
10804+ env->bypass_spec_v4 = bpf_bypass_spec_v4();
10805+ env->bpf_capable = bpf_capable();
1080310806
1080410807 if (is_priv)
1080510808 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
kernel/trace/bpf_trace.c+3−0
@@ -315,6 +315,9 @@ static const struct bpf_func_proto bpf_probe_write_user_proto = { · 314 unmodified lines
315315
316316 static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
317317 {
318+ if (!capable(CAP_SYS_ADMIN))
319+ return NULL;
320+
318321 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
319322 current->comm, task_pid_nr(current));
320323
net/core/bpf_sk_storage.c+2−2
@@ -625,7 +625,7 @@ static int bpf_sk_storage_map_alloc_check(union bpf_attr *attr) · 624 unmodified lines
625625 !attr->btf_key_type_id || !attr->btf_value_type_id)
626626 return -EINVAL;
627627
628− if (!capable(CAP_SYS_ADMIN))
628+ if (!bpf_capable())
629629 return -EPERM;
630630
631631 if (attr->value_size > MAX_VALUE_SIZE)
@@ -978,7 +978,7 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) · 346 unmodified lines
978978 /* bpf_sk_storage_map is currently limited to CAP_SYS_ADMIN as
979979 * the map_alloc_check() side also does.
980980 */
981− if (!capable(CAP_SYS_ADMIN))
981+ if (!bpf_capable())
982982 return ERR_PTR(-EPERM);
983983
984984 nla_for_each_nested(nla, nla_stgs, rem) {
net/core/filter.c+2−2
@@ -6687,7 +6687,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 6686 unmodified lines
66876687 return false;
66886688 case bpf_ctx_range(struct __sk_buff, data):
66896689 case bpf_ctx_range(struct __sk_buff, data_end):
6690− if (!capable(CAP_SYS_ADMIN))
6690+ if (!bpf_capable())
66916691 return false;
66926692 break;
66936693 }
@@ -6699,7 +6699,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 5 unmodified lines
66996699 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
67006700 break;
67016701 case bpf_ctx_range(struct __sk_buff, tstamp):
6702− if (!capable(CAP_SYS_ADMIN))
6702+ if (!bpf_capable())
67036703 return false;
67046704 break;
67056705 default:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t