1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
// https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox.md
#include "sandbox/linux/suid/common/sandbox.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <asm/unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/wait.h>
#include <unistd.h>
#include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h"
#include "sandbox/linux/suid/process_util.h"
#if !defined(CLONE_NEWPID)
#define CLONE_NEWPID 0x20000000
#endif
#if !defined(CLONE_NEWNET)
#define CLONE_NEWNET 0x40000000
#endif
static bool DropRoot();
#define HANDLE_EINTR(x) ({ \
long eintr_wrapper_result; \
do { \
eintr_wrapper_result = (x); \
} while (eintr_wrapper_result == -1L && errno == EINTR); \
eintr_wrapper_result; \
})
static void FatalError(const char* msg, ...)
__attribute__((noreturn, format(printf, 1, 2)));
static void FatalError(const char* msg, ...) {
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
fprintf(stderr, ": %s\n", strerror(errno));
fflush(stderr);
va_end(ap);
_exit(1);
}
static void ExitWithErrorSignalHandler(int signal) {
const char msg[] = "\nThe setuid sandbox got signaled, exiting.\n";
if (-1 == write(2, msg, sizeof(msg) - 1)) {
// Do nothing.
}
_exit(1);
}
// We will chroot() to the helper's /proc/self directory. Anything there will
// not exist anymore if we make sure to wait() for the helper.
//
// /proc/self/fdinfo or /proc/self/fd are especially safe and will be empty
// even if the helper survives as a zombie.
//
// There is very little reason to use fdinfo/ instead of fd/ but we are
// paranoid. fdinfo/ only exists since 2.6.22 so we allow fallback to fd/
#define SAFE_DIR "/proc/self/fdinfo"
#define SAFE_DIR2 "/proc/self/fd"
static bool SpawnChrootHelper() {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
perror("socketpair");
return false;
}
char* safedir = NULL;
struct stat sdir_stat;
if (!stat(SAFE_DIR, &sdir_stat) && S_ISDIR(sdir_stat.st_mode)) {
safedir = SAFE_DIR;
} else if (!stat(SAFE_DIR2, &sdir_stat) && S_ISDIR(sdir_stat.st_mode)) {
safedir = SAFE_DIR2;
} else {
fprintf(stderr, "Could not find %s\n", SAFE_DIR2);
return false;
}
const pid_t pid = syscall(__NR_clone, CLONE_FS | SIGCHLD, 0, 0, 0);
if (pid == -1) {
perror("clone");
close(sv[0]);
close(sv[1]);
return false;
}
if (pid == 0) {
// We share our files structure with an untrusted process. As a security in
// depth measure, we make sure that we can't open anything by mistake.
// TODO(agl): drop CAP_SYS_RESOURCE / use SECURE_NOROOT
const struct rlimit nofile = {0, 0};
if (setrlimit(RLIMIT_NOFILE, &nofile))
FatalError("Setting RLIMIT_NOFILE");
if (close(sv[1]))
FatalError("close");
// wait for message
char msg;
ssize_t bytes;
do {
bytes = read(sv[0], &msg, 1);
} while (bytes == -1 && errno == EINTR);
if (bytes == 0)
_exit(0);
if (bytes != 1)
FatalError("read");
// do chrooting
if (msg != kMsgChrootMe)
FatalError("Unknown message from sandboxed process");
// sanity check
if (chdir(safedir))
FatalError("Cannot chdir into /proc/ directory");
if (chroot(safedir))
FatalError("Cannot chroot into /proc/ directory");
if (chdir("/"))
FatalError("Cannot chdir to / after chroot");
const char reply = kMsgChrootSuccessful;
do {
bytes = write(sv[0], &reply, 1);
} while (bytes == -1 && errno == EINTR);
if (bytes != 1)
FatalError("Writing reply");
_exit(0);
// We now become a zombie. /proc/self/fd(info) is now an empty dir and we
// are chrooted there.
// Our (unprivileged) parent should not even be able to open "." or "/"
// since they would need to pass the ptrace() check. If our parent wait()
// for us, our root directory will completely disappear.
}
if (close(sv[0])) {
close(sv[1]);
perror("close");
return false;
}
// In the parent process, we install an environment variable containing the
// number of the file descriptor.
char desc_str[64];
int printed = snprintf(desc_str, sizeof(desc_str), "%u", sv[1]);
if (printed < 0 || printed >= (int)sizeof(desc_str)) {
fprintf(stderr, "Failed to snprintf\n");
return false;
}
if (setenv(kSandboxDescriptorEnvironmentVarName, desc_str, 1)) {
perror("setenv");
close(sv[1]);
return false;
}
// We also install an environment variable containing the pid of the child
char helper_pid_str[64];
printed = snprintf(helper_pid_str, sizeof(helper_pid_str), "%u", pid);
if (printed < 0 || printed >= (int)sizeof(helper_pid_str)) {
fprintf(stderr, "Failed to snprintf\n");
return false;
}
if (setenv(kSandboxHelperPidEnvironmentVarName, helper_pid_str, 1)) {
perror("setenv");
close(sv[1]);
return false;
}
return true;
}
// Block until child_pid exits, then exit. Try to preserve the exit code.
static void WaitForChildAndExit(pid_t child_pid) {
int exit_code = -1;
siginfo_t reaped_child_info;
// Don't "Core" on SIGABRT. SIGABRT is sent by the Chrome OS session manager
// when things are hanging.
// Here, the current process is going to waitid() and _exit(), so there is no
// point in generating a crash report. The child process is the one
// blocking us.
if (signal(SIGABRT, ExitWithErrorSignalHandler) == SIG_ERR) {
FatalError("Failed to change signal handler");
}
int wait_ret =
HANDLE_EINTR(waitid(P_PID, child_pid, &reaped_child_info, WEXITED));
if (!wait_ret && reaped_child_info.si_pid == child_pid) {
if (reaped_child_info.si_code == CLD_EXITED) {
exit_code = reaped_child_info.si_status;
} else {
// Exit with code 0 if the child got signaled.
exit_code = 0;
}
}
_exit(exit_code);
}
static bool MoveToNewNamespaces() {
// These are the sets of flags which we'll try, in order.
const int kCloneExtraFlags[] = {CLONE_NEWPID | CLONE_NEWNET, CLONE_NEWPID, };
// We need to close kZygoteIdFd before the child can continue. We use this
// socketpair to tell the child when to continue;
int sync_fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sync_fds)) {
FatalError("Failed to create a socketpair");
}
for (size_t i = 0; i < sizeof(kCloneExtraFlags) / sizeof(kCloneExtraFlags[0]);
i++) {
pid_t pid = syscall(__NR_clone, SIGCHLD | kCloneExtraFlags[i], 0, 0, 0);
const int clone_errno = errno;
if (pid > 0) {
if (!DropRoot()) {
FatalError("Could not drop privileges");
} else {
if (close(sync_fds[0]) || shutdown(sync_fds[1], SHUT_RD))
FatalError("Could not close socketpair");
// The kZygoteIdFd needs to be closed in the parent before
// Zygote gets started.
if (close(kZygoteIdFd))
FatalError("close");
// Tell our child to continue
if (HANDLE_EINTR(send(sync_fds[1], "C", 1, MSG_NOSIGNAL)) != 1)
FatalError("send");
if (close(sync_fds[1]))
FatalError("close");
// We want to keep a full process tree and we don't want our childs to
// be reparented to (the outer PID namespace) init. So we wait for it.
WaitForChildAndExit(pid);
}
// NOTREACHED
FatalError("Not reached");
}
if (pid == 0) {
if (close(sync_fds[1]) || shutdown(sync_fds[0], SHUT_WR))
FatalError("Could not close socketpair");
// Wait for the parent to confirm it closed kZygoteIdFd before we
// continue
char should_continue;
if (HANDLE_EINTR(read(sync_fds[0], &should_continue, 1)) != 1)
FatalError("Read on socketpair");
if (close(sync_fds[0]))
FatalError("close");
if (kCloneExtraFlags[i] & CLONE_NEWPID) {
setenv(kSandboxPIDNSEnvironmentVarName, "", 1 /* overwrite */);
} else {
unsetenv(kSandboxPIDNSEnvironmentVarName);
}
if (kCloneExtraFlags[i] & CLONE_NEWNET) {
setenv(kSandboxNETNSEnvironmentVarName, "", 1 /* overwrite */);
} else {
unsetenv(kSandboxNETNSEnvironmentVarName);
}
break;
}
// If EINVAL then the system doesn't support the requested flags, so
// continue to try a different set.
// On any other errno value the system *does* support these flags but
// something went wrong, hence we bail with an error message rather then
// provide less security.
if (errno != EINVAL) {
fprintf(stderr, "Failed to move to new namespace:");
if (kCloneExtraFlags[i] & CLONE_NEWPID) {
fprintf(stderr, " PID namespaces supported,");
}
if (kCloneExtraFlags[i] & CLONE_NEWNET) {
fprintf(stderr, " Network namespace supported,");
}
fprintf(stderr, " but failed: errno = %s\n", strerror(clone_errno));
return false;
}
}
// If the system doesn't support NEWPID then we carry on anyway.
return true;
}
static bool DropRoot() {
if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0)) {
perror("prctl(PR_SET_DUMPABLE)");
return false;
}
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
perror("Still dumpable after prctl(PR_SET_DUMPABLE)");
return false;
}
gid_t rgid, egid, sgid;
if (getresgid(&rgid, &egid, &sgid)) {
perror("getresgid");
return false;
}
if (setresgid(rgid, rgid, rgid)) {
perror("setresgid");
return false;
}
uid_t ruid, euid, suid;
if (getresuid(&ruid, &euid, &suid)) {
perror("getresuid");
return false;
}
if (setresuid(ruid, ruid, ruid)) {
perror("setresuid");
return false;
}
return true;
}
static bool SetupChildEnvironment() {
unsigned i;
// ld.so may have cleared several environment variables because we are SUID.
// However, the child process might need them so zygote_host_impl_linux.cc
// saves a copy in SANDBOX_$x. This is safe because we have dropped root by
// this point, so we can only exec a binary with the permissions of the user
// who ran us in the first place.
for (i = 0; kSUIDUnsafeEnvironmentVariables[i]; ++i) {
const char* const envvar = kSUIDUnsafeEnvironmentVariables[i];
char* const saved_envvar = SandboxSavedEnvironmentVariable(envvar);
if (!saved_envvar)
return false;
const char* const value = getenv(saved_envvar);
if (value) {
setenv(envvar, value, 1 /* overwrite */);
unsetenv(saved_envvar);
}
free(saved_envvar);
}
return true;
}
bool CheckAndExportApiVersion() {
// Check the environment to see if a specific API version was requested.
// assume version 0 if none.
int api_number = -1;
char* api_string = getenv(kSandboxEnvironmentApiRequest);
if (!api_string) {
api_number = 0;
} else {
errno = 0;
char* endptr = NULL;
long long_api_number = strtol(api_string, &endptr, 10);
if (!endptr || *endptr || errno != 0 || long_api_number < INT_MIN ||
long_api_number > INT_MAX) {
return false;
}
api_number = long_api_number;
}
// Warn only for now.
if (api_number != kSUIDSandboxApiNumber) {
fprintf(
stderr,
"The setuid sandbox provides API version %d, "
"but you need %d\n"
"Please read "
"https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox_development.md."
"\n\n",
kSUIDSandboxApiNumber,
api_number);
}
// Export our version so that the sandboxed process can verify it did not
// use an old sandbox.
char version_string[64];
snprintf(version_string, sizeof(version_string), "%d", kSUIDSandboxApiNumber);
if (setenv(kSandboxEnvironmentApiProvides, version_string, 1)) {
perror("setenv");
return false;
}
return true;
}
int main(int argc, char** argv) {
if (argc <= 1) {
if (argc <= 0) {
return 1;
}
fprintf(stderr, "Usage: %s <renderer process> <args...>\n", argv[0]);
return 1;
}
// Allow someone to query our API version
if (argc == 2 && 0 == strcmp(argv[1], kSuidSandboxGetApiSwitch)) {
printf("%d\n", kSUIDSandboxApiNumber);
return 0;
}
// We cannot adjust /proc/pid/oom_adj for sandboxed renderers
// because those files are owned by root. So we need a helper here.
if (argc == 4 && (0 == strcmp(argv[1], kAdjustOOMScoreSwitch))) {
char* endptr = NULL;
long score;
errno = 0;
unsigned long pid_ul = strtoul(argv[2], &endptr, 10);
if (pid_ul == ULONG_MAX || !endptr || *endptr || errno != 0)
return 1;
pid_t pid = pid_ul;
endptr = NULL;
errno = 0;
score = strtol(argv[3], &endptr, 10);
if (score == LONG_MAX || score == LONG_MIN || !endptr || *endptr ||
errno != 0) {
return 1;
}
return AdjustOOMScore(pid, score);
}
// Protect the core setuid sandbox functionality with an API version
if (!CheckAndExportApiVersion()) {
return 1;
}
if (geteuid() != 0) {
fprintf(stderr,
"The setuid sandbox is not running as root. Common causes:\n"
" * An unprivileged process using ptrace on it, like a debugger.\n"
" * A parent process set prctl(PR_SET_NO_NEW_PRIVS, ...)\n");
}
if (!MoveToNewNamespaces())
return 1;
if (!SpawnChrootHelper())
return 1;
if (!DropRoot())
return 1;
if (!SetupChildEnvironment())
return 1;
execv(argv[1], &argv[1]);
FatalError("execv failed");
}
|