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 CB |
69 | ret = path_from_stashed(&ns->stashed, ns->inum, nsfs_mnt, |
70 | &ns_file_operations, ns, path); | |
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 | ||
1fa08aec CB |
125 | err = path_from_stashed(&relative->stashed, relative->inum, nsfs_mnt, |
126 | &ns_file_operations, relative, &path); | |
127 | if (err <= 0 && err != -EAGAIN) | |
128 | relative->ops->put(relative); | |
ce623f89 | 129 | } while (err == -EAGAIN); |
357ab5b5 | 130 | |
1fa08aec | 131 | if (err < 0) { |
6786741d | 132 | put_unused_fd(fd); |
ce623f89 | 133 | return err; |
6786741d AV |
134 | } |
135 | ||
136 | f = dentry_open(&path, O_RDONLY, current_cred()); | |
137 | path_put(&path); | |
138 | if (IS_ERR(f)) { | |
139 | put_unused_fd(fd); | |
140 | fd = PTR_ERR(f); | |
141 | } else | |
142 | fd_install(fd, f); | |
143 | ||
144 | return fd; | |
145 | } | |
24dce080 | 146 | EXPORT_SYMBOL_GPL(open_related_ns); |
6786741d AV |
147 | |
148 | static long ns_ioctl(struct file *filp, unsigned int ioctl, | |
149 | unsigned long arg) | |
150 | { | |
d95fa3c7 | 151 | struct user_namespace *user_ns; |
6786741d | 152 | struct ns_common *ns = get_proc_ns(file_inode(filp)); |
d95fa3c7 MK |
153 | uid_t __user *argp; |
154 | uid_t uid; | |
6786741d AV |
155 | |
156 | switch (ioctl) { | |
157 | case NS_GET_USERNS: | |
158 | return open_related_ns(ns, ns_get_owner); | |
a7306ed8 AV |
159 | case NS_GET_PARENT: |
160 | if (!ns->ops->get_parent) | |
161 | return -EINVAL; | |
162 | return open_related_ns(ns, ns->ops->get_parent); | |
e5ff5ce6 MK |
163 | case NS_GET_NSTYPE: |
164 | return ns->ops->type; | |
d95fa3c7 MK |
165 | case NS_GET_OWNER_UID: |
166 | if (ns->ops->type != CLONE_NEWUSER) | |
167 | return -EINVAL; | |
168 | user_ns = container_of(ns, struct user_namespace, ns); | |
169 | argp = (uid_t __user *) arg; | |
170 | uid = from_kuid_munged(current_user_ns(), user_ns->owner); | |
171 | return put_user(uid, argp); | |
6786741d AV |
172 | default: |
173 | return -ENOTTY; | |
174 | } | |
175 | } | |
176 | ||
e149ed2b AV |
177 | int ns_get_name(char *buf, size_t size, struct task_struct *task, |
178 | const struct proc_ns_operations *ns_ops) | |
179 | { | |
180 | struct ns_common *ns; | |
181 | int res = -ENOENT; | |
25b14e92 | 182 | const char *name; |
e149ed2b AV |
183 | ns = ns_ops->get(task); |
184 | if (ns) { | |
25b14e92 KT |
185 | name = ns_ops->real_ns_name ? : ns_ops->name; |
186 | res = snprintf(buf, size, "%s:[%u]", name, ns->inum); | |
e149ed2b AV |
187 | ns_ops->put(ns); |
188 | } | |
189 | return res; | |
190 | } | |
191 | ||
303cc571 CB |
192 | bool proc_ns_file(const struct file *file) |
193 | { | |
194 | return file->f_op == &ns_file_operations; | |
195 | } | |
196 | ||
1e2328e7 CN |
197 | /** |
198 | * ns_match() - Returns true if current namespace matches dev/ino provided. | |
39ecb653 | 199 | * @ns: current namespace |
1e2328e7 CN |
200 | * @dev: dev_t from nsfs that will be matched against current nsfs |
201 | * @ino: ino_t from nsfs that will be matched against current nsfs | |
202 | * | |
203 | * Return: true if dev and ino matches the current nsfs. | |
204 | */ | |
205 | bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino) | |
206 | { | |
207 | return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev); | |
208 | } | |
209 | ||
210 | ||
75509fd8 EB |
211 | static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry) |
212 | { | |
213 | struct inode *inode = d_inode(dentry); | |
1fa08aec CB |
214 | const struct ns_common *ns = inode->i_private; |
215 | const struct proc_ns_operations *ns_ops = ns->ops; | |
75509fd8 | 216 | |
6798a8ca JP |
217 | seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino); |
218 | return 0; | |
75509fd8 EB |
219 | } |
220 | ||
e149ed2b AV |
221 | static const struct super_operations nsfs_ops = { |
222 | .statfs = simple_statfs, | |
223 | .evict_inode = nsfs_evict, | |
75509fd8 | 224 | .show_path = nsfs_show_path, |
e149ed2b | 225 | }; |
059b20d9 DH |
226 | |
227 | static int nsfs_init_fs_context(struct fs_context *fc) | |
e149ed2b | 228 | { |
059b20d9 DH |
229 | struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC); |
230 | if (!ctx) | |
231 | return -ENOMEM; | |
232 | ctx->ops = &nsfs_ops; | |
233 | ctx->dops = &ns_dentry_operations; | |
234 | return 0; | |
e149ed2b | 235 | } |
059b20d9 | 236 | |
e149ed2b AV |
237 | static struct file_system_type nsfs = { |
238 | .name = "nsfs", | |
059b20d9 | 239 | .init_fs_context = nsfs_init_fs_context, |
e149ed2b AV |
240 | .kill_sb = kill_anon_super, |
241 | }; | |
242 | ||
243 | void __init nsfs_init(void) | |
244 | { | |
245 | nsfs_mnt = kern_mount(&nsfs); | |
246 | if (IS_ERR(nsfs_mnt)) | |
247 | panic("can't set nsfs up\n"); | |
1751e8a6 | 248 | nsfs_mnt->mnt_sb->s_flags &= ~SB_NOUSER; |
e149ed2b | 249 | } |