module.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2018-2020, 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 <Kernel/FileSystem/FileDescription.h>
  27. #include <Kernel/FileSystem/VirtualFileSystem.h>
  28. #include <Kernel/KSyms.h>
  29. #include <Kernel/Module.h>
  30. #include <Kernel/Process.h>
  31. #include <LibELF/Loader.h>
  32. namespace Kernel {
  33. extern HashMap<String, OwnPtr<Module>>* g_modules;
  34. int Process::sys$module_load(Userspace<const char*> user_path, size_t path_length)
  35. {
  36. if (!is_superuser())
  37. return -EPERM;
  38. REQUIRE_NO_PROMISES;
  39. auto path = get_syscall_path_argument(user_path, path_length);
  40. if (path.is_error())
  41. return path.error();
  42. auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
  43. if (description_or_error.is_error())
  44. return description_or_error.error();
  45. auto& description = description_or_error.value();
  46. auto payload_or_error = description->read_entire_file();
  47. if (payload_or_error.is_error())
  48. return payload_or_error.error();
  49. auto payload = payload_or_error.value();
  50. auto storage = KBuffer::create_with_size(payload.size());
  51. memcpy(storage.data(), payload.data(), payload.size());
  52. auto elf_image = make<ELF::Image>(storage.data(), storage.size());
  53. if (!elf_image->parse())
  54. return -ENOEXEC;
  55. HashMap<String, u8*> section_storage_by_name;
  56. auto module = make<Module>();
  57. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  58. if (!section.size())
  59. return IterationDecision::Continue;
  60. auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  61. section_storage_by_name.set(section.name(), section_storage.data());
  62. module->sections.append(move(section_storage));
  63. return IterationDecision::Continue;
  64. });
  65. bool missing_symbols = false;
  66. elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
  67. if (!section.size())
  68. return IterationDecision::Continue;
  69. auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
  70. ASSERT(section_storage);
  71. section.relocations().for_each_relocation([&](const ELF::Image::Relocation& relocation) {
  72. auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
  73. switch (relocation.type()) {
  74. case R_386_PC32: {
  75. // PC-relative relocation
  76. dbg() << "PC-relative relocation: " << relocation.symbol().name();
  77. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  78. if (symbol_address == 0)
  79. missing_symbols = true;
  80. dbg() << " Symbol address: " << (void*)symbol_address;
  81. ptrdiff_t relative_offset = (char*)symbol_address - ((char*)&patch_ptr + 4);
  82. patch_ptr = relative_offset;
  83. break;
  84. }
  85. case R_386_32: // Absolute relocation
  86. dbg() << "Absolute relocation: '" << relocation.symbol().name() << "' value:" << relocation.symbol().value() << ", index:" << relocation.symbol_index();
  87. if (relocation.symbol().bind() == STB_LOCAL) {
  88. auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
  89. ASSERT(section_storage_containing_symbol);
  90. u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
  91. if (symbol_address == 0)
  92. missing_symbols = true;
  93. dbg() << " Symbol address: " << (void*)symbol_address;
  94. patch_ptr += symbol_address;
  95. } else if (relocation.symbol().bind() == STB_GLOBAL) {
  96. u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
  97. if (symbol_address == 0)
  98. missing_symbols = true;
  99. dbg() << " Symbol address: " << (void*)symbol_address;
  100. patch_ptr += symbol_address;
  101. } else {
  102. ASSERT_NOT_REACHED();
  103. }
  104. break;
  105. }
  106. return IterationDecision::Continue;
  107. });
  108. return IterationDecision::Continue;
  109. });
  110. if (missing_symbols)
  111. return -EINVAL;
  112. auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
  113. if (!text_base) {
  114. dbg() << "No .text section found in module!";
  115. return -EINVAL;
  116. }
  117. elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
  118. dbg() << " - " << symbol.type() << " '" << symbol.name() << "' @ " << (void*)symbol.value() << ", size=" << symbol.size();
  119. if (symbol.name() == "module_init") {
  120. module->module_init = (ModuleInitPtr)(text_base + symbol.value());
  121. } else if (symbol.name() == "module_fini") {
  122. module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
  123. } else if (symbol.name() == "module_name") {
  124. const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
  125. if (storage)
  126. module->name = String((const char*)(storage + symbol.value()));
  127. }
  128. return IterationDecision::Continue;
  129. });
  130. if (!module->module_init)
  131. return -EINVAL;
  132. if (g_modules->contains(module->name)) {
  133. dbg() << "a module with the name " << module->name << " is already loaded; please unload it first";
  134. return -EEXIST;
  135. }
  136. module->module_init();
  137. auto name = module->name;
  138. g_modules->set(name, move(module));
  139. return 0;
  140. }
  141. int Process::sys$module_unload(Userspace<const char*> user_name, size_t name_length)
  142. {
  143. if (!is_superuser())
  144. return -EPERM;
  145. REQUIRE_NO_PROMISES;
  146. auto module_name = validate_and_copy_string_from_user(user_name, name_length);
  147. if (module_name.is_null())
  148. return -EFAULT;
  149. auto it = g_modules->find(module_name);
  150. if (it == g_modules->end())
  151. return -ENOENT;
  152. if (it->value->module_fini)
  153. it->value->module_fini();
  154. g_modules->remove(it);
  155. return 0;
  156. }
  157. }