2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/KSyms.h>
|
|
|
|
#include <Kernel/Module.h>
|
|
|
|
#include <Kernel/Process.h>
|
2020-12-25 01:14:56 +00:00
|
|
|
#include <LibELF/Image.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
extern HashMap<String, OwnPtr<Module>>* g_modules;
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$module_load(Userspace<const char*> user_path, size_t path_length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!is_superuser())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
2021-09-06 18:23:08 +00:00
|
|
|
auto path = TRY(get_syscall_path_argument(user_path, path_length));
|
|
|
|
auto description = TRY(VirtualFileSystem::the().open(path->view(), O_RDONLY, 0, current_directory()));
|
|
|
|
auto payload = TRY(description->read_entire_file());
|
|
|
|
|
2021-09-08 16:29:52 +00:00
|
|
|
auto storage = TRY(KBuffer::try_create_with_bytes(payload->bytes()));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-08-01 11:01:05 +00:00
|
|
|
auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
|
2021-05-13 06:36:25 +00:00
|
|
|
if (!elf_image)
|
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!elf_image->parse())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOEXEC;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
HashMap<String, u8*> section_storage_by_name;
|
|
|
|
|
2021-06-20 08:21:16 +00:00
|
|
|
auto module = try_make<Module>();
|
2021-05-13 06:36:25 +00:00
|
|
|
if (!module)
|
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-07 13:36:39 +00:00
|
|
|
KResult section_loading_result = KSuccess;
|
2020-07-30 21:38:15 +00:00
|
|
|
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
|
2021-09-07 13:36:39 +00:00
|
|
|
if (!section.size() || !section_loading_result.is_error())
|
|
|
|
return;
|
2021-09-07 14:22:29 +00:00
|
|
|
auto section_storage_or_error = KBuffer::try_create_with_bytes(ReadonlyBytes { section.raw_data(), section.size() }, Memory::Region::Access::ReadWriteExecute);
|
2021-09-07 13:36:39 +00:00
|
|
|
if (section_storage_or_error.is_error()) {
|
|
|
|
section_loading_result = section_storage_or_error.error();
|
2021-05-16 09:36:52 +00:00
|
|
|
return;
|
2021-09-07 13:36:39 +00:00
|
|
|
}
|
|
|
|
auto section_storage = section_storage_or_error.release_value();
|
|
|
|
section_storage_by_name.set(section.name(), section_storage->data());
|
2020-07-30 21:38:15 +00:00
|
|
|
module->sections.append(move(section_storage));
|
|
|
|
});
|
2021-09-07 13:36:39 +00:00
|
|
|
if (section_loading_result.is_error())
|
|
|
|
return section_loading_result;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
bool missing_symbols = false;
|
|
|
|
|
|
|
|
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
|
|
|
|
if (!section.size())
|
2021-05-16 09:36:52 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(section_storage);
|
2021-05-14 22:13:44 +00:00
|
|
|
auto relocations = section.relocations();
|
|
|
|
VERIFY(relocations.has_value());
|
|
|
|
relocations->for_each_relocation([&](const ELF::Image::Relocation& relocation) {
|
2020-07-30 21:38:15 +00:00
|
|
|
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
|
|
|
|
switch (relocation.type()) {
|
|
|
|
case R_386_PC32: {
|
|
|
|
// PC-relative relocation
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln("PC-relative relocation: {}", relocation.symbol().name());
|
2021-02-25 15:18:36 +00:00
|
|
|
auto symbol_address = address_for_kernel_symbol(relocation.symbol().name());
|
2020-07-30 21:38:15 +00:00
|
|
|
if (symbol_address == 0)
|
|
|
|
missing_symbols = true;
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln(" Symbol address: {:p}", symbol_address);
|
2021-02-25 15:18:36 +00:00
|
|
|
ptrdiff_t relative_offset = (FlatPtr)symbol_address - ((FlatPtr)&patch_ptr + 4);
|
2020-07-30 21:38:15 +00:00
|
|
|
patch_ptr = relative_offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_32: // Absolute relocation
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln("Absolute relocation: '{}' value={}, index={}", relocation.symbol().name(), relocation.symbol().value(), relocation.symbol_index());
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (relocation.symbol().bind() == STB_LOCAL) {
|
|
|
|
auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(section_storage_containing_symbol);
|
2020-07-30 21:38:15 +00:00
|
|
|
u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
|
|
|
|
if (symbol_address == 0)
|
|
|
|
missing_symbols = true;
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln(" Symbol address: {:p}", symbol_address);
|
2020-07-30 21:38:15 +00:00
|
|
|
patch_ptr += symbol_address;
|
|
|
|
} else if (relocation.symbol().bind() == STB_GLOBAL) {
|
|
|
|
u32 symbol_address = address_for_kernel_symbol(relocation.symbol().name());
|
|
|
|
if (symbol_address == 0)
|
|
|
|
missing_symbols = true;
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln(" Symbol address: {:p}", symbol_address);
|
2020-07-30 21:38:15 +00:00
|
|
|
patch_ptr += symbol_address;
|
|
|
|
} else {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missing_symbols)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
|
|
|
|
if (!text_base) {
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("No .text section found in module!");
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln(" - {} '{}' @ {:p}, size={}", symbol.type(), symbol.name(), symbol.value(), symbol.size());
|
2020-07-30 21:38:15 +00:00
|
|
|
if (symbol.name() == "module_init") {
|
|
|
|
module->module_init = (ModuleInitPtr)(text_base + symbol.value());
|
|
|
|
} else if (symbol.name() == "module_fini") {
|
|
|
|
module->module_fini = (ModuleFiniPtr)(text_base + symbol.value());
|
|
|
|
} else if (symbol.name() == "module_name") {
|
|
|
|
const u8* storage = section_storage_by_name.get(symbol.section().name()).value_or(nullptr);
|
|
|
|
if (storage)
|
|
|
|
module->name = String((const char*)(storage + symbol.value()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!module->module_init)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (g_modules->contains(module->name)) {
|
2021-01-10 14:43:09 +00:00
|
|
|
dbgln("a module with the name {} is already loaded; please unload it first", module->name);
|
2021-03-01 12:49:16 +00:00
|
|
|
return EEXIST;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module->module_init();
|
|
|
|
|
|
|
|
auto name = module->name;
|
|
|
|
g_modules->set(name, move(module));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$module_unload(Userspace<const char*> user_name, size_t name_length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
if (!is_superuser())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
2021-09-06 18:23:08 +00:00
|
|
|
auto module_name = TRY(try_copy_kstring_from_user(user_name, name_length));
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-09-06 18:23:08 +00:00
|
|
|
auto it = g_modules->find(module_name->view());
|
2020-07-30 21:38:15 +00:00
|
|
|
if (it == g_modules->end())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOENT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
if (it->value->module_fini)
|
|
|
|
it->value->module_fini();
|
|
|
|
|
|
|
|
g_modules->remove(it);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|