If dynamic linker fails to load a library object because
for example it is not a valid ELF, it would try to run FINI
functions as it unloads it and crash with a page fault.

This patch tweaks dynamic linker logic to skip running
FINI functions if object has not been fully initialized.

Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com>
---
 core/elf.cc        | 4 ++++
 include/osv/elf.hh | 1 +
 2 files changed, 5 insertions(+)

diff --git a/core/elf.cc b/core/elf.cc
index 349e3515..a61bb035 100644
--- a/core/elf.cc
+++ b/core/elf.cc
@@ -1057,11 +1057,15 @@ void object::run_init_funcs(int argc, char** argv)
             funcs[i](argc, argv);
         }
     }
+    _initialized = true;
 }
 
 // Run the object's static destructors or similar finalization
 void object::run_fini_funcs()
 {
+    if (!_initialized) {
+        return;
+    }
     if (dynamic_exists(DT_FINI_ARRAY)) {
         auto funcs = dynamic_ptr<void (*)()>(DT_FINI_ARRAY);
         auto nr = dynamic_val(DT_FINI_ARRAYSZ) / sizeof(*funcs);
diff --git a/include/osv/elf.hh b/include/osv/elf.hh
index 4466b2ab..7043ec74 100644
--- a/include/osv/elf.hh
+++ b/include/osv/elf.hh
@@ -423,6 +423,7 @@ protected:
     ulong _module_index;
     std::unique_ptr<char[]> _section_names_cache;
     bool _is_executable;
+    bool _initialized = false;
     bool is_core();
 
     std::unordered_map<std::string,void*> _cached_symbols;
-- 
2.20.1

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/20191201194401.5723-1-jwkozaczuk%40gmail.com.

Reply via email to