blob: 524ac7f13202cb59aa92f2f8618efd357a4db332 [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
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10
11#include "libminijail.h"
Will Drewry32ac9f52011-08-18 21:36:27 -050012#include "libsyscalls.h"
Elly Jonescd7a9042011-07-22 13:56:51 -040013
Elly Jonese1749eb2011-10-07 13:54:59 -040014static void set_user(struct minijail *j, const char *arg)
15{
16 char *end = NULL;
17 int uid = strtod(arg, &end);
18 if (!*end && *arg) {
19 minijail_change_uid(j, uid);
20 return;
21 }
Elly Jonescd7a9042011-07-22 13:56:51 -040022
Elly Jonese1749eb2011-10-07 13:54:59 -040023 if (minijail_change_user(j, arg)) {
24 fprintf(stderr, "Bad user: '%s'\n", arg);
25 exit(1);
26 }
Elly Jonescd7a9042011-07-22 13:56:51 -040027}
28
Elly Jonese1749eb2011-10-07 13:54:59 -040029static void set_group(struct minijail *j, const char *arg)
30{
31 char *end = NULL;
32 int gid = strtod(arg, &end);
33 if (!*end && *arg) {
34 minijail_change_gid(j, gid);
35 return;
36 }
Elly Jonescd7a9042011-07-22 13:56:51 -040037
Elly Jonese1749eb2011-10-07 13:54:59 -040038 if (minijail_change_group(j, arg)) {
39 fprintf(stderr, "Bad group: '%s'\n", arg);
40 exit(1);
41 }
Elly Jonescd7a9042011-07-22 13:56:51 -040042}
43
Elly Jonese1749eb2011-10-07 13:54:59 -040044static void use_caps(struct minijail *j, const char *arg)
45{
46 uint64_t caps;
47 char *end = NULL;
48 caps = strtoull(arg, &end, 16);
49 if (*end) {
50 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
51 exit(1);
52 }
53 minijail_use_caps(j, caps);
Elly Jonescd7a9042011-07-22 13:56:51 -040054}
55
Elly Jones51a5b6c2011-10-12 19:09:26 -040056static void add_binding(struct minijail *j, char *arg) {
57 char *src = strtok(arg, ",");
Elly Jones5ba42b52011-12-07 13:31:43 -050058 char *dest = strtok(NULL, ",");
59 char *flags = strtok(NULL, ",");
Elly Jones51a5b6c2011-10-12 19:09:26 -040060 if (!src || !dest) {
61 fprintf(stderr, "Bad binding: %s %s\n", src, dest);
62 exit(1);
63 }
64 if (minijail_bind(j, src, dest, flags ? atoi(flags) : 0)) {
65 fprintf(stderr, "Bind failure\n");
66 exit(1);
67 }
68}
69
Elly Jonese1749eb2011-10-07 13:54:59 -040070static void usage(const char *progn)
71{
Elly Jones51a5b6c2011-10-12 19:09:26 -040072 printf("Usage: %s [-Ghprsv] [-b <src>,<dest>[,<writeable>]] [-c <caps>] "
73 "[-C <dir>] [-g <group>] [-S <file>] [-u <user>] <program> "
74 "[args...]\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040075 " -b: binds <src> to <dest> in chroot. Multiple "
76 "instances allowed\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040077 " -c <caps>: restrict caps to <caps>\n"
Elly Jonesa8d1e1b2011-10-21 15:38:00 -040078 " -C <dir>: chroot to <dir>\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040079 " -G: inherit secondary groups from uid\n"
80 " -g <group>: change gid to <group>\n"
81 " -h: help (this message)\n"
82 " -H: seccomp filter help message\n"
Elly Jonese58176c2012-01-23 11:46:17 -050083 " -p: use pid namespace (implies -vr)\n"
Elly Jonesfdd5f2d2012-01-23 13:27:43 -050084 " -r: remount /proc readonly (implies -v)\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040085 " -s: use seccomp\n"
86 " -S <file>: set seccomp filters using <file>\n"
87 " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n"
88 " -u <user>: change uid to <user>\n"
Jorge Lucangeli Obes2343d832012-04-25 21:59:48 -070089 " -v: use vfs namespace\n"
90 " -F: no dry run, force setting seccomp filters\n",
91 progn);
Elly Jonescd7a9042011-07-22 13:56:51 -040092}
93
Elly Jonese1749eb2011-10-07 13:54:59 -040094static void seccomp_filter_usage(const char *progn)
95{
96 const struct syscall_entry *entry = syscall_table;
97 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
98 "System call names supported:\n", progn);
99 for (; entry->name && entry->nr >= 0; ++entry)
100 printf(" %s [%d]\n", entry->name, entry->nr);
101 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -0500102}
103
Elly Jonese1749eb2011-10-07 13:54:59 -0400104int main(int argc, char *argv[])
105{
106 struct minijail *j = minijail_new();
Elly Jonescd7a9042011-07-22 13:56:51 -0400107
Elly Jonese1749eb2011-10-07 13:54:59 -0400108 int opt;
Jorge Lucangeli Obes2343d832012-04-25 21:59:48 -0700109 int use_seccomp_filter = 0;
110 int dry_run = 1;
111 while ((opt = getopt(argc, argv, "u:g:sS:c:C:b:vrGhHpF")) != -1) {
Elly Jonese1749eb2011-10-07 13:54:59 -0400112 switch (opt) {
113 case 'u':
114 set_user(j, optarg);
115 break;
116 case 'g':
117 set_group(j, optarg);
118 break;
119 case 's':
120 minijail_use_seccomp(j);
121 break;
122 case 'S':
123 minijail_parse_seccomp_filters(j, optarg);
124 minijail_use_seccomp_filter(j);
Jorge Lucangeli Obes2343d832012-04-25 21:59:48 -0700125 use_seccomp_filter = 1;
126 break;
127 case 'F':
128 dry_run = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400129 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400130 case 'b':
131 add_binding(j, optarg);
132 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400133 case 'c':
134 use_caps(j, optarg);
135 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400136 case 'C':
137 minijail_enter_chroot(j, optarg);
138 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400139 case 'v':
140 minijail_namespace_vfs(j);
141 break;
142 case 'r':
143 minijail_remount_readonly(j);
144 break;
145 case 'G':
146 minijail_inherit_usergroups(j);
147 break;
148 case 'p':
149 minijail_namespace_pids(j);
150 break;
151 case 'H':
152 seccomp_filter_usage(argv[0]);
153 exit(1);
154 default:
155 usage(argv[0]);
156 exit(1);
157 }
158 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400159
Jorge Lucangeli Obes2343d832012-04-25 21:59:48 -0700160 /* TODO(jorgelo): remove this when the seccomp BPF merge is done. */
161 if (use_seccomp_filter && !dry_run)
162 minijail_force_seccomp_filter(j);
163
Elly Jonese1749eb2011-10-07 13:54:59 -0400164 if (argc == optind) {
165 usage(argv[0]);
166 exit(1);
167 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400168
Elly Jonese1749eb2011-10-07 13:54:59 -0400169 argc -= optind;
170 argv += optind;
Elly Jonescd7a9042011-07-22 13:56:51 -0400171
Elly Jonese1749eb2011-10-07 13:54:59 -0400172 minijail_run(j, argv[0], argv);
173 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400174}