blob: 0be0b6adef2b482740b514e2d937802be1839958 [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 Obes2b12ba42016-01-26 10:37:51 -080070 fprintf(stderr, "minijail_bind failed.\n");
Elly Jones51a5b6c2011-10-12 19:09:26 -040071 exit(1);
72 }
73}
74
Dylan Reid648b2202015-10-23 00:50:00 -070075static void add_mount(struct minijail *j, char *arg)
76{
77 char *src = strtok(arg, ",");
78 char *dest = strtok(NULL, ",");
79 char *type = strtok(NULL, ",");
80 char *flags = strtok(NULL, ",");
81 if (!src || !dest || !type) {
82 fprintf(stderr, "Bad mount: %s %s %s\n", src, dest, type);
83 exit(1);
84 }
85 if (minijail_mount(j, src, dest, type,
86 flags ? strtoul(flags, NULL, 16) : 0)) {
87 fprintf(stderr, "minijail_mount failed.\n");
88 exit(1);
89 }
90}
91
Elly Jonese1749eb2011-10-07 13:54:59 -040092static void usage(const char *progn)
93{
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070094 size_t i;
95
Dylan Reidf7942472015-11-18 17:55:26 -080096 printf("Usage: %s [-GhiInprsvtUl] [-b <src>,<dest>[,<writeable>]] [-f <file>]"
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070097 "[-c <caps>] [-C <dir>] [-g <group>] [-S <file>] [-u <user>] "
Matthew Dempsky2ed09122016-02-11 09:43:37 -080098 "[-k <src>,<dest>,<type>[,<flags>]] [-T <type>] "
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +080099 "[-m \"<uid> <loweruid> <count>[,<uid> <loweruid> <count>]\"] "
100 "[-M \"<gid> <lowergid> <count>[,<uid> <loweruid> <count>]\"] "
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700101 "<program> [args...]\n"
Andrew Brestickereac28942015-11-11 16:04:46 -0800102 " -a <table>: use alternate syscall table <table>\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400103 " -b: binds <src> to <dest> in chroot. Multiple "
104 "instances allowed\n"
Dylan Reid648b2202015-10-23 00:50:00 -0700105 " -k: mount <src> to <dest> in chroot. Multiple "
106 "instances allowed, flags are passed to mount(2)\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400107 " -c <caps>: restrict caps to <caps>\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400108 " -C <dir>: chroot to <dir>\n"
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800109 " Not compatible with -P\n"
Dylan Reid1102f5a2015-09-15 11:52:20 -0700110 " -e[file]: enter new network namespace, or existing one if 'file' is provided\n"
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800111 " -f <file>: write the pid of the jailed process to <file>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400112 " -G: inherit secondary groups from uid\n"
113 " -g <group>: change gid to <group>\n"
114 " -h: help (this message)\n"
115 " -H: seccomp filter help message\n"
Christopher Wiley88f76a72013-11-01 14:12:56 -0700116 " -i: exit immediately after fork (do not act as init)\n"
117 " Not compatible with -p\n"
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800118 " -I: run <program> as init (pid 1) inside a new pid namespace (implies -p)\n"
Dylan Reidf7942472015-11-18 17:55:26 -0800119 " -l: enter new IPC namespace\n"
Kees Cook03b2af22014-12-18 17:11:13 -0800120 " -L: report blocked syscalls to syslog when using seccomp filter.\n"
121 " Forces the following syscalls to be allowed:\n"
122 " ", progn);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700123 for (i = 0; i < log_syscalls_len; i++)
124 printf("%s ", log_syscalls[i]);
125
126 printf("\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800127 " -m: set the uid mapping of a user namespace (implies -pU).\n"
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800128 " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800129 " Not compatible with -b without writable\n"
130 " -M: set the gid mapping of a user namespace (implies -pU).\n"
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800131 " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800132 " Not compatible with -b without writable\n"
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700133 " -n: set no_new_privs\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700134 " -p: enter new pid namespace (implies -vr)\n"
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800135 " -P <dir>: pivot_root to <dir> (implies -v)\n"
136 " Not compatible with -C\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700137 " -r: remount /proc read-only (implies -v)\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400138 " -s: use seccomp\n"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700139 " -S <file>: set seccomp filter using <file>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400140 " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n"
Kees Cook03b2af22014-12-18 17:11:13 -0800141 " Requires -n when not running as root\n"
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700142 " -t: mount tmpfs at /tmp inside chroot\n"
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800143 " -T <type>: assume <program> is a <type> ELF binary.\n"
144 " Must be 'static' or 'dynamic'.\n"
Elly Jonese1749eb2011-10-07 13:54:59 -0400145 " -u <user>: change uid to <user>\n"
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800146 " -U enter new user namespace (implies -p)\n"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700147 " -v: enter new mount namespace\n"
148 " -V <file>: enter specified mount namespace\n");
Elly Jonescd7a9042011-07-22 13:56:51 -0400149}
150
Elly Jonese1749eb2011-10-07 13:54:59 -0400151static void seccomp_filter_usage(const char *progn)
152{
153 const struct syscall_entry *entry = syscall_table;
154 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
155 "System call names supported:\n", progn);
156 for (; entry->name && entry->nr >= 0; ++entry)
157 printf(" %s [%d]\n", entry->name, entry->nr);
158 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -0500159}
160
Christopher Wiley88f76a72013-11-01 14:12:56 -0700161static int parse_args(struct minijail *j, int argc, char *argv[],
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800162 int *exit_immediately, ElfType *elftype)
Elly Jonese1749eb2011-10-07 13:54:59 -0400163{
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 int opt;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700165 int use_seccomp_filter = 0;
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800166 int binding = 0;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800167 int pivot_root = 0, chroot = 0;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700168 const size_t path_max = 4096;
169 const char *filter_path;
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500170 if (argc > 1 && argv[1][0] != '-')
171 return 1;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700172 while ((opt = getopt(argc, argv,
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800173 "u:g:sS:c:C:P:b:V:f:m:M:k:a:e::T:vrGhHinplLtIU"))
Dylan Reid648b2202015-10-23 00:50:00 -0700174 != -1) {
Elly Jonese1749eb2011-10-07 13:54:59 -0400175 switch (opt) {
176 case 'u':
177 set_user(j, optarg);
178 break;
179 case 'g':
180 set_group(j, optarg);
181 break;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700182 case 'n':
183 minijail_no_new_privs(j);
Jorge Lucangeli Obes0341d6c2012-07-16 15:27:31 -0700184 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400185 case 's':
186 minijail_use_seccomp(j);
187 break;
188 case 'S':
Elly Jonese1749eb2011-10-07 13:54:59 -0400189 minijail_use_seccomp_filter(j);
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700190 if (strlen(optarg) >= path_max) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800191 fprintf(stderr, "Filter path is too long.\n");
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700192 exit(1);
193 }
194 filter_path = strndup(optarg, path_max);
195 if (!filter_path) {
196 fprintf(stderr,
197 "Could not strndup(3) filter path.\n");
198 exit(1);
199 }
200 use_seccomp_filter = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400201 break;
Dylan Reidf7942472015-11-18 17:55:26 -0800202 case 'l':
203 minijail_namespace_ipc(j);
204 break;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700205 case 'L':
206 minijail_log_seccomp_filter_failures(j);
207 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400208 case 'b':
209 add_binding(j, optarg);
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800210 binding = 1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400211 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400212 case 'c':
213 use_caps(j, optarg);
214 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400215 case 'C':
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800216 if (pivot_root) {
217 fprintf(stderr, "Could not set chroot because "
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800218 "'-P' was specified.\n");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800219 exit(1);
220 }
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700221 if (0 != minijail_enter_chroot(j, optarg)) {
222 fprintf(stderr, "Could not set chroot.\n");
Lee Campbell11af0622014-05-22 12:36:04 -0700223 exit(1);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700224 }
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800225 chroot = 1;
226 break;
Dylan Reid648b2202015-10-23 00:50:00 -0700227 case 'k':
228 add_mount(j, optarg);
229 break;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800230 case 'P':
231 if (chroot) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800232 fprintf(stderr,
233 "Could not set pivot_root because "
234 "'-C' was specified.\n");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800235 exit(1);
236 }
237 if (0 != minijail_enter_pivot_root(j, optarg)) {
238 fprintf(stderr, "Could not set pivot_root.\n");
239 exit(1);
240 }
241 minijail_namespace_vfs(j);
242 pivot_root = 1;
Lee Campbell11af0622014-05-22 12:36:04 -0700243 break;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800244 case 'f':
245 if (0 != minijail_write_pid_file(j, optarg)) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800246 fprintf(stderr,
247 "Could not prepare pid file path.\n");
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800248 exit(1);
249 }
250 break;
Lee Campbell11af0622014-05-22 12:36:04 -0700251 case 't':
252 minijail_mount_tmp(j);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400253 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400254 case 'v':
255 minijail_namespace_vfs(j);
256 break;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700257 case 'V':
258 minijail_namespace_enter_vfs(j, optarg);
259 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400260 case 'r':
Dylan Reid791f5772015-09-14 20:02:42 -0700261 minijail_remount_proc_readonly(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400262 break;
263 case 'G':
264 minijail_inherit_usergroups(j);
265 break;
266 case 'p':
267 minijail_namespace_pids(j);
268 break;
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400269 case 'e':
Dylan Reid1102f5a2015-09-15 11:52:20 -0700270 if (optarg)
271 minijail_namespace_enter_net(j, optarg);
272 else
273 minijail_namespace_net(j);
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400274 break;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700275 case 'i':
Christopher Wiley88f76a72013-11-01 14:12:56 -0700276 *exit_immediately = 1;
277 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400278 case 'H':
279 seccomp_filter_usage(argv[0]);
280 exit(1);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800281 case 'I':
282 minijail_namespace_pids(j);
283 minijail_run_as_init(j);
284 break;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800285 case 'U':
286 minijail_namespace_user(j);
287 minijail_namespace_pids(j);
288 break;
289 case 'm':
290 minijail_namespace_user(j);
291 minijail_namespace_pids(j);
292 if (0 != minijail_uidmap(j, optarg)) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800293 fprintf(stderr, "Could not set uidmap.\n");
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800294 exit(1);
295 }
296 break;
297 case 'M':
298 minijail_namespace_user(j);
299 minijail_namespace_pids(j);
300 if (0 != minijail_gidmap(j, optarg)) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800301 fprintf(stderr, "Could not set gidmap.\n");
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800302 exit(1);
303 }
304 break;
Andrew Brestickereac28942015-11-11 16:04:46 -0800305 case 'a':
306 if (0 != minijail_use_alt_syscall(j, optarg)) {
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800307 fprintf(stderr,
308 "Could not set alt-syscall table.\n");
Andrew Brestickereac28942015-11-11 16:04:46 -0800309 exit(1);
310 }
311 break;
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800312 case 'T':
313 if (!strcmp(optarg, "static"))
314 *elftype = ELFSTATIC;
315 else if (!strcmp(optarg, "dynamic"))
316 *elftype = ELFDYNAMIC;
317 else {
318 fprintf(stderr, "ELF type must be 'static' or 'dynamic'.\n");
319 exit(1);
320 }
321 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400322 default:
323 usage(argv[0]);
324 exit(1);
325 }
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500326 if (optind < argc && argv[optind][0] != '-')
Lee Campbell11af0622014-05-22 12:36:04 -0700327 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400328 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400329
Jorge Lucangeli Obes2b12ba42016-01-26 10:37:51 -0800330 /* Only allow bind mounts when entering a chroot or using pivot_root. */
331 if (binding && !(chroot || pivot_root)) {
332 fprintf(stderr, "Can't add bind mounts without chroot or"
333 " pivot_root.\n");
334 exit(1);
335 }
336
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700337 /*
338 * We parse seccomp filters here to make sure we've collected all
339 * cmdline options.
340 */
341 if (use_seccomp_filter) {
342 minijail_parse_seccomp_filters(j, filter_path);
343 free((void*)filter_path);
344 }
345
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 if (argc == optind) {
347 usage(argv[0]);
348 exit(1);
349 }
Lee Campbell11af0622014-05-22 12:36:04 -0700350
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500351 return optind;
352}
Elly Jonescd7a9042011-07-22 13:56:51 -0400353
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500354int main(int argc, char *argv[])
355{
356 struct minijail *j = minijail_new();
Jorge Lucangeli Obesd99a40d2016-01-26 13:50:44 -0800357 const char *dl_mesg = NULL;
Christopher Wiley88f76a72013-11-01 14:12:56 -0700358 int exit_immediately = 0;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700359 ElfType elftype = ELFERROR;
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800360 int consumed = parse_args(j, argc, argv, &exit_immediately, &elftype);
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -0500361 argc -= consumed;
362 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700363
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800364 if (elftype == ELFERROR) {
365 /*
366 * -T was not specified.
367 * Get the path to the program adjusted for changing root.
368 */
369 char *program_path = minijail_get_original_path(j, argv[0]);
Dylan Reid08946cc2015-09-16 19:10:57 -0700370
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800371 /* Check that we can access the target program. */
372 if (access(program_path, X_OK)) {
373 fprintf(stderr,
374 "Target program '%s' is not accessible.\n",
375 argv[0]);
376 return 1;
377 }
378
379 /* Check if target is statically or dynamically linked. */
380 elftype = get_elf_linkage(program_path);
381 free(program_path);
Elly Fong-Jones6d717852013-03-19 16:29:03 -0400382 }
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -0700383
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700384 if (elftype == ELFSTATIC) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700385 /*
386 * Target binary is statically linked so we cannot use
387 * libminijailpreload.so.
388 */
389 minijail_run_no_preload(j, argv[0], argv);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700390 } else if (elftype == ELFDYNAMIC) {
391 /*
392 * Target binary is dynamically linked so we can
393 * inject libminijailpreload.so into it.
394 */
395
396 /* Check that we can dlopen() libminijailpreload.so. */
397 if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) {
Matthew Dempsky2ed09122016-02-11 09:43:37 -0800398 dl_mesg = dlerror();
399 fprintf(stderr, "dlopen(): %s\n", dl_mesg);
400 return 1;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700401 }
402 minijail_run(j, argv[0], argv);
403 } else {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700404 fprintf(stderr,
405 "Target program '%s' is not a valid ELF file.\n",
406 argv[0]);
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -0800407 return 1;
408 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700409
Christopher Wiley88f76a72013-11-01 14:12:56 -0700410 if (exit_immediately) {
411 info("not running init loop, exiting immediately");
412 return 0;
413 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400414 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400415}