blob: 0559f552c3fdb4cc20f6bdfa7b0bb19aa2b0b4ce [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, ",");
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"
83 " -p: use pid namespace\n"
84 " -r: remount filesystems readonly (implies -v)\n"
85 " -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"
89 " -v: use vfs namespace\n", progn);
Elly Jonescd7a9042011-07-22 13:56:51 -040090}
91
Elly Jonese1749eb2011-10-07 13:54:59 -040092static void seccomp_filter_usage(const char *progn)
93{
94 const struct syscall_entry *entry = syscall_table;
95 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
96 "System call names supported:\n", progn);
97 for (; entry->name && entry->nr >= 0; ++entry)
98 printf(" %s [%d]\n", entry->name, entry->nr);
99 printf("\nSee minijail0(5) for example policies.\n");
Will Drewry32ac9f52011-08-18 21:36:27 -0500100}
101
Elly Jonese1749eb2011-10-07 13:54:59 -0400102int main(int argc, char *argv[])
103{
104 struct minijail *j = minijail_new();
Elly Jonescd7a9042011-07-22 13:56:51 -0400105
Elly Jonese1749eb2011-10-07 13:54:59 -0400106 int opt;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400107 while ((opt = getopt(argc, argv, "u:g:sS:c:C:b:vrGhHp")) != -1) {
Elly Jonese1749eb2011-10-07 13:54:59 -0400108 switch (opt) {
109 case 'u':
110 set_user(j, optarg);
111 break;
112 case 'g':
113 set_group(j, optarg);
114 break;
115 case 's':
116 minijail_use_seccomp(j);
117 break;
118 case 'S':
119 minijail_parse_seccomp_filters(j, optarg);
120 minijail_use_seccomp_filter(j);
121 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400122 case 'b':
123 add_binding(j, optarg);
124 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400125 case 'c':
126 use_caps(j, optarg);
127 break;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400128 case 'C':
129 minijail_enter_chroot(j, optarg);
130 break;
Elly Jonese1749eb2011-10-07 13:54:59 -0400131 case 'v':
132 minijail_namespace_vfs(j);
133 break;
134 case 'r':
135 minijail_remount_readonly(j);
136 break;
137 case 'G':
138 minijail_inherit_usergroups(j);
139 break;
140 case 'p':
141 minijail_namespace_pids(j);
142 break;
143 case 'H':
144 seccomp_filter_usage(argv[0]);
145 exit(1);
146 default:
147 usage(argv[0]);
148 exit(1);
149 }
150 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400151
Elly Jonese1749eb2011-10-07 13:54:59 -0400152 if (argc == optind) {
153 usage(argv[0]);
154 exit(1);
155 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400156
Elly Jonese1749eb2011-10-07 13:54:59 -0400157 argc -= optind;
158 argv += optind;
Elly Jonescd7a9042011-07-22 13:56:51 -0400159
Elly Jonese1749eb2011-10-07 13:54:59 -0400160 minijail_run(j, argv[0], argv);
161 return minijail_wait(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400162}