Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e149ed2b | 2 | #include <linux/mount.h> |
059b20d9 | 3 | #include <linux/pseudo_fs.h> |
e149ed2b AV |
4 | #include <linux/file.h> |
5 | #include <linux/fs.h> | |
7bebd69e | 6 | #include <linux/proc_fs.h> |
e149ed2b AV |
7 | #include <linux/proc_ns.h> |
8 | #include <linux/magic.h> | |
9 | #include <linux/ktime.h> | |
75509fd8 | 10 | #include <linux/seq_file.h> |
6786741d AV |
11 | #include <linux/user_namespace.h> |
12 | #include <linux/nsfs.h> | |
d95fa3c7 | 13 | #include <linux/uaccess.h> |
e149ed2b | 14 | |
7bebd69e EB |
15 | #include "internal.h" |
16 | ||
e149ed2b AV |
17 | static struct vfsmount *nsfs_mnt; |
18 | ||
6786741d AV |
19 | static long ns_ioctl(struct file *filp, unsigned int ioctl, |
20 | unsigned long arg); | |
e149ed2b AV |
21 | static const struct file_operations ns_file_operations = { |
22 | .llseek = no_llseek, | |
6786741d | 23 | .unlocked_ioctl = ns_ioctl, |
1cb925c0 | 24 | .compat_ioctl = compat_ptr_ioctl, |
e149ed2b AV |
25 | }; |
26 | ||
27 | static char *ns_dname(struct dentry *dentry, char *buffer, int buflen) | |
28 | { | |
75c3cfa8 | 29 | struct inode *inode = d_inode(dentry); |
1fa08aec CB |
30 | struct ns_common *ns = inode->i_private; |
31 | const struct proc_ns_operations *ns_ops = ns->ops; | |
e149ed2b | 32 | |
0f60d288 | 33 | return dynamic_dname(buffer, buflen, "%s:[%lu]", |
e149ed2b AV |
34 | ns_ops->name, inode->i_ino); |
35 | } | |
36 | ||
37 | static void ns_prune_dentry(struct dentry *dentry) | |
38 | { | |
75c3cfa8 | 39 | struct inode *inode = d_inode(dentry); |
e149ed2b AV |
40 | if (inode) { |
41 | struct ns_common *ns = inode->i_private; | |
1fa08aec | 42 | WRITE_ONCE(ns->stashed, NULL); |
e149ed2b AV |
43 | } |
44 | } | |
45 | ||
46 | const struct dentry_operations ns_dentry_operations = | |
47 | { | |
48 | .d_prune = ns_prune_dentry, | |
49 | .d_delete = always_delete_dentry, | |
50 | .d_dname = ns_dname, | |
51 | }; | |
52 | ||
53 | static void nsfs_evict(struct inode *inode) | |
54 | { | |
55 | struct ns_common *ns = inode->i_private; | |
56 | clear_inode(inode); | |
57 | ns->ops->put(ns); | |
58 | } | |
59 | ||
ce623f89 | 60 | int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb, |
cdab6ba8 | 61 | void *private_data) |
6786741d | 62 | { |
ce623f89 | 63 | int ret; |
6786741d | 64 | |
357ab5b5 AV |
65 | do { |
66 | struct ns_common *ns = ns_get_cb(private_data); | |
67 | if (!ns) | |
ce623f89 | 68 | return -ENOENT; |
1fa08aec | 69 | ret = path_from_stashed(&ns->stashed, ns->inum, nsfs_mnt, |
b28ddcc3 | 70 | &ns_file_operations, NULL, ns, path); |
1fa08aec CB |
71 | if (ret <= 0 && ret != -EAGAIN) |
72 | ns->ops->put(ns); | |
ce623f89 | 73 | } while (ret == -EAGAIN); |
6786741d | 74 | |
1fa08aec CB |
75 | if (ret < 0) |
76 | return ret; | |
77 | ||
78 | return 0; | |
6786741d AV |
79 | } |
80 | ||
cdab6ba8 JK |
81 | struct ns_get_path_task_args { |
82 | const struct proc_ns_operations *ns_ops; | |
83 | struct task_struct *task; | |
84 | }; | |
85 | ||
86 | static struct ns_common *ns_get_path_task(void *private_data) | |
87 | { | |
88 | struct ns_get_path_task_args *args = private_data; | |
89 | ||
90 | return args->ns_ops->get(args->task); | |
91 | } | |
92 | ||
ce623f89 | 93 | int ns_get_path(struct path *path, struct task_struct *task, |
cdab6ba8 JK |
94 | const struct proc_ns_operations *ns_ops) |
95 | { | |
96 | struct ns_get_path_task_args args = { | |
97 | .ns_ops = ns_ops, | |
98 | .task = task, | |
99 | }; | |
100 | ||
101 | return ns_get_path_cb(path, ns_get_path_task, &args); | |
102 | } | |
103 | ||
c62cce2c | 104 | int open_related_ns(struct ns_common *ns, |
6786741d AV |
105 | struct ns_common *(*get_ns)(struct ns_common *ns)) |
106 | { | |
107 | struct path path = {}; | |
108 | struct file *f; | |
ce623f89 | 109 | int err; |
6786741d AV |
110 | int fd; |
111 | ||
112 | fd = get_unused_fd_flags(O_CLOEXEC); | |
113 | if (fd < 0) | |
114 | return fd; | |
115 | ||
357ab5b5 | 116 | do { |
6786741d AV |
117 | struct ns_common *relative; |
118 | ||
119 | relative = get_ns(ns); | |
120 | if (IS_ERR(relative)) { | |
121 | put_unused_fd(fd); | |
122 | return PTR_ERR(relative); | |
123 | } | |
124 | ||
b28ddcc3 CB |
125 | err = path_from_stashed(&relative->stashed, relative->inum, |
126 | nsfs_mnt, &ns_file_operations, NULL, | |
127 | relative, &path); | |
1fa08aec CB |
128 | if (err <= 0 && err != -EAGAIN) |
129 | relative->ops->put(relative); | |
ce623f89 | 130 | } while (err == -EAGAIN); |
357ab5b5 | 131 | |
1fa08aec | 132 | if (err < 0) { |
6786741d | 133 | put_unused_fd(fd); |
ce623f89 | 134 | return err; |
6786741d AV |
135 | } |
136 | ||
137 | f = dentry_open(&path, O_RDONLY, current_cred()); | |
138 | path_put(&path); | |
139 | if (IS_ERR(f)) { | |
140 | put_unused_fd(fd); | |
141 | fd = PTR_ERR(f); | |
142 | } else | |
143 | fd_install(fd, f); | |
144 | ||
145 | return fd; | |
146 | } | |
24dce080 | 147 | EXPORT_SYMBOL_GPL(open_related_ns); |
6786741d AV |
148 | |
149 | static long ns_ioctl(struct file *filp, unsigned int ioctl, | |
150 | unsigned long arg) | |
151 | { | |
d95fa3c7 | 152 | struct user_namespace *user_ns; |
6786741d | 153 | struct ns_common *ns = get_proc_ns(file_inode(filp)); |
d95fa3c7 MK |
154 | uid_t __user *argp; |
155 | uid_t uid; | |
6786741d AV |
156 | |
157 | switch (ioctl) { | |
158 | case NS_GET_USERNS: | |
159 | return open_related_ns(ns, ns_get_owner); | |
a7306ed8 AV |
160 | case NS_GET_PARENT: |
161 | if (!ns->ops->get_parent) | |
162 | return -EINVAL; | |
163 | return open_related_ns(ns, ns->ops->get_parent); | |
e5ff5ce6 MK |
164 | case NS_GET_NSTYPE: |
165 | return ns->ops->type; | |
d95fa3c7 MK |
166 | case NS_GET_OWNER_UID: |
167 | if (ns->ops->type != CLONE_NEWUSER) | |
168 | return -EINVAL; | |
169 | user_ns = container_of(ns, struct user_namespace, ns); | |
170 | argp = (uid_t __user *) arg; | |
171 | uid = from_kuid_munged(current_user_ns(), user_ns->owner); | |
172 | return put_user(uid, argp); | |
6786741d AV |
173 | default: |
174 | return -ENOTTY; | |
175 | } | |
176 | } | |
177 | ||
e149ed2b AV |
178 | int ns_get_name(char *buf, size_t size, struct task_struct *task, |
179 | const struct proc_ns_operations *ns_ops) | |
180 | { | |
181 | struct ns_common *ns; | |
182 | int res = -ENOENT; | |
25b14e92 | 183 | const char *name; |
e149ed2b AV |
184 | ns = ns_ops->get(task); |
185 | if (ns) { | |
25b14e92 KT |
186 | name = ns_ops->real_ns_name ? : ns_ops->name; |
187 | res = snprintf(buf, size, "%s:[%u]", name, ns->inum); | |
e149ed2b AV |
188 | ns_ops->put(ns); |
189 | } | |
190 | return res; | |
191 | } | |
192 | ||
303cc571 CB |
193 | bool proc_ns_file(const struct file *file) |
194 | { | |
195 | return file->f_op == &ns_file_operations; | |
196 | } | |
197 | ||
1e2328e7 CN |
198 | /** |
199 | * ns_match() - Returns true if current namespace matches dev/ino provided. | |
39ecb653 | 200 | * @ns: current namespace |
1e2328e7 CN |
201 | * @dev: dev_t from nsfs that will be matched against current nsfs |
202 | * @ino: ino_t from nsfs that will be matched against current nsfs | |
203 | * | |
204 | * Return: true if dev and ino matches the current nsfs. | |
205 | */ | |
206 | bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino) | |
207 | { | |
208 | return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev); | |
209 | } | |
210 | ||
211 | ||
75509fd8 EB |
212 | static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry) |
213 | { | |
214 | struct inode *inode = d_inode(dentry); | |
1fa08aec CB |
215 | const struct ns_common *ns = inode->i_private; |
216 | const struct proc_ns_operations *ns_ops = ns->ops; | |
75509fd8 | 217 | |
6798a8ca JP |
218 | seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino); |
219 | return 0; | |
75509fd8 EB |
220 | } |
221 | ||
e149ed2b AV |
222 | static const struct super_operations nsfs_ops = { |
223 | .statfs = simple_statfs, | |
224 | .evict_inode = nsfs_evict, | |
75509fd8 | 225 | .show_path = nsfs_show_path, |
e149ed2b | 226 | }; |
059b20d9 DH |
227 | |
228 | static int nsfs_init_fs_context(struct fs_context *fc) | |
e149ed2b | 229 | { |
059b20d9 DH |
230 | struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC); |
231 | if (!ctx) | |
232 | return -ENOMEM; | |
233 | ctx->ops = &nsfs_ops; | |
234 | ctx->dops = &ns_dentry_operations; | |
235 | return 0; | |
e149ed2b | 236 | } |
059b20d9 | 237 | |
e149ed2b AV |
238 | static struct file_system_type nsfs = { |
239 | .name = "nsfs", | |
059b20d9 | 240 | .init_fs_context = nsfs_init_fs_context, |
e149ed2b AV |
241 | .kill_sb = kill_anon_super, |
242 | }; | |
243 | ||
244 | void __init nsfs_init(void) | |
245 | { | |
246 | nsfs_mnt = kern_mount(&nsfs); | |
247 | if (IS_ERR(nsfs_mnt)) | |
248 | panic("can't set nsfs up\n"); | |
1751e8a6 | 249 | nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; |
e149ed2b | 250 | } |