]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.19.29/proc-fix-proc-net-after-setns-2.patch
fixes for 4.19
[thirdparty/kernel/stable-queue.git] / releases / 4.19.29 / proc-fix-proc-net-after-setns-2.patch
1 From b5eedf57e9d482f4426b037bc4c10379c10fb05d Mon Sep 17 00:00:00 2001
2 From: Alexey Dobriyan <adobriyan@gmail.com>
3 Date: Fri, 1 Feb 2019 14:20:01 -0800
4 Subject: proc: fix /proc/net/* after setns(2)
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 [ Upstream commit 1fde6f21d90f8ba5da3cb9c54ca991ed72696c43 ]
10
11 /proc entries under /proc/net/* can't be cached into dcache because
12 setns(2) can change current net namespace.
13
14 [akpm@linux-foundation.org: coding-style fixes]
15 [akpm@linux-foundation.org: avoid vim miscolorization]
16 [adobriyan@gmail.com: write test, add dummy ->d_revalidate hook: necessary if /proc/net/* is pinned at setns time]
17 Link: http://lkml.kernel.org/r/20190108192350.GA12034@avx2
18 Link: http://lkml.kernel.org/r/20190107162336.GA9239@avx2
19 Fixes: 1da4d377f943fe4194ffb9fb9c26cc58fad4dd24 ("proc: revalidate misc dentries")
20 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
21 Reported-by: Mateusz Stępień <mateusz.stepien@netrounds.com>
22 Reported-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
23 Cc: Al Viro <viro@zeniv.linux.org.uk>
24 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
25 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
26 Signed-off-by: Sasha Levin <sashal@kernel.org>
27 ---
28 fs/proc/generic.c | 4 +-
29 fs/proc/internal.h | 1 +
30 fs/proc/proc_net.c | 20 +++
31 tools/testing/selftests/proc/.gitignore | 1 +
32 tools/testing/selftests/proc/Makefile | 1 +
33 tools/testing/selftests/proc/setns-dcache.c | 129 ++++++++++++++++++++
34 6 files changed, 155 insertions(+), 1 deletion(-)
35 create mode 100644 tools/testing/selftests/proc/setns-dcache.c
36
37 diff --git a/fs/proc/generic.c b/fs/proc/generic.c
38 index 8ae109429a88..e39bac94dead 100644
39 --- a/fs/proc/generic.c
40 +++ b/fs/proc/generic.c
41 @@ -256,7 +256,7 @@ struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
42 inode = proc_get_inode(dir->i_sb, de);
43 if (!inode)
44 return ERR_PTR(-ENOMEM);
45 - d_set_d_op(dentry, &proc_misc_dentry_ops);
46 + d_set_d_op(dentry, de->proc_dops);
47 return d_splice_alias(inode, dentry);
48 }
49 read_unlock(&proc_subdir_lock);
50 @@ -429,6 +429,8 @@ static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
51 INIT_LIST_HEAD(&ent->pde_openers);
52 proc_set_user(ent, (*parent)->uid, (*parent)->gid);
53
54 + ent->proc_dops = &proc_misc_dentry_ops;
55 +
56 out:
57 return ent;
58 }
59 diff --git a/fs/proc/internal.h b/fs/proc/internal.h
60 index 5185d7f6a51e..95b14196f284 100644
61 --- a/fs/proc/internal.h
62 +++ b/fs/proc/internal.h
63 @@ -44,6 +44,7 @@ struct proc_dir_entry {
64 struct completion *pde_unload_completion;
65 const struct inode_operations *proc_iops;
66 const struct file_operations *proc_fops;
67 + const struct dentry_operations *proc_dops;
68 union {
69 const struct seq_operations *seq_ops;
70 int (*single_show)(struct seq_file *, void *);
71 diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c
72 index d5e0fcb3439e..a7b12435519e 100644
73 --- a/fs/proc/proc_net.c
74 +++ b/fs/proc/proc_net.c
75 @@ -38,6 +38,22 @@ static struct net *get_proc_net(const struct inode *inode)
76 return maybe_get_net(PDE_NET(PDE(inode)));
77 }
78
79 +static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
80 +{
81 + return 0;
82 +}
83 +
84 +static const struct dentry_operations proc_net_dentry_ops = {
85 + .d_revalidate = proc_net_d_revalidate,
86 + .d_delete = always_delete_dentry,
87 +};
88 +
89 +static void pde_force_lookup(struct proc_dir_entry *pde)
90 +{
91 + /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
92 + pde->proc_dops = &proc_net_dentry_ops;
93 +}
94 +
95 static int seq_open_net(struct inode *inode, struct file *file)
96 {
97 unsigned int state_size = PDE(inode)->state_size;
98 @@ -90,6 +106,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
99 p = proc_create_reg(name, mode, &parent, data);
100 if (!p)
101 return NULL;
102 + pde_force_lookup(p);
103 p->proc_fops = &proc_net_seq_fops;
104 p->seq_ops = ops;
105 p->state_size = state_size;
106 @@ -133,6 +150,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode
107 p = proc_create_reg(name, mode, &parent, data);
108 if (!p)
109 return NULL;
110 + pde_force_lookup(p);
111 p->proc_fops = &proc_net_seq_fops;
112 p->seq_ops = ops;
113 p->state_size = state_size;
114 @@ -181,6 +199,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
115 p = proc_create_reg(name, mode, &parent, data);
116 if (!p)
117 return NULL;
118 + pde_force_lookup(p);
119 p->proc_fops = &proc_net_single_fops;
120 p->single_show = show;
121 return proc_register(parent, p);
122 @@ -223,6 +242,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
123 p = proc_create_reg(name, mode, &parent, data);
124 if (!p)
125 return NULL;
126 + pde_force_lookup(p);
127 p->proc_fops = &proc_net_single_fops;
128 p->single_show = show;
129 p->write = write;
130 diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
131 index 82121a81681f..29bac5ef9a93 100644
132 --- a/tools/testing/selftests/proc/.gitignore
133 +++ b/tools/testing/selftests/proc/.gitignore
134 @@ -10,4 +10,5 @@
135 /proc-uptime-002
136 /read
137 /self
138 +/setns-dcache
139 /thread-self
140 diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
141 index 1c12c34cf85d..434d033ee067 100644
142 --- a/tools/testing/selftests/proc/Makefile
143 +++ b/tools/testing/selftests/proc/Makefile
144 @@ -14,6 +14,7 @@ TEST_GEN_PROGS += proc-uptime-001
145 TEST_GEN_PROGS += proc-uptime-002
146 TEST_GEN_PROGS += read
147 TEST_GEN_PROGS += self
148 +TEST_GEN_PROGS += setns-dcache
149 TEST_GEN_PROGS += thread-self
150
151 include ../lib.mk
152 diff --git a/tools/testing/selftests/proc/setns-dcache.c b/tools/testing/selftests/proc/setns-dcache.c
153 new file mode 100644
154 index 000000000000..60ab197a73fc
155 --- /dev/null
156 +++ b/tools/testing/selftests/proc/setns-dcache.c
157 @@ -0,0 +1,129 @@
158 +/*
159 + * Copyright © 2019 Alexey Dobriyan <adobriyan@gmail.com>
160 + *
161 + * Permission to use, copy, modify, and distribute this software for any
162 + * purpose with or without fee is hereby granted, provided that the above
163 + * copyright notice and this permission notice appear in all copies.
164 + *
165 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
166 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
167 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
168 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
169 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
170 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
171 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
172 + */
173 +/*
174 + * Test that setns(CLONE_NEWNET) points to new /proc/net content even
175 + * if old one is in dcache.
176 + *
177 + * FIXME /proc/net/unix is under CONFIG_UNIX which can be disabled.
178 + */
179 +#undef NDEBUG
180 +#include <assert.h>
181 +#include <errno.h>
182 +#include <sched.h>
183 +#include <signal.h>
184 +#include <stdio.h>
185 +#include <stdlib.h>
186 +#include <string.h>
187 +#include <unistd.h>
188 +#include <sys/types.h>
189 +#include <sys/stat.h>
190 +#include <fcntl.h>
191 +#include <sys/socket.h>
192 +
193 +static pid_t pid = -1;
194 +
195 +static void f(void)
196 +{
197 + if (pid > 0) {
198 + kill(pid, SIGTERM);
199 + }
200 +}
201 +
202 +int main(void)
203 +{
204 + int fd[2];
205 + char _ = 0;
206 + int nsfd;
207 +
208 + atexit(f);
209 +
210 + /* Check for priviledges and syscall availability straight away. */
211 + if (unshare(CLONE_NEWNET) == -1) {
212 + if (errno == ENOSYS || errno == EPERM) {
213 + return 4;
214 + }
215 + return 1;
216 + }
217 + /* Distinguisher between two otherwise empty net namespaces. */
218 + if (socket(AF_UNIX, SOCK_STREAM, 0) == -1) {
219 + return 1;
220 + }
221 +
222 + if (pipe(fd) == -1) {
223 + return 1;
224 + }
225 +
226 + pid = fork();
227 + if (pid == -1) {
228 + return 1;
229 + }
230 +
231 + if (pid == 0) {
232 + if (unshare(CLONE_NEWNET) == -1) {
233 + return 1;
234 + }
235 +
236 + if (write(fd[1], &_, 1) != 1) {
237 + return 1;
238 + }
239 +
240 + pause();
241 +
242 + return 0;
243 + }
244 +
245 + if (read(fd[0], &_, 1) != 1) {
246 + return 1;
247 + }
248 +
249 + {
250 + char buf[64];
251 + snprintf(buf, sizeof(buf), "/proc/%u/ns/net", pid);
252 + nsfd = open(buf, O_RDONLY);
253 + if (nsfd == -1) {
254 + return 1;
255 + }
256 + }
257 +
258 + /* Reliably pin dentry into dcache. */
259 + (void)open("/proc/net/unix", O_RDONLY);
260 +
261 + if (setns(nsfd, CLONE_NEWNET) == -1) {
262 + return 1;
263 + }
264 +
265 + kill(pid, SIGTERM);
266 + pid = 0;
267 +
268 + {
269 + char buf[4096];
270 + ssize_t rv;
271 + int fd;
272 +
273 + fd = open("/proc/net/unix", O_RDONLY);
274 + if (fd == -1) {
275 + return 1;
276 + }
277 +
278 +#define S "Num RefCount Protocol Flags Type St Inode Path\n"
279 + rv = read(fd, buf, sizeof(buf));
280 +
281 + assert(rv == strlen(S));
282 + assert(memcmp(buf, S, strlen(S)) == 0);
283 + }
284 +
285 + return 0;
286 +}
287 --
288 2.19.1
289