blob: f13dd20b7f47beb9e26e83704fd596374af6ba94 [file] [log] [blame]
Elly Jonescd7a9042011-07-22 13:56:51 -04001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * 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, ",");
58 char *dest = strtok(arg, ",");
59 char *flags = strtok(arg, ",");
60 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"
75 " -b: binds <src> to <dest> in chroot. Multiple instances allowed\n"
Elly Jonese1749eb2011-10-07 13:54:59 -040076 " -c <caps>: restrict caps to <caps>\n"
77 " -G: inherit secondary groups from uid\n"
78 " -g <group>: change gid to <group>\n"
79 " -h: help (this message)\n"
80 " -H: seccomp filter help message\n"
81 " -p: use pid namespace\n"
82 " -r: remount filesystems readonly (implies -v)\n"
83 " -s: use seccomp\n"
84 " -S <file>: set seccomp filters using <file>\n"
85 " E.g., -S /usr/share/filters/<prog>.$(uname -m)\n"
86 " -u <user>: change uid to <user>\n"
87 " -v: use vfs namespace\n", progn);
Elly Jonescd7a9042011-07-22 13:56:51 -040088}
89
Elly Jonese1749eb2011-10-07 13:54:59 -040090static void seccomp_filter_usage(const char *progn)
91{
92 const struct syscall_entry *entry = syscall_table;
93 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
94 "System call names supported:\n", progn);
95 for (; entry->name && entry->nr >= 0; ++entry)
96 printf(" %s [%d]\n", entry->name, entry->nr);
97 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -050098}
99
Elly Jonese1749eb2011-10-07 13:54:59 -0400100int main(int argc, char *argv[])
101{
102 struct minijail *j = minijail_new();
Elly Jonescd7a9042011-07-22 13:56:51 -0400103
Elly Jonese1749eb2011-10-07 13:54:59 -0400104 int opt;
105 while ((opt = getopt(argc, argv, "u:g:sS:c:vrGhHp")) != -1) {
106 switch (opt) {
107 case 'u':
108 set_user(j, optarg);
109 break;
110 case 'g':
111 set_group(j, optarg);
112 break;
113 case 's':
114 minijail_use_seccomp(j);
115 break;
116 case 'S':
117 minijail_parse_seccomp_filters(j, optarg);
118 minijail_use_seccomp_filter(j);
119 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400120 case 'b':
121 add_binding(j, optarg);
122 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400123 case 'c':
124 use_caps(j, optarg);
125 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400126 case 'C':
127 minijail_enter_chroot(j, optarg);
128 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400129 case 'v':
130 minijail_namespace_vfs(j);
131 break;
132 case 'r':
133 minijail_remount_readonly(j);
134 break;
135 case 'G':
136 minijail_inherit_usergroups(j);
137 break;
138 case 'p':
139 minijail_namespace_pids(j);
140 break;
141 case 'H':
142 seccomp_filter_usage(argv[0]);
143 exit(1);
144 default:
145 usage(argv[0]);
146 exit(1);
147 }
148 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400149
Elly Jonese1749eb2011-10-07 13:54:59 -0400150 if (argc == optind) {
151 usage(argv[0]);
152 exit(1);
153 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400154
Elly Jonese1749eb2011-10-07 13:54:59 -0400155 argc -= optind;
156 argv += optind;
Elly Jonescd7a9042011-07-22 13:56:51 -0400157
Elly Jonese1749eb2011-10-07 13:54:59 -0400158 minijail_run(j, argv[0], argv);
159 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400160}