2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#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;
|
|
|
|
|
2020-08-09 22:36:41 +00:00
|
|
|
int Process::sys$module_load(Userspace<const char*> user_path, size_t path_length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
if (!is_superuser())
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
|
|
|
auto path = get_syscall_path_argument(user_path, path_length);
|
|
|
|
if (path.is_error())
|
|
|
|
return path.error();
|
|
|
|
auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
|
|
|
|
if (description_or_error.is_error())
|
|
|
|
return description_or_error.error();
|
|
|
|
auto& description = description_or_error.value();
|
|
|
|
auto payload_or_error = description->read_entire_file();
|
|
|
|
if (payload_or_error.is_error())
|
|
|
|
return payload_or_error.error();
|
|
|
|
|
2020-12-18 13:10:10 +00:00
|
|
|
auto& payload = *payload_or_error.value();
|
2020-07-30 21:38:15 +00:00
|
|
|
auto storage = KBuffer::create_with_size(payload.size());
|
|
|
|
memcpy(storage.data(), payload.data(), payload.size());
|
|
|
|
|
|
|
|
auto elf_image = make<ELF::Image>(storage.data(), storage.size());
|
|
|
|
if (!elf_image->parse())
|
|
|
|
return -ENOEXEC;
|
|
|
|
|
|
|
|
HashMap<String, u8*> section_storage_by_name;
|
|
|
|
|
|
|
|
auto module = make<Module>();
|
|
|
|
|
|
|
|
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
|
|
|
|
if (!section.size())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Region::Access::Read | Region::Access::Write | Region::Access::Execute);
|
|
|
|
section_storage_by_name.set(section.name(), section_storage.data());
|
|
|
|
module->sections.append(move(section_storage));
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
bool missing_symbols = false;
|
|
|
|
|
|
|
|
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
|
|
|
|
if (!section.size())
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(section_storage);
|
2020-07-30 21:38:15 +00:00
|
|
|
section.relocations().for_each_relocation([&](const ELF::Image::Relocation& relocation) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missing_symbols)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
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!");
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!module->module_init)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
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);
|
2020-07-30 21:38:15 +00:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
module->module_init();
|
|
|
|
|
|
|
|
auto name = module->name;
|
|
|
|
g_modules->set(name, move(module));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-09 22:37:45 +00:00
|
|
|
int Process::sys$module_unload(Userspace<const char*> user_name, size_t name_length)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
if (!is_superuser())
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
auto module_name = copy_string_from_user(user_name, name_length);
|
2020-07-30 21:38:15 +00:00
|
|
|
if (module_name.is_null())
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
auto it = g_modules->find(module_name);
|
|
|
|
if (it == g_modules->end())
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (it->value->module_fini)
|
|
|
|
it->value->module_fini();
|
|
|
|
|
|
|
|
g_modules->remove(it);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|