2017-11-01 15:07:57 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2010-03-07 16:41:34 -08:00
|
|
|
#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>
|
2012-07-26 06:24:06 -07:00
|
|
|
#include <linux/user_namespace.h>
|
2010-03-07 16:41:34 -08:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
|
2025-09-09 09:55:06 +02:00
|
|
|
static const struct proc_ns_operations *const ns_entries[] = {
|
2010-03-07 18:14:23 -08:00
|
|
|
#ifdef CONFIG_NET_NS
|
|
|
|
|
&netns_operations,
|
|
|
|
|
#endif
|
2010-03-07 18:43:27 -08:00
|
|
|
#ifdef CONFIG_UTS_NS
|
|
|
|
|
&utsns_operations,
|
|
|
|
|
#endif
|
2010-03-07 18:48:39 -08:00
|
|
|
#ifdef CONFIG_IPC_NS
|
|
|
|
|
&ipcns_operations,
|
|
|
|
|
#endif
|
2010-03-07 18:17:03 -08:00
|
|
|
#ifdef CONFIG_PID_NS
|
|
|
|
|
&pidns_operations,
|
2017-05-08 15:56:41 -07:00
|
|
|
&pidns_for_children_operations,
|
2012-07-26 06:24:06 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef CONFIG_USER_NS
|
|
|
|
|
&userns_operations,
|
2010-03-07 18:17:03 -08:00
|
|
|
#endif
|
2010-03-07 18:49:36 -08:00
|
|
|
&mntns_operations,
|
2016-01-29 02:54:06 -06:00
|
|
|
#ifdef CONFIG_CGROUPS
|
|
|
|
|
&cgroupns_operations,
|
|
|
|
|
#endif
|
2019-11-12 01:26:52 +00:00
|
|
|
#ifdef CONFIG_TIME_NS
|
|
|
|
|
&timens_operations,
|
|
|
|
|
&timens_for_children_operations,
|
|
|
|
|
#endif
|
2010-03-07 16:41:34 -08:00
|
|
|
};
|
|
|
|
|
|
2015-11-17 10:20:54 -05:00
|
|
|
static const char *proc_ns_get_link(struct dentry *dentry,
|
2015-12-29 15:58:39 -05:00
|
|
|
struct inode *inode,
|
|
|
|
|
struct delayed_call *done)
|
2011-06-18 17:48:18 -07:00
|
|
|
{
|
2014-11-01 11:10:28 -04:00
|
|
|
const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
|
2011-06-18 17:48:18 -07:00
|
|
|
struct task_struct *task;
|
2013-03-09 00:14:45 -08:00
|
|
|
struct path ns_path;
|
2019-12-07 01:13:27 +11:00
|
|
|
int error = -EACCES;
|
2011-06-18 17:48:18 -07:00
|
|
|
|
2015-11-17 10:20:54 -05:00
|
|
|
if (!dentry)
|
|
|
|
|
return ERR_PTR(-ECHILD);
|
|
|
|
|
|
2011-06-18 17:48:18 -07:00
|
|
|
task = get_proc_task(inode);
|
|
|
|
|
if (!task)
|
2019-12-07 01:13:27 +11:00
|
|
|
return ERR_PTR(-EACCES);
|
2011-06-18 17:48:18 -07:00
|
|
|
|
2019-12-07 01:13:28 +11:00
|
|
|
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
error = ns_get_path(&ns_path, task, ns_ops);
|
|
|
|
|
if (error)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
error = nd_jump_link(&ns_path);
|
|
|
|
|
out:
|
2011-06-18 17:48:18 -07:00
|
|
|
put_task_struct(task);
|
2019-12-07 01:13:27 +11:00
|
|
|
return ERR_PTR(error);
|
2011-06-18 17:48:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
|
|
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2014-11-01 11:10:28 -04:00
|
|
|
const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
|
2011-06-18 17:48:18 -07:00
|
|
|
struct task_struct *task;
|
|
|
|
|
char name[50];
|
2014-03-14 13:42:45 -04:00
|
|
|
int res = -EACCES;
|
2011-06-18 17:48:18 -07:00
|
|
|
|
|
|
|
|
task = get_proc_task(inode);
|
|
|
|
|
if (!task)
|
2014-11-01 10:57:28 -04:00
|
|
|
return res;
|
2011-06-18 17:48:18 -07:00
|
|
|
|
2016-01-20 15:00:04 -08:00
|
|
|
if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
|
2014-11-01 10:57:28 -04:00
|
|
|
res = ns_get_name(name, sizeof(name), task, ns_ops);
|
|
|
|
|
if (res >= 0)
|
2024-11-20 12:20:34 +01:00
|
|
|
res = readlink_copy(buffer, buflen, name, strlen(name));
|
2014-11-01 10:57:28 -04:00
|
|
|
}
|
2011-06-18 17:48:18 -07:00
|
|
|
put_task_struct(task);
|
2014-03-14 13:42:45 -04:00
|
|
|
return res;
|
2011-06-18 17:48:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct inode_operations proc_ns_link_inode_operations = {
|
|
|
|
|
.readlink = proc_ns_readlink,
|
2015-11-17 10:20:54 -05:00
|
|
|
.get_link = proc_ns_get_link,
|
2026-03-25 07:36:51 +01:00
|
|
|
.setattr = proc_nochmod_setattr,
|
2011-06-18 17:48:18 -07:00
|
|
|
};
|
|
|
|
|
|
2018-05-03 09:21:05 -04:00
|
|
|
static struct dentry *proc_ns_instantiate(struct dentry *dentry,
|
|
|
|
|
struct task_struct *task, const void *ptr)
|
2010-03-07 16:41:34 -08:00
|
|
|
{
|
|
|
|
|
const struct proc_ns_operations *ns_ops = ptr;
|
|
|
|
|
struct inode *inode;
|
|
|
|
|
struct proc_inode *ei;
|
|
|
|
|
|
2018-05-03 09:21:05 -04:00
|
|
|
inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
|
2010-03-07 16:41:34 -08:00
|
|
|
if (!inode)
|
2018-05-03 09:21:05 -04:00
|
|
|
return ERR_PTR(-ENOENT);
|
2010-03-07 16:41:34 -08:00
|
|
|
|
|
|
|
|
ei = PROC_I(inode);
|
2011-06-18 17:48:18 -07:00
|
|
|
inode->i_op = &proc_ns_link_inode_operations;
|
2014-11-01 11:10:28 -04:00
|
|
|
ei->ns_ops = ns_ops;
|
2018-05-02 21:26:16 -04:00
|
|
|
pid_update_inode(task, inode);
|
2010-03-07 16:41:34 -08:00
|
|
|
|
2025-02-24 13:08:52 -05:00
|
|
|
return d_splice_alias_ops(inode, dentry, &pid_dentry_operations);
|
2010-03-07 16:41:34 -08:00
|
|
|
}
|
|
|
|
|
|
2013-05-16 12:07:31 -04:00
|
|
|
static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
|
2010-03-07 16:41:34 -08:00
|
|
|
{
|
2013-05-16 12:07:31 -04:00
|
|
|
struct task_struct *task = get_proc_task(file_inode(file));
|
2025-09-09 09:55:06 +02:00
|
|
|
const struct proc_ns_operations *const *entry, *const *last;
|
2010-03-07 16:41:34 -08:00
|
|
|
|
|
|
|
|
if (!task)
|
2013-05-16 12:07:31 -04:00
|
|
|
return -ENOENT;
|
2010-03-07 16:41:34 -08:00
|
|
|
|
2013-05-16 12:07:31 -04:00
|
|
|
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++;
|
2010-03-07 16:41:34 -08:00
|
|
|
}
|
|
|
|
|
out:
|
|
|
|
|
put_task_struct(task);
|
2013-05-16 12:07:31 -04:00
|
|
|
return 0;
|
2010-03-07 16:41:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const struct file_operations proc_ns_dir_operations = {
|
|
|
|
|
.read = generic_read_dir,
|
2016-04-20 17:13:54 -04:00
|
|
|
.iterate_shared = proc_ns_dir_readdir,
|
|
|
|
|
.llseek = generic_file_llseek,
|
2010-03-07 16:41:34 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct dentry *proc_ns_dir_lookup(struct inode *dir,
|
2012-06-10 17:13:09 -04:00
|
|
|
struct dentry *dentry, unsigned int flags)
|
2010-03-07 16:41:34 -08:00
|
|
|
{
|
|
|
|
|
struct task_struct *task = get_proc_task(dir);
|
2025-09-09 09:55:06 +02:00
|
|
|
const struct proc_ns_operations *const *entry, *const *last;
|
2010-03-07 16:41:34 -08:00
|
|
|
unsigned int len = dentry->d_name.len;
|
2018-05-03 09:21:05 -04:00
|
|
|
struct dentry *res = ERR_PTR(-ENOENT);
|
2010-03-07 16:41:34 -08:00
|
|
|
|
|
|
|
|
if (!task)
|
|
|
|
|
goto out_no_task;
|
|
|
|
|
|
2012-03-28 14:42:52 -07:00
|
|
|
last = &ns_entries[ARRAY_SIZE(ns_entries)];
|
|
|
|
|
for (entry = ns_entries; entry < last; entry++) {
|
2010-03-07 16:41:34 -08:00
|
|
|
if (strlen((*entry)->name) != len)
|
|
|
|
|
continue;
|
|
|
|
|
if (!memcmp(dentry->d_name.name, (*entry)->name, len))
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-03-28 14:42:52 -07:00
|
|
|
if (entry == last)
|
2010-03-07 16:41:34 -08:00
|
|
|
goto out;
|
|
|
|
|
|
2018-05-03 09:21:05 -04:00
|
|
|
res = proc_ns_instantiate(dentry, task, *entry);
|
2010-03-07 16:41:34 -08:00
|
|
|
out:
|
|
|
|
|
put_task_struct(task);
|
|
|
|
|
out_no_task:
|
2018-05-03 09:21:05 -04:00
|
|
|
return res;
|
2010-03-07 16:41:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const struct inode_operations proc_ns_dir_inode_operations = {
|
|
|
|
|
.lookup = proc_ns_dir_lookup,
|
|
|
|
|
.getattr = pid_getattr,
|
2026-03-25 07:36:51 +01:00
|
|
|
.setattr = proc_nochmod_setattr,
|
2010-03-07 16:41:34 -08:00
|
|
|
};
|