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> | |
16 | 15 |
17 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
18 #include "base/logging.h" | 17 #include "base/logging.h" |
19 #include "base/third_party/valgrind/valgrind.h" | 18 #include "base/third_party/valgrind/valgrind.h" |
20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
21 #include "sandbox/linux/system_headers/capability.h" | 20 #include "sandbox/linux/system_headers/capability.h" |
22 #include "sandbox/linux/system_headers/linux_signal.h" | |
23 #include "sandbox/linux/system_headers/linux_syscalls.h" | 21 #include "sandbox/linux/system_headers/linux_syscalls.h" |
24 | 22 |
25 namespace sandbox { | 23 namespace sandbox { |
26 | 24 |
27 pid_t sys_getpid(void) { | 25 pid_t sys_getpid(void) { |
28 return syscall(__NR_getpid); | 26 return syscall(__NR_getpid); |
29 } | 27 } |
30 | 28 |
31 pid_t sys_gettid(void) { | 29 pid_t sys_gettid(void) { |
32 return syscall(__NR_gettid); | 30 return syscall(__NR_gettid); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 130 } |
133 | 131 |
134 int sys_chroot(const char* path) { | 132 int sys_chroot(const char* path) { |
135 return syscall(__NR_chroot, path); | 133 return syscall(__NR_chroot, path); |
136 } | 134 } |
137 | 135 |
138 int sys_unshare(int flags) { | 136 int sys_unshare(int flags) { |
139 return syscall(__NR_unshare, flags); | 137 return syscall(__NR_unshare, flags); |
140 } | 138 } |
141 | 139 |
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 // struct sigaction is different ABI from the Linux's. | |
152 struct KernelSigAction { | |
153 void (*kernel_handler)(int); | |
154 uint32_t sa_flags; | |
155 void (*sa_restorer)(void); | |
156 uint64_t sa_mask; | |
157 }; | |
158 | |
159 // On X86_64 arch, it is necessary to set sa_restorer always. | |
160 #if defined(ARCH_CPU_X86_64) | |
161 #if !defined(SA_RESTORER) | |
162 #define SA_RESTORER 0x04000000 | |
163 #endif | |
164 | |
165 static void sys_rt_sigreturn() { | |
166 syscall(__NR_rt_sigreturn); | |
167 } | |
168 #endif | |
169 | |
170 int sys_sigaction(int signum, | |
171 const struct sigaction* act, | |
172 struct sigaction* oldact) { | |
173 KernelSigAction kernel_act = {}; | |
174 if (act) { | |
175 kernel_act.kernel_handler = act->sa_handler; | |
176 std::memcpy(&kernel_act.sa_mask, &act->sa_mask, | |
177 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask))); | |
178 kernel_act.sa_flags = act->sa_flags; | |
179 | |
180 #if defined(ARCH_CPU_X86_64) | |
181 if (!(kernel_act.sa_flags & SA_RESTORER)) { | |
182 kernel_act.sa_flags |= SA_RESTORER; | |
183 kernel_act.sa_restorer = sys_rt_sigreturn; | |
184 } | |
185 #endif | |
186 } | |
187 | |
188 KernelSigAction kernel_oldact = {}; | |
189 int result = syscall(__NR_rt_sigaction, signum, act ? &kernel_act : nullptr, | |
190 oldact ? &kernel_oldact : nullptr, sizeof(uint64_t)); | |
191 if (result == 0 && oldact) { | |
192 oldact->sa_handler = kernel_oldact.kernel_handler; | |
193 sigemptyset(&oldact->sa_mask); | |
194 std::memcpy(&oldact->sa_mask, &kernel_oldact.sa_mask, | |
195 std::min(sizeof(kernel_act.sa_mask), sizeof(act->sa_mask))); | |
196 oldact->sa_flags = kernel_oldact.sa_flags; | |
197 } | |
198 return result; | |
199 } | |
200 | |
201 } // namespace sandbox | 140 } // namespace sandbox |
OLD | NEW |