Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Minimal file system backend for holding eBPF maps and programs,
  3 * used by bpf(2) object pinning.
  4 *
  5 * Authors:
  6 *
  7 *	Daniel Borkmann <daniel@iogearbox.net>
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * version 2 as published by the Free Software Foundation.
 12 */
 13
 14#include <linux/init.h>
 15#include <linux/magic.h>
 16#include <linux/major.h>
 17#include <linux/mount.h>
 18#include <linux/namei.h>
 19#include <linux/fs.h>
 
 
 20#include <linux/kdev_t.h>
 21#include <linux/parser.h>
 22#include <linux/filter.h>
 23#include <linux/bpf.h>
 
 24
 25enum bpf_type {
 26	BPF_TYPE_UNSPEC	= 0,
 27	BPF_TYPE_PROG,
 28	BPF_TYPE_MAP,
 
 29};
 30
 31static void *bpf_any_get(void *raw, enum bpf_type type)
 32{
 33	switch (type) {
 34	case BPF_TYPE_PROG:
 35		raw = bpf_prog_inc(raw);
 36		break;
 37	case BPF_TYPE_MAP:
 38		raw = bpf_map_inc(raw, true);
 
 
 
 39		break;
 40	default:
 41		WARN_ON_ONCE(1);
 42		break;
 43	}
 44
 45	return raw;
 46}
 47
 48static void bpf_any_put(void *raw, enum bpf_type type)
 49{
 50	switch (type) {
 51	case BPF_TYPE_PROG:
 52		bpf_prog_put(raw);
 53		break;
 54	case BPF_TYPE_MAP:
 55		bpf_map_put_with_uref(raw);
 56		break;
 
 
 
 57	default:
 58		WARN_ON_ONCE(1);
 59		break;
 60	}
 61}
 62
 63static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
 64{
 65	void *raw;
 66
 67	*type = BPF_TYPE_MAP;
 68	raw = bpf_map_get_with_uref(ufd);
 69	if (IS_ERR(raw)) {
 
 
 
 
 
 
 70		*type = BPF_TYPE_PROG;
 71		raw = bpf_prog_get(ufd);
 72	}
 73
 74	return raw;
 
 
 
 
 
 
 75}
 76
 77static const struct inode_operations bpf_dir_iops;
 78
 79static const struct inode_operations bpf_prog_iops = { };
 80static const struct inode_operations bpf_map_iops  = { };
 
 81
 82static struct inode *bpf_get_inode(struct super_block *sb,
 83				   const struct inode *dir,
 84				   umode_t mode)
 85{
 86	struct inode *inode;
 87
 88	switch (mode & S_IFMT) {
 89	case S_IFDIR:
 90	case S_IFREG:
 91	case S_IFLNK:
 92		break;
 93	default:
 94		return ERR_PTR(-EINVAL);
 95	}
 96
 97	inode = new_inode(sb);
 98	if (!inode)
 99		return ERR_PTR(-ENOSPC);
100
101	inode->i_ino = get_next_ino();
102	inode->i_atime = current_time(inode);
103	inode->i_mtime = inode->i_atime;
104	inode->i_ctime = inode->i_atime;
105
106	inode_init_owner(inode, dir, mode);
107
108	return inode;
109}
110
111static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
112{
113	*type = BPF_TYPE_UNSPEC;
114	if (inode->i_op == &bpf_prog_iops)
115		*type = BPF_TYPE_PROG;
116	else if (inode->i_op == &bpf_map_iops)
117		*type = BPF_TYPE_MAP;
 
 
118	else
119		return -EACCES;
120
121	return 0;
122}
123
124static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
125				struct inode *dir)
126{
127	d_instantiate(dentry, inode);
128	dget(dentry);
129
130	dir->i_mtime = current_time(dir);
131	dir->i_ctime = dir->i_mtime;
132}
133
134static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
135{
136	struct inode *inode;
137
138	inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
139	if (IS_ERR(inode))
140		return PTR_ERR(inode);
141
142	inode->i_op = &bpf_dir_iops;
143	inode->i_fop = &simple_dir_operations;
144
145	inc_nlink(inode);
146	inc_nlink(dir);
147
148	bpf_dentry_finalize(dentry, inode, dir);
149	return 0;
150}
151
152static int bpf_mkobj_ops(struct inode *dir, struct dentry *dentry,
153			 umode_t mode, const struct inode_operations *iops)
 
 
 
 
154{
155	struct inode *inode;
 
 
 
 
 
 
156
157	inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFREG);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158	if (IS_ERR(inode))
159		return PTR_ERR(inode);
160
161	inode->i_op = iops;
162	inode->i_private = dentry->d_fsdata;
 
163
164	bpf_dentry_finalize(dentry, inode, dir);
165	return 0;
166}
167
168static int bpf_mkobj(struct inode *dir, struct dentry *dentry, umode_t mode,
169		     dev_t devt)
170{
171	enum bpf_type type = MINOR(devt);
 
 
172
173	if (MAJOR(devt) != UNNAMED_MAJOR || !S_ISREG(mode) ||
174	    dentry->d_fsdata == NULL)
175		return -EPERM;
176
177	switch (type) {
178	case BPF_TYPE_PROG:
179		return bpf_mkobj_ops(dir, dentry, mode, &bpf_prog_iops);
180	case BPF_TYPE_MAP:
181		return bpf_mkobj_ops(dir, dentry, mode, &bpf_map_iops);
182	default:
183		return -EPERM;
184	}
 
 
 
 
185}
186
187static struct dentry *
188bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
189{
 
 
 
190	if (strchr(dentry->d_name.name, '.'))
191		return ERR_PTR(-EPERM);
192
193	return simple_lookup(dir, dentry, flags);
194}
195
196static int bpf_symlink(struct inode *dir, struct dentry *dentry,
197		       const char *target)
198{
199	char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
200	struct inode *inode;
201
202	if (!link)
203		return -ENOMEM;
204
205	inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
206	if (IS_ERR(inode)) {
207		kfree(link);
208		return PTR_ERR(inode);
209	}
210
211	inode->i_op = &simple_symlink_inode_operations;
212	inode->i_link = link;
213
214	bpf_dentry_finalize(dentry, inode, dir);
215	return 0;
216}
217
218static const struct inode_operations bpf_dir_iops = {
219	.lookup		= bpf_lookup,
220	.mknod		= bpf_mkobj,
221	.mkdir		= bpf_mkdir,
222	.symlink	= bpf_symlink,
223	.rmdir		= simple_rmdir,
224	.rename		= simple_rename,
225	.link		= simple_link,
226	.unlink		= simple_unlink,
227};
228
229static int bpf_obj_do_pin(const struct filename *pathname, void *raw,
230			  enum bpf_type type)
231{
232	struct dentry *dentry;
233	struct inode *dir;
234	struct path path;
235	umode_t mode;
236	dev_t devt;
237	int ret;
238
239	dentry = kern_path_create(AT_FDCWD, pathname->name, &path, 0);
240	if (IS_ERR(dentry))
241		return PTR_ERR(dentry);
242
243	mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
244	devt = MKDEV(UNNAMED_MAJOR, type);
245
246	ret = security_path_mknod(&path, dentry, mode, devt);
247	if (ret)
248		goto out;
249
250	dir = d_inode(path.dentry);
251	if (dir->i_op != &bpf_dir_iops) {
252		ret = -EPERM;
253		goto out;
254	}
255
256	dentry->d_fsdata = raw;
257	ret = vfs_mknod(dir, dentry, mode, devt);
258	dentry->d_fsdata = NULL;
 
 
 
 
 
 
 
 
 
 
259out:
260	done_path_create(&path, dentry);
261	return ret;
262}
263
264int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
265{
266	struct filename *pname;
267	enum bpf_type type;
268	void *raw;
269	int ret;
270
271	pname = getname(pathname);
272	if (IS_ERR(pname))
273		return PTR_ERR(pname);
274
275	raw = bpf_fd_probe_obj(ufd, &type);
276	if (IS_ERR(raw)) {
277		ret = PTR_ERR(raw);
278		goto out;
279	}
280
281	ret = bpf_obj_do_pin(pname, raw, type);
282	if (ret != 0)
283		bpf_any_put(raw, type);
284out:
285	putname(pname);
286	return ret;
287}
288
289static void *bpf_obj_do_get(const struct filename *pathname,
290			    enum bpf_type *type)
291{
292	struct inode *inode;
293	struct path path;
294	void *raw;
295	int ret;
296
297	ret = kern_path(pathname->name, LOOKUP_FOLLOW, &path);
298	if (ret)
299		return ERR_PTR(ret);
300
301	inode = d_backing_inode(path.dentry);
302	ret = inode_permission(inode, MAY_WRITE);
303	if (ret)
304		goto out;
305
306	ret = bpf_inode_type(inode, type);
307	if (ret)
308		goto out;
309
310	raw = bpf_any_get(inode->i_private, *type);
311	if (!IS_ERR(raw))
312		touch_atime(&path);
313
314	path_put(&path);
315	return raw;
316out:
317	path_put(&path);
318	return ERR_PTR(ret);
319}
320
321int bpf_obj_get_user(const char __user *pathname)
322{
323	enum bpf_type type = BPF_TYPE_UNSPEC;
324	struct filename *pname;
325	int ret = -ENOENT;
326	void *raw;
 
327
328	pname = getname(pathname);
329	if (IS_ERR(pname))
330		return PTR_ERR(pname);
331
332	raw = bpf_obj_do_get(pname, &type);
333	if (IS_ERR(raw)) {
334		ret = PTR_ERR(raw);
335		goto out;
336	}
337
338	if (type == BPF_TYPE_PROG)
339		ret = bpf_prog_new_fd(raw);
340	else if (type == BPF_TYPE_MAP)
341		ret = bpf_map_new_fd(raw);
 
 
342	else
343		goto out;
344
345	if (ret < 0)
346		bpf_any_put(raw, type);
347out:
348	putname(pname);
349	return ret;
350}
351
352static void bpf_evict_inode(struct inode *inode)
353{
354	enum bpf_type type;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
356	truncate_inode_pages_final(&inode->i_data);
357	clear_inode(inode);
 
 
 
 
 
 
358
359	if (S_ISLNK(inode->i_mode))
360		kfree(inode->i_link);
361	if (!bpf_inode_type(inode, &type))
362		bpf_any_put(inode->i_private, type);
 
363}
364
365static const struct super_operations bpf_super_ops = {
366	.statfs		= simple_statfs,
367	.drop_inode	= generic_delete_inode,
368	.show_options	= generic_show_options,
369	.evict_inode	= bpf_evict_inode,
370};
371
372enum {
373	OPT_MODE,
374	OPT_ERR,
375};
376
377static const match_table_t bpf_mount_tokens = {
378	{ OPT_MODE, "mode=%o" },
379	{ OPT_ERR, NULL },
380};
381
382struct bpf_mount_opts {
383	umode_t mode;
384};
385
386static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
387{
388	substring_t args[MAX_OPT_ARGS];
389	int option, token;
390	char *ptr;
391
392	opts->mode = S_IRWXUGO;
393
394	while ((ptr = strsep(&data, ",")) != NULL) {
395		if (!*ptr)
396			continue;
397
398		token = match_token(ptr, bpf_mount_tokens, args);
399		switch (token) {
400		case OPT_MODE:
401			if (match_octal(&args[0], &option))
402				return -EINVAL;
403			opts->mode = option & S_IALLUGO;
404			break;
405		/* We might like to report bad mount options here, but
406		 * traditionally we've ignored all mount options, so we'd
407		 * better continue to ignore non-existing options for bpf.
408		 */
409		}
 
 
 
 
 
410	}
411
412	return 0;
413}
414
415static int bpf_fill_super(struct super_block *sb, void *data, int silent)
416{
417	static struct tree_descr bpf_rfiles[] = { { "" } };
418	struct bpf_mount_opts opts;
419	struct inode *inode;
420	int ret;
421
422	save_mount_options(sb, data);
423
424	ret = bpf_parse_options(data, &opts);
425	if (ret)
426		return ret;
427
428	ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
429	if (ret)
430		return ret;
431
432	sb->s_op = &bpf_super_ops;
433
434	inode = sb->s_root->d_inode;
435	inode->i_op = &bpf_dir_iops;
436	inode->i_mode &= ~S_IALLUGO;
437	inode->i_mode |= S_ISVTX | opts.mode;
438
439	return 0;
440}
441
442static struct dentry *bpf_mount(struct file_system_type *type, int flags,
443				const char *dev_name, void *data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444{
445	return mount_nodev(type, flags, data, bpf_fill_super);
 
 
 
 
 
 
 
 
 
 
446}
447
448static struct file_system_type bpf_fs_type = {
449	.owner		= THIS_MODULE,
450	.name		= "bpf",
451	.mount		= bpf_mount,
 
452	.kill_sb	= kill_litter_super,
453};
454
455static int __init bpf_init(void)
456{
457	int ret;
458
459	ret = sysfs_create_mount_point(fs_kobj, "bpf");
460	if (ret)
461		return ret;
462
463	ret = register_filesystem(&bpf_fs_type);
464	if (ret)
465		sysfs_remove_mount_point(fs_kobj, "bpf");
466
467	return ret;
468}
469fs_initcall(bpf_init);
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Minimal file system backend for holding eBPF maps and programs,
  4 * used by bpf(2) object pinning.
  5 *
  6 * Authors:
  7 *
  8 *	Daniel Borkmann <daniel@iogearbox.net>
 
 
 
 
  9 */
 10
 11#include <linux/init.h>
 12#include <linux/magic.h>
 13#include <linux/major.h>
 14#include <linux/mount.h>
 15#include <linux/namei.h>
 16#include <linux/fs.h>
 17#include <linux/fs_context.h>
 18#include <linux/fs_parser.h>
 19#include <linux/kdev_t.h>
 
 20#include <linux/filter.h>
 21#include <linux/bpf.h>
 22#include <linux/bpf_trace.h>
 23
 24enum bpf_type {
 25	BPF_TYPE_UNSPEC	= 0,
 26	BPF_TYPE_PROG,
 27	BPF_TYPE_MAP,
 28	BPF_TYPE_LINK,
 29};
 30
 31static void *bpf_any_get(void *raw, enum bpf_type type)
 32{
 33	switch (type) {
 34	case BPF_TYPE_PROG:
 35		bpf_prog_inc(raw);
 36		break;
 37	case BPF_TYPE_MAP:
 38		bpf_map_inc_with_uref(raw);
 39		break;
 40	case BPF_TYPE_LINK:
 41		bpf_link_inc(raw);
 42		break;
 43	default:
 44		WARN_ON_ONCE(1);
 45		break;
 46	}
 47
 48	return raw;
 49}
 50
 51static void bpf_any_put(void *raw, enum bpf_type type)
 52{
 53	switch (type) {
 54	case BPF_TYPE_PROG:
 55		bpf_prog_put(raw);
 56		break;
 57	case BPF_TYPE_MAP:
 58		bpf_map_put_with_uref(raw);
 59		break;
 60	case BPF_TYPE_LINK:
 61		bpf_link_put(raw);
 62		break;
 63	default:
 64		WARN_ON_ONCE(1);
 65		break;
 66	}
 67}
 68
 69static void *bpf_fd_probe_obj(u32 ufd, enum bpf_type *type)
 70{
 71	void *raw;
 72
 
 73	raw = bpf_map_get_with_uref(ufd);
 74	if (!IS_ERR(raw)) {
 75		*type = BPF_TYPE_MAP;
 76		return raw;
 77	}
 78
 79	raw = bpf_prog_get(ufd);
 80	if (!IS_ERR(raw)) {
 81		*type = BPF_TYPE_PROG;
 82		return raw;
 83	}
 84
 85	raw = bpf_link_get_from_fd(ufd);
 86	if (!IS_ERR(raw)) {
 87		*type = BPF_TYPE_LINK;
 88		return raw;
 89	}
 90
 91	return ERR_PTR(-EINVAL);
 92}
 93
 94static const struct inode_operations bpf_dir_iops;
 95
 96static const struct inode_operations bpf_prog_iops = { };
 97static const struct inode_operations bpf_map_iops  = { };
 98static const struct inode_operations bpf_link_iops  = { };
 99
100static struct inode *bpf_get_inode(struct super_block *sb,
101				   const struct inode *dir,
102				   umode_t mode)
103{
104	struct inode *inode;
105
106	switch (mode & S_IFMT) {
107	case S_IFDIR:
108	case S_IFREG:
109	case S_IFLNK:
110		break;
111	default:
112		return ERR_PTR(-EINVAL);
113	}
114
115	inode = new_inode(sb);
116	if (!inode)
117		return ERR_PTR(-ENOSPC);
118
119	inode->i_ino = get_next_ino();
120	inode->i_atime = current_time(inode);
121	inode->i_mtime = inode->i_atime;
122	inode->i_ctime = inode->i_atime;
123
124	inode_init_owner(inode, dir, mode);
125
126	return inode;
127}
128
129static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
130{
131	*type = BPF_TYPE_UNSPEC;
132	if (inode->i_op == &bpf_prog_iops)
133		*type = BPF_TYPE_PROG;
134	else if (inode->i_op == &bpf_map_iops)
135		*type = BPF_TYPE_MAP;
136	else if (inode->i_op == &bpf_link_iops)
137		*type = BPF_TYPE_LINK;
138	else
139		return -EACCES;
140
141	return 0;
142}
143
144static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
145				struct inode *dir)
146{
147	d_instantiate(dentry, inode);
148	dget(dentry);
149
150	dir->i_mtime = current_time(dir);
151	dir->i_ctime = dir->i_mtime;
152}
153
154static int bpf_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
155{
156	struct inode *inode;
157
158	inode = bpf_get_inode(dir->i_sb, dir, mode | S_IFDIR);
159	if (IS_ERR(inode))
160		return PTR_ERR(inode);
161
162	inode->i_op = &bpf_dir_iops;
163	inode->i_fop = &simple_dir_operations;
164
165	inc_nlink(inode);
166	inc_nlink(dir);
167
168	bpf_dentry_finalize(dentry, inode, dir);
169	return 0;
170}
171
172struct map_iter {
173	void *key;
174	bool done;
175};
176
177static struct map_iter *map_iter(struct seq_file *m)
178{
179	return m->private;
180}
181
182static struct bpf_map *seq_file_to_map(struct seq_file *m)
183{
184	return file_inode(m->file)->i_private;
185}
186
187static void map_iter_free(struct map_iter *iter)
188{
189	if (iter) {
190		kfree(iter->key);
191		kfree(iter);
192	}
193}
194
195static struct map_iter *map_iter_alloc(struct bpf_map *map)
196{
197	struct map_iter *iter;
198
199	iter = kzalloc(sizeof(*iter), GFP_KERNEL | __GFP_NOWARN);
200	if (!iter)
201		goto error;
202
203	iter->key = kzalloc(map->key_size, GFP_KERNEL | __GFP_NOWARN);
204	if (!iter->key)
205		goto error;
206
207	return iter;
208
209error:
210	map_iter_free(iter);
211	return NULL;
212}
213
214static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
215{
216	struct bpf_map *map = seq_file_to_map(m);
217	void *key = map_iter(m)->key;
218	void *prev_key;
219
220	(*pos)++;
221	if (map_iter(m)->done)
222		return NULL;
223
224	if (unlikely(v == SEQ_START_TOKEN))
225		prev_key = NULL;
226	else
227		prev_key = key;
228
229	rcu_read_lock();
230	if (map->ops->map_get_next_key(map, prev_key, key)) {
231		map_iter(m)->done = true;
232		key = NULL;
233	}
234	rcu_read_unlock();
235	return key;
236}
237
238static void *map_seq_start(struct seq_file *m, loff_t *pos)
239{
240	if (map_iter(m)->done)
241		return NULL;
242
243	return *pos ? map_iter(m)->key : SEQ_START_TOKEN;
244}
245
246static void map_seq_stop(struct seq_file *m, void *v)
247{
248}
249
250static int map_seq_show(struct seq_file *m, void *v)
251{
252	struct bpf_map *map = seq_file_to_map(m);
253	void *key = map_iter(m)->key;
254
255	if (unlikely(v == SEQ_START_TOKEN)) {
256		seq_puts(m, "# WARNING!! The output is for debug purpose only\n");
257		seq_puts(m, "# WARNING!! The output format will change\n");
258	} else {
259		map->ops->map_seq_show_elem(map, key, m);
260	}
261
262	return 0;
263}
264
265static const struct seq_operations bpffs_map_seq_ops = {
266	.start	= map_seq_start,
267	.next	= map_seq_next,
268	.show	= map_seq_show,
269	.stop	= map_seq_stop,
270};
271
272static int bpffs_map_open(struct inode *inode, struct file *file)
273{
274	struct bpf_map *map = inode->i_private;
275	struct map_iter *iter;
276	struct seq_file *m;
277	int err;
278
279	iter = map_iter_alloc(map);
280	if (!iter)
281		return -ENOMEM;
282
283	err = seq_open(file, &bpffs_map_seq_ops);
284	if (err) {
285		map_iter_free(iter);
286		return err;
287	}
288
289	m = file->private_data;
290	m->private = iter;
291
292	return 0;
293}
294
295static int bpffs_map_release(struct inode *inode, struct file *file)
296{
297	struct seq_file *m = file->private_data;
298
299	map_iter_free(map_iter(m));
300
301	return seq_release(inode, file);
302}
303
304/* bpffs_map_fops should only implement the basic
305 * read operation for a BPF map.  The purpose is to
306 * provide a simple user intuitive way to do
307 * "cat bpffs/pathto/a-pinned-map".
308 *
309 * Other operations (e.g. write, lookup...) should be realized by
310 * the userspace tools (e.g. bpftool) through the
311 * BPF_OBJ_GET_INFO_BY_FD and the map's lookup/update
312 * interface.
313 */
314static const struct file_operations bpffs_map_fops = {
315	.open		= bpffs_map_open,
316	.read		= seq_read,
317	.release	= bpffs_map_release,
318};
319
320static int bpffs_obj_open(struct inode *inode, struct file *file)
321{
322	return -EIO;
323}
324
325static const struct file_operations bpffs_obj_fops = {
326	.open		= bpffs_obj_open,
327};
328
329static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
330			 const struct inode_operations *iops,
331			 const struct file_operations *fops)
332{
333	struct inode *dir = dentry->d_parent->d_inode;
334	struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
335	if (IS_ERR(inode))
336		return PTR_ERR(inode);
337
338	inode->i_op = iops;
339	inode->i_fop = fops;
340	inode->i_private = raw;
341
342	bpf_dentry_finalize(dentry, inode, dir);
343	return 0;
344}
345
346static int bpf_mkprog(struct dentry *dentry, umode_t mode, void *arg)
 
347{
348	return bpf_mkobj_ops(dentry, mode, arg, &bpf_prog_iops,
349			     &bpffs_obj_fops);
350}
351
352static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
353{
354	struct bpf_map *map = arg;
355
356	return bpf_mkobj_ops(dentry, mode, arg, &bpf_map_iops,
357			     bpf_map_support_seq_show(map) ?
358			     &bpffs_map_fops : &bpffs_obj_fops);
359}
360
361static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
362{
363	struct bpf_link *link = arg;
364
365	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
366			     bpf_link_is_iter(link) ?
367			     &bpf_iter_fops : &bpffs_obj_fops);
368}
369
370static struct dentry *
371bpf_lookup(struct inode *dir, struct dentry *dentry, unsigned flags)
372{
373	/* Dots in names (e.g. "/sys/fs/bpf/foo.bar") are reserved for future
374	 * extensions.
375	 */
376	if (strchr(dentry->d_name.name, '.'))
377		return ERR_PTR(-EPERM);
378
379	return simple_lookup(dir, dentry, flags);
380}
381
382static int bpf_symlink(struct inode *dir, struct dentry *dentry,
383		       const char *target)
384{
385	char *link = kstrdup(target, GFP_USER | __GFP_NOWARN);
386	struct inode *inode;
387
388	if (!link)
389		return -ENOMEM;
390
391	inode = bpf_get_inode(dir->i_sb, dir, S_IRWXUGO | S_IFLNK);
392	if (IS_ERR(inode)) {
393		kfree(link);
394		return PTR_ERR(inode);
395	}
396
397	inode->i_op = &simple_symlink_inode_operations;
398	inode->i_link = link;
399
400	bpf_dentry_finalize(dentry, inode, dir);
401	return 0;
402}
403
404static const struct inode_operations bpf_dir_iops = {
405	.lookup		= bpf_lookup,
 
406	.mkdir		= bpf_mkdir,
407	.symlink	= bpf_symlink,
408	.rmdir		= simple_rmdir,
409	.rename		= simple_rename,
410	.link		= simple_link,
411	.unlink		= simple_unlink,
412};
413
414static int bpf_obj_do_pin(const char __user *pathname, void *raw,
415			  enum bpf_type type)
416{
417	struct dentry *dentry;
418	struct inode *dir;
419	struct path path;
420	umode_t mode;
 
421	int ret;
422
423	dentry = user_path_create(AT_FDCWD, pathname, &path, 0);
424	if (IS_ERR(dentry))
425		return PTR_ERR(dentry);
426
427	mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
 
428
429	ret = security_path_mknod(&path, dentry, mode, 0);
430	if (ret)
431		goto out;
432
433	dir = d_inode(path.dentry);
434	if (dir->i_op != &bpf_dir_iops) {
435		ret = -EPERM;
436		goto out;
437	}
438
439	switch (type) {
440	case BPF_TYPE_PROG:
441		ret = vfs_mkobj(dentry, mode, bpf_mkprog, raw);
442		break;
443	case BPF_TYPE_MAP:
444		ret = vfs_mkobj(dentry, mode, bpf_mkmap, raw);
445		break;
446	case BPF_TYPE_LINK:
447		ret = vfs_mkobj(dentry, mode, bpf_mklink, raw);
448		break;
449	default:
450		ret = -EPERM;
451	}
452out:
453	done_path_create(&path, dentry);
454	return ret;
455}
456
457int bpf_obj_pin_user(u32 ufd, const char __user *pathname)
458{
 
459	enum bpf_type type;
460	void *raw;
461	int ret;
462
 
 
 
 
463	raw = bpf_fd_probe_obj(ufd, &type);
464	if (IS_ERR(raw))
465		return PTR_ERR(raw);
 
 
466
467	ret = bpf_obj_do_pin(pathname, raw, type);
468	if (ret != 0)
469		bpf_any_put(raw, type);
470
 
471	return ret;
472}
473
474static void *bpf_obj_do_get(const char __user *pathname,
475			    enum bpf_type *type, int flags)
476{
477	struct inode *inode;
478	struct path path;
479	void *raw;
480	int ret;
481
482	ret = user_path_at(AT_FDCWD, pathname, LOOKUP_FOLLOW, &path);
483	if (ret)
484		return ERR_PTR(ret);
485
486	inode = d_backing_inode(path.dentry);
487	ret = inode_permission(inode, ACC_MODE(flags));
488	if (ret)
489		goto out;
490
491	ret = bpf_inode_type(inode, type);
492	if (ret)
493		goto out;
494
495	raw = bpf_any_get(inode->i_private, *type);
496	if (!IS_ERR(raw))
497		touch_atime(&path);
498
499	path_put(&path);
500	return raw;
501out:
502	path_put(&path);
503	return ERR_PTR(ret);
504}
505
506int bpf_obj_get_user(const char __user *pathname, int flags)
507{
508	enum bpf_type type = BPF_TYPE_UNSPEC;
509	int f_flags;
 
510	void *raw;
511	int ret;
512
513	f_flags = bpf_get_file_flag(flags);
514	if (f_flags < 0)
515		return f_flags;
516
517	raw = bpf_obj_do_get(pathname, &type, f_flags);
518	if (IS_ERR(raw))
519		return PTR_ERR(raw);
 
 
520
521	if (type == BPF_TYPE_PROG)
522		ret = bpf_prog_new_fd(raw);
523	else if (type == BPF_TYPE_MAP)
524		ret = bpf_map_new_fd(raw, f_flags);
525	else if (type == BPF_TYPE_LINK)
526		ret = bpf_link_new_fd(raw);
527	else
528		return -ENOENT;
529
530	if (ret < 0)
531		bpf_any_put(raw, type);
 
 
532	return ret;
533}
534
535static struct bpf_prog *__get_prog_inode(struct inode *inode, enum bpf_prog_type type)
536{
537	struct bpf_prog *prog;
538	int ret = inode_permission(inode, MAY_READ);
539	if (ret)
540		return ERR_PTR(ret);
541
542	if (inode->i_op == &bpf_map_iops)
543		return ERR_PTR(-EINVAL);
544	if (inode->i_op == &bpf_link_iops)
545		return ERR_PTR(-EINVAL);
546	if (inode->i_op != &bpf_prog_iops)
547		return ERR_PTR(-EACCES);
548
549	prog = inode->i_private;
550
551	ret = security_bpf_prog(prog);
552	if (ret < 0)
553		return ERR_PTR(ret);
554
555	if (!bpf_prog_get_ok(prog, &type, false))
556		return ERR_PTR(-EINVAL);
557
558	bpf_prog_inc(prog);
559	return prog;
560}
561
562struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type)
563{
564	struct bpf_prog *prog;
565	struct path path;
566	int ret = kern_path(name, LOOKUP_FOLLOW, &path);
567	if (ret)
568		return ERR_PTR(ret);
569	prog = __get_prog_inode(d_backing_inode(path.dentry), type);
570	if (!IS_ERR(prog))
571		touch_atime(&path);
572	path_put(&path);
573	return prog;
574}
575EXPORT_SYMBOL(bpf_prog_get_type_path);
576
577/*
578 * Display the mount options in /proc/mounts.
579 */
580static int bpf_show_options(struct seq_file *m, struct dentry *root)
581{
582	umode_t mode = d_inode(root)->i_mode & S_IALLUGO & ~S_ISVTX;
583
584	if (mode != S_IRWXUGO)
585		seq_printf(m, ",mode=%o", mode);
586	return 0;
587}
588
589static void bpf_free_inode(struct inode *inode)
590{
591	enum bpf_type type;
592
593	if (S_ISLNK(inode->i_mode))
594		kfree(inode->i_link);
595	if (!bpf_inode_type(inode, &type))
596		bpf_any_put(inode->i_private, type);
597	free_inode_nonrcu(inode);
598}
599
600static const struct super_operations bpf_super_ops = {
601	.statfs		= simple_statfs,
602	.drop_inode	= generic_delete_inode,
603	.show_options	= bpf_show_options,
604	.free_inode	= bpf_free_inode,
605};
606
607enum {
608	OPT_MODE,
 
609};
610
611static const struct fs_parameter_spec bpf_fs_parameters[] = {
612	fsparam_u32oct	("mode",			OPT_MODE),
613	{}
614};
615
616struct bpf_mount_opts {
617	umode_t mode;
618};
619
620static int bpf_parse_param(struct fs_context *fc, struct fs_parameter *param)
621{
622	struct bpf_mount_opts *opts = fc->fs_private;
623	struct fs_parse_result result;
624	int opt;
 
 
625
626	opt = fs_parse(fc, bpf_fs_parameters, param, &result);
627	if (opt < 0)
 
 
 
 
 
 
 
 
 
628		/* We might like to report bad mount options here, but
629		 * traditionally we've ignored all mount options, so we'd
630		 * better continue to ignore non-existing options for bpf.
631		 */
632		return opt == -ENOPARAM ? 0 : opt;
633
634	switch (opt) {
635	case OPT_MODE:
636		opts->mode = result.uint_32 & S_IALLUGO;
637		break;
638	}
639
640	return 0;
641}
642
643static int bpf_fill_super(struct super_block *sb, struct fs_context *fc)
644{
645	static const struct tree_descr bpf_rfiles[] = { { "" } };
646	struct bpf_mount_opts *opts = fc->fs_private;
647	struct inode *inode;
648	int ret;
649
 
 
 
 
 
 
650	ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
651	if (ret)
652		return ret;
653
654	sb->s_op = &bpf_super_ops;
655
656	inode = sb->s_root->d_inode;
657	inode->i_op = &bpf_dir_iops;
658	inode->i_mode &= ~S_IALLUGO;
659	inode->i_mode |= S_ISVTX | opts->mode;
660
661	return 0;
662}
663
664static int bpf_get_tree(struct fs_context *fc)
665{
666	return get_tree_nodev(fc, bpf_fill_super);
667}
668
669static void bpf_free_fc(struct fs_context *fc)
670{
671	kfree(fc->fs_private);
672}
673
674static const struct fs_context_operations bpf_context_ops = {
675	.free		= bpf_free_fc,
676	.parse_param	= bpf_parse_param,
677	.get_tree	= bpf_get_tree,
678};
679
680/*
681 * Set up the filesystem mount context.
682 */
683static int bpf_init_fs_context(struct fs_context *fc)
684{
685	struct bpf_mount_opts *opts;
686
687	opts = kzalloc(sizeof(struct bpf_mount_opts), GFP_KERNEL);
688	if (!opts)
689		return -ENOMEM;
690
691	opts->mode = S_IRWXUGO;
692
693	fc->fs_private = opts;
694	fc->ops = &bpf_context_ops;
695	return 0;
696}
697
698static struct file_system_type bpf_fs_type = {
699	.owner		= THIS_MODULE,
700	.name		= "bpf",
701	.init_fs_context = bpf_init_fs_context,
702	.parameters	= bpf_fs_parameters,
703	.kill_sb	= kill_litter_super,
704};
705
706static int __init bpf_init(void)
707{
708	int ret;
709
710	ret = sysfs_create_mount_point(fs_kobj, "bpf");
711	if (ret)
712		return ret;
713
714	ret = register_filesystem(&bpf_fs_type);
715	if (ret)
716		sysfs_remove_mount_point(fs_kobj, "bpf");
717
718	return ret;
719}
720fs_initcall(bpf_init);