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
110110 case BPF_FUNC_get_prandom_u32:
111111 return &bpf_get_prandom_u32_proto;
112112 case BPF_FUNC_trace_printk:
113− if (perfmon_capable())
113+ if (bpf_token_capable(prog->aux->token, CAP_PERFMON))
114114 return bpf_get_trace_printk_proto();
115115 fallthrough;
116116 default:
include/linux/bpf.h+73−10
@@ -51,6 +51,10 @@ struct module; · 50 unmodified lines
5151 struct bpf_func_state;
5252 struct ftrace_ops;
5353 struct cgroup;
54+struct bpf_token;
55+struct user_namespace;
56+struct super_block;
57+struct inode;
5458
5559 extern struct idr btf_idr;
5660 extern spinlock_t btf_idr_lock;
@@ -1457,6 +1461,7 @@ struct bpf_prog_aux { · 1400 unmodified lines
14571461 #ifdef CONFIG_SECURITY
14581462 void *security;
14591463 #endif
1464+ struct bpf_token *token;
14601465 struct bpf_prog_offload *offload;
14611466 struct btf *btf;
14621467 struct bpf_func_info *func_info;
@@ -1581,6 +1586,29 @@ struct bpf_link_primer { · 118 unmodified lines
15811586 u32 id;
15821587 };
15831588
1589+struct 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+
1599+struct 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+
15841612 struct bpf_struct_ops_value;
15851613 struct btf_member;
15861614
@@ -2038,6 +2066,7 @@ static inline void bpf_enable_instrumentation(void) · 451 unmodified lines
20382066 migrate_enable();
20392067 }
20402068
2069+extern const struct super_operations bpf_super_ops;
20412070 extern const struct file_operations bpf_map_fops;
20422071 extern const struct file_operations bpf_prog_fops;
20432072 extern 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
21722201
21732202 extern int sysctl_unprivileged_bpf_disabled;
21742203
2175−static inline bool bpf_allow_ptr_leaks(void)
2204+bool bpf_token_capable(const struct bpf_token *token, int cap);
2205+
2206+static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)
21762207 {
2177− return perfmon_capable();
2208+ return bpf_token_capable(token, CAP_PERFMON);
21782209 }
21792210
2180−static inline bool bpf_allow_uninit_stack(void)
2211+static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)
21812212 {
2182− return perfmon_capable();
2213+ return bpf_token_capable(token, CAP_PERFMON);
21832214 }
21842215
2185−static inline bool bpf_bypass_spec_v1(void)
2216+static inline bool bpf_bypass_spec_v1(const struct bpf_token *token)
21862217 {
2187− return cpu_mitigations_off() || perfmon_capable();
2218+ return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
21882219 }
21892220
2190−static inline bool bpf_bypass_spec_v4(void)
2221+static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
21912222 {
2192− return cpu_mitigations_off() || perfmon_capable();
2223+ return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
21932224 }
21942225
21952226 int 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
22062237 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
22072238 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
22082239
2240+void bpf_token_inc(struct bpf_token *token);
2241+void bpf_token_put(struct bpf_token *token);
2242+int bpf_token_create(union bpf_attr *attr);
2243+struct bpf_token *bpf_token_get_from_fd(u32 ufd);
2244+
2245+bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2246+bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
2247+bool 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+
22092251 int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);
22102252 int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);
2253+struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir,
2254+ umode_t mode);
22112255
22122256 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
22132257 #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
24512495 struct bpf_prog *bpf_prog_by_id(u32 id);
24522496 struct bpf_link *bpf_link_by_id(u32 id);
24532497
2454−const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
2498+const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
2499+ const struct bpf_prog *prog);
24552500 void bpf_task_storage_free(struct task_struct *task);
24562501 void bpf_cgrp_storage_free(struct cgroup *cgroup);
24572502 bool 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
25702615 return -EOPNOTSUPP;
25712616 }
25722617
2618+static 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+
2623+static inline void bpf_token_inc(struct bpf_token *token)
2624+{
2625+}
2626+
2627+static inline void bpf_token_put(struct bpf_token *token)
2628+{
2629+}
2630+
2631+static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
2632+{
2633+ return ERR_PTR(-EOPNOTSUPP);
2634+}
2635+
25732636 static inline void __dev_flush(void)
25742637 {
25752638 }
@@ -2693,7 +2756,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log, · 117 unmodified lines
26932756 }
26942757
26952758 static inline const struct bpf_func_proto *
2696−bpf_base_func_proto(enum bpf_func_id func_id)
2759+bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
26972760 {
26982761 return NULL;
26992762 }
include/linux/filter.h+1−1
@@ -1139,7 +1139,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) · 1138 unmodified lines
11391139 return false;
11401140 if (!bpf_jit_harden)
11411141 return false;
1142− if (bpf_jit_harden == 1 && bpf_capable())
1142+ if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF))
11431143 return false;
11441144
11451145 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
398398 LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size)
399399 LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode)
400400 LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog)
401−LSM_HOOK(int, 0, bpf_map_alloc_security, struct bpf_map *map)
402−LSM_HOOK(void, LSM_RET_VOID, bpf_map_free_security, struct bpf_map *map)
403−LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux)
404−LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux)
401+LSM_HOOK(int, 0, bpf_map_create, struct bpf_map *map, union bpf_attr *attr,
402+ struct bpf_token *token)
403+LSM_HOOK(void, LSM_RET_VOID, bpf_map_free, struct bpf_map *map)
404+LSM_HOOK(int, 0, bpf_prog_load, struct bpf_prog *prog, union bpf_attr *attr,
405+ struct bpf_token *token)
406+LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free, struct bpf_prog *prog)
407+LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr,
408+ struct path *path)
409+LSM_HOOK(void, LSM_RET_VOID, bpf_token_free, struct bpf_token *token)
410+LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
411+LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
405412 #endif /* CONFIG_BPF_SYSCALL */
406413
407414 LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h+36−7
@@ -32,6 +32,7 @@ · 31 unmodified lines
3232 #include <linux/string.h>
3333 #include <linux/mm.h>
3434 #include <linux/sockptr.h>
35+#include <linux/bpf.h>
3536
3637 struct linux_binprm;
3738 struct cred;
@@ -2020,15 +2021,22 @@ static inline void securityfs_remove(struct dentry *dentry) · 1982 unmodified lines
20202021 union bpf_attr;
20212022 struct bpf_map;
20222023 struct bpf_prog;
2023−struct bpf_prog_aux;
2024+struct bpf_token;
20242025 #ifdef CONFIG_SECURITY
20252026 extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size);
20262027 extern int security_bpf_map(struct bpf_map *map, fmode_t fmode);
20272028 extern int security_bpf_prog(struct bpf_prog *prog);
2028−extern int security_bpf_map_alloc(struct bpf_map *map);
2029+extern int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2030+ struct bpf_token *token);
20292031 extern void security_bpf_map_free(struct bpf_map *map);
2030−extern int security_bpf_prog_alloc(struct bpf_prog_aux *aux);
2031−extern void security_bpf_prog_free(struct bpf_prog_aux *aux);
2032+extern int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2033+ struct bpf_token *token);
2034+extern void security_bpf_prog_free(struct bpf_prog *prog);
2035+extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2036+ struct path *path);
2037+extern void security_bpf_token_free(struct bpf_token *token);
2038+extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2039+extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
20322040 #else
20332041 static inline int security_bpf(int cmd, union bpf_attr *attr,
20342042 unsigned int size)
@@ -2046,7 +2054,8 @@ static inline int security_bpf_prog(struct bpf_prog *prog) · 11 unmodified lines
20462054 return 0;
20472055 }
20482056
2049−static inline int security_bpf_map_alloc(struct bpf_map *map)
2057+static inline int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2058+ struct bpf_token *token)
20502059 {
20512060 return 0;
20522061 }
@@ -2054,13 +2063,33 @@ static inline int security_bpf_map_alloc(struct bpf_map *map) · 1 unmodified line
20542063 static inline void security_bpf_map_free(struct bpf_map *map)
20552064 { }
20562065
2057−static inline int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
2066+static inline int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2067+ struct bpf_token *token)
20582068 {
20592069 return 0;
20602070 }
20612071
2062−static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)
2072+static inline void security_bpf_prog_free(struct bpf_prog *prog)
20632073 { }
2074+
2075+static inline int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
2076+ struct path *path)
2077+{
2078+ return 0;
2079+}
2080+
2081+static inline void security_bpf_token_free(struct bpf_token *token)
2082+{ }
2083+
2084+static inline int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
2085+{
2086+ return 0;
2087+}
2088+
2089+static inline int security_bpf_token_capable(const struct bpf_token *token, int cap)
2090+{
2091+ return 0;
2092+}
20642093 #endif /* CONFIG_SECURITY */
20652094 #endif /* CONFIG_BPF_SYSCALL */
20662095
include/uapi/linux/bpf.h+42−0
@@ -847,6 +847,36 @@ union bpf_iter_link_info { · 846 unmodified lines
847847 * Returns zero on success. On error, -1 is returned and *errno*
848848 * is set appropriately.
849849 *
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+ *
850880 * NOTES
851881 * eBPF objects (maps and programs) can be shared between processes.
852882 *
@@ -901,6 +931,8 @@ enum bpf_cmd { · 48 unmodified lines
901931 BPF_ITER_CREATE,
902932 BPF_LINK_DETACH,
903933 BPF_PROG_BIND_MAP,
934+ BPF_TOKEN_CREATE,
935+ __MAX_BPF_CMD,
904936 };
905937
906938 enum bpf_map_type {
@@ -951,6 +983,7 @@ enum bpf_map_type { · 44 unmodified lines
951983 BPF_MAP_TYPE_BLOOM_FILTER,
952984 BPF_MAP_TYPE_USER_RINGBUF,
953985 BPF_MAP_TYPE_CGRP_STORAGE,
986+ __MAX_BPF_MAP_TYPE
954987 };
955988
956989 /* Note that tracing related programs such as
@@ -995,6 +1028,7 @@ enum bpf_prog_type { · 38 unmodified lines
9951028 BPF_PROG_TYPE_SK_LOOKUP,
9961029 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
9971030 BPF_PROG_TYPE_NETFILTER,
1031+ __MAX_BPF_PROG_TYPE
9981032 };
9991033
10001034 enum bpf_attach_type {
@@ -1401,6 +1435,7 @@ union bpf_attr { · 400 unmodified lines
14011435 * to using 5 hash functions).
14021436 */
14031437 __u64 map_extra;
1438+ __u32 map_token_fd;
14041439 };
14051440
14061441 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1470,6 +1505,7 @@ union bpf_attr { · 63 unmodified lines
14701505 * truncated), or smaller (if log buffer wasn't filled completely).
14711506 */
14721507 __u32 log_true_size;
1508+ __u32 prog_token_fd;
14731509 };
14741510
14751511 struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -1582,6 +1618,7 @@ union bpf_attr { · 106 unmodified lines
15821618 * truncated), or smaller (if log buffer wasn't filled completely).
15831619 */
15841620 __u32 btf_log_true_size;
1621+ __u32 btf_token_fd;
15851622 };
15861623
15871624 struct {
@@ -1712,6 +1749,11 @@ union bpf_attr { · 124 unmodified lines
17121749 __u32 flags; /* extra flags */
17131750 } prog_bind_map;
17141751
1752+ struct { /* struct used by BPF_TOKEN_CREATE command */
1753+ __u32 flags;
1754+ __u32 bpffs_fd;
1755+ } token_create;
1756+
17151757 } __attribute__((aligned(8)));
17161758
17171759 /* 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
66 endif
77 CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
88
9−obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o
9+obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o
1010 obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o
1111 obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o
1212 obj-$(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
8282 bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
8383 int numa_node = bpf_map_attr_numa_node(attr);
8484 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);
8686 u64 array_size, mask64;
8787 struct bpf_array *array;
8888
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
260260 BTF_SET_START(sleepable_lsm_hooks)
261261 BTF_ID(func, bpf_lsm_bpf)
262262 BTF_ID(func, bpf_lsm_bpf_map)
263−BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
264−BTF_ID(func, bpf_lsm_bpf_map_free_security)
263+BTF_ID(func, bpf_lsm_bpf_map_create)
264+BTF_ID(func, bpf_lsm_bpf_map_free)
265265 BTF_ID(func, bpf_lsm_bpf_prog)
266+BTF_ID(func, bpf_lsm_bpf_prog_load)
267+BTF_ID(func, bpf_lsm_bpf_prog_free)
268+BTF_ID(func, bpf_lsm_bpf_token_create)
269+BTF_ID(func, bpf_lsm_bpf_token_free)
270+BTF_ID(func, bpf_lsm_bpf_token_cmd)
271+BTF_ID(func, bpf_lsm_bpf_token_capable)
266272 BTF_ID(func, bpf_lsm_bprm_check_security)
267273 BTF_ID(func, bpf_lsm_bprm_committed_creds)
268274 BTF_ID(func, bpf_lsm_bprm_committing_creds)
@@ -345,9 +351,8 @@ BTF_ID(func, bpf_lsm_userns_create) · 76 unmodified lines
345351 BTF_SET_END(sleepable_lsm_hooks)
346352
347353 BTF_SET_START(untrusted_lsm_hooks)
348−BTF_ID(func, bpf_lsm_bpf_map_free_security)
349−BTF_ID(func, bpf_lsm_bpf_prog_alloc_security)
350−BTF_ID(func, bpf_lsm_bpf_prog_free_security)
354+BTF_ID(func, bpf_lsm_bpf_map_free)
355+BTF_ID(func, bpf_lsm_bpf_prog_free)
351356 BTF_ID(func, bpf_lsm_file_alloc_security)
352357 BTF_ID(func, bpf_lsm_file_free_security)
353358 #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
16301630 case BPF_FUNC_perf_event_output:
16311631 return &bpf_event_output_data_proto;
16321632 default:
1633− return bpf_base_func_proto(func_id);
1633+ return bpf_base_func_proto(func_id, prog);
16341634 }
16351635 }
16361636
@@ -2191,7 +2191,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 554 unmodified lines
21912191 case BPF_FUNC_perf_event_output:
21922192 return &bpf_event_output_data_proto;
21932193 default:
2194− return bpf_base_func_proto(func_id);
2194+ return bpf_base_func_proto(func_id, prog);
21952195 }
21962196 }
21972197
@@ -2348,7 +2348,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 150 unmodified lines
23482348 case BPF_FUNC_perf_event_output:
23492349 return &bpf_event_output_data_proto;
23502350 default:
2351− return bpf_base_func_proto(func_id);
2351+ return bpf_base_func_proto(func_id, prog);
23522352 }
23532353 }
23542354
kernel/bpf/core.c+2−1
@@ -675,7 +675,7 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) · 674 unmodified lines
675675 void bpf_prog_kallsyms_add(struct bpf_prog *fp)
676676 {
677677 if (!bpf_prog_kallsyms_candidate(fp) ||
678− !bpf_capable())
678+ !bpf_token_capable(fp->aux->token, CAP_BPF))
679679 return;
680680
681681 bpf_prog_ksym_set_addr(fp);
@@ -2751,6 +2751,7 @@ void bpf_prog_free(struct bpf_prog *fp) · 2069 unmodified lines
27512751
27522752 if (aux->dst_prog)
27532753 bpf_prog_put(aux->dst_prog);
2754+ bpf_token_put(aux->token);
27542755 INIT_WORK(&aux->work, bpf_prog_free_deferred);
27552756 schedule_work(&aux->work);
27562757 }
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
16791679 const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
16801680
16811681 const struct bpf_func_proto *
1682−bpf_base_func_proto(enum bpf_func_id func_id)
1682+bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
16831683 {
16841684 switch (func_id) {
16851685 case BPF_FUNC_map_lookup_elem:
@@ -1730,7 +1730,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 44 unmodified lines
17301730 break;
17311731 }
17321732
1733− if (!bpf_capable())
1733+ if (!bpf_token_capable(prog->aux->token, CAP_BPF))
17341734 return NULL;
17351735
17361736 switch (func_id) {
@@ -1788,7 +1788,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 51 unmodified lines
17881788 break;
17891789 }
17901790
1791− if (!perfmon_capable())
1791+ if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
17921792 return NULL;
17931793
17941794 switch (func_id) {
kernel/bpf/inode.c+87−14
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <linux/filter.h>
2121 #include <linux/bpf.h>
2222 #include <linux/bpf_trace.h>
23+#include <linux/kstrtox.h>
2324 #include "preload/bpf_preload.h"
2425
2526 enum bpf_type {
@@ -98,9 +99,9 @@ static const struct inode_operations bpf_prog_iops = { }; · 72 unmodified lines
9899 static const struct inode_operations bpf_map_iops = { };
99100 static const struct inode_operations bpf_link_iops = { };
100101
101−static struct inode *bpf_get_inode(struct super_block *sb,
102− const struct inode *dir,
103− umode_t mode)
102+struct inode *bpf_get_inode(struct super_block *sb,
103+ const struct inode *dir,
104+ umode_t mode)
104105 {
105106 struct inode *inode;
106107
@@ -599,10 +600,36 @@ EXPORT_SYMBOL(bpf_prog_get_type_path); · 492 unmodified lines
599600 */
600601 static int bpf_show_options(struct seq_file *m, struct dentry *root)
601602 {
603+ struct bpf_mount_opts *opts = root->d_sb->s_fs_info;
602604 umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
605+ u64 mask;
603606
604607 if (mode != S_IRWXUGO)
605608 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);
606633 return 0;
607634 }
608635
@@ -617,7 +644,7 @@ static void bpf_free_inode(struct inode *inode) · 8 unmodified lines
617644 free_inode_nonrcu(inode);
618645 }
619646
620−static const struct super_operations bpf_super_ops = {
647+const struct super_operations bpf_super_ops = {
621648 .statfs = simple_statfs,
622649 .drop_inode = generic_delete_inode,
623650 .show_options = bpf_show_options,
@@ -626,22 +653,27 @@ static const struct super_operations bpf_super_ops = { · 2 unmodified lines
626653
627654 enum {
628655 OPT_MODE,
656+ OPT_DELEGATE_CMDS,
657+ OPT_DELEGATE_MAPS,
658+ OPT_DELEGATE_PROGS,
659+ OPT_DELEGATE_ATTACHS,
629660 };
630661
631662 static const struct fs_parameter_spec bpf_fs_parameters[] = {
632663 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),
633668 {}
634669 };
635670
636−struct bpf_mount_opts {
637− umode_t mode;
638−};
639−
640671 static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
641672 {
642− struct bpf_mount_opts *opts = fc->fs_private;
673+ struct bpf_mount_opts *opts = fc->s_fs_info;
643674 struct fs_parse_result result;
644− int opt;
675+ int opt, err;
676+ u64 msk;
645677
646678 opt = fs_parse(fc, bpf_fs_parameters, param, &result);
647679 if (opt < 0) {
@@ -665,6 +697,28 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param) · 17 unmodified lines
665697 case OPT_MODE:
666698 opts->mode = result.uint_32 & S_IALLUGO;
667699 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;
668722 }
669723
670724 return 0;
@@ -739,10 +793,14 @@ out: · 68 unmodified lines
739793 static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
740794 {
741795 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;
743797 struct inode *inode;
744798 int ret;
745799
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+
746804 ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
747805 if (ret)
748806 return ret;
@@ -764,7 +822,7 @@ static int bpf_get_tree(struct fs_context *fc) · 15 unmodified lines
764822
765823 static void bpf_free_fc(struct fs_context *fc)
766824 {
767− kfree(fc->fs_private);
825+ kfree(fc->s_fs_info);
768826 }
769827
770828 static 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
786844
787845 opts->mode = S_IRWXUGO;
788846
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;
790854 fc->ops = &bpf_context_ops;
791855 return 0;
792856 }
793857
858+static 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+
794866 static struct file_system_type bpf_fs_type = {
795867 .owner = THIS_MODULE,
796868 .name = "bpf",
797869 .init_fs_context = bpf_init_fs_context,
798870 .parameters = bpf_fs_parameters,
799− .kill_sb = kill_litter_super,
871+ .kill_sb = bpf_kill_super,
872+ .fs_flags = FS_USERNS_MOUNT,
800873 };
801874
802875 static 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
10091009 return -ENOTSUPP;
10101010 }
10111011
1012−static int map_check_btf(struct bpf_map *map, const struct btf *btf,
1013− u32 btf_key_id, u32 btf_value_id)
1012+static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
1013+ const struct btf *btf, u32 btf_key_id, u32 btf_value_id)
10141014 {
10151015 const struct btf_type *key_type, *value_type;
10161016 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
10381038 if (!IS_ERR_OR_NULL(map->record)) {
10391039 int i;
10401040
1041− if (!bpf_capable()) {
1041+ if (!bpf_token_capable(token, CAP_BPF)) {
10421042 ret = -EPERM;
10431043 goto free_map_tab;
10441044 }
@@ -1121,11 +1121,17 @@ free_map_tab: · 76 unmodified lines
11211121 return ret;
11221122 }
11231123
1124−#define BPF_MAP_CREATE_LAST_FIELD map_extra
1124+static 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
11251130 /* called via syscall */
11261131 static int map_create(union bpf_attr *attr)
11271132 {
11281133 const struct bpf_map_ops *ops;
1134+ struct bpf_token *token = NULL;
11291135 int numa_node = bpf_map_attr_numa_node(attr);
11301136 u32 map_type = attr->map_type;
11311137 struct bpf_map *map;
@@ -1176,14 +1182,32 @@ static int map_create(union bpf_attr *attr) · 44 unmodified lines
11761182 if (!ops->map_mem_usage)
11771183 return -EINVAL;
11781184
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+
11791203 /* Intent here is for unprivileged_bpf_disabled to block BPF map
11801204 * creation for unprivileged users; other actions depend
11811205 * on fd availability and access to bpffs, so are dependent on
11821206 * object creation success. Even with unprivileged BPF disabled,
11831207 * capability checks are still carried out.
11841208 */
1185− if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
1186− return -EPERM;
1209+ if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF))
1210+ goto put_token;
11871211
11881212 /* check privileged map type permissions */
11891213 switch (map_type) {
@@ -1216,25 +1240,27 @@ static int map_create(union bpf_attr *attr) · 26 unmodified lines
12161240 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
12171241 case BPF_MAP_TYPE_STRUCT_OPS:
12181242 case BPF_MAP_TYPE_CPUMAP:
1219− if (!bpf_capable())
1220− return -EPERM;
1243+ if (!bpf_token_capable(token, CAP_BPF))
1244+ goto put_token;
12211245 break;
12221246 case BPF_MAP_TYPE_SOCKMAP:
12231247 case BPF_MAP_TYPE_SOCKHASH:
12241248 case BPF_MAP_TYPE_DEVMAP:
12251249 case BPF_MAP_TYPE_DEVMAP_HASH:
12261250 case BPF_MAP_TYPE_XSKMAP:
1227− if (!capable(CAP_NET_ADMIN))
1228− return -EPERM;
1251+ if (!bpf_token_capable(token, CAP_NET_ADMIN))
1252+ goto put_token;
12291253 break;
12301254 default:
12311255 WARN(1, "unsupported map type %d", map_type);
1232− return -EPERM;
1256+ goto put_token;
12331257 }
12341258
12351259 map = ops->map_alloc(attr);
1236− if (IS_ERR(map))
1237− return PTR_ERR(map);
1260+ if (IS_ERR(map)) {
1261+ err = PTR_ERR(map);
1262+ goto put_token;
1263+ }
12381264 map->ops = ops;
12391265 map->map_type = map_type;
12401266
@@ -1271,7 +1297,7 @@ static int map_create(union bpf_attr *attr) · 30 unmodified lines
12711297 map->btf = btf;
12721298
12731299 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,
12751301 attr->btf_value_type_id);
12761302 if (err)
12771303 goto free_map;
@@ -1283,15 +1309,16 @@ static int map_create(union bpf_attr *attr) · 5 unmodified lines
12831309 attr->btf_vmlinux_value_type_id;
12841310 }
12851311
1286− err = security_bpf_map_alloc(map);
1312+ err = security_bpf_map_create(map, attr, token);
12871313 if (err)
1288− goto free_map;
1314+ goto free_map_sec;
12891315
12901316 err = bpf_map_alloc_id(map);
12911317 if (err)
12921318 goto free_map_sec;
12931319
12941320 bpf_map_save_memcg(map);
1321+ bpf_token_put(token);
12951322
12961323 err = bpf_map_new_fd(map, f_flags);
12971324 if (err < 0) {
@@ -1312,6 +1339,8 @@ free_map_sec: · 14 unmodified lines
13121339 free_map:
13131340 btf_put(map->btf);
13141341 map->ops->map_free(map);
1342+put_token:
1343+ bpf_token_put(token);
13151344 return err;
13161345 }
13171346
@@ -2133,7 +2162,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu) · 815 unmodified lines
21332162 kvfree(aux->func_info);
21342163 kfree(aux->func_info_aux);
21352164 free_uid(aux->user);
2136− security_bpf_prog_free(aux);
2165+ security_bpf_prog_free(aux->prog);
21372166 bpf_prog_free(aux->prog);
21382167 }
21392168
@@ -2579,13 +2608,15 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · 439 unmodified lines
25792608 }
25802609
25812610 /* last field in 'union bpf_attr' used by this command */
2582−#define BPF_PROG_LOAD_LAST_FIELD log_true_size
2611+#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd
25832612
25842613 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
25852614 {
25862615 enum bpf_prog_type type = attr->prog_type;
25872616 struct bpf_prog *prog, *dst_prog = NULL;
25882617 struct btf *attach_btf = NULL;
2618+ struct bpf_token *token = NULL;
2619+ bool bpf_cap;
25892620 int err;
25902621 char license[128];
25912622
@@ -2602,10 +2633,31 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 10 unmodified lines
26022633 BPF_F_TEST_REG_INVARIANTS))
26032634 return -EINVAL;
26042635
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+
26052657 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
26062658 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2607− !bpf_capable())
2608− return -EPERM;
2659+ !bpf_cap)
2660+ goto put_token;
26092661
26102662 /* Intent here is for unprivileged_bpf_disabled to block BPF program
26112663 * 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
26142666 * capability checks are still carried out for these
26152667 * and other operations.
26162668 */
2617− if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
2618− return -EPERM;
2669+ if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
2670+ goto put_token;
26192671
26202672 if (attr->insn_cnt == 0 ||
2621− attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2622− return -E2BIG;
2673+ attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) {
2674+ err = -E2BIG;
2675+ goto put_token;
2676+ }
26232677 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
26242678 type != BPF_PROG_TYPE_CGROUP_SKB &&
2625− !bpf_capable())
2626− return -EPERM;
2679+ !bpf_cap)
2680+ goto put_token;
26272681
2628− if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2629− return -EPERM;
2630− if (is_perfmon_prog_type(type) && !perfmon_capable())
2631− return -EPERM;
2682+ if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN))
2683+ goto put_token;
2684+ if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON))
2685+ goto put_token;
26322686
26332687 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
26342688 * 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
26382692 if (IS_ERR(dst_prog)) {
26392693 dst_prog = NULL;
26402694 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2641− if (IS_ERR(attach_btf))
2642− return -EINVAL;
2695+ if (IS_ERR(attach_btf)) {
2696+ err = -EINVAL;
2697+ goto put_token;
2698+ }
26432699 if (!btf_is_kernel(attach_btf)) {
26442700 /* attaching through specifying bpf_prog's BTF
26452701 * objects directly might be supported eventually
26462702 */
26472703 btf_put(attach_btf);
2648− return -ENOTSUPP;
2704+ err = -ENOTSUPP;
2705+ goto put_token;
26492706 }
26502707 }
26512708 } else if (attr->attach_btf_id) {
26522709 /* fall back to vmlinux BTF, if BTF type ID is specified */
26532710 attach_btf = bpf_get_btf_vmlinux();
2654− if (IS_ERR(attach_btf))
2655− return PTR_ERR(attach_btf);
2656− if (!attach_btf)
2657− return -EINVAL;
2711+ if (IS_ERR(attach_btf)) {
2712+ err = PTR_ERR(attach_btf);
2713+ goto put_token;
2714+ }
2715+ if (!attach_btf) {
2716+ err = -EINVAL;
2717+ goto put_token;
2718+ }
26582719 btf_get(attach_btf);
26592720 }
26602721
2661− bpf_prog_load_fixup_attach_type(attr);
26622722 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
26632723 attach_btf, attr->attach_btf_id,
26642724 dst_prog)) {
@@ -2666,7 +2726,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) · 1 unmodified line
26662726 bpf_prog_put(dst_prog);
26672727 if (attach_btf)
26682728 btf_put(attach_btf);
2669− return -EINVAL;
2729+ err = -EINVAL;
2730+ goto put_token;
26702731 }
26712732
26722733 /* 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
26762737 bpf_prog_put(dst_prog);
26772738 if (attach_btf)
26782739 btf_put(attach_btf);
2679− return -ENOMEM;
2740+ err = -EINVAL;
2741+ goto put_token;
26802742 }
26812743
26822744 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
26872749 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
26882750 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
26892751
2690− err = security_bpf_prog_alloc(prog->aux);
2691− if (err)
2692− goto free_prog;
2752+ /* move token into prog->aux, reuse taken refcnt */
2753+ prog->aux->token = token;
2754+ token = NULL;
26932755
26942756 prog->aux->user = get_current_user();
26952757 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
26982760 if (copy_from_bpfptr(prog->insns,
26992761 make_bpfptr(attr->insns, uattr.is_kernel),
27002762 bpf_prog_insn_size(prog)) != 0)
2701− goto free_prog_sec;
2763+ goto free_prog;
27022764 /* copy eBPF program license from user space */
27032765 if (strncpy_from_bpfptr(license,
27042766 make_bpfptr(attr->license, uattr.is_kernel),
27052767 sizeof(license) - 1) < 0)
2706− goto free_prog_sec;
2768+ goto free_prog;
27072769 license[sizeof(license) - 1] = 0;
27082770
27092771 /* 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
27172779 if (bpf_prog_is_dev_bound(prog->aux)) {
27182780 err = bpf_prog_dev_bound_init(prog, attr);
27192781 if (err)
2720− goto free_prog_sec;
2782+ goto free_prog;
27212783 }
27222784
27232785 if (type == BPF_PROG_TYPE_EXT && dst_prog &&
27242786 bpf_prog_is_dev_bound(dst_prog->aux)) {
27252787 err = bpf_prog_dev_bound_inherit(prog, dst_prog);
27262788 if (err)
2727− goto free_prog_sec;
2789+ goto free_prog;
27282790 }
27292791
27302792 /* find program type: socket_filter vs tracing_filter */
27312793 err = find_prog_type(type, prog);
27322794 if (err < 0)
2733− goto free_prog_sec;
2795+ goto free_prog;
27342796
27352797 prog->aux->load_time = ktime_get_boottime_ns();
27362798 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
27372799 sizeof(attr->prog_name));
27382800 if (err < 0)
2801+ goto free_prog;
2802+
2803+ err = security_bpf_prog_load(prog, attr, token);
2804+ if (err)
27392805 goto free_prog_sec;
27402806
27412807 /* run eBPF verifier */
@@ -2781,13 +2847,16 @@ free_used_maps: · 39 unmodified lines
27812847 */
27822848 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt);
27832849 return err;
2850+
27842851 free_prog_sec:
2785− free_uid(prog->aux->user);
2786− security_bpf_prog_free(prog->aux);
2852+ security_bpf_prog_free(prog);
27872853 free_prog:
2854+ free_uid(prog->aux->user);
27882855 if (prog->aux->attach_btf)
27892856 btf_put(prog->aux->attach_btf);
27902857 bpf_prog_free(prog);
2858+put_token:
2859+ bpf_token_put(token);
27912860 return err;
27922861 }
27932862
@@ -3777,7 +3846,7 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, · 983 unmodified lines
37773846 case BPF_PROG_TYPE_SK_LOOKUP:
37783847 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
37793848 case BPF_PROG_TYPE_CGROUP_SKB:
3780− if (!capable(CAP_NET_ADMIN))
3849+ if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
37813850 /* cg-skb progs can be loaded by unpriv user.
37823851 * check permissions at attach time.
37833852 */
@@ -3980,7 +4049,7 @@ static int bpf_prog_detach(const union bpf_attr *attr) · 196 unmodified lines
39804049 static int bpf_prog_query(const union bpf_attr *attr,
39814050 union bpf_attr __user *uattr)
39824051 {
3983− if (!capable(CAP_NET_ADMIN))
4052+ if (!bpf_net_capable())
39844053 return -EPERM;
39854054 if (CHECK_ATTR(BPF_PROG_QUERY))
39864055 return -EINVAL;
@@ -4748,15 +4817,31 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, · 761 unmodified lines
47484817 return err;
47494818 }
47504819
4751−#define BPF_BTF_LOAD_LAST_FIELD btf_log_true_size
4820+#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
47524821
47534822 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
47544823 {
4824+ struct bpf_token *token = NULL;
4825+
47554826 if (CHECK_ATTR(BPF_BTF_LOAD))
47564827 return -EINVAL;
47574828
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);
47594841 return -EPERM;
4842+ }
4843+
4844+ bpf_token_put(token);
47604845
47614846 return btf_new_fd(attr, uattr, uattr_size);
47624847 }
@@ -5372,6 +5457,20 @@ out_prog_put: · 609 unmodified lines
53725457 return ret;
53735458 }
53745459
5460+#define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd
5461+
5462+static 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+
53755474 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
53765475 {
53775476 union bpf_attr attr;
@@ -5505,6 +5604,9 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) · 127 unmodified lines
55055604 case BPF_PROG_BIND_MAP:
55065605 err = bpf_prog_bind_map(&attr);
55075606 break;
5607+ case BPF_TOKEN_CREATE:
5608+ err = token_create(&attr);
5609+ break;
55085610 default:
55095611 err = -EINVAL;
55105612 break;
@@ -5611,7 +5713,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { · 100 unmodified lines
56115713 const struct bpf_func_proto * __weak
56125714 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
56135715 {
5614− return bpf_base_func_proto(func_id);
5716+ return bpf_base_func_proto(func_id, prog);
56155717 }
56165718
56175719 BPF_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
56615763 {
56625764 switch (func_id) {
56635765 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;
56655768 case BPF_FUNC_btf_find_by_name_kind:
56665769 return &bpf_btf_find_by_name_kind_proto;
56675770 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+
12+bool 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+
26+void bpf_token_inc(struct bpf_token *token)
27+{
28+ atomic64_inc(&token->refcnt);
29+}
30+
31+static 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+
38+static 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+
45+void 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+
57+static 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+
65+static 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+
101+static const struct inode_operations bpf_token_iops = { };
102+
103+static const struct file_operations bpf_token_fops = {
104+ .release = bpf_token_release,
105+ .show_fdinfo = bpf_token_show_fdinfo,
106+};
107+
108+int 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+
206+out_token:
207+ bpf_token_free(token);
208+out_file:
209+ fput(file);
210+out_path:
211+ path_put(&path);
212+ return err;
213+}
214+
215+struct 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+
234+bool 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+
246+bool 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+
254+bool 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
2059720597 env->prog = *prog;
2059820598 env->ops = bpf_verifier_ops[env->prog->type];
2059920599 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);
2060120606
2060220607 bpf_get_btf_vmlinux();
2060320608
@@ -20629,12 +20634,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3 · 25 unmodified lines
2062920634 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
2063020635 env->strict_alignment = false;
2063120636
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−
2063820637 if (is_priv)
2063920638 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
2064020639 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
16261626 case BPF_FUNC_trace_vprintk:
16271627 return bpf_get_trace_vprintk_proto();
16281628 default:
1629− return bpf_base_func_proto(func_id);
1629+ return bpf_base_func_proto(func_id, prog);
16301630 }
16311631 }
16321632
net/core/filter.c+18−18
@@ -87,7 +87,7 @@ · 86 unmodified lines
8787 #include "dev.h"
8888
8989 static const struct bpf_func_proto *
90−bpf_sk_base_func_proto(enum bpf_func_id func_id);
90+bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
9191
9292 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
9393 {
@@ -7841,7 +7841,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 7747 unmodified lines
78417841 case BPF_FUNC_ktime_get_coarse_ns:
78427842 return &bpf_ktime_get_coarse_ns_proto;
78437843 default:
7844− return bpf_base_func_proto(func_id);
7844+ return bpf_base_func_proto(func_id, prog);
78457845 }
78467846 }
78477847
@@ -7934,7 +7934,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 86 unmodified lines
79347934 return NULL;
79357935 }
79367936 default:
7937− return bpf_sk_base_func_proto(func_id);
7937+ return bpf_sk_base_func_proto(func_id, prog);
79387938 }
79397939 }
79407940
@@ -7953,7 +7953,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 12 unmodified lines
79537953 case BPF_FUNC_perf_event_output:
79547954 return &bpf_skb_event_output_proto;
79557955 default:
7956− return bpf_sk_base_func_proto(func_id);
7956+ return bpf_sk_base_func_proto(func_id, prog);
79577957 }
79587958 }
79597959
@@ -8140,7 +8140,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 180 unmodified lines
81408140 #endif
81418141 #endif
81428142 default:
8143− return bpf_sk_base_func_proto(func_id);
8143+ return bpf_sk_base_func_proto(func_id, prog);
81448144 }
81458145 }
81468146
@@ -8199,7 +8199,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 52 unmodified lines
81998199 #endif
82008200 #endif
82018201 default:
8202− return bpf_sk_base_func_proto(func_id);
8202+ return bpf_sk_base_func_proto(func_id, prog);
82038203 }
82048204
82058205 #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
82608260 return &bpf_tcp_sock_proto;
82618261 #endif /* CONFIG_INET */
82628262 default:
8263− return bpf_sk_base_func_proto(func_id);
8263+ return bpf_sk_base_func_proto(func_id, prog);
82648264 }
82658265 }
82668266
@@ -8302,7 +8302,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 35 unmodified lines
83028302 return &bpf_get_cgroup_classid_curr_proto;
83038303 #endif
83048304 default:
8305− return bpf_sk_base_func_proto(func_id);
8305+ return bpf_sk_base_func_proto(func_id, prog);
83068306 }
83078307 }
83088308
@@ -8346,7 +8346,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 37 unmodified lines
83468346 return &bpf_skc_lookup_tcp_proto;
83478347 #endif
83488348 default:
8349− return bpf_sk_base_func_proto(func_id);
8349+ return bpf_sk_base_func_proto(func_id, prog);
83508350 }
83518351 }
83528352
@@ -8357,7 +8357,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 4 unmodified lines
83578357 case BPF_FUNC_skb_load_bytes:
83588358 return &bpf_flow_dissector_load_bytes_proto;
83598359 default:
8360− return bpf_sk_base_func_proto(func_id);
8360+ return bpf_sk_base_func_proto(func_id, prog);
83618361 }
83628362 }
83638363
@@ -8384,7 +8384,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 20 unmodified lines
83848384 case BPF_FUNC_skb_under_cgroup:
83858385 return &bpf_skb_under_cgroup_proto;
83868386 default:
8387− return bpf_sk_base_func_proto(func_id);
8387+ return bpf_sk_base_func_proto(func_id, prog);
83888388 }
83898389 }
83908390
@@ -8559,7 +8559,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 168 unmodified lines
85598559 return false;
85608560 case bpf_ctx_range(struct __sk_buff, data):
85618561 case bpf_ctx_range(struct __sk_buff, data_end):
8562− if (!bpf_capable())
8562+ if (!bpf_token_capable(prog->aux->token, CAP_BPF))
85638563 return false;
85648564 break;
85658565 }
@@ -8571,7 +8571,7 @@ static bool cg_skb_is_valid_access(int off, int size, · 5 unmodified lines
85718571 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
85728572 break;
85738573 case bpf_ctx_range(struct __sk_buff, tstamp):
8574− if (!bpf_capable())
8574+ if (!bpf_token_capable(prog->aux->token, CAP_BPF))
85758575 return false;
85768576 break;
85778577 default:
@@ -11215,7 +11215,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id, · 2637 unmodified lines
1121511215 case BPF_FUNC_ktime_get_coarse_ns:
1121611216 return &bpf_ktime_get_coarse_ns_proto;
1121711217 default:
11218− return bpf_base_func_proto(func_id);
11218+ return bpf_base_func_proto(func_id, prog);
1121911219 }
1122011220 }
1122111221
@@ -11397,7 +11397,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 175 unmodified lines
1139711397 case BPF_FUNC_sk_release:
1139811398 return &bpf_sk_release_proto;
1139911399 default:
11400− return bpf_sk_base_func_proto(func_id);
11400+ return bpf_sk_base_func_proto(func_id, prog);
1140111401 }
1140211402 }
1140311403
@@ -11731,7 +11731,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = { · 327 unmodified lines
1173111731 };
1173211732
1173311733 static const struct bpf_func_proto *
11734−bpf_sk_base_func_proto(enum bpf_func_id func_id)
11734+bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
1173511735 {
1173611736 const struct bpf_func_proto *func;
1173711737
@@ -11760,10 +11760,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id) · 22 unmodified lines
1176011760 case BPF_FUNC_ktime_get_coarse_ns:
1176111761 return &bpf_ktime_get_coarse_ns_proto;
1176211762 default:
11763− return bpf_base_func_proto(func_id);
11763+ return bpf_base_func_proto(func_id, prog);
1176411764 }
1176511765
11766− if (!perfmon_capable())
11766+ if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
1176711767 return NULL;
1176811768
1176911769 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
191191 case BPF_FUNC_ktime_get_coarse_ns:
192192 return &bpf_ktime_get_coarse_ns_proto;
193193 default:
194− return bpf_base_func_proto(func_id);
194+ return bpf_base_func_proto(func_id, prog);
195195 }
196196 }
197197
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
314314 static const struct bpf_func_proto *
315315 bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
316316 {
317− return bpf_base_func_proto(func_id);
317+ return bpf_base_func_proto(func_id, prog);
318318 }
319319
320320 const 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
51675167 }
51685168
51695169 /**
5170− * security_bpf_map_alloc() - Allocate a bpf map LSM blob
5171− * @map: bpf map
5170+ * security_bpf_map_create() - Check if BPF map creation is allowed
5171+ * @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+ */
5180+int 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
51725191 *
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.
51745195 *
51755196 * Return: Returns 0 on success, error on failure.
51765197 */
5177−int security_bpf_map_alloc(struct bpf_map *map)
5198+int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5199+ struct bpf_token *token)
51785200 {
5179− return call_int_hook(bpf_map_alloc_security, 0, map);
5201+ return call_int_hook(bpf_prog_load, 0, prog, attr, token);
51805202 }
51815203
51825204 /**
5183− * security_bpf_prog_alloc() - Allocate a bpf program LSM blob
5184− * @aux: bpf program aux info struct
5205+ * security_bpf_token_create() - Check if creating of BPF token is allowed
5206+ * @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
51855209 *
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.
51875212 *
51885213 * Return: Returns 0 on success, error on failure.
51895214 */
5190−int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
5215+int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
5216+ struct path *path)
51915217 {
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+ */
5232+int 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+ */
5248+int security_bpf_token_capable(const struct bpf_token *token, int cap)
5249+{
5250+ return call_int_hook(bpf_token_capable, 0, token, cap);
51935251 }
51945252
51955253 /**
@@ -5200,18 +5258,29 @@ int security_bpf_prog_alloc(struct bpf_prog_aux *aux) · 4 unmodified lines
52005258 */
52015259 void security_bpf_map_free(struct bpf_map *map)
52025260 {
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+ */
5270+void security_bpf_prog_free(struct bpf_prog *prog)
5271+{
5272+ call_void_hook(bpf_prog_free, prog);
52045273 }
52055274
52065275 /**
5207− * security_bpf_prog_free() - Free a bpf program's LSM blob
5208− * @aux: bpf program aux info struct
5276+ * security_bpf_token_free() - Free a BPF token's LSM blob
5277+ * @token: BPF token struct
52095278 *
5210− * Clean up the security information stored inside bpf prog.
5279+ * Clean up the security information stored inside BPF token.
52115280 */
5212−void security_bpf_prog_free(struct bpf_prog_aux *aux)
5281+void security_bpf_token_free(struct bpf_token *token)
52135282 {
5214− call_void_hook(bpf_prog_free_security, aux);
5283+ call_void_hook(bpf_token_free, token);
52155284 }
52165285 #endif /* CONFIG_BPF_SYSCALL */
52175286
security/selinux/hooks.c+37−10
@@ -6783,7 +6783,8 @@ static int selinux_bpf_prog(struct bpf_prog *prog) · 6782 unmodified lines
67836783 BPF__PROG_RUN, NULL);
67846784 }
67856785
6786−static int selinux_bpf_map_alloc(struct bpf_map *map)
6786+static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
6787+ struct bpf_token *token)
67876788 {
67886789 struct bpf_security_struct *bpfsec;
67896790
@@ -6805,7 +6806,8 @@ static void selinux_bpf_map_free(struct bpf_map *map) · 15 unmodified lines
68056806 kfree(bpfsec);
68066807 }
68076808
6808−static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6809+static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
6810+ struct bpf_token *token)
68096811 {
68106812 struct bpf_security_struct *bpfsec;
68116813
@@ -6814,16 +6816,39 @@ static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux) · 2 unmodified lines
68146816 return -ENOMEM;
68156817
68166818 bpfsec->sid = current_sid();
6817− aux->security = bpfsec;
6819+ prog->aux->security = bpfsec;
68186820
68196821 return 0;
68206822 }
68216823
6822−static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6824+static void selinux_bpf_prog_free(struct bpf_prog *prog)
68236825 {
6824− struct bpf_security_struct *bpfsec = aux->security;
6826+ struct bpf_security_struct *bpfsec = prog->aux->security;
68256827
6826− aux->security = NULL;
6828+ prog->aux->security = NULL;
6829+ kfree(bpfsec);
6830+}
6831+
6832+static 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+
6847+static void selinux_bpf_token_free(struct bpf_token *token)
6848+{
6849+ struct bpf_security_struct *bpfsec = token->security;
6850+
6851+ token->security = NULL;
68276852 kfree(bpfsec);
68286853 }
68296854 #endif
@@ -7179,8 +7204,9 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { · 349 unmodified lines
71797204 LSM_HOOK_INIT(bpf, selinux_bpf),
71807205 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
71817206 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7182− LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7183− LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7207+ LSM_HOOK_INIT(bpf_map_free, selinux_bpf_map_free),
7208+ LSM_HOOK_INIT(bpf_prog_free, selinux_bpf_prog_free),
7209+ LSM_HOOK_INIT(bpf_token_free, selinux_bpf_token_free),
71847210 #endif
71857211
71867212 #ifdef CONFIG_PERF_EVENTS
@@ -7237,8 +7263,9 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = { · 50 unmodified lines
72377263 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
72387264 #endif
72397265 #ifdef CONFIG_BPF_SYSCALL
7240− LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7241− LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7266+ LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
7267+ LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
7268+ LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
72427269 #endif
72437270 #ifdef CONFIG_PERF_EVENTS
72447271 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
847847 * Returns zero on success. On error, -1 is returned and *errno*
848848 * is set appropriately.
849849 *
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+ *
850880 * NOTES
851881 * eBPF objects (maps and programs) can be shared between processes.
852882 *
@@ -901,6 +931,8 @@ enum bpf_cmd { · 48 unmodified lines
901931 BPF_ITER_CREATE,
902932 BPF_LINK_DETACH,
903933 BPF_PROG_BIND_MAP,
934+ BPF_TOKEN_CREATE,
935+ __MAX_BPF_CMD,
904936 };
905937
906938 enum bpf_map_type {
@@ -951,6 +983,7 @@ enum bpf_map_type { · 44 unmodified lines
951983 BPF_MAP_TYPE_BLOOM_FILTER,
952984 BPF_MAP_TYPE_USER_RINGBUF,
953985 BPF_MAP_TYPE_CGRP_STORAGE,
986+ __MAX_BPF_MAP_TYPE
954987 };
955988
956989 /* Note that tracing related programs such as
@@ -995,6 +1028,7 @@ enum bpf_prog_type { · 38 unmodified lines
9951028 BPF_PROG_TYPE_SK_LOOKUP,
9961029 BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
9971030 BPF_PROG_TYPE_NETFILTER,
1031+ __MAX_BPF_PROG_TYPE
9981032 };
9991033
10001034 enum bpf_attach_type {
@@ -1401,6 +1435,7 @@ union bpf_attr { · 400 unmodified lines
14011435 * to using 5 hash functions).
14021436 */
14031437 __u64 map_extra;
1438+ __u32 map_token_fd;
14041439 };
14051440
14061441 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
@@ -1470,6 +1505,7 @@ union bpf_attr { · 63 unmodified lines
14701505 * truncated), or smaller (if log buffer wasn't filled completely).
14711506 */
14721507 __u32 log_true_size;
1508+ __u32 prog_token_fd;
14731509 };
14741510
14751511 struct { /* anonymous struct used by BPF_OBJ_* commands */
@@ -1582,6 +1618,7 @@ union bpf_attr { · 106 unmodified lines
15821618 * truncated), or smaller (if log buffer wasn't filled completely).
15831619 */
15841620 __u32 btf_log_true_size;
1621+ __u32 btf_token_fd;
15851622 };
15861623
15871624 struct {
@@ -1712,6 +1749,11 @@ union bpf_attr { · 124 unmodified lines
17121749 __u32 flags; /* extra flags */
17131750 } prog_bind_map;
17141751
1752+ struct { /* struct used by BPF_TOKEN_CREATE command */
1753+ __u32 flags;
1754+ __u32 bpffs_fd;
1755+ } token_create;
1756+
17151757 } __attribute__((aligned(8)));
17161758
17171759 /* 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
169169 __u32 max_entries,
170170 const struct bpf_map_create_opts *opts)
171171 {
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);
173173 union bpf_attr attr;
174174 int fd;
175175
@@ -198,6 +198,8 @@ int bpf_map_create(enum bpf_map_type map_type, · 22 unmodified lines
198198 attr.numa_node = OPTS_GET(opts, numa_node, 0);
199199 attr.map_ifindex = OPTS_GET(opts, map_ifindex, 0);
200200
201+ attr.map_token_fd = OPTS_GET(opts, token_fd, 0);
202+
201203 fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
202204 return libbpf_err_errno(fd);
203205 }
@@ -232,7 +234,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, · 28 unmodified lines
232234 const struct bpf_insn *insns, size_t insn_cnt,
233235 struct bpf_prog_load_opts *opts)
234236 {
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);
236238 void *finfo = NULL, *linfo = NULL;
237239 const char *func_info, *line_info;
238240 __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
261263 attr.prog_flags = OPTS_GET(opts, prog_flags, 0);
262264 attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
263265 attr.kern_version = OPTS_GET(opts, kern_version, 0);
266+ attr.prog_token_fd = OPTS_GET(opts, token_fd, 0);
264267
265268 if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
266269 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
11821185
11831186 int bpf_btf_load(const void *btf_data, size_t btf_size, struct bpf_btf_load_opts *opts)
11841187 {
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);
11861189 union bpf_attr attr;
11871190 char *log_buf;
11881191 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
12071210
12081211 attr.btf = ptr_to_u64(btf_data);
12091212 attr.btf_size = btf_size;
1213+ attr.btf_token_fd = OPTS_GET(opts, token_fd, 0);
1214+
12101215 /* log_level == 0 and log_buf != NULL means "try loading without
12111216 * log_buf, but retry with log_buf and log_level=1 on error", which is
12121217 * 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
12871292 ret = sys_bpf(BPF_PROG_BIND_MAP, &attr, attr_sz);
12881293 return libbpf_err_errno(ret);
12891294 }
1295+
1296+int 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
5151
5252 __u32 numa_node;
5353 __u32 map_ifindex;
54+
55+ __u32 token_fd;
56+ size_t :0;
5457 };
55−#define bpf_map_create_opts__last_field map_ifindex
58+#define bpf_map_create_opts__last_field token_fd
5659
5760 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
5861 const char *map_name,
@@ -102,9 +105,10 @@ struct bpf_prog_load_opts { · 43 unmodified lines
102105 * If kernel doesn't support this feature, log_size is left unchanged.
103106 */
104107 __u32 log_true_size;
108+ __u32 token_fd;
105109 size_t :0;
106110 };
107−#define bpf_prog_load_opts__last_field log_true_size
111+#define bpf_prog_load_opts__last_field token_fd
108112
109113 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
110114 const char *prog_name, const char *license,
@@ -130,9 +134,10 @@ struct bpf_btf_load_opts { · 19 unmodified lines
130134 * If kernel doesn't support this feature, log_size is left unchanged.
131135 */
132136 __u32 log_true_size;
137+ __u32 token_fd;
133138 size_t :0;
134139 };
135−#define bpf_btf_load_opts__last_field log_true_size
140+#define bpf_btf_load_opts__last_field token_fd
136141
137142 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
138143 struct bpf_btf_load_opts *opts);
@@ -640,6 +645,30 @@ struct bpf_test_run_opts { · 501 unmodified lines
640645 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
641646 struct bpf_test_run_opts *opts);
642647
648+struct 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+ */
669+LIBBPF_API int bpf_token_create(int bpffs_fd,
670+ struct bpf_token_create_opts *opts);
671+
643672 #ifdef __cplusplus
644673 } /* extern "C" */
645674 #endif
tools/lib/bpf/libbpf.map+1−0
@@ -401,6 +401,7 @@ LIBBPF_1.3.0 { · 400 unmodified lines
401401 bpf_program__attach_netkit;
402402 bpf_program__attach_tcx;
403403 bpf_program__attach_uprobe_multi;
404+ bpf_token_create;
404405 ring__avail_data_size;
405406 ring__consume;
406407 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
3030
3131 if (prog_type == BPF_PROG_TYPE_UNSPEC)
3232 continue;
33+ if (strcmp(prog_type_name, "__MAX_BPF_PROG_TYPE") == 0)
34+ continue;
3335
3436 if (!test__start_subtest(prog_type_name))
3537 continue;
@@ -68,6 +70,8 @@ void test_libbpf_probe_map_types(void) · 32 unmodified lines
6870
6971 if (map_type == BPF_MAP_TYPE_UNSPEC)
7072 continue;
73+ if (strcmp(map_type_name, "__MAX_BPF_MAP_TYPE") == 0)
74+ continue;
7175
7276 if (!test__start_subtest(map_type_name))
7377 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
132132 const char *map_type_str;
133133 char buf[256];
134134
135+ if (map_type == __MAX_BPF_MAP_TYPE)
136+ continue;
137+
135138 map_type_name = btf__str_by_offset(btf, e->name_off);
136139 map_type_str = libbpf_bpf_map_type_str(map_type);
137140 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
186189 const char *prog_type_str;
187190 char buf[256];
188191
192+ if (prog_type == __MAX_BPF_PROG_TYPE)
193+ continue;
194+
189195 prog_type_name = btf__str_by_offset(btf, e->name_off);
190196 prog_type_str = libbpf_bpf_prog_type_str(prog_type);
191197 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+
18+static 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+
25+static inline int sys_fsopen(const char *fsname, unsigned flags)
26+{
27+ return syscall(__NR_fsopen, fsname, flags);
28+}
29+
30+static inline int sys_fspick(int dfd, const char *path, unsigned flags)
31+{
32+ return syscall(__NR_fspick, dfd, path, flags);
33+}
34+
35+static 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+
40+static 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+
45+static 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+
53+static int restore_priv_caps(__u64 old_caps)
54+{
55+ return cap_enable_effective(old_caps, NULL);
56+}
57+
58+static 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+
73+struct bpffs_opts {
74+ __u64 cmds;
75+ __u64 maps;
76+ __u64 progs;
77+ __u64 attachs;
78+};
79+
80+static 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+
91+static 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 */
123+static 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 */
158+static 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+
197+static 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+
208+static 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+
225+static 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+
259+typedef int (*child_callback_fn)(int);
260+
261+static 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;
351+cleanup:
352+ zclose(sock_fd);
353+ zclose(mnt_fd);
354+ zclose(fs_fd);
355+ zclose(bpffs_fd);
356+
357+ exit(-err);
358+}
359+
360+static int wait_for_pid(pid_t pid)
361+{
362+ int status, ret;
363+
364+again:
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+
379+static 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+
403+cleanup:
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+
412+static 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+
434+cleanup:
435+ zclose(sock_fds[0]);
436+ zclose(sock_fds[1]);
437+ if (child_pid > 0)
438+ (void)kill(child_pid, SIGKILL);
439+}
440+
441+static 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+
499+cleanup:
500+ zclose(token_fd);
501+ zclose(map_fd);
502+ return err;
503+}
504+
505+static 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;
564+cleanup:
565+ btf__free(btf);
566+ zclose(btf_fd);
567+ zclose(token_fd);
568+ return err;
569+}
570+
571+static 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;
640+cleanup:
641+ zclose(prog_fd);
642+ zclose(token_fd);
643+ return err;
644+}
645+
646+void 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