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)
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 [ Upstream commit 1fde6f21d90f8ba5da3cb9c54ca991ed72696c43 ]
11 /proc entries under /proc/net/* can't be cached into dcache because
12 setns(2) can change current net namespace.
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>
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
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);
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);
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);
54 + ent->proc_dops = &proc_misc_dentry_ops;
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;
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)));
79 +static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
84 +static const struct dentry_operations proc_net_dentry_ops = {
85 + .d_revalidate = proc_net_d_revalidate,
86 + .d_delete = always_delete_dentry,
89 +static void pde_force_lookup(struct proc_dir_entry *pde)
91 + /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
92 + pde->proc_dops = &proc_net_dentry_ops;
95 static int seq_open_net(struct inode *inode, struct file *file)
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);
102 + pde_force_lookup(p);
103 p->proc_fops = &proc_net_seq_fops;
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);
110 + pde_force_lookup(p);
111 p->proc_fops = &proc_net_seq_fops;
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);
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);
126 + pde_force_lookup(p);
127 p->proc_fops = &proc_net_single_fops;
128 p->single_show = show;
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
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
152 diff --git a/tools/testing/selftests/proc/setns-dcache.c b/tools/testing/selftests/proc/setns-dcache.c
154 index 000000000000..60ab197a73fc
156 +++ b/tools/testing/selftests/proc/setns-dcache.c
159 + * Copyright © 2019 Alexey Dobriyan <adobriyan@gmail.com>
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.
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.
174 + * Test that setns(CLONE_NEWNET) points to new /proc/net content even
175 + * if old one is in dcache.
177 + * FIXME /proc/net/unix is under CONFIG_UNIX which can be disabled.
188 +#include <sys/types.h>
189 +#include <sys/stat.h>
191 +#include <sys/socket.h>
193 +static pid_t pid = -1;
198 + kill(pid, SIGTERM);
210 + /* Check for priviledges and syscall availability straight away. */
211 + if (unshare(CLONE_NEWNET) == -1) {
212 + if (errno == ENOSYS || errno == EPERM) {
217 + /* Distinguisher between two otherwise empty net namespaces. */
218 + if (socket(AF_UNIX, SOCK_STREAM, 0) == -1) {
222 + if (pipe(fd) == -1) {
232 + if (unshare(CLONE_NEWNET) == -1) {
236 + if (write(fd[1], &_, 1) != 1) {
245 + if (read(fd[0], &_, 1) != 1) {
251 + snprintf(buf, sizeof(buf), "/proc/%u/ns/net", pid);
252 + nsfd = open(buf, O_RDONLY);
258 + /* Reliably pin dentry into dcache. */
259 + (void)open("/proc/net/unix", O_RDONLY);
261 + if (setns(nsfd, CLONE_NEWNET) == -1) {
265 + kill(pid, SIGTERM);
273 + fd = open("/proc/net/unix", O_RDONLY);
278 +#define S "Num RefCount Protocol Flags Type St Inode Path\n"
279 + rv = read(fd, buf, sizeof(buf));
281 + assert(rv == strlen(S));
282 + assert(memcmp(buf, S, strlen(S)) == 0);