1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mount.h>
3 #include <linux/file.h>
4 #include <linux/fs.h>
5 #include <linux/proc_ns.h>
6 #include <linux/magic.h>
7 #include <linux/ktime.h>
8 #include <linux/seq_file.h>
9 #include <linux/user_namespace.h>
10 #include <linux/nsfs.h>
11 #include <linux/uaccess.h>
12 
13 static struct vfsmount *nsfs_mnt;
14 
15 static long ns_ioctl(struct file *filp, unsigned int ioctl,
16 			unsigned long arg);
17 static const struct file_operations ns_file_operations = {
18 	.llseek		= no_llseek,
19 	.unlocked_ioctl = ns_ioctl,
20 };
21 
ns_dname(struct dentry * dentry,char * buffer,int buflen)22 static char *ns_dname(struct dentry *dentry, char *buffer, int buflen)
23 {
24 	struct inode *inode = d_inode(dentry);
25 	const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
26 
27 	return dynamic_dname(dentry, buffer, buflen, "%s:[%lu]",
28 		ns_ops->name, inode->i_ino);
29 }
30 
ns_prune_dentry(struct dentry * dentry)31 static void ns_prune_dentry(struct dentry *dentry)
32 {
33 	struct inode *inode = d_inode(dentry);
34 	if (inode) {
35 		struct ns_common *ns = inode->i_private;
36 		atomic_long_set(&ns->stashed, 0);
37 	}
38 }
39 
40 const struct dentry_operations ns_dentry_operations =
41 {
42 	.d_prune	= ns_prune_dentry,
43 	.d_delete	= always_delete_dentry,
44 	.d_dname	= ns_dname,
45 };
46 
nsfs_evict(struct inode * inode)47 static void nsfs_evict(struct inode *inode)
48 {
49 	struct ns_common *ns = inode->i_private;
50 	clear_inode(inode);
51 	ns->ops->put(ns);
52 }
53 
__ns_get_path(struct path * path,struct ns_common * ns)54 static void *__ns_get_path(struct path *path, struct ns_common *ns)
55 {
56 	struct vfsmount *mnt = nsfs_mnt;
57 	struct dentry *dentry;
58 	struct inode *inode;
59 	unsigned long d;
60 
61 	rcu_read_lock();
62 	d = atomic_long_read(&ns->stashed);
63 	if (!d)
64 		goto slow;
65 	dentry = (struct dentry *)d;
66 	if (!lockref_get_not_dead(&dentry->d_lockref))
67 		goto slow;
68 	rcu_read_unlock();
69 	ns->ops->put(ns);
70 got_it:
71 	path->mnt = mntget(mnt);
72 	path->dentry = dentry;
73 	return NULL;
74 slow:
75 	rcu_read_unlock();
76 	inode = new_inode_pseudo(mnt->mnt_sb);
77 	if (!inode) {
78 		ns->ops->put(ns);
79 		return ERR_PTR(-ENOMEM);
80 	}
81 	inode->i_ino = ns->inum;
82 	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
83 	inode->i_flags |= S_IMMUTABLE;
84 	inode->i_mode = S_IFREG | S_IRUGO;
85 	inode->i_fop = &ns_file_operations;
86 	inode->i_private = ns;
87 
88 	dentry = d_alloc_anon(mnt->mnt_sb);
89 	if (!dentry) {
90 		iput(inode);
91 		return ERR_PTR(-ENOMEM);
92 	}
93 	d_instantiate(dentry, inode);
94 	dentry->d_fsdata = (void *)ns->ops;
95 	d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry);
96 	if (d) {
97 		d_delete(dentry);	/* make sure ->d_prune() does nothing */
98 		dput(dentry);
99 		cpu_relax();
100 		return ERR_PTR(-EAGAIN);
101 	}
102 	goto got_it;
103 }
104 
ns_get_path_cb(struct path * path,ns_get_path_helper_t * ns_get_cb,void * private_data)105 void *ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
106 		     void *private_data)
107 {
108 	struct ns_common *ns;
109 	void *ret;
110 
111 again:
112 	ns = ns_get_cb(private_data);
113 	if (!ns)
114 		return ERR_PTR(-ENOENT);
115 
116 	ret = __ns_get_path(path, ns);
117 	if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
118 		goto again;
119 	return ret;
120 }
121 
122 struct ns_get_path_task_args {
123 	const struct proc_ns_operations *ns_ops;
124 	struct task_struct *task;
125 };
126 
ns_get_path_task(void * private_data)127 static struct ns_common *ns_get_path_task(void *private_data)
128 {
129 	struct ns_get_path_task_args *args = private_data;
130 
131 	return args->ns_ops->get(args->task);
132 }
133 
ns_get_path(struct path * path,struct task_struct * task,const struct proc_ns_operations * ns_ops)134 void *ns_get_path(struct path *path, struct task_struct *task,
135 		  const struct proc_ns_operations *ns_ops)
136 {
137 	struct ns_get_path_task_args args = {
138 		.ns_ops	= ns_ops,
139 		.task	= task,
140 	};
141 
142 	return ns_get_path_cb(path, ns_get_path_task, &args);
143 }
144 
open_related_ns(struct ns_common * ns,struct ns_common * (* get_ns)(struct ns_common * ns))145 int open_related_ns(struct ns_common *ns,
146 		   struct ns_common *(*get_ns)(struct ns_common *ns))
147 {
148 	struct path path = {};
149 	struct file *f;
150 	void *err;
151 	int fd;
152 
153 	fd = get_unused_fd_flags(O_CLOEXEC);
154 	if (fd < 0)
155 		return fd;
156 
157 	while (1) {
158 		struct ns_common *relative;
159 
160 		relative = get_ns(ns);
161 		if (IS_ERR(relative)) {
162 			put_unused_fd(fd);
163 			return PTR_ERR(relative);
164 		}
165 
166 		err = __ns_get_path(&path, relative);
167 		if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
168 			continue;
169 		break;
170 	}
171 	if (IS_ERR(err)) {
172 		put_unused_fd(fd);
173 		return PTR_ERR(err);
174 	}
175 
176 	f = dentry_open(&path, O_RDONLY, current_cred());
177 	path_put(&path);
178 	if (IS_ERR(f)) {
179 		put_unused_fd(fd);
180 		fd = PTR_ERR(f);
181 	} else
182 		fd_install(fd, f);
183 
184 	return fd;
185 }
186 EXPORT_SYMBOL_GPL(open_related_ns);
187 
ns_ioctl(struct file * filp,unsigned int ioctl,unsigned long arg)188 static long ns_ioctl(struct file *filp, unsigned int ioctl,
189 			unsigned long arg)
190 {
191 	struct user_namespace *user_ns;
192 	struct ns_common *ns = get_proc_ns(file_inode(filp));
193 	uid_t __user *argp;
194 	uid_t uid;
195 
196 	switch (ioctl) {
197 	case NS_GET_USERNS:
198 		return open_related_ns(ns, ns_get_owner);
199 	case NS_GET_PARENT:
200 		if (!ns->ops->get_parent)
201 			return -EINVAL;
202 		return open_related_ns(ns, ns->ops->get_parent);
203 	case NS_GET_NSTYPE:
204 		return ns->ops->type;
205 	case NS_GET_OWNER_UID:
206 		if (ns->ops->type != CLONE_NEWUSER)
207 			return -EINVAL;
208 		user_ns = container_of(ns, struct user_namespace, ns);
209 		argp = (uid_t __user *) arg;
210 		uid = from_kuid_munged(current_user_ns(), user_ns->owner);
211 		return put_user(uid, argp);
212 	default:
213 		return -ENOTTY;
214 	}
215 }
216 
ns_get_name(char * buf,size_t size,struct task_struct * task,const struct proc_ns_operations * ns_ops)217 int ns_get_name(char *buf, size_t size, struct task_struct *task,
218 			const struct proc_ns_operations *ns_ops)
219 {
220 	struct ns_common *ns;
221 	int res = -ENOENT;
222 	const char *name;
223 	ns = ns_ops->get(task);
224 	if (ns) {
225 		name = ns_ops->real_ns_name ? : ns_ops->name;
226 		res = snprintf(buf, size, "%s:[%u]", name, ns->inum);
227 		ns_ops->put(ns);
228 	}
229 	return res;
230 }
231 
proc_ns_fget(int fd)232 struct file *proc_ns_fget(int fd)
233 {
234 	struct file *file;
235 
236 	file = fget(fd);
237 	if (!file)
238 		return ERR_PTR(-EBADF);
239 
240 	if (file->f_op != &ns_file_operations)
241 		goto out_invalid;
242 
243 	return file;
244 
245 out_invalid:
246 	fput(file);
247 	return ERR_PTR(-EINVAL);
248 }
249 
nsfs_show_path(struct seq_file * seq,struct dentry * dentry)250 static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
251 {
252 	struct inode *inode = d_inode(dentry);
253 	const struct proc_ns_operations *ns_ops = dentry->d_fsdata;
254 
255 	seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino);
256 	return 0;
257 }
258 
259 static const struct super_operations nsfs_ops = {
260 	.statfs = simple_statfs,
261 	.evict_inode = nsfs_evict,
262 	.show_path = nsfs_show_path,
263 };
nsfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)264 static struct dentry *nsfs_mount(struct file_system_type *fs_type,
265 			int flags, const char *dev_name, void *data)
266 {
267 	return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
268 			&ns_dentry_operations, NSFS_MAGIC);
269 }
270 static struct file_system_type nsfs = {
271 	.name = "nsfs",
272 	.mount = nsfs_mount,
273 	.kill_sb = kill_anon_super,
274 };
275 
nsfs_init(void)276 void __init nsfs_init(void)
277 {
278 	nsfs_mnt = kern_mount(&nsfs);
279 	if (IS_ERR(nsfs_mnt))
280 		panic("can't set nsfs up\n");
281 	nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
282 }
283