execve.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/ScopeGuard.h>
  28. #include <AK/TemporaryChange.h>
  29. #include <AK/WeakPtr.h>
  30. #include <Kernel/FileSystem/Custody.h>
  31. #include <Kernel/FileSystem/FileDescription.h>
  32. #include <Kernel/PerformanceEventBuffer.h>
  33. #include <Kernel/Process.h>
  34. #include <Kernel/Random.h>
  35. #include <Kernel/Time/TimeManagement.h>
  36. #include <Kernel/VM/AllocationStrategy.h>
  37. #include <Kernel/VM/MemoryManager.h>
  38. #include <Kernel/VM/PageDirectory.h>
  39. #include <Kernel/VM/Region.h>
  40. #include <Kernel/VM/SharedInodeVMObject.h>
  41. #include <LibC/limits.h>
  42. #include <LibELF/AuxiliaryVector.h>
  43. #include <LibELF/Image.h>
  44. #include <LibELF/Validation.h>
  45. //#define EXEC_DEBUG
  46. namespace Kernel {
  47. static Vector<ELF::AuxiliaryValue> generate_auxiliary_vector(FlatPtr load_base, FlatPtr entry_eip, uid_t uid, uid_t euid, gid_t gid, gid_t egid, String executable_path, int main_program_fd);
  48. static bool validate_stack_size(const Vector<String>& arguments, const Vector<String>& environment)
  49. {
  50. size_t total_blob_size = 0;
  51. for (auto& a : arguments)
  52. total_blob_size += a.length() + 1;
  53. for (auto& e : environment)
  54. total_blob_size += e.length() + 1;
  55. size_t total_meta_size = sizeof(char*) * (arguments.size() + 1) + sizeof(char*) * (environment.size() + 1);
  56. // FIXME: This doesn't account for the size of the auxiliary vector
  57. return (total_blob_size + total_meta_size) < Thread::default_userspace_stack_size;
  58. }
  59. static KResultOr<FlatPtr> make_userspace_stack_for_main_thread(Region& region, Vector<String> arguments, Vector<String> environment, Vector<ELF::AuxiliaryValue> auxiliary_values)
  60. {
  61. FlatPtr new_esp = region.vaddr().offset(Thread::default_userspace_stack_size).get();
  62. auto push_on_new_stack = [&new_esp](u32 value) {
  63. new_esp -= 4;
  64. Userspace<u32*> stack_ptr = new_esp;
  65. return copy_to_user(stack_ptr, &value);
  66. };
  67. auto push_aux_value_on_new_stack = [&new_esp](auxv_t value) {
  68. new_esp -= sizeof(auxv_t);
  69. Userspace<auxv_t*> stack_ptr = new_esp;
  70. return copy_to_user(stack_ptr, &value);
  71. };
  72. auto push_string_on_new_stack = [&new_esp](const String& string) {
  73. new_esp -= round_up_to_power_of_two(string.length() + 1, 4);
  74. Userspace<u32*> stack_ptr = new_esp;
  75. return copy_to_user(stack_ptr, string.characters(), string.length() + 1);
  76. };
  77. Vector<FlatPtr> argv_entries;
  78. for (auto& argument : arguments) {
  79. push_string_on_new_stack(argument);
  80. argv_entries.append(new_esp);
  81. }
  82. Vector<FlatPtr> env_entries;
  83. for (auto& variable : environment) {
  84. push_string_on_new_stack(variable);
  85. env_entries.append(new_esp);
  86. }
  87. for (auto& value : auxiliary_values) {
  88. if (!value.optional_string.is_empty()) {
  89. push_string_on_new_stack(value.optional_string);
  90. value.auxv.a_un.a_ptr = (void*)new_esp;
  91. }
  92. }
  93. for (ssize_t i = auxiliary_values.size() - 1; i >= 0; --i) {
  94. auto& value = auxiliary_values[i];
  95. push_aux_value_on_new_stack(value.auxv);
  96. }
  97. push_on_new_stack(0);
  98. for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
  99. push_on_new_stack(env_entries[i]);
  100. FlatPtr envp = new_esp;
  101. push_on_new_stack(0);
  102. for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
  103. push_on_new_stack(argv_entries[i]);
  104. FlatPtr argv = new_esp;
  105. // NOTE: The stack needs to be 16-byte aligned.
  106. new_esp -= new_esp % 16;
  107. push_on_new_stack((FlatPtr)envp);
  108. push_on_new_stack((FlatPtr)argv);
  109. push_on_new_stack((FlatPtr)argv_entries.size());
  110. push_on_new_stack(0);
  111. return new_esp;
  112. }
  113. KResultOr<Process::LoadResult> Process::load_elf_object(FileDescription& object_description, FlatPtr load_offset, ShouldAllocateTls should_allocate_tls)
  114. {
  115. auto& inode = *(object_description.inode());
  116. auto vmobject = SharedInodeVMObject::create_with_inode(inode);
  117. if (vmobject->writable_mappings()) {
  118. dbgln("Refusing to execute a write-mapped program");
  119. return KResult(-ETXTBSY);
  120. }
  121. size_t executable_size = inode.size();
  122. auto executable_region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(executable_size), "ELF loading", Region::Access::Read);
  123. if (!executable_region) {
  124. dbgln("Could not allocate memory for ELF loading");
  125. return KResult(-ENOMEM);
  126. }
  127. auto elf_image = ELF::Image(executable_region->vaddr().as_ptr(), executable_size);
  128. if (!elf_image.is_valid())
  129. return KResult(-ENOEXEC);
  130. Region* master_tls_region { nullptr };
  131. size_t master_tls_size = 0;
  132. size_t master_tls_alignment = 0;
  133. FlatPtr load_base_address = 0;
  134. String elf_name = object_description.absolute_path();
  135. ASSERT(!Processor::current().in_critical());
  136. KResult ph_load_result = KSuccess;
  137. elf_image.for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
  138. if (program_header.type() == PT_TLS) {
  139. ASSERT(should_allocate_tls == ShouldAllocateTls::Yes);
  140. ASSERT(program_header.size_in_memory());
  141. if (!elf_image.is_within_image(program_header.raw_data(), program_header.size_in_image())) {
  142. dbgln("Shenanigans! ELF PT_TLS header sneaks outside of executable.");
  143. ph_load_result = KResult(-ENOEXEC);
  144. return IterationDecision::Break;
  145. }
  146. auto region_or_error = allocate_region({}, program_header.size_in_memory(), String::formatted("{} (master-tls)", elf_name), PROT_READ | PROT_WRITE, AllocationStrategy::Reserve);
  147. if (region_or_error.is_error()) {
  148. ph_load_result = region_or_error.error();
  149. return IterationDecision::Break;
  150. }
  151. master_tls_region = region_or_error.value();
  152. master_tls_size = program_header.size_in_memory();
  153. master_tls_alignment = program_header.alignment();
  154. if (!copy_to_user(master_tls_region->vaddr().as_ptr(), program_header.raw_data(), program_header.size_in_image())) {
  155. ph_load_result = KResult(-EFAULT);
  156. return IterationDecision::Break;
  157. }
  158. return IterationDecision::Continue;
  159. }
  160. if (program_header.type() != PT_LOAD)
  161. return IterationDecision::Continue;
  162. if (program_header.is_writable()) {
  163. // Writable section: create a copy in memory.
  164. ASSERT(program_header.size_in_memory());
  165. ASSERT(program_header.alignment() == PAGE_SIZE);
  166. if (!elf_image.is_within_image(program_header.raw_data(), program_header.size_in_image())) {
  167. dbgln("Shenanigans! Writable ELF PT_LOAD header sneaks outside of executable.");
  168. ph_load_result = KResult(-ENOEXEC);
  169. return IterationDecision::Break;
  170. }
  171. int prot = 0;
  172. if (program_header.is_readable())
  173. prot |= PROT_READ;
  174. if (program_header.is_writable())
  175. prot |= PROT_WRITE;
  176. auto region_name = String::formatted("{} (data-{}{})", elf_name, program_header.is_readable() ? "r" : "", program_header.is_writable() ? "w" : "");
  177. auto region_or_error = allocate_region(program_header.vaddr().offset(load_offset), program_header.size_in_memory(), move(region_name), prot, AllocationStrategy::Reserve);
  178. if (region_or_error.is_error()) {
  179. ph_load_result = region_or_error.error();
  180. return IterationDecision::Break;
  181. }
  182. // It's not always the case with PIE executables (and very well shouldn't be) that the
  183. // virtual address in the program header matches the one we end up giving the process.
  184. // In order to copy the data image correctly into memory, we need to copy the data starting at
  185. // the right initial page offset into the pages allocated for the elf_alloc-XX section.
  186. // FIXME: There's an opportunity to munmap, or at least mprotect, the padding space between
  187. // the .text and .data PT_LOAD sections of the executable.
  188. // Accessing it would definitely be a bug.
  189. auto page_offset = program_header.vaddr();
  190. page_offset.mask(~PAGE_MASK);
  191. if (!copy_to_user((u8*)region_or_error.value()->vaddr().as_ptr() + page_offset.get(), program_header.raw_data(), program_header.size_in_image())) {
  192. ph_load_result = KResult(-EFAULT);
  193. return IterationDecision::Break;
  194. }
  195. return IterationDecision::Continue;
  196. }
  197. // Non-writable section: map the executable itself in memory.
  198. ASSERT(program_header.size_in_memory());
  199. ASSERT(program_header.alignment() == PAGE_SIZE);
  200. int prot = 0;
  201. if (program_header.is_readable())
  202. prot |= PROT_READ;
  203. if (program_header.is_writable())
  204. prot |= PROT_WRITE;
  205. if (program_header.is_executable())
  206. prot |= PROT_EXEC;
  207. auto region_or_error = allocate_region_with_vmobject(program_header.vaddr().offset(load_offset), program_header.size_in_memory(), *vmobject, program_header.offset(), elf_name, prot, true);
  208. if (region_or_error.is_error()) {
  209. ph_load_result = region_or_error.error();
  210. return IterationDecision::Break;
  211. }
  212. if (program_header.offset() == 0)
  213. load_base_address = (FlatPtr)region_or_error.value()->vaddr().as_ptr();
  214. return IterationDecision::Continue;
  215. });
  216. if (ph_load_result.is_error()) {
  217. dbgln("do_exec: Failure loading program ({})", ph_load_result.error());
  218. return ph_load_result;
  219. }
  220. if (!elf_image.entry().offset(load_offset).get()) {
  221. dbgln("do_exec: Failure loading program, entry pointer is invalid! {})", elf_image.entry().offset(load_offset));
  222. return KResult(-ENOEXEC);
  223. }
  224. auto stack_region_or_error = allocate_region(VirtualAddress(), Thread::default_userspace_stack_size, "Stack (Main thread)", PROT_READ | PROT_WRITE, AllocationStrategy::Reserve);
  225. if (stack_region_or_error.is_error())
  226. return stack_region_or_error.error();
  227. auto& stack_region = *stack_region_or_error.value();
  228. stack_region.set_stack(true);
  229. return LoadResult {
  230. load_base_address,
  231. elf_image.entry().offset(load_offset).get(),
  232. executable_size,
  233. VirtualAddress(elf_image.program_header_table_offset()).offset(load_offset).get(),
  234. elf_image.program_header_count(),
  235. AK::try_make_weak_ptr(master_tls_region),
  236. master_tls_size,
  237. master_tls_alignment,
  238. stack_region.make_weak_ptr()
  239. };
  240. }
  241. KResultOr<Process::LoadResult> Process::load(NonnullRefPtr<FileDescription> main_program_description, RefPtr<FileDescription> interpreter_description, const Elf32_Ehdr& main_program_header)
  242. {
  243. RefPtr<PageDirectory> old_page_directory;
  244. NonnullOwnPtrVector<Region> old_regions;
  245. {
  246. auto page_directory = PageDirectory::create_for_userspace(*this);
  247. if (!page_directory)
  248. return KResult(-ENOMEM);
  249. // Need to make sure we don't swap contexts in the middle
  250. ScopedCritical critical;
  251. old_page_directory = move(m_page_directory);
  252. old_regions = move(m_regions);
  253. m_page_directory = page_directory.release_nonnull();
  254. MM.enter_process_paging_scope(*this);
  255. }
  256. ArmedScopeGuard rollback_regions_guard([&]() {
  257. ASSERT(Process::current() == this);
  258. // Need to make sure we don't swap contexts in the middle
  259. ScopedCritical critical;
  260. // Explicitly clear m_regions *before* restoring the page directory,
  261. // otherwise we may silently corrupt memory!
  262. m_regions.clear();
  263. // Now that we freed the regions, revert to the original page directory
  264. // and restore the original regions
  265. m_page_directory = move(old_page_directory);
  266. MM.enter_process_paging_scope(*this);
  267. m_regions = move(old_regions);
  268. });
  269. if (interpreter_description.is_null()) {
  270. auto result = load_elf_object(main_program_description, FlatPtr { 0 }, ShouldAllocateTls::Yes);
  271. if (result.is_error())
  272. return result.error();
  273. rollback_regions_guard.disarm();
  274. return result;
  275. }
  276. auto interpreter_load_offset = get_interpreter_load_offset(main_program_header, main_program_description, *interpreter_description);
  277. if (interpreter_load_offset.is_error()) {
  278. return interpreter_load_offset.error();
  279. }
  280. auto interpreter_load_result = load_elf_object(*interpreter_description, interpreter_load_offset.value(), ShouldAllocateTls::No);
  281. if (interpreter_load_result.is_error())
  282. return interpreter_load_result.error();
  283. // TLS allocation will be done in userspace by the loader
  284. ASSERT(!interpreter_load_result.value().tls_region);
  285. ASSERT(!interpreter_load_result.value().tls_alignment);
  286. ASSERT(!interpreter_load_result.value().tls_size);
  287. rollback_regions_guard.disarm();
  288. return interpreter_load_result;
  289. }
  290. struct RequiredLoadRange {
  291. FlatPtr start { 0 };
  292. FlatPtr end { 0 };
  293. };
  294. static KResultOr<RequiredLoadRange> get_required_load_range(FileDescription& program_description)
  295. {
  296. auto& inode = *(program_description.inode());
  297. auto vmobject = SharedInodeVMObject::create_with_inode(inode);
  298. size_t executable_size = inode.size();
  299. auto region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_ROUND_UP(executable_size), "ELF memory range calculation", Region::Access::Read);
  300. if (!region) {
  301. dbgln("Could not allocate memory for ELF");
  302. return KResult(-ENOMEM);
  303. }
  304. auto elf_image = ELF::Image(region->vaddr().as_ptr(), executable_size);
  305. if (!elf_image.is_valid()) {
  306. return -EINVAL;
  307. }
  308. RequiredLoadRange range {};
  309. elf_image.for_each_program_header([&range](const auto& pheader) {
  310. if (pheader.type() != PT_LOAD)
  311. return IterationDecision::Continue;
  312. auto region_start = (FlatPtr)pheader.vaddr().as_ptr();
  313. auto region_end = region_start + pheader.size_in_memory();
  314. if (range.start == 0 || region_start < range.start)
  315. range.start = region_start;
  316. if (range.end == 0 || region_end > range.end)
  317. range.end = region_end;
  318. return IterationDecision::Continue;
  319. });
  320. ASSERT(range.end > range.start);
  321. return range;
  322. };
  323. KResultOr<FlatPtr> Process::get_interpreter_load_offset(const Elf32_Ehdr& main_program_header, FileDescription& main_program_description, FileDescription& interpreter_description)
  324. {
  325. constexpr FlatPtr interpreter_load_range_start = 0x08000000;
  326. constexpr FlatPtr interpreter_load_range_size = 65536 * PAGE_SIZE; // 2**16 * PAGE_SIZE = 256MB
  327. constexpr FlatPtr minimum_interpreter_load_offset_randomization_size = 10 * MiB;
  328. auto random_load_offset_in_range([](auto start, auto size) {
  329. return PAGE_ROUND_DOWN(start + get_good_random<FlatPtr>() % size);
  330. });
  331. if (main_program_header.e_type == ET_DYN) {
  332. return random_load_offset_in_range(interpreter_load_range_start, interpreter_load_range_size);
  333. }
  334. if (main_program_header.e_type != ET_EXEC)
  335. return -EINVAL;
  336. auto main_program_load_range_result = get_required_load_range(main_program_description);
  337. if (main_program_load_range_result.is_error())
  338. return main_program_load_range_result.error();
  339. auto main_program_load_range = main_program_load_range_result.value();
  340. auto interpreter_load_range_result = get_required_load_range(interpreter_description);
  341. if (interpreter_load_range_result.is_error())
  342. return interpreter_load_range_result.error();
  343. auto interpreter_size_in_memory = interpreter_load_range_result.value().end - interpreter_load_range_result.value().start;
  344. auto interpreter_load_range_end = interpreter_load_range_start + interpreter_load_range_size - interpreter_size_in_memory;
  345. // No intersection
  346. if (main_program_load_range.end < interpreter_load_range_start || main_program_load_range.start > interpreter_load_range_end)
  347. return random_load_offset_in_range(interpreter_load_range_start, interpreter_load_range_size);
  348. RequiredLoadRange first_available_part = { interpreter_load_range_start, main_program_load_range.start };
  349. RequiredLoadRange second_available_part = { main_program_load_range.end, interpreter_load_range_end };
  350. RequiredLoadRange selected_range {};
  351. // Select larger part
  352. if (first_available_part.end - first_available_part.start > second_available_part.end - second_available_part.start)
  353. selected_range = first_available_part;
  354. else
  355. selected_range = second_available_part;
  356. // If main program is too big and leaves us without enough space for adequate loader randmoization
  357. if (selected_range.end - selected_range.start < minimum_interpreter_load_offset_randomization_size)
  358. return -E2BIG;
  359. return random_load_offset_in_range(selected_range.start, selected_range.end - selected_range.start);
  360. }
  361. int Process::do_exec(NonnullRefPtr<FileDescription> main_program_description, Vector<String> arguments, Vector<String> environment, RefPtr<FileDescription> interpreter_description, Thread*& new_main_thread, u32& prev_flags, const Elf32_Ehdr& main_program_header)
  362. {
  363. ASSERT(is_user_process());
  364. ASSERT(!Processor::current().in_critical());
  365. auto path = main_program_description->absolute_path();
  366. #ifdef EXEC_DEBUG
  367. dbgln("do_exec({})", path);
  368. #endif
  369. // FIXME: How much stack space does process startup need?
  370. if (!validate_stack_size(arguments, environment))
  371. return -E2BIG;
  372. auto parts = path.split('/');
  373. if (parts.is_empty())
  374. return -ENOENT;
  375. // Disable profiling temporarily in case it's running on this process.
  376. TemporaryChange profiling_disabler(m_profiling, false);
  377. // Mark this thread as the current thread that does exec
  378. // No other thread from this process will be scheduled to run
  379. auto current_thread = Thread::current();
  380. m_exec_tid = current_thread->tid();
  381. // NOTE: We switch credentials before altering the memory layout of the process.
  382. // This ensures that ptrace access control takes the right credentials into account.
  383. // FIXME: This still feels rickety. Perhaps it would be better to simply block ptrace
  384. // clients until we're ready to be traced? Or reject them with EPERM?
  385. auto main_program_metadata = main_program_description->metadata();
  386. auto old_euid = m_euid;
  387. auto old_suid = m_suid;
  388. auto old_egid = m_egid;
  389. auto old_sgid = m_sgid;
  390. ArmedScopeGuard cred_restore_guard = [&] {
  391. m_euid = old_euid;
  392. m_suid = old_suid;
  393. m_egid = old_egid;
  394. m_sgid = old_sgid;
  395. };
  396. bool executable_is_setid = false;
  397. if (!(main_program_description->custody()->mount_flags() & MS_NOSUID)) {
  398. if (main_program_metadata.is_setuid()) {
  399. executable_is_setid = true;
  400. m_euid = m_suid = main_program_metadata.uid;
  401. }
  402. if (main_program_metadata.is_setgid()) {
  403. executable_is_setid = true;
  404. m_egid = m_sgid = main_program_metadata.gid;
  405. }
  406. }
  407. auto load_result_or_error = load(main_program_description, interpreter_description, main_program_header);
  408. if (load_result_or_error.is_error()) {
  409. dbgln("do_exec({}): Failed to load main program or interpreter", path);
  410. return load_result_or_error.error();
  411. }
  412. auto& load_result = load_result_or_error.value();
  413. // We can commit to the new credentials at this point.
  414. cred_restore_guard.disarm();
  415. kill_threads_except_self();
  416. #ifdef EXEC_DEBUG
  417. dbgln("Memory layout after ELF load:");
  418. dump_regions();
  419. #endif
  420. m_executable = main_program_description->custody();
  421. m_promises = m_execpromises;
  422. m_veil_state = VeilState::None;
  423. m_unveiled_paths.clear();
  424. current_thread->set_default_signal_dispositions();
  425. current_thread->clear_signals();
  426. m_futex_queues.clear();
  427. m_region_lookup_cache = {};
  428. disown_all_shared_buffers();
  429. set_dumpable(!executable_is_setid);
  430. for (size_t i = 0; i < m_fds.size(); ++i) {
  431. auto& description_and_flags = m_fds[i];
  432. if (description_and_flags.description() && description_and_flags.flags() & FD_CLOEXEC)
  433. description_and_flags = {};
  434. }
  435. int main_program_fd = -1;
  436. if (interpreter_description) {
  437. main_program_fd = alloc_fd();
  438. ASSERT(main_program_fd >= 0);
  439. main_program_description->seek(0, SEEK_SET);
  440. main_program_description->set_readable(true);
  441. m_fds[main_program_fd].set(move(main_program_description), FD_CLOEXEC);
  442. }
  443. new_main_thread = nullptr;
  444. if (&current_thread->process() == this) {
  445. new_main_thread = current_thread;
  446. } else {
  447. for_each_thread([&](auto& thread) {
  448. new_main_thread = &thread;
  449. return IterationDecision::Break;
  450. });
  451. }
  452. ASSERT(new_main_thread);
  453. auto auxv = generate_auxiliary_vector(load_result.load_base, load_result.entry_eip, m_uid, m_euid, m_gid, m_egid, path, main_program_fd);
  454. // NOTE: We create the new stack before disabling interrupts since it will zero-fault
  455. // and we don't want to deal with faults after this point.
  456. auto make_stack_result = make_userspace_stack_for_main_thread(*load_result.stack_region.unsafe_ptr(), move(arguments), move(environment), move(auxv));
  457. if (make_stack_result.is_error())
  458. return make_stack_result.error();
  459. u32 new_userspace_esp = make_stack_result.value();
  460. if (wait_for_tracer_at_next_execve())
  461. Thread::current()->send_urgent_signal_to_self(SIGSTOP);
  462. // We enter a critical section here because we don't want to get interrupted between do_exec()
  463. // and Processor::assume_context() or the next context switch.
  464. // If we used an InterruptDisabler that sti()'d on exit, we might timer tick'd too soon in exec().
  465. Processor::current().enter_critical(prev_flags);
  466. // NOTE: Be careful to not trigger any page faults below!
  467. m_name = parts.take_last();
  468. new_main_thread->set_name(m_name);
  469. // FIXME: PID/TID ISSUE
  470. m_pid = new_main_thread->tid().value();
  471. auto tsr_result = new_main_thread->make_thread_specific_region({});
  472. if (tsr_result.is_error())
  473. return tsr_result.error();
  474. new_main_thread->reset_fpu_state();
  475. auto& tss = new_main_thread->m_tss;
  476. tss.cs = GDT_SELECTOR_CODE3 | 3;
  477. tss.ds = GDT_SELECTOR_DATA3 | 3;
  478. tss.es = GDT_SELECTOR_DATA3 | 3;
  479. tss.ss = GDT_SELECTOR_DATA3 | 3;
  480. tss.fs = GDT_SELECTOR_DATA3 | 3;
  481. tss.gs = GDT_SELECTOR_TLS | 3;
  482. tss.eip = load_result.entry_eip;
  483. tss.esp = new_userspace_esp;
  484. tss.cr3 = m_page_directory->cr3();
  485. tss.ss2 = m_pid.value();
  486. // Throw away any recorded performance events in this process.
  487. if (m_perf_event_buffer)
  488. m_perf_event_buffer->clear();
  489. {
  490. ScopedSpinLock lock(g_scheduler_lock);
  491. new_main_thread->set_state(Thread::State::Runnable);
  492. }
  493. u32 lock_count_to_restore;
  494. [[maybe_unused]] auto rc = big_lock().force_unlock_if_locked(lock_count_to_restore);
  495. ASSERT_INTERRUPTS_DISABLED();
  496. ASSERT(Processor::current().in_critical());
  497. return 0;
  498. }
  499. static Vector<ELF::AuxiliaryValue> generate_auxiliary_vector(FlatPtr load_base, FlatPtr entry_eip, uid_t uid, uid_t euid, gid_t gid, gid_t egid, String executable_path, int main_program_fd)
  500. {
  501. Vector<ELF::AuxiliaryValue> auxv;
  502. // PHDR/EXECFD
  503. // PH*
  504. auxv.append({ ELF::AuxiliaryValue::PageSize, PAGE_SIZE });
  505. auxv.append({ ELF::AuxiliaryValue::BaseAddress, (void*)load_base });
  506. auxv.append({ ELF::AuxiliaryValue::Entry, (void*)entry_eip });
  507. // NOTELF
  508. auxv.append({ ELF::AuxiliaryValue::Uid, (long)uid });
  509. auxv.append({ ELF::AuxiliaryValue::EUid, (long)euid });
  510. auxv.append({ ELF::AuxiliaryValue::Gid, (long)gid });
  511. auxv.append({ ELF::AuxiliaryValue::EGid, (long)egid });
  512. // FIXME: Don't hard code this? We might support other platforms later.. (e.g. x86_64)
  513. auxv.append({ ELF::AuxiliaryValue::Platform, "i386" });
  514. // FIXME: This is platform specific
  515. auxv.append({ ELF::AuxiliaryValue::HwCap, (long)CPUID(1).edx() });
  516. auxv.append({ ELF::AuxiliaryValue::ClockTick, (long)TimeManagement::the().ticks_per_second() });
  517. // FIXME: Also take into account things like extended filesystem permissions? That's what linux does...
  518. auxv.append({ ELF::AuxiliaryValue::Secure, ((uid != euid) || (gid != egid)) ? 1 : 0 });
  519. char random_bytes[16] {};
  520. get_fast_random_bytes((u8*)random_bytes, sizeof(random_bytes));
  521. auxv.append({ ELF::AuxiliaryValue::Random, String(random_bytes, sizeof(random_bytes)) });
  522. auxv.append({ ELF::AuxiliaryValue::ExecFilename, executable_path });
  523. auxv.append({ ELF::AuxiliaryValue::ExecFileDescriptor, main_program_fd });
  524. auxv.append({ ELF::AuxiliaryValue::Null, 0L });
  525. return auxv;
  526. }
  527. static KResultOr<Vector<String>> find_shebang_interpreter_for_executable(const char first_page[], int nread)
  528. {
  529. int word_start = 2;
  530. int word_length = 0;
  531. if (nread > 2 && first_page[0] == '#' && first_page[1] == '!') {
  532. Vector<String> interpreter_words;
  533. for (int i = 2; i < nread; ++i) {
  534. if (first_page[i] == '\n') {
  535. break;
  536. }
  537. if (first_page[i] != ' ') {
  538. ++word_length;
  539. }
  540. if (first_page[i] == ' ') {
  541. if (word_length > 0) {
  542. interpreter_words.append(String(&first_page[word_start], word_length));
  543. }
  544. word_length = 0;
  545. word_start = i + 1;
  546. }
  547. }
  548. if (word_length > 0)
  549. interpreter_words.append(String(&first_page[word_start], word_length));
  550. if (!interpreter_words.is_empty())
  551. return interpreter_words;
  552. }
  553. return KResult(-ENOEXEC);
  554. }
  555. KResultOr<RefPtr<FileDescription>> Process::find_elf_interpreter_for_executable(const String& path, const Elf32_Ehdr& main_program_header, int nread, size_t file_size)
  556. {
  557. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  558. String interpreter_path;
  559. if (!ELF::validate_program_headers(main_program_header, file_size, (const u8*)&main_program_header, nread, &interpreter_path)) {
  560. dbgln("exec({}): File has invalid ELF Program headers", path);
  561. return KResult(-ENOEXEC);
  562. }
  563. if (!interpreter_path.is_empty()) {
  564. #ifdef EXEC_DEBUG
  565. dbgln("exec({}): Using program interpreter {}", path, interpreter_path);
  566. #endif
  567. auto interp_result = VFS::the().open(interpreter_path, O_EXEC, 0, current_directory());
  568. if (interp_result.is_error()) {
  569. dbgln("exec({}): Unable to open program interpreter {}", path, interpreter_path);
  570. return interp_result.error();
  571. }
  572. auto interpreter_description = interp_result.value();
  573. auto interp_metadata = interpreter_description->metadata();
  574. ASSERT(interpreter_description->inode());
  575. // Validate the program interpreter as a valid elf binary.
  576. // If your program interpreter is a #! file or something, it's time to stop playing games :)
  577. if (interp_metadata.size < (int)sizeof(Elf32_Ehdr))
  578. return KResult(-ENOEXEC);
  579. char first_page[PAGE_SIZE] = {};
  580. auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
  581. auto nread_or_error = interpreter_description->read(first_page_buffer, sizeof(first_page));
  582. if (nread_or_error.is_error())
  583. return KResult(-ENOEXEC);
  584. nread = nread_or_error.value();
  585. if (nread < (int)sizeof(Elf32_Ehdr))
  586. return KResult(-ENOEXEC);
  587. auto elf_header = (Elf32_Ehdr*)first_page;
  588. if (!ELF::validate_elf_header(*elf_header, interp_metadata.size)) {
  589. dbgln("exec({}): Interpreter ({}) has invalid ELF header", path, interpreter_description->absolute_path());
  590. return KResult(-ENOEXEC);
  591. }
  592. // Not using KResultOr here because we'll want to do the same thing in userspace in the RTLD
  593. String interpreter_interpreter_path;
  594. if (!ELF::validate_program_headers(*elf_header, interp_metadata.size, (u8*)first_page, nread, &interpreter_interpreter_path)) {
  595. dbgln("exec({}): Interpreter ({}) has invalid ELF Program headers", path, interpreter_description->absolute_path());
  596. return KResult(-ENOEXEC);
  597. }
  598. if (!interpreter_interpreter_path.is_empty()) {
  599. dbgln("exec({}): Interpreter ({}) has its own interpreter ({})! No thank you!", path, interpreter_description->absolute_path(), interpreter_interpreter_path);
  600. return KResult(-ELOOP);
  601. }
  602. return interpreter_description;
  603. }
  604. if (main_program_header.e_type == ET_REL) {
  605. // We can't exec an ET_REL, that's just an object file from the compiler
  606. return KResult(-ENOEXEC);
  607. }
  608. if (main_program_header.e_type == ET_DYN) {
  609. // If it's ET_DYN with no PT_INTERP, then it's a dynamic executable responsible
  610. // for its own relocation (i.e. it's /usr/lib/Loader.so)
  611. if (path != "/usr/lib/Loader.so")
  612. dbgln("exec({}): WARNING - Dynamic ELF executable without a PT_INTERP header, and isn't /usr/lib/Loader.so");
  613. return nullptr;
  614. }
  615. // No interpreter, but, path refers to a valid elf image
  616. return KResult(KSuccess);
  617. }
  618. int Process::exec(String path, Vector<String> arguments, Vector<String> environment, int recursion_depth)
  619. {
  620. if (recursion_depth > 2) {
  621. dbgln("exec({}): SHENANIGANS! recursed too far trying to find #! interpreter", path);
  622. return -ELOOP;
  623. }
  624. // Open the file to check what kind of binary format it is
  625. // Currently supported formats:
  626. // - #! interpreted file
  627. // - ELF32
  628. // * ET_EXEC binary that just gets loaded
  629. // * ET_DYN binary that requires a program interpreter
  630. //
  631. auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
  632. if (result.is_error())
  633. return result.error();
  634. auto description = result.release_value();
  635. auto metadata = description->metadata();
  636. // Always gonna need at least 3 bytes. these are for #!X
  637. if (metadata.size < 3)
  638. return -ENOEXEC;
  639. ASSERT(description->inode());
  640. // Read the first page of the program into memory so we can validate the binfmt of it
  641. char first_page[PAGE_SIZE];
  642. auto first_page_buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&first_page);
  643. auto nread_or_error = description->read(first_page_buffer, sizeof(first_page));
  644. if (nread_or_error.is_error())
  645. return -ENOEXEC;
  646. // 1) #! interpreted file
  647. auto shebang_result = find_shebang_interpreter_for_executable(first_page, nread_or_error.value());
  648. if (!shebang_result.is_error()) {
  649. Vector<String> new_arguments(shebang_result.value());
  650. new_arguments.append(path);
  651. arguments.remove(0);
  652. new_arguments.append(move(arguments));
  653. return exec(shebang_result.value().first(), move(new_arguments), move(environment), ++recursion_depth);
  654. }
  655. // #2) ELF32 for i386
  656. if (nread_or_error.value() < (int)sizeof(Elf32_Ehdr))
  657. return KResult(-ENOEXEC);
  658. auto main_program_header = (Elf32_Ehdr*)first_page;
  659. if (!ELF::validate_elf_header(*main_program_header, metadata.size)) {
  660. dbgln("exec({}): File has invalid ELF header", path);
  661. return KResult(-ENOEXEC);
  662. }
  663. auto elf_result = find_elf_interpreter_for_executable(path, *main_program_header, nread_or_error.value(), metadata.size);
  664. // Assume a static ELF executable by default
  665. RefPtr<FileDescription> interpreter_description;
  666. // We're getting either an interpreter, an error, or KSuccess (i.e. no interpreter but file checks out)
  667. if (!elf_result.is_error()) {
  668. // It's a dynamic ELF executable, with or without an interpreter. Do not allocate TLS
  669. interpreter_description = elf_result.value();
  670. } else if (elf_result.error().is_error())
  671. return elf_result.error();
  672. // The bulk of exec() is done by do_exec(), which ensures that all locals
  673. // are cleaned up by the time we yield-teleport below.
  674. Thread* new_main_thread = nullptr;
  675. u32 prev_flags = 0;
  676. int rc = do_exec(move(description), move(arguments), move(environment), move(interpreter_description), new_main_thread, prev_flags, *main_program_header);
  677. m_exec_tid = 0;
  678. if (rc < 0)
  679. return rc;
  680. ASSERT_INTERRUPTS_DISABLED();
  681. ASSERT(Processor::current().in_critical());
  682. auto current_thread = Thread::current();
  683. if (current_thread == new_main_thread) {
  684. // We need to enter the scheduler lock before changing the state
  685. // and it will be released after the context switch into that
  686. // thread. We should also still be in our critical section
  687. ASSERT(!g_scheduler_lock.own_lock());
  688. ASSERT(Processor::current().in_critical() == 1);
  689. g_scheduler_lock.lock();
  690. current_thread->set_state(Thread::State::Running);
  691. Processor::assume_context(*current_thread, prev_flags);
  692. ASSERT_NOT_REACHED();
  693. }
  694. Processor::current().leave_critical(prev_flags);
  695. return 0;
  696. }
  697. int Process::sys$execve(Userspace<const Syscall::SC_execve_params*> user_params)
  698. {
  699. REQUIRE_PROMISE(exec);
  700. // NOTE: Be extremely careful with allocating any kernel memory in exec().
  701. // On success, the kernel stack will be lost.
  702. Syscall::SC_execve_params params;
  703. if (!copy_from_user(&params, user_params))
  704. return -EFAULT;
  705. if (params.arguments.length > ARG_MAX || params.environment.length > ARG_MAX)
  706. return -E2BIG;
  707. String path;
  708. {
  709. auto path_arg = get_syscall_path_argument(params.path);
  710. if (path_arg.is_error())
  711. return path_arg.error();
  712. path = path_arg.value();
  713. }
  714. auto copy_user_strings = [](const auto& list, auto& output) {
  715. if (!list.length)
  716. return true;
  717. Checked size = sizeof(list.strings);
  718. size *= list.length;
  719. if (size.has_overflow())
  720. return false;
  721. Vector<Syscall::StringArgument, 32> strings;
  722. strings.resize(list.length);
  723. if (!copy_from_user(strings.data(), list.strings, list.length * sizeof(Syscall::StringArgument)))
  724. return false;
  725. for (size_t i = 0; i < list.length; ++i) {
  726. auto string = copy_string_from_user(strings[i]);
  727. if (string.is_null())
  728. return false;
  729. output.append(move(string));
  730. }
  731. return true;
  732. };
  733. Vector<String> arguments;
  734. if (!copy_user_strings(params.arguments, arguments))
  735. return -EFAULT;
  736. Vector<String> environment;
  737. if (!copy_user_strings(params.environment, environment))
  738. return -EFAULT;
  739. int rc = exec(move(path), move(arguments), move(environment));
  740. ASSERT(rc < 0); // We should never continue after a successful exec!
  741. return rc;
  742. }
  743. }