namespaces.c 4.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10
#include <linux/proc_fs.h>
#include <linux/nsproxy.h>
#include <linux/ptrace.h>
#include <linux/namei.h>
#include <linux/file.h>
#include <linux/utsname.h>
#include <net/net_namespace.h>
#include <linux/ipc_namespace.h>
#include <linux/pid_namespace.h>
11
#include <linux/user_namespace.h>
12 13 14 15
#include "internal.h"


static const struct proc_ns_operations *ns_entries[] = {
16 17 18
#ifdef CONFIG_NET_NS
	&netns_operations,
#endif
19 20 21
#ifdef CONFIG_UTS_NS
	&utsns_operations,
#endif
22 23 24
#ifdef CONFIG_IPC_NS
	&ipcns_operations,
#endif
25 26
#ifdef CONFIG_PID_NS
	&pidns_operations,
27
	&pidns_for_children_operations,
28 29 30
#endif
#ifdef CONFIG_USER_NS
	&userns_operations,
31
#endif
32
	&mntns_operations,
33 34 35
#ifdef CONFIG_CGROUPS
	&cgroupns_operations,
#endif
36 37
};

38
static const char *proc_ns_get_link(struct dentry *dentry,
39 40
				    struct inode *inode,
				    struct delayed_call *done)
41
{
Al Viro's avatar
Al Viro committed
42
	const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
43
	struct task_struct *task;
44
	struct path ns_path;
45 46
	void *error = ERR_PTR(-EACCES);

47 48 49
	if (!dentry)
		return ERR_PTR(-ECHILD);

50 51
	task = get_proc_task(inode);
	if (!task)
52
		return error;
53

54
	if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
55 56
		error = ns_get_path(&ns_path, task, ns_ops);
		if (!error)
57
			nd_jump_link(&ns_path);
58 59 60 61 62 63 64
	}
	put_task_struct(task);
	return error;
}

static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
{
65
	struct inode *inode = d_inode(dentry);
Al Viro's avatar
Al Viro committed
66
	const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
67 68
	struct task_struct *task;
	char name[50];
Al Viro's avatar
Al Viro committed
69
	int res = -EACCES;
70 71 72

	task = get_proc_task(inode);
	if (!task)
73
		return res;
74

75
	if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
76 77 78 79
		res = ns_get_name(name, sizeof(name), task, ns_ops);
		if (res >= 0)
			res = readlink_copy(buffer, buflen, name);
	}
80
	put_task_struct(task);
Al Viro's avatar
Al Viro committed
81
	return res;
82 83 84 85
}

static const struct inode_operations proc_ns_link_inode_operations = {
	.readlink	= proc_ns_readlink,
86
	.get_link	= proc_ns_get_link,
87 88 89
	.setattr	= proc_setattr,
};

90 91
static struct dentry *proc_ns_instantiate(struct dentry *dentry,
	struct task_struct *task, const void *ptr)
92 93 94 95 96
{
	const struct proc_ns_operations *ns_ops = ptr;
	struct inode *inode;
	struct proc_inode *ei;

97
	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
98
	if (!inode)
99
		return ERR_PTR(-ENOENT);
100 101

	ei = PROC_I(inode);
102
	inode->i_op = &proc_ns_link_inode_operations;
Al Viro's avatar
Al Viro committed
103
	ei->ns_ops = ns_ops;
104
	pid_update_inode(task, inode);
105

106
	d_set_d_op(dentry, &pid_dentry_operations);
107
	return d_splice_alias(inode, dentry);
108 109
}

Al Viro's avatar
Al Viro committed
110
static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
111
{
Al Viro's avatar
Al Viro committed
112
	struct task_struct *task = get_proc_task(file_inode(file));
113 114 115
	const struct proc_ns_operations **entry, **last;

	if (!task)
Al Viro's avatar
Al Viro committed
116
		return -ENOENT;
117

Al Viro's avatar
Al Viro committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131
	if (!dir_emit_dots(file, ctx))
		goto out;
	if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
		goto out;
	entry = ns_entries + (ctx->pos - 2);
	last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
	while (entry <= last) {
		const struct proc_ns_operations *ops = *entry;
		if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
				     proc_ns_instantiate, task, ops))
			break;
		ctx->pos++;
		entry++;
	}
132 133
out:
	put_task_struct(task);
Al Viro's avatar
Al Viro committed
134
	return 0;
135 136 137 138
}

const struct file_operations proc_ns_dir_operations = {
	.read		= generic_read_dir,
139 140
	.iterate_shared	= proc_ns_dir_readdir,
	.llseek		= generic_file_llseek,
141 142 143
};

static struct dentry *proc_ns_dir_lookup(struct inode *dir,
144
				struct dentry *dentry, unsigned int flags)
145 146 147 148
{
	struct task_struct *task = get_proc_task(dir);
	const struct proc_ns_operations **entry, **last;
	unsigned int len = dentry->d_name.len;
149
	struct dentry *res = ERR_PTR(-ENOENT);
150 151 152 153

	if (!task)
		goto out_no_task;

154 155
	last = &ns_entries[ARRAY_SIZE(ns_entries)];
	for (entry = ns_entries; entry < last; entry++) {
156 157 158 159 160
		if (strlen((*entry)->name) != len)
			continue;
		if (!memcmp(dentry->d_name.name, (*entry)->name, len))
			break;
	}
161
	if (entry == last)
162 163
		goto out;

164
	res = proc_ns_instantiate(dentry, task, *entry);
165 166 167
out:
	put_task_struct(task);
out_no_task:
168
	return res;
169 170 171 172 173 174 175
}

const struct inode_operations proc_ns_dir_inode_operations = {
	.lookup		= proc_ns_dir_lookup,
	.getattr	= pid_getattr,
	.setattr	= proc_setattr,
};