Skip to content

Commit 2c78ee8

Browse files
Alexei Starovoitovborkmann
authored andcommitted
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. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200513230355.7858-3-alexei.starovoitov@gmail.com
1 parent a17b53c commit 2c78ee8

19 files changed

Lines changed: 134 additions & 60 deletions

‎drivers/media/rc/bpf-lirc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
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‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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 {
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);
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‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ struct bpf_verifier_env {
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‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
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)
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)
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)
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,
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
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‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ static int htab_map_alloc_check(union bpf_attr *attr)
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‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)
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)
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
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 */

0 commit comments

Comments
 (0)