DynamicLinker.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2021, the SerenityOS developers.
  5. * Copyright (c) 2022, Jesse Buhagiar <jooster669@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Debug.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/HashTable.h>
  13. #include <AK/LexicalPath.h>
  14. #include <AK/Platform.h>
  15. #include <AK/Random.h>
  16. #include <AK/ScopeGuard.h>
  17. #include <AK/Vector.h>
  18. #include <Kernel/API/VirtualMemoryAnnotations.h>
  19. #include <Kernel/API/prctl_numbers.h>
  20. #include <LibELF/AuxiliaryVector.h>
  21. #include <LibELF/DynamicLinker.h>
  22. #include <LibELF/DynamicLoader.h>
  23. #include <LibELF/DynamicObject.h>
  24. #include <LibELF/Hashes.h>
  25. #include <bits/dlfcn_integration.h>
  26. #include <bits/pthread_integration.h>
  27. #include <dlfcn.h>
  28. #include <fcntl.h>
  29. #include <link.h>
  30. #include <pthread.h>
  31. #include <string.h>
  32. #include <sys/mman.h>
  33. #include <sys/types.h>
  34. #include <syscall.h>
  35. #include <unistd.h>
  36. namespace ELF {
  37. static HashMap<ByteString, NonnullRefPtr<ELF::DynamicLoader>> s_loaders;
  38. static ByteString s_main_program_path;
  39. // Dependencies have to always be added after the object that depends on them in `s_global_objects`.
  40. // This is needed for calling the destructors in the correct order.
  41. static OrderedHashMap<ByteString, NonnullRefPtr<ELF::DynamicObject>> s_global_objects;
  42. using EntryPointFunction = int (*)(int, char**, char**);
  43. using LibCExitFunction = void (*)(int);
  44. using DlIteratePhdrCallbackFunction = int (*)(struct dl_phdr_info*, size_t, void*);
  45. using DlIteratePhdrFunction = int (*)(DlIteratePhdrCallbackFunction, void*);
  46. using CallFiniFunctionsFunction = void (*)();
  47. extern "C" [[noreturn]] void _invoke_entry(int argc, char** argv, char** envp, EntryPointFunction entry);
  48. struct TLSData {
  49. size_t total_tls_size { 0 };
  50. size_t tls_template_size { 0 };
  51. };
  52. static TLSData s_tls_data;
  53. static char** s_envp = nullptr;
  54. static __pthread_mutex_t s_loader_lock = __PTHREAD_MUTEX_INITIALIZER;
  55. static ByteString s_cwd;
  56. static bool s_allowed_to_check_environment_variables { false };
  57. static bool s_do_breakpoint_trap_before_entry { false };
  58. static StringView s_ld_library_path;
  59. static StringView s_main_program_pledge_promises;
  60. static ByteString s_loader_pledge_promises;
  61. class MagicWeakSymbol : public RefCounted<MagicWeakSymbol> {
  62. AK_MAKE_NONCOPYABLE(MagicWeakSymbol);
  63. AK_MAKE_NONMOVABLE(MagicWeakSymbol);
  64. public:
  65. template<typename T>
  66. MagicWeakSymbol(unsigned int type, T value)
  67. {
  68. m_storage = reinterpret_cast<uintptr_t>(value);
  69. m_lookup_result.size = 8;
  70. m_lookup_result.type = type;
  71. m_lookup_result.address = VirtualAddress { &m_storage };
  72. m_lookup_result.bind = STB_GLOBAL;
  73. }
  74. auto lookup_result() const
  75. {
  76. return m_lookup_result;
  77. }
  78. private:
  79. DynamicObject::SymbolLookupResult m_lookup_result;
  80. uintptr_t m_storage;
  81. };
  82. static HashMap<StringView, NonnullRefPtr<MagicWeakSymbol>> s_magic_weak_symbols;
  83. Optional<DynamicObject::SymbolLookupResult> DynamicLinker::lookup_global_symbol(StringView name)
  84. {
  85. Optional<DynamicObject::SymbolLookupResult> weak_result;
  86. auto symbol = DynamicObject::HashSymbol { name };
  87. for (auto& lib : s_global_objects) {
  88. auto res = lib.value->lookup_symbol(symbol);
  89. if (!res.has_value())
  90. continue;
  91. if (res.value().bind == STB_GLOBAL)
  92. return res;
  93. if (res.value().bind == STB_WEAK && !weak_result.has_value())
  94. weak_result = res;
  95. // We don't want to allow local symbols to be pulled in to other modules
  96. }
  97. if (auto magic_lookup = s_magic_weak_symbols.get(name); magic_lookup.has_value())
  98. weak_result = (*magic_lookup)->lookup_result();
  99. return weak_result;
  100. }
  101. static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(ByteString const& filepath, int fd)
  102. {
  103. VERIFY(filepath.starts_with('/'));
  104. auto loader = TRY(ELF::DynamicLoader::try_create(fd, filepath));
  105. s_loaders.set(filepath, *loader);
  106. static size_t s_current_tls_offset = 0;
  107. s_current_tls_offset -= loader->tls_size_of_current_object();
  108. if (loader->tls_alignment_of_current_object())
  109. s_current_tls_offset = align_down_to(s_current_tls_offset, loader->tls_alignment_of_current_object());
  110. loader->set_tls_offset(s_current_tls_offset);
  111. // This actually maps the library at the intended and final place.
  112. auto main_library_object = loader->map();
  113. s_global_objects.set(filepath, *main_library_object);
  114. return loader;
  115. }
  116. Optional<ByteString> DynamicLinker::resolve_library(ByteString const& name, DynamicObject const& parent_object)
  117. {
  118. // Absolute and relative (to the current working directory) paths are already considered resolved.
  119. // However, ensure that the returned path is absolute and canonical, so pass it through LexicalPath.
  120. if (name.contains('/'))
  121. return LexicalPath::absolute_path(s_cwd, name);
  122. Vector<StringView> search_paths;
  123. // Search RPATH values indicated by the ELF (only if RUNPATH is not present).
  124. if (parent_object.runpath().is_empty())
  125. search_paths.extend(parent_object.rpath().split_view(':'));
  126. // Scan the LD_LIBRARY_PATH environment variable if applicable.
  127. search_paths.extend(s_ld_library_path.split_view(':'));
  128. // Search RUNPATH values indicated by the ELF.
  129. search_paths.extend(parent_object.runpath().split_view(':'));
  130. // Last are the default search paths.
  131. search_paths.append("/usr/lib"sv);
  132. search_paths.append("/usr/local/lib"sv);
  133. for (auto const& search_path : search_paths) {
  134. LexicalPath library_path(search_path.replace("$ORIGIN"sv, LexicalPath::dirname(parent_object.filepath()), ReplaceMode::FirstOnly));
  135. ByteString library_name = library_path.append(name).string();
  136. if (access(library_name.characters(), F_OK) == 0) {
  137. if (!library_name.starts_with('/')) {
  138. // FIXME: Non-absolute paths should resolve from the current working directory. However,
  139. // since that's almost never the effect that is actually desired, let's print
  140. // a warning and only implement it once something actually needs that behavior.
  141. dbgln("\033[33mWarning:\033[0m Resolving library '{}' resulted in non-absolute path '{}'. Check your binary for relative RPATHs and RUNPATHs.", name, library_name);
  142. }
  143. return library_name;
  144. }
  145. }
  146. return {};
  147. }
  148. static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(ByteString const& path)
  149. {
  150. VERIFY(path.starts_with('/'));
  151. int fd = open(path.characters(), O_RDONLY);
  152. if (fd < 0)
  153. return DlErrorMessage { ByteString::formatted("Could not open shared library '{}': {}", path, strerror(errno)) };
  154. return map_library(path, fd);
  155. }
  156. static Vector<ByteString> get_dependencies(ByteString const& path)
  157. {
  158. VERIFY(path.starts_with('/'));
  159. auto name = LexicalPath::basename(path);
  160. auto lib = s_loaders.get(path).value();
  161. Vector<ByteString> dependencies;
  162. lib->for_each_needed_library([&dependencies, &name](auto needed_name) {
  163. if (name == needed_name)
  164. return;
  165. dependencies.append(needed_name);
  166. });
  167. return dependencies;
  168. }
  169. static Result<void, DlErrorMessage> map_dependencies(ByteString const& path)
  170. {
  171. VERIFY(path.starts_with('/'));
  172. dbgln_if(DYNAMIC_LOAD_DEBUG, "mapping dependencies for: {}", path);
  173. auto const& parent_object = (*s_loaders.get(path))->dynamic_object();
  174. for (auto const& needed_name : get_dependencies(path)) {
  175. dbgln_if(DYNAMIC_LOAD_DEBUG, "needed library: {}", needed_name.characters());
  176. auto dependency_path = DynamicLinker::resolve_library(needed_name, parent_object);
  177. if (!dependency_path.has_value())
  178. return DlErrorMessage { ByteString::formatted("Could not find required shared library: {}", needed_name) };
  179. if (!s_loaders.contains(dependency_path.value()) && !s_global_objects.contains(dependency_path.value())) {
  180. auto loader = TRY(map_library(dependency_path.value()));
  181. TRY(map_dependencies(loader->filepath()));
  182. }
  183. }
  184. dbgln_if(DYNAMIC_LOAD_DEBUG, "mapped dependencies for {}", path);
  185. return {};
  186. }
  187. static void allocate_tls()
  188. {
  189. for (auto const& data : s_loaders) {
  190. dbgln_if(DYNAMIC_LOAD_DEBUG, "{}: TLS Size: {}, TLS Alignment: {}", data.key, data.value->tls_size_of_current_object(), data.value->tls_alignment_of_current_object());
  191. s_tls_data.total_tls_size += data.value->tls_size_of_current_object() + data.value->tls_alignment_of_current_object();
  192. }
  193. if (s_tls_data.total_tls_size == 0)
  194. return;
  195. auto page_aligned_size = align_up_to(s_tls_data.total_tls_size, PAGE_SIZE);
  196. auto initial_tls_data_result = ByteBuffer::create_zeroed(page_aligned_size);
  197. if (initial_tls_data_result.is_error()) {
  198. dbgln("Failed to allocate initial TLS data");
  199. VERIFY_NOT_REACHED();
  200. }
  201. auto& initial_tls_data = initial_tls_data_result.value();
  202. // Initialize TLS data
  203. for (auto const& entry : s_loaders) {
  204. entry.value->copy_initial_tls_data_into(initial_tls_data);
  205. }
  206. void* master_tls = ::allocate_tls((char*)initial_tls_data.data(), initial_tls_data.size());
  207. VERIFY(master_tls != (void*)-1);
  208. dbgln_if(DYNAMIC_LOAD_DEBUG, "from userspace, master_tls: {:p}", master_tls);
  209. s_tls_data.tls_template_size = initial_tls_data.size();
  210. }
  211. static int __dl_iterate_phdr(DlIteratePhdrCallbackFunction callback, void* data)
  212. {
  213. pthread_mutex_lock(&s_loader_lock);
  214. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  215. for (auto& it : s_global_objects) {
  216. auto& object = it.value;
  217. auto info = dl_phdr_info {
  218. .dlpi_addr = (Elf_Addr)object->base_address().as_ptr(),
  219. .dlpi_name = object->filepath().characters(),
  220. .dlpi_phdr = object->program_headers(),
  221. .dlpi_phnum = object->program_header_count()
  222. };
  223. auto res = callback(&info, sizeof(info), data);
  224. if (res != 0)
  225. return res;
  226. }
  227. return 0;
  228. }
  229. static void initialize_libc(DynamicObject& libc)
  230. {
  231. auto res = libc.lookup_symbol("__libc_init"sv);
  232. VERIFY(res.has_value());
  233. typedef void libc_init_func();
  234. ((libc_init_func*)res.value().address.as_ptr())();
  235. }
  236. template<typename Callback>
  237. static void for_each_unfinished_dependency_of(ByteString const& path, HashTable<ByteString>& seen_names, Callback callback)
  238. {
  239. VERIFY(path.starts_with('/'));
  240. auto loader = s_loaders.get(path);
  241. if (!loader.has_value()) {
  242. // Not having a loader here means that the library has already been loaded in at an earlier point,
  243. // and the loader itself was cleared during the end of `linker_main`.
  244. return;
  245. }
  246. if (loader.value()->is_fully_relocated()) {
  247. if (!loader.value()->is_fully_initialized()) {
  248. // If we are ending up here, that possibly means that this library either dlopens itself or a library that depends
  249. // on it while running its initializers. Assuming that this is the only funny thing that the library does, there is
  250. // a reasonable chance that nothing breaks, so just warn and continue.
  251. dbgln("\033[33mWarning:\033[0m Querying for dependencies of '{}' while running its initializers", path);
  252. }
  253. return;
  254. }
  255. if (seen_names.contains(path))
  256. return;
  257. seen_names.set(path);
  258. for (auto const& needed_name : get_dependencies(path)) {
  259. auto dependency_path = *DynamicLinker::resolve_library(needed_name, loader.value()->dynamic_object());
  260. for_each_unfinished_dependency_of(dependency_path, seen_names, callback);
  261. }
  262. callback(*s_loaders.get(path).value());
  263. }
  264. static Vector<NonnullRefPtr<DynamicLoader>> collect_loaders_for_library(ByteString const& path)
  265. {
  266. VERIFY(path.starts_with('/'));
  267. HashTable<ByteString> seen_names;
  268. Vector<NonnullRefPtr<DynamicLoader>> loaders;
  269. for_each_unfinished_dependency_of(path, seen_names, [&](auto& loader) {
  270. loaders.append(loader);
  271. });
  272. return loaders;
  273. }
  274. static void drop_loader_promise(StringView promise_to_drop)
  275. {
  276. if (s_main_program_pledge_promises.is_empty() || s_loader_pledge_promises.is_empty())
  277. return;
  278. s_loader_pledge_promises = s_loader_pledge_promises.replace(promise_to_drop, ""sv, ReplaceMode::All);
  279. auto extended_promises = ByteString::formatted("{} {}", s_main_program_pledge_promises, s_loader_pledge_promises);
  280. Syscall::SC_pledge_params params {
  281. { extended_promises.characters(), extended_promises.length() },
  282. { nullptr, 0 },
  283. };
  284. int rc = syscall(SC_pledge, &params);
  285. if (rc < 0 && rc > -EMAXERRNO) {
  286. warnln("Failed to drop loader pledge promise: {}. errno={}", promise_to_drop, errno);
  287. _exit(1);
  288. }
  289. }
  290. static Result<void, DlErrorMessage> link_main_library(ByteString const& path, int flags)
  291. {
  292. VERIFY(path.starts_with('/'));
  293. auto loaders = collect_loaders_for_library(path);
  294. // Verify that all objects are already mapped
  295. for (auto& loader : loaders)
  296. VERIFY(!loader->map());
  297. for (auto& loader : loaders) {
  298. bool success = loader->link(flags);
  299. if (!success) {
  300. return DlErrorMessage { ByteString::formatted("Failed to link library {}", loader->filepath()) };
  301. }
  302. }
  303. for (auto& loader : loaders) {
  304. auto result = loader->load_stage_3(flags);
  305. VERIFY(!result.is_error());
  306. auto& object = result.value();
  307. if (loader->filepath().ends_with("/libc.so"sv)) {
  308. initialize_libc(*object);
  309. }
  310. if (loader->filepath().ends_with("/libsystem.so"sv)) {
  311. VERIFY(!loader->text_segments().is_empty());
  312. for (auto const& segment : loader->text_segments()) {
  313. auto flags = static_cast<int>(VirtualMemoryRangeFlags::SyscallCode) | static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  314. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  315. VERIFY_NOT_REACHED();
  316. }
  317. }
  318. } else {
  319. for (auto const& segment : loader->text_segments()) {
  320. auto flags = static_cast<int>(VirtualMemoryRangeFlags::Immutable);
  321. if (syscall(SC_annotate_mapping, segment.address().get(), flags)) {
  322. VERIFY_NOT_REACHED();
  323. }
  324. }
  325. }
  326. }
  327. drop_loader_promise("prot_exec"sv);
  328. for (auto& loader : loaders) {
  329. loader->load_stage_4();
  330. }
  331. return {};
  332. }
  333. static Result<void, DlErrorMessage> __dlclose(void* handle)
  334. {
  335. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlclose: {}", handle);
  336. pthread_mutex_lock(&s_loader_lock);
  337. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  338. // FIXME: this will not currently destroy the dynamic object
  339. // because we're intentionally holding a strong reference to it
  340. // via s_global_objects until there's proper unload support.
  341. auto object = static_cast<ELF::DynamicObject*>(handle);
  342. object->unref();
  343. return {};
  344. }
  345. static Optional<DlErrorMessage> verify_tls_for_dlopen(DynamicLoader const& loader)
  346. {
  347. if (loader.tls_size_of_current_object() == 0)
  348. return {};
  349. if (s_tls_data.total_tls_size + loader.tls_size_of_current_object() + loader.tls_alignment_of_current_object() > s_tls_data.tls_template_size)
  350. return DlErrorMessage("TLS size too large");
  351. bool tls_data_is_all_zero = true;
  352. loader.image().for_each_program_header([&loader, &tls_data_is_all_zero](ELF::Image::ProgramHeader program_header) {
  353. if (program_header.type() != PT_TLS)
  354. return IterationDecision::Continue;
  355. auto* tls_data = (u8 const*)loader.image().base_address() + program_header.offset();
  356. for (size_t i = 0; i < program_header.size_in_image(); ++i) {
  357. if (tls_data[i] != 0) {
  358. tls_data_is_all_zero = false;
  359. break;
  360. }
  361. }
  362. return IterationDecision::Break;
  363. });
  364. if (tls_data_is_all_zero)
  365. return {};
  366. return DlErrorMessage("Using dlopen() with libraries that have non-zeroed TLS is currently not supported");
  367. }
  368. static Result<void*, DlErrorMessage> __dlopen(char const* filename, int flags)
  369. {
  370. // FIXME: RTLD_NOW and RTLD_LOCAL are not supported
  371. flags &= ~RTLD_NOW;
  372. flags |= RTLD_LAZY;
  373. flags &= ~RTLD_LOCAL;
  374. flags |= RTLD_GLOBAL;
  375. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlopen invoked, filename={}, flags={}", filename, flags);
  376. if (pthread_mutex_trylock(&s_loader_lock) != 0)
  377. return DlErrorMessage { "Nested calls to dlopen() are not permitted." };
  378. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  379. auto const& parent_object = **s_global_objects.get(s_main_program_path);
  380. auto library_path = (filename ? DynamicLinker::resolve_library(filename, parent_object) : s_main_program_path);
  381. if (!library_path.has_value())
  382. return DlErrorMessage { ByteString::formatted("Could not find required shared library: {}", filename) };
  383. auto existing_elf_object = s_global_objects.get(library_path.value());
  384. if (existing_elf_object.has_value()) {
  385. // It's up to the caller to release the ref with dlclose().
  386. existing_elf_object.value()->ref();
  387. return *existing_elf_object;
  388. }
  389. auto loader = TRY(map_library(library_path.value()));
  390. if (auto error = verify_tls_for_dlopen(loader); error.has_value())
  391. return error.value();
  392. TRY(map_dependencies(loader->filepath()));
  393. TRY(link_main_library(loader->filepath(), flags));
  394. s_tls_data.total_tls_size += loader->tls_size_of_current_object() + loader->tls_alignment_of_current_object();
  395. auto object = s_global_objects.get(library_path.value());
  396. if (!object.has_value())
  397. return DlErrorMessage { "Could not load ELF object." };
  398. // It's up to the caller to release the ref with dlclose().
  399. object.value()->ref();
  400. return *object;
  401. }
  402. static Result<void*, DlErrorMessage> __dlsym(void* handle, char const* symbol_name)
  403. {
  404. dbgln_if(DYNAMIC_LOAD_DEBUG, "__dlsym: {}, {}", handle, symbol_name);
  405. pthread_mutex_lock(&s_loader_lock);
  406. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  407. StringView symbol_name_view { symbol_name, strlen(symbol_name) };
  408. Optional<DynamicObject::SymbolLookupResult> symbol;
  409. if (handle) {
  410. auto object = static_cast<DynamicObject*>(handle);
  411. symbol = object->lookup_symbol(symbol_name_view);
  412. } else {
  413. // When handle is 0 (RTLD_DEFAULT) we should look up the symbol in all global modules
  414. // https://pubs.opengroup.org/onlinepubs/009604499/functions/dlsym.html
  415. symbol = DynamicLinker::lookup_global_symbol(symbol_name_view);
  416. }
  417. if (!symbol.has_value())
  418. return DlErrorMessage { ByteString::formatted("Symbol {} not found", symbol_name_view) };
  419. if (symbol.value().type == STT_GNU_IFUNC)
  420. return (void*)reinterpret_cast<DynamicObject::IfuncResolver>(symbol.value().address.as_ptr())();
  421. return symbol.value().address.as_ptr();
  422. }
  423. static Result<void, DlErrorMessage> __dladdr(void const* addr, Dl_info* info)
  424. {
  425. VirtualAddress user_addr { addr };
  426. pthread_mutex_lock(&s_loader_lock);
  427. ScopeGuard unlock_guard = [] { pthread_mutex_unlock(&s_loader_lock); };
  428. RefPtr<DynamicObject> best_matching_library;
  429. VirtualAddress best_library_offset;
  430. for (auto& lib : s_global_objects) {
  431. if (user_addr < lib.value->base_address())
  432. continue;
  433. auto offset = user_addr - lib.value->base_address();
  434. if (!best_matching_library || offset < best_library_offset) {
  435. best_matching_library = lib.value;
  436. best_library_offset = offset;
  437. }
  438. }
  439. if (!best_matching_library) {
  440. return DlErrorMessage { "No library found which contains the specified address" };
  441. }
  442. Optional<DynamicObject::Symbol> best_matching_symbol;
  443. best_matching_library->for_each_symbol([&](auto const& symbol) {
  444. if (user_addr < symbol.address() || user_addr > symbol.address().offset(symbol.size()))
  445. return;
  446. best_matching_symbol = symbol;
  447. });
  448. info->dli_fbase = best_matching_library->base_address().as_ptr();
  449. // This works because we don't support unloading objects.
  450. info->dli_fname = best_matching_library->filepath().characters();
  451. if (best_matching_symbol.has_value()) {
  452. info->dli_saddr = best_matching_symbol.value().address().as_ptr();
  453. info->dli_sname = best_matching_symbol.value().raw_name();
  454. } else {
  455. info->dli_saddr = nullptr;
  456. info->dli_sname = nullptr;
  457. }
  458. return {};
  459. }
  460. static void __call_fini_functions()
  461. {
  462. typedef void (*FiniFunc)();
  463. for (auto& it : s_global_objects) {
  464. auto object = it.value;
  465. if (object->has_fini_array_section()) {
  466. auto fini_array_section = object->fini_array_section();
  467. FiniFunc* fini_begin = (FiniFunc*)(fini_array_section.address().as_ptr());
  468. FiniFunc* fini_end = fini_begin + fini_array_section.entry_count();
  469. while (fini_begin != fini_end) {
  470. --fini_end;
  471. // Android sources claim that these can be -1, to be ignored.
  472. // 0 deffiniely shows up. Apparently 0/-1 are valid? Confusing.
  473. if (!*fini_end || ((FlatPtr)*fini_end == (FlatPtr)-1))
  474. continue;
  475. (*fini_end)();
  476. }
  477. }
  478. if (object->has_fini_section()) {
  479. auto fini_function = object->fini_section_function();
  480. (fini_function)();
  481. }
  482. }
  483. }
  484. static void read_environment_variables()
  485. {
  486. for (char** env = s_envp; *env; ++env) {
  487. StringView env_string { *env, strlen(*env) };
  488. if (env_string == "_LOADER_BREAKPOINT=1"sv) {
  489. s_do_breakpoint_trap_before_entry = true;
  490. }
  491. constexpr auto library_path_string = "LD_LIBRARY_PATH="sv;
  492. if (env_string.starts_with(library_path_string)) {
  493. s_ld_library_path = env_string.substring_view(library_path_string.length());
  494. }
  495. constexpr auto main_pledge_promises_key = "_LOADER_MAIN_PROGRAM_PLEDGE_PROMISES="sv;
  496. if (env_string.starts_with(main_pledge_promises_key)) {
  497. s_main_program_pledge_promises = env_string.substring_view(main_pledge_promises_key.length());
  498. }
  499. constexpr auto loader_pledge_promises_key = "_LOADER_PLEDGE_PROMISES="sv;
  500. if (env_string.starts_with(loader_pledge_promises_key)) {
  501. s_loader_pledge_promises = env_string.substring_view(loader_pledge_promises_key.length());
  502. }
  503. }
  504. }
  505. void ELF::DynamicLinker::linker_main(ByteString&& main_program_path, int main_program_fd, bool is_secure, int argc, char** argv, char** envp)
  506. {
  507. VERIFY(main_program_path.starts_with('/'));
  508. s_envp = envp;
  509. uintptr_t stack_guard = get_random<uintptr_t>();
  510. #ifdef AK_ARCH_64_BIT
  511. // For 64-bit platforms we include an additional hardening: zero the first byte of the stack guard to avoid
  512. // leaking or overwriting the stack guard with C-style string functions.
  513. stack_guard &= ~0xffULL;
  514. #endif
  515. s_magic_weak_symbols.set("environ"sv, make_ref_counted<MagicWeakSymbol>(STT_OBJECT, s_envp));
  516. s_magic_weak_symbols.set("__stack_chk_guard"sv, make_ref_counted<MagicWeakSymbol>(STT_OBJECT, stack_guard));
  517. s_magic_weak_symbols.set("__call_fini_functions"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __call_fini_functions));
  518. s_magic_weak_symbols.set("__dl_iterate_phdr"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __dl_iterate_phdr));
  519. s_magic_weak_symbols.set("__dlclose"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __dlclose));
  520. s_magic_weak_symbols.set("__dlopen"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __dlopen));
  521. s_magic_weak_symbols.set("__dlsym"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __dlsym));
  522. s_magic_weak_symbols.set("__dladdr"sv, make_ref_counted<MagicWeakSymbol>(STT_FUNC, __dladdr));
  523. char* raw_current_directory = getcwd(nullptr, 0);
  524. s_cwd = raw_current_directory;
  525. free(raw_current_directory);
  526. s_allowed_to_check_environment_variables = !is_secure;
  527. if (s_allowed_to_check_environment_variables)
  528. read_environment_variables();
  529. s_main_program_path = main_program_path;
  530. // NOTE: We always map the main library first, since it may require
  531. // placement at a specific address.
  532. auto result1 = map_library(main_program_path, main_program_fd);
  533. if (result1.is_error()) {
  534. warnln("{}", result1.error().text);
  535. fflush(stderr);
  536. _exit(1);
  537. }
  538. auto loader = result1.release_value();
  539. size_t needed_dependencies = 0;
  540. loader->for_each_needed_library([&needed_dependencies](auto) {
  541. needed_dependencies++;
  542. });
  543. bool has_interpreter = false;
  544. loader->image().for_each_program_header([&has_interpreter](const ELF::Image::ProgramHeader& program_header) {
  545. if (program_header.type() == PT_INTERP)
  546. has_interpreter = true;
  547. });
  548. // NOTE: Refuse to run a program if it has a dynamic section,
  549. // it is pie, and does not have an interpreter or needed libraries
  550. // which is also called "static-pie". These binaries are probably
  551. // some sort of ELF packers or dynamic loaders, and there's no added
  552. // value in trying to run them, as they will probably crash due to trying
  553. // to invoke syscalls from a non-syscall memory executable (code) region.
  554. if (loader->is_dynamic() && (!has_interpreter || needed_dependencies == 0) && loader->dynamic_object().is_pie()) {
  555. char const message[] = R"(error: the dynamic loader can't reasonably run static-pie ELF. static-pie ELFs might run executable code that invokes syscalls
  556. outside of the defined syscall memory executable (code) region security measure we implement.
  557. Examples of static-pie ELF objects are ELF packers, and the system dynamic loader itself.)";
  558. fprintf(stderr, "%s", message);
  559. fflush(stderr);
  560. _exit(1);
  561. }
  562. auto result2 = map_dependencies(main_program_path);
  563. if (result2.is_error()) {
  564. warnln("{}", result2.error().text);
  565. fflush(stderr);
  566. _exit(1);
  567. }
  568. dbgln_if(DYNAMIC_LOAD_DEBUG, "loaded all dependencies");
  569. for ([[maybe_unused]] auto& lib : s_loaders) {
  570. dbgln_if(DYNAMIC_LOAD_DEBUG, "{} - tls size: {}, tls alignment: {}, tls offset: {}", lib.key, lib.value->tls_size_of_current_object(), lib.value->tls_alignment_of_current_object(), lib.value->tls_offset());
  571. }
  572. allocate_tls();
  573. auto entry_point_function = [&main_program_path] {
  574. auto result = link_main_library(main_program_path, RTLD_GLOBAL | RTLD_LAZY);
  575. if (result.is_error()) {
  576. warnln("{}", result.error().text);
  577. _exit(1);
  578. }
  579. drop_loader_promise("rpath"sv);
  580. auto& main_executable_loader = *s_loaders.get(main_program_path);
  581. auto entry_point = main_executable_loader->image().entry();
  582. if (main_executable_loader->is_dynamic())
  583. entry_point = entry_point.offset(main_executable_loader->base_address().get());
  584. return (EntryPointFunction)(entry_point.as_ptr());
  585. }();
  586. s_loaders.clear();
  587. int rc = syscall(SC_prctl, PR_SET_NO_NEW_SYSCALL_REGION_ANNOTATIONS, 1, 0, nullptr);
  588. if (rc < 0) {
  589. VERIFY_NOT_REACHED();
  590. }
  591. dbgln_if(DYNAMIC_LOAD_DEBUG, "Jumping to entry point: {:p}", entry_point_function);
  592. if (s_do_breakpoint_trap_before_entry) {
  593. #if ARCH(AARCH64)
  594. asm("brk #0");
  595. #elif ARCH(RISCV64)
  596. asm("ebreak");
  597. #elif ARCH(X86_64)
  598. asm("int3");
  599. #else
  600. # error "Unknown architecture"
  601. #endif
  602. }
  603. _invoke_entry(argc, argv, envp, entry_point_function);
  604. VERIFY_NOT_REACHED();
  605. }
  606. }