drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // Some portions Copyright (c) 2009 The Chromium Authors. |
| 5 | // |
| 6 | // Default implementation of the Env interface. |
| 7 | |
| 8 | #include "minijail/env.h" |
| 9 | |
| 10 | #include <asm/unistd.h> |
| 11 | #include <errno.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <grp.h> |
Chris Masone | 871d781 | 2010-02-04 09:34:23 -0800 | [diff] [blame] | 14 | #include <pwd.h> |
drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 15 | #include <sched.h> |
| 16 | #include <signal.h> |
| 17 | #include <stdarg.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/capability.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/prctl.h> |
| 25 | #include <sys/resource.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
Will Drewry | 6b195b4 | 2010-04-01 09:39:30 -0500 | [diff] [blame] | 30 | #include <sys/wait.h> |
drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <base/logging.h> |
| 34 | |
| 35 | // prctl constants that are still missing in the headers. |
| 36 | #define PR_GET_KEEPCAPS 7 |
| 37 | #define PR_SET_KEEPCAPS 8 |
| 38 | #define PR_CAPBSET_READ 23 |
| 39 | #define PR_CAPBSET_DROP 24 |
| 40 | #define PR_GET_SECUREBITS 27 |
| 41 | #define PR_SET_SECUREBITS 28 |
| 42 | |
| 43 | namespace chromeos { |
| 44 | |
| 45 | namespace minijail { |
| 46 | |
| 47 | bool Env::DisableTracing() const { |
| 48 | DLOG(INFO) << "Disabling DUMPABLE..."; |
| 49 | if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0)) { |
| 50 | PLOG(FATAL) << "Failed to set PR_SET_KEEPCAPS"; |
| 51 | } |
| 52 | if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 53 | LOG(FATAL) << "PR_SET_DUMPABLE could not be set"; |
| 54 | } |
| 55 | DLOG(INFO) << "Success"; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool Env::KeepRootCapabilities() const { |
| 60 | DLOG(INFO) << "Enabling KEEPCAPS..."; |
| 61 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 62 | PLOG(FATAL) << "Failed to set PR_SET_KEEPCAPS"; |
| 63 | } |
| 64 | if (prctl(PR_GET_KEEPCAPS, 0) != 1) { |
| 65 | LOG(FATAL) << "PR_GET_KEEPCAPS could not be set"; |
| 66 | } |
| 67 | |
| 68 | DLOG(INFO) << "Success."; |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool Env::DisableDefaultRootPrivileges() const { |
| 73 | DLOG(INFO) << "Enabling SECURE_ALL..."; |
| 74 | // From: kernel/include/linux/securebits.h: |
| 75 | // http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/securebits.h |
| 76 | const int kSecureBitsAllLocked = 0x3f; |
| 77 | if (prctl(PR_SET_SECUREBITS, kSecureBitsAllLocked)) { |
| 78 | PLOG(FATAL) << "Failed to set PR_SET_SECUREBITS"; |
| 79 | } |
| 80 | DLOG(INFO) << "Success."; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool Env::ChangeUser(uid_t uid, gid_t gid) const { |
| 85 | // TODO(wad) support supplemental groups |
| 86 | DLOG(INFO) << "Dropping root..."; |
Chris Masone | 871d781 | 2010-02-04 09:34:23 -0800 | [diff] [blame] | 87 | struct passwd* entry = getpwuid(uid); |
| 88 | endpwent(); |
| 89 | if (!entry) { |
| 90 | LOG(INFO) << "UID is unknown. Clearing all supplemental groups"; |
| 91 | PLOG_IF(FATAL, setgroups(0, NULL)) |
| 92 | << "Failed to clear supplementary groups"; |
| 93 | } else { |
| 94 | PLOG_IF(FATAL, initgroups(entry->pw_name, entry->pw_gid)) |
| 95 | << "Failed to set supplementary groups"; |
drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 96 | } |
| 97 | if (setresgid(gid, gid, gid)) { |
| 98 | PLOG(FATAL) << "Failed to change to gid " << gid; |
| 99 | } |
| 100 | if (setresuid(uid, uid, uid)) { |
| 101 | PLOG(FATAL) << "Failed to change to uid " << uid; |
| 102 | } |
| 103 | DLOG(INFO) << "Success."; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | // At present, the total number of capabilities is less than 32. We |
| 108 | // will just pack them into a bitmask to save on effort. |
| 109 | bool Env::SanitizeBoundingSet(uint64 cap_mask) const { |
| 110 | unsigned int cap; |
| 111 | DLOG(INFO) << "Cleaning the bounding set..."; |
| 112 | // XXX: we read until prctl complains but that may not |
| 113 | // match CAP_LAST_CAP. We'll just drop the excess if it turns up. |
| 114 | // We mustn“t drop CAP_SETPCAP on the way though. |
| 115 | static const uint32 kBitsInAByte = 8; |
| 116 | static const uint32 kMaxCaps = sizeof(cap_mask) * kBitsInAByte; |
| 117 | for (cap = 0; cap < kMaxCaps && prctl(PR_CAPBSET_READ, cap) >= 0; ++cap) { |
| 118 | if (cap == CAP_SETPCAP) { |
| 119 | continue; |
| 120 | } |
| 121 | if (cap_mask & (1ULL << (cap))) { |
| 122 | DLOG(INFO) << "Leaving cap " << cap << " in bounding set"; |
| 123 | continue; |
| 124 | } |
| 125 | if (prctl(PR_CAPBSET_DROP, cap)) { |
| 126 | PLOG(FATAL) << "Failed to clean the bounding set of cap " << cap; |
| 127 | } |
| 128 | } |
| 129 | DLOG(INFO) << "Success."; |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool Env::SanitizeCapabilities(uint64 effective_capmask) const { |
| 134 | DLOG(INFO) << "Dropping capabilities..."; |
| 135 | unsigned int cap; |
| 136 | cap_t caps = cap_get_proc(); |
| 137 | cap_value_t raise_flag[1]; |
| 138 | if (!caps) { |
| 139 | PLOG(FATAL) << "cap_get_proc failed"; |
| 140 | } |
| 141 | if (cap_clear_flag(caps, CAP_INHERITABLE)) { |
| 142 | PLOG(FATAL) << "Failed to clear all inheritable caps"; |
| 143 | } |
| 144 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) { |
| 145 | PLOG(FATAL) << "Failed to clear all effective caps"; |
| 146 | } |
| 147 | if (cap_clear_flag(caps, CAP_PERMITTED)) { |
| 148 | PLOG(FATAL) << "Failed to clear all permitted caps"; |
| 149 | } |
| 150 | for (cap = 0; cap < sizeof(effective_capmask)*8; ++cap) { |
| 151 | // In a secure_noroot jail, cap_setpcap is safe. |
| 152 | if (cap == CAP_SETPCAP || |
| 153 | effective_capmask & (1 << cap)) { |
| 154 | raise_flag[0] = cap; |
| 155 | DLOG(INFO) << "Adding cap " << cap << "=eip"; |
| 156 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, raise_flag, CAP_SET)) { |
| 157 | PLOG(FATAL) << "Failed to add cap " << cap << " to the effective set"; |
| 158 | } |
| 159 | if (cap_set_flag(caps, CAP_PERMITTED, 1, raise_flag, CAP_SET)) { |
| 160 | PLOG(FATAL) << "Failed to add cap " << cap << " to the permitted set"; |
| 161 | } |
| 162 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, raise_flag, CAP_SET)) { |
| 163 | PLOG(FATAL) << "Failed to add cap " << cap << " to the inherite set"; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | if (cap_set_proc(caps)) { |
| 168 | PLOG(FATAL) << "Failed to apply cleaned capset"; |
| 169 | } |
| 170 | cap_free(caps); |
| 171 | DLOG(INFO) << "Success."; |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool Env::FilterSyscallsBySource() const { |
| 176 | DLOG(INFO) << "Calling seccomp(2)"; |
| 177 | if (prctl(PR_SET_SECCOMP, 2)) { |
| 178 | PLOG(FATAL) << "Failed to enabled seccomp(2)"; |
| 179 | } |
| 180 | DLOG(INFO) << "System calls now filtered by source"; |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | bool Env::FilterSyscallsBenchmarkOnly() const { |
| 185 | DLOG(INFO) << "Calling seccomp(3)"; |
| 186 | if (prctl(PR_SET_SECCOMP, 3)) { |
| 187 | PLOG(FATAL) << "Failed to enabled seccomp(3)"; |
| 188 | } |
| 189 | DLOG(INFO) << "System calls now nop filtered"; |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool Env::EnterNamespace(int namespaces) const { |
| 194 | if (namespaces == 0) { |
| 195 | DLOG(INFO) << "No namespacing to be done."; |
| 196 | return true; |
| 197 | } |
| 198 | DLOG(INFO) << "Entering namespaces " << namespaces; |
| 199 | // TODO(wad) support namespace args |
| 200 | const pid_t pid = syscall( |
| 201 | __NR_clone, namespaces | CLONE_VFORK | SIGCHLD, 0, 0, 0); |
| 202 | if (pid == -1) { |
| 203 | PLOG(FATAL) << "Could not use PID namespacing"; |
| 204 | return false; |
| 205 | } |
| 206 | if (pid) { |
Will Drewry | 6b195b4 | 2010-04-01 09:39:30 -0500 | [diff] [blame] | 207 | // We want to wait on the child pid to ensure that pid-tracking code |
| 208 | // isn't completely broken. |
| 209 | int status = 0; |
| 210 | waitpid(pid, &status, 0); |
drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 211 | // Kill the original process without atexit handlers. |
Will Drewry | 6b195b4 | 2010-04-01 09:39:30 -0500 | [diff] [blame] | 212 | DLOG(INFO) << "jailed process death:" << pid; |
| 213 | if (WIFEXITED(status)) { |
| 214 | _exit(WEXITSTATUS(status)); |
| 215 | } |
| 216 | if (WIFSIGNALED(status)) { |
| 217 | _exit(WTERMSIG(status)); |
| 218 | } |
| 219 | DLOG(INFO) << "unknown terminal condition for child"; |
| 220 | _exit(1); |
drewry@google.com | bd940e9 | 2009-12-07 19:13:27 +0000 | [diff] [blame] | 221 | } |
| 222 | DLOG(INFO) << "Success: " << getpid(); |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | bool Env::Mount() const { |
| 227 | DLOG(INFO) << "Attempting to mount /proc RO."; |
| 228 | if (mount("proc", |
| 229 | "/proc", |
| 230 | "proc", |
| 231 | MS_NODEV|MS_NOEXEC|MS_NOSUID|MS_RDONLY, |
| 232 | "")) { |
| 233 | PLOG(FATAL) << "Failed to mount a local /proc"; |
| 234 | } |
| 235 | DLOG(INFO) << "Success."; |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | bool Env::Run(const char *path, char * const *argv, char * const *envp) const { |
| 240 | // TODO(wad) log-pid option |
| 241 | DLOG(INFO) << "Executing: " << path << " with args: "; |
| 242 | for (char * const* arg = argv; *arg; ++arg) { |
| 243 | DLOG(INFO) << "-> " << *arg; |
| 244 | } |
| 245 | execve(path, argv, envp); |
| 246 | PLOG(FATAL) << "failed to execute " << path; |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | } // namespace minijail |
| 251 | } // namespace chromeos |