1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
11 #include "base/macros.h"
12 #include "sandbox/sandbox_export.h"
14 // Android's signal.h doesn't define ucontext etc.
15 #if defined(OS_ANDROID)
16 #include "sandbox/linux/services/android_ucontext.h"
21 // This purely static class can be used to perform system calls with some
23 class SANDBOX_EXPORT Syscall
{
25 // InvalidCall() invokes Call() with a platform-appropriate syscall
26 // number that is guaranteed to not be implemented (i.e., normally
28 // This is primarily meant to be useful for writing sandbox policy
30 static intptr_t InvalidCall();
32 // System calls can take up to six parameters (up to eight on some
33 // architectures). Traditionally, glibc
34 // implements this property by using variadic argument lists. This works, but
35 // confuses modern tools such as valgrind, because we are nominally passing
36 // uninitialized data whenever we call through this function and pass less
37 // than the full six arguments.
38 // So, instead, we use C++'s template system to achieve a very similar
39 // effect. C++ automatically sets the unused parameters to zero for us, and
40 // it also does the correct type expansion (e.g. from 32bit to 64bit) where
42 // We have to use C-style cast operators as we want to be able to accept both
43 // integer and pointer types.
52 static inline intptr_t
53 Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
, T5 p5
, T6 p6
, T7 p7
) {
72 static inline intptr_t
73 Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
, T5 p5
, T6 p6
) {
85 template <class T0
, class T1
, class T2
, class T3
, class T4
, class T5
>
86 static inline intptr_t
87 Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
, T5 p5
) {
99 template <class T0
, class T1
, class T2
, class T3
, class T4
>
100 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
, T4 p4
) {
101 return Call(nr
, p0
, p1
, p2
, p3
, p4
, 0, 0, 0);
104 template <class T0
, class T1
, class T2
, class T3
>
105 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
, T3 p3
) {
106 return Call(nr
, p0
, p1
, p2
, p3
, 0, 0, 0, 0);
109 template <class T0
, class T1
, class T2
>
110 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
, T2 p2
) {
111 return Call(nr
, p0
, p1
, p2
, 0, 0, 0, 0, 0);
114 template <class T0
, class T1
>
115 static inline intptr_t Call(int nr
, T0 p0
, T1 p1
) {
116 return Call(nr
, p0
, p1
, 0, 0, 0, 0, 0, 0);
120 static inline intptr_t Call(int nr
, T0 p0
) {
121 return Call(nr
, p0
, 0, 0, 0, 0, 0, 0, 0);
124 static inline intptr_t Call(int nr
) {
125 return Call(nr
, 0, 0, 0, 0, 0, 0, 0, 0);
128 // Set the registers in |ctx| to match what they would be after a system call
129 // returning |ret_val|. |ret_val| must follow the Syscall::Call() convention
130 // of being -errno on errors.
131 static void PutValueInUcontext(intptr_t ret_val
, ucontext_t
* ctx
);
134 // This performs system call |nr| with the arguments p0 to p7 from a constant
135 // userland address, which is for instance observable by seccomp-bpf filters.
136 // The constant userland address from which these system calls are made will
137 // be returned if |nr| is passed as -1.
138 // On error, this function will return a value between -1 and -4095 which
139 // should be interpreted as -errno.
140 static intptr_t Call(int nr
,
150 #if defined(__mips__)
151 // This function basically does on MIPS what SandboxSyscall() is doing on
152 // other architectures. However, because of specificity of MIPS regarding
153 // handling syscall errors, SandboxSyscall() is made as a wrapper for this
154 // function in order for SandboxSyscall() to behave more like on other
155 // architectures on places where return value from SandboxSyscall() is used
156 // directly (like in most tests).
157 // The syscall "nr" is called with arguments that are set in an array on which
158 // pointer "args" points to and an information weather there is an error or no
159 // is returned to SandboxSyscall() by err_stat.
160 static intptr_t SandboxSyscallRaw(int nr
,
161 const intptr_t* args
,
163 #endif // defined(__mips__)
165 DISALLOW_IMPLICIT_CONSTRUCTORS(Syscall
);
168 } // namespace sandbox
170 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__