mirror of
https://github.com/AtlasLinux/linux.git
synced 2026-02-02 15:22:09 -08:00
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). ==================== Link: https://lore.kernel.org/r/20231130185229.2688956-1-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
@@ -110,7 +110,7 @@ lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_get_prandom_u32:
|
||||
return &bpf_get_prandom_u32_proto;
|
||||
case BPF_FUNC_trace_printk:
|
||||
if (perfmon_capable())
|
||||
if (bpf_token_capable(prog->aux->token, CAP_PERFMON))
|
||||
return bpf_get_trace_printk_proto();
|
||||
fallthrough;
|
||||
default:
|
||||
|
||||
@@ -51,6 +51,10 @@ struct module;
|
||||
struct bpf_func_state;
|
||||
struct ftrace_ops;
|
||||
struct cgroup;
|
||||
struct bpf_token;
|
||||
struct user_namespace;
|
||||
struct super_block;
|
||||
struct inode;
|
||||
|
||||
extern struct idr btf_idr;
|
||||
extern spinlock_t btf_idr_lock;
|
||||
@@ -1457,6 +1461,7 @@ struct bpf_prog_aux {
|
||||
#ifdef CONFIG_SECURITY
|
||||
void *security;
|
||||
#endif
|
||||
struct bpf_token *token;
|
||||
struct bpf_prog_offload *offload;
|
||||
struct btf *btf;
|
||||
struct bpf_func_info *func_info;
|
||||
@@ -1581,6 +1586,29 @@ struct bpf_link_primer {
|
||||
u32 id;
|
||||
};
|
||||
|
||||
struct bpf_mount_opts {
|
||||
umode_t mode;
|
||||
|
||||
/* BPF token-related delegation options */
|
||||
u64 delegate_cmds;
|
||||
u64 delegate_maps;
|
||||
u64 delegate_progs;
|
||||
u64 delegate_attachs;
|
||||
};
|
||||
|
||||
struct bpf_token {
|
||||
struct work_struct work;
|
||||
atomic64_t refcnt;
|
||||
struct user_namespace *userns;
|
||||
u64 allowed_cmds;
|
||||
u64 allowed_maps;
|
||||
u64 allowed_progs;
|
||||
u64 allowed_attachs;
|
||||
#ifdef CONFIG_SECURITY
|
||||
void *security;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct bpf_struct_ops_value;
|
||||
struct btf_member;
|
||||
|
||||
@@ -2038,6 +2066,7 @@ static inline void bpf_enable_instrumentation(void)
|
||||
migrate_enable();
|
||||
}
|
||||
|
||||
extern const struct super_operations bpf_super_ops;
|
||||
extern const struct file_operations bpf_map_fops;
|
||||
extern const struct file_operations bpf_prog_fops;
|
||||
extern const struct file_operations bpf_iter_fops;
|
||||
@@ -2172,24 +2201,26 @@ static inline void bpf_map_dec_elem_count(struct bpf_map *map)
|
||||
|
||||
extern int sysctl_unprivileged_bpf_disabled;
|
||||
|
||||
static inline bool bpf_allow_ptr_leaks(void)
|
||||
bool bpf_token_capable(const struct bpf_token *token, int cap);
|
||||
|
||||
static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)
|
||||
{
|
||||
return perfmon_capable();
|
||||
return bpf_token_capable(token, CAP_PERFMON);
|
||||
}
|
||||
|
||||
static inline bool bpf_allow_uninit_stack(void)
|
||||
static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)
|
||||
{
|
||||
return perfmon_capable();
|
||||
return bpf_token_capable(token, CAP_PERFMON);
|
||||
}
|
||||
|
||||
static inline bool bpf_bypass_spec_v1(void)
|
||||
static inline bool bpf_bypass_spec_v1(const struct bpf_token *token)
|
||||
{
|
||||
return cpu_mitigations_off() || perfmon_capable();
|
||||
return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
|
||||
}
|
||||
|
||||
static inline bool bpf_bypass_spec_v4(void)
|
||||
static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
|
||||
{
|
||||
return cpu_mitigations_off() || perfmon_capable();
|
||||
return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
|
||||
}
|
||||
|
||||
int bpf_map_new_fd(struct bpf_map *map, int flags);
|
||||
@@ -2206,8 +2237,21 @@ int bpf_link_new_fd(struct bpf_link *link);
|
||||
struct bpf_link *bpf_link_get_from_fd(u32 ufd);
|
||||
struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
|
||||
|
||||
void bpf_token_inc(struct bpf_token *token);
|
||||
void bpf_token_put(struct bpf_token *token);
|
||||
int bpf_token_create(union bpf_attr *attr);
|
||||
struct bpf_token *bpf_token_get_from_fd(u32 ufd);
|
||||
|
||||
bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
|
||||
bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
|
||||
bool bpf_token_allow_prog_type(const struct bpf_token *token,
|
||||
enum bpf_prog_type prog_type,
|
||||
enum bpf_attach_type attach_type);
|
||||
|
||||
int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);
|
||||
int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);
|
||||
struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir,
|
||||
umode_t mode);
|
||||
|
||||
#define BPF_ITER_FUNC_PREFIX "bpf_iter_"
|
||||
#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
|
||||
struct bpf_prog *bpf_prog_by_id(u32 id);
|
||||
struct bpf_link *bpf_link_by_id(u32 id);
|
||||
|
||||
const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
|
||||
const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
|
||||
const struct bpf_prog *prog);
|
||||
void bpf_task_storage_free(struct task_struct *task);
|
||||
void bpf_cgrp_storage_free(struct cgroup *cgroup);
|
||||
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)
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
static inline bool bpf_token_capable(const struct bpf_token *token, int cap)
|
||||
{
|
||||
return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
|
||||
}
|
||||
|
||||
static inline void bpf_token_inc(struct bpf_token *token)
|
||||
{
|
||||
}
|
||||
|
||||
static inline void bpf_token_put(struct bpf_token *token)
|
||||
{
|
||||
}
|
||||
|
||||
static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
|
||||
{
|
||||
return ERR_PTR(-EOPNOTSUPP);
|
||||
}
|
||||
|
||||
static inline void __dev_flush(void)
|
||||
{
|
||||
}
|
||||
@@ -2693,7 +2756,7 @@ static inline int btf_struct_access(struct bpf_verifier_log *log,
|
||||
}
|
||||
|
||||
static inline const struct bpf_func_proto *
|
||||
bpf_base_func_proto(enum bpf_func_id func_id)
|
||||
bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -1139,7 +1139,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
|
||||
return false;
|
||||
if (!bpf_jit_harden)
|
||||
return false;
|
||||
if (bpf_jit_harden == 1 && bpf_capable())
|
||||
if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -398,10 +398,17 @@ LSM_HOOK(void, LSM_RET_VOID, audit_rule_free, void *lsmrule)
|
||||
LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size)
|
||||
LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode)
|
||||
LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog)
|
||||
LSM_HOOK(int, 0, bpf_map_alloc_security, struct bpf_map *map)
|
||||
LSM_HOOK(void, LSM_RET_VOID, bpf_map_free_security, struct bpf_map *map)
|
||||
LSM_HOOK(int, 0, bpf_prog_alloc_security, struct bpf_prog_aux *aux)
|
||||
LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free_security, struct bpf_prog_aux *aux)
|
||||
LSM_HOOK(int, 0, bpf_map_create, struct bpf_map *map, union bpf_attr *attr,
|
||||
struct bpf_token *token)
|
||||
LSM_HOOK(void, LSM_RET_VOID, bpf_map_free, struct bpf_map *map)
|
||||
LSM_HOOK(int, 0, bpf_prog_load, struct bpf_prog *prog, union bpf_attr *attr,
|
||||
struct bpf_token *token)
|
||||
LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free, struct bpf_prog *prog)
|
||||
LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr,
|
||||
struct path *path)
|
||||
LSM_HOOK(void, LSM_RET_VOID, bpf_token_free, struct bpf_token *token)
|
||||
LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
|
||||
LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
|
||||
#endif /* CONFIG_BPF_SYSCALL */
|
||||
|
||||
LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include <linux/string.h>
|
||||
#include <linux/mm.h>
|
||||
#include <linux/sockptr.h>
|
||||
#include <linux/bpf.h>
|
||||
|
||||
struct linux_binprm;
|
||||
struct cred;
|
||||
@@ -2020,15 +2021,22 @@ static inline void securityfs_remove(struct dentry *dentry)
|
||||
union bpf_attr;
|
||||
struct bpf_map;
|
||||
struct bpf_prog;
|
||||
struct bpf_prog_aux;
|
||||
struct bpf_token;
|
||||
#ifdef CONFIG_SECURITY
|
||||
extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size);
|
||||
extern int security_bpf_map(struct bpf_map *map, fmode_t fmode);
|
||||
extern int security_bpf_prog(struct bpf_prog *prog);
|
||||
extern int security_bpf_map_alloc(struct bpf_map *map);
|
||||
extern int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
|
||||
struct bpf_token *token);
|
||||
extern void security_bpf_map_free(struct bpf_map *map);
|
||||
extern int security_bpf_prog_alloc(struct bpf_prog_aux *aux);
|
||||
extern void security_bpf_prog_free(struct bpf_prog_aux *aux);
|
||||
extern int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
|
||||
struct bpf_token *token);
|
||||
extern void security_bpf_prog_free(struct bpf_prog *prog);
|
||||
extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
|
||||
struct path *path);
|
||||
extern void security_bpf_token_free(struct bpf_token *token);
|
||||
extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
|
||||
extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
|
||||
#else
|
||||
static inline int security_bpf(int cmd, union bpf_attr *attr,
|
||||
unsigned int size)
|
||||
@@ -2046,7 +2054,8 @@ static inline int security_bpf_prog(struct bpf_prog *prog)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int security_bpf_map_alloc(struct bpf_map *map)
|
||||
static inline int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
|
||||
struct bpf_token *token)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -2054,13 +2063,33 @@ static inline int security_bpf_map_alloc(struct bpf_map *map)
|
||||
static inline void security_bpf_map_free(struct bpf_map *map)
|
||||
{ }
|
||||
|
||||
static inline int security_bpf_prog_alloc(struct bpf_prog_aux *aux)
|
||||
static inline int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
|
||||
struct bpf_token *token)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)
|
||||
static inline void security_bpf_prog_free(struct bpf_prog *prog)
|
||||
{ }
|
||||
|
||||
static inline int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
|
||||
struct path *path)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void security_bpf_token_free(struct bpf_token *token)
|
||||
{ }
|
||||
|
||||
static inline int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int security_bpf_token_capable(const struct bpf_token *token, int cap)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif /* CONFIG_SECURITY */
|
||||
#endif /* CONFIG_BPF_SYSCALL */
|
||||
|
||||
|
||||
@@ -847,6 +847,36 @@ union bpf_iter_link_info {
|
||||
* Returns zero on success. On error, -1 is returned and *errno*
|
||||
* is set appropriately.
|
||||
*
|
||||
* BPF_TOKEN_CREATE
|
||||
* Description
|
||||
* Create BPF token with embedded information about what
|
||||
* BPF-related functionality it allows:
|
||||
* - a set of allowed bpf() syscall commands;
|
||||
* - a set of allowed BPF map types to be created with
|
||||
* BPF_MAP_CREATE command, if BPF_MAP_CREATE itself is allowed;
|
||||
* - a set of allowed BPF program types and BPF program attach
|
||||
* types to be loaded with BPF_PROG_LOAD command, if
|
||||
* BPF_PROG_LOAD itself is allowed.
|
||||
*
|
||||
* BPF token is created (derived) from an instance of BPF FS,
|
||||
* assuming it has necessary delegation mount options specified.
|
||||
* This BPF token can be passed as an extra parameter to various
|
||||
* bpf() syscall commands to grant BPF subsystem functionality to
|
||||
* unprivileged processes.
|
||||
*
|
||||
* When created, BPF token is "associated" with the owning
|
||||
* user namespace of BPF FS instance (super block) that it was
|
||||
* derived from, and subsequent BPF operations performed with
|
||||
* BPF token would be performing capabilities checks (i.e.,
|
||||
* CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN) within
|
||||
* that user namespace. Without BPF token, such capabilities
|
||||
* have to be granted in init user namespace, making bpf()
|
||||
* syscall incompatible with user namespace, for the most part.
|
||||
*
|
||||
* Return
|
||||
* A new file descriptor (a nonnegative integer), or -1 if an
|
||||
* error occurred (in which case, *errno* is set appropriately).
|
||||
*
|
||||
* NOTES
|
||||
* eBPF objects (maps and programs) can be shared between processes.
|
||||
*
|
||||
@@ -901,6 +931,8 @@ enum bpf_cmd {
|
||||
BPF_ITER_CREATE,
|
||||
BPF_LINK_DETACH,
|
||||
BPF_PROG_BIND_MAP,
|
||||
BPF_TOKEN_CREATE,
|
||||
__MAX_BPF_CMD,
|
||||
};
|
||||
|
||||
enum bpf_map_type {
|
||||
@@ -951,6 +983,7 @@ enum bpf_map_type {
|
||||
BPF_MAP_TYPE_BLOOM_FILTER,
|
||||
BPF_MAP_TYPE_USER_RINGBUF,
|
||||
BPF_MAP_TYPE_CGRP_STORAGE,
|
||||
__MAX_BPF_MAP_TYPE
|
||||
};
|
||||
|
||||
/* Note that tracing related programs such as
|
||||
@@ -995,6 +1028,7 @@ enum bpf_prog_type {
|
||||
BPF_PROG_TYPE_SK_LOOKUP,
|
||||
BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
|
||||
BPF_PROG_TYPE_NETFILTER,
|
||||
__MAX_BPF_PROG_TYPE
|
||||
};
|
||||
|
||||
enum bpf_attach_type {
|
||||
@@ -1401,6 +1435,7 @@ union bpf_attr {
|
||||
* to using 5 hash functions).
|
||||
*/
|
||||
__u64 map_extra;
|
||||
__u32 map_token_fd;
|
||||
};
|
||||
|
||||
struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
|
||||
@@ -1470,6 +1505,7 @@ union bpf_attr {
|
||||
* truncated), or smaller (if log buffer wasn't filled completely).
|
||||
*/
|
||||
__u32 log_true_size;
|
||||
__u32 prog_token_fd;
|
||||
};
|
||||
|
||||
struct { /* anonymous struct used by BPF_OBJ_* commands */
|
||||
@@ -1582,6 +1618,7 @@ union bpf_attr {
|
||||
* truncated), or smaller (if log buffer wasn't filled completely).
|
||||
*/
|
||||
__u32 btf_log_true_size;
|
||||
__u32 btf_token_fd;
|
||||
};
|
||||
|
||||
struct {
|
||||
@@ -1712,6 +1749,11 @@ union bpf_attr {
|
||||
__u32 flags; /* extra flags */
|
||||
} prog_bind_map;
|
||||
|
||||
struct { /* struct used by BPF_TOKEN_CREATE command */
|
||||
__u32 flags;
|
||||
__u32 bpffs_fd;
|
||||
} token_create;
|
||||
|
||||
} __attribute__((aligned(8)));
|
||||
|
||||
/* The description below is an attempt at providing documentation to eBPF
|
||||
|
||||
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse
|
||||
endif
|
||||
CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
|
||||
|
||||
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o
|
||||
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o
|
||||
obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o
|
||||
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
|
||||
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
|
||||
|
||||
@@ -82,7 +82,7 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr)
|
||||
bool percpu = attr->map_type == BPF_MAP_TYPE_PERCPU_ARRAY;
|
||||
int numa_node = bpf_map_attr_numa_node(attr);
|
||||
u32 elem_size, index_mask, max_entries;
|
||||
bool bypass_spec_v1 = bpf_bypass_spec_v1();
|
||||
bool bypass_spec_v1 = bpf_bypass_spec_v1(NULL);
|
||||
u64 array_size, mask64;
|
||||
struct bpf_array *array;
|
||||
|
||||
|
||||
@@ -260,9 +260,15 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
BTF_SET_START(sleepable_lsm_hooks)
|
||||
BTF_ID(func, bpf_lsm_bpf)
|
||||
BTF_ID(func, bpf_lsm_bpf_map)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_alloc_security)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_free_security)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_create)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_free)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog_load)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog_free)
|
||||
BTF_ID(func, bpf_lsm_bpf_token_create)
|
||||
BTF_ID(func, bpf_lsm_bpf_token_free)
|
||||
BTF_ID(func, bpf_lsm_bpf_token_cmd)
|
||||
BTF_ID(func, bpf_lsm_bpf_token_capable)
|
||||
BTF_ID(func, bpf_lsm_bprm_check_security)
|
||||
BTF_ID(func, bpf_lsm_bprm_committed_creds)
|
||||
BTF_ID(func, bpf_lsm_bprm_committing_creds)
|
||||
@@ -345,9 +351,8 @@ BTF_ID(func, bpf_lsm_userns_create)
|
||||
BTF_SET_END(sleepable_lsm_hooks)
|
||||
|
||||
BTF_SET_START(untrusted_lsm_hooks)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_free_security)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog_alloc_security)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog_free_security)
|
||||
BTF_ID(func, bpf_lsm_bpf_map_free)
|
||||
BTF_ID(func, bpf_lsm_bpf_prog_free)
|
||||
BTF_ID(func, bpf_lsm_file_alloc_security)
|
||||
BTF_ID(func, bpf_lsm_file_free_security)
|
||||
#ifdef CONFIG_SECURITY_NETWORK
|
||||
|
||||
@@ -1630,7 +1630,7 @@ cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_perf_event_output:
|
||||
return &bpf_event_output_data_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2191,7 +2191,7 @@ sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_perf_event_output:
|
||||
return &bpf_event_output_data_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2348,7 +2348,7 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_perf_event_output:
|
||||
return &bpf_event_output_data_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -675,7 +675,7 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp)
|
||||
void bpf_prog_kallsyms_add(struct bpf_prog *fp)
|
||||
{
|
||||
if (!bpf_prog_kallsyms_candidate(fp) ||
|
||||
!bpf_capable())
|
||||
!bpf_token_capable(fp->aux->token, CAP_BPF))
|
||||
return;
|
||||
|
||||
bpf_prog_ksym_set_addr(fp);
|
||||
@@ -2751,6 +2751,7 @@ void bpf_prog_free(struct bpf_prog *fp)
|
||||
|
||||
if (aux->dst_prog)
|
||||
bpf_prog_put(aux->dst_prog);
|
||||
bpf_token_put(aux->token);
|
||||
INIT_WORK(&aux->work, bpf_prog_free_deferred);
|
||||
schedule_work(&aux->work);
|
||||
}
|
||||
|
||||
@@ -1679,7 +1679,7 @@ const struct bpf_func_proto bpf_probe_read_kernel_str_proto __weak;
|
||||
const struct bpf_func_proto bpf_task_pt_regs_proto __weak;
|
||||
|
||||
const struct bpf_func_proto *
|
||||
bpf_base_func_proto(enum bpf_func_id func_id)
|
||||
bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
{
|
||||
switch (func_id) {
|
||||
case BPF_FUNC_map_lookup_elem:
|
||||
@@ -1730,7 +1730,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!bpf_capable())
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_BPF))
|
||||
return NULL;
|
||||
|
||||
switch (func_id) {
|
||||
@@ -1788,7 +1788,7 @@ bpf_base_func_proto(enum bpf_func_id func_id)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!perfmon_capable())
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
|
||||
return NULL;
|
||||
|
||||
switch (func_id) {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <linux/filter.h>
|
||||
#include <linux/bpf.h>
|
||||
#include <linux/bpf_trace.h>
|
||||
#include <linux/kstrtox.h>
|
||||
#include "preload/bpf_preload.h"
|
||||
|
||||
enum bpf_type {
|
||||
@@ -98,9 +99,9 @@ static const struct inode_operations bpf_prog_iops = { };
|
||||
static const struct inode_operations bpf_map_iops = { };
|
||||
static const struct inode_operations bpf_link_iops = { };
|
||||
|
||||
static struct inode *bpf_get_inode(struct super_block *sb,
|
||||
const struct inode *dir,
|
||||
umode_t mode)
|
||||
struct inode *bpf_get_inode(struct super_block *sb,
|
||||
const struct inode *dir,
|
||||
umode_t mode)
|
||||
{
|
||||
struct inode *inode;
|
||||
|
||||
@@ -599,10 +600,36 @@ EXPORT_SYMBOL(bpf_prog_get_type_path);
|
||||
*/
|
||||
static int bpf_show_options(struct seq_file *m, struct dentry *root)
|
||||
{
|
||||
struct bpf_mount_opts *opts = root->d_sb->s_fs_info;
|
||||
umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
|
||||
u64 mask;
|
||||
|
||||
if (mode != S_IRWXUGO)
|
||||
seq_printf(m, ",mode=%o", mode);
|
||||
|
||||
mask = (1ULL << __MAX_BPF_CMD) - 1;
|
||||
if ((opts->delegate_cmds & mask) == mask)
|
||||
seq_printf(m, ",delegate_cmds=any");
|
||||
else if (opts->delegate_cmds)
|
||||
seq_printf(m, ",delegate_cmds=0x%llx", opts->delegate_cmds);
|
||||
|
||||
mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1;
|
||||
if ((opts->delegate_maps & mask) == mask)
|
||||
seq_printf(m, ",delegate_maps=any");
|
||||
else if (opts->delegate_maps)
|
||||
seq_printf(m, ",delegate_maps=0x%llx", opts->delegate_maps);
|
||||
|
||||
mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1;
|
||||
if ((opts->delegate_progs & mask) == mask)
|
||||
seq_printf(m, ",delegate_progs=any");
|
||||
else if (opts->delegate_progs)
|
||||
seq_printf(m, ",delegate_progs=0x%llx", opts->delegate_progs);
|
||||
|
||||
mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1;
|
||||
if ((opts->delegate_attachs & mask) == mask)
|
||||
seq_printf(m, ",delegate_attachs=any");
|
||||
else if (opts->delegate_attachs)
|
||||
seq_printf(m, ",delegate_attachs=0x%llx", opts->delegate_attachs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -617,7 +644,7 @@ static void bpf_free_inode(struct inode *inode)
|
||||
free_inode_nonrcu(inode);
|
||||
}
|
||||
|
||||
static const struct super_operations bpf_super_ops = {
|
||||
const struct super_operations bpf_super_ops = {
|
||||
.statfs = simple_statfs,
|
||||
.drop_inode = generic_delete_inode,
|
||||
.show_options = bpf_show_options,
|
||||
@@ -626,22 +653,27 @@ static const struct super_operations bpf_super_ops = {
|
||||
|
||||
enum {
|
||||
OPT_MODE,
|
||||
OPT_DELEGATE_CMDS,
|
||||
OPT_DELEGATE_MAPS,
|
||||
OPT_DELEGATE_PROGS,
|
||||
OPT_DELEGATE_ATTACHS,
|
||||
};
|
||||
|
||||
static const struct fs_parameter_spec bpf_fs_parameters[] = {
|
||||
fsparam_u32oct ("mode", OPT_MODE),
|
||||
fsparam_string ("delegate_cmds", OPT_DELEGATE_CMDS),
|
||||
fsparam_string ("delegate_maps", OPT_DELEGATE_MAPS),
|
||||
fsparam_string ("delegate_progs", OPT_DELEGATE_PROGS),
|
||||
fsparam_string ("delegate_attachs", OPT_DELEGATE_ATTACHS),
|
||||
{}
|
||||
};
|
||||
|
||||
struct bpf_mount_opts {
|
||||
umode_t mode;
|
||||
};
|
||||
|
||||
static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
|
||||
{
|
||||
struct bpf_mount_opts *opts = fc->fs_private;
|
||||
struct bpf_mount_opts *opts = fc->s_fs_info;
|
||||
struct fs_parse_result result;
|
||||
int opt;
|
||||
int opt, err;
|
||||
u64 msk;
|
||||
|
||||
opt = fs_parse(fc, bpf_fs_parameters, param, &result);
|
||||
if (opt < 0) {
|
||||
@@ -665,6 +697,28 @@ static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
|
||||
case OPT_MODE:
|
||||
opts->mode = result.uint_32 & S_IALLUGO;
|
||||
break;
|
||||
case OPT_DELEGATE_CMDS:
|
||||
case OPT_DELEGATE_MAPS:
|
||||
case OPT_DELEGATE_PROGS:
|
||||
case OPT_DELEGATE_ATTACHS:
|
||||
if (strcmp(param->string, "any") == 0) {
|
||||
msk = ~0ULL;
|
||||
} else {
|
||||
err = kstrtou64(param->string, 0, &msk);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
/* Setting delegation mount options requires privileges */
|
||||
if (msk && !capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
switch (opt) {
|
||||
case OPT_DELEGATE_CMDS: opts->delegate_cmds |= msk; break;
|
||||
case OPT_DELEGATE_MAPS: opts->delegate_maps |= msk; break;
|
||||
case OPT_DELEGATE_PROGS: opts->delegate_progs |= msk; break;
|
||||
case OPT_DELEGATE_ATTACHS: opts->delegate_attachs |= msk; break;
|
||||
default: return -EINVAL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -739,10 +793,14 @@ out:
|
||||
static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
|
||||
{
|
||||
static const struct tree_descr bpf_rfiles[] = { { "" } };
|
||||
struct bpf_mount_opts *opts = fc->fs_private;
|
||||
struct bpf_mount_opts *opts = sb->s_fs_info;
|
||||
struct inode *inode;
|
||||
int ret;
|
||||
|
||||
/* Mounting an instance of BPF FS requires privileges */
|
||||
if (fc->user_ns != &init_user_ns && !capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
|
||||
ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
|
||||
if (ret)
|
||||
return ret;
|
||||
@@ -764,7 +822,7 @@ static int bpf_get_tree(struct fs_context *fc)
|
||||
|
||||
static void bpf_free_fc(struct fs_context *fc)
|
||||
{
|
||||
kfree(fc->fs_private);
|
||||
kfree(fc->s_fs_info);
|
||||
}
|
||||
|
||||
static const struct fs_context_operations bpf_context_ops = {
|
||||
@@ -786,17 +844,32 @@ static int bpf_init_fs_context(struct fs_context *fc)
|
||||
|
||||
opts->mode = S_IRWXUGO;
|
||||
|
||||
fc->fs_private = opts;
|
||||
/* start out with no BPF token delegation enabled */
|
||||
opts->delegate_cmds = 0;
|
||||
opts->delegate_maps = 0;
|
||||
opts->delegate_progs = 0;
|
||||
opts->delegate_attachs = 0;
|
||||
|
||||
fc->s_fs_info = opts;
|
||||
fc->ops = &bpf_context_ops;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void bpf_kill_super(struct super_block *sb)
|
||||
{
|
||||
struct bpf_mount_opts *opts = sb->s_fs_info;
|
||||
|
||||
kill_litter_super(sb);
|
||||
kfree(opts);
|
||||
}
|
||||
|
||||
static struct file_system_type bpf_fs_type = {
|
||||
.owner = THIS_MODULE,
|
||||
.name = "bpf",
|
||||
.init_fs_context = bpf_init_fs_context,
|
||||
.parameters = bpf_fs_parameters,
|
||||
.kill_sb = kill_litter_super,
|
||||
.kill_sb = bpf_kill_super,
|
||||
.fs_flags = FS_USERNS_MOUNT,
|
||||
};
|
||||
|
||||
static int __init bpf_init(void)
|
||||
|
||||
@@ -1009,8 +1009,8 @@ int map_check_no_btf(const struct bpf_map *map,
|
||||
return -ENOTSUPP;
|
||||
}
|
||||
|
||||
static int map_check_btf(struct bpf_map *map, const struct btf *btf,
|
||||
u32 btf_key_id, u32 btf_value_id)
|
||||
static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
|
||||
const struct btf *btf, u32 btf_key_id, u32 btf_value_id)
|
||||
{
|
||||
const struct btf_type *key_type, *value_type;
|
||||
u32 key_size, value_size;
|
||||
@@ -1038,7 +1038,7 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,
|
||||
if (!IS_ERR_OR_NULL(map->record)) {
|
||||
int i;
|
||||
|
||||
if (!bpf_capable()) {
|
||||
if (!bpf_token_capable(token, CAP_BPF)) {
|
||||
ret = -EPERM;
|
||||
goto free_map_tab;
|
||||
}
|
||||
@@ -1121,11 +1121,17 @@ free_map_tab:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define BPF_MAP_CREATE_LAST_FIELD map_extra
|
||||
static bool bpf_net_capable(void)
|
||||
{
|
||||
return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
|
||||
}
|
||||
|
||||
#define BPF_MAP_CREATE_LAST_FIELD map_token_fd
|
||||
/* called via syscall */
|
||||
static int map_create(union bpf_attr *attr)
|
||||
{
|
||||
const struct bpf_map_ops *ops;
|
||||
struct bpf_token *token = NULL;
|
||||
int numa_node = bpf_map_attr_numa_node(attr);
|
||||
u32 map_type = attr->map_type;
|
||||
struct bpf_map *map;
|
||||
@@ -1176,14 +1182,32 @@ static int map_create(union bpf_attr *attr)
|
||||
if (!ops->map_mem_usage)
|
||||
return -EINVAL;
|
||||
|
||||
if (attr->map_token_fd) {
|
||||
token = bpf_token_get_from_fd(attr->map_token_fd);
|
||||
if (IS_ERR(token))
|
||||
return PTR_ERR(token);
|
||||
|
||||
/* if current token doesn't grant map creation permissions,
|
||||
* then we can't use this token, so ignore it and rely on
|
||||
* system-wide capabilities checks
|
||||
*/
|
||||
if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) ||
|
||||
!bpf_token_allow_map_type(token, attr->map_type)) {
|
||||
bpf_token_put(token);
|
||||
token = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
err = -EPERM;
|
||||
|
||||
/* Intent here is for unprivileged_bpf_disabled to block BPF map
|
||||
* creation for unprivileged users; other actions depend
|
||||
* on fd availability and access to bpffs, so are dependent on
|
||||
* object creation success. Even with unprivileged BPF disabled,
|
||||
* capability checks are still carried out.
|
||||
*/
|
||||
if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
|
||||
return -EPERM;
|
||||
if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF))
|
||||
goto put_token;
|
||||
|
||||
/* check privileged map type permissions */
|
||||
switch (map_type) {
|
||||
@@ -1216,25 +1240,27 @@ static int map_create(union bpf_attr *attr)
|
||||
case BPF_MAP_TYPE_LRU_PERCPU_HASH:
|
||||
case BPF_MAP_TYPE_STRUCT_OPS:
|
||||
case BPF_MAP_TYPE_CPUMAP:
|
||||
if (!bpf_capable())
|
||||
return -EPERM;
|
||||
if (!bpf_token_capable(token, CAP_BPF))
|
||||
goto put_token;
|
||||
break;
|
||||
case BPF_MAP_TYPE_SOCKMAP:
|
||||
case BPF_MAP_TYPE_SOCKHASH:
|
||||
case BPF_MAP_TYPE_DEVMAP:
|
||||
case BPF_MAP_TYPE_DEVMAP_HASH:
|
||||
case BPF_MAP_TYPE_XSKMAP:
|
||||
if (!capable(CAP_NET_ADMIN))
|
||||
return -EPERM;
|
||||
if (!bpf_token_capable(token, CAP_NET_ADMIN))
|
||||
goto put_token;
|
||||
break;
|
||||
default:
|
||||
WARN(1, "unsupported map type %d", map_type);
|
||||
return -EPERM;
|
||||
goto put_token;
|
||||
}
|
||||
|
||||
map = ops->map_alloc(attr);
|
||||
if (IS_ERR(map))
|
||||
return PTR_ERR(map);
|
||||
if (IS_ERR(map)) {
|
||||
err = PTR_ERR(map);
|
||||
goto put_token;
|
||||
}
|
||||
map->ops = ops;
|
||||
map->map_type = map_type;
|
||||
|
||||
@@ -1271,7 +1297,7 @@ static int map_create(union bpf_attr *attr)
|
||||
map->btf = btf;
|
||||
|
||||
if (attr->btf_value_type_id) {
|
||||
err = map_check_btf(map, btf, attr->btf_key_type_id,
|
||||
err = map_check_btf(map, token, btf, attr->btf_key_type_id,
|
||||
attr->btf_value_type_id);
|
||||
if (err)
|
||||
goto free_map;
|
||||
@@ -1283,15 +1309,16 @@ static int map_create(union bpf_attr *attr)
|
||||
attr->btf_vmlinux_value_type_id;
|
||||
}
|
||||
|
||||
err = security_bpf_map_alloc(map);
|
||||
err = security_bpf_map_create(map, attr, token);
|
||||
if (err)
|
||||
goto free_map;
|
||||
goto free_map_sec;
|
||||
|
||||
err = bpf_map_alloc_id(map);
|
||||
if (err)
|
||||
goto free_map_sec;
|
||||
|
||||
bpf_map_save_memcg(map);
|
||||
bpf_token_put(token);
|
||||
|
||||
err = bpf_map_new_fd(map, f_flags);
|
||||
if (err < 0) {
|
||||
@@ -1312,6 +1339,8 @@ free_map_sec:
|
||||
free_map:
|
||||
btf_put(map->btf);
|
||||
map->ops->map_free(map);
|
||||
put_token:
|
||||
bpf_token_put(token);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -2133,7 +2162,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
|
||||
kvfree(aux->func_info);
|
||||
kfree(aux->func_info_aux);
|
||||
free_uid(aux->user);
|
||||
security_bpf_prog_free(aux);
|
||||
security_bpf_prog_free(aux->prog);
|
||||
bpf_prog_free(aux->prog);
|
||||
}
|
||||
|
||||
@@ -2579,13 +2608,15 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
|
||||
}
|
||||
|
||||
/* last field in 'union bpf_attr' used by this command */
|
||||
#define BPF_PROG_LOAD_LAST_FIELD log_true_size
|
||||
#define BPF_PROG_LOAD_LAST_FIELD prog_token_fd
|
||||
|
||||
static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
|
||||
{
|
||||
enum bpf_prog_type type = attr->prog_type;
|
||||
struct bpf_prog *prog, *dst_prog = NULL;
|
||||
struct btf *attach_btf = NULL;
|
||||
struct bpf_token *token = NULL;
|
||||
bool bpf_cap;
|
||||
int err;
|
||||
char license[128];
|
||||
|
||||
@@ -2602,10 +2633,31 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
|
||||
BPF_F_TEST_REG_INVARIANTS))
|
||||
return -EINVAL;
|
||||
|
||||
bpf_prog_load_fixup_attach_type(attr);
|
||||
|
||||
if (attr->prog_token_fd) {
|
||||
token = bpf_token_get_from_fd(attr->prog_token_fd);
|
||||
if (IS_ERR(token))
|
||||
return PTR_ERR(token);
|
||||
/* if current token doesn't grant prog loading permissions,
|
||||
* then we can't use this token, so ignore it and rely on
|
||||
* system-wide capabilities checks
|
||||
*/
|
||||
if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) ||
|
||||
!bpf_token_allow_prog_type(token, attr->prog_type,
|
||||
attr->expected_attach_type)) {
|
||||
bpf_token_put(token);
|
||||
token = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bpf_cap = bpf_token_capable(token, CAP_BPF);
|
||||
err = -EPERM;
|
||||
|
||||
if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
|
||||
(attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
|
||||
!bpf_capable())
|
||||
return -EPERM;
|
||||
!bpf_cap)
|
||||
goto put_token;
|
||||
|
||||
/* Intent here is for unprivileged_bpf_disabled to block BPF program
|
||||
* 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)
|
||||
* capability checks are still carried out for these
|
||||
* and other operations.
|
||||
*/
|
||||
if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
|
||||
return -EPERM;
|
||||
if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
|
||||
goto put_token;
|
||||
|
||||
if (attr->insn_cnt == 0 ||
|
||||
attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
|
||||
return -E2BIG;
|
||||
attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) {
|
||||
err = -E2BIG;
|
||||
goto put_token;
|
||||
}
|
||||
if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
|
||||
type != BPF_PROG_TYPE_CGROUP_SKB &&
|
||||
!bpf_capable())
|
||||
return -EPERM;
|
||||
!bpf_cap)
|
||||
goto put_token;
|
||||
|
||||
if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
if (is_perfmon_prog_type(type) && !perfmon_capable())
|
||||
return -EPERM;
|
||||
if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN))
|
||||
goto put_token;
|
||||
if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON))
|
||||
goto put_token;
|
||||
|
||||
/* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
|
||||
* 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)
|
||||
if (IS_ERR(dst_prog)) {
|
||||
dst_prog = NULL;
|
||||
attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
|
||||
if (IS_ERR(attach_btf))
|
||||
return -EINVAL;
|
||||
if (IS_ERR(attach_btf)) {
|
||||
err = -EINVAL;
|
||||
goto put_token;
|
||||
}
|
||||
if (!btf_is_kernel(attach_btf)) {
|
||||
/* attaching through specifying bpf_prog's BTF
|
||||
* objects directly might be supported eventually
|
||||
*/
|
||||
btf_put(attach_btf);
|
||||
return -ENOTSUPP;
|
||||
err = -ENOTSUPP;
|
||||
goto put_token;
|
||||
}
|
||||
}
|
||||
} else if (attr->attach_btf_id) {
|
||||
/* fall back to vmlinux BTF, if BTF type ID is specified */
|
||||
attach_btf = bpf_get_btf_vmlinux();
|
||||
if (IS_ERR(attach_btf))
|
||||
return PTR_ERR(attach_btf);
|
||||
if (!attach_btf)
|
||||
return -EINVAL;
|
||||
if (IS_ERR(attach_btf)) {
|
||||
err = PTR_ERR(attach_btf);
|
||||
goto put_token;
|
||||
}
|
||||
if (!attach_btf) {
|
||||
err = -EINVAL;
|
||||
goto put_token;
|
||||
}
|
||||
btf_get(attach_btf);
|
||||
}
|
||||
|
||||
bpf_prog_load_fixup_attach_type(attr);
|
||||
if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
|
||||
attach_btf, attr->attach_btf_id,
|
||||
dst_prog)) {
|
||||
@@ -2666,7 +2726,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
|
||||
bpf_prog_put(dst_prog);
|
||||
if (attach_btf)
|
||||
btf_put(attach_btf);
|
||||
return -EINVAL;
|
||||
err = -EINVAL;
|
||||
goto put_token;
|
||||
}
|
||||
|
||||
/* plain bpf_prog allocation */
|
||||
@@ -2676,7 +2737,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
|
||||
bpf_prog_put(dst_prog);
|
||||
if (attach_btf)
|
||||
btf_put(attach_btf);
|
||||
return -ENOMEM;
|
||||
err = -EINVAL;
|
||||
goto put_token;
|
||||
}
|
||||
|
||||
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)
|
||||
prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
|
||||
prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
|
||||
|
||||
err = security_bpf_prog_alloc(prog->aux);
|
||||
if (err)
|
||||
goto free_prog;
|
||||
/* move token into prog->aux, reuse taken refcnt */
|
||||
prog->aux->token = token;
|
||||
token = NULL;
|
||||
|
||||
prog->aux->user = get_current_user();
|
||||
prog->len = attr->insn_cnt;
|
||||
@@ -2698,12 +2760,12 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
|
||||
if (copy_from_bpfptr(prog->insns,
|
||||
make_bpfptr(attr->insns, uattr.is_kernel),
|
||||
bpf_prog_insn_size(prog)) != 0)
|
||||
goto free_prog_sec;
|
||||
goto free_prog;
|
||||
/* copy eBPF program license from user space */
|
||||
if (strncpy_from_bpfptr(license,
|
||||
make_bpfptr(attr->license, uattr.is_kernel),
|
||||
sizeof(license) - 1) < 0)
|
||||
goto free_prog_sec;
|
||||
goto free_prog;
|
||||
license[sizeof(license) - 1] = 0;
|
||||
|
||||
/* 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)
|
||||
if (bpf_prog_is_dev_bound(prog->aux)) {
|
||||
err = bpf_prog_dev_bound_init(prog, attr);
|
||||
if (err)
|
||||
goto free_prog_sec;
|
||||
goto free_prog;
|
||||
}
|
||||
|
||||
if (type == BPF_PROG_TYPE_EXT && dst_prog &&
|
||||
bpf_prog_is_dev_bound(dst_prog->aux)) {
|
||||
err = bpf_prog_dev_bound_inherit(prog, dst_prog);
|
||||
if (err)
|
||||
goto free_prog_sec;
|
||||
goto free_prog;
|
||||
}
|
||||
|
||||
/* find program type: socket_filter vs tracing_filter */
|
||||
err = find_prog_type(type, prog);
|
||||
if (err < 0)
|
||||
goto free_prog_sec;
|
||||
goto free_prog;
|
||||
|
||||
prog->aux->load_time = ktime_get_boottime_ns();
|
||||
err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
|
||||
sizeof(attr->prog_name));
|
||||
if (err < 0)
|
||||
goto free_prog;
|
||||
|
||||
err = security_bpf_prog_load(prog, attr, token);
|
||||
if (err)
|
||||
goto free_prog_sec;
|
||||
|
||||
/* run eBPF verifier */
|
||||
@@ -2781,13 +2847,16 @@ free_used_maps:
|
||||
*/
|
||||
__bpf_prog_put_noref(prog, prog->aux->real_func_cnt);
|
||||
return err;
|
||||
|
||||
free_prog_sec:
|
||||
free_uid(prog->aux->user);
|
||||
security_bpf_prog_free(prog->aux);
|
||||
security_bpf_prog_free(prog);
|
||||
free_prog:
|
||||
free_uid(prog->aux->user);
|
||||
if (prog->aux->attach_btf)
|
||||
btf_put(prog->aux->attach_btf);
|
||||
bpf_prog_free(prog);
|
||||
put_token:
|
||||
bpf_token_put(token);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -3777,7 +3846,7 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
|
||||
case BPF_PROG_TYPE_SK_LOOKUP:
|
||||
return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
|
||||
case BPF_PROG_TYPE_CGROUP_SKB:
|
||||
if (!capable(CAP_NET_ADMIN))
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
|
||||
/* cg-skb progs can be loaded by unpriv user.
|
||||
* check permissions at attach time.
|
||||
*/
|
||||
@@ -3980,7 +4049,7 @@ static int bpf_prog_detach(const union bpf_attr *attr)
|
||||
static int bpf_prog_query(const union bpf_attr *attr,
|
||||
union bpf_attr __user *uattr)
|
||||
{
|
||||
if (!capable(CAP_NET_ADMIN))
|
||||
if (!bpf_net_capable())
|
||||
return -EPERM;
|
||||
if (CHECK_ATTR(BPF_PROG_QUERY))
|
||||
return -EINVAL;
|
||||
@@ -4748,15 +4817,31 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
|
||||
return err;
|
||||
}
|
||||
|
||||
#define BPF_BTF_LOAD_LAST_FIELD btf_log_true_size
|
||||
#define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
|
||||
|
||||
static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
|
||||
{
|
||||
struct bpf_token *token = NULL;
|
||||
|
||||
if (CHECK_ATTR(BPF_BTF_LOAD))
|
||||
return -EINVAL;
|
||||
|
||||
if (!bpf_capable())
|
||||
if (attr->btf_token_fd) {
|
||||
token = bpf_token_get_from_fd(attr->btf_token_fd);
|
||||
if (IS_ERR(token))
|
||||
return PTR_ERR(token);
|
||||
if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) {
|
||||
bpf_token_put(token);
|
||||
token = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!bpf_token_capable(token, CAP_BPF)) {
|
||||
bpf_token_put(token);
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
bpf_token_put(token);
|
||||
|
||||
return btf_new_fd(attr, uattr, uattr_size);
|
||||
}
|
||||
@@ -5372,6 +5457,20 @@ out_prog_put:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd
|
||||
|
||||
static int token_create(union bpf_attr *attr)
|
||||
{
|
||||
if (CHECK_ATTR(BPF_TOKEN_CREATE))
|
||||
return -EINVAL;
|
||||
|
||||
/* no flags are supported yet */
|
||||
if (attr->token_create.flags)
|
||||
return -EINVAL;
|
||||
|
||||
return bpf_token_create(attr);
|
||||
}
|
||||
|
||||
static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
|
||||
{
|
||||
union bpf_attr attr;
|
||||
@@ -5505,6 +5604,9 @@ static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
|
||||
case BPF_PROG_BIND_MAP:
|
||||
err = bpf_prog_bind_map(&attr);
|
||||
break;
|
||||
case BPF_TOKEN_CREATE:
|
||||
err = token_create(&attr);
|
||||
break;
|
||||
default:
|
||||
err = -EINVAL;
|
||||
break;
|
||||
@@ -5611,7 +5713,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = {
|
||||
const struct bpf_func_proto * __weak
|
||||
tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
{
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
switch (func_id) {
|
||||
case BPF_FUNC_sys_bpf:
|
||||
return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
|
||||
return !bpf_token_capable(prog->aux->token, CAP_PERFMON)
|
||||
? NULL : &bpf_sys_bpf_proto;
|
||||
case BPF_FUNC_btf_find_by_name_kind:
|
||||
return &bpf_btf_find_by_name_kind_proto;
|
||||
case BPF_FUNC_sys_close:
|
||||
|
||||
263
kernel/bpf/token.c
Normal file
263
kernel/bpf/token.c
Normal file
@@ -0,0 +1,263 @@
|
||||
#include <linux/bpf.h>
|
||||
#include <linux/vmalloc.h>
|
||||
#include <linux/fdtable.h>
|
||||
#include <linux/file.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/idr.h>
|
||||
#include <linux/namei.h>
|
||||
#include <linux/user_namespace.h>
|
||||
#include <linux/security.h>
|
||||
|
||||
bool bpf_token_capable(const struct bpf_token *token, int cap)
|
||||
{
|
||||
/* BPF token allows ns_capable() level of capabilities, but only if
|
||||
* token's userns is *exactly* the same as current user's userns
|
||||
*/
|
||||
if (token && current_user_ns() == token->userns) {
|
||||
if (ns_capable(token->userns, cap) ||
|
||||
(cap != CAP_SYS_ADMIN && ns_capable(token->userns, CAP_SYS_ADMIN)))
|
||||
return security_bpf_token_capable(token, cap) == 0;
|
||||
}
|
||||
/* otherwise fallback to capable() checks */
|
||||
return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
|
||||
}
|
||||
|
||||
void bpf_token_inc(struct bpf_token *token)
|
||||
{
|
||||
atomic64_inc(&token->refcnt);
|
||||
}
|
||||
|
||||
static void bpf_token_free(struct bpf_token *token)
|
||||
{
|
||||
security_bpf_token_free(token);
|
||||
put_user_ns(token->userns);
|
||||
kvfree(token);
|
||||
}
|
||||
|
||||
static void bpf_token_put_deferred(struct work_struct *work)
|
||||
{
|
||||
struct bpf_token *token = container_of(work, struct bpf_token, work);
|
||||
|
||||
bpf_token_free(token);
|
||||
}
|
||||
|
||||
void bpf_token_put(struct bpf_token *token)
|
||||
{
|
||||
if (!token)
|
||||
return;
|
||||
|
||||
if (!atomic64_dec_and_test(&token->refcnt))
|
||||
return;
|
||||
|
||||
INIT_WORK(&token->work, bpf_token_put_deferred);
|
||||
schedule_work(&token->work);
|
||||
}
|
||||
|
||||
static int bpf_token_release(struct inode *inode, struct file *filp)
|
||||
{
|
||||
struct bpf_token *token = filp->private_data;
|
||||
|
||||
bpf_token_put(token);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void bpf_token_show_fdinfo(struct seq_file *m, struct file *filp)
|
||||
{
|
||||
struct bpf_token *token = filp->private_data;
|
||||
u64 mask;
|
||||
|
||||
BUILD_BUG_ON(__MAX_BPF_CMD >= 64);
|
||||
mask = (1ULL << __MAX_BPF_CMD) - 1;
|
||||
if ((token->allowed_cmds & mask) == mask)
|
||||
seq_printf(m, "allowed_cmds:\tany\n");
|
||||
else
|
||||
seq_printf(m, "allowed_cmds:\t0x%llx\n", token->allowed_cmds);
|
||||
|
||||
BUILD_BUG_ON(__MAX_BPF_MAP_TYPE >= 64);
|
||||
mask = (1ULL << __MAX_BPF_MAP_TYPE) - 1;
|
||||
if ((token->allowed_maps & mask) == mask)
|
||||
seq_printf(m, "allowed_maps:\tany\n");
|
||||
else
|
||||
seq_printf(m, "allowed_maps:\t0x%llx\n", token->allowed_maps);
|
||||
|
||||
BUILD_BUG_ON(__MAX_BPF_PROG_TYPE >= 64);
|
||||
mask = (1ULL << __MAX_BPF_PROG_TYPE) - 1;
|
||||
if ((token->allowed_progs & mask) == mask)
|
||||
seq_printf(m, "allowed_progs:\tany\n");
|
||||
else
|
||||
seq_printf(m, "allowed_progs:\t0x%llx\n", token->allowed_progs);
|
||||
|
||||
BUILD_BUG_ON(__MAX_BPF_ATTACH_TYPE >= 64);
|
||||
mask = (1ULL << __MAX_BPF_ATTACH_TYPE) - 1;
|
||||
if ((token->allowed_attachs & mask) == mask)
|
||||
seq_printf(m, "allowed_attachs:\tany\n");
|
||||
else
|
||||
seq_printf(m, "allowed_attachs:\t0x%llx\n", token->allowed_attachs);
|
||||
}
|
||||
|
||||
#define BPF_TOKEN_INODE_NAME "bpf-token"
|
||||
|
||||
static const struct inode_operations bpf_token_iops = { };
|
||||
|
||||
static const struct file_operations bpf_token_fops = {
|
||||
.release = bpf_token_release,
|
||||
.show_fdinfo = bpf_token_show_fdinfo,
|
||||
};
|
||||
|
||||
int bpf_token_create(union bpf_attr *attr)
|
||||
{
|
||||
struct bpf_mount_opts *mnt_opts;
|
||||
struct bpf_token *token = NULL;
|
||||
struct user_namespace *userns;
|
||||
struct inode *inode;
|
||||
struct file *file;
|
||||
struct path path;
|
||||
struct fd f;
|
||||
umode_t mode;
|
||||
int err, fd;
|
||||
|
||||
f = fdget(attr->token_create.bpffs_fd);
|
||||
if (!f.file)
|
||||
return -EBADF;
|
||||
|
||||
path = f.file->f_path;
|
||||
path_get(&path);
|
||||
fdput(f);
|
||||
|
||||
if (path.dentry != path.mnt->mnt_sb->s_root) {
|
||||
err = -EINVAL;
|
||||
goto out_path;
|
||||
}
|
||||
if (path.mnt->mnt_sb->s_op != &bpf_super_ops) {
|
||||
err = -EINVAL;
|
||||
goto out_path;
|
||||
}
|
||||
err = path_permission(&path, MAY_ACCESS);
|
||||
if (err)
|
||||
goto out_path;
|
||||
|
||||
userns = path.dentry->d_sb->s_user_ns;
|
||||
/*
|
||||
* Enforce that creators of BPF tokens are in the same user
|
||||
* namespace as the BPF FS instance. This makes reasoning about
|
||||
* permissions a lot easier and we can always relax this later.
|
||||
*/
|
||||
if (current_user_ns() != userns) {
|
||||
err = -EPERM;
|
||||
goto out_path;
|
||||
}
|
||||
if (!ns_capable(userns, CAP_BPF)) {
|
||||
err = -EPERM;
|
||||
goto out_path;
|
||||
}
|
||||
|
||||
mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
|
||||
inode = bpf_get_inode(path.mnt->mnt_sb, NULL, mode);
|
||||
if (IS_ERR(inode)) {
|
||||
err = PTR_ERR(inode);
|
||||
goto out_path;
|
||||
}
|
||||
|
||||
inode->i_op = &bpf_token_iops;
|
||||
inode->i_fop = &bpf_token_fops;
|
||||
clear_nlink(inode); /* make sure it is unlinked */
|
||||
|
||||
file = alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME, O_RDWR, &bpf_token_fops);
|
||||
if (IS_ERR(file)) {
|
||||
iput(inode);
|
||||
err = PTR_ERR(file);
|
||||
goto out_path;
|
||||
}
|
||||
|
||||
token = kvzalloc(sizeof(*token), GFP_USER);
|
||||
if (!token) {
|
||||
err = -ENOMEM;
|
||||
goto out_file;
|
||||
}
|
||||
|
||||
atomic64_set(&token->refcnt, 1);
|
||||
|
||||
/* remember bpffs owning userns for future ns_capable() checks */
|
||||
token->userns = get_user_ns(userns);
|
||||
|
||||
mnt_opts = path.dentry->d_sb->s_fs_info;
|
||||
token->allowed_cmds = mnt_opts->delegate_cmds;
|
||||
token->allowed_maps = mnt_opts->delegate_maps;
|
||||
token->allowed_progs = mnt_opts->delegate_progs;
|
||||
token->allowed_attachs = mnt_opts->delegate_attachs;
|
||||
|
||||
err = security_bpf_token_create(token, attr, &path);
|
||||
if (err)
|
||||
goto out_token;
|
||||
|
||||
fd = get_unused_fd_flags(O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
err = fd;
|
||||
goto out_token;
|
||||
}
|
||||
|
||||
file->private_data = token;
|
||||
fd_install(fd, file);
|
||||
|
||||
path_put(&path);
|
||||
return fd;
|
||||
|
||||
out_token:
|
||||
bpf_token_free(token);
|
||||
out_file:
|
||||
fput(file);
|
||||
out_path:
|
||||
path_put(&path);
|
||||
return err;
|
||||
}
|
||||
|
||||
struct bpf_token *bpf_token_get_from_fd(u32 ufd)
|
||||
{
|
||||
struct fd f = fdget(ufd);
|
||||
struct bpf_token *token;
|
||||
|
||||
if (!f.file)
|
||||
return ERR_PTR(-EBADF);
|
||||
if (f.file->f_op != &bpf_token_fops) {
|
||||
fdput(f);
|
||||
return ERR_PTR(-EINVAL);
|
||||
}
|
||||
|
||||
token = f.file->private_data;
|
||||
bpf_token_inc(token);
|
||||
fdput(f);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
|
||||
{
|
||||
/* BPF token can be used only within exactly the same userns in which
|
||||
* it was created
|
||||
*/
|
||||
if (!token || current_user_ns() != token->userns)
|
||||
return false;
|
||||
if (!(token->allowed_cmds & (1ULL << cmd)))
|
||||
return false;
|
||||
return security_bpf_token_cmd(token, cmd) == 0;
|
||||
}
|
||||
|
||||
bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type)
|
||||
{
|
||||
if (!token || type >= __MAX_BPF_MAP_TYPE)
|
||||
return false;
|
||||
|
||||
return token->allowed_maps & (1ULL << type);
|
||||
}
|
||||
|
||||
bool bpf_token_allow_prog_type(const struct bpf_token *token,
|
||||
enum bpf_prog_type prog_type,
|
||||
enum bpf_attach_type attach_type)
|
||||
{
|
||||
if (!token || prog_type >= __MAX_BPF_PROG_TYPE || attach_type >= __MAX_BPF_ATTACH_TYPE)
|
||||
return false;
|
||||
|
||||
return (token->allowed_progs & (1ULL << prog_type)) &&
|
||||
(token->allowed_attachs & (1ULL << attach_type));
|
||||
}
|
||||
@@ -20597,7 +20597,12 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
|
||||
env->prog = *prog;
|
||||
env->ops = bpf_verifier_ops[env->prog->type];
|
||||
env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel);
|
||||
is_priv = bpf_capable();
|
||||
|
||||
env->allow_ptr_leaks = bpf_allow_ptr_leaks(env->prog->aux->token);
|
||||
env->allow_uninit_stack = bpf_allow_uninit_stack(env->prog->aux->token);
|
||||
env->bypass_spec_v1 = bpf_bypass_spec_v1(env->prog->aux->token);
|
||||
env->bypass_spec_v4 = bpf_bypass_spec_v4(env->prog->aux->token);
|
||||
env->bpf_capable = is_priv = bpf_token_capable(env->prog->aux->token, CAP_BPF);
|
||||
|
||||
bpf_get_btf_vmlinux();
|
||||
|
||||
@@ -20629,12 +20634,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
|
||||
if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
|
||||
env->strict_alignment = false;
|
||||
|
||||
env->allow_ptr_leaks = bpf_allow_ptr_leaks();
|
||||
env->allow_uninit_stack = bpf_allow_uninit_stack();
|
||||
env->bypass_spec_v1 = bpf_bypass_spec_v1();
|
||||
env->bypass_spec_v4 = bpf_bypass_spec_v4();
|
||||
env->bpf_capable = bpf_capable();
|
||||
|
||||
if (is_priv)
|
||||
env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
|
||||
env->test_reg_invariants = attr->prog_flags & BPF_F_TEST_REG_INVARIANTS;
|
||||
|
||||
@@ -1626,7 +1626,7 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_trace_vprintk:
|
||||
return bpf_get_trace_vprintk_proto();
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@
|
||||
#include "dev.h"
|
||||
|
||||
static const struct bpf_func_proto *
|
||||
bpf_sk_base_func_proto(enum bpf_func_id func_id);
|
||||
bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
|
||||
|
||||
int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
|
||||
{
|
||||
@@ -7841,7 +7841,7 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_ktime_get_coarse_ns:
|
||||
return &bpf_ktime_get_coarse_ns_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7934,7 +7934,7 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
return NULL;
|
||||
}
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7953,7 +7953,7 @@ sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_perf_event_output:
|
||||
return &bpf_skb_event_output_proto;
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8140,7 +8140,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
#endif
|
||||
#endif
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8199,7 +8199,7 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
#endif
|
||||
#endif
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
|
||||
#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)
|
||||
return &bpf_tcp_sock_proto;
|
||||
#endif /* CONFIG_INET */
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8302,7 +8302,7 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
return &bpf_get_cgroup_classid_curr_proto;
|
||||
#endif
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8346,7 +8346,7 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
return &bpf_skc_lookup_tcp_proto;
|
||||
#endif
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8357,7 +8357,7 @@ flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_skb_load_bytes:
|
||||
return &bpf_flow_dissector_load_bytes_proto;
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8384,7 +8384,7 @@ lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_skb_under_cgroup:
|
||||
return &bpf_skb_under_cgroup_proto;
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8559,7 +8559,7 @@ static bool cg_skb_is_valid_access(int off, int size,
|
||||
return false;
|
||||
case bpf_ctx_range(struct __sk_buff, data):
|
||||
case bpf_ctx_range(struct __sk_buff, data_end):
|
||||
if (!bpf_capable())
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_BPF))
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
@@ -8571,7 +8571,7 @@ static bool cg_skb_is_valid_access(int off, int size,
|
||||
case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
|
||||
break;
|
||||
case bpf_ctx_range(struct __sk_buff, tstamp):
|
||||
if (!bpf_capable())
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_BPF))
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
@@ -11215,7 +11215,7 @@ sk_reuseport_func_proto(enum bpf_func_id func_id,
|
||||
case BPF_FUNC_ktime_get_coarse_ns:
|
||||
return &bpf_ktime_get_coarse_ns_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11397,7 +11397,7 @@ sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
case BPF_FUNC_sk_release:
|
||||
return &bpf_sk_release_proto;
|
||||
default:
|
||||
return bpf_sk_base_func_proto(func_id);
|
||||
return bpf_sk_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11731,7 +11731,7 @@ const struct bpf_func_proto bpf_sock_from_file_proto = {
|
||||
};
|
||||
|
||||
static const struct bpf_func_proto *
|
||||
bpf_sk_base_func_proto(enum bpf_func_id func_id)
|
||||
bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
{
|
||||
const struct bpf_func_proto *func;
|
||||
|
||||
@@ -11760,10 +11760,10 @@ bpf_sk_base_func_proto(enum bpf_func_id func_id)
|
||||
case BPF_FUNC_ktime_get_coarse_ns:
|
||||
return &bpf_ktime_get_coarse_ns_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
|
||||
if (!perfmon_capable())
|
||||
if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
|
||||
return NULL;
|
||||
|
||||
return func;
|
||||
|
||||
@@ -191,7 +191,7 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
|
||||
case BPF_FUNC_ktime_get_coarse_ns:
|
||||
return &bpf_ktime_get_coarse_ns_proto;
|
||||
default:
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -314,7 +314,7 @@ static bool nf_is_valid_access(int off, int size, enum bpf_access_type type,
|
||||
static const struct bpf_func_proto *
|
||||
bpf_nf_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
|
||||
{
|
||||
return bpf_base_func_proto(func_id);
|
||||
return bpf_base_func_proto(func_id, prog);
|
||||
}
|
||||
|
||||
const struct bpf_verifier_ops netfilter_verifier_ops = {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user