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> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "libminijail.h" |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 13 | #include "libsyscalls.h" |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 14 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 15 | #include "elfparse.h" |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 16 | #include "util.h" |
| 17 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 18 | static 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 Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 26 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 27 | if (minijail_change_user(j, arg)) { |
| 28 | fprintf(stderr, "Bad user: '%s'\n", arg); |
| 29 | exit(1); |
| 30 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 31 | } |
| 32 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 33 | static 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 Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 41 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 42 | if (minijail_change_group(j, arg)) { |
| 43 | fprintf(stderr, "Bad group: '%s'\n", arg); |
| 44 | exit(1); |
| 45 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 48 | static 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 Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 60 | static void add_binding(struct minijail *j, char *arg) |
| 61 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 62 | char *src = strtok(arg, ","); |
Elly Jones | 5ba42b5 | 2011-12-07 13:31:43 -0500 | [diff] [blame] | 63 | char *dest = strtok(NULL, ","); |
| 64 | char *flags = strtok(NULL, ","); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 65 | 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 Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 70 | fprintf(stderr, "Bind failure.\n"); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 71 | exit(1); |
| 72 | } |
| 73 | } |
| 74 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 75 | static void usage(const char *progn) |
| 76 | { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 77 | size_t i; |
| 78 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 79 | printf("Usage: %s [-GhiInprsvtU] [-b <src>,<dest>[,<writeable>]] [-f <file>]" |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 80 | "[-c <caps>] [-C <dir>] [-g <group>] [-S <file>] [-u <user>] " |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 81 | "[-m \"<uid> <loweruid> <count>[,<uid> <loweruid> <count>]\"] " |
| 82 | "[-M \"<gid> <lowergid> <count>[,<uid> <loweruid> <count>]\"] " |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 83 | "<program> [args...]\n" |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 84 | " -b: binds <src> to <dest> in chroot. Multiple " |
| 85 | "instances allowed\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 86 | " -c <caps>: restrict caps to <caps>\n" |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 87 | " -C <dir>: chroot to <dir>\n" |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 88 | " Not compatible with -P\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 89 | " -e: enter new network namespace\n" |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 90 | " -f <file>: write the pid of the jailed process to <file>\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 91 | " -G: inherit secondary groups from uid\n" |
| 92 | " -g <group>: change gid to <group>\n" |
| 93 | " -h: help (this message)\n" |
| 94 | " -H: seccomp filter help message\n" |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 95 | " -i: exit immediately after fork (do not act as init)\n" |
| 96 | " Not compatible with -p\n" |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 97 | " -I: run <program> as init (pid 1) inside a new pid namespace (implies -p)\n" |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame] | 98 | " -L: report blocked syscalls to syslog when using seccomp filter.\n" |
| 99 | " Forces the following syscalls to be allowed:\n" |
| 100 | " ", progn); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 101 | for (i = 0; i < log_syscalls_len; i++) |
| 102 | printf("%s ", log_syscalls[i]); |
| 103 | |
| 104 | printf("\n" |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 105 | " -m: set the uid mapping of a user namespace (implies -pU).\n" |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 106 | " Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n" |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 107 | " Not compatible with -b without writable\n" |
| 108 | " -M: set the gid mapping of a user namespace (implies -pU).\n" |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 109 | " Same arguments as newgidmap(1), multiple mappings should be separated by ',' (comma).\n" |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 110 | " Not compatible with -b without writable\n" |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 111 | " -n: set no_new_privs\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 112 | " -p: enter new pid namespace (implies -vr)\n" |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 113 | " -P <dir>: pivot_root to <dir> (implies -v)\n" |
| 114 | " Not compatible with -C\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 115 | " -r: remount /proc read-only (implies -v)\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 116 | " -s: use seccomp\n" |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 117 | " -S <file>: set seccomp filter using <file>\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 118 | " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n" |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame] | 119 | " Requires -n when not running as root\n" |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 120 | " -t: mount tmpfs at /tmp inside chroot\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 121 | " -u <user>: change uid to <user>\n" |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 122 | " -U enter new user namespace (implies -p)\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 123 | " -v: enter new mount namespace\n" |
| 124 | " -V <file>: enter specified mount namespace\n"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 127 | static void seccomp_filter_usage(const char *progn) |
| 128 | { |
| 129 | const struct syscall_entry *entry = syscall_table; |
| 130 | printf("Usage: %s -S <policy.file> <program> [args...]\n\n" |
| 131 | "System call names supported:\n", progn); |
| 132 | for (; entry->name && entry->nr >= 0; ++entry) |
| 133 | printf(" %s [%d]\n", entry->name, entry->nr); |
| 134 | printf("\nSee minijail0(5) for example policies.\n"); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 137 | static int parse_args(struct minijail *j, int argc, char *argv[], |
| 138 | int *exit_immediately) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 139 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 140 | int opt; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 141 | int use_seccomp_filter = 0; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 142 | int pivot_root = 0, chroot = 0; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 143 | const size_t path_max = 4096; |
| 144 | const char *filter_path; |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 145 | if (argc > 1 && argv[1][0] != '-') |
| 146 | return 1; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 147 | while ((opt = getopt(argc, argv, |
| 148 | "u:g:sS:c:C:P:b:V:f:m:M:vrGhHinpLetIU")) != -1) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 149 | switch (opt) { |
| 150 | case 'u': |
| 151 | set_user(j, optarg); |
| 152 | break; |
| 153 | case 'g': |
| 154 | set_group(j, optarg); |
| 155 | break; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 156 | case 'n': |
| 157 | minijail_no_new_privs(j); |
Jorge Lucangeli Obes | 0341d6c | 2012-07-16 15:27:31 -0700 | [diff] [blame] | 158 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 159 | case 's': |
| 160 | minijail_use_seccomp(j); |
| 161 | break; |
| 162 | case 'S': |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 163 | minijail_use_seccomp_filter(j); |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 164 | if (strlen(optarg) >= path_max) { |
| 165 | fprintf(stderr, |
| 166 | "Filter path is too long.\n"); |
| 167 | exit(1); |
| 168 | } |
| 169 | filter_path = strndup(optarg, path_max); |
| 170 | if (!filter_path) { |
| 171 | fprintf(stderr, |
| 172 | "Could not strndup(3) filter path.\n"); |
| 173 | exit(1); |
| 174 | } |
| 175 | use_seccomp_filter = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 176 | break; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 177 | case 'L': |
| 178 | minijail_log_seccomp_filter_failures(j); |
| 179 | break; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 180 | case 'b': |
| 181 | add_binding(j, optarg); |
| 182 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 183 | case 'c': |
| 184 | use_caps(j, optarg); |
| 185 | break; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 186 | case 'C': |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 187 | if (pivot_root) { |
| 188 | fprintf(stderr, "Could not set chroot because " |
| 189 | "'-P' was specified.\n"); |
| 190 | exit(1); |
| 191 | } |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 192 | if (0 != minijail_enter_chroot(j, optarg)) { |
| 193 | fprintf(stderr, "Could not set chroot.\n"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 194 | exit(1); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 195 | } |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 196 | chroot = 1; |
| 197 | break; |
| 198 | case 'P': |
| 199 | if (chroot) { |
| 200 | fprintf(stderr, "Could not set pivot_root because " |
| 201 | "'-C' was specified.\n"); |
| 202 | exit(1); |
| 203 | } |
| 204 | if (0 != minijail_enter_pivot_root(j, optarg)) { |
| 205 | fprintf(stderr, "Could not set pivot_root.\n"); |
| 206 | exit(1); |
| 207 | } |
| 208 | minijail_namespace_vfs(j); |
| 209 | pivot_root = 1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 210 | break; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 211 | case 'f': |
| 212 | if (0 != minijail_write_pid_file(j, optarg)) { |
| 213 | fprintf(stderr, "Could not prepare pid file path.\n"); |
| 214 | exit(1); |
| 215 | } |
| 216 | break; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 217 | case 't': |
| 218 | minijail_mount_tmp(j); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 219 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 220 | case 'v': |
| 221 | minijail_namespace_vfs(j); |
| 222 | break; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 223 | case 'V': |
| 224 | minijail_namespace_enter_vfs(j, optarg); |
| 225 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 226 | case 'r': |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 227 | minijail_remount_proc_readonly(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 228 | break; |
| 229 | case 'G': |
| 230 | minijail_inherit_usergroups(j); |
| 231 | break; |
| 232 | case 'p': |
| 233 | minijail_namespace_pids(j); |
| 234 | break; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 235 | case 'e': |
| 236 | minijail_namespace_net(j); |
| 237 | break; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 238 | case 'i': |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 239 | *exit_immediately = 1; |
| 240 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 241 | case 'H': |
| 242 | seccomp_filter_usage(argv[0]); |
| 243 | exit(1); |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 244 | case 'I': |
| 245 | minijail_namespace_pids(j); |
| 246 | minijail_run_as_init(j); |
| 247 | break; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 248 | case 'U': |
| 249 | minijail_namespace_user(j); |
| 250 | minijail_namespace_pids(j); |
| 251 | break; |
| 252 | case 'm': |
| 253 | minijail_namespace_user(j); |
| 254 | minijail_namespace_pids(j); |
| 255 | if (0 != minijail_uidmap(j, optarg)) { |
| 256 | fprintf(stderr, "Could not set uidmap\n"); |
| 257 | exit(1); |
| 258 | } |
| 259 | break; |
| 260 | case 'M': |
| 261 | minijail_namespace_user(j); |
| 262 | minijail_namespace_pids(j); |
| 263 | if (0 != minijail_gidmap(j, optarg)) { |
| 264 | fprintf(stderr, "Could not set gidmap\n"); |
| 265 | exit(1); |
| 266 | } |
| 267 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 268 | default: |
| 269 | usage(argv[0]); |
| 270 | exit(1); |
| 271 | } |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 272 | if (optind < argc && argv[optind][0] != '-') |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 273 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 274 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 275 | |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 276 | /* |
| 277 | * We parse seccomp filters here to make sure we've collected all |
| 278 | * cmdline options. |
| 279 | */ |
| 280 | if (use_seccomp_filter) { |
| 281 | minijail_parse_seccomp_filters(j, filter_path); |
| 282 | free((void*)filter_path); |
| 283 | } |
| 284 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 285 | if (argc == optind) { |
| 286 | usage(argv[0]); |
| 287 | exit(1); |
| 288 | } |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 289 | |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 290 | return optind; |
| 291 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 292 | |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 293 | int main(int argc, char *argv[]) |
| 294 | { |
| 295 | struct minijail *j = minijail_new(); |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 296 | char *dl_mesg = NULL; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 297 | int exit_immediately = 0; |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 298 | char *program_path; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 299 | int consumed = parse_args(j, argc, argv, &exit_immediately); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 300 | ElfType elftype = ELFERROR; |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 301 | argc -= consumed; |
| 302 | argv += consumed; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 303 | |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 304 | /* Get the path to the program adjusted for changing root. */ |
| 305 | program_path = minijail_get_original_path(j, argv[0]); |
| 306 | |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 307 | /* Check that we can access the target program. */ |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 308 | if (!minijail_has_bind_mounts(j) && access(program_path, X_OK)) { |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 309 | fprintf(stderr, "Target program '%s' is not accessible.\n", |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 310 | argv[0]); |
Elly Fong-Jones | 6d71785 | 2013-03-19 16:29:03 -0400 | [diff] [blame] | 311 | return 1; |
| 312 | } |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 313 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 314 | /* Check if target is statically or dynamically linked. */ |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 315 | if (minijail_has_bind_mounts(j)) { |
| 316 | /* We can't tell what the internal path to the binary is so |
| 317 | * assume it's dynamically linked. |
| 318 | */ |
| 319 | elftype = ELFDYNAMIC; |
| 320 | warn("assuming program '%s' is dynamically linked\n", argv[0]); |
| 321 | } else { |
| 322 | elftype = get_elf_linkage(program_path); |
| 323 | } |
| 324 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 325 | if (elftype == ELFSTATIC) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 326 | /* |
| 327 | * Target binary is statically linked so we cannot use |
| 328 | * libminijailpreload.so. |
| 329 | */ |
| 330 | minijail_run_no_preload(j, argv[0], argv); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 331 | } else if (elftype == ELFDYNAMIC) { |
| 332 | /* |
| 333 | * Target binary is dynamically linked so we can |
| 334 | * inject libminijailpreload.so into it. |
| 335 | */ |
| 336 | |
| 337 | /* Check that we can dlopen() libminijailpreload.so. */ |
| 338 | if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) { |
| 339 | dl_mesg = dlerror(); |
| 340 | fprintf(stderr, "dlopen(): %s\n", dl_mesg); |
| 341 | return 1; |
| 342 | } |
| 343 | minijail_run(j, argv[0], argv); |
| 344 | } else { |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 345 | fprintf(stderr, |
| 346 | "Target program '%s' is not a valid ELF file.\n", |
| 347 | argv[0]); |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 348 | return 1; |
| 349 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 350 | |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 351 | free(program_path); |
| 352 | |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 353 | if (exit_immediately) { |
| 354 | info("not running init loop, exiting immediately"); |
| 355 | return 0; |
| 356 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 357 | return minijail_wait(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 358 | } |