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 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 79 | printf("Usage: %s [-Ghinprsvt] [-b <src>,<dest>[,<writeable>]] " |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 80 | "[-c <caps>] [-C <dir>] [-g <group>] [-S <file>] [-u <user>] " |
| 81 | "<program> [args...]\n" |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 82 | " -b: binds <src> to <dest> in chroot. Multiple " |
| 83 | "instances allowed\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 84 | " -c <caps>: restrict caps to <caps>\n" |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 85 | " -C <dir>: chroot to <dir>\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 86 | " -e: enter new network namespace\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 87 | " -G: inherit secondary groups from uid\n" |
| 88 | " -g <group>: change gid to <group>\n" |
| 89 | " -h: help (this message)\n" |
| 90 | " -H: seccomp filter help message\n" |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 91 | " -i: exit immediately after fork (do not act as init)\n" |
| 92 | " Not compatible with -p\n" |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame^] | 93 | " -L: report blocked syscalls to syslog when using seccomp filter.\n" |
| 94 | " Forces the following syscalls to be allowed:\n" |
| 95 | " ", progn); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 96 | for (i = 0; i < log_syscalls_len; i++) |
| 97 | printf("%s ", log_syscalls[i]); |
| 98 | |
| 99 | printf("\n" |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 100 | " -n: set no_new_privs\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 101 | " -p: enter new pid namespace (implies -vr)\n" |
| 102 | " -r: remount /proc read-only (implies -v)\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 103 | " -s: use seccomp\n" |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 104 | " -S <file>: set seccomp filter using <file>\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 105 | " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n" |
Kees Cook | 03b2af2 | 2014-12-18 17:11:13 -0800 | [diff] [blame^] | 106 | " Requires -n when not running as root\n" |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 107 | " -t: mount tmpfs at /tmp inside chroot\n" |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 108 | " -u <user>: change uid to <user>\n" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 109 | " -v: enter new mount namespace\n" |
| 110 | " -V <file>: enter specified mount namespace\n"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 113 | static void seccomp_filter_usage(const char *progn) |
| 114 | { |
| 115 | const struct syscall_entry *entry = syscall_table; |
| 116 | printf("Usage: %s -S <policy.file> <program> [args...]\n\n" |
| 117 | "System call names supported:\n", progn); |
| 118 | for (; entry->name && entry->nr >= 0; ++entry) |
| 119 | printf(" %s [%d]\n", entry->name, entry->nr); |
| 120 | printf("\nSee minijail0(5) for example policies.\n"); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 123 | static int parse_args(struct minijail *j, int argc, char *argv[], |
| 124 | int *exit_immediately) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 125 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 126 | int opt; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 127 | int chroot = 0; |
| 128 | int mount_tmp = 0; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 129 | int use_pid_ns = 0; |
| 130 | int use_seccomp_filter = 0; |
| 131 | const size_t path_max = 4096; |
| 132 | const char *filter_path; |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 133 | if (argc > 1 && argv[1][0] != '-') |
| 134 | return 1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 135 | while ((opt = getopt(argc, argv, "u:g:sS:c:C:b:V:vrGhHinpLet")) != -1) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 136 | switch (opt) { |
| 137 | case 'u': |
| 138 | set_user(j, optarg); |
| 139 | break; |
| 140 | case 'g': |
| 141 | set_group(j, optarg); |
| 142 | break; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 143 | case 'n': |
| 144 | minijail_no_new_privs(j); |
Jorge Lucangeli Obes | 0341d6c | 2012-07-16 15:27:31 -0700 | [diff] [blame] | 145 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 146 | case 's': |
| 147 | minijail_use_seccomp(j); |
| 148 | break; |
| 149 | case 'S': |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 150 | minijail_use_seccomp_filter(j); |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 151 | if (strlen(optarg) >= path_max) { |
| 152 | fprintf(stderr, |
| 153 | "Filter path is too long.\n"); |
| 154 | exit(1); |
| 155 | } |
| 156 | filter_path = strndup(optarg, path_max); |
| 157 | if (!filter_path) { |
| 158 | fprintf(stderr, |
| 159 | "Could not strndup(3) filter path.\n"); |
| 160 | exit(1); |
| 161 | } |
| 162 | use_seccomp_filter = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 163 | break; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 164 | case 'L': |
| 165 | minijail_log_seccomp_filter_failures(j); |
| 166 | break; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 167 | case 'b': |
| 168 | add_binding(j, optarg); |
| 169 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 170 | case 'c': |
| 171 | use_caps(j, optarg); |
| 172 | break; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 173 | case 'C': |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 174 | if (0 != minijail_enter_chroot(j, optarg)) { |
| 175 | fprintf(stderr, "Could not set chroot.\n"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 176 | exit(1); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 177 | } |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 178 | chroot = 1; |
| 179 | break; |
| 180 | case 't': |
| 181 | minijail_mount_tmp(j); |
| 182 | mount_tmp = 1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 183 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 184 | case 'v': |
| 185 | minijail_namespace_vfs(j); |
| 186 | break; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 187 | case 'V': |
| 188 | minijail_namespace_enter_vfs(j, optarg); |
| 189 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 190 | case 'r': |
| 191 | minijail_remount_readonly(j); |
| 192 | break; |
| 193 | case 'G': |
| 194 | minijail_inherit_usergroups(j); |
| 195 | break; |
| 196 | case 'p': |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 197 | if (*exit_immediately) { |
| 198 | fprintf(stderr, |
| 199 | "Could not enter pid namespace because " |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 200 | "'-i' was specified.\n"); |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 201 | exit(1); |
| 202 | } |
| 203 | use_pid_ns = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 204 | minijail_namespace_pids(j); |
| 205 | break; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 206 | case 'e': |
| 207 | minijail_namespace_net(j); |
| 208 | break; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 209 | case 'i': |
| 210 | if (use_pid_ns) { |
| 211 | fprintf(stderr, |
| 212 | "Could not disable init loop because " |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 213 | "'-p' was specified.\n"); |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 214 | exit(1); |
| 215 | } |
| 216 | *exit_immediately = 1; |
| 217 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 218 | case 'H': |
| 219 | seccomp_filter_usage(argv[0]); |
| 220 | exit(1); |
| 221 | default: |
| 222 | usage(argv[0]); |
| 223 | exit(1); |
| 224 | } |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 225 | if (optind < argc && argv[optind][0] != '-') |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 226 | break; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 227 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 228 | |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 229 | /* |
| 230 | * We parse seccomp filters here to make sure we've collected all |
| 231 | * cmdline options. |
| 232 | */ |
| 233 | if (use_seccomp_filter) { |
| 234 | minijail_parse_seccomp_filters(j, filter_path); |
| 235 | free((void*)filter_path); |
| 236 | } |
| 237 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 238 | if (argc == optind) { |
| 239 | usage(argv[0]); |
| 240 | exit(1); |
| 241 | } |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 242 | |
| 243 | if (mount_tmp && !chroot) { |
| 244 | fprintf(stderr, |
| 245 | "Could not mount tmpfs at /tmp " |
| 246 | "because '-C' was not specified.\n"); |
| 247 | exit(1); |
| 248 | } |
| 249 | |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 250 | return optind; |
| 251 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 252 | |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 253 | int main(int argc, char *argv[]) |
| 254 | { |
| 255 | struct minijail *j = minijail_new(); |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 256 | char *dl_mesg = NULL; |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 257 | int exit_immediately = 0; |
| 258 | int consumed = parse_args(j, argc, argv, &exit_immediately); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 259 | ElfType elftype = ELFERROR; |
Elly Fong-Jones | f65c9fe | 2013-01-22 13:55:02 -0500 | [diff] [blame] | 260 | argc -= consumed; |
| 261 | argv += consumed; |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 262 | |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 263 | /* Check that we can access the target program. */ |
Elly Fong-Jones | 6d71785 | 2013-03-19 16:29:03 -0400 | [diff] [blame] | 264 | if (access(argv[0], X_OK)) { |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 265 | fprintf(stderr, "Target program '%s' is not accessible.\n", |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 266 | argv[0]); |
Elly Fong-Jones | 6d71785 | 2013-03-19 16:29:03 -0400 | [diff] [blame] | 267 | return 1; |
| 268 | } |
Jorge Lucangeli Obes | 482cb9d | 2014-07-23 15:16:04 -0700 | [diff] [blame] | 269 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 270 | /* Check if target is statically or dynamically linked. */ |
| 271 | elftype = get_elf_linkage(argv[0]); |
| 272 | if (elftype == ELFSTATIC) { |
| 273 | /* Target binary is static. */ |
| 274 | minijail_run_static(j, argv[0], argv); |
| 275 | } else if (elftype == ELFDYNAMIC) { |
| 276 | /* |
| 277 | * Target binary is dynamically linked so we can |
| 278 | * inject libminijailpreload.so into it. |
| 279 | */ |
| 280 | |
| 281 | /* Check that we can dlopen() libminijailpreload.so. */ |
| 282 | if (!dlopen(PRELOADPATH, RTLD_LAZY | RTLD_LOCAL)) { |
| 283 | dl_mesg = dlerror(); |
| 284 | fprintf(stderr, "dlopen(): %s\n", dl_mesg); |
| 285 | return 1; |
| 286 | } |
| 287 | minijail_run(j, argv[0], argv); |
| 288 | } else { |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 289 | fprintf(stderr, |
| 290 | "Target program '%s' is not a valid ELF file.\n", |
| 291 | argv[0]); |
Jorge Lucangeli Obes | 4b2d5ee | 2014-01-09 15:47:47 -0800 | [diff] [blame] | 292 | return 1; |
| 293 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 294 | |
Christopher Wiley | 88f76a7 | 2013-11-01 14:12:56 -0700 | [diff] [blame] | 295 | if (exit_immediately) { |
| 296 | info("not running init loop, exiting immediately"); |
| 297 | return 0; |
| 298 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 299 | return minijail_wait(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 300 | } |