summaryrefslogtreecommitdiffstats
path: root/sandbox/linux/seccomp/sigprocmask.cc
blob: 9ff2922e86bb21710bd540262ee9ea050895a0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "debug.h"
#include "sandbox_impl.h"

namespace playground {

// If the sandboxed process tries to mask SIGSEGV, there is a good chance
// the process will eventually get terminated. If this is really ever a
// problem, we can hide the fact that SIGSEGV is unmasked. But I don't think
// we really need this. Masking of synchronous signals is rarely necessary.

#if defined(__NR_sigprocmask)
long Sandbox::sandbox_sigprocmask(int how, const void* set, void* old_set) {
  long long tm;
  Debug::syscall(&tm, __NR_sigprocmask, "Executing handler");

  // Access the signal mask by triggering a SEGV and modifying the signal state
  // prior to calling rt_sigreturn().
  long res = -ENOSYS;
  #if defined(__x86_64__)
  #error x86-64 does not support sigprocmask(); use rt_sigprocmask() instead
  #elif defined(__i386__)
  asm volatile(
    "push  %%ebx\n"
    "movl  %2, %%ebx\n"
    "int   $0\n"
    "pop   %%ebx\n"
    : "=a"(res)
    : "0"(__NR_sigprocmask), "ri"((long)how),
      "c"((long)set), "d"((long)old_set)
    : "esp", "memory");
  #else
  #error Unsupported target platform
  #endif

  // Update our shadow signal mask, so that we can copy it upon creation of
  // new threads.
  if (res == 0 && set != NULL) {
    SecureMem::Args* args = getSecureMem();
    switch (how) {
    case SIG_BLOCK:
      *(unsigned long long *)&args->signalMask |=  *(unsigned long long *)set;
      break;
    case SIG_UNBLOCK:
      *(unsigned long long *)&args->signalMask &= ~*(unsigned long long *)set;
      break;
    case SIG_SETMASK:
      *(unsigned long long *)&args->signalMask  =  *(unsigned long long *)set;
      break;
    default:
      break;
    }
  }

  Debug::elapsed(tm, __NR_sigprocmask);

  return res;
}
#endif

#if defined(__NR_rt_sigprocmask)
long Sandbox::sandbox_rt_sigprocmask(int how, const void* set, void* old_set,
                                     size_t bytes) {
  long long tm;
  Debug::syscall(&tm, __NR_rt_sigprocmask, "Executing handler");

  // Access the signal mask by triggering a SEGV and modifying the signal state
  // prior to calling rt_sigreturn().
  long res = -ENOSYS;
  #if defined(__x86_64__)
  asm volatile(
    "movq %5, %%r10\n"
    "int $0\n"
    : "=a"(res)
    : "0"(__NR_rt_sigprocmask), "D"((long)how),
      "S"((long)set), "d"((long)old_set), "r"((long)bytes)
    : "r10", "r11", "rcx", "memory");
  #elif defined(__i386__)
  asm volatile(
    "push  %%ebx\n"
    "movl  %2, %%ebx\n"
    "int   $0\n"
    "pop   %%ebx\n"
    : "=a"(res)
    : "0"(__NR_rt_sigprocmask), "ri"((long)how),
      "c"((long)set), "d"((long)old_set), "S"((long)bytes)
    : "esp", "memory");
  #else
  #error Unsupported target platform
  #endif

  // Update our shadow signal mask, so that we can copy it upon creation of
  // new threads.
  if (res == 0 && set != NULL && bytes >= 8) {
    SecureMem::Args* args = getSecureMem();
    switch (how) {
    case SIG_BLOCK:
      *(unsigned long long *)&args->signalMask |=  *(unsigned long long *)set;
      break;
    case SIG_UNBLOCK:
      *(unsigned long long *)&args->signalMask &= ~*(unsigned long long *)set;
      break;
    case SIG_SETMASK:
      *(unsigned long long *)&args->signalMask  =  *(unsigned long long *)set;
      break;
    default:
      break;
    }
  }

  Debug::elapsed(tm, __NR_rt_sigprocmask);

  return res;
}
#endif

} // namespace