diff options
author | jln@chromium.org <jln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-14 19:26:07 +0000 |
---|---|---|
committer | jln@chromium.org <jln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-14 19:26:07 +0000 |
commit | 96f5d2373e93d1ed0e82cebe2cc135fabad14c74 (patch) | |
tree | 451a6fb2fa3229b0efb3a690dae5ccb37296afc8 /sandbox/linux | |
parent | f897276c31306ca333ee173ecaefbbee5d371d4b (diff) | |
download | chromium_src-96f5d2373e93d1ed0e82cebe2cc135fabad14c74.zip chromium_src-96f5d2373e93d1ed0e82cebe2cc135fabad14c74.tar.gz chromium_src-96f5d2373e93d1ed0e82cebe2cc135fabad14c74.tar.bz2 |
Linux sandbox: style cleanup for setuid sandbox.
R=rsesek@chromium.org
Review URL: https://codereview.chromium.org/236083002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263695 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sandbox/linux')
-rw-r--r-- | sandbox/linux/suid/client/setuid_sandbox_client.cc | 3 | ||||
-rw-r--r-- | sandbox/linux/suid/client/setuid_sandbox_client_unittest.cc | 5 | ||||
-rw-r--r-- | sandbox/linux/suid/common/suid_unsafe_environment_variables.h | 2 | ||||
-rw-r--r-- | sandbox/linux/suid/linux_util.c | 10 | ||||
-rw-r--r-- | sandbox/linux/suid/process_util.h | 6 | ||||
-rw-r--r-- | sandbox/linux/suid/process_util_linux.c | 4 | ||||
-rw-r--r-- | sandbox/linux/suid/sandbox.c | 77 |
7 files changed, 52 insertions, 55 deletions
diff --git a/sandbox/linux/suid/client/setuid_sandbox_client.cc b/sandbox/linux/suid/client/setuid_sandbox_client.cc index 224f754..8ed1a97 100644 --- a/sandbox/linux/suid/client/setuid_sandbox_client.cc +++ b/sandbox/linux/suid/client/setuid_sandbox_client.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "sandbox/linux/suid/client/setuid_sandbox_client.h" + #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> @@ -15,7 +17,6 @@ #include "sandbox/linux/services/init_process_reaper.h" #include "sandbox/linux/suid/common/sandbox.h" #include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h" -#include "setuid_sandbox_client.h" namespace { diff --git a/sandbox/linux/suid/client/setuid_sandbox_client_unittest.cc b/sandbox/linux/suid/client/setuid_sandbox_client_unittest.cc index 552dc4b..a2cc7e8 100644 --- a/sandbox/linux/suid/client/setuid_sandbox_client_unittest.cc +++ b/sandbox/linux/suid/client/setuid_sandbox_client_unittest.cc @@ -6,10 +6,9 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/strings/string_number_conversions.h" -#include "testing/gtest/include/gtest/gtest.h" - +#include "sandbox/linux/suid/client/setuid_sandbox_client.h" #include "sandbox/linux/suid/common/sandbox.h" -#include "setuid_sandbox_client.h" +#include "testing/gtest/include/gtest/gtest.h" namespace sandbox { diff --git a/sandbox/linux/suid/common/suid_unsafe_environment_variables.h b/sandbox/linux/suid/common/suid_unsafe_environment_variables.h index 1132a7a7..33ba4b6 100644 --- a/sandbox/linux/suid/common/suid_unsafe_environment_variables.h +++ b/sandbox/linux/suid/common/suid_unsafe_environment_variables.h @@ -54,7 +54,7 @@ static inline char* SandboxSavedEnvironmentVariable(const char* envvar) { const size_t envvar_len = strlen(envvar); const size_t kMaxSizeT = (size_t) -1; - if (envvar_len > kMaxSizeT - 1 -8) + if (envvar_len > kMaxSizeT - 1 - 8) return NULL; const size_t saved_envvarlen = envvar_len + 1 /* NUL terminator */ + diff --git a/sandbox/linux/suid/linux_util.c b/sandbox/linux/suid/linux_util.c index 256468f..320f6ca 100644 --- a/sandbox/linux/suid/linux_util.c +++ b/sandbox/linux/suid/linux_util.c @@ -5,8 +5,9 @@ // The following is duplicated from base/linux_utils.cc. // We shouldn't link against C++ code in a setuid binary. +#include "sandbox/linux/suid/linux_util.h" + #define _GNU_SOURCE // For O_DIRECTORY -#include "linux_util.h" #include <dirent.h> #include <errno.h> @@ -26,7 +27,8 @@ static const char kSocketLinkPrefix[] = "socket:["; // socket. // inode_out: (output) set to the inode number on success // path: e.g. /proc/1234/fd/5 (must be a UNIX domain socket descriptor) -static bool ProcPathGetInodeAt(ino_t* inode_out, int base_dir_fd, +static bool ProcPathGetInodeAt(ino_t* inode_out, + int base_dir_fd, const char* path) { // We also check that the path is relative. if (!inode_out || !path || *path == '/') @@ -40,7 +42,7 @@ static bool ProcPathGetInodeAt(ino_t* inode_out, int base_dir_fd, if (memcmp(kSocketLinkPrefix, buf, sizeof(kSocketLinkPrefix) - 1)) return false; - char *endptr = NULL; + char* endptr = NULL; errno = 0; const unsigned long long int inode_ull = strtoull(buf + sizeof(kSocketLinkPrefix) - 1, &endptr, 10); @@ -72,7 +74,7 @@ bool FindProcessHoldingSocket(pid_t* pid_out, ino_t socket_inode) { const uid_t uid = getuid(); struct dirent* dent; while ((dent = readdir(proc))) { - char *endptr = NULL; + char* endptr = NULL; errno = 0; const unsigned long int pid_ul = strtoul(dent->d_name, &endptr, 10); if (pid_ul == ULONG_MAX || !endptr || *endptr || errno != 0) diff --git a/sandbox/linux/suid/process_util.h b/sandbox/linux/suid/process_util.h index f6b4c31..9fb9a87 100644 --- a/sandbox/linux/suid/process_util.h +++ b/sandbox/linux/suid/process_util.h @@ -11,8 +11,6 @@ #include <stdbool.h> #include <sys/types.h> -#include "base/base_export.h" - // This adjusts /proc/process/oom_score_adj so the Linux OOM killer // will prefer certain process types over others. The range for the // adjustment is [-1000, 1000], with [0, 1000] being user accessible. @@ -21,12 +19,12 @@ // try to set the older oom_adj value instead, scaling the score to // the required range of [0, 15]. This may result in some aliasing of // values, of course. -BASE_EXPORT bool AdjustOOMScore(pid_t process, int score); +bool AdjustOOMScore(pid_t process, int score); // This adjusts /sys/kernel/mm/chromeos-low_mem/margin so that // the kernel notifies us that we are low on memory when less than // |margin_mb| megabytes are available. Setting |margin_mb| to -1 // turns off low memory notification. -BASE_EXPORT bool AdjustLowMemoryMargin(int64_t margin_mb); +bool AdjustLowMemoryMargin(int64_t margin_mb); #endif // SANDBOX_LINUX_SUID_PROCESS_UTIL_H_ diff --git a/sandbox/linux/suid/process_util_linux.c b/sandbox/linux/suid/process_util_linux.c index 78c27ef..d4858c4 100644 --- a/sandbox/linux/suid/process_util_linux.c +++ b/sandbox/linux/suid/process_util_linux.c @@ -5,9 +5,9 @@ // The following is the C version of code from base/process_utils_linux.cc. // We shouldn't link against C++ code in a setuid binary. -#define _GNU_SOURCE // needed for O_DIRECTORY +#include "sandbox/linux/suid/process_util.h" -#include "process_util.h" +#define _GNU_SOURCE // needed for O_DIRECTORY #include <fcntl.h> #include <inttypes.h> diff --git a/sandbox/linux/suid/sandbox.c b/sandbox/linux/suid/sandbox.c index d91c147..7410b71 100644 --- a/sandbox/linux/suid/sandbox.c +++ b/sandbox/linux/suid/sandbox.c @@ -4,7 +4,7 @@ // http://code.google.com/p/chromium/wiki/LinuxSUIDSandbox -#include "common/sandbox.h" +#include "sandbox/linux/suid/common/sandbox.h" #define _GNU_SOURCE #include <asm/unistd.h> @@ -29,9 +29,9 @@ #include <sys/wait.h> #include <unistd.h> -#include "linux_util.h" -#include "process_util.h" -#include "common/suid_unsafe_environment_variables.h" +#include "sandbox/linux/suid/common/suid_unsafe_environment_variables.h" +#include "sandbox/linux/suid/linux_util.h" +#include "sandbox/linux/suid/process_util.h" #if !defined(CLONE_NEWPID) #define CLONE_NEWPID 0x20000000 @@ -44,10 +44,10 @@ static bool DropRoot(); #define HANDLE_EINTR(x) TEMP_FAILURE_RETRY(x) -static void FatalError(const char *msg, ...) +static void FatalError(const char* msg, ...) __attribute__((noreturn, format(printf, 1, 2))); -static void FatalError(const char *msg, ...) { +static void FatalError(const char* msg, ...) { va_list ap; va_start(ap, msg); @@ -85,20 +85,18 @@ static bool SpawnChrootHelper() { return false; } - char *safedir = NULL; + char* safedir = NULL; struct stat sdir_stat; - if (!stat(SAFE_DIR, &sdir_stat) && S_ISDIR(sdir_stat.st_mode)) + 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; - } + } 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); + const pid_t pid = syscall(__NR_clone, CLONE_FS | SIGCHLD, 0, 0, 0); if (pid == -1) { perror("clone"); @@ -214,7 +212,7 @@ static void WaitForChildAndExit(pid_t child_pid) { } int wait_ret = - HANDLE_EINTR(waitid(P_PID, child_pid, &reaped_child_info, WEXITED)); + 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) { @@ -229,10 +227,7 @@ static void WaitForChildAndExit(pid_t child_pid) { static bool MoveToNewNamespaces() { // These are the sets of flags which we'll try, in order. - const int kCloneExtraFlags[] = { - CLONE_NEWPID | CLONE_NEWNET, - CLONE_NEWPID, - }; + 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; @@ -241,8 +236,7 @@ static bool MoveToNewNamespaces() { FatalError("Failed to create a socketpair"); } - for (size_t i = 0; - i < sizeof(kCloneExtraFlags) / sizeof(kCloneExtraFlags[0]); + 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; @@ -386,7 +380,7 @@ bool CheckAndExportApiVersion() { // Check the environment to see if a specific API version was requested. // assume version 0 if none. long api_number = -1; - char *api_string = getenv(kSandboxEnvironmentApiRequest); + char* api_string = getenv(kSandboxEnvironmentApiRequest); if (!api_string) { api_number = 0; } else { @@ -399,20 +393,22 @@ bool CheckAndExportApiVersion() { // Warn only for now. if (api_number != kSUIDSandboxApiNumber) { - fprintf(stderr, "The setuid sandbox provides API version %ld, " - "but you need %ld\n" - "Please read " - "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment." - "\n\n", - kSUIDSandboxApiNumber, - api_number); + fprintf( + stderr, + "The setuid sandbox provides API version %ld, " + "but you need %ld\n" + "Please read " + "https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment." + "\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), "%ld", - kSUIDSandboxApiNumber); + snprintf( + version_string, sizeof(version_string), "%ld", kSUIDSandboxApiNumber); if (setenv(kSandboxEnvironmentApiProvides, version_string, 1)) { perror("setenv"); return false; @@ -421,7 +417,7 @@ bool CheckAndExportApiVersion() { return true; } -int main(int argc, char **argv) { +int main(int argc, char** argv) { if (argc <= 1) { if (argc <= 0) { return 1; @@ -476,9 +472,10 @@ int main(int argc, char **argv) { endptr = NULL; errno = 0; score = strtol(argv[3], &endptr, 10); - if (score == LONG_MAX || score == LONG_MIN || - !endptr || *endptr || errno != 0) + if (score == LONG_MAX || score == LONG_MIN || !endptr || *endptr || + errno != 0) { return 1; + } return AdjustOOMScore(pid, score); } @@ -489,9 +486,9 @@ int main(int argc, char **argv) { 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"); + "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()) |