[tbb-commits] [tor-browser/tor-browser-60.1.0esr-8.0-1] Bug 859782 - Firefox cannot start without /proc (chroot). r=sfink, evilpie, jld

gk at torproject.org gk at torproject.org
Wed Jul 4 17:01:32 UTC 2018


commit dbca4ee64600d9ab17e7c9abdc7dac76562def01
Author: Richard Pospesel <richard at torproject.org>
Date:   Tue Jun 19 08:00:58 2018 -0600

    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.
    
    This fixes bug 20283 on our side.
---
 js/src/util/NativeStack.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 60 insertions(+), 4 deletions(-)

diff --git a/js/src/util/NativeStack.cpp b/js/src/util/NativeStack.cpp
index 2507374a36da..300b5259b5e5 100644
--- a/js/src/util/NativeStack.cpp
+++ b/js/src/util/NativeStack.cpp
@@ -13,10 +13,24 @@
 # 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
@@ -34,8 +48,6 @@ js::GetNativeStackBaseImpl()
 
 #elif defined(SOLARIS)
 
-#include <ucontext.h>
-
 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
 
 void*
@@ -48,8 +60,6 @@ js::GetNativeStackBaseImpl()
 
 #elif defined(AIX)
 
-#include <ucontext.h>
-
 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
 
 void*
@@ -61,6 +71,52 @@ js::GetNativeStackBaseImpl()
         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*



More information about the tbb-commits mailing list