DynamicLinker.cpp 30 KB

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