blob: 9e2beceb1b17f916199ccb94a5b2b9fcc750f256 [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
14static void set_user(struct minijail *j, const char *arg) {
15 char *end = NULL;
16 int uid = strtod(arg, &end);
17 if (!*end && *arg) {
18 minijail_change_uid(j, uid);
19 return;
20 }
21
22 if (minijail_change_user(j, arg)) {
23 fprintf(stderr, "Bad user: '%s'\n", arg);
24 exit(1);
25 }
26}
27
28static void set_group(struct minijail *j, const char *arg) {
29 char *end = NULL;
30 int gid = strtod(arg, &end);
31 if (!*end && *arg) {
32 minijail_change_gid(j, gid);
33 return;
34 }
35
36 if (minijail_change_group(j, arg)) {
37 fprintf(stderr, "Bad group: '%s'\n", arg);
38 exit(1);
39 }
40}
41
42static void use_caps(struct minijail *j, const char *arg) {
43 uint64_t caps;
44 char *end = NULL;
45 caps = strtoull(arg, &end, 16);
46 if (*end) {
47 fprintf(stderr, "Invalid cap set: '%s'\n", arg);
48 exit(1);
49 }
50 minijail_use_caps(j, caps);
51}
52
53static void usage(const char *progn) {
Will Drewry32ac9f52011-08-18 21:36:27 -050054 printf("Usage: %s [-Ghprsv] [-c <caps>] [-g <group>] [-S <file>] [-u <user>] "
55 "<program> [args...]\n"
Elly Jonescd7a9042011-07-22 13:56:51 -040056 " -c: restrict caps to <caps>\n"
57 " -G: inherit groups from uid\n"
58 " -g: change gid to <group>\n"
59 " -h: help (this message)\n"
Will Drewry32ac9f52011-08-18 21:36:27 -050060 " -H: seccomp filter help message\n"
Elly Jonescd7a9042011-07-22 13:56:51 -040061 " -p: use pid namespace\n"
62 " -r: remount filesystems readonly (implies -v)\n"
63 " -s: use seccomp\n"
Will Drewry32ac9f52011-08-18 21:36:27 -050064 " -S: set seccomp filters using <file>\n"
65 " E.g., -S /usr/share/blah/seccomp_filters.$(uname -m)\n"
Elly Jonescd7a9042011-07-22 13:56:51 -040066 " -u: change uid to <user>\n"
67 " -v: use vfs namespace\n", progn);
68}
69
Will Drewry32ac9f52011-08-18 21:36:27 -050070static void seccomp_filter_usage(const char *progn) {
71 const struct syscall_entry *entry = syscall_table;
72 printf("Usage: %s -S <policy.file> <program> [args...]\n\n"
73 "System call names supported:\n", progn);
74 for (; entry->name && entry->nr >= 0; ++entry)
75 printf(" %s [%d]\n", entry->name, entry->nr);
76 printf("\nSee minijail0(5) for example policies.\n");
77}
78
Elly Jonescd7a9042011-07-22 13:56:51 -040079int main(int argc, char *argv[]) {
80 struct minijail *j = minijail_new();
81
82 int opt;
Will Drewry32ac9f52011-08-18 21:36:27 -050083 while ((opt = getopt(argc, argv, "u:g:sS:c:vrGhHp")) != -1) {
Elly Jonescd7a9042011-07-22 13:56:51 -040084 switch (opt) {
85 case 'u':
86 set_user(j, optarg);
87 break;
88 case 'g':
89 set_group(j, optarg);
90 break;
91 case 's':
92 minijail_use_seccomp(j);
93 break;
Will Drewry32ac9f52011-08-18 21:36:27 -050094 case 'S':
95 minijail_parse_seccomp_filters(j, optarg);
96 minijail_use_seccomp_filter(j);
97 break;
Elly Jonescd7a9042011-07-22 13:56:51 -040098 case 'c':
99 use_caps(j, optarg);
100 break;
101 case 'v':
102 minijail_namespace_vfs(j);
103 break;
104 case 'r':
105 minijail_remount_readonly(j);
106 break;
107 case 'G':
108 minijail_inherit_usergroups(j);
109 break;
110 case 'p':
111 minijail_namespace_pids(j);
112 break;
Will Drewry32ac9f52011-08-18 21:36:27 -0500113 case 'H':
114 seccomp_filter_usage(argv[0]);
115 exit(1);
Elly Jonescd7a9042011-07-22 13:56:51 -0400116 default:
117 usage(argv[0]);
118 exit(1);
119 }
120 }
121
122 if (argc == optind) {
123 usage(argv[0]);
124 exit(1);
125 }
126
127 argc -= optind;
128 argv += optind;
129
130 minijail_run(j, argv[0], argv);
131 return minijail_wait(j);
132}