-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathns.c
144 lines (127 loc) · 3.5 KB
/
ns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright © 2020 Arista Networks, Inc. All rights reserved.
*
* Use of this source code is governed by the MIT license that can be found
* in the LICENSE file.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdlib.h>
#include "capable.h"
#include "enter.h"
#include "ns.h"
#include "path.h"
#include "util.h"
static struct {
int flag;
const char *proc_ns_name;
} flags[] = {
[NS_CGROUP] = { BST_CLONE_NEWCGROUP, "cgroup" },
[NS_IPC] = { BST_CLONE_NEWIPC, "ipc", },
[NS_MNT] = { BST_CLONE_NEWNS, "mnt" },
[NS_NET] = { BST_CLONE_NEWNET, "net" },
[NS_PID] = { BST_CLONE_NEWPID, "pid" },
[NS_TIME] = { BST_CLONE_NEWTIME, "time" },
[NS_USER] = { BST_CLONE_NEWUSER, "user" },
[NS_UTS] = { BST_CLONE_NEWUTS, "uts" },
};
const char *ns_name(enum nstype ns)
{
return flags[ns].proc_ns_name;
}
int ns_cloneflag(enum nstype ns)
{
return flags[ns].flag;
}
bool is_nsfd_current(int nsfd, const char *name)
{
const char *path = makepath("/proc/self/ns/%s", name);
struct stat self;
if (stat(path, &self) == -1) {
if (errno == ENOENT) {
/* This namespace is unsupported. */
return false;
}
err(1, "stat %s", path);
}
struct stat stat;
if (fstat(nsfd, &stat) == -1) {
err(1, "fstat %s nsfs", name);
}
return self.st_ino == stat.st_ino;
}
static void ns_enter_one(struct nsid *ns)
{
switch (ns->action) {
case NSACTION_UNSHARE:
if (unshare(flags[ns->ns].flag) == -1) {
if (errno == EINVAL) {
/* We realized that the namespace isn't supported -- remove it
from the unshare set. */
//nsactions[ns->ns] = NSACTION_SHARE_WITH_PARENT;
ns->action = NSACTION_SHARE_WITH_PARENT;
} else {
err(1, "unshare %s", flags[ns->ns].proc_ns_name);
}
}
break;
case NSACTION_SHARE_WITH_PARENT:
break;
default:
if (setns(ns->action, flags[ns->ns].flag) == -1) {
err(1, "setns %s", flags[ns->ns].proc_ns_name);
}
break;
}
}
// Note that for namespaces that want to enter into a specific namespace,
// we actually setns those before forking.
static bool is_postfork_ns(struct nsid *ns)
{
/* For now, only the cgroup namespace needs to be unshared postfork */
return ns->ns == NS_CGROUP;
}
void ns_enter_prefork(struct nsid *namespaces, size_t *len)
{
/* Enter all relevant namespaces. It's hard to check in advance which
namespaces are supported, so we unshare them one by one in order. */
// First we setns the things that have a specific fd to share into.
struct nsid *ns = &namespaces[0];
for (; ns < namespaces + *len; ++ns) {
if (ns->action < 0) {
continue;
}
// Note that we also setns the postfork namespaces here. If they
// have a specific namespace to share into then we must share into
// that namespace while we are still in the user namespace of that
// target namespace.
ns_enter_one(ns);
}
// Then setns the things that just need a blanket unshare (postfork
// namespaces with NSACTION_UNSHARE need to be shard post-fork).
ns = &namespaces[0];
for (; ns < namespaces + *len; ++ns) {
if (is_postfork_ns(ns)) {
continue;
}
if (ns->action >= 0) {
continue;
}
ns_enter_one(ns);
}
}
void ns_enter_postfork(struct nsid *namespaces, size_t len)
{
for (struct nsid *ns = &namespaces[0]; ns < namespaces + len; ++ns) {
if (!is_postfork_ns(ns)) {
// Already handled in ns_enter_prefork.
continue;
}
if (ns->action >= 0) {
// If there is an fd action then we already did this prefork.
continue;
}
ns_enter_one(ns);
}
}