blob: 35fbe1b597f8a94dd1fe6a2e346a20d2f93c66b6 [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;
Christopher Wiley88f76a72013-11-01 14:12:56 -070022 int exit_immediately = 0;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070023 ElfType elftype = ELFERROR;
Matthew Dempsky2ed09122016-02-11 09:43:37 -080024 int consumed = parse_args(j, argc, argv, &exit_immediately, &elftype);
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -050025 argc -= consumed;
26 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -070027
Stephen Barber5dd5b1b2017-10-16 23:02:39 -070028 /*
29 * Make the process group ID of this process equal to its PID.
30 * In the non-interactive case (e.g. when minijail0 is started from
31 * init) this ensures the parent process and the jailed process
32 * can be killed together.
33 *
34 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
35 * the process is already a process group leader.
36 */
37 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
38 if (errno != EPERM) {
39 fprintf(stderr, "setpgid(0, 0) failed\n");
40 exit(1);
41 }
42 }
43
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070044 if (elftype == ELFSTATIC) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070045 /*
46 * Target binary is statically linked so we cannot use
47 * libminijailpreload.so.
48 */
49 minijail_run_no_preload(j, argv[0], argv);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070050 } else if (elftype == ELFDYNAMIC) {
51 /*
52 * Target binary is dynamically linked so we can
53 * inject libminijailpreload.so into it.
54 */
55
56 /* Check that we can dlopen() libminijailpreload.so. */
57 if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) {
Matthew Dempsky2ed09122016-02-11 09:43:37 -080058 dl_mesg = dlerror();
59 fprintf(stderr, "dlopen(): %s\n", dl_mesg);
60 return 1;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070061 }
62 minijail_run(j, argv[0], argv);
63 } else {
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -070064 fprintf(stderr,
65 "Target program '%s' is not a valid ELF file.\n",
66 argv[0]);
Jorge Lucangeli Obes4b2d5ee2014-01-09 15:47:47 -080067 return 1;
68 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070069
Christopher Wiley88f76a72013-11-01 14:12:56 -070070 if (exit_immediately) {
Luis Hector Chavez114a9302017-09-05 20:36:58 -070071 info("not running init loop, exiting immediately\n");
Christopher Wiley88f76a72013-11-01 14:12:56 -070072 return 0;
73 }
lhchavez6c8d8202017-09-01 03:55:11 +000074 int ret = minijail_wait(j);
75#if defined(__SANITIZE_ADDRESS__)
76 minijail_destroy(j);
77#endif /* __SANITIZE_ADDRESS__ */
78 return ret;
Elly Jonescd7a9042011-07-22 13:56:51 -040079}