Bug 859782 - Firefox cannot start without /proc (chroot). r=sfink,evilpie,jld
authorRichard Pospesel <richard@torproject.org>
Tue, 19 Jun 2018 08:00:58 -0600
changeset 422976 e8d4a24e47a943db327206a4680fb75c156f9086
parent 422975 45a968a577b741e70c5122e3ee118352a754425f
child 422977 72bd48d00704bf1c1d30cc945898b57d73081511
push id34160
push userdluca@mozilla.com
push dateTue, 19 Jun 2018 21:55:15 +0000
treeherdermozilla-central@e429320fcdd2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfink, evilpie, jld
bugs859782
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 859782 - Firefox cannot start without /proc (chroot). r=sfink,evilpie,jld Firefox uses the current stack frame address and the stack size as a sort of heuristic for various things in the javascript engine. The js::GetNativeStackBaseImpl() function is used to get the base stack address (ie the address from which the stack grows, so this can be either the first or last memory address of the stack memory space depending on the CPU architecture). On Linux, this function is implemented using the pthreads APIs. For non-main threads, the queried thread info is stored in memory. The main thread does not have this information on hand, so it gets the stack memory range via the /proc/self/maps file ( see glibc's pthread_get_attr_np.c ). Fortunately (per discussions with the firefox devs in #jsapi) the base address only needs to be approximation. In reality, environment variables, args, and other things are stored in space between the end/beginning of the mapped stack memory and the 'top' of the stack space used by stack frames. We can get the top of this usable stack from __libc_stack_end, which is a void* set by glibc during program initialization. Non-main threads still get their stack-base through the usual pthreads APIs.
js/src/util/NativeStack.cpp
--- a/js/src/util/NativeStack.cpp
+++ b/js/src/util/NativeStack.cpp
@@ -8,20 +8,34 @@
 
 #ifdef XP_WIN
 # include "util/Windows.h"
 #elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
 # include <pthread.h>
 # if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
 #  include <pthread_np.h>
 # endif
+# if defined(SOLARIS) || defined(AIX)
+#  include <ucontext.h>
+# endif
 # if defined(ANDROID) && !defined(__aarch64__)
 #  include <sys/types.h>
 #  include <unistd.h>
 # endif
+# if defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
+#  include <dlfcn.h>
+#  include <sys/syscall.h>
+#  include <sys/types.h>
+#  include <unistd.h>
+static pid_t
+gettid()
+{
+    return syscall(__NR_gettid);
+}
+# endif
 #else
 # error "Unsupported platform"
 #endif
 
 #include "jsfriendapi.h"
 
 #if defined(XP_WIN)
 
@@ -29,43 +43,85 @@ void*
 js::GetNativeStackBaseImpl()
 {
     PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
     return static_cast<void*>(pTib->StackBase);
 }
 
 #elif defined(SOLARIS)
 
-#include <ucontext.h>
-
 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
 
 void*
 js::GetNativeStackBaseImpl()
 {
     stack_t st;
     stack_getbounds(&st);
     return static_cast<char*>(st.ss_sp) + st.ss_size;
 }
 
 #elif defined(AIX)
 
-#include <ucontext.h>
-
 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
 
 void*
 js::GetNativeStackBaseImpl()
 {
     ucontext_t context;
     getcontext(&context);
     return static_cast<char*>(context.uc_stack.ss_sp) +
         context.uc_stack.ss_size;
 }
 
+#elif defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
+void*
+js::GetNativeStackBaseImpl()
+{
+
+    // On the main thread, get stack base from glibc's __libc_stack_end rather than pthread APIs
+    // to avoid filesystem calls /proc/self/maps.  Non-main threads spawned with pthreads can read
+    // this information directly from their pthread struct, but the main thread must go parse
+    // /proc/self/maps to figure the mapped stack address space ranges.  We want to avoid reading
+    // from /proc/ so that firefox can run in sandboxed environments where /proc may not be mounted
+    if (gettid() == getpid()) {
+        void** pLibcStackEnd = (void**)dlsym(RTLD_DEFAULT, "__libc_stack_end");
+
+        // If __libc_stack_end is not found, architecture specific frame pointer hopping will need
+        // to be implemented.
+        MOZ_RELEASE_ASSERT(pLibcStackEnd, "__libc_stack_end unavailable, unable to setup stack range for JS");
+        void* stackBase = *pLibcStackEnd;
+        MOZ_RELEASE_ASSERT(stackBase, "invalid stack base, unable to setup stack range for JS");
+
+        // We don't need to fix stackBase, as it already roughly points to beginning of the stack
+        return stackBase;
+    }
+
+    // Non-main threads have the required info stored in memory, so no filesystem calls are made.
+    pthread_t thread = pthread_self();
+    pthread_attr_t sattr;
+    pthread_attr_init(&sattr);
+    pthread_getattr_np(thread, &sattr);
+
+    // stackBase will be the *lowest* address on all architectures.
+    void* stackBase = nullptr;
+    size_t stackSize = 0;
+    int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
+    if (rc) {
+        MOZ_CRASH("call to pthread_attr_getstack failed, unable to setup stack range for JS");
+    }
+    MOZ_RELEASE_ASSERT(stackBase, "invalid stack base, unable to setup stack range for JS");
+    pthread_attr_destroy(&sattr);
+
+# if JS_STACK_GROWTH_DIRECTION > 0
+    return stackBase;
+# else
+    return static_cast<char*>(stackBase) + stackSize;
+# endif
+}
+
 #else /* XP_UNIX */
 
 void*
 js::GetNativeStackBaseImpl()
 {
     pthread_t thread = pthread_self();
 # if defined(XP_DARWIN) || defined(DARWIN)
     return pthread_get_stackaddr_np(thread);