Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1205)

Side by Side Diff: minijail_main.cc

Issue 6881066: [minijail] Add the ability to set capabilities from the command line (Closed) Base URL: http://git.chromium.org/git/minijail.git@master
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « minijail.cc ('k') | minijail_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // Some portions Copyright (c) 2009 The Chromium Authors. 4 // Some portions Copyright (c) 2011 The Chromium Authors.
5 // 5 //
6 // Driver program for applying a minijail from the commandline to 6 // Driver program for applying a minijail from the commandline to
7 // a process and its children (depending on the feature). 7 // a process and its children (depending on the feature).
8 8
9 #include "minijail/minijail.h" 9 #include "minijail/minijail.h"
10 10
11 #include <errno.h> 11 #include <errno.h>
12 #include <linux/capability.h> 12 #include <linux/capability.h>
13 #include <stdio.h> 13 #include <stdio.h>
14 #include <sys/prctl.h> 14 #include <sys/prctl.h>
15 #include <unistd.h> 15 #include <unistd.h>
16 16
17 #include <iostream> 17 #include <iostream>
18 #include <new> 18 #include <new>
19 #include <string> 19 #include <string>
20 #include <vector> 20 #include <vector>
21 21
22 #include <base/basictypes.h> 22 #include <base/basictypes.h>
23 #include <base/command_line.h> 23 #include <base/command_line.h>
24 #include <base/logging.h> 24 #include <base/logging.h>
25 #include <base/string_number_conversions.h>
25 #include <base/string_util.h> 26 #include <base/string_util.h>
26 27
27 namespace switches { 28 namespace switches {
28 static const char kAddReadonlyMounts[] = "add-readonly-mounts"; 29 static const char kAddReadonlyMounts[] = "add-readonly-mounts";
29 static const char kDisableTracing[] = "disable-tracing"; 30 static const char kDisableTracing[] = "disable-tracing";
30 static const char kEnforceSyscallsBenchmark[] = "enforce-syscall-benchmark"; 31 static const char kEnforceSyscallsBenchmark[] = "enforce-syscall-benchmark";
31 static const char kEnforceSyscallsBySource[] = "enforce-syscall-by-source"; 32 static const char kEnforceSyscallsBySource[] = "enforce-syscall-by-source";
32 static const char kGid[] = "gid"; 33 static const char kGid[] = "gid";
33 static const char kNamespaceVfs[] = "namespace-vfs"; 34 static const char kNamespaceVfs[] = "namespace-vfs";
34 static const char kNamespacePid[] = "namespace-pid"; 35 static const char kNamespacePid[] = "namespace-pid";
(...skipping 21 matching lines...) Expand all
56 " --namespace-vfs\n" 57 " --namespace-vfs\n"
57 " Enables a process-tree specific VFS view.\n" 58 " Enables a process-tree specific VFS view.\n"
58 " --namespace-pid\n" 59 " --namespace-pid\n"
59 " Makes the executed process into procss id 1 in its own process view.\n" 60 " Makes the executed process into procss id 1 in its own process view.\n"
60 " With --add-readonly-mounts, other processes will not be visible\n" 61 " With --add-readonly-mounts, other processes will not be visible\n"
61 " --sanitize-environment\n" 62 " --sanitize-environment\n"
62 " Scrubs the environment clean of potentially dangerous values.\n" 63 " Scrubs the environment clean of potentially dangerous values.\n"
63 " (Note, this is a blacklist and not a whitelist so it may need attention)\n" 64 " (Note, this is a blacklist and not a whitelist so it may need attention)\n"
64 " --uid [number]\n" 65 " --uid [number]\n"
65 " Numeric uid to transition to prior to execution.\n" 66 " Numeric uid to transition to prior to execution.\n"
66 " --use-capabilities\n" 67 " --use-capabilities [uint64 bitmask]\n"
67 " Restricts all root-level capabilities to CAP_SETPCAP and enables\n" 68 " Restricts all root-level capabilities to CAP_SETPCAP and enables\n"
68 " SECURE_NOROOT.\n" 69 " SECURE_NOROOT.\n"
69 " -- /path/to/program [arg1 [arg2 [ . . . ] ] ]\n" 70 " -- /path/to/program [arg1 [arg2 [ . . . ] ] ]\n"
70 " Supplies the required program to execute and its arguments.\n" 71 " Supplies the required program to execute and its arguments.\n"
71 " At present, an empty environment will be passed.\n" 72 " At present, an empty environment will be passed.\n"
72 "\n"; 73 "\n";
73 74
74 } // namespace switches 75 } // namespace switches
75 76
76 static void ProcessSwitches(CommandLine *cl, 77 static void ProcessSwitches(CommandLine *cl,
(...skipping 10 matching lines...) Expand all
87 cl->HasSwitch(switches::kAddReadonlyMounts)); 88 cl->HasSwitch(switches::kAddReadonlyMounts));
88 jail_opts->set_disable_tracing(cl->HasSwitch(switches::kDisableTracing)); 89 jail_opts->set_disable_tracing(cl->HasSwitch(switches::kDisableTracing));
89 jail_opts->set_enforce_syscalls_benchmark( 90 jail_opts->set_enforce_syscalls_benchmark(
90 cl->HasSwitch(switches::kEnforceSyscallsBenchmark)); 91 cl->HasSwitch(switches::kEnforceSyscallsBenchmark));
91 jail_opts->set_enforce_syscalls_by_source( 92 jail_opts->set_enforce_syscalls_by_source(
92 cl->HasSwitch(switches::kEnforceSyscallsBySource)); 93 cl->HasSwitch(switches::kEnforceSyscallsBySource));
93 jail_opts->set_use_capabilities(cl->HasSwitch(switches::kUseCapabilities)); 94 jail_opts->set_use_capabilities(cl->HasSwitch(switches::kUseCapabilities));
94 jail_opts->set_sanitize_environment( 95 jail_opts->set_sanitize_environment(
95 cl->HasSwitch(switches::kSanitizeEnvironment)); 96 cl->HasSwitch(switches::kSanitizeEnvironment));
96 97
98 if (jail_opts->use_capabilities()) {
99 jail_opts->set_caps_bitmask(0);
100 // TODO(cmasone): switch to something that parses unsigned ints.
101 int64 caps = 0;
102 if (base::StringToInt64(
103 cl->GetSwitchValueASCII(switches::kUseCapabilities), &caps)) {
104 uint64 bitmask = (caps < 0 ? 0 : caps);
105 jail_opts->set_caps_bitmask(bitmask);
106 }
107 }
108
97 std::string uid_string = cl->GetSwitchValueASCII(switches::kUid); 109 std::string uid_string = cl->GetSwitchValueASCII(switches::kUid);
98 if (!uid_string.empty()) { 110 if (!uid_string.empty()) {
99 errno = 0; 111 errno = 0;
100 uid_t uid = static_cast<uid_t>(strtol(uid_string.c_str(), NULL, 0)); 112 uid_t uid = static_cast<uid_t>(strtol(uid_string.c_str(), NULL, 0));
101 PLOG_IF(WARNING, errno) << "failed to parse uid"; 113 PLOG_IF(WARNING, errno) << "failed to parse uid";
102 jail_opts->set_uid(uid); 114 jail_opts->set_uid(uid);
103 } 115 }
104 116
105 std::string gid_string = cl->GetSwitchValueASCII(switches::kGid); 117 std::string gid_string = cl->GetSwitchValueASCII(switches::kGid);
106 if (!gid_string.empty()) { 118 if (!gid_string.empty()) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 ProcessSwitches(cl, &jail_opts); 157 ProcessSwitches(cl, &jail_opts);
146 jail_opts.set_environment(envp); 158 jail_opts.set_environment(envp);
147 159
148 LOG_IF(FATAL, !jail_opts.executable_path()) << "No executable given"; 160 LOG_IF(FATAL, !jail_opts.executable_path()) << "No executable given";
149 161
150 chromeos::MiniJail jail; 162 chromeos::MiniJail jail;
151 jail.Initialize(&jail_opts); 163 jail.Initialize(&jail_opts);
152 bool ok = jail.Jail() && jail.Run(); 164 bool ok = jail.Jail() && jail.Run();
153 return !ok; 165 return !ok;
154 } 166 }
OLDNEW
« no previous file with comments | « minijail.cc ('k') | minijail_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698