blob: db413d82de54514e0cba1f73a80dc4bad061eec9 [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
Lee Campbell11af0622014-05-22 12:36:04 -070079 printf("Usage: %s [-Ghinprsvt] [-b <src>,<dest>[,<writeable>]] "
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070080 "[-c <caps>] [-C <dir>] [-g <group>] [-S <file>] [-u <user>] "
81 "<program> [args...]\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040082 " -b: binds <src> to <dest> in chroot. Multiple "
83 "instances allowed\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040084 " -c <caps>: restrict caps to <caps>\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040085 " -C <dir>: chroot to <dir>\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 " -e: enter new network namespace\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040087 " -G: inherit secondary groups from uid\n"
88 " -g <group>: change gid to <group>\n"
89 " -h: help (this message)\n"
90 " -H: seccomp filter help message\n"
Christopher Wiley88f76a72013-11-01 14:12:56 -070091 " -i: exit immediately after fork (do not act as init)\n"
92 " Not compatible with -p\n"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070093 " -L: log blocked syscalls when using seccomp filter. "
94 "Forces the following syscalls to be allowed:\n"
95 " ", progn);
96 for (i = 0; i < log_syscalls_len; i++)
97 printf("%s ", log_syscalls[i]);
98
99 printf("\n"
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700100 " -n: set no_new_privs\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700101 " -p: enter new pid namespace (implies -vr)\n"
102 " -r: remount /proc read-only (implies -v)\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400103 " -s: use seccomp\n"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700104 " -S <file>: set seccomp filter using <file>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400105 " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n"
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700106 " -t: mount tmpfs at /tmp inside chroot\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400107 " -u <user>: change uid to <user>\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700108 " -v: enter new mount namespace\n"
109 " -V <file>: enter specified mount namespace\n");
Elly Jonescd7a9042011-07-22 13:56:51 -0400110}
111
Elly Jonese1749eb2011-10-07 13:54:59 -0400112static void seccomp_filter_usage(const char *progn)
113{
114 const struct syscall_entry *entry = syscall_table;
115 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
116 "System call names supported:\n", progn);
117 for (; entry->name && entry->nr >= 0; ++entry)
118 printf(" %s [%d]\n", entry->name, entry->nr);
119 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -0500120}
121
Christopher Wiley88f76a72013-11-01 14:12:56 -0700122static int parse_args(struct minijail *j, int argc, char *argv[],
123 int *exit_immediately)
Elly Jonese1749eb2011-10-07 13:54:59 -0400124{
Elly Jonese1749eb2011-10-07 13:54:59 -0400125 int opt;
Lee Campbell11af0622014-05-22 12:36:04 -0700126 int chroot = 0;
127 int mount_tmp = 0;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700128 int use_pid_ns = 0;
129 int use_seccomp_filter = 0;
130 const size_t path_max = 4096;
131 const char *filter_path;
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500132 if (argc > 1 && argv[1][0] != '-')
133 return 1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700134 while ((opt = getopt(argc, argv, "u:g:sS:c:C:b:V:vrGhHinpLet")) != -1) {
Elly Jonese1749eb2011-10-07 13:54:59 -0400135 switch (opt) {
136 case 'u':
137 set_user(j, optarg);
138 break;
139 case 'g':
140 set_group(j, optarg);
141 break;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700142 case 'n':
143 minijail_no_new_privs(j);
Jorge Lucangeli Obes0341d6c2012-07-16 15:27:31 -0700144 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400145 case 's':
146 minijail_use_seccomp(j);
147 break;
148 case 'S':
Elly Jonese1749eb2011-10-07 13:54:59 -0400149 minijail_use_seccomp_filter(j);
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700150 if (strlen(optarg) >= path_max) {
151 fprintf(stderr,
152 "Filter path is too long.\n");
153 exit(1);
154 }
155 filter_path = strndup(optarg, path_max);
156 if (!filter_path) {
157 fprintf(stderr,
158 "Could not strndup(3) filter path.\n");
159 exit(1);
160 }
161 use_seccomp_filter = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400162 break;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700163 case 'L':
164 minijail_log_seccomp_filter_failures(j);
165 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400166 case 'b':
167 add_binding(j, optarg);
168 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400169 case 'c':
170 use_caps(j, optarg);
171 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400172 case 'C':
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700173 if (0 != minijail_enter_chroot(j, optarg)) {
174 fprintf(stderr, "Could not set chroot.\n");
Lee Campbell11af0622014-05-22 12:36:04 -0700175 exit(1);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700176 }
Lee Campbell11af0622014-05-22 12:36:04 -0700177 chroot = 1;
178 break;
179 case 't':
180 minijail_mount_tmp(j);
181 mount_tmp = 1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400182 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400183 case 'v':
184 minijail_namespace_vfs(j);
185 break;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700186 case 'V':
187 minijail_namespace_enter_vfs(j, optarg);
188 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400189 case 'r':
190 minijail_remount_readonly(j);
191 break;
192 case 'G':
193 minijail_inherit_usergroups(j);
194 break;
195 case 'p':
Christopher Wiley88f76a72013-11-01 14:12:56 -0700196 if (*exit_immediately) {
197 fprintf(stderr,
198 "Could not enter pid namespace because "
Lee Campbell11af0622014-05-22 12:36:04 -0700199 "'-i' was specified.\n");
Christopher Wiley88f76a72013-11-01 14:12:56 -0700200 exit(1);
201 }
202 use_pid_ns = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400203 minijail_namespace_pids(j);
204 break;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400205 case 'e':
206 minijail_namespace_net(j);
207 break;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700208 case 'i':
209 if (use_pid_ns) {
210 fprintf(stderr,
211 "Could not disable init loop because "
Lee Campbell11af0622014-05-22 12:36:04 -0700212 "'-p' was specified.\n");
Christopher Wiley88f76a72013-11-01 14:12:56 -0700213 exit(1);
214 }
215 *exit_immediately = 1;
216 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 case 'H':
218 seccomp_filter_usage(argv[0]);
219 exit(1);
220 default:
221 usage(argv[0]);
222 exit(1);
223 }
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500224 if (optind < argc && argv[optind][0] != '-')
Lee Campbell11af0622014-05-22 12:36:04 -0700225 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400227
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700228 /*
229 * We parse seccomp filters here to make sure we've collected all
230 * cmdline options.
231 */
232 if (use_seccomp_filter) {
233 minijail_parse_seccomp_filters(j, filter_path);
234 free((void*)filter_path);
235 }
236
Elly Jonese1749eb2011-10-07 13:54:59 -0400237 if (argc == optind) {
238 usage(argv[0]);
239 exit(1);
240 }
Lee Campbell11af0622014-05-22 12:36:04 -0700241
242 if (mount_tmp && !chroot) {
243 fprintf(stderr,
244 "Could not mount tmpfs at /tmp "
245 "because '-C' was not specified.\n");
246 exit(1);
247 }
248
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500249 return optind;
250}
Elly Jonescd7a9042011-07-22 13:56:51 -0400251
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500252int main(int argc, char *argv[])
253{
254 struct minijail *j = minijail_new();
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800255 char *dl_mesg = NULL;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700256 int exit_immediately = 0;
257 int consumed = parse_args(j, argc, argv, &exit_immediately);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700258 ElfType elftype = ELFERROR;
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500259 argc -= consumed;
260 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700261
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800262 /* Check that we can access the target program. */
Elly Fong-Jones6d717852013-03-19 16:29:03 -0400263 if (access(argv[0], X_OK)) {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700264 fprintf(stderr, "Target program '%s' is not accessible.\n",
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700265 argv[0]);
Elly Fong-Jones6d717852013-03-19 16:29:03 -0400266 return 1;
267 }
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700268
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700269 /* Check if target is statically or dynamically linked. */
270 elftype = get_elf_linkage(argv[0]);
271 if (elftype == ELFSTATIC) {
272 /* Target binary is static. */
273 minijail_run_static(j, argv[0], argv);
274 } else if (elftype == ELFDYNAMIC) {
275 /*
276 * Target binary is dynamically linked so we can
277 * inject libminijailpreload.so into it.
278 */
279
280 /* Check that we can dlopen() libminijailpreload.so. */
281 if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) {
282 dl_mesg = dlerror();
283 fprintf(stderr, "dlopen(): %s\n", dl_mesg);
284 return 1;
285 }
286 minijail_run(j, argv[0], argv);
287 } else {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700288 fprintf(stderr,
289 "Target program '%s' is not a valid ELF file.\n",
290 argv[0]);
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800291 return 1;
292 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700293
Christopher Wiley88f76a72013-11-01 14:12:56 -0700294 if (exit_immediately) {
295 info("not running init loop, exiting immediately");
296 return 0;
297 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400298 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400299}