blob: 59b17e509f4633c31d86fa34425838bc390a6a88 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002#include <linux/proc_fs.h>
3#include <linux/nsproxy.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08004#include <linux/ptrace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08005#include <linux/namei.h>
6#include <linux/file.h>
7#include <linux/utsname.h>
8#include <net/net_namespace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08009#include <linux/ipc_namespace.h>
10#include <linux/pid_namespace.h>
Eric W. Biedermancde19752012-07-26 06:24:06 -070011#include <linux/user_namespace.h>
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080012#include "internal.h"
13
14
15static const struct proc_ns_operations *ns_entries[] = {
Eric W. Biederman13b6f572010-03-07 18:14:23 -080016#ifdef CONFIG_NET_NS
17 &netns_operations,
18#endif
Eric W. Biederman34482e82010-03-07 18:43:27 -080019#ifdef CONFIG_UTS_NS
20 &utsns_operations,
21#endif
Eric W. Biedermana00eaf12010-03-07 18:48:39 -080022#ifdef CONFIG_IPC_NS
23 &ipcns_operations,
24#endif
Eric W. Biederman57e83912010-03-07 18:17:03 -080025#ifdef CONFIG_PID_NS
26 &pidns_operations,
Kirill Tkhaieaa0d192017-05-08 15:56:41 -070027 &pidns_for_children_operations,
Eric W. Biederman57e83912010-03-07 18:17:03 -080028#endif
Eric W. Biedermancde19752012-07-26 06:24:06 -070029#ifdef CONFIG_USER_NS
30 &userns_operations,
31#endif
Eric W. Biederman8823c072010-03-07 18:49:36 -080032 &mntns_operations,
Aditya Kalia79a9082016-01-29 02:54:06 -060033#ifdef CONFIG_CGROUPS
34 &cgroupns_operations,
35#endif
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080036};
37
Al Viro6b255392015-11-17 10:20:54 -050038static const char *proc_ns_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -050039 struct inode *inode,
40 struct delayed_call *done)
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070041{
Al Viro3d3d35b2014-11-01 11:10:28 -040042 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070043 struct task_struct *task;
Eric W. Biedermandb04dc62013-03-09 00:14:45 -080044 struct path ns_path;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070045 void *error = ERR_PTR(-EACCES);
46
Al Viro6b255392015-11-17 10:20:54 -050047 if (!dentry)
48 return ERR_PTR(-ECHILD);
49
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070050 task = get_proc_task(inode);
51 if (!task)
Al Viroe149ed22014-11-01 10:57:28 -040052 return error;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070053
Jann Horncaaee622016-01-20 15:00:04 -080054 if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Al Viroe149ed22014-11-01 10:57:28 -040055 error = ns_get_path(&ns_path, task, ns_ops);
56 if (!error)
Al Viro6e77137b2015-05-02 13:37:52 -040057 nd_jump_link(&ns_path);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070058 }
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070059 put_task_struct(task);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070060 return error;
61}
62
63static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
64{
David Howells2b0143b2015-03-17 22:25:59 +000065 struct inode *inode = d_inode(dentry);
Al Viro3d3d35b2014-11-01 11:10:28 -040066 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070067 struct task_struct *task;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070068 char name[50];
Al Viro5d826c82014-03-14 13:42:45 -040069 int res = -EACCES;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070070
71 task = get_proc_task(inode);
72 if (!task)
Al Viroe149ed22014-11-01 10:57:28 -040073 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070074
Jann Horncaaee622016-01-20 15:00:04 -080075 if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Al Viroe149ed22014-11-01 10:57:28 -040076 res = ns_get_name(name, sizeof(name), task, ns_ops);
77 if (res >= 0)
78 res = readlink_copy(buffer, buflen, name);
79 }
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070080 put_task_struct(task);
Al Viro5d826c82014-03-14 13:42:45 -040081 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070082}
83
84static const struct inode_operations proc_ns_link_inode_operations = {
85 .readlink = proc_ns_readlink,
Al Viro6b255392015-11-17 10:20:54 -050086 .get_link = proc_ns_get_link,
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070087 .setattr = proc_setattr,
88};
89
Al Viroc52a47a2013-06-15 11:15:20 +040090static int proc_ns_instantiate(struct inode *dir,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080091 struct dentry *dentry, struct task_struct *task, const void *ptr)
92{
93 const struct proc_ns_operations *ns_ops = ptr;
94 struct inode *inode;
95 struct proc_inode *ei;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080096
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +010097 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK | S_IRWXUGO);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080098 if (!inode)
99 goto out;
100
101 ei = PROC_I(inode);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -0700102 inode->i_op = &proc_ns_link_inode_operations;
Al Viro3d3d35b2014-11-01 11:10:28 -0400103 ei->ns_ops = ns_ops;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800104
Pravin B Shelar1b26c9b2012-03-23 15:02:55 -0700105 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800106 d_add(dentry, inode);
107 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -0400108 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +0400109 return 0;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800110out:
Al Viroc52a47a2013-06-15 11:15:20 +0400111 return -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800112}
113
Al Virof0c3b502013-05-16 12:07:31 -0400114static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800115{
Al Virof0c3b502013-05-16 12:07:31 -0400116 struct task_struct *task = get_proc_task(file_inode(file));
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800117 const struct proc_ns_operations **entry, **last;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800118
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800119 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -0400120 return -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800121
Al Virof0c3b502013-05-16 12:07:31 -0400122 if (!dir_emit_dots(file, ctx))
123 goto out;
124 if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
125 goto out;
126 entry = ns_entries + (ctx->pos - 2);
127 last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
128 while (entry <= last) {
129 const struct proc_ns_operations *ops = *entry;
130 if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
131 proc_ns_instantiate, task, ops))
132 break;
133 ctx->pos++;
134 entry++;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800135 }
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800136out:
137 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -0400138 return 0;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800139}
140
141const struct file_operations proc_ns_dir_operations = {
142 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400143 .iterate_shared = proc_ns_dir_readdir,
144 .llseek = generic_file_llseek,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800145};
146
147static struct dentry *proc_ns_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -0400148 struct dentry *dentry, unsigned int flags)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800149{
Al Viroc52a47a2013-06-15 11:15:20 +0400150 int error;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800151 struct task_struct *task = get_proc_task(dir);
152 const struct proc_ns_operations **entry, **last;
153 unsigned int len = dentry->d_name.len;
154
Al Viroc52a47a2013-06-15 11:15:20 +0400155 error = -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800156
157 if (!task)
158 goto out_no_task;
159
Andrew Morton4c619aa2012-03-28 14:42:52 -0700160 last = &ns_entries[ARRAY_SIZE(ns_entries)];
161 for (entry = ns_entries; entry < last; entry++) {
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800162 if (strlen((*entry)->name) != len)
163 continue;
164 if (!memcmp(dentry->d_name.name, (*entry)->name, len))
165 break;
166 }
Andrew Morton4c619aa2012-03-28 14:42:52 -0700167 if (entry == last)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800168 goto out;
169
170 error = proc_ns_instantiate(dir, dentry, task, *entry);
171out:
172 put_task_struct(task);
173out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +0400174 return ERR_PTR(error);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800175}
176
177const struct inode_operations proc_ns_dir_inode_operations = {
178 .lookup = proc_ns_dir_lookup,
179 .getattr = pid_getattr,
180 .setattr = proc_setattr,
181};