-
Notifications
You must be signed in to change notification settings - Fork 1
/
execns.c
executable file
·227 lines (178 loc) · 4.33 KB
/
execns.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Tool running a program in the namespaces of a running process
Author: R. Koucha
Date: 22-Jan-2020
*/
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include "util.h"
#define DEFAULT_CMD "/bin/sh"
#define NB_NS_NAME 7
struct ns_list_t {
char *name;
int selected;
int flag;
} ns_list[NB_NS_NAME] = {
{ "ipc", 0, CLONE_NEWIPC },
{ "pid", 0, CLONE_NEWPID },
{ "net", 0, CLONE_NEWNET },
{ "user", 0, CLONE_NEWUSER },
{ "uts", 0, CLONE_NEWUTS },
{ "cgroup", 0, CLONE_NEWCGROUP },
{ "mnt", 0, CLONE_NEWNS }
};
// Set the "selected" field in ns_list[] for the given
// namespace or all (if keyword "all" is passed)
static int select_ns(const char *ns)
{
int i, fnd;
fnd = 0;
for (i = 0; i < NB_NS_NAME; i ++) {
if (!strcmp("all", ns)) {
ns_list[i].selected = 1;
fnd = 1;
} else {
if (!strcmp(ns_list[i].name, ns)) {
ns_list[i].selected = 1;
return 0;
}
}
} // End for
// Bad namespace name
if (!fnd) {
return -1;
}
return 0;
} // select_ns
int main(int ac, char *av[])
{
pid_t my_pid, tpid, child;
int n, i, fd, rc;
char *p, *p1, *p2;
char tpath[256];
if (ac < 2) {
fprintf(stderr, "Usage: %s pid:ns_name1:ns_name2... [program parameters]\n", basename(av[0]));
return 1;
}
// Target pid
p = strdup(av[1]);
p1 = p;
while (*p1) {
if (*p1 == ':') {
break;
}
p1 ++;
} // End while
// If no namespaces on command line, select all of them
if (*p1 != ':') {
select_ns("all");
n = NB_NS_NAME;
} else {
*p1 = '\0';
p1 ++;
// Get the requested namespaces
p2 = p1;
n = 0;
while (*p2) {
p2 = strchr(p2, ':');
if (p2) {
n += 1;
*p2 = '\0';
if (select_ns(p1) != 0) {
ERR("Bad namespace name '%s'\n", p1);
return 1;
}
p2 ++;
p1 = p2;
} else {
// Last name
n += 1;
if (select_ns(p1) != 0) {
ERR("Bad namespace name '%s'\n", p1);
return 1;
}
break;
}
} // End while
if (!n) {
select_ns("all");
n = NB_NS_NAME;
}
} // End if no namespace specified
// Target pid
if (!is_pid(p)) {
fprintf(stderr, "Parameter '%s' must be a pid\n", p);
return 1;
}
tpid = atoi(p);
free(p);
// My pid
my_pid = getpid();
// For each namespace of the target process:
// . Build the pathname to the symbolic link
// . Get the identifier and compare it to the calling process one
// . If different, enter into the namespace
for (i = 0; i < NB_NS_NAME; i ++) {
// If this namespace is requested
if (ns_list[i].selected) {
// Compare the identifiers
if (cmp_ns(my_pid, tpid, ns_list[i].name)) {
// Don't do anything if same namespace
continue;
}
// Build the pathname
snprintf(tpath, sizeof(tpath), "/proc/%d/ns/%s", tpid, ns_list[i].name);
// Open the target namespace symbolic link
fd = open(tpath, O_RDONLY);
if (fd < 0) {
ERR("open(%s): '%m (%d)\n", tpath, errno);
return 1;
}
// Enter into the target namespace
rc = setns(fd, ns_list[i].flag);
if (rc != 0) {
ERR("Unable to enter into namespace '%s' (%m-%d)\n", ns_list[i].name, errno);
return 1;
} else {
printf("Moved into namespace %s\n", ns_list[i].name);
}
close(fd);
} // End if namespace selected
} // End for
// Fork a child process
child = fork();
if (!child) {
// Child process
char *cmd;
// If no command is passed
if (ac < 3) {
char *av_cmd[] = { DEFAULT_CMD, NULL };
cmd = av_cmd[0];
execv(cmd, av_cmd);
} else {
cmd = av[2];
execv(cmd, &(av[2]));
}
ERR("Unable to run '%s': %m (%d)\n", cmd, errno);
_exit(1);
} else {
// Father process
int status;
// Wait for the end of the child process
rc = waitpid(child, &status, 0);
if (rc < 0) {
ERR("waitpid(%d): %m (%d)", child, errno);
return 1;
}
printf("program's status: %d (0x%x)\n", status, status);
}
return 0;
} // main