Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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