浏览代码

LibC+DynamicLoader: Store the auxiliary vector address at startup

Previously, getauxval() got the address of the auxiliary vector by
traversing to the end of the `environ` pointer.

The assumption that the auxiliary vector comes after the environment
array is true at program startup, however the environment array may
be re-allocated and change its address during runtime which would cause
getauxval() to work with an incorrect auxiliary vector address.

To fix this, we now get the address of the auxiliary vector once in
__libc_init and store it in a libc-internal pointer which is then used
by getauxval().

Fixes #10087.
Itamar 3 年之前
父节点
当前提交
a3360bcee8
共有 3 个文件被更改,包括 15 次插入4 次删除
  1. 13 0
      Userland/Libraries/LibC/libcinit.cpp
  2. 1 4
      Userland/Libraries/LibC/stdlib.cpp
  3. 1 0
      Userland/Libraries/LibC/sys/internals.h

+ 13 - 0
Userland/Libraries/LibC/libcinit.cpp

@@ -19,10 +19,23 @@ __thread int errno;
 char** environ;
 char** environ;
 bool __environ_is_malloced;
 bool __environ_is_malloced;
 bool __stdio_is_initialized;
 bool __stdio_is_initialized;
+void* __auxiliary_vector;
+
+static void __auxiliary_vector_init();
 
 
 void __libc_init()
 void __libc_init()
 {
 {
+    __auxiliary_vector_init();
     __malloc_init();
     __malloc_init();
     __stdio_init();
     __stdio_init();
 }
 }
+
+static void __auxiliary_vector_init()
+{
+    char** env;
+    for (env = environ; *env; ++env) {
+    }
+
+    __auxiliary_vector = (void*)++env;
+}
 }
 }

+ 1 - 4
Userland/Libraries/LibC/stdlib.cpp

@@ -182,11 +182,8 @@ extern "C" {
 long getauxval(long type)
 long getauxval(long type)
 {
 {
     errno = 0;
     errno = 0;
-    char** env;
-    for (env = environ; *env; ++env) {
-    }
 
 
-    auxv_t* auxvp = (auxv_t*)++env;
+    auxv_t* auxvp = (auxv_t*)__auxiliary_vector;
     for (; auxvp->a_type != AT_NULL; ++auxvp) {
     for (; auxvp->a_type != AT_NULL; ++auxvp) {
         if (auxvp->a_type == type)
         if (auxvp->a_type == type)
             return auxvp->a_un.a_val;
             return auxvp->a_un.a_val;

+ 1 - 0
Userland/Libraries/LibC/sys/internals.h

@@ -19,6 +19,7 @@ extern void _init();
 extern bool __environ_is_malloced;
 extern bool __environ_is_malloced;
 extern bool __stdio_is_initialized;
 extern bool __stdio_is_initialized;
 extern bool __heap_is_stable;
 extern bool __heap_is_stable;
+extern void* __auxiliary_vector;
 
 
 int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle);
 int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle);
 void __cxa_finalize(void* dso_handle);
 void __cxa_finalize(void* dso_handle);