2 #include <linux/vmalloc.h>
3 #include <linux/file.h>
5 #include <linux/kernel.h>
7 #include <linux/namei.h>
8 #include <linux/user_namespace.h>
9 #include <linux/security.h>
11 static bool bpf_ns_capable(struct user_namespace
*ns
, int cap
)
13 return ns_capable(ns
, cap
) || (cap
!= CAP_SYS_ADMIN
&& ns_capable(ns
, CAP_SYS_ADMIN
));
16 bool bpf_token_capable(const struct bpf_token
*token
, int cap
)
18 struct user_namespace
*userns
;
20 /* BPF token allows ns_capable() level of capabilities */
21 userns
= token
? token
->userns
: &init_user_ns
;
22 if (!bpf_ns_capable(userns
, cap
))
24 if (token
&& security_bpf_token_capable(token
, cap
) < 0)
29 void bpf_token_inc(struct bpf_token
*token
)
31 atomic64_inc(&token
->refcnt
);
34 static void bpf_token_free(struct bpf_token
*token
)
36 security_bpf_token_free(token
);
37 put_user_ns(token
->userns
);
41 static void bpf_token_put_deferred(struct work_struct
*work
)
43 struct bpf_token
*token
= container_of(work
, struct bpf_token
, work
);
45 bpf_token_free(token
);
48 void bpf_token_put(struct bpf_token
*token
)
53 if (!atomic64_dec_and_test(&token
->refcnt
))
56 INIT_WORK(&token
->work
, bpf_token_put_deferred
);
57 schedule_work(&token
->work
);
60 static int bpf_token_release(struct inode
*inode
, struct file
*filp
)
62 struct bpf_token
*token
= filp
->private_data
;
68 static void bpf_token_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
70 struct bpf_token
*token
= filp
->private_data
;
73 BUILD_BUG_ON(__MAX_BPF_CMD
>= 64);
74 mask
= BIT_ULL(__MAX_BPF_CMD
) - 1;
75 if ((token
->allowed_cmds
& mask
) == mask
)
76 seq_printf(m
, "allowed_cmds:\tany\n");
78 seq_printf(m
, "allowed_cmds:\t0x%llx\n", token
->allowed_cmds
);
80 BUILD_BUG_ON(__MAX_BPF_MAP_TYPE
>= 64);
81 mask
= BIT_ULL(__MAX_BPF_MAP_TYPE
) - 1;
82 if ((token
->allowed_maps
& mask
) == mask
)
83 seq_printf(m
, "allowed_maps:\tany\n");
85 seq_printf(m
, "allowed_maps:\t0x%llx\n", token
->allowed_maps
);
87 BUILD_BUG_ON(__MAX_BPF_PROG_TYPE
>= 64);
88 mask
= BIT_ULL(__MAX_BPF_PROG_TYPE
) - 1;
89 if ((token
->allowed_progs
& mask
) == mask
)
90 seq_printf(m
, "allowed_progs:\tany\n");
92 seq_printf(m
, "allowed_progs:\t0x%llx\n", token
->allowed_progs
);
94 BUILD_BUG_ON(__MAX_BPF_ATTACH_TYPE
>= 64);
95 mask
= BIT_ULL(__MAX_BPF_ATTACH_TYPE
) - 1;
96 if ((token
->allowed_attachs
& mask
) == mask
)
97 seq_printf(m
, "allowed_attachs:\tany\n");
99 seq_printf(m
, "allowed_attachs:\t0x%llx\n", token
->allowed_attachs
);
102 #define BPF_TOKEN_INODE_NAME "bpf-token"
104 static const struct inode_operations bpf_token_iops
= { };
106 static const struct file_operations bpf_token_fops
= {
107 .release
= bpf_token_release
,
108 .show_fdinfo
= bpf_token_show_fdinfo
,
111 int bpf_token_create(union bpf_attr
*attr
)
113 struct bpf_mount_opts
*mnt_opts
;
114 struct bpf_token
*token
= NULL
;
115 struct user_namespace
*userns
;
118 CLASS(fd
, f
)(attr
->token_create
.bpffs_fd
);
120 struct super_block
*sb
;
127 path
= fd_file(f
)->f_path
;
128 sb
= path
.dentry
->d_sb
;
130 if (path
.dentry
!= sb
->s_root
)
132 if (sb
->s_op
!= &bpf_super_ops
)
134 err
= path_permission(&path
, MAY_ACCESS
);
138 userns
= sb
->s_user_ns
;
140 * Enforce that creators of BPF tokens are in the same user
141 * namespace as the BPF FS instance. This makes reasoning about
142 * permissions a lot easier and we can always relax this later.
144 if (current_user_ns() != userns
)
146 if (!ns_capable(userns
, CAP_BPF
))
149 /* Creating BPF token in init_user_ns doesn't make much sense. */
150 if (current_user_ns() == &init_user_ns
)
153 mnt_opts
= sb
->s_fs_info
;
154 if (mnt_opts
->delegate_cmds
== 0 &&
155 mnt_opts
->delegate_maps
== 0 &&
156 mnt_opts
->delegate_progs
== 0 &&
157 mnt_opts
->delegate_attachs
== 0)
158 return -ENOENT
; /* no BPF token delegation is set up */
160 mode
= S_IFREG
| ((S_IRUSR
| S_IWUSR
) & ~current_umask());
161 inode
= bpf_get_inode(sb
, NULL
, mode
);
163 return PTR_ERR(inode
);
165 inode
->i_op
= &bpf_token_iops
;
166 inode
->i_fop
= &bpf_token_fops
;
167 clear_nlink(inode
); /* make sure it is unlinked */
169 file
= alloc_file_pseudo(inode
, path
.mnt
, BPF_TOKEN_INODE_NAME
, O_RDWR
, &bpf_token_fops
);
172 return PTR_ERR(file
);
175 token
= kzalloc(sizeof(*token
), GFP_USER
);
181 atomic64_set(&token
->refcnt
, 1);
183 /* remember bpffs owning userns for future ns_capable() checks */
184 token
->userns
= get_user_ns(userns
);
186 token
->allowed_cmds
= mnt_opts
->delegate_cmds
;
187 token
->allowed_maps
= mnt_opts
->delegate_maps
;
188 token
->allowed_progs
= mnt_opts
->delegate_progs
;
189 token
->allowed_attachs
= mnt_opts
->delegate_attachs
;
191 err
= security_bpf_token_create(token
, attr
, &path
);
195 fd
= get_unused_fd_flags(O_CLOEXEC
);
201 file
->private_data
= token
;
202 fd_install(fd
, file
);
207 bpf_token_free(token
);
213 struct bpf_token
*bpf_token_get_from_fd(u32 ufd
)
216 struct bpf_token
*token
;
219 return ERR_PTR(-EBADF
);
220 if (fd_file(f
)->f_op
!= &bpf_token_fops
)
221 return ERR_PTR(-EINVAL
);
223 token
= fd_file(f
)->private_data
;
224 bpf_token_inc(token
);
229 bool bpf_token_allow_cmd(const struct bpf_token
*token
, enum bpf_cmd cmd
)
233 if (!(token
->allowed_cmds
& BIT_ULL(cmd
)))
235 return security_bpf_token_cmd(token
, cmd
) == 0;
238 bool bpf_token_allow_map_type(const struct bpf_token
*token
, enum bpf_map_type type
)
240 if (!token
|| type
>= __MAX_BPF_MAP_TYPE
)
243 return token
->allowed_maps
& BIT_ULL(type
);
246 bool bpf_token_allow_prog_type(const struct bpf_token
*token
,
247 enum bpf_prog_type prog_type
,
248 enum bpf_attach_type attach_type
)
250 if (!token
|| prog_type
>= __MAX_BPF_PROG_TYPE
|| attach_type
>= __MAX_BPF_ATTACH_TYPE
)
253 return (token
->allowed_progs
& BIT_ULL(prog_type
)) &&
254 (token
->allowed_attachs
& BIT_ULL(attach_type
));