CAP_BPF and CAP_TRACING
Subsystems (18)
Alexei Starovoitov
maintainerv4 cover CAP_BPF and CAP_TRACING
v3->v4:
- rebase and typo fixes
- split selftests into separate patch
- update perf* docs with CAP_TRACING
- add a note to commit log that existing unpriv bpf behavior is not changing
v2->v3:
- dropped ftrace and kallsyms from CAP_TRACING description. In the future these mechanisms can start using it too.
- added CAP_SYS_ADMIN backward compatibility.
Alexei Starovoitov (4):
capability: introduce CAP_BPF and CAP_TRACING
bpf: implement CAP_BPF
perf: implement CAP_TRACING
selftests/bpf: use CAP_BPF and CAP_TRACING in tests
Documentation/admin-guide/perf-security.rst | 4 +- Documentation/admin-guide/sysctl/kernel.rst | 10 ++--- arch/powerpc/perf/core-book3s.c | 4 +- arch/x86/events/intel/bts.c | 2 +- arch/x86/events/intel/core.c | 2 +- arch/x86/events/intel/p4.c | 2 +- include/linux/capability.h | 18 ++++++++ include/uapi/linux/capability.h | 49 ++++++++++++++++++++- kernel/bpf/arraymap.c | 2 +- kernel/bpf/cgroup.c | 2 +- kernel/bpf/core.c | 4 +- kernel/bpf/hashtab.c | 4 +- kernel/bpf/lpm_trie.c | 2 +- kernel/bpf/queue_stack_maps.c | 2 +- kernel/bpf/reuseport_array.c | 2 +- kernel/bpf/stackmap.c | 2 +- kernel/bpf/syscall.c | 32 ++++++++------ kernel/bpf/verifier.c | 2 +- kernel/events/core.c | 14 +++--- kernel/events/hw_breakpoint.c | 2 +- kernel/trace/bpf_trace.c | 2 +- kernel/trace/trace_event_perf.c | 4 +- net/core/bpf_sk_storage.c | 2 +- net/core/filter.c | 10 +++-- security/selinux/include/classmap.h | 4 +- tools/testing/selftests/bpf/test_verifier.c | 46 +++++++++++++++---- 26 files changed, 165 insertions(+), 64 deletions(-)
Signature · 1 line
2.20.0
Alexei Starovoitov
maintainerPATCH v4 1/4 capability: introduce CAP_BPF and CAP_TRACING
Split BPF and perf/tracing operations that are allowed under CAP_SYS_ADMIN into corresponding CAP_BPF and CAP_TRACING. For backward compatibility include them in CAP_SYS_ADMIN as well.
The end result provides simple safety model for applications that use BPF:
- for tracing program types
BPF_PROG_TYPE_{KPROBE, TRACEPOINT, PERF_EVENT, RAW_TRACEPOINT, etc} use CAP_BPF and CAP_TRACING
- for networking program types
BPF_PROG_TYPE_{SCHED_CLS, XDP, CGROUP_SKB, SK_SKB, etc}
use CAP_BPF and CAP_NET_ADMIN
There are few exceptions from this simple rule:
- bpf_trace_printk() is allowed in networking programs, but it's using ftrace mechanism, hence this helper needs additional CAP_TRACING.
- cpumap is used by XDP programs. Currently it's kept under CAP_SYS_ADMIN, but could be relaxed to CAP_NET_ADMIN in the future.
- BPF_F_ZERO_SEED flag for hash/lru map is allowed under CAP_SYS_ADMIN only to discourage production use.
- BPF HW offload is allowed under CAP_SYS_ADMIN.
- cg_sysctl, cg_device, lirc program types are neither networking nor tracing. They can be loaded under CAP_BPF, but attach is allowed under CAP_NET_ADMIN. This will be cleaned up in the future.
userid=nobody + (CAP_TRACING | CAP_NET_ADMIN) + CAP_BPF is safer than typical setup with userid=root and sudo by existing bpf applications. It's not secure, since these capabilities:
- allow bpf progs access arbitrary memory
- let tasks access any bpf map
- let tasks attach/detach any bpf prog
bpftool, bpftrace, bcc tools binaries should not be installed with cap_bpf+cap_tracing, since unpriv users will be able to read kernel secrets.
CAP_BPF, CAP_NET_ADMIN, CAP_TRACING are roughly equal in terms of damage they can make to the system.
Example:CAP_NET_ADMIN can stop network traffic. CAP_BPF can write into map and if that map is used by firewall-like bpf prog the network traffic may stop.CAP_BPF allows many bpf prog_load commands in parallel. The verifier may consume large amount of memory and significantly slow down the system. CAP_TRACING allows many kprobes that can slow down the system.
In the future more fine-grained bpf permissions may be added.
Existing unprivileged BPF operations are not affected.
In particular unprivileged users are allowed to load socket_filter and cg_skb program types and to create array, hash, prog_array, map-in-map map types.
--- include/linux/capability.h | 18 +++++++++++ include/uapi/linux/capability.h | 49 ++++++++++++++++++++++++++++- security/selinux/include/classmap.h | 4 +-- 3 files changed, 68 insertions(+), 3 deletions(-)
include/linux/capability.h +18 -0
@@ -247,6 +247,24 @@ static inline bool ns_capable_setid(struct user_namespace *ns, int cap)return true; }#endif /* CONFIG_MULTIUSER */ + +static inline bool capable_bpf (void ) +{ +return capable (CAP_SYS_ADMIN ) ||capable (CAP_BPF ); +} +static inline bool capable_tracing (void ) +{ +return capable (CAP_SYS_ADMIN ) ||capable (CAP_TRACING ); +} +static inline bool capable_bpf_tracing (void ) +{ +return capable (CAP_SYS_ADMIN ) || (capable (CAP_BPF ) &&capable (CAP_TRACING )); +} +static inline bool capable_bpf_net_admin (void ) +{ +return (capable (CAP_SYS_ADMIN ) ||capable (CAP_BPF )) &&capable (CAP_NET_ADMIN ); +} +extern bool privileged_wrt_inode_uidgid (struct user_namespace *ns,const struct inode *inode);extern bool capable_wrt_inode_uidgid (const struct inode *inode,int cap);extern bool file_ns_capable (const struct file *file,struct user_namespace *ns,int cap);
include/uapi/linux/capability.h +48 -1
62 lines not on this page · raw message
security/selinux/include/classmap.h +2 -2
@@ -26,9 +26,9 @@ "audit_control", "setfcap"#define COMMON_CAP2_PERMS "mac_override", "mac_admin", "syslog", \ - "wake_alarm", "block_suspend", "audit_read" + "wake_alarm", "block_suspend", "audit_read", "bpf", "tracing" -#if CAP_LAST_CAP >CAP_AUDIT_READ +#if CAP_LAST_CAP >CAP_TRACING #error New capability defined, please update COMMON_CAP2_PERMS.#endif
Signature · 1 line
2.20.0
Re: [PATCH v4 bpf-next 1/4] capability: introduce CAP_BPF and CAP_TRACING
Quoted 35 lines · Alexei Starovoitov
Split BPF and perf/tracing operations that are allowed under
CAP_SYS_ADMINinto correspondingCAP_BPFandCAP_TRACING. For backward compatibility include them inCAP_SYS_ADMINas well.The end result provides simple safety model for applications that use BPF:
- for tracing program types
BPF_PROG_TYPE_{KPROBE, TRACEPOINT,PERF_EVENT,RAW_TRACEPOINT, etc} useCAP_BPFandCAP_TRACING
- for networking program types
BPF_PROG_TYPE_{SCHED_CLS, XDP,CGROUP_SKB,SK_SKB, etc}
useCAP_BPFandCAP_NET_ADMINThere are few exceptions from this simple rule:
-bpf_trace_printk()is allowed in networking programs, but it's using ftrace mechanism, hence this helper needs additionalCAP_TRACING.
- cpumap is used by XDP programs. Currently it's kept underCAP_SYS_ADMIN, but could be relaxed toCAP_NET_ADMINin the future.
-BPF_F_ZERO_SEEDflag for hash/lru map is allowed underCAP_SYS_ADMINonly to discourage production use.
- BPF HW offload is allowed underCAP_SYS_ADMIN.
-cg_sysctl,cg_device, lirc program types are neither networking nor tracing. They can be loaded underCAP_BPF, but attach is allowed underCAP_NET_ADMIN. This will be cleaned up in the future.userid=nobody + (
CAP_TRACING|CAP_NET_ADMIN) +CAP_BPFis safer than typical setup with userid=root and sudo by existing bpf applications. It's not secure, since these capabilities:
- allow bpf progs access arbitrary memory
- let tasks access any bpf map
- let tasks attach/detach any bpf progbpftool, bpftrace, bcc tools binaries should not be installed with cap_bpf+cap_tracing, since unpriv users will be able to read kernel secrets.
Quoted 9 lines
CAP_BPF,CAP_NET_ADMIN,CAP_TRACINGare roughly equal in terms of damage they can make to the system.
Example:CAP_NET_ADMINcan stop network traffic.CAP_BPFcan write into map and if that map is used by firewall-like bpf prog the network traffic may stop.CAP_BPFallows many bpfprog_loadcommands in parallel. The verifier may consume large amount of memory and significantly slow down the system.CAP_TRACINGallows many kprobes that can slow down the system.
Do we want to split CAP_TRACE_KERNEL and CAP_TRACE_USER? It's not entirely clear to me that it's useful.
Quoted 30 lines
In the future more fine-grained bpf permissions may be added.
Existing unprivileged BPF operations are not affected.
In particular unprivileged users are allowed to loadsocket_filterandcg_skbprogram types and to create array, hash,prog_array, map-in-map map types.Signed-off-by Alexei Starovoitov <[email protected]>--- include/linux/capability.h | 18 +++++++++++ include/uapi/linux/capability.h | 49 ++++++++++++++++++++++++++++- security/selinux/include/classmap.h | 4 +-- 3 files changed, 68 insertions(+), 3 deletions(-)include/linux/capability.h +8
-0@@ -247,6 +247,24 @@ static inline bool ns_capable_setid(struct user_namespace *ns, int cap) return true; }#endif /* CONFIG_MULTIUSER */ + +static inline bool capable_bpf (void ) +{ +return capable (CAP_SYS_ADMIN ) ||capable (CAP_BPF ); +} +static inline bool capable_tracing (void ) +{ +return capable (CAP_SYS_ADMIN ) ||capable (CAP_TRACING );
include/linux/capability.h static inline bool ns_capable_setid(struct user_namespace *ns, int cap) · line 267
Quoted 10 lines
include/linux/capability.h +10
-0+} +static inline bool capable_bpf_tracing (void ) +{ +return capable (CAP_SYS_ADMIN ) || (capable (CAP_BPF ) &&capable (CAP_TRACING )); +} +static inline bool capable_bpf_net_admin (void ) +{ +return (capable (CAP_SYS_ADMIN ) ||capable (CAP_BPF )) &&capable (CAP_NET_ADMIN ); +} +
These helpers are all wrong, unfortunately, since they will produce inappropriate audit events. capable_bpf() should look more like this:
if (capable_noaudit(CAP_BPF))
return capable(CAP_BPF);
if (capable_noaudit(CAP_SYS_ADMIN))
return capable(CAP_SYS_ADMIN);
return capable(CAP_BPF);
James, etc: should there instead be new helpers to do this more generically rather than going through the noaudit contortions? My code above is horrible.
Alexei Starovoitov
maintainerPATCH v4 2/4 bpf: implement CAP_BPF
In reply to Alexei StarovoitovImplement permissions as stated in uapi/linux/capability.h
--- kernel/bpf/arraymap.c | 2 +- kernel/bpf/cgroup.c | 2 +- kernel/bpf/core.c | 4 ++-- kernel/bpf/hashtab.c | 4 ++-- kernel/bpf/lpm_trie.c | 2 +- kernel/bpf/queue_stack_maps.c | 2 +- kernel/bpf/reuseport_array.c | 2 +- kernel/bpf/stackmap.c | 2 +- kernel/bpf/syscall.c | 32 +++++++++++++++++++------------- kernel/bpf/verifier.c | 2 +- kernel/trace/bpf_trace.c | 2 +- net/core/bpf_sk_storage.c | 2 +- net/core/filter.c | 10 ++++++---- 13 files changed, 38 insertions(+), 30 deletions(-)
kernel/bpf/arraymap.c +1 -1
@@ -73,7 +73,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)bool percpu = attr->map_type ==BPF_MAP_TYPE_PERCPU_ARRAY ;int ret, numa_node =bpf_map_attr_numa_node (attr);u32 elem_size, index_mask, max_entries; -bool unpriv = !capable (CAP_SYS_ADMIN ); +bool unpriv = !capable_bpf ();u64 cost, array_size, mask64;struct bpf_map_memory mem;struct bpf_array *array;
kernel/bpf/cgroup.c +1 -1
@@ -795,7 +795,7 @@ cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)case BPF_FUNC_get_current_cgroup_id:return &bpf_get_current_cgroup_id_proto;case BPF_FUNC_trace_printk: -if (capable (CAP_SYS_ADMIN )) +if (capable_bpf_tracing ())return bpf_get_trace_printk_proto ();/* fall through */ default :
kernel/bpf/core.c +2 -2
@@ -646,7 +646,7 @@ static bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)void bpf_prog_kallsyms_add (struct bpf_prog *fp) {if (!bpf_prog_kallsyms_candidate (fp) || - !capable (CAP_SYS_ADMIN )) + !capable_bpf ())return ;spin_lock_bh (&bpf_lock);
@@ -768,7 +768,7 @@ static int bpf_jit_charge_modmem(u32 pages) {if (atomic_long_add_return (pages, &bpf_jit_current) > (bpf_jit_limit >>PAGE_SHIFT )) { -if (!capable (CAP_SYS_ADMIN )) { +if (!capable_bpf ()) {atomic_long_sub (pages, &bpf_jit_current);return -EPERM ; }
kernel/bpf/hashtab.c +2 -2
@@ -244,9 +244,9 @@ static int htab_map_alloc_check(union bpf_attr *attr) BUILD_BUG_ON(offsetof(struct htab_elem, fnode.next) != offsetof(struct htab_elem, hash_node.pprev)); -if (lru && !capable (CAP_SYS_ADMIN )) +if (lru && !capable_bpf ())/* LRU implementation is much complicated than other -* maps. Hence, limit to CAP_SYS_ADMIN for now. +* maps. Hence, limit to CAP_BPF. */ return -EPERM ;
kernel/bpf/lpm_trie.c +1 -1
@@ -543,7 +543,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)u64 cost =sizeof (*trie), cost_per_node;int ret; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return ERR_PTR (-EPERM );/* check sanity of attributes */
kernel/bpf/queue_stack_maps.c +1 -1
@@ -45,7 +45,7 @@ static bool queue_stack_map_is_full(struct bpf_queue_stack *qs)/* Called from syscall */ static int queue_stack_map_alloc_check (union bpf_attr *attr) { -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ;/* 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)struct bpf_map_memory mem;u64 array_size; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return ERR_PTR (-EPERM ); array_size =sizeof (*array);
kernel/bpf/stackmap.c +1 -1
@@ -90,7 +90,7 @@ static struct bpf_map *stack_map_alloc(union bpf_attr *attr)u64 cost, n_buckets;int err; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_tracing ())return ERR_PTR (-EPERM );if (attr->map_flags & ~STACK_CREATE_FLAG_MASK )
kernel/bpf/syscall.c +19 -13
@@ -1176,7 +1176,7 @@ static int map_freeze(const union bpf_attr *attr) err = -EBUSY ; goto err_put; } -if (!capable (CAP_SYS_ADMIN )) { +if (!capable_bpf ()) { err = -EPERM ; goto err_put; }
@@ -1635,7 +1635,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)if (!IS_ENABLED (CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS ) && (attr->prog_flags &BPF_F_ANY_ALIGNMENT ) && - !capable (CAP_SYS_ADMIN )) + !capable_bpf ())return -EPERM ;/* copy eBPF program license from user space */
@@ -1648,11 +1648,11 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr) is_gpl =license_is_gpl_compatible (license);if (attr->insn_cnt ==0 || - attr->insn_cnt > (capable (CAP_SYS_ADMIN ) ?BPF_COMPLEXITY_LIMIT_INSNS :BPF_MAXINSNS )) + attr->insn_cnt > (capable_bpf () ?BPF_COMPLEXITY_LIMIT_INSNS :BPF_MAXINSNS ))return -E2BIG ;if (type !=BPF_PROG_TYPE_SOCKET_FILTER && type !=BPF_PROG_TYPE_CGROUP_SKB && - !capable (CAP_SYS_ADMIN )) + !capable_bpf ())return -EPERM ;bpf_prog_load_fixup_attach_type (attr);
@@ -1809,6 +1809,9 @@ static int bpf_raw_tracepoint_open(const union bpf_attr *attr)char tp_name[128 ];int tp_fd, err; +if (!capable_bpf_tracing ()) +return -EPERM ; +if (strncpy_from_user (tp_name,u64_to_user_ptr (attr->raw_tracepoint.name),sizeof (tp_name) -1 ) <0 )return -EFAULT ;
@@ -2087,7 +2090,10 @@ static int bpf_prog_test_run(const union bpf_attr *attr,struct bpf_prog *prog;int ret = -ENOTSUPP ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf_net_admin ()) +/* test_run callback is available for networking progs only. +* Add capable_bpf_tracing() above when tracing progs become runable. +*/ return -EPERM ;if (CHECK_ATTR (BPF_PROG_TEST_RUN ))return -EINVAL ;
@@ -2124,7 +2130,7 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,if (CHECK_ATTR (BPF_OBJ_GET_NEXT_ID ) || next_id >=INT_MAX )return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ; next_id++;
@@ -2150,7 +2156,7 @@ static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)if (CHECK_ATTR (BPF_PROG_GET_FD_BY_ID ))return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ;spin_lock_bh (&prog_idr_lock);
@@ -2184,7 +2190,7 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr) attr->open_flags & ~BPF_OBJ_FLAG_MASK)return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ; f_flags =bpf_get_file_flag (attr->open_flags);
@@ -2359,7 +2365,7 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog, info.run_time_ns = stats.nsecs; info.run_cnt = stats.cnt; -if (!capable (CAP_SYS_ADMIN )) { +if (!capable_bpf ()) { info.jited_prog_len =0 ; info.xlated_prog_len =0 ; info.nr_jited_ksyms =0 ;
@@ -2677,7 +2683,7 @@ static int bpf_btf_load(const union bpf_attr *attr)if (CHECK_ATTR (BPF_BTF_LOAD ))return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ;return btf_new_fd (attr);
@@ -2690,7 +2696,7 @@ static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)if (CHECK_ATTR (BPF_BTF_GET_FD_BY_ID ))return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ;return btf_get_fd_by_id (attr->btf_id);
@@ -2759,7 +2765,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,if (CHECK_ATTR (BPF_TASK_FD_QUERY ))return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf_tracing ())return -EPERM ;if (attr->task_fd_query.flags !=0 )
@@ -2827,7 +2833,7 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizunion bpf_attr attr = {};int err; -if (sysctl_unprivileged_bpf_disabled && !capable (CAP_SYS_ADMIN )) +if (sysctl_unprivileged_bpf_disabled && !capable_bpf ())return -EPERM ; err =bpf_check_uarg_tail_zero (uattr,sizeof (attr), size);
kernel/bpf/verifier.c +1 -1
@@ -9234,7 +9234,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, env->insn_aux_data[i].orig_idx = i; env->prog = *prog; env->ops = bpf_verifier_ops[env->prog->type]; - is_priv =capable (CAP_SYS_ADMIN ); + is_priv =capable_bpf ();/* grab the mutex to protect few globals used by verifier */ if (!is_priv)
kernel/trace/bpf_trace.c +1 -1
@@ -1246,7 +1246,7 @@ int perf_event_query_prog_array(struct perf_event *event, void __user *info)u32 *ids, prog_cnt, ids_len;int ret; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf_tracing ())return -EPERM ;if (event->attr.type !=PERF_TYPE_TRACEPOINT )return -EINVAL ;
net/core/bpf_sk_storage.c +1 -1
@@ -616,7 +616,7 @@ static int bpf_sk_storage_map_alloc_check(union bpf_attr *attr) !attr->btf_key_type_id || !attr->btf_value_type_id)return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return -EPERM ; if (attr->value_size >= KMALLOC_MAX_SIZE -
net/core/filter.c +6 -4
@@ -5990,7 +5990,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)break ; } -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return NULL ;switch (func_id) {
@@ -5999,7 +5999,9 @@ bpf_base_func_proto(enum bpf_func_id func_id)case BPF_FUNC_spin_unlock:return &bpf_spin_unlock_proto;case BPF_FUNC_trace_printk: -return bpf_get_trace_printk_proto (); +if (capable_bpf_tracing ()) +return bpf_get_trace_printk_proto (); +/* fall through */ default :return NULL ; }
@@ -6563,7 +6565,7 @@ static bool cg_skb_is_valid_access(int off, int size,return false; case bpf_ctx_range(struct __sk_buff, data): case bpf_ctx_range(struct __sk_buff, data_end): -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return false;break ; }
@@ -6575,7 +6577,7 @@ static bool cg_skb_is_valid_access(int off, int size, case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):break ; case bpf_ctx_range(struct __sk_buff, tstamp): -if (!capable (CAP_SYS_ADMIN )) +if (!capable_bpf ())return false;break ;default :
Signature · 1 line
2.20.0
Alexei Starovoitov
maintainerPATCH v4 3/4 perf: implement CAP_TRACING
In reply to Alexei StarovoitovImplement permissions as stated in uapi/linux/capability.h and update Documentation.
--- Documentation/admin-guide/perf-security.rst | 4 ++-- Documentation/admin-guide/sysctl/kernel.rst | 10 ++++------ arch/powerpc/perf/core-book3s.c | 4 ++-- arch/x86/events/intel/bts.c | 2 +- arch/x86/events/intel/core.c | 2 +- arch/x86/events/intel/p4.c | 2 +- kernel/events/core.c | 14 +++++++------- kernel/events/hw_breakpoint.c | 2 +- kernel/trace/trace_event_perf.c | 4 ++-- 9 files changed, 21 insertions(+), 23 deletions(-)
Documentation/admin-guide/perf-security.rst +2 -2
@@ -66,8 +66,8 @@ into distinct units, known as capabilities [6]_ , which can be independently enabled and disabled on per-thread basis for processes and files of unprivileged users. -Unprivileged processes with enabled CAP_SYS_ADMIN capability are treated -as privileged processes with respect to perf_events performance +Unprivileged processes with enabled CAP_SYS_ADMIN or CAP_TRACING capability +are treated as privileged processes with respect to perf_events performance monitoring and bypass *scope* permissions checks in the kernel. Unprivileged processes using perf_events system call API is also subject
Documentation/admin-guide/sysctl/kernel.rst +4 -6
@@ -720,20 +720,18 @@ allowed to execute. ==================== Controls use of the performance events system by unprivileged -users (without CAP_SYS_ADMIN). The default value is 2. +users (without CAP_SYS_ADMIN and without CAP_TRACING). The default value is 2. === ================================================================== -1 Allow use of (almost) all events by all users Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK ->=0 Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN +>=0 Disallow ftrace function tracepoint and raw tracepoint - Disallow raw tracepoint access by users without CAP_SYS_ADMIN +>=1 Disallow CPU event access ->=1 Disallow CPU event access by users without CAP_SYS_ADMIN - ->=2 Disallow kernel profiling by users without CAP_SYS_ADMIN +>=2 Disallow kernel profiling === ==================================================================
arch/powerpc/perf/core-book3s.c +2 -2
@@ -204,7 +204,7 @@ static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)if (!(mmcra &MMCRA_SAMPLE_ENABLE ) || sdar_valid) *addrp =mfspr (SPRN_SDAR ); -if (perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN ) && +if (perf_paranoid_kernel () && !capable_tracing () &&is_kernel_addr (mfspr (SPRN_SDAR ))) *addrp =0 ; }
@@ -472,7 +472,7 @@ static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)* exporting it to userspace (avoid exposure of regions * where we could have speculative execution) */ -if (perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN ) && +if (perf_paranoid_kernel () && !capable_tracing () &&is_kernel_addr (addr))continue ;
arch/x86/events/intel/bts.c +1 -1
@@ -550,7 +550,7 @@ static int bts_event_init(struct perf_event *event)* users to profile the kernel. */ if (event->attr.exclude_kernel &&perf_paranoid_kernel () && - !capable (CAP_SYS_ADMIN )) + !capable_tracing ())return -EACCES ;if (x86_add_exclusive (x86_lbr_exclusive_bts))
arch/x86/events/intel/core.c +1 -1
@@ -3307,7 +3307,7 @@ static int intel_pmu_hw_config(struct perf_event *event)if (x86_pmu.version <3 )return -EINVAL ; -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_cpu () && !capable_tracing ())return -EACCES ; event->hw.config |=ARCH_PERFMON_EVENTSEL_ANY ;
arch/x86/events/intel/p4.c +1 -1
@@ -776,7 +776,7 @@ static int p4_validate_raw_event(struct perf_event *event)* the user needs special permissions to be able to use it */ if (p4_ht_active () && p4_event_bind_map[v].shared) { -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_cpu () && !capable_tracing ())return -EACCES ; }
kernel/events/core.c +7 -7
@@ -4134,7 +4134,7 @@ find_get_context(struct pmu *pmu, struct task_struct *task,if (!task) {/* Must be root to operate on a CPU event: */ -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_cpu () && !capable_tracing ())return ERR_PTR (-EACCES ); cpuctx =per_cpu_ptr (pmu->pmu_cpu_context, cpu);
@@ -8741,7 +8741,7 @@ static int perf_kprobe_event_init(struct perf_event *event)if (event->attr.type != perf_kprobe.type)return -ENOENT ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_tracing ())return -EACCES ;/*
@@ -8801,7 +8801,7 @@ static int perf_uprobe_event_init(struct perf_event *event)if (event->attr.type != perf_uprobe.type)return -ENOENT ; -if (!capable (CAP_SYS_ADMIN )) +if (!capable_tracing ())return -EACCES ;/*
@@ -10588,7 +10588,7 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr, }/* privileged levels capture (kernel, hv): check permissions */ if ((mask &PERF_SAMPLE_BRANCH_PERM_PLM ) - &&perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN )) + &&perf_paranoid_kernel () && !capable_tracing ())return -EACCES ; }
@@ -10807,12 +10807,12 @@ SYSCALL_DEFINE5(perf_event_open,return err;if (!attr.exclude_kernel) { -if (perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_kernel () && !capable_tracing ())return -EACCES ; }if (attr.namespaces) { -if (!capable (CAP_SYS_ADMIN )) +if (!capable_tracing ())return -EACCES ; }
@@ -10826,7 +10826,7 @@ SYSCALL_DEFINE5(perf_event_open,/* Only privileged users can get physical addresses */ if ((attr.sample_type &PERF_SAMPLE_PHYS_ADDR ) && -perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN )) +perf_paranoid_kernel () && !capable_tracing ())return -EACCES ;/*
kernel/events/hw_breakpoint.c +1 -1
@@ -404,7 +404,7 @@ static int hw_breakpoint_parse(struct perf_event *bp,* Don't let unprivileged users set a breakpoint in the trap * path to avoid trap recursion attacks. */ -if (!capable (CAP_SYS_ADMIN )) +if (!capable_tracing ())return -EPERM ; }
kernel/trace/trace_event_perf.c +2 -2
@@ -46,7 +46,7 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,/* The ftrace function trace is allowed only for root. */ if (ftrace_event_is_function (tp_event)) { -if (perf_paranoid_tracepoint_raw () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_tracepoint_raw () && !capable_tracing ())return -EPERM ;if (!is_sampling_event (p_event))
@@ -82,7 +82,7 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,* ...otherwise raw tracepoint data can be a severe data leak, * only allow root to have these. */ -if (perf_paranoid_tracepoint_raw () && !capable (CAP_SYS_ADMIN )) +if (perf_paranoid_tracepoint_raw () && !capable_tracing ())return -EPERM ;return 0 ;
Signature · 1 line
2.20.0
Alexei Starovoitov
maintainerPATCH v4 4/4 selftests/bpf: use CAP_BPF and CAP_TRACING in tests
In reply to Alexei StarovoitovMake all test_verifier test exercise CAP_BPF and CAP_TRACING
--- tools/testing/selftests/bpf/test_verifier.c | 46 +++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +38 -8
@@ -807,10 +807,20 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, } } +struct libcap { +struct __user_cap_header_struct hdr; +struct __user_cap_data_struct data[2 ]; +}; +static int set_admin (bool admin) {cap_t caps; -const cap_value_t cap_val =CAP_SYS_ADMIN ; +/* need CAP_BPF to load progs and CAP_NET_ADMIN to run networking progs, +* and CAP_TRACING to create stackmap +*/ +const cap_value_t cap_net_admin =CAP_NET_ADMIN ; +const cap_value_t cap_sys_admin =CAP_SYS_ADMIN ; +struct libcap *cap;int ret =-1 ; caps =cap_get_proc ();
@@ -818,11 +828,26 @@ static int set_admin(bool admin)perror ("cap_get_proc" );return -1 ; } -if (cap_set_flag (caps,CAP_EFFECTIVE ,1 , &cap_val, + cap = (struct libcap *)caps; +if (cap_set_flag (caps,CAP_EFFECTIVE ,1 , &cap_sys_admin,CAP_CLEAR )) { +perror ("cap_set_flag clear admin" ); + goto out; + } +if (cap_set_flag (caps,CAP_EFFECTIVE ,1 , &cap_net_admin, admin ?CAP_SET :CAP_CLEAR )) { -perror ("cap_set_flag" ); +perror ("cap_set_flag set_or_clear net" ); goto out; } +/* libcap is likely old and simply ignores CAP_BPF and CAP_TRACING, +* so update effective bits manually +*/ +if (admin) { + cap->data[1 ].effective |=1 << (38 /* CAP_BPF */ -32 ); + cap->data[1 ].effective |=1 << (39 /* CAP_TRACING */ -32 ); + }else { + cap->data[1 ].effective &= ~(1 << (38 -32 )); + cap->data[1 ].effective &= ~(1 << (39 -32 )); + }if (cap_set_proc (caps)) {perror ("cap_set_proc" ); goto out;
@@ -1051,9 +1076,11 @@ static void do_test_single(struct bpf_test *test, bool unpriv,static bool is_admin (void ) { +cap_flag_value_t net_priv =CAP_CLEAR ; +bool tracing_priv = false; +bool bpf_priv = false; +struct libcap *cap;cap_t caps; -cap_flag_value_t sysadmin =CAP_CLEAR ; -const cap_value_t cap_val =CAP_SYS_ADMIN ;#ifdef CAP_IS_SUPPORTED if (!CAP_IS_SUPPORTED (CAP_SETFCAP )) {
@@ -1066,11 +1093,14 @@ static bool is_admin(void)perror ("cap_get_proc" );return false; } -if (cap_get_flag (caps, cap_val,CAP_EFFECTIVE , &sysadmin)) -perror ("cap_get_flag" ); + cap = (struct libcap *)caps; + bpf_priv = cap->data[1 ].effective & (1 << (38 /* CAP_BPF */ -32 )); + tracing_priv = cap->data[1 ].effective & (1 << (39 /* CAP_TRACING */ -32 )); +if (cap_get_flag (caps,CAP_NET_ADMIN ,CAP_EFFECTIVE , &net_priv)) +perror ("cap_get_flag NET" );if (cap_free (caps))perror ("cap_free" ); -return (sysadmin ==CAP_SET ); +return bpf_priv && tracing_priv && net_priv ==CAP_SET ; }static void get_unpriv_disabled ()
Signature · 1 line
2.20.0