-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.c
205 lines (172 loc) · 4.21 KB
/
helper.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
#include "config.h"
#define _GNU_SOURCE /* Required for CLONE_NEWNS */
#include <sys/mount.h>
#include <sched.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
char *
strconcat (const char *s1,
const char *s2,
const char *s3)
{
size_t len = 0;
char *res;
if (s1)
len += strlen (s1);
if (s2)
len += strlen (s2);
if (s3)
len += strlen (s3);
res = malloc (len + 1);
if (res == NULL)
return NULL;
*res = 0;
if (s1)
strcat (res, s1);
if (s2)
strcat (res, s2);
if (s3)
strcat (res, s3);
return res;
}
static void
update_env_var_list (const char *var, const char *item)
{
const char *env;
char *value;
env = getenv (var);
if (env == NULL || *env == 0)
{
setenv (var, item, 1);
}
else
{
value = strconcat (item, ":", env);
setenv (var, value, 1);
free (value);
}
}
#ifndef MS_PRIVATE /* May not be defined in older glibc headers */
#define MS_PRIVATE (1<<18) /* change to private */
#endif
int
main (int argc,
char **argv)
{
int res;
char *mount_source;
char *executable_relative;
char *executable;
char *extra_mount_source;
char **child_argv;
int i, j, fd, argv_offset;
int mount_count;
/* The initial code is run with a high permission euid
(at least CAP_SYS_ADMIN), so take lots of care. */
if (argc < 3) {
fprintf (stderr, "Not enough arguments. Need source dir and executable relative path.\n");
return 1;
}
argv_offset = 1;
mount_source = argv[argv_offset++];
executable_relative = argv[argv_offset++];
fd = 0;
extra_mount_source = 0;
while (argv_offset + 1 < argc)
{
if (strcmp (argv[argv_offset], "-fd") == 0)
fd = atoi (argv[argv_offset+1]);
else if (strcmp (argv[argv_offset], "-extra") == 0)
extra_mount_source = argv[argv_offset+1];
else
break;
argv_offset += 2;
}
res = unshare (CLONE_NEWNS);
if (res != 0) {
perror ("Creating new namespace failed");
return 1;
}
mount_count = 0;
res = mount (BUNDLE_PREFIX, BUNDLE_PREFIX,
NULL, MS_PRIVATE, NULL);
if (res != 0 && errno == EINVAL) {
/* Maybe if failed because there is no mount
to be made private at that point, letsa
add a bind mount there. */
res = mount (BUNDLE_PREFIX, BUNDLE_PREFIX,
NULL, MS_BIND, NULL);
/* And try again */
if (res == 0)
{
mount_count++; /* Bind mount succeeded */
res = mount (BUNDLE_PREFIX, BUNDLE_PREFIX,
NULL, MS_PRIVATE, NULL);
}
}
if (res != 0) {
perror ("Failed to make prefix namespace private");
goto error_out;
}
if (extra_mount_source != NULL)
{
mount (extra_mount_source, extra_mount_source,
NULL, MS_PRIVATE, NULL);
res = mount (extra_mount_source, extra_mount_source,
NULL, MS_BIND, NULL);
if (res != 0) {
perror ("Failed to bind the extra source directory");
goto error_out;
}
mount_count++; /* Extra mount succeeded */
}
res = mount (mount_source, BUNDLE_PREFIX,
NULL, MS_BIND, NULL);
if (res != 0) {
perror ("Failed to bind the source directory");
goto error_out;
}
mount_count++; /* Normal mount succeeded */
/* Now we have everything we need CAP_SYS_ADMIN for, so drop setuid */
setuid (getuid ());
if (fd != 0)
{
char c = 'x';
write (fd, &c, 1);
}
executable = NULL;
child_argv = NULL;
if (executable_relative[0] == '/')
executable = executable_relative;
else
{
executable = malloc (strlen (BUNDLE_PREFIX) + strlen (executable_relative) + 1);
if (executable != NULL)
{
strcpy (executable, BUNDLE_PREFIX);
strcat (executable, executable_relative);
}
}
if (executable == NULL)
goto oom;
child_argv = malloc ((1 + argc - argv_offset + 1) * sizeof (char *));
if (child_argv == NULL)
goto oom;
j = 0;
for (i = argv_offset; i < argc; i++)
child_argv[j++] = argv[i];
child_argv[j++] = NULL;
update_env_var_list ("LD_LIBRARY_PATH", BUNDLE_PREFIX "/lib");
return execv (executable, child_argv);
oom:
if (executable)
free (executable);
fprintf (stderr, "Out of memory.\n");
error_out:
while (mount_count-- > 0)
umount (BUNDLE_PREFIX);
return 1;
}