blob: 8329080a55e7f083bdb18e8ac618e3964f3f9165 [file] [log] [blame]
Elly Jonese58176c2012-01-23 11:46:17 -05001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -08006#include <dlfcn.h>
Elly Jonescd7a9042011-07-22 13:56:51 -04007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
11
12#include "libminijail.h"
Will Drewry32ac9f52011-08-18 21:36:27 -050013#include "libsyscalls.h"
Elly Jonescd7a9042011-07-22 13:56:51 -040014
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070015#include "elfparse.h"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070016#include "util.h"
17
Elly Jonese1749eb2011-10-07 13:54:59 -040018static void set_user(struct minijail *j, const char *arg)
19{
20 char *end = NULL;
21 int uid = strtod(arg, &end);
22 if (!*end && *arg) {
23 minijail_change_uid(j, uid);
24 return;
25 }
Elly Jonescd7a9042011-07-22 13:56:51 -040026
Elly Jonese1749eb2011-10-07 13:54:59 -040027 if (minijail_change_user(j, arg)) {
28 fprintf(stderr, "Bad user: '%s'\n", arg);
29 exit(1);
30 }
Elly Jonescd7a9042011-07-22 13:56:51 -040031}
32
Elly Jonese1749eb2011-10-07 13:54:59 -040033static void set_group(struct minijail *j, const char *arg)
34{
35 char *end = NULL;
36 int gid = strtod(arg, &end);
37 if (!*end && *arg) {
38 minijail_change_gid(j, gid);
39 return;
40 }
Elly Jonescd7a9042011-07-22 13:56:51 -040041
Elly Jonese1749eb2011-10-07 13:54:59 -040042 if (minijail_change_group(j, arg)) {
43 fprintf(stderr, "Bad group: '%s'\n", arg);
44 exit(1);
45 }
Elly Jonescd7a9042011-07-22 13:56:51 -040046}
47
Elly Jonese1749eb2011-10-07 13:54:59 -040048static void use_caps(struct minijail *j, const char *arg)
49{
50 uint64_t caps;
51 char *end = NULL;
52 caps = strtoull(arg, &end, 16);
53 if (*end) {
54 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
55 exit(1);
56 }
57 minijail_use_caps(j, caps);
Elly Jonescd7a9042011-07-22 13:56:51 -040058}
59
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070060static void add_binding(struct minijail *j, char *arg)
61{
Elly Jones51a5b6c2011-10-12 19:09:26 -040062 char *src = strtok(arg, ",");
Elly Jones5ba42b52011-12-07 13:31:43 -050063 char *dest = strtok(NULL, ",");
64 char *flags = strtok(NULL, ",");
Elly Jones51a5b6c2011-10-12 19:09:26 -040065 if (!src || !dest) {
66 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
67 exit(1);
68 }
69 if (minijail_bind(j, src, dest, flags ? atoi(flags) : 0)) {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -070070 fprintf(stderr, "Bind failure.\n");
Elly Jones51a5b6c2011-10-12 19:09:26 -040071 exit(1);
72 }
73}
74
Elly Jonese1749eb2011-10-07 13:54:59 -040075static void usage(const char *progn)
76{
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070077 size_t i;
78
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080079 printf("Usage: %s [-GhiInprsvtU] [-b <src>,<dest>[,<writeable>]] [-f <file>]"
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070080 "[-c <caps>] [-C <dir>] [-g <group>] [-S <file>] [-u <user>] "
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +080081 "[-m \"<uid> <loweruid> <count>[,<uid> <loweruid> <count>]\"] "
82 "[-M \"<gid> <lowergid> <count>[,<uid> <loweruid> <count>]\"] "
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070083 "<program> [args...]\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040084 " -b: binds <src> to <dest> in chroot. Multiple "
85 "instances allowed\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040086 " -c <caps>: restrict caps to <caps>\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040087 " -C <dir>: chroot to <dir>\n"
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080088 " Not compatible with -P\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070089 " -e: enter new network namespace\n"
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080090 " -f <file>: write the pid of the jailed process to <file>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040091 " -G: inherit secondary groups from uid\n"
92 " -g <group>: change gid to <group>\n"
93 " -h: help (this message)\n"
94 " -H: seccomp filter help message\n"
Christopher Wiley88f76a72013-11-01 14:12:56 -070095 " -i: exit immediately after fork (do not act as init)\n"
96 " Not compatible with -p\n"
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080097 " -I: run <program> as init (pid 1) inside a new pid namespace (implies -p)\n"
Kees Cook03b2af22014-12-18 17:11:13 -080098 " -L: report blocked syscalls to syslog when using seccomp filter.\n"
99 " Forces the following syscalls to be allowed:\n"
100 " ", progn);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700101 for (i = 0; i < log_syscalls_len; i++)
102 printf("%s ", log_syscalls[i]);
103
104 printf("\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800105 " -m: set the uid mapping of a user namespace (implies -pU).\n"
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800106 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800107 " Not compatible with -b without writable\n"
108 " -M: set the gid mapping of a user namespace (implies -pU).\n"
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800109 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800110 " Not compatible with -b without writable\n"
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700111 " -n: set no_new_privs\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700112 " -p: enter new pid namespace (implies -vr)\n"
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800113 " -P <dir>: pivot_root to <dir> (implies -v)\n"
114 " Not compatible with -C\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700115 " -r: remount /proc read-only (implies -v)\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400116 " -s: use seccomp\n"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700117 " -S <file>: set seccomp filter using <file>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n"
Kees Cook03b2af22014-12-18 17:11:13 -0800119 " Requires -n when not running as root\n"
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700120 " -t: mount tmpfs at /tmp inside chroot\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400121 " -u <user>: change uid to <user>\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800122 " -U enter new user namespace (implies -p)\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700123 " -v: enter new mount namespace\n"
124 " -V <file>: enter specified mount namespace\n");
Elly Jonescd7a9042011-07-22 13:56:51 -0400125}
126
Elly Jonese1749eb2011-10-07 13:54:59 -0400127static void seccomp_filter_usage(const char *progn)
128{
129 const struct syscall_entry *entry = syscall_table;
130 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
131 "System call names supported:\n", progn);
132 for (; entry->name && entry->nr >= 0; ++entry)
133 printf(" %s [%d]\n", entry->name, entry->nr);
134 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -0500135}
136
Christopher Wiley88f76a72013-11-01 14:12:56 -0700137static int parse_args(struct minijail *j, int argc, char *argv[],
138 int *exit_immediately)
Elly Jonese1749eb2011-10-07 13:54:59 -0400139{
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 int opt;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700141 int use_seccomp_filter = 0;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800142 int pivot_root = 0, chroot = 0;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700143 const size_t path_max = 4096;
144 const char *filter_path;
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500145 if (argc > 1 && argv[1][0] != '-')
146 return 1;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700147 while ((opt = getopt(argc, argv,
148 "u:g:sS:c:C:P:b:V:f:m:M:vrGhHinpLetIU")) != -1) {
Elly Jonese1749eb2011-10-07 13:54:59 -0400149 switch (opt) {
150 case 'u':
151 set_user(j, optarg);
152 break;
153 case 'g':
154 set_group(j, optarg);
155 break;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700156 case 'n':
157 minijail_no_new_privs(j);
Jorge Lucangeli Obes0341d6c2012-07-16 15:27:31 -0700158 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400159 case 's':
160 minijail_use_seccomp(j);
161 break;
162 case 'S':
Elly Jonese1749eb2011-10-07 13:54:59 -0400163 minijail_use_seccomp_filter(j);
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700164 if (strlen(optarg) >= path_max) {
165 fprintf(stderr,
166 "Filter path is too long.\n");
167 exit(1);
168 }
169 filter_path = strndup(optarg, path_max);
170 if (!filter_path) {
171 fprintf(stderr,
172 "Could not strndup(3) filter path.\n");
173 exit(1);
174 }
175 use_seccomp_filter = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400176 break;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700177 case 'L':
178 minijail_log_seccomp_filter_failures(j);
179 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400180 case 'b':
181 add_binding(j, optarg);
182 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400183 case 'c':
184 use_caps(j, optarg);
185 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400186 case 'C':
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800187 if (pivot_root) {
188 fprintf(stderr, "Could not set chroot because "
189 "'-P' was specified.\n");
190 exit(1);
191 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700192 if (0 != minijail_enter_chroot(j, optarg)) {
193 fprintf(stderr, "Could not set chroot.\n");
Lee Campbell11af0622014-05-22 12:36:04 -0700194 exit(1);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700195 }
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800196 chroot = 1;
197 break;
198 case 'P':
199 if (chroot) {
200 fprintf(stderr, "Could not set pivot_root because "
201 "'-C' was specified.\n");
202 exit(1);
203 }
204 if (0 != minijail_enter_pivot_root(j, optarg)) {
205 fprintf(stderr, "Could not set pivot_root.\n");
206 exit(1);
207 }
208 minijail_namespace_vfs(j);
209 pivot_root = 1;
Lee Campbell11af0622014-05-22 12:36:04 -0700210 break;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800211 case 'f':
212 if (0 != minijail_write_pid_file(j, optarg)) {
213 fprintf(stderr, "Could not prepare pid file path.\n");
214 exit(1);
215 }
216 break;
Lee Campbell11af0622014-05-22 12:36:04 -0700217 case 't':
218 minijail_mount_tmp(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400219 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400220 case 'v':
221 minijail_namespace_vfs(j);
222 break;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700223 case 'V':
224 minijail_namespace_enter_vfs(j, optarg);
225 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 case 'r':
Dylan Reid791f5772015-09-14 20:02:42 -0700227 minijail_remount_proc_readonly(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400228 break;
229 case 'G':
230 minijail_inherit_usergroups(j);
231 break;
232 case 'p':
233 minijail_namespace_pids(j);
234 break;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400235 case 'e':
236 minijail_namespace_net(j);
237 break;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700238 case 'i':
Christopher Wiley88f76a72013-11-01 14:12:56 -0700239 *exit_immediately = 1;
240 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400241 case 'H':
242 seccomp_filter_usage(argv[0]);
243 exit(1);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800244 case 'I':
245 minijail_namespace_pids(j);
246 minijail_run_as_init(j);
247 break;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800248 case 'U':
249 minijail_namespace_user(j);
250 minijail_namespace_pids(j);
251 break;
252 case 'm':
253 minijail_namespace_user(j);
254 minijail_namespace_pids(j);
255 if (0 != minijail_uidmap(j, optarg)) {
256 fprintf(stderr, "Could not set uidmap\n");
257 exit(1);
258 }
259 break;
260 case 'M':
261 minijail_namespace_user(j);
262 minijail_namespace_pids(j);
263 if (0 != minijail_gidmap(j, optarg)) {
264 fprintf(stderr, "Could not set gidmap\n");
265 exit(1);
266 }
267 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400268 default:
269 usage(argv[0]);
270 exit(1);
271 }
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500272 if (optind < argc && argv[optind][0] != '-')
Lee Campbell11af0622014-05-22 12:36:04 -0700273 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400274 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400275
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700276 /*
277 * We parse seccomp filters here to make sure we've collected all
278 * cmdline options.
279 */
280 if (use_seccomp_filter) {
281 minijail_parse_seccomp_filters(j, filter_path);
282 free((void*)filter_path);
283 }
284
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 if (argc == optind) {
286 usage(argv[0]);
287 exit(1);
288 }
Lee Campbell11af0622014-05-22 12:36:04 -0700289
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500290 return optind;
291}
Elly Jonescd7a9042011-07-22 13:56:51 -0400292
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500293int main(int argc, char *argv[])
294{
295 struct minijail *j = minijail_new();
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800296 char *dl_mesg = NULL;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700297 int exit_immediately = 0;
Dylan Reid08946cc2015-09-16 19:10:57 -0700298 char *program_path;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700299 int consumed = parse_args(j, argc, argv, &exit_immediately);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700300 ElfType elftype = ELFERROR;
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500301 argc -= consumed;
302 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700303
Dylan Reid08946cc2015-09-16 19:10:57 -0700304 /* Get the path to the program adjusted for changing root. */
305 program_path = minijail_get_original_path(j, argv[0]);
306
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800307 /* Check that we can access the target program. */
Dylan Reid08946cc2015-09-16 19:10:57 -0700308 if (!minijail_has_bind_mounts(j) && access(program_path, X_OK)) {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700309 fprintf(stderr, "Target program '%s' is not accessible.\n",
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700310 argv[0]);
Elly Fong-Jones6d717852013-03-19 16:29:03 -0400311 return 1;
312 }
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700313
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700314 /* Check if target is statically or dynamically linked. */
Dylan Reid08946cc2015-09-16 19:10:57 -0700315 if (minijail_has_bind_mounts(j)) {
316 /* We can't tell what the internal path to the binary is so
317 * assume it's dynamically linked.
318 */
319 elftype = ELFDYNAMIC;
320 warn("assuming program '%s' is dynamically linked\n", argv[0]);
321 } else {
322 elftype = get_elf_linkage(program_path);
323 }
324
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700325 if (elftype == ELFSTATIC) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700326 /*
327 * Target binary is statically linked so we cannot use
328 * libminijailpreload.so.
329 */
330 minijail_run_no_preload(j, argv[0], argv);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700331 } else if (elftype == ELFDYNAMIC) {
332 /*
333 * Target binary is dynamically linked so we can
334 * inject libminijailpreload.so into it.
335 */
336
337 /* Check that we can dlopen() libminijailpreload.so. */
338 if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) {
339 dl_mesg = dlerror();
340 fprintf(stderr, "dlopen(): %s\n", dl_mesg);
341 return 1;
342 }
343 minijail_run(j, argv[0], argv);
344 } else {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700345 fprintf(stderr,
346 "Target program '%s' is not a valid ELF file.\n",
347 argv[0]);
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800348 return 1;
349 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700350
Dylan Reid08946cc2015-09-16 19:10:57 -0700351 free(program_path);
352
Christopher Wiley88f76a72013-11-01 14:12:56 -0700353 if (exit_immediately) {
354 info("not running init loop, exiting immediately");
355 return 0;
356 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400357 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400358}