xref: /linux-6.15/kernel/bpf/token.c (revision 6b4a64ba)
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 }
264