blob: 8e159fc78c0a12499536f00b40587b3c4c9c56b0 [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
Andrei Vagin769071a2019-11-12 01:26:52 +000036#ifdef CONFIG_TIME_NS
37 &timens_operations,
38 &timens_for_children_operations,
39#endif
Eric W. Biederman6b4e3062010-03-07 16:41:34 -080040};
41
Al Viro6b255392015-11-17 10:20:54 -050042static const char *proc_ns_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -050043 struct inode *inode,
44 struct delayed_call *done)
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070045{
Al Viro3d3d35b2014-11-01 11:10:28 -040046 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070047 struct task_struct *task;
Eric W. Biedermandb04dc62013-03-09 00:14:45 -080048 struct path ns_path;
Aleksa Saraice623f82019-12-07 01:13:27 +110049 int error = -EACCES;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070050
Al Viro6b255392015-11-17 10:20:54 -050051 if (!dentry)
52 return ERR_PTR(-ECHILD);
53
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070054 task = get_proc_task(inode);
55 if (!task)
Aleksa Saraice623f82019-12-07 01:13:27 +110056 return ERR_PTR(-EACCES);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070057
Aleksa Sarai1bc82072019-12-07 01:13:28 +110058 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
59 goto out;
60
61 error = ns_get_path(&ns_path, task, ns_ops);
62 if (error)
63 goto out;
64
65 error = nd_jump_link(&ns_path);
66out:
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070067 put_task_struct(task);
Aleksa Saraice623f82019-12-07 01:13:27 +110068 return ERR_PTR(error);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070069}
70
71static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
72{
David Howells2b0143b2015-03-17 22:25:59 +000073 struct inode *inode = d_inode(dentry);
Al Viro3d3d35b2014-11-01 11:10:28 -040074 const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070075 struct task_struct *task;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070076 char name[50];
Al Viro5d826c82014-03-14 13:42:45 -040077 int res = -EACCES;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070078
79 task = get_proc_task(inode);
80 if (!task)
Al Viroe149ed22014-11-01 10:57:28 -040081 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070082
Jann Horncaaee622016-01-20 15:00:04 -080083 if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Al Viroe149ed22014-11-01 10:57:28 -040084 res = ns_get_name(name, sizeof(name), task, ns_ops);
85 if (res >= 0)
86 res = readlink_copy(buffer, buflen, name);
87 }
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070088 put_task_struct(task);
Al Viro5d826c82014-03-14 13:42:45 -040089 return res;
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070090}
91
92static const struct inode_operations proc_ns_link_inode_operations = {
93 .readlink = proc_ns_readlink,
Al Viro6b255392015-11-17 10:20:54 -050094 .get_link = proc_ns_get_link,
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -070095 .setattr = proc_setattr,
96};
97
Al Viro0168b9e2018-05-03 09:21:05 -040098static struct dentry *proc_ns_instantiate(struct dentry *dentry,
99 struct task_struct *task, const void *ptr)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800100{
101 const struct proc_ns_operations *ns_ops = ptr;
102 struct inode *inode;
103 struct proc_inode *ei;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800104
Al Viro0168b9e2018-05-03 09:21:05 -0400105 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800106 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -0400107 return ERR_PTR(-ENOENT);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800108
109 ei = PROC_I(inode);
Eric W. Biedermanbf056bf2011-06-18 17:48:18 -0700110 inode->i_op = &proc_ns_link_inode_operations;
Al Viro3d3d35b2014-11-01 11:10:28 -0400111 ei->ns_ops = ns_ops;
Al Viro1bbc5512018-05-02 21:26:16 -0400112 pid_update_inode(task, inode);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800113
Pravin B Shelar1b26c9b2012-03-23 15:02:55 -0700114 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -0400115 return d_splice_alias(inode, dentry);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800116}
117
Al Virof0c3b502013-05-16 12:07:31 -0400118static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800119{
Al Virof0c3b502013-05-16 12:07:31 -0400120 struct task_struct *task = get_proc_task(file_inode(file));
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800121 const struct proc_ns_operations **entry, **last;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800122
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800123 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -0400124 return -ENOENT;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800125
Al Virof0c3b502013-05-16 12:07:31 -0400126 if (!dir_emit_dots(file, ctx))
127 goto out;
128 if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
129 goto out;
130 entry = ns_entries + (ctx->pos - 2);
131 last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
132 while (entry <= last) {
133 const struct proc_ns_operations *ops = *entry;
134 if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
135 proc_ns_instantiate, task, ops))
136 break;
137 ctx->pos++;
138 entry++;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800139 }
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800140out:
141 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -0400142 return 0;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800143}
144
145const struct file_operations proc_ns_dir_operations = {
146 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400147 .iterate_shared = proc_ns_dir_readdir,
148 .llseek = generic_file_llseek,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800149};
150
151static struct dentry *proc_ns_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -0400152 struct dentry *dentry, unsigned int flags)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800153{
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800154 struct task_struct *task = get_proc_task(dir);
155 const struct proc_ns_operations **entry, **last;
156 unsigned int len = dentry->d_name.len;
Al Viro0168b9e2018-05-03 09:21:05 -0400157 struct dentry *res = ERR_PTR(-ENOENT);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800158
159 if (!task)
160 goto out_no_task;
161
Andrew Morton4c619aa2012-03-28 14:42:52 -0700162 last = &ns_entries[ARRAY_SIZE(ns_entries)];
163 for (entry = ns_entries; entry < last; entry++) {
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800164 if (strlen((*entry)->name) != len)
165 continue;
166 if (!memcmp(dentry->d_name.name, (*entry)->name, len))
167 break;
168 }
Andrew Morton4c619aa2012-03-28 14:42:52 -0700169 if (entry == last)
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800170 goto out;
171
Al Viro0168b9e2018-05-03 09:21:05 -0400172 res = proc_ns_instantiate(dentry, task, *entry);
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800173out:
174 put_task_struct(task);
175out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -0400176 return res;
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800177}
178
179const struct inode_operations proc_ns_dir_inode_operations = {
180 .lookup = proc_ns_dir_lookup,
181 .getattr = pid_getattr,
182 .setattr = proc_setattr,
183};