OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 | 4 |
5 #include "sandbox/linux/services/syscall_wrappers.h" | 5 #include "sandbox/linux/services/syscall_wrappers.h" |
6 | 6 |
7 #include <pthread.h> | 7 #include <pthread.h> |
8 #include <sched.h> | 8 #include <sched.h> |
9 #include <setjmp.h> | 9 #include <setjmp.h> |
10 #include <sys/resource.h> | 10 #include <sys/resource.h> |
11 #include <sys/syscall.h> | 11 #include <sys/syscall.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
| 15 #include <cstring> |
15 | 16 |
16 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
17 #include "base/logging.h" | 18 #include "base/logging.h" |
18 #include "base/third_party/valgrind/valgrind.h" | 19 #include "base/third_party/valgrind/valgrind.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "sandbox/linux/system_headers/capability.h" | 21 #include "sandbox/linux/system_headers/capability.h" |
| 22 #include "sandbox/linux/system_headers/linux_signal.h" |
21 #include "sandbox/linux/system_headers/linux_syscalls.h" | 23 #include "sandbox/linux/system_headers/linux_syscalls.h" |
22 | 24 |
23 namespace sandbox { | 25 namespace sandbox { |
24 | 26 |
25 pid_t sys_getpid(void) { | 27 pid_t sys_getpid(void) { |
26 return syscall(__NR_getpid); | 28 return syscall(__NR_getpid); |
27 } | 29 } |
28 | 30 |
29 pid_t sys_gettid(void) { | 31 pid_t sys_gettid(void) { |
30 return syscall(__NR_gettid); | 32 return syscall(__NR_gettid); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 132 } |
131 | 133 |
132 int sys_chroot(const char* path) { | 134 int sys_chroot(const char* path) { |
133 return syscall(__NR_chroot, path); | 135 return syscall(__NR_chroot, path); |
134 } | 136 } |
135 | 137 |
136 int sys_unshare(int flags) { | 138 int sys_unshare(int flags) { |
137 return syscall(__NR_unshare, flags); | 139 return syscall(__NR_unshare, flags); |
138 } | 140 } |
139 | 141 |
| 142 int sys_sigprocmask(int how, const sigset_t* set, decltype(nullptr) oldset) { |
| 143 // In some toolchain (in particular Android and PNaCl toolchain), |
| 144 // sigset_t is 32 bits, but Linux ABI requires 64 bits. |
| 145 uint64_t linux_value = 0; |
| 146 std::memcpy(&linux_value, set, std::min(sizeof(sigset_t), sizeof(uint64_t))); |
| 147 return syscall(__NR_rt_sigprocmask, how, &linux_value, nullptr, |
| 148 sizeof(linux_value)); |
| 149 } |
| 150 |
| 151 #if defined(MEMORY_SANITIZER) || \ |
| 152 (defined(ARCH_CPU_X86_64) && defined(__GNUC__) && !defined(__clang__)) |
| 153 // If MEMORY_SANITIZER is enabled, it is necessary to call sigaction() here, |
| 154 // rather than the direct syscall (sys_sigaction() defined by ourselves). |
| 155 // It is because, if MEMORY_SANITIZER is enabled, sigaction is wrapped, and |
| 156 // |act->sa_handler| is injected in order to unpoisonize the memory passed via |
| 157 // callback's arguments. Please see msan_interceptors.cc for more details. |
| 158 // So, if the direct syscall is used, as MEMORY_SANITIZER does not know about |
| 159 // it, sigaction() invocation in other places would be broken (in more precise, |
| 160 // returned |oldact| would have a broken |sa_handler| callback). |
| 161 // Practically, it would break NaCl's signal handler installation. |
| 162 // cf) native_client/src/trusted/service_runtime/linux/nacl_signal.c. |
| 163 // |
| 164 // Also on x86_64 architecture, we need naked function for rt_sigreturn. |
| 165 // However, there is no simple way to define it with GCC. Note that the body |
| 166 // of function is actually very small (only two instructions), but we need to |
| 167 // define much debug information in addition, otherwise backtrace() used by |
| 168 // base::StackTrace would not work so that some tests would fail. |
| 169 int sys_sigaction(int signum, |
| 170 const struct sigaction* act, |
| 171 struct sigaction* oldact) { |
| 172 return sigaction(signum, act, oldact); |
| 173 } |
| 174 #else |
| 175 // struct sigaction is different ABI from the Linux's. |
| 176 struct KernelSigAction { |
| 177 void (*kernel_handler)(int); |
| 178 uint32_t sa_flags; |
| 179 void (*sa_restorer)(void); |
| 180 uint64_t sa_mask; |
| 181 }; |
| 182 |
| 183 // On X86_64 arch, it is necessary to set sa_restorer always. |
| 184 #if defined(ARCH_CPU_X86_64) |
| 185 #if !defined(SA_RESTORER) |
| 186 #define SA_RESTORER 0x04000000 |
| 187 #endif |
| 188 |
| 189 // rt_sigreturn is a special system call that interacts with the user land |
| 190 // stack. Thus, here prologue must not be created, which implies syscall() |
| 191 // does not work properly, too. Note that rt_sigreturn will never return. |
| 192 static __attribute__((naked)) void sys_rt_sigreturn() { |
| 193 // Just invoke rt_sigreturn system call. |
| 194 asm volatile ("syscall\n" |
| 195 :: "a"(__NR_rt_sigreturn)); |
| 196 } |
| 197 #endif |
| 198 |
| 199 int sys_sigaction(int signum, |
| 200 const struct sigaction* act, |
| 201 struct sigaction* oldact) { |
| 202 KernelSigAction kernel_act = {}; |
| 203 if (act) { |
| 204 kernel_act.kernel_handler = act->sa_handler; |
| 205 std::memcpy(&kernel_act.sa_mask, &act->sa_mask, |
| 206 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask))); |
| 207 kernel_act.sa_flags = act->sa_flags; |
| 208 |
| 209 #if defined(ARCH_CPU_X86_64) |
| 210 if (!(kernel_act.sa_flags & SA_RESTORER)) { |
| 211 kernel_act.sa_flags |= SA_RESTORER; |
| 212 kernel_act.sa_restorer = sys_rt_sigreturn; |
| 213 } |
| 214 #endif |
| 215 } |
| 216 |
| 217 KernelSigAction kernel_oldact = {}; |
| 218 int result = syscall(__NR_rt_sigaction, signum, act ? &kernel_act : nullptr, |
| 219 oldact ? &kernel_oldact : nullptr, sizeof(uint64_t)); |
| 220 if (result == 0 && oldact) { |
| 221 oldact->sa_handler = kernel_oldact.kernel_handler; |
| 222 sigemptyset(&oldact->sa_mask); |
| 223 std::memcpy(&oldact->sa_mask, &kernel_oldact.sa_mask, |
| 224 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask))); |
| 225 oldact->sa_flags = kernel_oldact.sa_flags; |
| 226 } |
| 227 return result; |
| 228 } |
| 229 |
| 230 #endif // defined(MEMORY_SANITIZER) |
| 231 |
140 } // namespace sandbox | 232 } // namespace sandbox |
OLD | NEW |