blob: f216d6e658f9922bca707fb4b6b71d558998b3a8 [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>
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07007#include <errno.h>
Elly Jonescd7a9042011-07-22 13:56:51 -04008#include <stdio.h>
9#include <stdlib.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040010#include <unistd.h>
11
12#include "libminijail.h"
13
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070014#include "elfparse.h"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050015#include "minijail0_cli.h"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070016#include "util.h"
17
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -050018int main(int argc, char *argv[])
19{
20 struct minijail *j = minijail_new();
Jorge Lucangeli Obesd99a40d2016-01-26 13:50:44 -080021 const char *dl_mesg = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -070022 const char *preload_path = PRELOADPATH;
Christopher Wiley88f76a72013-11-01 14:12:56 -070023 int exit_immediately = 0;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070024 ElfType elftype = ELFERROR;
Luis Hector Chavez9acba452018-10-11 10:13:25 -070025 int consumed = parse_args(j, argc, argv, &exit_immediately, &elftype,
26 &preload_path);
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -050027 argc -= consumed;
28 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -070029
Stephen Barber5dd5b1b2017-10-16 23:02:39 -070030 /*
31 * Make the process group ID of this process equal to its PID.
32 * In the non-interactive case (e.g. when minijail0 is started from
33 * init) this ensures the parent process and the jailed process
34 * can be killed together.
35 *
36 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
37 * the process is already a process group leader.
38 */
39 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
40 if (errno != EPERM) {
41 fprintf(stderr, "setpgid(0, 0) failed\n");
42 exit(1);
43 }
44 }
45
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070046 if (elftype == ELFSTATIC) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070047 /*
48 * Target binary is statically linked so we cannot use
49 * libminijailpreload.so.
50 */
51 minijail_run_no_preload(j, argv[0], argv);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070052 } else if (elftype == ELFDYNAMIC) {
53 /*
54 * Target binary is dynamically linked so we can
55 * inject libminijailpreload.so into it.
56 */
57
58 /* Check that we can dlopen() libminijailpreload.so. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -070059 if (!dlopen(preload_path, RTLD_LAZY | RTLD_LOCAL)) {
Matthew Dempsky2ed09122016-02-11 09:43:37 -080060 dl_mesg = dlerror();
61 fprintf(stderr, "dlopen(): %s\n", dl_mesg);
62 return 1;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070063 }
Luis Hector Chavez1790e282018-10-16 20:43:03 -070064 minijail_set_preload_path(j, preload_path);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070065 minijail_run(j, argv[0], argv);
66 } else {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -070067 fprintf(stderr,
68 "Target program '%s' is not a valid ELF file.\n",
69 argv[0]);
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -080070 return 1;
71 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070072
Jorge Lucangeli Obesbeadf612019-03-20 09:32:15 -040073 if (exit_immediately)
Christopher Wiley88f76a72013-11-01 14:12:56 -070074 return 0;
Jorge Lucangeli Obesbeadf612019-03-20 09:32:15 -040075
lhchavez6c8d8202017-09-01 03:55:11 +000076 int ret = minijail_wait(j);
77#if defined(__SANITIZE_ADDRESS__)
78 minijail_destroy(j);
79#endif /* __SANITIZE_ADDRESS__ */
80 return ret;
Elly Jonescd7a9042011-07-22 13:56:51 -040081}