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> | |
8 #include <sched.h> | |
9 #include <setjmp.h> | |
7 #include <sys/resource.h> | 10 #include <sys/resource.h> |
8 #include <sys/syscall.h> | 11 #include <sys/syscall.h> |
9 #include <sys/time.h> | 12 #include <sys/time.h> |
10 #include <sys/types.h> | 13 #include <sys/types.h> |
11 #include <unistd.h> | 14 #include <unistd.h> |
12 | 15 |
16 #include "base/compiler_specific.h" | |
13 #include "base/logging.h" | 17 #include "base/logging.h" |
14 #include "build/build_config.h" | 18 #include "build/build_config.h" |
15 #include "sandbox/linux/services/linux_syscalls.h" | 19 #include "sandbox/linux/services/linux_syscalls.h" |
16 | 20 |
17 namespace sandbox { | 21 namespace sandbox { |
18 | 22 |
19 pid_t sys_getpid(void) { | 23 pid_t sys_getpid(void) { |
20 return syscall(__NR_getpid); | 24 return syscall(__NR_getpid); |
21 } | 25 } |
22 | 26 |
23 pid_t sys_gettid(void) { | 27 pid_t sys_gettid(void) { |
24 return syscall(__NR_gettid); | 28 return syscall(__NR_gettid); |
25 } | 29 } |
26 | 30 |
27 long sys_clone(unsigned long flags) { | 31 namespace { |
28 return sys_clone(flags, nullptr, nullptr, nullptr, nullptr); | 32 |
33 int CloneHelper(void* arg) { | |
34 jmp_buf* env_ptr = reinterpret_cast<jmp_buf*>(arg); | |
35 // This function runs on the stack specified on the clone call. Use longjmp to | |
36 // switch back to the original stack so the child can return from sys_clone. | |
37 longjmp(*env_ptr, 1); | |
38 | |
39 // Should not be reached. | |
40 RAW_CHECK(false); | |
41 return 1; | |
29 } | 42 } |
30 | 43 |
44 } // namespace | |
45 | |
31 long sys_clone(unsigned long flags, | 46 long sys_clone(unsigned long flags, |
32 void* child_stack, | 47 decltype(nullptr) child_stack, |
33 pid_t* ptid, | 48 pid_t* ptid, |
34 pid_t* ctid, | 49 pid_t* ctid, |
35 decltype(nullptr) tls) { | 50 decltype(nullptr) tls) { |
36 const bool clone_tls_used = flags & CLONE_SETTLS; | 51 const bool clone_tls_used = flags & CLONE_SETTLS; |
37 const bool invalid_ctid = | 52 const bool invalid_ctid = |
38 (flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) && !ctid; | 53 (flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)) && !ctid; |
39 const bool invalid_ptid = (flags & CLONE_PARENT_SETTID) && !ptid; | 54 const bool invalid_ptid = (flags & CLONE_PARENT_SETTID) && !ptid; |
40 const bool invalid_stack = (flags & CLONE_VM) && !child_stack; | |
41 | 55 |
42 if (clone_tls_used || invalid_ctid || invalid_ptid || invalid_stack) { | 56 // Since child_stack must be nullptr, we do not support CLONE_VM. |
57 const bool clone_vm_used = flags & CLONE_VM; | |
58 | |
59 if (clone_tls_used || invalid_ctid || invalid_ptid || clone_vm_used) { | |
43 RAW_LOG(FATAL, "Invalid usage of sys_clone"); | 60 RAW_LOG(FATAL, "Invalid usage of sys_clone"); |
44 } | 61 } |
45 | 62 |
46 // See kernel/fork.c in Linux. There is different ordering of sys_clone | 63 jmp_buf env; |
47 // parameters depending on CONFIG_CLONE_BACKWARDS* configuration options. | 64 if (setjmp(env) == 0) { |
48 #if defined(ARCH_CPU_X86_64) | 65 // We use the libc clone wrapper instead of making the syscall |
49 return syscall(__NR_clone, flags, child_stack, ptid, ctid, tls); | 66 // directly because making the syscall may fail to update the libc's |
50 #elif defined(ARCH_CPU_X86) || defined(ARCH_CPU_ARM_FAMILY) || \ | 67 // internal pid cache. The libc interface unfortunately requires |
51 defined(ARCH_CPU_MIPS_FAMILY) || defined(ARCH_CPU_MIPS64_FAMILY) | 68 // specifying a new stack, so we use setjmp/longjmp to emulate |
52 // CONFIG_CLONE_BACKWARDS defined. | 69 // fork-like behavior. |
53 return syscall(__NR_clone, flags, child_stack, ptid, tls, ctid); | 70 char stack_buf[PTHREAD_STACK_MIN]; |
71 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || \ | |
jln (very slow on Chromium)
2014/12/16 01:37:51
Nit: might be better to rely on what build_config.
| |
72 defined(__mips__) || defined(__aarch64__) | |
73 // The stack grows downward. | |
74 void* stack = stack_buf + sizeof(stack_buf); | |
75 #else | |
76 #error "Unsupported architecture" | |
54 #endif | 77 #endif |
78 return clone(&CloneHelper, stack, flags, &env, ptid, tls, ctid); | |
79 } | |
80 | |
81 return 0; | |
82 } | |
83 | |
84 long sys_clone(unsigned long flags) { | |
85 return sys_clone(flags, nullptr, nullptr, nullptr, nullptr); | |
55 } | 86 } |
56 | 87 |
57 void sys_exit_group(int status) { | 88 void sys_exit_group(int status) { |
58 syscall(__NR_exit_group, status); | 89 syscall(__NR_exit_group, status); |
59 } | 90 } |
60 | 91 |
61 int sys_seccomp(unsigned int operation, | 92 int sys_seccomp(unsigned int operation, |
62 unsigned int flags, | 93 unsigned int flags, |
63 const struct sock_fprog* args) { | 94 const struct sock_fprog* args) { |
64 return syscall(__NR_seccomp, operation, flags, args); | 95 return syscall(__NR_seccomp, operation, flags, args); |
65 } | 96 } |
66 | 97 |
67 int sys_prlimit64(pid_t pid, | 98 int sys_prlimit64(pid_t pid, |
68 int resource, | 99 int resource, |
69 const struct rlimit64* new_limit, | 100 const struct rlimit64* new_limit, |
70 struct rlimit64* old_limit) { | 101 struct rlimit64* old_limit) { |
71 return syscall(__NR_prlimit64, pid, resource, new_limit, old_limit); | 102 return syscall(__NR_prlimit64, pid, resource, new_limit, old_limit); |
72 } | 103 } |
73 | 104 |
74 } // namespace sandbox | 105 } // namespace sandbox |
OLD | NEW |