-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathloader-transfer.c
More file actions
116 lines (106 loc) · 3.91 KB
/
loader-transfer.c
File metadata and controls
116 lines (106 loc) · 3.91 KB
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
/* SPDX-License-Identifier: MIT */
#include <string.h>
#include "kbox/compiler.h"
#include "loader-transfer.h"
int kbox_loader_prepare_transfer(const struct kbox_loader_handoff *handoff,
struct kbox_loader_transfer_state *state)
{
if (!handoff || !state)
return -1;
if (handoff->entry_map_end <= handoff->entry_map_start ||
handoff->stack_map_end <= handoff->stack_map_start) {
return -1;
}
if (handoff->entry.pc < handoff->entry_map_start ||
handoff->entry.pc >= handoff->entry_map_end) {
return -1;
}
if (handoff->entry.sp < handoff->stack_map_start ||
handoff->entry.sp >= handoff->stack_map_end) {
return -1;
}
if ((handoff->entry.sp & 0xfu) != 0)
return -1;
memset(state, 0, sizeof(*state));
state->arch = handoff->entry.arch;
state->pc = handoff->entry.pc;
state->sp = handoff->entry.sp;
memcpy(state->regs, handoff->entry.regs, sizeof(state->regs));
state->entry_map_start = handoff->entry_map_start;
state->entry_map_end = handoff->entry_map_end;
state->stack_map_start = handoff->stack_map_start;
state->stack_map_end = handoff->stack_map_end;
return 0;
}
/* The transfer boundary must not run sanitizer/runtime callbacks or stack
* protector epilogues. It switches to the guest stack and branches into
* guest code after the exec-range seccomp filter is active.
*/
__attribute__((noreturn)) __attribute__((no_stack_protector))
#if KBOX_HAS_ASAN
__attribute__((no_sanitize("address")))
#endif
__attribute__((no_sanitize("undefined"))) void
kbox_loader_transfer_to_guest(const struct kbox_loader_transfer_state *state)
{
if (!state)
__builtin_trap();
#if defined(__x86_64__)
if (state->arch != KBOX_LOADER_ENTRY_ARCH_X86_64)
__builtin_trap();
register uint64_t rdi __asm__("rdi") = state->regs[0];
register uint64_t rsi __asm__("rsi") = state->regs[1];
register uint64_t rdx __asm__("rdx") = state->regs[2];
register uint64_t r10 __asm__("r10") = state->regs[3];
register uint64_t r8 __asm__("r8") = state->regs[4];
register uint64_t r9 __asm__("r9") = state->regs[5];
uint64_t sp = state->sp;
uint64_t pc = state->pc;
__asm__ volatile(
"mov %0, %%rsp\n\t"
"jmp *%1\n\t"
:
: "r"(sp), "r"(pc), "r"(rdi), "r"(rsi), "r"(rdx), "r"(r10), "r"(r8),
"r"(r9)
: "memory");
#elif defined(__aarch64__)
if (state->arch != KBOX_LOADER_ENTRY_ARCH_AARCH64)
__builtin_trap();
register uint64_t x0 __asm__("x0") = state->regs[0];
register uint64_t x1 __asm__("x1") = state->regs[1];
register uint64_t x2 __asm__("x2") = state->regs[2];
register uint64_t x3 __asm__("x3") = state->regs[3];
register uint64_t x4 __asm__("x4") = state->regs[4];
register uint64_t x5 __asm__("x5") = state->regs[5];
register uint64_t x16 __asm__("x16") = state->pc;
uint64_t sp = state->sp;
__asm__ volatile(
"mov sp, %0\n\t"
"br x16\n\t"
:
: "r"(sp), "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5),
"r"(x16)
: "memory");
#elif defined(__riscv) && (__riscv_xlen == 64)
if (state->arch != KBOX_LOADER_ENTRY_ARCH_RISCV64)
__builtin_trap();
register uint64_t a0 __asm__("a0") = state->regs[0];
register uint64_t a1 __asm__("a1") = state->regs[1];
register uint64_t a2 __asm__("a2") = state->regs[2];
register uint64_t a3 __asm__("a3") = state->regs[3];
register uint64_t a4 __asm__("a4") = state->regs[4];
register uint64_t a5 __asm__("a5") = state->regs[5];
register uint64_t t0 __asm__("t0") = state->pc;
uint64_t sp = state->sp;
__asm__ volatile(
"mv sp, %0\n\t"
"jr t0\n\t"
:
: "r"(sp), "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(t0)
: "memory");
#else
(void) state;
__builtin_trap();
#endif
__builtin_unreachable();
}