Home | History | Annotate | Download | only in seccomp-bpf
      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.
      4 
      5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
      6 #define SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
      7 
      8 #include <stdint.h>
      9 
     10 namespace playground2 {
     11 
     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
     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.
     16 // Passing "nr" as "-1" computes the "magic" return address. Passing any
     17 // other value invokes the appropriate system call.
     18 intptr_t SandboxSyscall(int nr,
     19                         intptr_t p0, intptr_t p1, intptr_t p2,
     20                         intptr_t p3, intptr_t p4, intptr_t p5);
     21 
     22 
     23 // System calls can take up to six parameters. Traditionally, glibc
     24 // implements this property by using variadic argument lists. This works, but
     25 // confuses modern tools such as valgrind, because we are nominally passing
     26 // uninitialized data whenever we call through this function and pass less
     27 // than the full six arguments.
     28 // 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
     30 // it also does the correct type expansion (e.g. from 32bit to 64bit) where
     31 // necessary.
     32 // We have to use C-style cast operators as we want to be able to accept both
     33 // integer and pointer types.
     34 // We explicitly mark all functions as inline. This is not necessary in
     35 // optimized builds, where the compiler automatically figures out that it
     36 // can inline everything. But it makes stack traces of unoptimized builds
     37 // easier to read as it hides implementation details.
     38 #if __cplusplus >= 201103  // C++11
     39 
     40 template<class T0 = intptr_t, class T1 = intptr_t, class T2 = intptr_t,
     41          class T3 = intptr_t, class T4 = intptr_t, class T5 = intptr_t>
     42 inline intptr_t SandboxSyscall(int nr,
     43                                T0 p0 = 0, T1 p1 = 0, T2 p2 = 0,
     44                                T3 p3 = 0, T4 p4 = 0, T5 p5 = 0)
     45   __attribute__((always_inline));
     46 
     47 template<class T0, class T1, class T2, class T3, class T4, class T5>
     48 inline intptr_t SandboxSyscall(int nr,
     49                                T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) {
     50   return SandboxSyscall(nr,
     51                         (intptr_t)p0, (intptr_t)p1, (intptr_t)p2,
     52                         (intptr_t)p3, (intptr_t)p4, (intptr_t)p5);
     53 }
     54 
     55 #else  // Pre-C++11
     56 
     57 // 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
     59 //   compilers as soon as we have fully switched to C++11
     60 
     61 template<class T0, class T1, class T2, class T3, class T4, class T5>
     62 inline intptr_t SandboxSyscall(int nr,
     63                                T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
     64   __attribute__((always_inline));
     65 template<class T0, class T1, class T2, class T3, class T4, class T5>
     66 inline intptr_t SandboxSyscall(int nr,
     67                                T0 p0, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5) {
     68   return SandboxSyscall(nr,
     69                         (intptr_t)p0, (intptr_t)p1, (intptr_t)p2,
     70                         (intptr_t)p3, (intptr_t)p4, (intptr_t)p5);
     71 }
     72 
     73 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)
     75   __attribute__((always_inline));
     76 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) {
     78   return SandboxSyscall(nr, p0, p1, p2, p3, p4, 0);
     79 }
     80 
     81 template<class T0, class T1, class T2, class T3>
     82 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3)
     83   __attribute__((always_inline));
     84 template<class T0, class T1, class T2, class T3>
     85 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2, T3 p3) {
     86   return SandboxSyscall(nr, p0, p1, p2, p3, 0, 0);
     87 }
     88 
     89 template<class T0, class T1, class T2>
     90 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2)
     91   __attribute__((always_inline));
     92 template<class T0, class T1, class T2>
     93 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1, T2 p2) {
     94   return SandboxSyscall(nr, p0, p1, p2, 0, 0, 0);
     95 }
     96 
     97 template<class T0, class T1>
     98 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1)
     99   __attribute__((always_inline));
    100 template<class T0, class T1>
    101 inline intptr_t SandboxSyscall(int nr, T0 p0, T1 p1) {
    102   return SandboxSyscall(nr, p0, p1, 0, 0, 0, 0);
    103 }
    104 
    105 template<class T0>
    106 inline intptr_t SandboxSyscall(int nr, T0 p0)
    107   __attribute__((always_inline));
    108 template<class T0>
    109 inline intptr_t SandboxSyscall(int nr, T0 p0) {
    110   return SandboxSyscall(nr, p0, 0, 0, 0, 0, 0);
    111 }
    112 
    113 inline intptr_t SandboxSyscall(int nr)
    114   __attribute__((always_inline));
    115 inline intptr_t SandboxSyscall(int nr) {
    116   return SandboxSyscall(nr, 0, 0, 0, 0, 0, 0);
    117 }
    118 
    119 #endif  // Pre-C++11
    120 
    121 }  // namespace
    122 
    123 #endif  // SANDBOX_LINUX_SECCOMP_BPF_SYSCALL_H__
    124