blob: 9b1fcf330e009528adc2e30061d0a74ec69445dd [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>
Mike Frysinger68f7ccd2021-11-24 22:06:51 -05007#include <err.h>
Stephen Barber5dd5b1b2017-10-16 23:02:39 -07008#include <errno.h>
Elly Jonescd7a9042011-07-22 13:56:51 -04009#include <stdio.h>
10#include <stdlib.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <unistd.h>
12
13#include "libminijail.h"
14
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070015#include "elfparse.h"
Mike Frysinger5ef22ca2018-01-20 13:42:10 -050016#include "minijail0_cli.h"
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070017#include "util.h"
18
Stéphane Lesimplef65da3a2022-01-11 11:44:47 +010019int main(int argc, char *argv[], char *environ[])
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -050020{
21 struct minijail *j = minijail_new();
Jorge Lucangeli Obesd99a40d2016-01-26 13:50:44 -080022 const char *dl_mesg = NULL;
Luis Hector Chavez9acba452018-10-11 10:13:25 -070023 const char *preload_path = PRELOADPATH;
Christopher Wiley88f76a72013-11-01 14:12:56 -070024 int exit_immediately = 0;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070025 ElfType elftype = ELFERROR;
Stéphane Lesimplef65da3a2022-01-11 11:44:47 +010026 char **envp = NULL;
27 int consumed = parse_args(j, argc, argv, environ,
28 &exit_immediately, &elftype,
29 &preload_path, &envp);
Elly Fong-Jonesf65c9fe2013-01-22 13:55:02 -050030 argc -= consumed;
31 argv += consumed;
Jorge Lucangeli Obes482cb9d2014-07-23 15:16:04 -070032
Stephen Barber5dd5b1b2017-10-16 23:02:39 -070033 /*
34 * Make the process group ID of this process equal to its PID.
35 * In the non-interactive case (e.g. when minijail0 is started from
36 * init) this ensures the parent process and the jailed process
37 * can be killed together.
38 *
39 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
40 * the process is already a process group leader.
41 */
42 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050043 if (errno != EPERM)
44 err(1, "setpgid(0, 0) failed");
Stephen Barber5dd5b1b2017-10-16 23:02:39 -070045 }
46
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070047 if (elftype == ELFSTATIC) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070048 /*
49 * Target binary is statically linked so we cannot use
50 * libminijailpreload.so.
51 */
52 minijail_run_no_preload(j, argv[0], argv);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070053 } else if (elftype == ELFDYNAMIC) {
54 /*
55 * Target binary is dynamically linked so we can
56 * inject libminijailpreload.so into it.
57 */
58
59 /* Check that we can dlopen() libminijailpreload.so. */
Luis Hector Chavez9acba452018-10-11 10:13:25 -070060 if (!dlopen(preload_path, RTLD_LAZY | RTLD_LOCAL)) {
Matthew Dempsky2ed09122016-02-11 09:43:37 -080061 dl_mesg = dlerror();
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050062 errx(1, "dlopen(): %s", dl_mesg);
Matthew Dempsky2ed09122016-02-11 09:43:37 -080063 return 1;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070064 }
Luis Hector Chavez1790e282018-10-16 20:43:03 -070065 minijail_set_preload_path(j, preload_path);
Stéphane Lesimplef65da3a2022-01-11 11:44:47 +010066 if (envp) {
67 minijail_run_env(j, argv[0], argv, envp);
68 } else {
69 minijail_run(j, argv[0], argv);
70 }
71 } else {
Mike Frysinger68f7ccd2021-11-24 22:06:51 -050072 errx(1, "Target program '%s' is not a valid ELF file", argv[0]);
Stéphane Lesimplef65da3a2022-01-11 11:44:47 +010073 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -070074
Jorge Lucangeli Obesbeadf612019-03-20 09:32:15 -040075 if (exit_immediately)
Christopher Wiley88f76a72013-11-01 14:12:56 -070076 return 0;
Jorge Lucangeli Obesbeadf612019-03-20 09:32:15 -040077
lhchavez6c8d8202017-09-01 03:55:11 +000078 int ret = minijail_wait(j);
79#if defined(__SANITIZE_ADDRESS__)
80 minijail_destroy(j);
81#endif /* __SANITIZE_ADDRESS__ */
82 return ret;
Elly Jonescd7a9042011-07-22 13:56:51 -040083}