-
Notifications
You must be signed in to change notification settings - Fork 4
/
inmem-exec.cc
209 lines (174 loc) · 6.11 KB
/
inmem-exec.cc
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <cassert>
#include <fcntl.h>
#include <iostream>
#include <libelf.h>
#include <algorithm>
#include <array>
#include <bit>
#include <memory>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#include <sys/mman.h>
template<size_t>
struct traits;
template<> struct traits<32> {
static constexpr int elfclass = ELFCLASS32;
using Word = Elf32_Word;
using Addr = Elf32_Addr;
static constexpr int machine = EM_386;
static auto newehdr(Elf* elf) { return elf32_newehdr(elf); }
static auto newphdr(Elf* elf, size_t cnt) { return elf32_newphdr(elf, cnt); }
static auto getshdr(Elf_Scn* scn) { return elf32_getshdr(scn); }
};
template<> struct traits<64> {
static constexpr int elfclass = ELFCLASS64;
using Word = Elf64_Word;
using Addr = Elf64_Addr;
static constexpr int machine = EM_X86_64;
static auto newehdr(Elf* elf) { return elf64_newehdr(elf); }
static auto newphdr(Elf* elf, size_t cnt) { return elf64_newphdr(elf, cnt); }
static auto getshdr(Elf_Scn* scn) { return elf64_getshdr(scn); }
};
std::array codebuf {
'\xb8', '\x01', '\x00', '\x00', '\x00', // mov $SYS_write,%eax
'\xbf', '\x01', '\x00', '\x00', '\x00', // mov $0x1,%edi
'\x48', '\x8d', '\x34', '\x25', '\x00', '\x00', '\x00', '\x00', // lea 0x0,%rsi
'\xba', '\x0c', '\x00', '\x00', '\x00', // mov $0xc,%edx
'\x0f', '\x05', // syscall
'\xb8', '\xe7', '\x00', '\x00', '\x00', // mov $SYS_exit_group,%eax
'\xbf', '\x00', '\x00', '\x00', '\x00', // mov $0x0,%edi
'\x0f', '\x05', // syscall
};
std::array rodatabuf {
'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\n'
};
const std::unordered_map<std::string,std::tuple<std::string,size_t>> symbols {
{ "hello", { ".rodata", 0 } }
};
enum struct reloc_type {
abs4
};
std::vector<std::tuple<std::string,std::string,size_t,reloc_type>> relocations {
{ "hello", ".text", 14, reloc_type::abs4 }
};
std::unordered_map<std::string,size_t> sectionidx;
struct shstrtab_type {
auto data() { return mem.data(); }
auto size() const { return mem.size(); }
auto push(const std::string& name) {
auto res = mem.size();
std::copy(std::begin(name), std::end(name), std::back_inserter(mem));
mem.push_back('\0');
return res;
}
private:
std::vector<char> mem { '\0' };
} shstrbuf;
template<typename Traits, typename Buf>
auto newscn(Elf* elf, const std::string& name, typename Traits::Word type, auto flags, Buf& buf, size_t align)
{
auto scn = elf_newscn(elf);
auto shdr = Traits::getshdr(scn);
shdr->sh_name = shstrbuf.push(name);
shdr->sh_type = type;
shdr->sh_flags = flags;
auto data = elf_newdata(scn);
data->d_buf = buf.data();
data->d_type = ELF_T_BYTE;
data->d_version = EV_CURRENT;
data->d_size = buf.size();
data->d_off = 0;
data->d_align = align;
sectionidx[name] = elf_ndxscn(scn);
return std::make_tuple(scn, shdr, data);
}
template<typename Traits>
void apply_relocations(Elf* elf)
{
for (auto [symname, scnname, off, type] : relocations) {
const auto& sym = symbols.at(symname);
auto defscnidx = sectionidx[std::get<std::string>(sym)];
auto defscn = elf_getscn(elf, defscnidx);
auto defshdr = Traits::getshdr(defscn);
auto defval = defshdr->sh_addr + std::get<size_t>(sym);
auto refscnidx = sectionidx[scnname];
auto refscn = elf_getscn(elf, refscnidx);
auto refdata = elf_getdata(refscn, nullptr);
while (off >= refdata->d_size) {
off -= refdata->d_size;
refdata = elf_getdata(refscn, refdata);
}
switch (type) {
case reloc_type::abs4:
{
assert(off + 4 <= refdata->d_size);
auto buf = (unsigned char*) refdata->d_buf;
buf[off] = defval & 0xff;
buf[off + 1] = (defval >> 8) & 0xff;
buf[off + 2] = (defval >> 16) & 0xff;
buf[off + 3] = (defval >> 24) & 0xff;
}
break;
default:
__builtin_unreachable();
}
}
}
template<size_t N>
void genelf(int fd)
{
using E = traits<N>;
elf_version(EV_CURRENT);
Elf* elf = elf_begin(fd, ELF_C_WRITE, nullptr);
auto ehdr = E::newehdr(elf);
std::copy_n(ELFMAG, SELFMAG, ehdr->e_ident);
ehdr->e_ident[EI_CLASS] = E::elfclass;
ehdr->e_ident[EI_DATA] = std::endian::native == std::endian::little ? ELFDATA2LSB : ELFDATA2MSB;
ehdr->e_ident[EI_VERSION] = EV_CURRENT;
ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
ehdr->e_ident[EI_ABIVERSION] = 0;
ehdr->e_type = ET_EXEC;
ehdr->e_machine = E::machine;
ehdr->e_version = EV_CURRENT;
enum struct phdridx : size_t {
code,
// Keep it last
num
};
auto phdr = E::newphdr(elf, std::underlying_type_t<phdridx>(phdridx::num));
auto [codescn, codeshdr, codedata] = newscn<E>(elf, ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, codebuf, 16);
auto [rodatascn, rodatashdr, rodatadata] = newscn<E>(elf, ".rodata", SHT_PROGBITS, SHF_ALLOC, rodatabuf, 16);
// Keep as last added section.
auto [shstrscn, shstrshdr, shstrdata] = newscn<E>(elf, ".shstrtab", SHT_STRTAB, 0, shstrbuf, 1);
elf_update(elf, ELF_C_NULL);
const typename E::Addr loadaddr = 0x40000;
codeshdr->sh_addr = loadaddr + codeshdr->sh_offset;
rodatashdr->sh_addr = loadaddr + rodatashdr->sh_offset;
apply_relocations<E>(elf);
ehdr->e_shstrndx = elf_ndxscn(shstrscn);
ehdr->e_entry = codeshdr->sh_addr;
const auto codeidx = std::underlying_type_t<phdridx>(phdridx::code);
phdr[codeidx].p_type = PT_LOAD;
phdr[codeidx].p_flags = PF_R|PF_X;
phdr[codeidx].p_offset = 0;
phdr[codeidx].p_vaddr = loadaddr;
phdr[codeidx].p_paddr = phdr[codeidx].p_vaddr;
phdr[codeidx].p_filesz = rodatashdr->sh_offset + rodatashdr->sh_size;
phdr[codeidx].p_memsz = phdr[codeidx].p_filesz;
phdr[codeidx].p_align = sysconf(_SC_PAGESIZE);
elf_update(elf, ELF_C_WRITE);
elf_end(elf);
}
int main(int argc, char* argv[])
{
char* newargv[] {
(char*) "test",
nullptr
};
int fd = memfd_create(newargv[0], MFD_CLOEXEC);
genelf<64>(fd);
fexecve(fd, newargv, environ);
// We should never get here.
return 1;
}