-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.c
500 lines (426 loc) · 11.6 KB
/
util.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/* bubblewrap-oci
* Copyright (C) 2016, 2017 Giuseppe Scrivano
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include "util.h"
#include <unistd.h>
#include <stdlib.h>
#ifdef HAVE_ERROR_H
#include <error.h>
#endif
#include <stdio.h>
#include <glib.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <seccomp.h>
#include <errno.h>
#include <glib/gprintf.h>
#include <string.h>
#include <sys/socket.h>
#include <gio/gunixinputstream.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "safe-read-write.h"
#include "subugidmap.h"
/***
This part is taken from systemd:
Copyright 2010 Lennart Poettering
*/
#if defined __i386__ || defined __x86_64__
/* The precise definition of __O_TMPFILE is arch specific, so let's
* just define this on x86 where we know the value. */
#ifndef __O_TMPFILE
#define __O_TMPFILE 020000000
#endif
/* a horrid kludge trying to make sure that this will fail on old kernels */
#ifndef O_TMPFILE
#define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
#endif
#endif
static GHashTable *bwrap_options = NULL;
static const gchar *bwrap_path = BWRAP;
void
cleanup_freep (void *p)
{
void **pp = (void **) p;
free (*pp);
}
void
cleanup_filep (FILE **f)
{
FILE *file = *f;
if (file)
(void) fclose (file);
}
gchar *
get_run_directory ()
{
gchar run_directory_buffer[64], *ret;
const char *root = getenv ("XDG_RUNTIME_DIR");
struct stat st;
char *tmp = NULL;
int r;
if (root == NULL)
{
g_sprintf (run_directory_buffer, "/run/user/%d", getuid ());
root = run_directory_buffer;
}
ret = g_strdup_printf ("%s/%s", root, "bwrap-oci");
r = lstat (ret, &st);
if (r != 0)
{
char *it;
if (errno != ENOENT)
error (EXIT_FAILURE, errno, "error lstat %s", ret);
tmp = strdup (ret);
for (it = strchr (tmp + 1, '/'); it; it = strchr (it + 1, '/'))
{
*it = '\0';
mkdir (tmp, 0700);
*it = '/';
}
mkdir (ret, 0700);
}
free (tmp);
return ret;
}
uint32_t
get_seccomp_operator (const char *name)
{
if (g_strcmp0 (name, "SCMP_CMP_NE") == 0)
return SCMP_CMP_NE;
if (g_strcmp0 (name, "SCMP_CMP_LT") == 0)
return SCMP_CMP_LT;
if (g_strcmp0 (name, "SCMP_CMP_LE") == 0)
return SCMP_CMP_LE;
if (g_strcmp0 (name, "SCMP_CMP_EQ") == 0)
return SCMP_CMP_EQ;
if (g_strcmp0 (name, "SCMP_CMP_GE") == 0)
return SCMP_CMP_GE;
if (g_strcmp0 (name, "SCMP_CMP_GT") == 0)
return SCMP_CMP_GT;
if (g_strcmp0 (name, "SCMP_CMP_MASKED_EQ") == 0)
return SCMP_CMP_MASKED_EQ;
else
error (EXIT_FAILURE, 0, "unsupported seccomp operator %s\n", name);
return -1;
}
guint64
get_seccomp_action (const char *name)
{
if (g_strcmp0 (name, "SCMP_ACT_KILL") == 0)
return SCMP_ACT_KILL;
if (g_strcmp0 (name, "SCMP_ACT_ALLOW") == 0)
return SCMP_ACT_ALLOW;
if (g_strcmp0 (name, "SCMP_ACT_TRAP") == 0)
return SCMP_ACT_TRAP;
if (g_strcmp0 (name, "SCMP_ACT_ERRNO") == 0)
return SCMP_ACT_ERRNO(EPERM);
if (g_strcmp0 (name, "SCMP_ACT_TRACE") == 0)
return SCMP_ACT_TRACE(EPERM);
else
error (EXIT_FAILURE, 0, "unsupported seccomp action %s\n", name);
return -1;
}
void
set_bwrap_path (const char *path)
{
bwrap_path = path;
}
const char *
get_bwrap_path ()
{
return bwrap_path;
}
static void
read_bwrap_help ()
{
const gchar *argv[] = {bwrap_path, "--help", NULL};
gchar *output = NULL;
gint exit_status;
gchar *end, *it;
if (g_spawn_sync (NULL, (gchar **) argv, NULL, G_SPAWN_DEFAULT, NULL,
NULL, &output, NULL, &exit_status, NULL) == FALSE)
{
error (EXIT_FAILURE, errno, "error running bwrap --help");
}
bwrap_options = g_hash_table_new (g_str_hash, g_str_equal);
for (it = strstr (output, " --"); it; it = strstr (end + 1, " --"))
{
gchar *value;
end = strchr (it + 6, ' ');
if (end == NULL)
break;
*end = '\0';
value = g_strdup (it + 6);
g_hash_table_insert (bwrap_options, value, value);
}
g_free (output);
}
gboolean
bwrap_has_option (const gchar *option)
{
if (bwrap_options == NULL)
read_bwrap_help ();
return g_hash_table_contains (bwrap_options, option);
}
void
write_container_state (const char *container_state, pid_t child_pid, const char *bundle_path)
{
gchar *path = g_strdup_printf ("%s/status.json", container_state);
FILE *f = fopen (path, "w");
if (f != NULL)
{
const char *fmt_stdin = "{\"pid\":%i, \"bundlePath\":\"%s\"}";
char *stdin = g_strdup_printf (fmt_stdin, child_pid, bundle_path);
fprintf (f, "%s", stdin);
g_free (stdin);
fclose (f);
}
g_free (path);
}
void
detach_process ()
{
setsid ();
if (fork () != 0)
_exit (EXIT_SUCCESS);
}
static void
write_mapping (const char *program, pid_t pid, uint32_t host_id, uint32_t sandbox_id,
uint32_t first_subid, uint32_t n_subids)
{
char arg_buffer[32][16];
const gchar *argv[32] = { 0 };
int argc = 0;
gint exit_status;
#define APPEND_ARGUMENT(x) \
do \
{ \
g_sprintf (arg_buffer[argc], "%i", x); \
argv[argc] = arg_buffer[argc]; \
argc++; \
} while (0)
argv[argc++] = program;
APPEND_ARGUMENT (pid);
if (sandbox_id == 0)
{
APPEND_ARGUMENT (sandbox_id);
APPEND_ARGUMENT (host_id);
APPEND_ARGUMENT (1);
APPEND_ARGUMENT (1);
APPEND_ARGUMENT (first_subid);
APPEND_ARGUMENT (n_subids);
}
else if (sandbox_id < n_subids)
{
APPEND_ARGUMENT (0);
APPEND_ARGUMENT (first_subid);
APPEND_ARGUMENT (sandbox_id);
APPEND_ARGUMENT (sandbox_id);
APPEND_ARGUMENT (host_id);
APPEND_ARGUMENT (1);
APPEND_ARGUMENT (sandbox_id + 1);
APPEND_ARGUMENT (first_subid + sandbox_id);
APPEND_ARGUMENT (n_subids - sandbox_id);
}
else
{
APPEND_ARGUMENT (0);
APPEND_ARGUMENT (first_subid);
APPEND_ARGUMENT (n_subids);
APPEND_ARGUMENT (sandbox_id);
APPEND_ARGUMENT (host_id);
APPEND_ARGUMENT (1);
}
argv[argc] = NULL;
if (g_spawn_sync (NULL, (gchar **) argv, NULL, G_SPAWN_DEFAULT, NULL,
NULL, NULL, NULL, &exit_status, NULL) == FALSE ||
exit_status != 0)
{
error (EXIT_FAILURE, errno, "error running %s", program);
}
}
void
write_user_group_mappings (struct user_mapping *user_mapping, uid_t uid, gid_t gid, pid_t pid)
{
uid_t current_uid = getuid ();
gid_t current_gid = getgid ();
write_mapping ("/usr/bin/newuidmap", pid, current_uid, uid,
user_mapping->first_subuid, user_mapping->n_subuid);
write_mapping ("/usr/bin/newgidmap", pid, current_gid, gid,
user_mapping->first_subgid, user_mapping->n_subgid);
}
static int test_environment = -1;
static gboolean
test_environment_p ()
{
if (test_environment < 0)
test_environment = getenv ("TEST") ? 1 : 0;
return test_environment == 1 ? TRUE : FALSE;
}
void
set_test_environment (gboolean status)
{
test_environment = status;
}
gchar *
format_fd (gchar *buf, int fd)
{
if (test_environment_p ())
g_sprintf (buf, "FD");
else
g_sprintf (buf, "%i", fd);
return buf;
}
gboolean
file_exist_p (const char *root, const char *file)
{
int res;
struct stat st;
cleanup_free gchar *fpath = g_strdup_printf ("%s%s", root, file);
res = lstat (fpath, &st);
return res == 0;
}
gboolean
can_mask_or_ro_p (const char *path)
{
int res;
struct stat st;
if (test_environment_p ())
return TRUE;
if (!g_str_has_prefix (path, "/sys") && !g_str_has_prefix (path, "/proc"))
return TRUE;
res = lstat (path, &st);
return res == 0 && !S_ISDIR (st.st_mode);
}
gchar *
get_bundle_path (const char *rootfs)
{
gchar *ret;
cleanup_free gchar *tmp = g_strdup (rootfs);
ret = realpath(dirname (tmp), NULL);
return ret;
}
char *
create_container (const char *name)
{
struct stat st;
int r;
gchar *dir;
cleanup_free gchar *run_directory = get_run_directory ();
dir = g_strdup_printf ("%s/%s", run_directory, name);
r = lstat (dir, &st);
if (r == 0)
error (EXIT_FAILURE, 0, "container %s already exists", name);
if (r != 0 && errno != ENOENT)
error (EXIT_FAILURE, errno, "error lstat %s", dir);
if (mkdir (dir, 0700) < 0)
error (EXIT_FAILURE, errno, "error mkdir %s", dir);
return dir;
}
void
delete_container (const char *name)
{
cleanup_free gchar *dir = NULL;
cleanup_free gchar *status = NULL;
cleanup_free gchar *run_directory = get_run_directory ();
struct stat st;
pid_t pid;
dir = g_strdup_printf ("%s/%s", run_directory, name);
status = g_strdup_printf ("%s/%s/status.json", run_directory, name);
if (lstat (dir, &st) < 0 && errno == ENOENT)
error (EXIT_FAILURE, 0, "container %s does not exist", name);
read_container_status_file (status, &pid, NULL);
if (pid_running_p (pid))
{
error (EXIT_FAILURE, 0, "can't delete a running container");
}
if (unlink (status) < 0)
error (EXIT_FAILURE, errno, "unlink status file for container %s", name);
if (rmdir (dir) < 0)
error (EXIT_FAILURE, errno, "rmdir for container %s", name);
}
int
generate_seccomp_rules_file (scmp_filter_ctx seccomp)
{
int fd = -1;
if (seccomp)
{
fd = open ("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0)
{
if (errno != EOPNOTSUPP)
error (EXIT_FAILURE, errno, "error opening temp file");
else
{
char *template = strdup ("/tmp/bwrap-oci-XXXXXX");
fd = mkstemp (template);
if (fd < 0)
error (EXIT_FAILURE, errno, "error opening temp file");
unlink (template);
free (template);
}
}
if (seccomp_export_bpf (seccomp, fd) < 0)
error (EXIT_FAILURE, errno, "error writing seccomp rules file");
if (lseek (fd, 0, SEEK_SET) < 0)
error (EXIT_FAILURE, errno, "error seeking seccomp rules file");
}
return fd;
}
void
read_container_status_file (const char *path, pid_t *pid, char **bundlePath)
{
GError *gerror = NULL;
JsonParser *parser;
JsonObject *root;
JsonNode *tmp;
parser = json_parser_new ();
json_parser_load_from_file (parser, path, &gerror);
if (gerror)
error (EXIT_FAILURE, 0, "unable to parse `%s': %s\n", path, gerror->message);
root = json_node_get_object (json_parser_get_root (parser));
if (pid) {
tmp = json_object_get_member (root, "pid");
if (tmp)
*pid = json_node_get_int (tmp);
else
*pid = 0;
}
if (bundlePath) {
tmp = json_object_get_member (root, "bundlePath");
if (tmp)
*bundlePath = g_strdup (json_node_get_string (tmp));
else
*bundlePath = NULL;
}
if (gerror)
g_error_free (gerror);
g_object_unref (parser);
}
gboolean
pid_running_p (pid_t pid)
{
if (pid == 0)
return FALSE;
return kill (pid, 0) == 0;
}