forked from chaosblade-io/chaosblade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsexec.c
154 lines (135 loc) · 3.3 KB
/
nsexec.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
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
extern char** environ;
int enter_ns(int pid, const char* type) {
#ifdef __NR_setns
char path[64], selfpath[64];
snprintf(path, sizeof(path), "/proc/%d/ns/%s", pid, type);
snprintf(selfpath, sizeof(selfpath), "/proc/self/ns/%s", type);
struct stat oldns_stat, newns_stat;
if (stat(selfpath, &oldns_stat) == 0 && stat(path, &newns_stat) == 0) {
// Don't try to call setns() if we're in the same namespace already
if (oldns_stat.st_ino != newns_stat.st_ino) {
int newns = open(path, O_RDONLY);
if (newns < 0) {
return -1;
}
// Some ancient Linux distributions do not have setns() function
int result = syscall(__NR_setns, newns, 0);
close(newns);
return result < 0 ? -1 : 1;
}
}
#endif // __NR_setns
return 0;
}
void sig(int signum){}
int main(int argc, char *argv[]) {
int target = 0;
char *cmd;
int stop = 0;
int opt;
int option_index = 0;
char *string = "st:mpuni";
int ipcns = 0;
int utsns = 0;
int netns = 0;
int pidns = 0;
int mntns = 0;
while((opt =getopt(argc, argv, string))!= -1) {
switch (opt) {
case 's':
stop = 1;
break;
case 't':
target = atoi(optarg);
break;
case 'm':
mntns = 1;
break;
case 'p':
pidns = 1;
break;
case 'u':
utsns = 1;
break;
case 'n':
netns = 1;
break;
case 'i':
ipcns = 1;
break;
default:
break;
}
}
// check target pid
if (target <= 0) {
fprintf(stderr, "%s is not a valid process ID\n", target);
return 1;
}
// pause
if(stop) {
char *pe = "pause";
prctl(PR_SET_NAME, pe);
signal(SIGCONT,sig);
pause();
char *nc = "nsexec";
prctl(PR_SET_NAME, nc);
}
// enter namespace
if(ipcns) {
enter_ns(target, "ipc");
}
if(utsns) {
enter_ns(target, "uts");
}
if(netns) {
enter_ns(target, "net");
}
if(pidns) {
enter_ns(target, "pid");
}
if(mntns) {
enter_ns(target, "mnt");
}
// fork exec
pid_t pid;
int status;
if((pid = fork())<0) {
status = -1;
} else if(pid == 0){
// args
int i,j=0;
char *args[256] = {NULL};
for(i = optind; i < argc; i++, j++) {
args[j] = argv[i];
}
execvp(argv[optind], args);
_exit(127);
} else {
while(waitpid(pid, &status, 0) < 0){
if(errno != EINTR){
status = -1;
break;
}
}
if(WIFEXITED(status)){
exit(WEXITSTATUS(status));
}
}
return 0;
}