فهرست منبع

LibELF: Mark libc.so and libpthread.so as syscall regions

Also, before calling the main program entry function, inform the kernel
that no more syscall regions can be registered.

This effectively bans syscalls from everywhere except LibC and
LibPthread. Pretty neat! :^)
Andreas Kling 4 سال پیش
والد
کامیت
df7ddfb803
1فایلهای تغییر یافته به همراه16 افزوده شده و 1 حذف شده
  1. 16 1
      Userland/Libraries/LibELF/DynamicLinker.cpp

+ 16 - 1
Userland/Libraries/LibELF/DynamicLinker.cpp

@@ -31,6 +31,7 @@
 #include <AK/LexicalPath.h>
 #include <AK/LexicalPath.h>
 #include <AK/LogStream.h>
 #include <AK/LogStream.h>
 #include <AK/ScopeGuard.h>
 #include <AK/ScopeGuard.h>
+#include <Kernel/API/Syscall.h>
 #include <LibC/mman.h>
 #include <LibC/mman.h>
 #include <LibC/stdio.h>
 #include <LibC/stdio.h>
 #include <LibC/sys/internals.h>
 #include <LibC/sys/internals.h>
@@ -214,6 +215,14 @@ static NonnullRefPtr<DynamicLoader> commit_elf(const String& name)
 
 
     auto object = loader->load_stage_3(RTLD_GLOBAL | RTLD_LAZY, g_total_tls_size);
     auto object = loader->load_stage_3(RTLD_GLOBAL | RTLD_LAZY, g_total_tls_size);
     ASSERT(object);
     ASSERT(object);
+
+
+    if (name.is_one_of("libc.so", "libpthread.so")) {
+        if (syscall(SC_msyscall, object->base_address().as_ptr())) {
+            ASSERT_NOT_REACHED();
+        }
+    }
+
     if (name == "libc.so") {
     if (name == "libc.so") {
         initialize_libc(*object);
         initialize_libc(*object);
     }
     }
@@ -263,7 +272,13 @@ void ELF::DynamicLinker::linker_main(String&& main_program_name, int main_progra
     if (g_do_breakpoint_trap_before_entry) {
     if (g_do_breakpoint_trap_before_entry) {
         asm("int3");
         asm("int3");
     }
     }
-    int rc = main_function(argc, argv, envp);
+
+    int rc = syscall(SC_msyscall, nullptr);
+    if (rc < 0) {
+        ASSERT_NOT_REACHED();
+    }
+
+    rc = main_function(argc, argv, envp);
     dbgln<DYNAMIC_LOAD_DEBUG>("rc: {}", rc);
     dbgln<DYNAMIC_LOAD_DEBUG>("rc: {}", rc);
     if (g_libc_exit != nullptr) {
     if (g_libc_exit != nullptr) {
         g_libc_exit(rc);
         g_libc_exit(rc);