SandboxHooks.cpp (3317B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/Assertions.h" 8 #include "mozilla/Atomics.h" 9 #include "mozilla/Types.h" 10 11 #include <dlfcn.h> 12 #include <signal.h> 13 #include <errno.h> 14 #include <stdio.h> 15 #include <sys/inotify.h> 16 #ifdef MOZ_X11 17 # include <X11/Xlib.h> 18 #endif 19 20 // Signal number used to enable seccomp on each thread. 21 extern mozilla::Atomic<int> gSeccompTsyncBroadcastSignum; 22 23 static bool SigSetNeedsFixup(const sigset_t* aSet) { 24 int tsyncSignum = gSeccompTsyncBroadcastSignum; 25 26 return aSet != nullptr && 27 (sigismember(aSet, SIGSYS) || 28 (tsyncSignum != 0 && sigismember(aSet, tsyncSignum))); 29 } 30 31 static void SigSetFixup(sigset_t* aSet) { 32 int tsyncSignum = gSeccompTsyncBroadcastSignum; 33 int rv = sigdelset(aSet, SIGSYS); 34 MOZ_RELEASE_ASSERT(rv == 0); 35 if (tsyncSignum != 0) { 36 rv = sigdelset(aSet, tsyncSignum); 37 MOZ_RELEASE_ASSERT(rv == 0); 38 } 39 } 40 41 // This file defines a hook for sigprocmask() and pthread_sigmask(). 42 // Bug 1176099: some threads block SIGSYS signal which breaks our seccomp-bpf 43 // sandbox. To avoid this, we intercept the call and remove SIGSYS. 44 // 45 // ENOSYS indicates an error within the hook function itself. 46 static int HandleSigset(int (*aRealFunc)(int, const sigset_t*, sigset_t*), 47 int aHow, const sigset_t* aSet, sigset_t* aOldSet, 48 bool aUseErrno) { 49 if (!aRealFunc) { 50 if (aUseErrno) { 51 errno = ENOSYS; 52 return -1; 53 } 54 55 return ENOSYS; 56 } 57 58 // Avoid unnecessary work 59 if (aHow == SIG_UNBLOCK || !SigSetNeedsFixup(aSet)) { 60 return aRealFunc(aHow, aSet, aOldSet); 61 } 62 63 sigset_t newSet = *aSet; 64 SigSetFixup(&newSet); 65 return aRealFunc(aHow, &newSet, aOldSet); 66 } 67 68 extern "C" MOZ_EXPORT int sigprocmask(int how, const sigset_t* set, 69 sigset_t* oldset) { 70 static auto sRealFunc = 71 (int (*)(int, const sigset_t*, sigset_t*))dlsym(RTLD_NEXT, "sigprocmask"); 72 73 return HandleSigset(sRealFunc, how, set, oldset, true); 74 } 75 76 extern "C" MOZ_EXPORT int pthread_sigmask(int how, const sigset_t* set, 77 sigset_t* oldset) { 78 static auto sRealFunc = (int (*)(int, const sigset_t*, sigset_t*))dlsym( 79 RTLD_NEXT, "pthread_sigmask"); 80 81 return HandleSigset(sRealFunc, how, set, oldset, false); 82 } 83 84 extern "C" MOZ_EXPORT int sigaction(int signum, const struct sigaction* act, 85 struct sigaction* oldact) { 86 static auto sRealFunc = 87 (int (*)(int, const struct sigaction*, struct sigaction*))dlsym( 88 RTLD_NEXT, "sigaction"); 89 90 if (!sRealFunc) { 91 errno = ENOSYS; 92 return -1; 93 } 94 95 if (act == nullptr || !SigSetNeedsFixup(&act->sa_mask)) { 96 return sRealFunc(signum, act, oldact); 97 } 98 99 struct sigaction newact = *act; 100 SigSetFixup(&newact.sa_mask); 101 return sRealFunc(signum, &newact, oldact); 102 } 103 104 extern "C" MOZ_EXPORT int inotify_init(void) { return inotify_init1(0); } 105 106 extern "C" MOZ_EXPORT int inotify_init1(int flags) { 107 errno = ENOSYS; 108 return -1; 109 }