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
110 case BPF_FUNC_get_prandom_u32:110 case BPF_FUNC_get_prandom_u32:
111 return &bpf_get_prandom_u32_proto;111 return &bpf_get_prandom_u32_proto;
112 case BPF_FUNC_trace_printk:112 case BPF_FUNC_trace_printk:
113 if (capable(CAP_SYS_ADMIN))113 if (perfmon_capable())
114 return bpf_get_trace_printk_proto();114 return bpf_get_trace_printk_proto();
115 /* fall through */115 /* fall through */
116 default:116 default:
include/linux/bpf.h+17−1
@@ -19,6 +19,7 @@ · 18 unmodified lines
19#include <linux/mutex.h>19#include <linux/mutex.h>
20#include <linux/module.h>20#include <linux/module.h>
21#include <linux/kallsyms.h>21#include <linux/kallsyms.h>
22#include <linux/capability.h>
22 23
23struct bpf_verifier_env;24struct bpf_verifier_env;
24struct bpf_verifier_log;25struct bpf_verifier_log;
@@ -119,7 +120,7 @@ struct bpf_map { · 94 unmodified lines
119 struct bpf_map_memory memory;120 struct bpf_map_memory memory;
120 char name[BPF_OBJ_NAME_LEN];121 char name[BPF_OBJ_NAME_LEN];
121 u32 btf_vmlinux_value_type_id;122 u32 btf_vmlinux_value_type_id;
122 bool unpriv_array;123 bool bypass_spec_v1;
123 bool frozen; /* write-once; write-protected by freeze_mutex */124 bool frozen; /* write-once; write-protected by freeze_mutex */
124 /* 22 bytes hole */125 /* 22 bytes hole */
125 126
@@ -1095,6 +1096,21 @@ struct bpf_map *bpf_map_get_curr_or_next(u32 *id); · 969 unmodified lines
1095 1096
1096extern int sysctl_unprivileged_bpf_disabled;1097extern int sysctl_unprivileged_bpf_disabled;
1097 1098
1099static inline bool bpf_allow_ptr_leaks(void)
1100{
1101 return perfmon_capable();
1102}
1103
1104static inline bool bpf_bypass_spec_v1(void)
1105{
1106 return perfmon_capable();
1107}
1108
1109static inline bool bpf_bypass_spec_v4(void)
1110{
1111 return perfmon_capable();
1112}
1113
1098int bpf_map_new_fd(struct bpf_map *map, int flags);1114int bpf_map_new_fd(struct bpf_map *map, int flags);
1099int bpf_prog_new_fd(struct bpf_prog *prog);1115int bpf_prog_new_fd(struct bpf_prog *prog);
1100 1116
include/linux/bpf_verifier.h+3−0
@@ -375,6 +375,9 @@ struct bpf_verifier_env { · 374 unmodified lines
375 u32 used_map_cnt; /* number of used maps */375 u32 used_map_cnt; /* number of used maps */
376 u32 id_gen; /* used to generate unique reg IDs */376 u32 id_gen; /* used to generate unique reg IDs */
377 bool allow_ptr_leaks;377 bool allow_ptr_leaks;
378 bool bpf_capable;
379 bool bypass_spec_v1;
380 bool bypass_spec_v4;
378 bool seen_direct_write;381 bool seen_direct_write;
379 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */382 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
380 const struct bpf_line_info *prev_linfo;383 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
77 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;77 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
78 int ret, numa_node = bpf_map_attr_numa_node(attr);78 int ret, numa_node = bpf_map_attr_numa_node(attr);
79 u32 elem_size, index_mask, max_entries;79 u32 elem_size, index_mask, max_entries;
80 bool unpriv = !capable(CAP_SYS_ADMIN);80 bool bypass_spec_v1 = bpf_bypass_spec_v1();
81 u64 cost, array_size, mask64;81 u64 cost, array_size, mask64;
82 struct bpf_map_memory mem;82 struct bpf_map_memory mem;
83 struct bpf_array *array;83 struct bpf_array *array;
@@ -95,7 +95,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 11 unmodified lines
95 mask64 -= 1;95 mask64 -= 1;
96 96
97 index_mask = mask64;97 index_mask = mask64;
98 if (unpriv) {98 if (!bypass_spec_v1) {
99 /* round up array size to nearest power of 2,99 /* round up array size to nearest power of 2,
100 * since cpu will speculate within index_mask limits100 * since cpu will speculate within index_mask limits
101 */101 */
@@ -149,7 +149,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 47 unmodified lines
149 return ERR_PTR(-ENOMEM);149 return ERR_PTR(-ENOMEM);
150 }150 }
151 array->index_mask = index_mask;151 array->index_mask = index_mask;
152 array->map.unpriv_array = unpriv;152 array->map.bypass_spec_v1 = bypass_spec_v1;
153 153
154 /* copy mandatory map attributes */154 /* copy mandatory map attributes */
155 bpf_map_init_from_attr(&array->map, attr);155 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
219 219
220 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));220 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
221 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);221 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
222 if (map->unpriv_array) {222 if (!map->bypass_spec_v1) {
223 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);223 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 4);
224 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);224 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
225 } else {225 } else {
@@ -1053,7 +1053,7 @@ static u32 array_of_map_gen_lookup(struct bpf_map *map, · 827 unmodified lines
1053 1053
1054 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));1054 *insn++ = BPF_ALU64_IMM(BPF_ADD, map_ptr, offsetof(struct bpf_array, value));
1055 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);1055 *insn++ = BPF_LDX_MEM(BPF_W, ret, index, 0);
1056 if (map->unpriv_array) {1056 if (!map->bypass_spec_v1) {
1057 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);1057 *insn++ = BPF_JMP_IMM(BPF_JGE, ret, map->max_entries, 6);
1058 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);1058 *insn++ = BPF_ALU32_IMM(BPF_AND, ret, array->index_mask);
1059 } else {1059 } 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
557 struct bpf_map *map;557 struct bpf_map *map;
558 int err;558 int err;
559 559
560 if (!capable(CAP_SYS_ADMIN))560 if (!bpf_capable())
561 return ERR_PTR(-EPERM);561 return ERR_PTR(-EPERM);
562 562
563 st_ops = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);563 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
646void bpf_prog_kallsyms_add(struct bpf_prog *fp)646void bpf_prog_kallsyms_add(struct bpf_prog *fp)
647{647{
648 if (!bpf_prog_kallsyms_candidate(fp) ||648 if (!bpf_prog_kallsyms_candidate(fp) ||
649 !capable(CAP_SYS_ADMIN))649 !bpf_capable())
650 return;650 return;
651 651
652 bpf_prog_ksym_set_addr(fp);652 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
85 u64 cost;85 u64 cost;
86 int ret;86 int ret;
87 87
88 if (!capable(CAP_SYS_ADMIN))88 if (!bpf_capable())
89 return ERR_PTR(-EPERM);89 return ERR_PTR(-EPERM);
90 90
91 /* check sanity of attributes */91 /* 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
359 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=359 BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) !=
360 offsetof(struct htab_elem, hash_node.pprev));360 offsetof(struct htab_elem, hash_node.pprev));
361 361
362 if (lru && !capable(CAP_SYS_ADMIN))362 if (lru && !bpf_capable())
363 /* LRU implementation is much complicated than other363 /* LRU implementation is much complicated than other
364 * maps. Hence, limit to CAP_SYS_ADMIN for now.364 * maps. Hence, limit to CAP_BPF.
365 */365 */
366 return -EPERM;366 return -EPERM;
367 367
kernel/bpf/helpers.c+3−1
@@ -633,7 +633,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 632 unmodified lines
633 break;633 break;
634 }634 }
635 635
636 if (!capable(CAP_SYS_ADMIN))636 if (!bpf_capable())
637 return NULL;637 return NULL;
638 638
639 switch (func_id) {639 switch (func_id) {
@@ -642,6 +642,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 2 unmodified lines
642 case BPF_FUNC_spin_unlock:642 case BPF_FUNC_spin_unlock:
643 return &bpf_spin_unlock_proto;643 return &bpf_spin_unlock_proto;
644 case BPF_FUNC_trace_printk:644 case BPF_FUNC_trace_printk:
645 if (!perfmon_capable())
646 return NULL;
645 return bpf_get_trace_printk_proto();647 return bpf_get_trace_printk_proto();
646 case BPF_FUNC_jiffies64:648 case BPF_FUNC_jiffies64:
647 return &bpf_jiffies64_proto;649 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
543 u64 cost = sizeof(*trie), cost_per_node;543 u64 cost = sizeof(*trie), cost_per_node;
544 int ret;544 int ret;
545 545
546 if (!capable(CAP_SYS_ADMIN))546 if (!bpf_capable())
547 return ERR_PTR(-EPERM);547 return ERR_PTR(-EPERM);
548 548
549 /* check sanity of attributes */549 /* 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
60 /* Misc members not needed in bpf_map_meta_equal() check. */60 /* Misc members not needed in bpf_map_meta_equal() check. */
61 inner_map_meta->ops = inner_map->ops;61 inner_map_meta->ops = inner_map->ops;
62 if (inner_map->ops == &array_map_ops) {62 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;
64 container_of(inner_map_meta, struct bpf_array, map)->index_mask =64 container_of(inner_map_meta, struct bpf_array, map)->index_mask =
65 container_of(inner_map, struct bpf_array, map)->index_mask;65 container_of(inner_map, struct bpf_array, map)->index_mask;
66 }66 }
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
45/* Called from syscall */45/* Called from syscall */
46static int queue_stack_map_alloc_check(union bpf_attr *attr)46static int queue_stack_map_alloc_check(union bpf_attr *attr)
47{47{
48 if (!capable(CAP_SYS_ADMIN))48 if (!bpf_capable())
49 return -EPERM;49 return -EPERM;
50 50
51 /* check sanity of attributes */51 /* 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
154 struct bpf_map_memory mem;154 struct bpf_map_memory mem;
155 u64 array_size;155 u64 array_size;
156 156
157 if (!capable(CAP_SYS_ADMIN))157 if (!bpf_capable())
158 return ERR_PTR(-EPERM);158 return ERR_PTR(-EPERM);
159 159
160 array_size = sizeof(*array);160 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
93 u64 cost, n_buckets;93 u64 cost, n_buckets;
94 int err;94 int err;
95 95
96 if (!capable(CAP_SYS_ADMIN))96 if (!bpf_capable())
97 return ERR_PTR(-EPERM);97 return ERR_PTR(-EPERM);
98 98
99 if (attr->map_flags & ~STACK_CREATE_FLAG_MASK)99 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
1534 err = -EBUSY;1534 err = -EBUSY;
1535 goto err_put;1535 goto err_put;
1536 }1536 }
1537 if (!capable(CAP_SYS_ADMIN)) {1537 if (!bpf_capable()) {
1538 err = -EPERM;1538 err = -EPERM;
1539 goto err_put;1539 goto err_put;
1540 }1540 }
@@ -2009,6 +2009,55 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, · 468 unmodified lines
2009 }2009 }
2010}2010}
2011 2011
2012static 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
2043static 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
2012/* last field in 'union bpf_attr' used by this command */2061/* last field in 'union bpf_attr' used by this command */
2013#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd2062#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2014 2063
@@ -2031,7 +2080,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) · 16 unmodified lines
2031 2080
2032 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&2081 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2033 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&2082 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2034 !capable(CAP_SYS_ADMIN))2083 !bpf_capable())
2035 return -EPERM;2084 return -EPERM;
2036 2085
2037 /* copy eBPF program license from user space */2086 /* 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
2044 is_gpl = license_is_gpl_compatible(license);2093 is_gpl = license_is_gpl_compatible(license);
2045 2094
2046 if (attr->insn_cnt == 0 ||2095 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))
2048 return -E2BIG;2097 return -E2BIG;
2049 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&2098 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2050 type != BPF_PROG_TYPE_CGROUP_SKB &&2099 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())
2052 return -EPERM;2106 return -EPERM;
2053 2107
2054 bpf_prog_load_fixup_attach_type(attr);2108 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
2682 case BPF_PROG_TYPE_CGROUP_SOCKOPT:2736 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2683 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;2737 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2684 case BPF_PROG_TYPE_CGROUP_SKB:2738 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;
2685 return prog->enforce_expected_attach_type &&2744 return prog->enforce_expected_attach_type &&
2686 prog->expected_attach_type != attach_type ?2745 prog->expected_attach_type != attach_type ?
2687 -EINVAL : 0;2746 -EINVAL : 0;
@@ -2747,9 +2806,6 @@ static int bpf_prog_attach(const union bpf_attr *attr) · 59 unmodified lines
2747 struct bpf_prog *prog;2806 struct bpf_prog *prog;
2748 int ret;2807 int ret;
2749 2808
2750 if (!capable(CAP_NET_ADMIN))
2751 return -EPERM;
2752
2753 if (CHECK_ATTR(BPF_PROG_ATTACH))2809 if (CHECK_ATTR(BPF_PROG_ATTACH))
2754 return -EINVAL;2810 return -EINVAL;
2755 2811
@@ -2804,9 +2860,6 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 48 unmodified lines
2804{2860{
2805 enum bpf_prog_type ptype;2861 enum bpf_prog_type ptype;
2806 2862
2807 if (!capable(CAP_NET_ADMIN))
2808 return -EPERM;
2809
2810 if (CHECK_ATTR(BPF_PROG_DETACH))2863 if (CHECK_ATTR(BPF_PROG_DETACH))
2811 return -EINVAL;2864 return -EINVAL;
2812 2865
@@ -2819,6 +2872,8 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 6 unmodified lines
2819 case BPF_PROG_TYPE_LIRC_MODE2:2872 case BPF_PROG_TYPE_LIRC_MODE2:
2820 return lirc_prog_detach(attr);2873 return lirc_prog_detach(attr);
2821 case BPF_PROG_TYPE_FLOW_DISSECTOR:2874 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2875 if (!capable(CAP_NET_ADMIN))
2876 return -EPERM;
2822 return skb_flow_dissector_bpf_prog_detach(attr);2877 return skb_flow_dissector_bpf_prog_detach(attr);
2823 case BPF_PROG_TYPE_CGROUP_DEVICE:2878 case BPF_PROG_TYPE_CGROUP_DEVICE:
2824 case BPF_PROG_TYPE_CGROUP_SKB:2879 case BPF_PROG_TYPE_CGROUP_SKB:
@@ -2882,8 +2937,6 @@ static int bpf_prog_test_run(const union bpf_attr *attr, · 57 unmodified lines
2882 struct bpf_prog *prog;2937 struct bpf_prog *prog;
2883 int ret = -ENOTSUPP;2938 int ret = -ENOTSUPP;
2884 2939
2885 if (!capable(CAP_SYS_ADMIN))
2886 return -EPERM;
2887 if (CHECK_ATTR(BPF_PROG_TEST_RUN))2940 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2888 return -EINVAL;2941 return -EINVAL;
2889 2942
@@ -3184,7 +3237,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, · 294 unmodified lines
3184 info.run_time_ns = stats.nsecs;3237 info.run_time_ns = stats.nsecs;
3185 info.run_cnt = stats.cnt;3238 info.run_cnt = stats.cnt;
3186 3239
3187 if (!capable(CAP_SYS_ADMIN)) {3240 if (!bpf_capable()) {
3188 info.jited_prog_len = 0;3241 info.jited_prog_len = 0;
3189 info.xlated_prog_len = 0;3242 info.xlated_prog_len = 0;
3190 info.nr_jited_ksyms = 0;3243 info.nr_jited_ksyms = 0;
@@ -3543,7 +3596,7 @@ static int bpf_btf_load(const union bpf_attr *attr) · 352 unmodified lines
3543 if (CHECK_ATTR(BPF_BTF_LOAD))3596 if (CHECK_ATTR(BPF_BTF_LOAD))
3544 return -EINVAL;3597 return -EINVAL;
3545 3598
3546 if (!capable(CAP_SYS_ADMIN))3599 if (!bpf_capable())
3547 return -EPERM;3600 return -EPERM;
3548 3601
3549 return btf_new_fd(attr);3602 return btf_new_fd(attr);
@@ -3766,9 +3819,6 @@ static int link_create(union bpf_attr *attr) · 216 unmodified lines
3766 struct bpf_prog *prog;3819 struct bpf_prog *prog;
3767 int ret;3820 int ret;
3768 3821
3769 if (!capable(CAP_NET_ADMIN))
3770 return -EPERM;
3771
3772 if (CHECK_ATTR(BPF_LINK_CREATE))3822 if (CHECK_ATTR(BPF_LINK_CREATE))
3773 return -EINVAL;3823 return -EINVAL;
3774 3824
@@ -3817,9 +3867,6 @@ static int link_update(union bpf_attr *attr) · 42 unmodified lines
3817 u32 flags;3867 u32 flags;
3818 int ret;3868 int ret;
3819 3869
3820 if (!capable(CAP_NET_ADMIN))
3821 return -EPERM;
3822
3823 if (CHECK_ATTR(BPF_LINK_UPDATE))3870 if (CHECK_ATTR(BPF_LINK_UPDATE))
3824 return -EINVAL;3871 return -EINVAL;
3825 3872
@@ -3988,7 +4035,7 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz · 162 unmodified lines
3988 union bpf_attr attr;4035 union bpf_attr attr;
3989 int err;4036 int err;
3990 4037
3991 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))4038 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
3992 return -EPERM;4039 return -EPERM;
3993 4040
3994 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);4041 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
1295 reg->type = SCALAR_VALUE;1295 reg->type = SCALAR_VALUE;
1296 reg->var_off = tnum_unknown;1296 reg->var_off = tnum_unknown;
1297 reg->frameno = 0;1297 reg->frameno = 0;
1298 reg->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks;1298 reg->precise = env->subprog_cnt > 1 || !env->bpf_capable;
1299 __mark_reg_unbounded(reg);1299 __mark_reg_unbounded(reg);
1300}1300}
1301 1301
@@ -1427,8 +1427,9 @@ static int check_subprogs(struct bpf_verifier_env *env) · 125 unmodified lines
1427 continue;1427 continue;
1428 if (insn[i].src_reg != BPF_PSEUDO_CALL)1428 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1429 continue;1429 continue;
1430 if (!env->allow_ptr_leaks) {1430 if (!env->bpf_capable) {
1431 verbose(env, "function calls to other bpf functions are allowed for root only\n");1431 verbose(env,
1432 "function calls to other bpf functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n");
1432 return -EPERM;1433 return -EPERM;
1433 }1434 }
1434 ret = add_subprog(env, i + insn[i].imm + 1);1435 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
1962 bool new_marks = false;1963 bool new_marks = false;
1963 int i, err;1964 int i, err;
1964 1965
1965 if (!env->allow_ptr_leaks)1966 if (!env->bpf_capable)
1966 /* backtracking is root only for now */
1967 return 0;1967 return 0;
1968 1968
1969 func = st->frame[st->curframe];1969 func = st->frame[st->curframe];
@@ -2211,7 +2211,7 @@ static int check_stack_write(struct bpf_verifier_env *env, · 241 unmodified lines
2211 reg = &cur->regs[value_regno];2211 reg = &cur->regs[value_regno];
2212 2212
2213 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&2213 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) {
2215 if (dst_reg != BPF_REG_FP) {2215 if (dst_reg != BPF_REG_FP) {
2216 /* The backtracking logic can only recognize explicit2216 /* The backtracking logic can only recognize explicit
2217 * stack slot address like [fp - 8]. Other spill of2217 * 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
2237 return -EINVAL;2237 return -EINVAL;
2238 }2238 }
2239 2239
2240 if (!env->allow_ptr_leaks) {2240 if (!env->bypass_spec_v4) {
2241 bool sanitize = false;2241 bool sanitize = false;
2242 2242
2243 if (state->stack[spi].slot_type[0] == STACK_SPILL &&2243 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
3432 * Spectre masking for stack ALU.3432 * Spectre masking for stack ALU.
3433 * See also retrieve_ptr_limit().3433 * See also retrieve_ptr_limit().
3434 */3434 */
3435 if (!env->allow_ptr_leaks) {3435 if (!env->bypass_spec_v1) {
3436 char tn_buf[48];3436 char tn_buf[48];
3437 3437
3438 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);3438 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
4435 4435
4436 if (!BPF_MAP_PTR(aux->map_ptr_state))4436 if (!BPF_MAP_PTR(aux->map_ptr_state))
4437 bpf_map_ptr_store(aux, meta->map_ptr,4437 bpf_map_ptr_store(aux, meta->map_ptr,
4438 meta->map_ptr->unpriv_array);4438 !meta->map_ptr->bypass_spec_v1);
4439 else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr)4439 else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr)
4440 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,4440 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
4441 meta->map_ptr->unpriv_array);4441 !meta->map_ptr->bypass_spec_v1);
4442 return 0;4442 return 0;
4443}4443}
4444 4444
@@ -4807,7 +4807,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, · 362 unmodified lines
4807static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,4807static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
4808 const struct bpf_insn *insn)4808 const struct bpf_insn *insn)
4809{4809{
4810 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;4810 return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K;
4811}4811}
4812 4812
4813static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,4813static 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
5117 /* For unprivileged we require that resulting offset must be in bounds5117 /* For unprivileged we require that resulting offset must be in bounds
5118 * in order to be able to sanitize access later on.5118 * in order to be able to sanitize access later on.
5119 */5119 */
5120 if (!env->allow_ptr_leaks) {5120 if (!env->bypass_spec_v1) {
5121 if (dst_reg->type == PTR_TO_MAP_VALUE &&5121 if (dst_reg->type == PTR_TO_MAP_VALUE &&
5122 check_map_access(env, dst, dst_reg->off, 1, false)) {5122 check_map_access(env, dst, dst_reg->off, 1, false)) {
5123 verbose(env, "R%d pointer arithmetic of map value goes out of range, "5123 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
7244 insn_stack[env->cfg.cur_stack++] = w;7244 insn_stack[env->cfg.cur_stack++] = w;
7245 return 1;7245 return 1;
7246 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {7246 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
7247 if (loop_ok && env->allow_ptr_leaks)7247 if (loop_ok && env->bpf_capable)
7248 return 0;7248 return 0;
7249 verbose_linfo(env, t, "%d: ", t);7249 verbose_linfo(env, t, "%d: ", t);
7250 verbose_linfo(env, w, "%d: ", w);7250 verbose_linfo(env, w, "%d: ", w);
@@ -8353,7 +8353,7 @@ next: · 1102 unmodified lines
8353 if (env->max_states_per_insn < states_cnt)8353 if (env->max_states_per_insn < states_cnt)
8354 env->max_states_per_insn = states_cnt;8354 env->max_states_per_insn = states_cnt;
8355 8355
8356 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)8356 if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
8357 return push_jmp_history(env, cur);8357 return push_jmp_history(env, cur);
8358 8358
8359 if (!add_new_state)8359 if (!add_new_state)
@@ -10014,7 +10014,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) · 1654 unmodified lines
10014 insn->code = BPF_JMP | BPF_TAIL_CALL;10014 insn->code = BPF_JMP | BPF_TAIL_CALL;
10015 10015
10016 aux = &env->insn_aux_data[i + delta];10016 aux = &env->insn_aux_data[i + delta];
10017 if (env->allow_ptr_leaks && !expect_blinding &&10017 if (env->bpf_capable && !expect_blinding &&
10018 prog->jit_requested &&10018 prog->jit_requested &&
10019 !bpf_map_key_poisoned(aux) &&10019 !bpf_map_key_poisoned(aux) &&
10020 !bpf_map_ptr_poisoned(aux) &&10020 !bpf_map_ptr_poisoned(aux) &&
@@ -10758,7 +10758,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · 737 unmodified lines
10758 env->insn_aux_data[i].orig_idx = i;10758 env->insn_aux_data[i].orig_idx = i;
10759 env->prog = *prog;10759 env->prog = *prog;
10760 env->ops = bpf_verifier_ops[env->prog->type];10760 env->ops = bpf_verifier_ops[env->prog->type];
10761 is_priv = capable(CAP_SYS_ADMIN);10761 is_priv = bpf_capable();
10762 10762
10763 if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {10763 if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) {
10764 mutex_lock(&bpf_verifier_lock);10764 mutex_lock(&bpf_verifier_lock);
@@ -10799,7 +10799,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · 34 unmodified lines
10799 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)10799 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
10800 env->strict_alignment = false;10800 env->strict_alignment = false;
10801 10801
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();
10803 10806
10804 if (is_priv)10807 if (is_priv)
10805 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;10808 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
315 315
316static const struct bpf_func_proto *bpf_get_probe_write_proto(void)316static const struct bpf_func_proto *bpf_get_probe_write_proto(void)
317{317{
318 if (!capable(CAP_SYS_ADMIN))
319 return NULL;
320
318 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",321 pr_warn_ratelimited("%s[%d] is installing a program with bpf_probe_write_user helper that may corrupt user memory!",
319 current->comm, task_pid_nr(current));322 current->comm, task_pid_nr(current));
320 323
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
625 !attr->btf_key_type_id || !attr->btf_value_type_id)625 !attr->btf_key_type_id || !attr->btf_value_type_id)
626 return -EINVAL;626 return -EINVAL;
627 627
628 if (!capable(CAP_SYS_ADMIN))628 if (!bpf_capable())
629 return -EPERM;629 return -EPERM;
630 630
631 if (attr->value_size > MAX_VALUE_SIZE)631 if (attr->value_size > MAX_VALUE_SIZE)
@@ -978,7 +978,7 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) · 346 unmodified lines
978 /* bpf_sk_storage_map is currently limited to CAP_SYS_ADMIN as978 /* bpf_sk_storage_map is currently limited to CAP_SYS_ADMIN as
979 * the map_alloc_check() side also does.979 * the map_alloc_check() side also does.
980 */980 */
981 if (!capable(CAP_SYS_ADMIN))981 if (!bpf_capable())
982 return ERR_PTR(-EPERM);982 return ERR_PTR(-EPERM);
983 983
984 nla_for_each_nested(nla, nla_stgs, rem) {984 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
6687 return false;6687 return false;
6688 case bpf_ctx_range(struct __sk_buff, data):6688 case bpf_ctx_range(struct __sk_buff, data):
6689 case bpf_ctx_range(struct __sk_buff, data_end):6689 case bpf_ctx_range(struct __sk_buff, data_end):
6690 if (!capable(CAP_SYS_ADMIN))6690 if (!bpf_capable())
6691 return false;6691 return false;
6692 break;6692 break;
6693 }6693 }
@@ -6699,7 +6699,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 5 unmodified lines
6699 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):6699 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6700 break;6700 break;
6701 case bpf_ctx_range(struct __sk_buff, tstamp):6701 case bpf_ctx_range(struct __sk_buff, tstamp):
6702 if (!capable(CAP_SYS_ADMIN))6702 if (!bpf_capable())
6703 return false;6703 return false;
6704 break;6704 break;
6705 default:6705 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