| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 namespace playground2 { | 10 namespace playground2 { |
| 11 | 11 |
| 12 // We have to make sure that we have a single "magic" return address for | 12 // We have to make sure that we have a single "magic" return address for |
| 13 // our system calls, which we can check from within a BPF filter. This | 13 // our system calls, which we can check from within a BPF filter. This |
| 14 // works by writing a little bit of asm() code that a) enters the kernel, and | 14 // works by writing a little bit of asm() code that a) enters the kernel, and |
| 15 // that also b) can be invoked in a way that computes this return address. | 15 // that also b) can be invoked in a way that computes this return address. |
| 16 // Passing "nr" as "-1" computes the "magic" return address. Passing any | 16 // Passing "nr" as "-1" computes the "magic" return address. Passing any |
| 17 // other value invokes the appropriate system call. | 17 // other value invokes the appropriate system call. |
| 18 intptr_t SandboxSyscall(int nr, | 18 intptr_t SandboxSyscall(int nr, |
| 19 intptr_t p0, intptr_t p1, intptr_t p2, | 19 intptr_t p0, |
| 20 intptr_t p3, intptr_t p4, intptr_t p5); | 20 intptr_t p1, |
| 21 | 21 intptr_t p2, |
| 22 intptr_t p3, |
| 23 intptr_t p4, |
| 24 intptr_t p5); |
| 22 | 25 |
| 23 // System calls can take up to six parameters. Traditionally, glibc | 26 // System calls can take up to six parameters. Traditionally, glibc |
| 24 // implements this property by using variadic argument lists. This works, but | 27 // implements this property by using variadic argument lists. This works, but |
| 25 // confuses modern tools such as valgrind, because we are nominally passing | 28 // confuses modern tools such as valgrind, because we are nominally passing |
| 26 // uninitialized data whenever we call through this function and pass less | 29 // uninitialized data whenever we call through this function and pass less |
| 27 // than the full six arguments. | 30 // than the full six arguments. |
| 28 // So, instead, we use C++'s template system to achieve a very similar | 31 // So, instead, we use C++'s template system to achieve a very similar |
| 29 // effect. C++ automatically sets the unused parameters to zero for us, and | 32 // effect. C++ automatically sets the unused parameters to zero for us, and |
| 30 // it also does the correct type expansion (e.g. from 32bit to 64bit) where | 33 // it also does the correct type expansion (e.g. from 32bit to 64bit) where |
| 31 // necessary. | 34 // necessary. |
| 32 // We have to use C-style cast operators as we want to be able to accept both | 35 // We have to use C-style cast operators as we want to be able to accept both |
| 33 // integer and pointer types. | 36 // integer and pointer types. |
| 34 // We explicitly mark all functions as inline. This is not necessary in | 37 // We explicitly mark all functions as inline. This is not necessary in |
| 35 // optimized builds, where the compiler automatically figures out that it | 38 // optimized builds, where the compiler automatically figures out that it |
| 36 // can inline everything. But it makes stack traces of unoptimized builds | 39 // can inline everything. But it makes stack traces of unoptimized builds |
| 37 // easier to read as it hides implementation details. | 40 // easier to read as it hides implementation details. |
| 38 #if __cplusplus >= 201103 // C++11 | 41 #if __cplusplus >= 201103 // C++11 |
| 39 | 42 |
| 40 template<class T0 = intptr_t, class T1 = intptr_t, class T2 = intptr_t, | 43 template <class T0 = intptr_t, |
| 41 class T3 = intptr_t, class T4 = intptr_t, class T5 = intptr_t> | 44 class T1 = intptr_t, |
| 45 class T2 = intptr_t, |
| 46 class T3 = intptr_t, |
| 47 class T4 = intptr_t, |
| 48 class T5 = intptr_t> |
| 42 inline intptr_t SandboxSyscall(int nr, | 49 inline intptr_t SandboxSyscall(int nr, |
| 43 T0 p0 = 0, T1 p1 = 0, T2 p2 = 0, | 50 T0 p0 = 0, |
| 44 T3 p3 = 0, T4 p4 = 0, T5 p5 = 0) | 51 T1 p1 = 0, |
| 45 __attribute__((always_inline)); | 52 T2 p2 = 0, |
| 53 T3 p3 = 0, |
| 54 T4 p4 = 0, |
| 55 T5 p5 = 0) __attribute__((always_inline)); |
| 46 | 56 |
| 47 template<class T0, class T1, class T2, class T3, class T4, class T5> | 57 template <class T0, class T1, class T2, class T3, class T4, class T5> |
| 48 inline intptr_t SandboxSyscall(int nr, | 58 inline intptr_t |
| 49 T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { | 59 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { |
| 50 return SandboxSyscall(nr, | 60 return SandboxSyscall(nr, |
| 51 (intptr_t)p0, (intptr_t)p1, (intptr_t)p2, | 61 (intptr_t)p0, |
| 52 (intptr_t)p3, (intptr_t)p4, (intptr_t)p5); | 62 (intptr_t)p1, |
| 63 (intptr_t)p2, |
| 64 (intptr_t)p3, |
| 65 (intptr_t)p4, |
| 66 (intptr_t)p5); |
| 53 } | 67 } |
| 54 | 68 |
| 55 #else // Pre-C++11 | 69 #else // Pre-C++11 |
| 56 | 70 |
| 57 // TODO(markus): C++11 has a much more concise and readable solution for | 71 // TODO(markus): C++11 has a much more concise and readable solution for |
| 58 // expressing what we are doing here. Delete the fall-back code for older | 72 // expressing what we are doing here. Delete the fall-back code for older |
| 59 // compilers as soon as we have fully switched to C++11 | 73 // compilers as soon as we have fully switched to C++11 |
| 60 | 74 |
| 61 template<class T0, class T1, class T2, class T3, class T4, class T5> | 75 template <class T0, class T1, class T2, class T3, class T4, class T5> |
| 62 inline intptr_t SandboxSyscall(int nr, | 76 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) |
| 63 T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) | 77 __attribute__((always_inline)); |
| 64 __attribute__((always_inline)); | 78 template <class T0, class T1, class T2, class T3, class T4, class T5> |
| 65 template<class T0, class T1, class T2, class T3, class T4, class T5> | 79 inline intptr_t |
| 66 inline intptr_t SandboxSyscall(int nr, | 80 SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { |
| 67 T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) { | |
| 68 return SandboxSyscall(nr, | 81 return SandboxSyscall(nr, |
| 69 (intptr_t)p0, (intptr_t)p1, (intptr_t)p2, | 82 (intptr_t)p0, |
| 70 (intptr_t)p3, (intptr_t)p4, (intptr_t)p5); | 83 (intptr_t)p1, |
| 84 (intptr_t)p2, |
| 85 (intptr_t)p3, |
| 86 (intptr_t)p4, |
| 87 (intptr_t)p5); |
| 71 } | 88 } |
| 72 | 89 |
| 73 template<class T0, class T1, class T2, class T3, class T4> | 90 template <class T0, class T1, class T2, class T3, class T4> |
| 74 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4) | 91 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4) |
| 75 __attribute__((always_inline)); | 92 __attribute__((always_inline)); |
| 76 template<class T0, class T1, class T2, class T3, class T4> | 93 template <class T0, class T1, class T2, class T3, class T4> |
| 77 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4) { | 94 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3, T4 p4) { |
| 78 return SandboxSyscall(nr, p0, p1, p2, p3, p4, 0); | 95 return SandboxSyscall(nr, p0, p1, p2, p3, p4, 0); |
| 79 } | 96 } |
| 80 | 97 |
| 81 template<class T0, class T1, class T2, class T3> | 98 template <class T0, class T1, class T2, class T3> |
| 82 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) | 99 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) |
| 83 __attribute__((always_inline)); | 100 __attribute__((always_inline)); |
| 84 template<class T0, class T1, class T2, class T3> | 101 template <class T0, class T1, class T2, class T3> |
| 85 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) { | 102 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) { |
| 86 return SandboxSyscall(nr, p0, p1, p2, p3, 0, 0); | 103 return SandboxSyscall(nr, p0, p1, p2, p3, 0, 0); |
| 87 } | 104 } |
| 88 | 105 |
| 89 template<class T0, class T1, class T2> | 106 template <class T0, class T1, class T2> |
| 90 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) | 107 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) |
| 91 __attribute__((always_inline)); | 108 __attribute__((always_inline)); |
| 92 template<class T0, class T1, class T2> | 109 template <class T0, class T1, class T2> |
| 93 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) { | 110 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) { |
| 94 return SandboxSyscall(nr, p0, p1, p2, 0, 0, 0); | 111 return SandboxSyscall(nr, p0, p1, p2, 0, 0, 0); |
| 95 } | 112 } |
| 96 | 113 |
| 97 template<class T0, class T1> | 114 template <class T0, class T1> |
| 98 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) | 115 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) |
| 99 __attribute__((always_inline)); | 116 __attribute__((always_inline)); |
| 100 template<class T0, class T1> | 117 template <class T0, class T1> |
| 101 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) { | 118 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) { |
| 102 return SandboxSyscall(nr, p0, p1, 0, 0, 0, 0); | 119 return SandboxSyscall(nr, p0, p1, 0, 0, 0, 0); |
| 103 } | 120 } |
| 104 | 121 |
| 105 template<class T0> | 122 template <class T0> |
| 106 inline intptr_t SandboxSyscall(int nr, T0 p0) | 123 inline intptr_t SandboxSyscall(int nr, T0 p0) __attribute__((always_inline)); |
| 107 __attribute__((always_inline)); | 124 template <class T0> |
| 108 template<class T0> | |
| 109 inline intptr_t SandboxSyscall(int nr, T0 p0) { | 125 inline intptr_t SandboxSyscall(int nr, T0 p0) { |
| 110 return SandboxSyscall(nr, p0, 0, 0, 0, 0, 0); | 126 return SandboxSyscall(nr, p0, 0, 0, 0, 0, 0); |
| 111 } | 127 } |
| 112 | 128 |
| 113 inline intptr_t SandboxSyscall(int nr) | 129 inline intptr_t SandboxSyscall(int nr) __attribute__((always_inline)); |
| 114 __attribute__((always_inline)); | |
| 115 inline intptr_t SandboxSyscall(int nr) { | 130 inline intptr_t SandboxSyscall(int nr) { |
| 116 return SandboxSyscall(nr, 0, 0, 0, 0, 0, 0); | 131 return SandboxSyscall(nr, 0, 0, 0, 0, 0, 0); |
| 117 } | 132 } |
| 118 | 133 |
| 119 #endif // Pre-C++11 | 134 #endif // Pre-C++11 |
| 120 | 135 |
| 121 } // namespace | 136 } // namespace |
| 122 | 137 |
| 123 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ | 138 #endif // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__ |
| OLD | NEW |