Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 6 | #include <dlfcn.h> |
Stephen Barber | 5dd5b1b | 2017-10-16 23:02:39 -0700 | [diff] [blame] | 7 | #include <errno.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "libminijail.h" |
| 13 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 14 | #include "elfparse.h" |
Mike Frysinger | 5ef22ca | 2018-01-20 13:42:10 -0500 | [diff] [blame] | 15 | #include "minijail0_cli.h" |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 16 | #include "util.h" |
| 17 | |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 18 | int main(int argc, char *argv[]) |
| 19 | { |
| 20 | struct minijail *j = minijail_new(); |
Jorge Lucangeli Obes | d99a40d | 2016-01-26 13:50:44 -0800 | [diff] [blame] | 21 | const char *dl_mesg = NULL; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 22 | int exit_immediately = 0; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 23 | ElfType elftype = ELFERROR; |
Matthew Dempsky | 2ed0912 | 2016-02-11 09:43:37 -0800 | [diff] [blame] | 24 | int consumed = parse_args(j, argc, argv, &exit_immediately, &elftype); |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 25 | argc -= consumed; |
| 26 | argv += consumed; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 27 | |
Stephen Barber | 5dd5b1b | 2017-10-16 23:02:39 -0700 | [diff] [blame] | 28 | /* |
| 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 Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 44 | if (elftype == ELFSTATIC) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Target binary is statically linked so we cannot use |
| 47 | * libminijailpreload.so. |
| 48 | */ |
| 49 | minijail_run_no_preload(j, argv[0], argv); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 50 | } 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 Dempsky | 2ed0912 | 2016-02-11 09:43:37 -0800 | [diff] [blame] | 58 | dl_mesg = dlerror(); |
| 59 | fprintf(stderr, "dlopen(): %s\n", dl_mesg); |
| 60 | return 1; |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 61 | } |
| 62 | minijail_run(j, argv[0], argv); |
| 63 | } else { |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 64 | fprintf(stderr, |
| 65 | "Target program '%s' is not a valid ELF file.\n", |
| 66 | argv[0]); |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 67 | return 1; |
| 68 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 69 | |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 70 | if (exit_immediately) { |
Luis Hector Chavez | 114a930 | 2017-09-05 20:36:58 -0700 | [diff] [blame] | 71 | info("not running init loop, exiting immediately\n"); |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
lhchavez | 6c8d820 | 2017-09-01 03:55:11 +0000 | [diff] [blame] | 74 | int ret = minijail_wait(j); |
| 75 | #if defined(__SANITIZE_ADDRESS__) |
| 76 | minijail_destroy(j); |
| 77 | #endif /* __SANITIZE_ADDRESS__ */ |
| 78 | return ret; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 79 | } |