1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/KSyms.h>
#include <Kernel/Module.h>
#include <Kernel/Process.h>
#include <LibELF/Image.h>
namespace Kernel {
extern HashMap<String, OwnPtr<Module>>* g_modules;
KResultOr<FlatPtr> Process::sys$module_load(Userspace<const char*> user_path, size_t path_length)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
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 = VirtualFileSystem::the().open(path.value()->view(), 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();
auto& payload = *payload_or_error.value();
auto storage = KBuffer::try_create_with_size(payload.size());
if (!storage)
return ENOMEM;
memcpy(storage->data(), payload.data(), payload.size());
auto elf_image = try_make<ELF::Image>(storage->data(), storage->size());
if (!elf_image)
return ENOMEM;
if (!elf_image->parse())
return ENOEXEC;
HashMap<String, u8*> section_storage_by_name;
auto module = try_make<Module>();
if (!module)
return ENOMEM;
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
if (!section.size())
return;
auto section_storage = KBuffer::copy(section.raw_data(), section.size(), Memory::Region::Access::ReadWriteExecute);
section_storage_by_name.set(section.name(), section_storage.data());
module->sections.append(move(section_storage));
});
bool missing_symbols = false;
elf_image->for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
if (!section.size())
return;
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
VERIFY(section_storage);
auto relocations = section.relocations();
VERIFY(relocations.has_value());
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
dbgln("PC-relative relocation: {}", relocation.symbol().name());
auto symbol_address = address_for_kernel_symbol(relocation.symbol().name());
if (symbol_address == 0)
missing_symbols = true;
dbgln(" Symbol address: {:p}", symbol_address);
ptrdiff_t relative_offset = (FlatPtr)symbol_address - ((FlatPtr)&patch_ptr + 4);
patch_ptr = relative_offset;
break;
}
case R_386_32: // Absolute relocation
dbgln("Absolute relocation: '{}' value={}, index={}", relocation.symbol().name(), relocation.symbol().value(), relocation.symbol_index());
if (relocation.symbol().bind() == STB_LOCAL) {
auto* section_storage_containing_symbol = section_storage_by_name.get(relocation.symbol().section().name()).value_or(nullptr);
VERIFY(section_storage_containing_symbol);
u32 symbol_address = (ptrdiff_t)(section_storage_containing_symbol + relocation.symbol().value());
if (symbol_address == 0)
missing_symbols = true;
dbgln(" Symbol address: {:p}", symbol_address);
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;
dbgln(" Symbol address: {:p}", symbol_address);
patch_ptr += symbol_address;
} else {
VERIFY_NOT_REACHED();
}
break;
}
});
});
if (missing_symbols)
return EINVAL;
auto* text_base = section_storage_by_name.get(".text").value_or(nullptr);
if (!text_base) {
dbgln("No .text section found in module!");
return EINVAL;
}
elf_image->for_each_symbol([&](const ELF::Image::Symbol& symbol) {
dbgln(" - {} '{}' @ {:p}, size={}", symbol.type(), symbol.name(), symbol.value(), symbol.size());
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)
return EINVAL;
if (g_modules->contains(module->name)) {
dbgln("a module with the name {} is already loaded; please unload it first", module->name);
return EEXIST;
}
module->module_init();
auto name = module->name;
g_modules->set(name, move(module));
return 0;
}
KResultOr<FlatPtr> Process::sys$module_unload(Userspace<const char*> user_name, size_t name_length)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
if (!is_superuser())
return EPERM;
REQUIRE_NO_PROMISES;
auto module_name = copy_string_from_user(user_name, name_length);
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;
}
}
|