History

Merge branch 'bpf-token-and-bpf-fs-based-delegation'

Andrii Nakryiko says: ==================== BPF token and BPF FS-based delegation This patch set introduces an ability to delegate a subset of BPF subsystem functionality from privileged system-wide daemon (e.g., systemd or any other container manager) through special mount options for userns-bound BPF FS to a *trusted* unprivileged application. Trust is the key here. This functionality is not about allowing unconditional unprivileged BPF usage. Establishing trust, though, is completely up to the discretion of respective privileged application that would create and mount a BPF FS instance with delegation enabled, as different production setups can and do achieve it through a combination of different means (signing, LSM, code reviews, etc), and it's undesirable and infeasible for kernel to enforce any particular way of validating trustworthiness of particular process. The main motivation for this work is a desire to enable containerized BPF applications to be used together with user namespaces. This is currently impossible, as CAP_BPF, required for BPF subsystem usage, cannot be namespaced or sandboxed, as a general rule. E.g., tracing BPF programs, thanks to BPF helpers like bpf_probe_read_kernel() and bpf_probe_read_user() can safely read arbitrary memory, and it's impossible to ensure that they only read memory of processes belonging to any given namespace. This means that it's impossible to have a mechanically verifiable namespace-aware CAP_BPF capability, and as such another mechanism to allow safe usage of BPF functionality is necessary.BPF FS delegation mount options and BPF token derived from such BPF FS instance is such a mechanism. Kernel makes no assumption about what "trusted" constitutes in any particular case, and it's up to specific privileged applications and their surrounding infrastructure to decide that. What kernel provides is a set of APIs to setup and mount special BPF FS instanecs and derive BPF tokens from it. BPF FS and BPF token are both bound to its owning userns and in such a way are constrained inside intended container. Users can then pass BPF token FD to privileged bpf() syscall commands, like BPF map creation and BPF program loading, to perform such operations without having init userns privileged. This version incorporates feedback and suggestions ([3]) received on v3 of this patch set, and instead of allowing to create BPF tokens directly assuming capable(CAP_SYS_ADMIN), we instead enhance BPF FS to accept a few new delegation mount options. If these options are used and BPF FS itself is properly created, set up, and mounted inside the user namespaced container, user application is able to derive a BPF token object from BPF FS instance, and pass that token to bpf() syscall. As explained in patch #3, BPF token itself doesn't grant access to BPF functionality, but instead allows kernel to do namespaced capabilities checks (ns_capable() vs capable()) for CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, and CAP_SYS_ADMIN, as applicable. So it forms one half of a puzzle and allows container managers and sys admins to have safe and flexible configuration options: determining which containers get delegation of BPF functionality through BPF FS, and then which applications within such containers are allowed to perform bpf() commands, based on namespaces capabilities. Previous attempt at addressing this very same problem ([0]) attempted to utilize authoritative LSM approach, but was conclusively rejected by upstream LSM maintainers. BPF token concept is not changing anything about LSM approach, but can be combined with LSM hooks for very fine-grained security policy. Some ideas about making BPF token more convenient to use with LSM (in particular custom BPF LSM programs) was briefly described in recent LSF/MM/BPF 2023 presentation ([1]). E.g., an ability to specify user-provided data (context), which in combination with BPF LSM would allow implementing a very dynamic and fine-granular custom security policies on top of BPF token. In the interest of minimizing API surface area and discussions this was relegated to follow up patches, as it's not essential to the fundamental concept of delegatable BPF token. It should be noted that BPF token is conceptually quite similar to the idea of /dev/bpf device file, proposed by Song a while ago ([2]). The biggest difference is the idea of using virtual anon_inode file to hold BPF token and allowing multiple independent instances of them, each (potentially) with its own set of restrictions. And also, crucially, BPF token approach is not using any special stateful task-scoped flags. Instead, bpf() syscall accepts token_fd parameters explicitly for each relevant BPF command. This addresses main concerns brought up during the /dev/bpf discussion, and fits better with overall BPF subsystem design. This patch set adds a basic minimum of functionality to make BPF token idea useful and to discuss API and functionality. Currently only low-level libbpf APIs support creating and passing BPF token around, allowing to test kernel functionality, but for the most part is not sufficient for real-world applications, which typically use high-level libbpf APIs based on `struct bpf_object` type. This was done with the intent to limit the size of patch set and concentrate on mostly kernel-side changes. All the necessary plumbing for libbpf will be sent as a separate follow up patch set kernel support makes it upstream. Another part that should happen once kernel-side BPF token is established, is a set of conventions between applications (e.g., systemd), tools (e.g., bpftool), and libraries (e.g., libbpf) on exposing delegatable BPF FS instance(s) at well-defined locations to allow applications take advantage of this in automatic fashion without explicit code changes on BPF application's side. But I'd like to postpone this discussion to after BPF token concept lands. [0] https://lore.kernel.org/bpf/20230412043300.360803-1-andrii@kernel.org/ [1] http://vger.kernel.org/bpfconf2023_material/Trusted_unprivileged_BPF_LSFMM2023.pdf [2] https://lore.kernel.org/bpf/20190627201923.2589391-2-songliubraving@fb.com/ [3] https://lore.kernel.org/bpf/20230704-hochverdient-lehne-eeb9eeef785e@brauner/ v11->v12: - enforce exact userns match in bpf_token_capable() and bpf_token_allow_cmd() checks, for added strictness (Christian); v10->v11: - fix BPF FS root check to disallow using bind-mounted subdirectory of BPF FS instance (Christian); - further restrict BPF_TOKEN_CREATE command to be executed from inside exactly the same user namespace as the one used to create BPF FS instance (Christian); v9->v10: - slight adjustments in LSM parts (Paul); - setting delegate_xxx options require capable(CAP_SYS_ADMIN) (Christian); - simplify BPF_TOKEN_CREATE UAPI by accepting BPF FS FD directly (Christian); v8->v9: - fix issue in selftests due to sys/mount.h header (Jiri); - fix warning in doc comments in LSM hooks (kernel test robot); v7->v8: - add bpf_token_allow_cmd and bpf_token_capable hooks (Paul); - inline bpf_token_alloc() into bpf_token_create() to prevent accidental divergence with security_bpf_token_create() hook (Paul); v6->v7: - separate patches to refactor bpf_prog_alloc/bpf_map_alloc LSM hooks, as discussed with Paul, and now they also accept struct bpf_token; - added bpf_token_create/bpf_token_free to allow LSMs (SELinux, specifically) to set up security LSM blob (Paul); - last patch also wires bpf_security_struct setup by SELinux, similar to how it's done for BPF map/prog, though I'm not sure if that's enough, so worst case it's easy to drop this patch if more full fledged SELinux implementation will be done separately; - small fixes for issues caught by code reviews (Jiri, Hou); - fix for test_maps test that doesn't use LIBBPF_OPTS() macro (CI); v5->v6: - fix possible use of uninitialized variable in selftests (CI); - don't use anon_inode, instead create one from BPF FS instance (Christian); - don't store bpf_token inside struct bpf_map, instead pass it explicitly to map_check_btf(). We do store bpf_token inside prog->aux, because it's used during verification and even can be checked during attach time for some program types; - LSM hooks are left intact pending the conclusion of discussion with Paul Moore; I'd prefer to do LSM-related changes as a follow up patch set anyways; v4->v5: - add pre-patch unifying CAP_NET_ADMIN handling inside kernel/bpf/syscall.c (Paul Moore); - fix build warnings and errors in selftests and kernel, detected by CI and kernel test robot; v3->v4: - add delegation mount options to BPF FS; - BPF token is derived from the instance of BPF FS and associates itself with BPF FS' owning userns; - BPF token doesn't grant BPF functionality directly, it just turns capable() checks into ns_capable() checks within BPF FS' owning user; - BPF token cannot be pinned; v2->v3: - make BPF_TOKEN_CREATE pin created BPF token in BPF FS, and disallow BPF_OBJ_PIN for BPF token; v1->v2: - fix build failures on Kconfig with CONFIG_BPF_SYSCALL unset; - drop BPF_F_TOKEN_UNKNOWN_* flags and simplify UAPI (Stanislav). ====================

Alexei Starovoitov committed 2 years ago

Commit c35919d2 parents 3aee2bf 36fb94929 files · +1624 −167Browse files

Unified · Split

29 files
  1. drivers/media/rc/bpf-lirc.c +1 −1
  2. include/linux/bpf.h +73 −10
  3. include/linux/filter.h +1 −1
  4. include/linux/lsm_hook_defs.h +11 −4
  5. include/linux/security.h +36 −7
  6. include/uapi/linux/bpf.h +42 −0
  7. kernel/bpf/Makefile +1 −1
  8. kernel/bpf/arraymap.c +1 −1
  9. kernel/bpf/bpf_lsm.c +10 −5
  10. kernel/bpf/cgroup.c +3 −3
  11. kernel/bpf/core.c +2 −1
  12. kernel/bpf/helpers.c +3 −3
  13. kernel/bpf/inode.c +87 −14
  14. kernel/bpf/syscall.c +159 −56
  15. kernel/bpf/token.c +263 −0
  16. kernel/bpf/verifier.c +6 −7
  17. kernel/trace/bpf_trace.c +1 −1
  18. net/core/filter.c +18 −18
  19. net/ipv4/bpf_tcp_ca.c +1 −1
  20. net/netfilter/nf_bpf_link.c +1 −1
  21. security/security.c +85 −16
  22. security/selinux/hooks.c +37 −10
  23. tools/include/uapi/linux/bpf.h +42 −0
  24. tools/lib/bpf/bpf.c +25 −3
  25. tools/lib/bpf/bpf.h +32 −3
  26. tools/lib/bpf/libbpf.map +1 −0
  27. tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +4 −0
  28. tools/testing/selftests/bpf/prog_tests/libbpf_str.c +6 −0
  29. tools/testing/selftests/bpf/prog_tests/token.c +672 −0
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 (perfmon_capable())113 if (bpf_token_capable(prog->aux->token, CAP_PERFMON))
114 return bpf_get_trace_printk_proto();114 return bpf_get_trace_printk_proto();
115 fallthrough;115 fallthrough;
116 default:116 default:
include/linux/bpf.h+73−10
@@ -51,6 +51,10 @@ struct module; · 50 unmodified lines
51struct bpf_func_state;51struct bpf_func_state;
52struct ftrace_ops;52struct ftrace_ops;
53struct cgroup;53struct cgroup;
54struct bpf_token;
55struct user_namespace;
56struct super_block;
57struct inode;
54 58
55extern struct idr btf_idr;59extern struct idr btf_idr;
56extern spinlock_t btf_idr_lock;60extern spinlock_t btf_idr_lock;
@@ -1457,6 +1461,7 @@ struct bpf_prog_aux { · 1400 unmodified lines
1457#ifdef CONFIG_SECURITY1461#ifdef CONFIG_SECURITY
1458 void *security;1462 void *security;
1459#endif1463#endif
1464 struct bpf_token *token;
1460 struct bpf_prog_offload *offload;1465 struct bpf_prog_offload *offload;
1461 struct btf *btf;1466 struct btf *btf;
1462 struct bpf_func_info *func_info;1467 struct bpf_func_info *func_info;
@@ -1581,6 +1586,29 @@ struct bpf_link_primer { · 118 unmodified lines
1581 u32 id;1586 u32 id;
1582};1587};
1583 1588
1589struct bpf_mount_opts {
1590 umode_t mode;
1591
1592 /* BPF token-related delegation options */
1593 u64 delegate_cmds;
1594 u64 delegate_maps;
1595 u64 delegate_progs;
1596 u64 delegate_attachs;
1597};
1598
1599struct bpf_token {
1600 struct work_struct work;
1601 atomic64_t refcnt;
1602 struct user_namespace *userns;
1603 u64 allowed_cmds;
1604 u64 allowed_maps;
1605 u64 allowed_progs;
1606 u64 allowed_attachs;
1607#ifdef CONFIG_SECURITY
1608 void *security;
1609#endif
1610};
1611
1584struct bpf_struct_ops_value;1612struct bpf_struct_ops_value;
1585struct btf_member;1613struct btf_member;
1586 1614
@@ -2038,6 +2066,7 @@ static inline void bpf_enable_instrumentation(void) · 451 unmodified lines
2038 migrate_enable();2066 migrate_enable();
2039}2067}
2040 2068
2069extern const struct super_operations bpf_super_ops;
2041extern const struct file_operations bpf_map_fops;2070extern const struct file_operations bpf_map_fops;
2042extern const struct file_operations bpf_prog_fops;2071extern const struct file_operations bpf_prog_fops;
2043extern const struct file_operations bpf_iter_fops;2072extern const struct file_operations bpf_iter_fops;
@@ -2172,24 +2201,26 @@ static inline void bpf_map_dec_elem_count(struct bpf_map *map) · 128 unmodified lines
2172 2201
2173extern int sysctl_unprivileged_bpf_disabled;2202extern int sysctl_unprivileged_bpf_disabled;
2174 2203
2175static inline bool bpf_allow_ptr_leaks(void)2204bool bpf_token_capable(const struct bpf_token *token, int cap);
2205
2206static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)
2176{2207{
2177 return perfmon_capable();2208 return bpf_token_capable(token, CAP_PERFMON);
2178}2209}
2179 2210
2180static inline bool bpf_allow_uninit_stack(void)2211static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)
2181{2212{
2182 return perfmon_capable();2213 return bpf_token_capable(token, CAP_PERFMON);
2183}2214}
2184 2215
2185static inline bool bpf_bypass_spec_v1(void)2216static inline bool bpf_bypass_spec_v1(const struct bpf_token *token)
2186{2217{
2187 return cpu_mitigations_off() || perfmon_capable();2218 return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
2188}2219}
2189 2220
2190static inline bool bpf_bypass_spec_v4(void)2221static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
2191{2222{
2192 return cpu_mitigations_off() || perfmon_capable();2223 return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
2193}2224}
2194 2225
2195int bpf_map_new_fd(struct bpf_map *map, int flags);2226int bpf_map_new_fd(struct bpf_map *map, int flags);
@@ -2206,8 +2237,21 @@ int bpf_link_new_fd(struct bpf_link *link); · 10 unmodified lines
2206struct bpf_link *bpf_link_get_from_fd(u32 ufd);2237struct bpf_link *bpf_link_get_from_fd(u32 ufd);
2207struct bpf_link *bpf_link_get_curr_or_next(u32 *id);2238struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
2208 2239
2240void bpf_token_inc(struct bpf_token *token);
2241void bpf_token_put(struct bpf_token *token);
2242int bpf_token_create(union bpf_attr *attr);
2243struct bpf_token *bpf_token_get_from_fd(u32 ufd);
2244
2245bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2246bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
2247bool bpf_token_allow_prog_type(const struct bpf_token *token,
2248 enum bpf_prog_type prog_type,
2249 enum bpf_attach_type attach_type);
2250
2209int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);2251int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);
2210int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);2252int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);
2253struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir,
2254 umode_t mode);
2211 2255
2212#define BPF_ITER_FUNC_PREFIX "bpf_iter_"2256#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
2213#define DEFINE_BPF_ITER_FUNC(target, args...) \2257#define DEFINE_BPF_ITER_FUNC(target, args...) \
@@ -2451,7 +2495,8 @@ const char *btf_find_decl_tag_value(const struct btf *btf, const struct btf_type · 237 unmodified lines
2451struct bpf_prog *bpf_prog_by_id(u32 id);2495struct bpf_prog *bpf_prog_by_id(u32 id);
2452struct bpf_link *bpf_link_by_id(u32 id);2496struct bpf_link *bpf_link_by_id(u32 id);
2453 2497
2454const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);2498const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
2499 const struct bpf_prog *prog);
2455void bpf_task_storage_free(struct task_struct *task);2500void bpf_task_storage_free(struct task_struct *task);
2456void bpf_cgrp_storage_free(struct cgroup *cgroup);2501void bpf_cgrp_storage_free(struct cgroup *cgroup);
2457bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);2502bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
@@ -2570,6 +2615,24 @@ static inline int bpf_obj_get_user(const char __user *pathname, int flags) · 112 unmodified lines
2570 return -EOPNOTSUPP;2615 return -EOPNOTSUPP;
2571}2616}
2572 2617
2618static inline bool bpf_token_capable(const struct bpf_token *token, int cap)
2619{
2620 return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
2621}
2622
2623static inline void bpf_token_inc(struct bpf_token *token)
2624{
2625}
2626
2627static inline void bpf_token_put(struct bpf_token *token)
2628{
2629}
2630
2631static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
2632{
2633 return ERR_PTR(-EOPNOTSUPP);
2634}
2635
2573static inline void __dev_flush(void)2636static inline void __dev_flush(void)
2574{2637{
2575}2638}
@@ -2693,7 +2756,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log, · 117 unmodified lines
2693}2756}
2694 2757
2695static inline const struct bpf_func_proto *2758static inline const struct bpf_func_proto *
2696bpf_base_func_proto(enum bpf_func_id func_id)2759bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2697{2760{
2698 return NULL;2761 return NULL;
2699}2762}
include/linux/filter.h+1−1
@@ -1139,7 +1139,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) · 1138 unmodified lines
1139 return false;1139 return false;
1140 if (!bpf_jit_harden)1140 if (!bpf_jit_harden)
1141 return false;1141 return false;
1142 if (bpf_jit_harden == 1 && bpf_capable())1142 if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF))
1143 return false;1143 return false;
1144 1144
1145 return true;1145 return true;
include/linux/lsm_hook_defs.h+11−4
@@ -398,10 +398,17 @@ LSM_HOOK(void, LSM_RET_VOID, audit_rule_free, void *lsmrule) · 397 unmodified lines
398LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size)398LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size)
399LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode)399LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode)
400LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog)400LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog)
401LSM_HOOK(int, 0, bpf_map_alloc_security, struct bpf_map *map)401LSM_HOOK(int, 0, bpf_map_create, struct bpf_map *map, union bpf_attr *attr,
402LSM_HOOK(void, LSM_RET_VOID, bpf_map_free_security, struct bpf_map *map)402 struct bpf_token *token)
403LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux)403LSM_HOOK(void, LSM_RET_VOID, bpf_map_free, struct bpf_map *map)
404LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux)404LSM_HOOK(int, 0, bpf_prog_load, struct bpf_prog *prog, union bpf_attr *attr,
405 struct bpf_token *token)
406LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free, struct bpf_prog *prog)
407LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr,
408 struct path *path)
409LSM_HOOK(void, LSM_RET_VOID, bpf_token_free, struct bpf_token *token)
410LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
411LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
405#endif /* CONFIG_BPF_SYSCALL */412#endif /* CONFIG_BPF_SYSCALL */
406 413
407LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)414LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h+36−7
@@ -32,6 +32,7 @@ · 31 unmodified lines
32#include <linux/string.h>32#include <linux/string.h>
33#include <linux/mm.h>33#include <linux/mm.h>
34#include <linux/sockptr.h>34#include <linux/sockptr.h>
35#include <linux/bpf.h>
35 36
36struct linux_binprm;37struct linux_binprm;
37struct cred;38struct cred;
@@ -2020,15 +2021,22 @@ static inline void securityfs_remove(struct dentry *dentry) · 1982 unmodified lines
2020union bpf_attr;2021union bpf_attr;
2021struct bpf_map;2022struct bpf_map;
2022struct bpf_prog;2023struct bpf_prog;
2023struct bpf_prog_aux;2024struct bpf_token;
2024#ifdef CONFIG_SECURITY2025#ifdef CONFIG_SECURITY
2025extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size);2026extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size);
2026extern int security_bpf_map(struct bpf_map *map, fmode_t fmode);2027extern int security_bpf_map(struct bpf_map *map, fmode_t fmode);
2027extern int security_bpf_prog(struct bpf_prog *prog);2028extern int security_bpf_prog(struct bpf_prog *prog);
2028extern int security_bpf_map_alloc(struct bpf_map *map);2029extern int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2030 struct bpf_token *token);
2029extern void security_bpf_map_free(struct bpf_map *map);2031extern void security_bpf_map_free(struct bpf_map *map);
2030extern int security_bpf_prog_alloc(struct bpf_prog_aux *aux);2032extern int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2031extern void security_bpf_prog_free(struct bpf_prog_aux *aux);2033 struct bpf_token *token);
2034extern void security_bpf_prog_free(struct bpf_prog *prog);
2035extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2036 struct path *path);
2037extern void security_bpf_token_free(struct bpf_token *token);
2038extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2039extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
2032#else2040#else
2033static inline int security_bpf(int cmd, union bpf_attr *attr,2041static inline int security_bpf(int cmd, union bpf_attr *attr,
2034 unsigned int size)2042 unsigned int size)
@@ -2046,7 +2054,8 @@ static inline int security_bpf_prog(struct bpf_prog *prog) · 11 unmodified lines
2046 return 0;2054 return 0;
2047}2055}
2048 2056
2049static inline int security_bpf_map_alloc(struct bpf_map *map)2057static inline int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2058 struct bpf_token *token)
2050{2059{
2051 return 0;2060 return 0;
2052}2061}
@@ -2054,13 +2063,33 @@ static inline int security_bpf_map_alloc(struct bpf_map *map) · 1 unmodified line
2054static inline void security_bpf_map_free(struct bpf_map *map)2063static inline void security_bpf_map_free(struct bpf_map *map)
2055{ }2064{ }
2056 2065
2057static inline int security_bpf_prog_alloc(struct bpf_prog_aux *aux)2066static inline int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2067 struct bpf_token *token)
2058{2068{
2059 return 0;2069 return 0;
2060}2070}
2061 2071
2062static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)2072static inline void security_bpf_prog_free(struct bpf_prog *prog)
2063{ }2073{ }
2074
2075static inline int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2076 struct path *path)
2077{
2078 return 0;
2079}
2080
2081static inline void security_bpf_token_free(struct bpf_token *token)
2082{ }
2083
2084static inline int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
2085{
2086 return 0;
2087}
2088
2089static inline int security_bpf_token_capable(const struct bpf_token *token, int cap)
2090{
2091 return 0;
2092}
2064#endif /* CONFIG_SECURITY */2093#endif /* CONFIG_SECURITY */
2065#endif /* CONFIG_BPF_SYSCALL */2094#endif /* CONFIG_BPF_SYSCALL */
2066 2095
include/uapi/linux/bpf.h+42−0
@@ -847,6 +847,36 @@ union bpf_iter_link_info { · 846 unmodified lines
847 * Returns zero on success. On error, -1 is returned and *errno*847 * Returns zero on success. On error, -1 is returned and *errno*
848 * is set appropriately.848 * is set appropriately.
849 *849 *
850 * BPF_TOKEN_CREATE
851 * Description
852 * Create BPF token with embedded information about what
853 * BPF-related functionality it allows:
854 * - a set of allowed bpf() syscall commands;
855 * - a set of allowed BPF map types to be created with
856 * BPF_MAP_CREATE command, if BPF_MAP_CREATE itself is allowed;
857 * - a set of allowed BPF program types and BPF program attach
858 * types to be loaded with BPF_PROG_LOAD command, if
859 * BPF_PROG_LOAD itself is allowed.
860 *
861 * BPF token is created (derived) from an instance of BPF FS,
862 * assuming it has necessary delegation mount options specified.
863 * This BPF token can be passed as an extra parameter to various
864 * bpf() syscall commands to grant BPF subsystem functionality to
865 * unprivileged processes.
866 *
867 * When created, BPF token is "associated" with the owning
868 * user namespace of BPF FS instance (super block) that it was
869 * derived from, and subsequent BPF operations performed with
870 * BPF token would be performing capabilities checks (i.e.,
871 * CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN) within
872 * that user namespace. Without BPF token, such capabilities
873 * have to be granted in init user namespace, making bpf()
874 * syscall incompatible with user namespace, for the most part.
875 *
876 * Return
877 * A new file descriptor (a nonnegative integer), or -1 if an
878 * error occurred (in which case, *errno* is set appropriately).
879 *
850 * NOTES880 * NOTES
851 * eBPF objects (maps and programs) can be shared between processes.881 * eBPF objects (maps and programs) can be shared between processes.
852 *882 *
@@ -901,6 +931,8 @@ enum bpf_cmd { · 48 unmodified lines
901 BPF_ITER_CREATE,931 BPF_ITER_CREATE,
902 BPF_LINK_DETACH,932 BPF_LINK_DETACH,
903 BPF_PROG_BIND_MAP,933 BPF_PROG_BIND_MAP,
934 BPF_TOKEN_CREATE,
935 __MAX_BPF_CMD,
904};936};
905 937
906enum bpf_map_type {938enum bpf_map_type {
@@ -951,6 +983,7 @@ enum bpf_map_type { · 44 unmodified lines
951 BPF_MAP_TYPE_BLOOM_FILTER,983 BPF_MAP_TYPE_BLOOM_FILTER,
952 BPF_MAP_TYPE_USER_RINGBUF,984 BPF_MAP_TYPE_USER_RINGBUF,
953 BPF_MAP_TYPE_CGRP_STORAGE,985 BPF_MAP_TYPE_CGRP_STORAGE,
986 __MAX_BPF_MAP_TYPE
954};987};
955 988
956/* Note that tracing related programs such as989/* Note that tracing related programs such as
@@ -995,6 +1028,7 @@ enum bpf_prog_type { · 38 unmodified lines
995 BPF_PROG_TYPE_SK_LOOKUP,1028 BPF_PROG_TYPE_SK_LOOKUP,
996 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */1029 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
997 BPF_PROG_TYPE_NETFILTER,1030 BPF_PROG_TYPE_NETFILTER,
1031 __MAX_BPF_PROG_TYPE
998};1032};
999 1033
1000enum bpf_attach_type {1034enum bpf_attach_type {
@@ -1401,6 +1435,7 @@ union bpf_attr { · 400 unmodified lines
1401 * to using 5 hash functions).1435 * to using 5 hash functions).
1402 */1436 */
1403 __u64 map_extra;1437 __u64 map_extra;
1438 __u32 map_token_fd;
1404 };1439 };
1405 1440
1406 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */1441 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1470,6 +1505,7 @@ union bpf_attr { · 63 unmodified lines
1470 * truncated), or smaller (if log buffer wasn't filled completely).1505 * truncated), or smaller (if log buffer wasn't filled completely).
1471 */1506 */
1472 __u32 log_true_size;1507 __u32 log_true_size;
1508 __u32 prog_token_fd;
1473 };1509 };
1474 1510
1475 struct { /* anonymous struct used by BPF_OBJ_* commands */1511 struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -1582,6 +1618,7 @@ union bpf_attr { · 106 unmodified lines
1582 * truncated), or smaller (if log buffer wasn't filled completely).1618 * truncated), or smaller (if log buffer wasn't filled completely).
1583 */1619 */
1584 __u32 btf_log_true_size;1620 __u32 btf_log_true_size;
1621 __u32 btf_token_fd;
1585 };1622 };
1586 1623
1587 struct {1624 struct {
@@ -1712,6 +1749,11 @@ union bpf_attr { · 124 unmodified lines
1712 __u32 flags; /* extra flags */1749 __u32 flags; /* extra flags */
1713 } prog_bind_map;1750 } prog_bind_map;
1714 1751
1752 struct { /* struct used by BPF_TOKEN_CREATE command */
1753 __u32 flags;
1754 __u32 bpffs_fd;
1755 } token_create;
1756
1715} __attribute__((aligned(8)));1757} __attribute__((aligned(8)));
1716 1758
1717/* The description below is an attempt at providing documentation to eBPF1759/* The description below is an attempt at providing documentation to eBPF
kernel/bpf/Makefile+1−1
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse · 5 unmodified lines
6endif6endif
7CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)7CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
8 8
9obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o9obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o
10obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o10obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o
11obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o11obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
12obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o12obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
kernel/bpf/arraymap.c+1−1
@@ -82,7 +82,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) · 81 unmodified lines
82 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;82 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
83 int numa_node = bpf_map_attr_numa_node(attr);83 int numa_node = bpf_map_attr_numa_node(attr);
84 u32 elem_size, index_mask, max_entries;84 u32 elem_size, index_mask, max_entries;
85 bool bypass_spec_v1 = bpf_bypass_spec_v1();85 bool bypass_spec_v1 = bpf_bypass_spec_v1(NULL);
86 u64 array_size, mask64;86 u64 array_size, mask64;
87 struct bpf_array *array;87 struct bpf_array *array;
88 88
kernel/bpf/bpf_lsm.c+10−5
@@ -260,9 +260,15 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 259 unmodified lines
260BTF_SET_START(sleepable_lsm_hooks)260BTF_SET_START(sleepable_lsm_hooks)
261BTF_ID(func, bpf_lsm_bpf)261BTF_ID(func, bpf_lsm_bpf)
262BTF_ID(func, bpf_lsm_bpf_map)262BTF_ID(func, bpf_lsm_bpf_map)
263BTF_ID(func, bpf_lsm_bpf_map_alloc_security)263BTF_ID(func, bpf_lsm_bpf_map_create)
264BTF_ID(func, bpf_lsm_bpf_map_free_security)264BTF_ID(func, bpf_lsm_bpf_map_free)
265BTF_ID(func, bpf_lsm_bpf_prog)265BTF_ID(func, bpf_lsm_bpf_prog)
266BTF_ID(func, bpf_lsm_bpf_prog_load)
267BTF_ID(func, bpf_lsm_bpf_prog_free)
268BTF_ID(func, bpf_lsm_bpf_token_create)
269BTF_ID(func, bpf_lsm_bpf_token_free)
270BTF_ID(func, bpf_lsm_bpf_token_cmd)
271BTF_ID(func, bpf_lsm_bpf_token_capable)
266BTF_ID(func, bpf_lsm_bprm_check_security)272BTF_ID(func, bpf_lsm_bprm_check_security)
267BTF_ID(func, bpf_lsm_bprm_committed_creds)273BTF_ID(func, bpf_lsm_bprm_committed_creds)
268BTF_ID(func, bpf_lsm_bprm_committing_creds)274BTF_ID(func, bpf_lsm_bprm_committing_creds)
@@ -345,9 +351,8 @@ BTF_ID(func, bpf_lsm_userns_create) · 76 unmodified lines
345BTF_SET_END(sleepable_lsm_hooks)351BTF_SET_END(sleepable_lsm_hooks)
346 352
347BTF_SET_START(untrusted_lsm_hooks)353BTF_SET_START(untrusted_lsm_hooks)
348BTF_ID(func, bpf_lsm_bpf_map_free_security)354BTF_ID(func, bpf_lsm_bpf_map_free)
349BTF_ID(func, bpf_lsm_bpf_prog_alloc_security)355BTF_ID(func, bpf_lsm_bpf_prog_free)
350BTF_ID(func, bpf_lsm_bpf_prog_free_security)
351BTF_ID(func, bpf_lsm_file_alloc_security)356BTF_ID(func, bpf_lsm_file_alloc_security)
352BTF_ID(func, bpf_lsm_file_free_security)357BTF_ID(func, bpf_lsm_file_free_security)
353#ifdef CONFIG_SECURITY_NETWORK358#ifdef CONFIG_SECURITY_NETWORK
kernel/bpf/cgroup.c+3−3
@@ -1630,7 +1630,7 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 1629 unmodified lines
1630 case BPF_FUNC_perf_event_output:1630 case BPF_FUNC_perf_event_output:
1631 return &bpf_event_output_data_proto;1631 return &bpf_event_output_data_proto;
1632 default:1632 default:
1633 return bpf_base_func_proto(func_id);1633 return bpf_base_func_proto(func_id, prog);
1634 }1634 }
1635}1635}
1636 1636
@@ -2191,7 +2191,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 554 unmodified lines
2191 case BPF_FUNC_perf_event_output:2191 case BPF_FUNC_perf_event_output:
2192 return &bpf_event_output_data_proto;2192 return &bpf_event_output_data_proto;
2193 default:2193 default:
2194 return bpf_base_func_proto(func_id);2194 return bpf_base_func_proto(func_id, prog);
2195 }2195 }
2196}2196}
2197 2197
@@ -2348,7 +2348,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 150 unmodified lines
2348 case BPF_FUNC_perf_event_output:2348 case BPF_FUNC_perf_event_output:
2349 return &bpf_event_output_data_proto;2349 return &bpf_event_output_data_proto;
2350 default:2350 default:
2351 return bpf_base_func_proto(func_id);2351 return bpf_base_func_proto(func_id, prog);
2352 }2352 }
2353}2353}
2354 2354
kernel/bpf/core.c+2−1
@@ -675,7 +675,7 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) · 674 unmodified lines
675void bpf_prog_kallsyms_add(struct bpf_prog *fp)675void bpf_prog_kallsyms_add(struct bpf_prog *fp)
676{676{
677 if (!bpf_prog_kallsyms_candidate(fp) ||677 if (!bpf_prog_kallsyms_candidate(fp) ||
678 !bpf_capable())678 !bpf_token_capable(fp->aux->token, CAP_BPF))
679 return;679 return;
680 680
681 bpf_prog_ksym_set_addr(fp);681 bpf_prog_ksym_set_addr(fp);
@@ -2751,6 +2751,7 @@ void bpf_prog_free(struct bpf_prog *fp) · 2069 unmodified lines
2751 2751
2752 if (aux->dst_prog)2752 if (aux->dst_prog)
2753 bpf_prog_put(aux->dst_prog);2753 bpf_prog_put(aux->dst_prog);
2754 bpf_token_put(aux->token);
2754 INIT_WORK(&aux->work, bpf_prog_free_deferred);2755 INIT_WORK(&aux->work, bpf_prog_free_deferred);
2755 schedule_work(&aux->work);2756 schedule_work(&aux->work);
2756}2757}
kernel/bpf/helpers.c+3−3
@@ -1679,7 +1679,7 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak; · 1678 unmodified lines
1679const struct bpf_func_proto bpf_task_pt_regs_proto __weak;1679const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
1680 1680
1681const struct bpf_func_proto *1681const struct bpf_func_proto *
1682bpf_base_func_proto(enum bpf_func_id func_id)1682bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1683{1683{
1684 switch (func_id) {1684 switch (func_id) {
1685 case BPF_FUNC_map_lookup_elem:1685 case BPF_FUNC_map_lookup_elem:
@@ -1730,7 +1730,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 44 unmodified lines
1730 break;1730 break;
1731 }1731 }
1732 1732
1733 if (!bpf_capable())1733 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
1734 return NULL;1734 return NULL;
1735 1735
1736 switch (func_id) {1736 switch (func_id) {
@@ -1788,7 +1788,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 51 unmodified lines
1788 break;1788 break;
1789 }1789 }
1790 1790
1791 if (!perfmon_capable())1791 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
1792 return NULL;1792 return NULL;
1793 1793
1794 switch (func_id) {1794 switch (func_id) {
kernel/bpf/inode.c+87−14
@@ -20,6 +20,7 @@ · 19 unmodified lines
20#include <linux/filter.h>20#include <linux/filter.h>
21#include <linux/bpf.h>21#include <linux/bpf.h>
22#include <linux/bpf_trace.h>22#include <linux/bpf_trace.h>
23#include <linux/kstrtox.h>
23#include "preload/bpf_preload.h"24#include "preload/bpf_preload.h"
24 25
25enum bpf_type {26enum bpf_type {
@@ -98,9 +99,9 @@ static const struct inode_operations bpf_prog_iops = { }; · 72 unmodified lines
98static const struct inode_operations bpf_map_iops = { };99static const struct inode_operations bpf_map_iops = { };
99static const struct inode_operations bpf_link_iops = { };100static const struct inode_operations bpf_link_iops = { };
100 101
101static struct inode *bpf_get_inode(struct super_block *sb,102struct inode *bpf_get_inode(struct super_block *sb,
102 const struct inode *dir,103 const struct inode *dir,
103 umode_t mode)104 umode_t mode)
104{105{
105 struct inode *inode;106 struct inode *inode;
106 107
@@ -599,10 +600,36 @@ EXPORT_SYMBOL(bpf_prog_get_type_path); · 492 unmodified lines
599 */600 */
600static int bpf_show_options(struct seq_file *m, struct dentry *root)601static int bpf_show_options(struct seq_file *m, struct dentry *root)
601{602{
603 struct bpf_mount_opts *opts = root->d_sb->s_fs_info;
602 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;604 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
605 u64 mask;
603 606
604 if (mode != S_IRWXUGO)607 if (mode != S_IRWXUGO)
605 seq_printf(m, ",mode=%o", mode);608 seq_printf(m, ",mode=%o", mode);
609
610 mask = (1ULL << __MAX_BPF_CMD) - 1;
611 if ((opts->delegate_cmds & mask) == mask)
612 seq_printf(m, ",delegate_cmds=any");
613 else if (opts->delegate_cmds)
614 seq_printf(m, ",delegate_cmds=0x%llx", opts->delegate_cmds);
615
616 mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1;
617 if ((opts->delegate_maps & mask) == mask)
618 seq_printf(m, ",delegate_maps=any");
619 else if (opts->delegate_maps)
620 seq_printf(m, ",delegate_maps=0x%llx", opts->delegate_maps);
621
622 mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1;
623 if ((opts->delegate_progs & mask) == mask)
624 seq_printf(m, ",delegate_progs=any");
625 else if (opts->delegate_progs)
626 seq_printf(m, ",delegate_progs=0x%llx", opts->delegate_progs);
627
628 mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1;
629 if ((opts->delegate_attachs & mask) == mask)
630 seq_printf(m, ",delegate_attachs=any");
631 else if (opts->delegate_attachs)
632 seq_printf(m, ",delegate_attachs=0x%llx", opts->delegate_attachs);
606 return 0;633 return 0;
607}634}
608 635
@@ -617,7 +644,7 @@ static void bpf_free_inode(struct inode *inode) · 8 unmodified lines
617 free_inode_nonrcu(inode);644 free_inode_nonrcu(inode);
618}645}
619 646
620static const struct super_operations bpf_super_ops = {647const struct super_operations bpf_super_ops = {
621 .statfs = simple_statfs,648 .statfs = simple_statfs,
622 .drop_inode = generic_delete_inode,649 .drop_inode = generic_delete_inode,
623 .show_options = bpf_show_options,650 .show_options = bpf_show_options,
@@ -626,22 +653,27 @@ static const struct super_operations bpf_super_ops = { · 2 unmodified lines
626 653
627enum {654enum {
628 OPT_MODE,655 OPT_MODE,
656 OPT_DELEGATE_CMDS,
657 OPT_DELEGATE_MAPS,
658 OPT_DELEGATE_PROGS,
659 OPT_DELEGATE_ATTACHS,
629};660};
630 661
631static const struct fs_parameter_spec bpf_fs_parameters[] = {662static const struct fs_parameter_spec bpf_fs_parameters[] = {
632 fsparam_u32oct ("mode", OPT_MODE),663 fsparam_u32oct ("mode", OPT_MODE),
664 fsparam_string ("delegate_cmds", OPT_DELEGATE_CMDS),
665 fsparam_string ("delegate_maps", OPT_DELEGATE_MAPS),
666 fsparam_string ("delegate_progs", OPT_DELEGATE_PROGS),
667 fsparam_string ("delegate_attachs", OPT_DELEGATE_ATTACHS),
633 {}668 {}
634};669};
635 670
636struct bpf_mount_opts {
637 umode_t mode;
638};
639
640static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)671static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
641{672{
642 struct bpf_mount_opts *opts = fc->fs_private;673 struct bpf_mount_opts *opts = fc->s_fs_info;
643 struct fs_parse_result result;674 struct fs_parse_result result;
644 int opt;675 int opt, err;
676 u64 msk;
645 677
646 opt = fs_parse(fc, bpf_fs_parameters, param, &result);678 opt = fs_parse(fc, bpf_fs_parameters, param, &result);
647 if (opt < 0) {679 if (opt < 0) {
@@ -665,6 +697,28 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param) · 17 unmodified lines
665 case OPT_MODE:697 case OPT_MODE:
666 opts->mode = result.uint_32 & S_IALLUGO;698 opts->mode = result.uint_32 & S_IALLUGO;
667 break;699 break;
700 case OPT_DELEGATE_CMDS:
701 case OPT_DELEGATE_MAPS:
702 case OPT_DELEGATE_PROGS:
703 case OPT_DELEGATE_ATTACHS:
704 if (strcmp(param->string, "any") == 0) {
705 msk = ~0ULL;
706 } else {
707 err = kstrtou64(param->string, 0, &msk);
708 if (err)
709 return err;
710 }
711 /* Setting delegation mount options requires privileges */
712 if (msk && !capable(CAP_SYS_ADMIN))
713 return -EPERM;
714 switch (opt) {
715 case OPT_DELEGATE_CMDS: opts->delegate_cmds |= msk; break;
716 case OPT_DELEGATE_MAPS: opts->delegate_maps |= msk; break;
717 case OPT_DELEGATE_PROGS: opts->delegate_progs |= msk; break;
718 case OPT_DELEGATE_ATTACHS: opts->delegate_attachs |= msk; break;
719 default: return -EINVAL;
720 }
721 break;
668 }722 }
669 723
670 return 0;724 return 0;
@@ -739,10 +793,14 @@ out: · 68 unmodified lines
739static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)793static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
740{794{
741 static const struct tree_descr bpf_rfiles[] = { { "" } };795 static const struct tree_descr bpf_rfiles[] = { { "" } };
742 struct bpf_mount_opts *opts = fc->fs_private;796 struct bpf_mount_opts *opts = sb->s_fs_info;
743 struct inode *inode;797 struct inode *inode;
744 int ret;798 int ret;
745 799
800 /* Mounting an instance of BPF FS requires privileges */
801 if (fc->user_ns != &init_user_ns && !capable(CAP_SYS_ADMIN))
802 return -EPERM;
803
746 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);804 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
747 if (ret)805 if (ret)
748 return ret;806 return ret;
@@ -764,7 +822,7 @@ static int bpf_get_tree(struct fs_context *fc) · 15 unmodified lines
764 822
765static void bpf_free_fc(struct fs_context *fc)823static void bpf_free_fc(struct fs_context *fc)
766{824{
767 kfree(fc->fs_private);825 kfree(fc->s_fs_info);
768}826}
769 827
770static const struct fs_context_operations bpf_context_ops = {828static const struct fs_context_operations bpf_context_ops = {
@@ -786,17 +844,32 @@ static int bpf_init_fs_context(struct fs_context *fc) · 15 unmodified lines
786 844
787 opts->mode = S_IRWXUGO;845 opts->mode = S_IRWXUGO;
788 846
789 fc->fs_private = opts;847 /* start out with no BPF token delegation enabled */
848 opts->delegate_cmds = 0;
849 opts->delegate_maps = 0;
850 opts->delegate_progs = 0;
851 opts->delegate_attachs = 0;
852
853 fc->s_fs_info = opts;
790 fc->ops = &bpf_context_ops;854 fc->ops = &bpf_context_ops;
791 return 0;855 return 0;
792}856}
793 857
858static void bpf_kill_super(struct super_block *sb)
859{
860 struct bpf_mount_opts *opts = sb->s_fs_info;
861
862 kill_litter_super(sb);
863 kfree(opts);
864}
865
794static struct file_system_type bpf_fs_type = {866static struct file_system_type bpf_fs_type = {
795 .owner = THIS_MODULE,867 .owner = THIS_MODULE,
796 .name = "bpf",868 .name = "bpf",
797 .init_fs_context = bpf_init_fs_context,869 .init_fs_context = bpf_init_fs_context,
798 .parameters = bpf_fs_parameters,870 .parameters = bpf_fs_parameters,
799 .kill_sb = kill_litter_super,871 .kill_sb = bpf_kill_super,
872 .fs_flags = FS_USERNS_MOUNT,
800};873};
801 874
802static int __init bpf_init(void)875static int __init bpf_init(void)
kernel/bpf/syscall.c+159−56
@@ -1009,8 +1009,8 @@ int map_check_no_btf(const struct bpf_map *map, · 1008 unmodified lines
1009 return -ENOTSUPP;1009 return -ENOTSUPP;
1010}1010}
1011 1011
1012static int map_check_btf(struct bpf_map *map, const struct btf *btf,1012static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
1013 u32 btf_key_id, u32 btf_value_id)1013 const struct btf *btf, u32 btf_key_id, u32 btf_value_id)
1014{1014{
1015 const struct btf_type *key_type, *value_type;1015 const struct btf_type *key_type, *value_type;
1016 u32 key_size, value_size;1016 u32 key_size, value_size;
@@ -1038,7 +1038,7 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, · 21 unmodified lines
1038 if (!IS_ERR_OR_NULL(map->record)) {1038 if (!IS_ERR_OR_NULL(map->record)) {
1039 int i;1039 int i;
1040 1040
1041 if (!bpf_capable()) {1041 if (!bpf_token_capable(token, CAP_BPF)) {
1042 ret = -EPERM;1042 ret = -EPERM;
1043 goto free_map_tab;1043 goto free_map_tab;
1044 }1044 }
@@ -1121,11 +1121,17 @@ free_map_tab: · 76 unmodified lines
1121 return ret;1121 return ret;
1122}1122}
1123 1123
1124#define BPF_MAP_CREATE_LAST_FIELD map_extra1124static bool bpf_net_capable(void)
1125{
1126 return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
1127}
1128
1129#define BPF_MAP_CREATE_LAST_FIELD map_token_fd
1125/* called via syscall */1130/* called via syscall */
1126static int map_create(union bpf_attr *attr)1131static int map_create(union bpf_attr *attr)
1127{1132{
1128 const struct bpf_map_ops *ops;1133 const struct bpf_map_ops *ops;
1134 struct bpf_token *token = NULL;
1129 int numa_node = bpf_map_attr_numa_node(attr);1135 int numa_node = bpf_map_attr_numa_node(attr);
1130 u32 map_type = attr->map_type;1136 u32 map_type = attr->map_type;
1131 struct bpf_map *map;1137 struct bpf_map *map;
@@ -1176,14 +1182,32 @@ static int map_create(union bpf_attr *attr) · 44 unmodified lines
1176 if (!ops->map_mem_usage)1182 if (!ops->map_mem_usage)
1177 return -EINVAL;1183 return -EINVAL;
1178 1184
1185 if (attr->map_token_fd) {
1186 token = bpf_token_get_from_fd(attr->map_token_fd);
1187 if (IS_ERR(token))
1188 return PTR_ERR(token);
1189
1190 /* if current token doesn't grant map creation permissions,
1191 * then we can't use this token, so ignore it and rely on
1192 * system-wide capabilities checks
1193 */
1194 if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) ||
1195 !bpf_token_allow_map_type(token, attr->map_type)) {
1196 bpf_token_put(token);
1197 token = NULL;
1198 }
1199 }
1200
1201 err = -EPERM;
1202
1179 /* Intent here is for unprivileged_bpf_disabled to block BPF map1203 /* Intent here is for unprivileged_bpf_disabled to block BPF map
1180 * creation for unprivileged users; other actions depend1204 * creation for unprivileged users; other actions depend
1181 * on fd availability and access to bpffs, so are dependent on1205 * on fd availability and access to bpffs, so are dependent on
1182 * object creation success. Even with unprivileged BPF disabled,1206 * object creation success. Even with unprivileged BPF disabled,
1183 * capability checks are still carried out.1207 * capability checks are still carried out.
1184 */1208 */
1185 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())1209 if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF))
1186 return -EPERM;1210 goto put_token;
1187 1211
1188 /* check privileged map type permissions */1212 /* check privileged map type permissions */
1189 switch (map_type) {1213 switch (map_type) {
@@ -1216,25 +1240,27 @@ static int map_create(union bpf_attr *attr) · 26 unmodified lines
1216 case BPF_MAP_TYPE_LRU_PERCPU_HASH:1240 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
1217 case BPF_MAP_TYPE_STRUCT_OPS:1241 case BPF_MAP_TYPE_STRUCT_OPS:
1218 case BPF_MAP_TYPE_CPUMAP:1242 case BPF_MAP_TYPE_CPUMAP:
1219 if (!bpf_capable())1243 if (!bpf_token_capable(token, CAP_BPF))
1220 return -EPERM;1244 goto put_token;
1221 break;1245 break;
1222 case BPF_MAP_TYPE_SOCKMAP:1246 case BPF_MAP_TYPE_SOCKMAP:
1223 case BPF_MAP_TYPE_SOCKHASH:1247 case BPF_MAP_TYPE_SOCKHASH:
1224 case BPF_MAP_TYPE_DEVMAP:1248 case BPF_MAP_TYPE_DEVMAP:
1225 case BPF_MAP_TYPE_DEVMAP_HASH:1249 case BPF_MAP_TYPE_DEVMAP_HASH:
1226 case BPF_MAP_TYPE_XSKMAP:1250 case BPF_MAP_TYPE_XSKMAP:
1227 if (!capable(CAP_NET_ADMIN))1251 if (!bpf_token_capable(token, CAP_NET_ADMIN))
1228 return -EPERM;1252 goto put_token;
1229 break;1253 break;
1230 default:1254 default:
1231 WARN(1, "unsupported map type %d", map_type);1255 WARN(1, "unsupported map type %d", map_type);
1232 return -EPERM;1256 goto put_token;
1233 }1257 }
1234 1258
1235 map = ops->map_alloc(attr);1259 map = ops->map_alloc(attr);
1236 if (IS_ERR(map))1260 if (IS_ERR(map)) {
1237 return PTR_ERR(map);1261 err = PTR_ERR(map);
1262 goto put_token;
1263 }
1238 map->ops = ops;1264 map->ops = ops;
1239 map->map_type = map_type;1265 map->map_type = map_type;
1240 1266
@@ -1271,7 +1297,7 @@ static int map_create(union bpf_attr *attr) · 30 unmodified lines
1271 map->btf = btf;1297 map->btf = btf;
1272 1298
1273 if (attr->btf_value_type_id) {1299 if (attr->btf_value_type_id) {
1274 err = map_check_btf(map, btf, attr->btf_key_type_id,1300 err = map_check_btf(map, token, btf, attr->btf_key_type_id,
1275 attr->btf_value_type_id);1301 attr->btf_value_type_id);
1276 if (err)1302 if (err)
1277 goto free_map;1303 goto free_map;
@@ -1283,15 +1309,16 @@ static int map_create(union bpf_attr *attr) · 5 unmodified lines
1283 attr->btf_vmlinux_value_type_id;1309 attr->btf_vmlinux_value_type_id;
1284 }1310 }
1285 1311
1286 err = security_bpf_map_alloc(map);1312 err = security_bpf_map_create(map, attr, token);
1287 if (err)1313 if (err)
1288 goto free_map;1314 goto free_map_sec;
1289 1315
1290 err = bpf_map_alloc_id(map);1316 err = bpf_map_alloc_id(map);
1291 if (err)1317 if (err)
1292 goto free_map_sec;1318 goto free_map_sec;
1293 1319
1294 bpf_map_save_memcg(map);1320 bpf_map_save_memcg(map);
1321 bpf_token_put(token);
1295 1322
1296 err = bpf_map_new_fd(map, f_flags);1323 err = bpf_map_new_fd(map, f_flags);
1297 if (err < 0) {1324 if (err < 0) {
@@ -1312,6 +1339,8 @@ free_map_sec: · 14 unmodified lines
1312free_map:1339free_map:
1313 btf_put(map->btf);1340 btf_put(map->btf);
1314 map->ops->map_free(map);1341 map->ops->map_free(map);
1342put_token:
1343 bpf_token_put(token);
1315 return err;1344 return err;
1316}1345}
1317 1346
@@ -2133,7 +2162,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu) · 815 unmodified lines
2133 kvfree(aux->func_info);2162 kvfree(aux->func_info);
2134 kfree(aux->func_info_aux);2163 kfree(aux->func_info_aux);
2135 free_uid(aux->user);2164 free_uid(aux->user);
2136 security_bpf_prog_free(aux);2165 security_bpf_prog_free(aux->prog);
2137 bpf_prog_free(aux->prog);2166 bpf_prog_free(aux->prog);
2138}2167}
2139 2168
@@ -2579,13 +2608,15 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · 439 unmodified lines
2579}2608}
2580 2609
2581/* last field in 'union bpf_attr' used by this command */2610/* last field in 'union bpf_attr' used by this command */
2582#define BPF_PROG_LOAD_LAST_FIELD log_true_size2611#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd
2583 2612
2584static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)2613static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
2585{2614{
2586 enum bpf_prog_type type = attr->prog_type;2615 enum bpf_prog_type type = attr->prog_type;
2587 struct bpf_prog *prog, *dst_prog = NULL;2616 struct bpf_prog *prog, *dst_prog = NULL;
2588 struct btf *attach_btf = NULL;2617 struct btf *attach_btf = NULL;
2618 struct bpf_token *token = NULL;
2619 bool bpf_cap;
2589 int err;2620 int err;
2590 char license[128];2621 char license[128];
2591 2622
@@ -2602,10 +2633,31 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 10 unmodified lines
2602 BPF_F_TEST_REG_INVARIANTS))2633 BPF_F_TEST_REG_INVARIANTS))
2603 return -EINVAL;2634 return -EINVAL;
2604 2635
2636 bpf_prog_load_fixup_attach_type(attr);
2637
2638 if (attr->prog_token_fd) {
2639 token = bpf_token_get_from_fd(attr->prog_token_fd);
2640 if (IS_ERR(token))
2641 return PTR_ERR(token);
2642 /* if current token doesn't grant prog loading permissions,
2643 * then we can't use this token, so ignore it and rely on
2644 * system-wide capabilities checks
2645 */
2646 if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) ||
2647 !bpf_token_allow_prog_type(token, attr->prog_type,
2648 attr->expected_attach_type)) {
2649 bpf_token_put(token);
2650 token = NULL;
2651 }
2652 }
2653
2654 bpf_cap = bpf_token_capable(token, CAP_BPF);
2655 err = -EPERM;
2656
2605 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&2657 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2606 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&2658 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2607 !bpf_capable())2659 !bpf_cap)
2608 return -EPERM;2660 goto put_token;
2609 2661
2610 /* Intent here is for unprivileged_bpf_disabled to block BPF program2662 /* Intent here is for unprivileged_bpf_disabled to block BPF program
2611 * creation for unprivileged users; other actions depend2663 * creation for unprivileged users; other actions depend
@@ -2614,21 +2666,23 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 2 unmodified lines
2614 * capability checks are still carried out for these2666 * capability checks are still carried out for these
2615 * and other operations.2667 * and other operations.
2616 */2668 */
2617 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())2669 if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
2618 return -EPERM;2670 goto put_token;
2619 2671
2620 if (attr->insn_cnt == 0 ||2672 if (attr->insn_cnt == 0 ||
2621 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))2673 attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) {
2622 return -E2BIG;2674 err = -E2BIG;
2675 goto put_token;
2676 }
2623 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&2677 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2624 type != BPF_PROG_TYPE_CGROUP_SKB &&2678 type != BPF_PROG_TYPE_CGROUP_SKB &&
2625 !bpf_capable())2679 !bpf_cap)
2626 return -EPERM;2680 goto put_token;
2627 2681
2628 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))2682 if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN))
2629 return -EPERM;2683 goto put_token;
2630 if (is_perfmon_prog_type(type) && !perfmon_capable())2684 if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON))
2631 return -EPERM;2685 goto put_token;
2632 2686
2633 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog2687 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2634 * or btf, we need to check which one it is2688 * or btf, we need to check which one it is
@@ -2638,27 +2692,33 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 3 unmodified lines
2638 if (IS_ERR(dst_prog)) {2692 if (IS_ERR(dst_prog)) {
2639 dst_prog = NULL;2693 dst_prog = NULL;
2640 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);2694 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2641 if (IS_ERR(attach_btf))2695 if (IS_ERR(attach_btf)) {
2642 return -EINVAL;2696 err = -EINVAL;
2697 goto put_token;
2698 }
2643 if (!btf_is_kernel(attach_btf)) {2699 if (!btf_is_kernel(attach_btf)) {
2644 /* attaching through specifying bpf_prog's BTF2700 /* attaching through specifying bpf_prog's BTF
2645 * objects directly might be supported eventually2701 * objects directly might be supported eventually
2646 */2702 */
2647 btf_put(attach_btf);2703 btf_put(attach_btf);
2648 return -ENOTSUPP;2704 err = -ENOTSUPP;
2705 goto put_token;
2649 }2706 }
2650 }2707 }
2651 } else if (attr->attach_btf_id) {2708 } else if (attr->attach_btf_id) {
2652 /* fall back to vmlinux BTF, if BTF type ID is specified */2709 /* fall back to vmlinux BTF, if BTF type ID is specified */
2653 attach_btf = bpf_get_btf_vmlinux();2710 attach_btf = bpf_get_btf_vmlinux();
2654 if (IS_ERR(attach_btf))2711 if (IS_ERR(attach_btf)) {
2655 return PTR_ERR(attach_btf);2712 err = PTR_ERR(attach_btf);
2656 if (!attach_btf)2713 goto put_token;
2657 return -EINVAL;2714 }
2715 if (!attach_btf) {
2716 err = -EINVAL;
2717 goto put_token;
2718 }
2658 btf_get(attach_btf);2719 btf_get(attach_btf);
2659 }2720 }
2660 2721
2661 bpf_prog_load_fixup_attach_type(attr);
2662 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,2722 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2663 attach_btf, attr->attach_btf_id,2723 attach_btf, attr->attach_btf_id,
2664 dst_prog)) {2724 dst_prog)) {
@@ -2666,7 +2726,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 1 unmodified line
2666 bpf_prog_put(dst_prog);2726 bpf_prog_put(dst_prog);
2667 if (attach_btf)2727 if (attach_btf)
2668 btf_put(attach_btf);2728 btf_put(attach_btf);
2669 return -EINVAL;2729 err = -EINVAL;
2730 goto put_token;
2670 }2731 }
2671 2732
2672 /* plain bpf_prog allocation */2733 /* plain bpf_prog allocation */
@@ -2676,7 +2737,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 3 unmodified lines
2676 bpf_prog_put(dst_prog);2737 bpf_prog_put(dst_prog);
2677 if (attach_btf)2738 if (attach_btf)
2678 btf_put(attach_btf);2739 btf_put(attach_btf);
2679 return -ENOMEM;2740 err = -EINVAL;
2741 goto put_token;
2680 }2742 }
2681 2743
2682 prog->expected_attach_type = attr->expected_attach_type;2744 prog->expected_attach_type = attr->expected_attach_type;
@@ -2687,9 +2749,9 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 4 unmodified lines
2687 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;2749 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2688 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;2750 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2689 2751
2690 err = security_bpf_prog_alloc(prog->aux);2752 /* move token into prog->aux, reuse taken refcnt */
2691 if (err)2753 prog->aux->token = token;
2692 goto free_prog;2754 token = NULL;
2693 2755
2694 prog->aux->user = get_current_user();2756 prog->aux->user = get_current_user();
2695 prog->len = attr->insn_cnt;2757 prog->len = attr->insn_cnt;
@@ -2698,12 +2760,12 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 2 unmodified lines
2698 if (copy_from_bpfptr(prog->insns,2760 if (copy_from_bpfptr(prog->insns,
2699 make_bpfptr(attr->insns, uattr.is_kernel),2761 make_bpfptr(attr->insns, uattr.is_kernel),
2700 bpf_prog_insn_size(prog)) != 0)2762 bpf_prog_insn_size(prog)) != 0)
2701 goto free_prog_sec;2763 goto free_prog;
2702 /* copy eBPF program license from user space */2764 /* copy eBPF program license from user space */
2703 if (strncpy_from_bpfptr(license,2765 if (strncpy_from_bpfptr(license,
2704 make_bpfptr(attr->license, uattr.is_kernel),2766 make_bpfptr(attr->license, uattr.is_kernel),
2705 sizeof(license) - 1) < 0)2767 sizeof(license) - 1) < 0)
2706 goto free_prog_sec;2768 goto free_prog;
2707 license[sizeof(license) - 1] = 0;2769 license[sizeof(license) - 1] = 0;
2708 2770
2709 /* eBPF programs must be GPL compatible to use GPL-ed functions */2771 /* eBPF programs must be GPL compatible to use GPL-ed functions */
@@ -2717,25 +2779,29 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 7 unmodified lines
2717 if (bpf_prog_is_dev_bound(prog->aux)) {2779 if (bpf_prog_is_dev_bound(prog->aux)) {
2718 err = bpf_prog_dev_bound_init(prog, attr);2780 err = bpf_prog_dev_bound_init(prog, attr);
2719 if (err)2781 if (err)
2720 goto free_prog_sec;2782 goto free_prog;
2721 }2783 }
2722 2784
2723 if (type == BPF_PROG_TYPE_EXT && dst_prog &&2785 if (type == BPF_PROG_TYPE_EXT && dst_prog &&
2724 bpf_prog_is_dev_bound(dst_prog->aux)) {2786 bpf_prog_is_dev_bound(dst_prog->aux)) {
2725 err = bpf_prog_dev_bound_inherit(prog, dst_prog);2787 err = bpf_prog_dev_bound_inherit(prog, dst_prog);
2726 if (err)2788 if (err)
2727 goto free_prog_sec;2789 goto free_prog;
2728 }2790 }
2729 2791
2730 /* find program type: socket_filter vs tracing_filter */2792 /* find program type: socket_filter vs tracing_filter */
2731 err = find_prog_type(type, prog);2793 err = find_prog_type(type, prog);
2732 if (err < 0)2794 if (err < 0)
2733 goto free_prog_sec;2795 goto free_prog;
2734 2796
2735 prog->aux->load_time = ktime_get_boottime_ns();2797 prog->aux->load_time = ktime_get_boottime_ns();
2736 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,2798 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2737 sizeof(attr->prog_name));2799 sizeof(attr->prog_name));
2738 if (err < 0)2800 if (err < 0)
2801 goto free_prog;
2802
2803 err = security_bpf_prog_load(prog, attr, token);
2804 if (err)
2739 goto free_prog_sec;2805 goto free_prog_sec;
2740 2806
2741 /* run eBPF verifier */2807 /* run eBPF verifier */
@@ -2781,13 +2847,16 @@ free_used_maps: · 39 unmodified lines
2781 */2847 */
2782 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt);2848 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt);
2783 return err;2849 return err;
2850
2784free_prog_sec:2851free_prog_sec:
2785 free_uid(prog->aux->user);2852 security_bpf_prog_free(prog);
2786 security_bpf_prog_free(prog->aux);
2787free_prog:2853free_prog:
2854 free_uid(prog->aux->user);
2788 if (prog->aux->attach_btf)2855 if (prog->aux->attach_btf)
2789 btf_put(prog->aux->attach_btf);2856 btf_put(prog->aux->attach_btf);
2790 bpf_prog_free(prog);2857 bpf_prog_free(prog);
2858put_token:
2859 bpf_token_put(token);
2791 return err;2860 return err;
2792}2861}
2793 2862
@@ -3777,7 +3846,7 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, · 983 unmodified lines
3777 case BPF_PROG_TYPE_SK_LOOKUP:3846 case BPF_PROG_TYPE_SK_LOOKUP:
3778 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;3847 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3779 case BPF_PROG_TYPE_CGROUP_SKB:3848 case BPF_PROG_TYPE_CGROUP_SKB:
3780 if (!capable(CAP_NET_ADMIN))3849 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
3781 /* cg-skb progs can be loaded by unpriv user.3850 /* cg-skb progs can be loaded by unpriv user.
3782 * check permissions at attach time.3851 * check permissions at attach time.
3783 */3852 */
@@ -3980,7 +4049,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 196 unmodified lines
3980static int bpf_prog_query(const union bpf_attr *attr,4049static int bpf_prog_query(const union bpf_attr *attr,
3981 union bpf_attr __user *uattr)4050 union bpf_attr __user *uattr)
3982{4051{
3983 if (!capable(CAP_NET_ADMIN))4052 if (!bpf_net_capable())
3984 return -EPERM;4053 return -EPERM;
3985 if (CHECK_ATTR(BPF_PROG_QUERY))4054 if (CHECK_ATTR(BPF_PROG_QUERY))
3986 return -EINVAL;4055 return -EINVAL;
@@ -4748,15 +4817,31 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, · 761 unmodified lines
4748 return err;4817 return err;
4749}4818}
4750 4819
4751#define BPF_BTF_LOAD_LAST_FIELD btf_log_true_size4820#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
4752 4821
4753static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)4822static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
4754{4823{
4824 struct bpf_token *token = NULL;
4825
4755 if (CHECK_ATTR(BPF_BTF_LOAD))4826 if (CHECK_ATTR(BPF_BTF_LOAD))
4756 return -EINVAL;4827 return -EINVAL;
4757 4828
4758 if (!bpf_capable())4829 if (attr->btf_token_fd) {
4830 token = bpf_token_get_from_fd(attr->btf_token_fd);
4831 if (IS_ERR(token))
4832 return PTR_ERR(token);
4833 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) {
4834 bpf_token_put(token);
4835 token = NULL;
4836 }
4837 }
4838
4839 if (!bpf_token_capable(token, CAP_BPF)) {
4840 bpf_token_put(token);
4759 return -EPERM;4841 return -EPERM;
4842 }
4843
4844 bpf_token_put(token);
4760 4845
4761 return btf_new_fd(attr, uattr, uattr_size);4846 return btf_new_fd(attr, uattr, uattr_size);
4762}4847}
@@ -5372,6 +5457,20 @@ out_prog_put: · 609 unmodified lines
5372 return ret;5457 return ret;
5373}5458}
5374 5459
5460#define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd
5461
5462static int token_create(union bpf_attr *attr)
5463{
5464 if (CHECK_ATTR(BPF_TOKEN_CREATE))
5465 return -EINVAL;
5466
5467 /* no flags are supported yet */
5468 if (attr->token_create.flags)
5469 return -EINVAL;
5470
5471 return bpf_token_create(attr);
5472}
5473
5375static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)5474static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
5376{5475{
5377 union bpf_attr attr;5476 union bpf_attr attr;
@@ -5505,6 +5604,9 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) · 127 unmodified lines
5505 case BPF_PROG_BIND_MAP:5604 case BPF_PROG_BIND_MAP:
5506 err = bpf_prog_bind_map(&attr);5605 err = bpf_prog_bind_map(&attr);
5507 break;5606 break;
5607 case BPF_TOKEN_CREATE:
5608 err = token_create(&attr);
5609 break;
5508 default:5610 default:
5509 err = -EINVAL;5611 err = -EINVAL;
5510 break;5612 break;
@@ -5611,7 +5713,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { · 100 unmodified lines
5611const struct bpf_func_proto * __weak5713const struct bpf_func_proto * __weak
5612tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)5714tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5613{5715{
5614 return bpf_base_func_proto(func_id);5716 return bpf_base_func_proto(func_id, prog);
5615}5717}
5616 5718
5617BPF_CALL_1(bpf_sys_close, u32, fd)5719BPF_CALL_1(bpf_sys_close, u32, fd)
@@ -5661,7 +5763,8 @@ syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 43 unmodified lines
5661{5763{
5662 switch (func_id) {5764 switch (func_id) {
5663 case BPF_FUNC_sys_bpf:5765 case BPF_FUNC_sys_bpf:
5664 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;5766 return !bpf_token_capable(prog->aux->token, CAP_PERFMON)
5767 ? NULL : &bpf_sys_bpf_proto;
5665 case BPF_FUNC_btf_find_by_name_kind:5768 case BPF_FUNC_btf_find_by_name_kind:
5666 return &bpf_btf_find_by_name_kind_proto;5769 return &bpf_btf_find_by_name_kind_proto;
5667 case BPF_FUNC_sys_close:5770 case BPF_FUNC_sys_close:
kernel/bpf/token.c+263−0
@@ -0,0 +1,263 @@
1#include <linux/bpf.h>
2#include <linux/vmalloc.h>
3#include <linux/fdtable.h>
4#include <linux/file.h>
5#include <linux/fs.h>
6#include <linux/kernel.h>
7#include <linux/idr.h>
8#include <linux/namei.h>
9#include <linux/user_namespace.h>
10#include <linux/security.h>
11
12bool bpf_token_capable(const struct bpf_token *token, int cap)
13{
14 /* BPF token allows ns_capable() level of capabilities, but only if
15 * token's userns is *exactly* the same as current user's userns
16 */
17 if (token && current_user_ns() == token->userns) {
18 if (ns_capable(token->userns, cap) ||
19 (cap != CAP_SYS_ADMIN && ns_capable(token->userns, CAP_SYS_ADMIN)))
20 return security_bpf_token_capable(token, cap) == 0;
21 }
22 /* otherwise fallback to capable() checks */
23 return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
24}
25
26void bpf_token_inc(struct bpf_token *token)
27{
28 atomic64_inc(&token->refcnt);
29}
30
31static void bpf_token_free(struct bpf_token *token)
32{
33 security_bpf_token_free(token);
34 put_user_ns(token->userns);
35 kvfree(token);
36}
37
38static void bpf_token_put_deferred(struct work_struct *work)
39{
40 struct bpf_token *token = container_of(work, struct bpf_token, work);
41
42 bpf_token_free(token);
43}
44
45void bpf_token_put(struct bpf_token *token)
46{
47 if (!token)
48 return;
49
50 if (!atomic64_dec_and_test(&token->refcnt))
51 return;
52
53 INIT_WORK(&token->work, bpf_token_put_deferred);
54 schedule_work(&token->work);
55}
56
57static int bpf_token_release(struct inode *inode, struct file *filp)
58{
59 struct bpf_token *token = filp->private_data;
60
61 bpf_token_put(token);
62 return 0;
63}
64
65static void bpf_token_show_fdinfo(struct seq_file *m, struct file *filp)
66{
67 struct bpf_token *token = filp->private_data;
68 u64 mask;
69
70 BUILD_BUG_ON(__MAX_BPF_CMD >= 64);
71 mask = (1ULL << __MAX_BPF_CMD) - 1;
72 if ((token->allowed_cmds & mask) == mask)
73 seq_printf(m, "allowed_cmds:\tany\n");
74 else
75 seq_printf(m, "allowed_cmds:\t0x%llx\n", token->allowed_cmds);
76
77 BUILD_BUG_ON(__MAX_BPF_MAP_TYPE >= 64);
78 mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1;
79 if ((token->allowed_maps & mask) == mask)
80 seq_printf(m, "allowed_maps:\tany\n");
81 else
82 seq_printf(m, "allowed_maps:\t0x%llx\n", token->allowed_maps);
83
84 BUILD_BUG_ON(__MAX_BPF_PROG_TYPE >= 64);
85 mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1;
86 if ((token->allowed_progs & mask) == mask)
87 seq_printf(m, "allowed_progs:\tany\n");
88 else
89 seq_printf(m, "allowed_progs:\t0x%llx\n", token->allowed_progs);
90
91 BUILD_BUG_ON(__MAX_BPF_ATTACH_TYPE >= 64);
92 mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1;
93 if ((token->allowed_attachs & mask) == mask)
94 seq_printf(m, "allowed_attachs:\tany\n");
95 else
96 seq_printf(m, "allowed_attachs:\t0x%llx\n", token->allowed_attachs);
97}
98
99#define BPF_TOKEN_INODE_NAME "bpf-token"
100
101static const struct inode_operations bpf_token_iops = { };
102
103static const struct file_operations bpf_token_fops = {
104 .release = bpf_token_release,
105 .show_fdinfo = bpf_token_show_fdinfo,
106};
107
108int bpf_token_create(union bpf_attr *attr)
109{
110 struct bpf_mount_opts *mnt_opts;
111 struct bpf_token *token = NULL;
112 struct user_namespace *userns;
113 struct inode *inode;
114 struct file *file;
115 struct path path;
116 struct fd f;
117 umode_t mode;
118 int err, fd;
119
120 f = fdget(attr->token_create.bpffs_fd);
121 if (!f.file)
122 return -EBADF;
123
124 path = f.file->f_path;
125 path_get(&path);
126 fdput(f);
127
128 if (path.dentry != path.mnt->mnt_sb->s_root) {
129 err = -EINVAL;
130 goto out_path;
131 }
132 if (path.mnt->mnt_sb->s_op != &bpf_super_ops) {
133 err = -EINVAL;
134 goto out_path;
135 }
136 err = path_permission(&path, MAY_ACCESS);
137 if (err)
138 goto out_path;
139
140 userns = path.dentry->d_sb->s_user_ns;
141 /*
142 * Enforce that creators of BPF tokens are in the same user
143 * namespace as the BPF FS instance. This makes reasoning about
144 * permissions a lot easier and we can always relax this later.
145 */
146 if (current_user_ns() != userns) {
147 err = -EPERM;
148 goto out_path;
149 }
150 if (!ns_capable(userns, CAP_BPF)) {
151 err = -EPERM;
152 goto out_path;
153 }
154
155 mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
156 inode = bpf_get_inode(path.mnt->mnt_sb, NULL, mode);
157 if (IS_ERR(inode)) {
158 err = PTR_ERR(inode);
159 goto out_path;
160 }
161
162 inode->i_op = &bpf_token_iops;
163 inode->i_fop = &bpf_token_fops;
164 clear_nlink(inode); /* make sure it is unlinked */
165
166 file = alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME, O_RDWR, &bpf_token_fops);
167 if (IS_ERR(file)) {
168 iput(inode);
169 err = PTR_ERR(file);
170 goto out_path;
171 }
172
173 token = kvzalloc(sizeof(*token), GFP_USER);
174 if (!token) {
175 err = -ENOMEM;
176 goto out_file;
177 }
178
179 atomic64_set(&token->refcnt, 1);
180
181 /* remember bpffs owning userns for future ns_capable() checks */
182 token->userns = get_user_ns(userns);
183
184 mnt_opts = path.dentry->d_sb->s_fs_info;
185 token->allowed_cmds = mnt_opts->delegate_cmds;
186 token->allowed_maps = mnt_opts->delegate_maps;
187 token->allowed_progs = mnt_opts->delegate_progs;
188 token->allowed_attachs = mnt_opts->delegate_attachs;
189
190 err = security_bpf_token_create(token, attr, &path);
191 if (err)
192 goto out_token;
193
194 fd = get_unused_fd_flags(O_CLOEXEC);
195 if (fd < 0) {
196 err = fd;
197 goto out_token;
198 }
199
200 file->private_data = token;
201 fd_install(fd, file);
202
203 path_put(&path);
204 return fd;
205
206out_token:
207 bpf_token_free(token);
208out_file:
209 fput(file);
210out_path:
211 path_put(&path);
212 return err;
213}
214
215struct bpf_token *bpf_token_get_from_fd(u32 ufd)
216{
217 struct fd f = fdget(ufd);
218 struct bpf_token *token;
219
220 if (!f.file)
221 return ERR_PTR(-EBADF);
222 if (f.file->f_op != &bpf_token_fops) {
223 fdput(f);
224 return ERR_PTR(-EINVAL);
225 }
226
227 token = f.file->private_data;
228 bpf_token_inc(token);
229 fdput(f);
230
231 return token;
232}
233
234bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
235{
236 /* BPF token can be used only within exactly the same userns in which
237 * it was created
238 */
239 if (!token || current_user_ns() != token->userns)
240 return false;
241 if (!(token->allowed_cmds & (1ULL << cmd)))
242 return false;
243 return security_bpf_token_cmd(token, cmd) == 0;
244}
245
246bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type)
247{
248 if (!token || type >= __MAX_BPF_MAP_TYPE)
249 return false;
250
251 return token->allowed_maps & (1ULL << type);
252}
253
254bool bpf_token_allow_prog_type(const struct bpf_token *token,
255 enum bpf_prog_type prog_type,
256 enum bpf_attach_type attach_type)
257{
258 if (!token || prog_type >= __MAX_BPF_PROG_TYPE || attach_type >= __MAX_BPF_ATTACH_TYPE)
259 return false;
260
261 return (token->allowed_progs & (1ULL << prog_type)) &&
262 (token->allowed_attachs & (1ULL << attach_type));
263}
kernel/bpf/verifier.c+6−7
@@ -20597,7 +20597,12 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 · 20596 unmodified lines
20597 env->prog = *prog;20597 env->prog = *prog;
20598 env->ops = bpf_verifier_ops[env->prog->type];20598 env->ops = bpf_verifier_ops[env->prog->type];
20599 env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel);20599 env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel);
20600 is_priv = bpf_capable();20600
20601 env->allow_ptr_leaks = bpf_allow_ptr_leaks(env->prog->aux->token);
20602 env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token);
20603 env->bypass_spec_v1 = bpf_bypass_spec_v1(env->prog->aux->token);
20604 env->bypass_spec_v4 = bpf_bypass_spec_v4(env->prog->aux->token);
20605 env->bpf_capable = is_priv = bpf_token_capable(env->prog->aux->token, CAP_BPF);
20601 20606
20602 bpf_get_btf_vmlinux();20607 bpf_get_btf_vmlinux();
20603 20608
@@ -20629,12 +20634,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 · 25 unmodified lines
20629 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)20634 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
20630 env->strict_alignment = false;20635 env->strict_alignment = false;
20631 20636
20632 env->allow_ptr_leaks = bpf_allow_ptr_leaks();
20633 env->allow_uninit_stack = bpf_allow_uninit_stack();
20634 env->bypass_spec_v1 = bpf_bypass_spec_v1();
20635 env->bypass_spec_v4 = bpf_bypass_spec_v4();
20636 env->bpf_capable = bpf_capable();
20637
20638 if (is_priv)20637 if (is_priv)
20639 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;20638 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
20640 env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;20639 env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;
kernel/trace/bpf_trace.c+1−1
@@ -1626,7 +1626,7 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 1625 unmodified lines
1626 case BPF_FUNC_trace_vprintk:1626 case BPF_FUNC_trace_vprintk:
1627 return bpf_get_trace_vprintk_proto();1627 return bpf_get_trace_vprintk_proto();
1628 default:1628 default:
1629 return bpf_base_func_proto(func_id);1629 return bpf_base_func_proto(func_id, prog);
1630 }1630 }
1631}1631}
1632 1632
net/core/filter.c+18−18
@@ -87,7 +87,7 @@ · 86 unmodified lines
87#include "dev.h"87#include "dev.h"
88 88
89static const struct bpf_func_proto *89static const struct bpf_func_proto *
90bpf_sk_base_func_proto(enum bpf_func_id func_id);90bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
91 91
92int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)92int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
93{93{
@@ -7841,7 +7841,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 7747 unmodified lines
7841 case BPF_FUNC_ktime_get_coarse_ns:7841 case BPF_FUNC_ktime_get_coarse_ns:
7842 return &bpf_ktime_get_coarse_ns_proto;7842 return &bpf_ktime_get_coarse_ns_proto;
7843 default:7843 default:
7844 return bpf_base_func_proto(func_id);7844 return bpf_base_func_proto(func_id, prog);
7845 }7845 }
7846}7846}
7847 7847
@@ -7934,7 +7934,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 86 unmodified lines
7934 return NULL;7934 return NULL;
7935 }7935 }
7936 default:7936 default:
7937 return bpf_sk_base_func_proto(func_id);7937 return bpf_sk_base_func_proto(func_id, prog);
7938 }7938 }
7939}7939}
7940 7940
@@ -7953,7 +7953,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 12 unmodified lines
7953 case BPF_FUNC_perf_event_output:7953 case BPF_FUNC_perf_event_output:
7954 return &bpf_skb_event_output_proto;7954 return &bpf_skb_event_output_proto;
7955 default:7955 default:
7956 return bpf_sk_base_func_proto(func_id);7956 return bpf_sk_base_func_proto(func_id, prog);
7957 }7957 }
7958}7958}
7959 7959
@@ -8140,7 +8140,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 180 unmodified lines
8140#endif8140#endif
8141#endif8141#endif
8142 default:8142 default:
8143 return bpf_sk_base_func_proto(func_id);8143 return bpf_sk_base_func_proto(func_id, prog);
8144 }8144 }
8145}8145}
8146 8146
@@ -8199,7 +8199,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 52 unmodified lines
8199#endif8199#endif
8200#endif8200#endif
8201 default:8201 default:
8202 return bpf_sk_base_func_proto(func_id);8202 return bpf_sk_base_func_proto(func_id, prog);
8203 }8203 }
8204 8204
8205#if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)8205#if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)
@@ -8260,7 +8260,7 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 54 unmodified lines
8260 return &bpf_tcp_sock_proto;8260 return &bpf_tcp_sock_proto;
8261#endif /* CONFIG_INET */8261#endif /* CONFIG_INET */
8262 default:8262 default:
8263 return bpf_sk_base_func_proto(func_id);8263 return bpf_sk_base_func_proto(func_id, prog);
8264 }8264 }
8265}8265}
8266 8266
@@ -8302,7 +8302,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 35 unmodified lines
8302 return &bpf_get_cgroup_classid_curr_proto;8302 return &bpf_get_cgroup_classid_curr_proto;
8303#endif8303#endif
8304 default:8304 default:
8305 return bpf_sk_base_func_proto(func_id);8305 return bpf_sk_base_func_proto(func_id, prog);
8306 }8306 }
8307}8307}
8308 8308
@@ -8346,7 +8346,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 37 unmodified lines
8346 return &bpf_skc_lookup_tcp_proto;8346 return &bpf_skc_lookup_tcp_proto;
8347#endif8347#endif
8348 default:8348 default:
8349 return bpf_sk_base_func_proto(func_id);8349 return bpf_sk_base_func_proto(func_id, prog);
8350 }8350 }
8351}8351}
8352 8352
@@ -8357,7 +8357,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 4 unmodified lines
8357 case BPF_FUNC_skb_load_bytes:8357 case BPF_FUNC_skb_load_bytes:
8358 return &bpf_flow_dissector_load_bytes_proto;8358 return &bpf_flow_dissector_load_bytes_proto;
8359 default:8359 default:
8360 return bpf_sk_base_func_proto(func_id);8360 return bpf_sk_base_func_proto(func_id, prog);
8361 }8361 }
8362}8362}
8363 8363
@@ -8384,7 +8384,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 20 unmodified lines
8384 case BPF_FUNC_skb_under_cgroup:8384 case BPF_FUNC_skb_under_cgroup:
8385 return &bpf_skb_under_cgroup_proto;8385 return &bpf_skb_under_cgroup_proto;
8386 default:8386 default:
8387 return bpf_sk_base_func_proto(func_id);8387 return bpf_sk_base_func_proto(func_id, prog);
8388 }8388 }
8389}8389}
8390 8390
@@ -8559,7 +8559,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 168 unmodified lines
8559 return false;8559 return false;
8560 case bpf_ctx_range(struct __sk_buff, data):8560 case bpf_ctx_range(struct __sk_buff, data):
8561 case bpf_ctx_range(struct __sk_buff, data_end):8561 case bpf_ctx_range(struct __sk_buff, data_end):
8562 if (!bpf_capable())8562 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8563 return false;8563 return false;
8564 break;8564 break;
8565 }8565 }
@@ -8571,7 +8571,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 5 unmodified lines
8571 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):8571 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8572 break;8572 break;
8573 case bpf_ctx_range(struct __sk_buff, tstamp):8573 case bpf_ctx_range(struct __sk_buff, tstamp):
8574 if (!bpf_capable())8574 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8575 return false;8575 return false;
8576 break;8576 break;
8577 default:8577 default:
@@ -11215,7 +11215,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id, · 2637 unmodified lines
11215 case BPF_FUNC_ktime_get_coarse_ns:11215 case BPF_FUNC_ktime_get_coarse_ns:
11216 return &bpf_ktime_get_coarse_ns_proto;11216 return &bpf_ktime_get_coarse_ns_proto;
11217 default:11217 default:
11218 return bpf_base_func_proto(func_id);11218 return bpf_base_func_proto(func_id, prog);
11219 }11219 }
11220}11220}
11221 11221
@@ -11397,7 +11397,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 175 unmodified lines
11397 case BPF_FUNC_sk_release:11397 case BPF_FUNC_sk_release:
11398 return &bpf_sk_release_proto;11398 return &bpf_sk_release_proto;
11399 default:11399 default:
11400 return bpf_sk_base_func_proto(func_id);11400 return bpf_sk_base_func_proto(func_id, prog);
11401 }11401 }
11402}11402}
11403 11403
@@ -11731,7 +11731,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = { · 327 unmodified lines
11731};11731};
11732 11732
11733static const struct bpf_func_proto *11733static const struct bpf_func_proto *
11734bpf_sk_base_func_proto(enum bpf_func_id func_id)11734bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11735{11735{
11736 const struct bpf_func_proto *func;11736 const struct bpf_func_proto *func;
11737 11737
@@ -11760,10 +11760,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id) · 22 unmodified lines
11760 case BPF_FUNC_ktime_get_coarse_ns:11760 case BPF_FUNC_ktime_get_coarse_ns:
11761 return &bpf_ktime_get_coarse_ns_proto;11761 return &bpf_ktime_get_coarse_ns_proto;
11762 default:11762 default:
11763 return bpf_base_func_proto(func_id);11763 return bpf_base_func_proto(func_id, prog);
11764 }11764 }
11765 11765
11766 if (!perfmon_capable())11766 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
11767 return NULL;11767 return NULL;
11768 11768
11769 return func;11769 return func;
net/ipv4/bpf_tcp_ca.c+1−1
@@ -191,7 +191,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id, · 190 unmodified lines
191 case BPF_FUNC_ktime_get_coarse_ns:191 case BPF_FUNC_ktime_get_coarse_ns:
192 return &bpf_ktime_get_coarse_ns_proto;192 return &bpf_ktime_get_coarse_ns_proto;
193 default:193 default:
194 return bpf_base_func_proto(func_id);194 return bpf_base_func_proto(func_id, prog);
195 }195 }
196}196}
197 197
net/netfilter/nf_bpf_link.c+1−1
@@ -314,7 +314,7 @@ static bool nf_is_valid_access(int off, int size, enum bpf_access_type type, · 313 unmodified lines
314static const struct bpf_func_proto *314static const struct bpf_func_proto *
315bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)315bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
316{316{
317 return bpf_base_func_proto(func_id);317 return bpf_base_func_proto(func_id, prog);
318}318}
319 319
320const struct bpf_verifier_ops netfilter_verifier_ops = {320const struct bpf_verifier_ops netfilter_verifier_ops = {
security/security.c+85−16
@@ -5167,29 +5167,87 @@ int security_bpf_prog(struct bpf_prog *prog) · 5166 unmodified lines
5167}5167}
5168 5168
5169/**5169/**
5170 * security_bpf_map_alloc() - Allocate a bpf map LSM blob5170 * security_bpf_map_create() - Check if BPF map creation is allowed
5171 * @map: bpf map5171 * @map: BPF map object
5172 * @attr: BPF syscall attributes used to create BPF map
5173 * @token: BPF token used to grant user access
5174 *
5175 * Do a check when the kernel creates a new BPF map. This is also the
5176 * point where LSM blob is allocated for LSMs that need them.
5177 *
5178 * Return: Returns 0 on success, error on failure.
5179 */
5180int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
5181 struct bpf_token *token)
5182{
5183 return call_int_hook(bpf_map_create, 0, map, attr, token);
5184}
5185
5186/**
5187 * security_bpf_prog_load() - Check if loading of BPF program is allowed
5188 * @prog: BPF program object
5189 * @attr: BPF syscall attributes used to create BPF program
5190 * @token: BPF token used to grant user access to BPF subsystem
5172 *5191 *
5173 * Initialize the security field inside bpf map.5192 * Perform an access control check when the kernel loads a BPF program and
5193 * allocates associated BPF program object. This hook is also responsible for
5194 * allocating any required LSM state for the BPF program.
5174 *5195 *
5175 * Return: Returns 0 on success, error on failure.5196 * Return: Returns 0 on success, error on failure.
5176 */5197 */
5177int security_bpf_map_alloc(struct bpf_map *map)5198int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5199 struct bpf_token *token)
5178{5200{
5179 return call_int_hook(bpf_map_alloc_security, 0, map);5201 return call_int_hook(bpf_prog_load, 0, prog, attr, token);
5180}5202}
5181 5203
5182/**5204/**
5183 * security_bpf_prog_alloc() - Allocate a bpf program LSM blob5205 * security_bpf_token_create() - Check if creating of BPF token is allowed
5184 * @aux: bpf program aux info struct5206 * @token: BPF token object
5207 * @attr: BPF syscall attributes used to create BPF token
5208 * @path: path pointing to BPF FS mount point from which BPF token is created
5185 *5209 *
5186 * Initialize the security field inside bpf program.5210 * Do a check when the kernel instantiates a new BPF token object from BPF FS
5211 * instance. This is also the point where LSM blob can be allocated for LSMs.
5187 *5212 *
5188 * Return: Returns 0 on success, error on failure.5213 * Return: Returns 0 on success, error on failure.
5189 */5214 */
5190int security_bpf_prog_alloc(struct bpf_prog_aux *aux)5215int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
5216 struct path *path)
5191{5217{
5192 return call_int_hook(bpf_prog_alloc_security, 0, aux);5218 return call_int_hook(bpf_token_create, 0, token, attr, path);
5219}
5220
5221/**
5222 * security_bpf_token_cmd() - Check if BPF token is allowed to delegate
5223 * requested BPF syscall command
5224 * @token: BPF token object
5225 * @cmd: BPF syscall command requested to be delegated by BPF token
5226 *
5227 * Do a check when the kernel decides whether provided BPF token should allow
5228 * delegation of requested BPF syscall command.
5229 *
5230 * Return: Returns 0 on success, error on failure.
5231 */
5232int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
5233{
5234 return call_int_hook(bpf_token_cmd, 0, token, cmd);
5235}
5236
5237/**
5238 * security_bpf_token_capable() - Check if BPF token is allowed to delegate
5239 * requested BPF-related capability
5240 * @token: BPF token object
5241 * @cap: capabilities requested to be delegated by BPF token
5242 *
5243 * Do a check when the kernel decides whether provided BPF token should allow
5244 * delegation of requested BPF-related capabilities.
5245 *
5246 * Return: Returns 0 on success, error on failure.
5247 */
5248int security_bpf_token_capable(const struct bpf_token *token, int cap)
5249{
5250 return call_int_hook(bpf_token_capable, 0, token, cap);
5193}5251}
5194 5252
5195/**5253/**
@@ -5200,18 +5258,29 @@ int security_bpf_prog_alloc(struct bpf_prog_aux *aux) · 4 unmodified lines
5200 */5258 */
5201void security_bpf_map_free(struct bpf_map *map)5259void security_bpf_map_free(struct bpf_map *map)
5202{5260{
5203 call_void_hook(bpf_map_free_security, map);5261 call_void_hook(bpf_map_free, map);
5262}
5263
5264/**
5265 * security_bpf_prog_free() - Free a BPF program's LSM blob
5266 * @prog: BPF program struct
5267 *
5268 * Clean up the security information stored inside BPF program.
5269 */
5270void security_bpf_prog_free(struct bpf_prog *prog)
5271{
5272 call_void_hook(bpf_prog_free, prog);
5204}5273}
5205 5274
5206/**5275/**
5207 * security_bpf_prog_free() - Free a bpf program's LSM blob5276 * security_bpf_token_free() - Free a BPF token's LSM blob
5208 * @aux: bpf program aux info struct5277 * @token: BPF token struct
5209 *5278 *
5210 * Clean up the security information stored inside bpf prog.5279 * Clean up the security information stored inside BPF token.
5211 */5280 */
5212void security_bpf_prog_free(struct bpf_prog_aux *aux)5281void security_bpf_token_free(struct bpf_token *token)
5213{5282{
5214 call_void_hook(bpf_prog_free_security, aux);5283 call_void_hook(bpf_token_free, token);
5215}5284}
5216#endif /* CONFIG_BPF_SYSCALL */5285#endif /* CONFIG_BPF_SYSCALL */
5217 5286
security/selinux/hooks.c+37−10
@@ -6783,7 +6783,8 @@ static int selinux_bpf_prog(struct bpf_prog *prog) · 6782 unmodified lines
6783 BPF__PROG_RUN, NULL);6783 BPF__PROG_RUN, NULL);
6784}6784}
6785 6785
6786static int selinux_bpf_map_alloc(struct bpf_map *map)6786static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
6787 struct bpf_token *token)
6787{6788{
6788 struct bpf_security_struct *bpfsec;6789 struct bpf_security_struct *bpfsec;
6789 6790
@@ -6805,7 +6806,8 @@ static void selinux_bpf_map_free(struct bpf_map *map) · 15 unmodified lines
6805 kfree(bpfsec);6806 kfree(bpfsec);
6806}6807}
6807 6808
6808static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)6809static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
6810 struct bpf_token *token)
6809{6811{
6810 struct bpf_security_struct *bpfsec;6812 struct bpf_security_struct *bpfsec;
6811 6813
@@ -6814,16 +6816,39 @@ static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) · 2 unmodified lines
6814 return -ENOMEM;6816 return -ENOMEM;
6815 6817
6816 bpfsec->sid = current_sid();6818 bpfsec->sid = current_sid();
6817 aux->security = bpfsec;6819 prog->aux->security = bpfsec;
6818 6820
6819 return 0;6821 return 0;
6820}6822}
6821 6823
6822static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)6824static void selinux_bpf_prog_free(struct bpf_prog *prog)
6823{6825{
6824 struct bpf_security_struct *bpfsec = aux->security;6826 struct bpf_security_struct *bpfsec = prog->aux->security;
6825 6827
6826 aux->security = NULL;6828 prog->aux->security = NULL;
6829 kfree(bpfsec);
6830}
6831
6832static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
6833 struct path *path)
6834{
6835 struct bpf_security_struct *bpfsec;
6836
6837 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6838 if (!bpfsec)
6839 return -ENOMEM;
6840
6841 bpfsec->sid = current_sid();
6842 token->security = bpfsec;
6843
6844 return 0;
6845}
6846
6847static void selinux_bpf_token_free(struct bpf_token *token)
6848{
6849 struct bpf_security_struct *bpfsec = token->security;
6850
6851 token->security = NULL;
6827 kfree(bpfsec);6852 kfree(bpfsec);
6828}6853}
6829#endif6854#endif
@@ -7179,8 +7204,9 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { · 349 unmodified lines
7179 LSM_HOOK_INIT(bpf, selinux_bpf),7204 LSM_HOOK_INIT(bpf, selinux_bpf),
7180 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),7205 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7181 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),7206 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7182 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),7207 LSM_HOOK_INIT(bpf_map_free, selinux_bpf_map_free),
7183 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),7208 LSM_HOOK_INIT(bpf_prog_free, selinux_bpf_prog_free),
7209 LSM_HOOK_INIT(bpf_token_free, selinux_bpf_token_free),
7184#endif7210#endif
7185 7211
7186#ifdef CONFIG_PERF_EVENTS7212#ifdef CONFIG_PERF_EVENTS
@@ -7237,8 +7263,9 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { · 50 unmodified lines
7237 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),7263 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7238#endif7264#endif
7239#ifdef CONFIG_BPF_SYSCALL7265#ifdef CONFIG_BPF_SYSCALL
7240 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),7266 LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
7241 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),7267 LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
7268 LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
7242#endif7269#endif
7243#ifdef CONFIG_PERF_EVENTS7270#ifdef CONFIG_PERF_EVENTS
7244 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),7271 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
tools/include/uapi/linux/bpf.h+42−0
@@ -847,6 +847,36 @@ union bpf_iter_link_info { · 846 unmodified lines
847 * Returns zero on success. On error, -1 is returned and *errno*847 * Returns zero on success. On error, -1 is returned and *errno*
848 * is set appropriately.848 * is set appropriately.
849 *849 *
850 * BPF_TOKEN_CREATE
851 * Description
852 * Create BPF token with embedded information about what
853 * BPF-related functionality it allows:
854 * - a set of allowed bpf() syscall commands;
855 * - a set of allowed BPF map types to be created with
856 * BPF_MAP_CREATE command, if BPF_MAP_CREATE itself is allowed;
857 * - a set of allowed BPF program types and BPF program attach
858 * types to be loaded with BPF_PROG_LOAD command, if
859 * BPF_PROG_LOAD itself is allowed.
860 *
861 * BPF token is created (derived) from an instance of BPF FS,
862 * assuming it has necessary delegation mount options specified.
863 * This BPF token can be passed as an extra parameter to various
864 * bpf() syscall commands to grant BPF subsystem functionality to
865 * unprivileged processes.
866 *
867 * When created, BPF token is "associated" with the owning
868 * user namespace of BPF FS instance (super block) that it was
869 * derived from, and subsequent BPF operations performed with
870 * BPF token would be performing capabilities checks (i.e.,
871 * CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN) within
872 * that user namespace. Without BPF token, such capabilities
873 * have to be granted in init user namespace, making bpf()
874 * syscall incompatible with user namespace, for the most part.
875 *
876 * Return
877 * A new file descriptor (a nonnegative integer), or -1 if an
878 * error occurred (in which case, *errno* is set appropriately).
879 *
850 * NOTES880 * NOTES
851 * eBPF objects (maps and programs) can be shared between processes.881 * eBPF objects (maps and programs) can be shared between processes.
852 *882 *
@@ -901,6 +931,8 @@ enum bpf_cmd { · 48 unmodified lines
901 BPF_ITER_CREATE,931 BPF_ITER_CREATE,
902 BPF_LINK_DETACH,932 BPF_LINK_DETACH,
903 BPF_PROG_BIND_MAP,933 BPF_PROG_BIND_MAP,
934 BPF_TOKEN_CREATE,
935 __MAX_BPF_CMD,
904};936};
905 937
906enum bpf_map_type {938enum bpf_map_type {
@@ -951,6 +983,7 @@ enum bpf_map_type { · 44 unmodified lines
951 BPF_MAP_TYPE_BLOOM_FILTER,983 BPF_MAP_TYPE_BLOOM_FILTER,
952 BPF_MAP_TYPE_USER_RINGBUF,984 BPF_MAP_TYPE_USER_RINGBUF,
953 BPF_MAP_TYPE_CGRP_STORAGE,985 BPF_MAP_TYPE_CGRP_STORAGE,
986 __MAX_BPF_MAP_TYPE
954};987};
955 988
956/* Note that tracing related programs such as989/* Note that tracing related programs such as
@@ -995,6 +1028,7 @@ enum bpf_prog_type { · 38 unmodified lines
995 BPF_PROG_TYPE_SK_LOOKUP,1028 BPF_PROG_TYPE_SK_LOOKUP,
996 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */1029 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
997 BPF_PROG_TYPE_NETFILTER,1030 BPF_PROG_TYPE_NETFILTER,
1031 __MAX_BPF_PROG_TYPE
998};1032};
999 1033
1000enum bpf_attach_type {1034enum bpf_attach_type {
@@ -1401,6 +1435,7 @@ union bpf_attr { · 400 unmodified lines
1401 * to using 5 hash functions).1435 * to using 5 hash functions).
1402 */1436 */
1403 __u64 map_extra;1437 __u64 map_extra;
1438 __u32 map_token_fd;
1404 };1439 };
1405 1440
1406 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */1441 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1470,6 +1505,7 @@ union bpf_attr { · 63 unmodified lines
1470 * truncated), or smaller (if log buffer wasn't filled completely).1505 * truncated), or smaller (if log buffer wasn't filled completely).
1471 */1506 */
1472 __u32 log_true_size;1507 __u32 log_true_size;
1508 __u32 prog_token_fd;
1473 };1509 };
1474 1510
1475 struct { /* anonymous struct used by BPF_OBJ_* commands */1511 struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -1582,6 +1618,7 @@ union bpf_attr { · 106 unmodified lines
1582 * truncated), or smaller (if log buffer wasn't filled completely).1618 * truncated), or smaller (if log buffer wasn't filled completely).
1583 */1619 */
1584 __u32 btf_log_true_size;1620 __u32 btf_log_true_size;
1621 __u32 btf_token_fd;
1585 };1622 };
1586 1623
1587 struct {1624 struct {
@@ -1712,6 +1749,11 @@ union bpf_attr { · 124 unmodified lines
1712 __u32 flags; /* extra flags */1749 __u32 flags; /* extra flags */
1713 } prog_bind_map;1750 } prog_bind_map;
1714 1751
1752 struct { /* struct used by BPF_TOKEN_CREATE command */
1753 __u32 flags;
1754 __u32 bpffs_fd;
1755 } token_create;
1756
1715} __attribute__((aligned(8)));1757} __attribute__((aligned(8)));
1716 1758
1717/* The description below is an attempt at providing documentation to eBPF1759/* The description below is an attempt at providing documentation to eBPF
tools/lib/bpf/bpf.c+25−3
@@ -169,7 +169,7 @@ int bpf_map_create(enum bpf_map_type map_type, · 168 unmodified lines
169 __u32 max_entries,169 __u32 max_entries,
170 const struct bpf_map_create_opts *opts)170 const struct bpf_map_create_opts *opts)
171{171{
172 const size_t attr_sz = offsetofend(union bpf_attr, map_extra);172 const size_t attr_sz = offsetofend(union bpf_attr, map_token_fd);
173 union bpf_attr attr;173 union bpf_attr attr;
174 int fd;174 int fd;
175 175
@@ -198,6 +198,8 @@ int bpf_map_create(enum bpf_map_type map_type, · 22 unmodified lines
198 attr.numa_node = OPTS_GET(opts, numa_node, 0);198 attr.numa_node = OPTS_GET(opts, numa_node, 0);
199 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);199 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
200 200
201 attr.map_token_fd = OPTS_GET(opts, token_fd, 0);
202
201 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);203 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
202 return libbpf_err_errno(fd);204 return libbpf_err_errno(fd);
203}205}
@@ -232,7 +234,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, · 28 unmodified lines
232 const struct bpf_insn *insns, size_t insn_cnt,234 const struct bpf_insn *insns, size_t insn_cnt,
233 struct bpf_prog_load_opts *opts)235 struct bpf_prog_load_opts *opts)
234{236{
235 const size_t attr_sz = offsetofend(union bpf_attr, log_true_size);237 const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd);
236 void *finfo = NULL, *linfo = NULL;238 void *finfo = NULL, *linfo = NULL;
237 const char *func_info, *line_info;239 const char *func_info, *line_info;
238 __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;240 __u32 log_size, log_level, attach_prog_fd, attach_btf_obj_fd;
@@ -261,6 +263,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, · 22 unmodified lines
261 attr.prog_flags = OPTS_GET(opts, prog_flags, 0);263 attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
262 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);264 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
263 attr.kern_version = OPTS_GET(opts, kern_version, 0);265 attr.kern_version = OPTS_GET(opts, kern_version, 0);
266 attr.prog_token_fd = OPTS_GET(opts, token_fd, 0);
264 267
265 if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))268 if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
266 libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));269 libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
@@ -1182,7 +1185,7 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd) · 915 unmodified lines
1182 1185
1183int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)1186int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
1184{1187{
1185 const size_t attr_sz = offsetofend(union bpf_attr, btf_log_true_size);1188 const size_t attr_sz = offsetofend(union bpf_attr, btf_token_fd);
1186 union bpf_attr attr;1189 union bpf_attr attr;
1187 char *log_buf;1190 char *log_buf;
1188 size_t log_size;1191 size_t log_size;
@@ -1207,6 +1210,8 @@ int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts · 18 unmodified lines
1207 1210
1208 attr.btf = ptr_to_u64(btf_data);1211 attr.btf = ptr_to_u64(btf_data);
1209 attr.btf_size = btf_size;1212 attr.btf_size = btf_size;
1213 attr.btf_token_fd = OPTS_GET(opts, token_fd, 0);
1214
1210 /* log_level == 0 and log_buf != NULL means "try loading without1215 /* log_level == 0 and log_buf != NULL means "try loading without
1211 * log_buf, but retry with log_buf and log_level=1 on error", which is1216 * log_buf, but retry with log_buf and log_level=1 on error", which is
1212 * consistent across low-level and high-level BTF and program loading1217 * consistent across low-level and high-level BTF and program loading
@@ -1287,3 +1292,20 @@ int bpf_prog_bind_map(int prog_fd, int map_fd, · 74 unmodified lines
1287 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);1292 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
1288 return libbpf_err_errno(ret);1293 return libbpf_err_errno(ret);
1289}1294}
1295
1296int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts)
1297{
1298 const size_t attr_sz = offsetofend(union bpf_attr, token_create);
1299 union bpf_attr attr;
1300 int fd;
1301
1302 if (!OPTS_VALID(opts, bpf_token_create_opts))
1303 return libbpf_err(-EINVAL);
1304
1305 memset(&attr, 0, attr_sz);
1306 attr.token_create.bpffs_fd = bpffs_fd;
1307 attr.token_create.flags = OPTS_GET(opts, flags, 0);
1308
1309 fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz);
1310 return libbpf_err_errno(fd);
1311}
tools/lib/bpf/bpf.h+32−3
@@ -51,8 +51,11 @@ struct bpf_map_create_opts { · 50 unmodified lines
51 51
52 __u32 numa_node;52 __u32 numa_node;
53 __u32 map_ifindex;53 __u32 map_ifindex;
54
55 __u32 token_fd;
56 size_t :0;
54};57};
55#define bpf_map_create_opts__last_field map_ifindex58#define bpf_map_create_opts__last_field token_fd
56 59
57LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,60LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
58 const char *map_name,61 const char *map_name,
@@ -102,9 +105,10 @@ struct bpf_prog_load_opts { · 43 unmodified lines
102 * If kernel doesn't support this feature, log_size is left unchanged.105 * If kernel doesn't support this feature, log_size is left unchanged.
103 */106 */
104 __u32 log_true_size;107 __u32 log_true_size;
108 __u32 token_fd;
105 size_t :0;109 size_t :0;
106};110};
107#define bpf_prog_load_opts__last_field log_true_size111#define bpf_prog_load_opts__last_field token_fd
108 112
109LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,113LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
110 const char *prog_name, const char *license,114 const char *prog_name, const char *license,
@@ -130,9 +134,10 @@ struct bpf_btf_load_opts { · 19 unmodified lines
130 * If kernel doesn't support this feature, log_size is left unchanged.134 * If kernel doesn't support this feature, log_size is left unchanged.
131 */135 */
132 __u32 log_true_size;136 __u32 log_true_size;
137 __u32 token_fd;
133 size_t :0;138 size_t :0;
134};139};
135#define bpf_btf_load_opts__last_field log_true_size140#define bpf_btf_load_opts__last_field token_fd
136 141
137LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,142LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
138 struct bpf_btf_load_opts *opts);143 struct bpf_btf_load_opts *opts);
@@ -640,6 +645,30 @@ struct bpf_test_run_opts { · 501 unmodified lines
640LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,645LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
641 struct bpf_test_run_opts *opts);646 struct bpf_test_run_opts *opts);
642 647
648struct bpf_token_create_opts {
649 size_t sz; /* size of this struct for forward/backward compatibility */
650 __u32 flags;
651 size_t :0;
652};
653#define bpf_token_create_opts__last_field flags
654
655/**
656 * @brief **bpf_token_create()** creates a new instance of BPF token derived
657 * from specified BPF FS mount point.
658 *
659 * BPF token created with this API can be passed to bpf() syscall for
660 * commands like BPF_PROG_LOAD, BPF_MAP_CREATE, etc.
661 *
662 * @param bpffs_fd FD for BPF FS instance from which to derive a BPF token
663 * instance.
664 * @param opts optional BPF token creation options, can be NULL
665 *
666 * @return BPF token FD > 0, on success; negative error code, otherwise (errno
667 * is also set to the error code)
668 */
669LIBBPF_API int bpf_token_create(int bpffs_fd,
670 struct bpf_token_create_opts *opts);
671
643#ifdef __cplusplus672#ifdef __cplusplus
644} /* extern "C" */673} /* extern "C" */
645#endif674#endif
tools/lib/bpf/libbpf.map+1−0
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 { · 400 unmodified lines
401 bpf_program__attach_netkit;401 bpf_program__attach_netkit;
402 bpf_program__attach_tcx;402 bpf_program__attach_tcx;
403 bpf_program__attach_uprobe_multi;403 bpf_program__attach_uprobe_multi;
404 bpf_token_create;
404 ring__avail_data_size;405 ring__avail_data_size;
405 ring__consume;406 ring__consume;
406 ring__consumer_pos;407 ring__consumer_pos;
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c+4−0
@@ -30,6 +30,8 @@ void test_libbpf_probe_prog_types(void) · 29 unmodified lines
30 30
31 if (prog_type == BPF_PROG_TYPE_UNSPEC)31 if (prog_type == BPF_PROG_TYPE_UNSPEC)
32 continue;32 continue;
33 if (strcmp(prog_type_name, "__MAX_BPF_PROG_TYPE") == 0)
34 continue;
33 35
34 if (!test__start_subtest(prog_type_name))36 if (!test__start_subtest(prog_type_name))
35 continue;37 continue;
@@ -68,6 +70,8 @@ void test_libbpf_probe_map_types(void) · 32 unmodified lines
68 70
69 if (map_type == BPF_MAP_TYPE_UNSPEC)71 if (map_type == BPF_MAP_TYPE_UNSPEC)
70 continue;72 continue;
73 if (strcmp(map_type_name, "__MAX_BPF_MAP_TYPE") == 0)
74 continue;
71 75
72 if (!test__start_subtest(map_type_name))76 if (!test__start_subtest(map_type_name))
73 continue;77 continue;
tools/testing/selftests/bpf/prog_tests/libbpf_str.c+6−0
@@ -132,6 +132,9 @@ static void test_libbpf_bpf_map_type_str(void) · 131 unmodified lines
132 const char *map_type_str;132 const char *map_type_str;
133 char buf[256];133 char buf[256];
134 134
135 if (map_type == __MAX_BPF_MAP_TYPE)
136 continue;
137
135 map_type_name = btf__str_by_offset(btf, e->name_off);138 map_type_name = btf__str_by_offset(btf, e->name_off);
136 map_type_str = libbpf_bpf_map_type_str(map_type);139 map_type_str = libbpf_bpf_map_type_str(map_type);
137 ASSERT_OK_PTR(map_type_str, map_type_name);140 ASSERT_OK_PTR(map_type_str, map_type_name);
@@ -186,6 +189,9 @@ static void test_libbpf_bpf_prog_type_str(void) · 48 unmodified lines
186 const char *prog_type_str;189 const char *prog_type_str;
187 char buf[256];190 char buf[256];
188 191
192 if (prog_type == __MAX_BPF_PROG_TYPE)
193 continue;
194
189 prog_type_name = btf__str_by_offset(btf, e->name_off);195 prog_type_name = btf__str_by_offset(btf, e->name_off);
190 prog_type_str = libbpf_bpf_prog_type_str(prog_type);196 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
191 ASSERT_OK_PTR(prog_type_str, prog_type_name);197 ASSERT_OK_PTR(prog_type_str, prog_type_name);
tools/testing/selftests/bpf/prog_tests/token.c+672−0
@@ -0,0 +1,672 @@
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3#define _GNU_SOURCE
4#include <test_progs.h>
5#include <bpf/btf.h>
6#include "cap_helpers.h"
7#include <fcntl.h>
8#include <sched.h>
9#include <signal.h>
10#include <unistd.h>
11#include <linux/filter.h>
12#include <linux/unistd.h>
13#include <linux/mount.h>
14#include <sys/socket.h>
15#include <sys/syscall.h>
16#include <sys/un.h>
17
18static inline int sys_mount(const char *dev_name, const char *dir_name,
19 const char *type, unsigned long flags,
20 const void *data)
21{
22 return syscall(__NR_mount, dev_name, dir_name, type, flags, data);
23}
24
25static inline int sys_fsopen(const char *fsname, unsigned flags)
26{
27 return syscall(__NR_fsopen, fsname, flags);
28}
29
30static inline int sys_fspick(int dfd, const char *path, unsigned flags)
31{
32 return syscall(__NR_fspick, dfd, path, flags);
33}
34
35static inline int sys_fsconfig(int fs_fd, unsigned cmd, const char *key, const void *val, int aux)
36{
37 return syscall(__NR_fsconfig, fs_fd, cmd, key, val, aux);
38}
39
40static inline int sys_fsmount(int fs_fd, unsigned flags, unsigned ms_flags)
41{
42 return syscall(__NR_fsmount, fs_fd, flags, ms_flags);
43}
44
45static int drop_priv_caps(__u64 *old_caps)
46{
47 return cap_disable_effective((1ULL << CAP_BPF) |
48 (1ULL << CAP_PERFMON) |
49 (1ULL << CAP_NET_ADMIN) |
50 (1ULL << CAP_SYS_ADMIN), old_caps);
51}
52
53static int restore_priv_caps(__u64 old_caps)
54{
55 return cap_enable_effective(old_caps, NULL);
56}
57
58static int set_delegate_mask(int fs_fd, const char *key, __u64 mask)
59{
60 char buf[32];
61 int err;
62
63 snprintf(buf, sizeof(buf), "0x%llx", (unsigned long long)mask);
64 err = sys_fsconfig(fs_fd, FSCONFIG_SET_STRING, key,
65 mask == ~0ULL ? "any" : buf, 0);
66 if (err < 0)
67 err = -errno;
68 return err;
69}
70
71#define zclose(fd) do { if (fd >= 0) close(fd); fd = -1; } while (0)
72
73struct bpffs_opts {
74 __u64 cmds;
75 __u64 maps;
76 __u64 progs;
77 __u64 attachs;
78};
79
80static int create_bpffs_fd(void)
81{
82 int fs_fd;
83
84 /* create VFS context */
85 fs_fd = sys_fsopen("bpf", 0);
86 ASSERT_GE(fs_fd, 0, "fs_fd");
87
88 return fs_fd;
89}
90
91static int materialize_bpffs_fd(int fs_fd, struct bpffs_opts *opts)
92{
93 int mnt_fd, err;
94
95 /* set up token delegation mount options */
96 err = set_delegate_mask(fs_fd, "delegate_cmds", opts->cmds);
97 if (!ASSERT_OK(err, "fs_cfg_cmds"))
98 return err;
99 err = set_delegate_mask(fs_fd, "delegate_maps", opts->maps);
100 if (!ASSERT_OK(err, "fs_cfg_maps"))
101 return err;
102 err = set_delegate_mask(fs_fd, "delegate_progs", opts->progs);
103 if (!ASSERT_OK(err, "fs_cfg_progs"))
104 return err;
105 err = set_delegate_mask(fs_fd, "delegate_attachs", opts->attachs);
106 if (!ASSERT_OK(err, "fs_cfg_attachs"))
107 return err;
108
109 /* instantiate FS object */
110 err = sys_fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0);
111 if (err < 0)
112 return -errno;
113
114 /* create O_PATH fd for detached mount */
115 mnt_fd = sys_fsmount(fs_fd, 0, 0);
116 if (err < 0)
117 return -errno;
118
119 return mnt_fd;
120}
121
122/* send FD over Unix domain (AF_UNIX) socket */
123static int sendfd(int sockfd, int fd)
124{
125 struct msghdr msg = {};
126 struct cmsghdr *cmsg;
127 int fds[1] = { fd }, err;
128 char iobuf[1];
129 struct iovec io = {
130 .iov_base = iobuf,
131 .iov_len = sizeof(iobuf),
132 };
133 union {
134 char buf[CMSG_SPACE(sizeof(fds))];
135 struct cmsghdr align;
136 } u;
137
138 msg.msg_iov = &io;
139 msg.msg_iovlen = 1;
140 msg.msg_control = u.buf;
141 msg.msg_controllen = sizeof(u.buf);
142 cmsg = CMSG_FIRSTHDR(&msg);
143 cmsg->cmsg_level = SOL_SOCKET;
144 cmsg->cmsg_type = SCM_RIGHTS;
145 cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
146 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));
147
148 err = sendmsg(sockfd, &msg, 0);
149 if (err < 0)
150 err = -errno;
151 if (!ASSERT_EQ(err, 1, "sendmsg"))
152 return -EINVAL;
153
154 return 0;
155}
156
157/* receive FD over Unix domain (AF_UNIX) socket */
158static int recvfd(int sockfd, int *fd)
159{
160 struct msghdr msg = {};
161 struct cmsghdr *cmsg;
162 int fds[1], err;
163 char iobuf[1];
164 struct iovec io = {
165 .iov_base = iobuf,
166 .iov_len = sizeof(iobuf),
167 };
168 union {
169 char buf[CMSG_SPACE(sizeof(fds))];
170 struct cmsghdr align;
171 } u;
172
173 msg.msg_iov = &io;
174 msg.msg_iovlen = 1;
175 msg.msg_control = u.buf;
176 msg.msg_controllen = sizeof(u.buf);
177
178 err = recvmsg(sockfd, &msg, 0);
179 if (err < 0)
180 err = -errno;
181 if (!ASSERT_EQ(err, 1, "recvmsg"))
182 return -EINVAL;
183
184 cmsg = CMSG_FIRSTHDR(&msg);
185 if (!ASSERT_OK_PTR(cmsg, "cmsg_null") ||
186 !ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(sizeof(fds)), "cmsg_len") ||
187 !ASSERT_EQ(cmsg->cmsg_level, SOL_SOCKET, "cmsg_level") ||
188 !ASSERT_EQ(cmsg->cmsg_type, SCM_RIGHTS, "cmsg_type"))
189 return -EINVAL;
190
191 memcpy(fds, CMSG_DATA(cmsg), sizeof(fds));
192 *fd = fds[0];
193
194 return 0;
195}
196
197static ssize_t write_nointr(int fd, const void *buf, size_t count)
198{
199 ssize_t ret;
200
201 do {
202 ret = write(fd, buf, count);
203 } while (ret < 0 && errno == EINTR);
204
205 return ret;
206}
207
208static int write_file(const char *path, const void *buf, size_t count)
209{
210 int fd;
211 ssize_t ret;
212
213 fd = open(path, O_WRONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW);
214 if (fd < 0)
215 return -1;
216
217 ret = write_nointr(fd, buf, count);
218 close(fd);
219 if (ret < 0 || (size_t)ret != count)
220 return -1;
221
222 return 0;
223}
224
225static int create_and_enter_userns(void)
226{
227 uid_t uid;
228 gid_t gid;
229 char map[100];
230
231 uid = getuid();
232 gid = getgid();
233
234 if (unshare(CLONE_NEWUSER))
235 return -1;
236
237 if (write_file("/proc/self/setgroups", "deny", sizeof("deny") - 1) &&
238 errno != ENOENT)
239 return -1;
240
241 snprintf(map, sizeof(map), "0 %d 1", uid);
242 if (write_file("/proc/self/uid_map", map, strlen(map)))
243 return -1;
244
245
246 snprintf(map, sizeof(map), "0 %d 1", gid);
247 if (write_file("/proc/self/gid_map", map, strlen(map)))
248 return -1;
249
250 if (setgid(0))
251 return -1;
252
253 if (setuid(0))
254 return -1;
255
256 return 0;
257}
258
259typedef int (*child_callback_fn)(int);
260
261static void child(int sock_fd, struct bpffs_opts *opts, child_callback_fn callback)
262{
263 LIBBPF_OPTS(bpf_map_create_opts, map_opts);
264 int mnt_fd = -1, fs_fd = -1, err = 0, bpffs_fd = -1;
265
266 /* setup userns with root mappings */
267 err = create_and_enter_userns();
268 if (!ASSERT_OK(err, "create_and_enter_userns"))
269 goto cleanup;
270
271 /* setup mountns to allow creating BPF FS (fsopen("bpf")) from unpriv process */
272 err = unshare(CLONE_NEWNS);
273 if (!ASSERT_OK(err, "create_mountns"))
274 goto cleanup;
275
276 err = sys_mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0);
277 if (!ASSERT_OK(err, "remount_root"))
278 goto cleanup;
279
280 fs_fd = create_bpffs_fd();
281 if (!ASSERT_GE(fs_fd, 0, "create_bpffs_fd")) {
282 err = -EINVAL;
283 goto cleanup;
284 }
285
286 /* ensure unprivileged child cannot set delegation options */
287 err = set_delegate_mask(fs_fd, "delegate_cmds", 0x1);
288 ASSERT_EQ(err, -EPERM, "delegate_cmd_eperm");
289 err = set_delegate_mask(fs_fd, "delegate_maps", 0x1);
290 ASSERT_EQ(err, -EPERM, "delegate_maps_eperm");
291 err = set_delegate_mask(fs_fd, "delegate_progs", 0x1);
292 ASSERT_EQ(err, -EPERM, "delegate_progs_eperm");
293 err = set_delegate_mask(fs_fd, "delegate_attachs", 0x1);
294 ASSERT_EQ(err, -EPERM, "delegate_attachs_eperm");
295
296 /* pass BPF FS context object to parent */
297 err = sendfd(sock_fd, fs_fd);
298 if (!ASSERT_OK(err, "send_fs_fd"))
299 goto cleanup;
300 zclose(fs_fd);
301
302 /* avoid mucking around with mount namespaces and mounting at
303 * well-known path, just get detach-mounted BPF FS fd back from parent
304 */
305 err = recvfd(sock_fd, &mnt_fd);
306 if (!ASSERT_OK(err, "recv_mnt_fd"))
307 goto cleanup;
308
309 /* try to fspick() BPF FS and try to add some delegation options */
310 fs_fd = sys_fspick(mnt_fd, "", FSPICK_EMPTY_PATH);
311 if (!ASSERT_GE(fs_fd, 0, "bpffs_fspick")) {
312 err = -EINVAL;
313 goto cleanup;
314 }
315
316 /* ensure unprivileged child cannot reconfigure to set delegation options */
317 err = set_delegate_mask(fs_fd, "delegate_cmds", ~0ULL);
318 if (!ASSERT_EQ(err, -EPERM, "delegate_cmd_eperm_reconfig")) {
319 err = -EINVAL;
320 goto cleanup;
321 }
322 err = set_delegate_mask(fs_fd, "delegate_maps", ~0ULL);
323 if (!ASSERT_EQ(err, -EPERM, "delegate_maps_eperm_reconfig")) {
324 err = -EINVAL;
325 goto cleanup;
326 }
327 err = set_delegate_mask(fs_fd, "delegate_progs", ~0ULL);
328 if (!ASSERT_EQ(err, -EPERM, "delegate_progs_eperm_reconfig")) {
329 err = -EINVAL;
330 goto cleanup;
331 }
332 err = set_delegate_mask(fs_fd, "delegate_attachs", ~0ULL);
333 if (!ASSERT_EQ(err, -EPERM, "delegate_attachs_eperm_reconfig")) {
334 err = -EINVAL;
335 goto cleanup;
336 }
337 zclose(fs_fd);
338
339 bpffs_fd = openat(mnt_fd, ".", 0, O_RDWR);
340 if (!ASSERT_GE(bpffs_fd, 0, "bpffs_open")) {
341 err = -EINVAL;
342 goto cleanup;
343 }
344
345 /* do custom test logic with customly set up BPF FS instance */
346 err = callback(bpffs_fd);
347 if (!ASSERT_OK(err, "test_callback"))
348 goto cleanup;
349
350 err = 0;
351cleanup:
352 zclose(sock_fd);
353 zclose(mnt_fd);
354 zclose(fs_fd);
355 zclose(bpffs_fd);
356
357 exit(-err);
358}
359
360static int wait_for_pid(pid_t pid)
361{
362 int status, ret;
363
364again:
365 ret = waitpid(pid, &status, 0);
366 if (ret == -1) {
367 if (errno == EINTR)
368 goto again;
369
370 return -1;
371 }
372
373 if (!WIFEXITED(status))
374 return -1;
375
376 return WEXITSTATUS(status);
377}
378
379static void parent(int child_pid, struct bpffs_opts *bpffs_opts, int sock_fd)
380{
381 int fs_fd = -1, mnt_fd = -1, err;
382
383 err = recvfd(sock_fd, &fs_fd);
384 if (!ASSERT_OK(err, "recv_bpffs_fd"))
385 goto cleanup;
386
387 mnt_fd = materialize_bpffs_fd(fs_fd, bpffs_opts);
388 if (!ASSERT_GE(mnt_fd, 0, "materialize_bpffs_fd")) {
389 err = -EINVAL;
390 goto cleanup;
391 }
392 zclose(fs_fd);
393
394 /* pass BPF FS context object to parent */
395 err = sendfd(sock_fd, mnt_fd);
396 if (!ASSERT_OK(err, "send_mnt_fd"))
397 goto cleanup;
398 zclose(mnt_fd);
399
400 err = wait_for_pid(child_pid);
401 ASSERT_OK(err, "waitpid_child");
402
403cleanup:
404 zclose(sock_fd);
405 zclose(fs_fd);
406 zclose(mnt_fd);
407
408 if (child_pid > 0)
409 (void)kill(child_pid, SIGKILL);
410}
411
412static void subtest_userns(struct bpffs_opts *bpffs_opts, child_callback_fn cb)
413{
414 int sock_fds[2] = { -1, -1 };
415 int child_pid = 0, err;
416
417 err = socketpair(AF_UNIX, SOCK_STREAM, 0, sock_fds);
418 if (!ASSERT_OK(err, "socketpair"))
419 goto cleanup;
420
421 child_pid = fork();
422 if (!ASSERT_GE(child_pid, 0, "fork"))
423 goto cleanup;
424
425 if (child_pid == 0) {
426 zclose(sock_fds[0]);
427 return child(sock_fds[1], bpffs_opts, cb);
428
429 } else {
430 zclose(sock_fds[1]);
431 return parent(child_pid, bpffs_opts, sock_fds[0]);
432 }
433
434cleanup:
435 zclose(sock_fds[0]);
436 zclose(sock_fds[1]);
437 if (child_pid > 0)
438 (void)kill(child_pid, SIGKILL);
439}
440
441static int userns_map_create(int mnt_fd)
442{
443 LIBBPF_OPTS(bpf_map_create_opts, map_opts);
444 int err, token_fd = -1, map_fd = -1;
445 __u64 old_caps = 0;
446
447 /* create BPF token from BPF FS mount */
448 token_fd = bpf_token_create(mnt_fd, NULL);
449 if (!ASSERT_GT(token_fd, 0, "token_create")) {
450 err = -EINVAL;
451 goto cleanup;
452 }
453
454 /* while inside non-init userns, we need both a BPF token *and*
455 * CAP_BPF inside current userns to create privileged map; let's test
456 * that neither BPF token alone nor namespaced CAP_BPF is sufficient
457 */
458 err = drop_priv_caps(&old_caps);
459 if (!ASSERT_OK(err, "drop_caps"))
460 goto cleanup;
461
462 /* no token, no CAP_BPF -> fail */
463 map_opts.token_fd = 0;
464 map_fd = bpf_map_create(BPF_MAP_TYPE_STACK, "wo_token_wo_bpf", 0, 8, 1, &map_opts);
465 if (!ASSERT_LT(map_fd, 0, "stack_map_wo_token_wo_cap_bpf_should_fail")) {
466 err = -EINVAL;
467 goto cleanup;
468 }
469
470 /* token without CAP_BPF -> fail */
471 map_opts.token_fd = token_fd;
472 map_fd = bpf_map_create(BPF_MAP_TYPE_STACK, "w_token_wo_bpf", 0, 8, 1, &map_opts);
473 if (!ASSERT_LT(map_fd, 0, "stack_map_w_token_wo_cap_bpf_should_fail")) {
474 err = -EINVAL;
475 goto cleanup;
476 }
477
478 /* get back effective local CAP_BPF (and CAP_SYS_ADMIN) */
479 err = restore_priv_caps(old_caps);
480 if (!ASSERT_OK(err, "restore_caps"))
481 goto cleanup;
482
483 /* CAP_BPF without token -> fail */
484 map_opts.token_fd = 0;
485 map_fd = bpf_map_create(BPF_MAP_TYPE_STACK, "wo_token_w_bpf", 0, 8, 1, &map_opts);
486 if (!ASSERT_LT(map_fd, 0, "stack_map_wo_token_w_cap_bpf_should_fail")) {
487 err = -EINVAL;
488 goto cleanup;
489 }
490
491 /* finally, namespaced CAP_BPF + token -> success */
492 map_opts.token_fd = token_fd;
493 map_fd = bpf_map_create(BPF_MAP_TYPE_STACK, "w_token_w_bpf", 0, 8, 1, &map_opts);
494 if (!ASSERT_GT(map_fd, 0, "stack_map_w_token_w_cap_bpf")) {
495 err = -EINVAL;
496 goto cleanup;
497 }
498
499cleanup:
500 zclose(token_fd);
501 zclose(map_fd);
502 return err;
503}
504
505static int userns_btf_load(int mnt_fd)
506{
507 LIBBPF_OPTS(bpf_btf_load_opts, btf_opts);
508 int err, token_fd = -1, btf_fd = -1;
509 const void *raw_btf_data;
510 struct btf *btf = NULL;
511 __u32 raw_btf_size;
512 __u64 old_caps = 0;
513
514 /* create BPF token from BPF FS mount */
515 token_fd = bpf_token_create(mnt_fd, NULL);
516 if (!ASSERT_GT(token_fd, 0, "token_create")) {
517 err = -EINVAL;
518 goto cleanup;
519 }
520
521 /* while inside non-init userns, we need both a BPF token *and*
522 * CAP_BPF inside current userns to create privileged map; let's test
523 * that neither BPF token alone nor namespaced CAP_BPF is sufficient
524 */
525 err = drop_priv_caps(&old_caps);
526 if (!ASSERT_OK(err, "drop_caps"))
527 goto cleanup;
528
529 /* setup a trivial BTF data to load to the kernel */
530 btf = btf__new_empty();
531 if (!ASSERT_OK_PTR(btf, "empty_btf"))
532 goto cleanup;
533
534 ASSERT_GT(btf__add_int(btf, "int", 4, 0), 0, "int_type");
535
536 raw_btf_data = btf__raw_data(btf, &raw_btf_size);
537 if (!ASSERT_OK_PTR(raw_btf_data, "raw_btf_data"))
538 goto cleanup;
539
540 /* no token + no CAP_BPF -> failure */
541 btf_opts.token_fd = 0;
542 btf_fd = bpf_btf_load(raw_btf_data, raw_btf_size, &btf_opts);
543 if (!ASSERT_LT(btf_fd, 0, "no_token_no_cap_should_fail"))
544 goto cleanup;
545
546 /* token + no CAP_BPF -> failure */
547 btf_opts.token_fd = token_fd;
548 btf_fd = bpf_btf_load(raw_btf_data, raw_btf_size, &btf_opts);
549 if (!ASSERT_LT(btf_fd, 0, "token_no_cap_should_fail"))
550 goto cleanup;
551
552 /* get back effective local CAP_BPF (and CAP_SYS_ADMIN) */
553 err = restore_priv_caps(old_caps);
554 if (!ASSERT_OK(err, "restore_caps"))
555 goto cleanup;
556
557 /* token + CAP_BPF -> success */
558 btf_opts.token_fd = token_fd;
559 btf_fd = bpf_btf_load(raw_btf_data, raw_btf_size, &btf_opts);
560 if (!ASSERT_GT(btf_fd, 0, "token_and_cap_success"))
561 goto cleanup;
562
563 err = 0;
564cleanup:
565 btf__free(btf);
566 zclose(btf_fd);
567 zclose(token_fd);
568 return err;
569}
570
571static int userns_prog_load(int mnt_fd)
572{
573 LIBBPF_OPTS(bpf_prog_load_opts, prog_opts);
574 int err, token_fd = -1, prog_fd = -1;
575 struct bpf_insn insns[] = {
576 /* bpf_jiffies64() requires CAP_BPF */
577 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_jiffies64),
578 /* bpf_get_current_task() requires CAP_PERFMON */
579 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_current_task),
580 /* r0 = 0; exit; */
581 BPF_MOV64_IMM(BPF_REG_0, 0),
582 BPF_EXIT_INSN(),
583 };
584 size_t insn_cnt = ARRAY_SIZE(insns);
585 __u64 old_caps = 0;
586
587 /* create BPF token from BPF FS mount */
588 token_fd = bpf_token_create(mnt_fd, NULL);
589 if (!ASSERT_GT(token_fd, 0, "token_create")) {
590 err = -EINVAL;
591 goto cleanup;
592 }
593
594 /* validate we can successfully load BPF program with token; this
595 * being XDP program (CAP_NET_ADMIN) using bpf_jiffies64() (CAP_BPF)
596 * and bpf_get_current_task() (CAP_PERFMON) helpers validates we have
597 * BPF token wired properly in a bunch of places in the kernel
598 */
599 prog_opts.token_fd = token_fd;
600 prog_opts.expected_attach_type = BPF_XDP;
601 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, "token_prog", "GPL",
602 insns, insn_cnt, &prog_opts);
603 if (!ASSERT_GT(prog_fd, 0, "prog_fd")) {
604 err = -EPERM;
605 goto cleanup;
606 }
607
608 /* no token + caps -> failure */
609 prog_opts.token_fd = 0;
610 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, "token_prog", "GPL",
611 insns, insn_cnt, &prog_opts);
612 if (!ASSERT_EQ(prog_fd, -EPERM, "prog_fd_eperm")) {
613 err = -EPERM;
614 goto cleanup;
615 }
616
617 err = drop_priv_caps(&old_caps);
618 if (!ASSERT_OK(err, "drop_caps"))
619 goto cleanup;
620
621 /* no caps + token -> failure */
622 prog_opts.token_fd = token_fd;
623 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, "token_prog", "GPL",
624 insns, insn_cnt, &prog_opts);
625 if (!ASSERT_EQ(prog_fd, -EPERM, "prog_fd_eperm")) {
626 err = -EPERM;
627 goto cleanup;
628 }
629
630 /* no caps + no token -> definitely a failure */
631 prog_opts.token_fd = 0;
632 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, "token_prog", "GPL",
633 insns, insn_cnt, &prog_opts);
634 if (!ASSERT_EQ(prog_fd, -EPERM, "prog_fd_eperm")) {
635 err = -EPERM;
636 goto cleanup;
637 }
638
639 err = 0;
640cleanup:
641 zclose(prog_fd);
642 zclose(token_fd);
643 return err;
644}
645
646void test_token(void)
647{
648 if (test__start_subtest("map_token")) {
649 struct bpffs_opts opts = {
650 .cmds = 1ULL << BPF_MAP_CREATE,
651 .maps = 1ULL << BPF_MAP_TYPE_STACK,
652 };
653
654 subtest_userns(&opts, userns_map_create);
655 }
656 if (test__start_subtest("btf_token")) {
657 struct bpffs_opts opts = {
658 .cmds = 1ULL << BPF_BTF_LOAD,
659 };
660
661 subtest_userns(&opts, userns_btf_load);
662 }
663 if (test__start_subtest("prog_token")) {
664 struct bpffs_opts opts = {
665 .cmds = 1ULL << BPF_PROG_LOAD,
666 .progs = 1ULL << BPF_PROG_TYPE_XDP,
667 .attachs = 1ULL << BPF_XDP,
668 };
669
670 subtest_userns(&opts, userns_prog_load);
671 }
672}

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