summaryrefslogtreecommitdiffstats
path: root/run16.c
blob: 7a81de87c2d01edf78dd5f23660b10a9d4195429 (plain)
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
/*
 * Wrapper to run a 16-bit raw binary on Linux/i386
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <syscall.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/types.h>
#include <asm/ldt.h>
#include <string.h>
#include <asm/unistd.h>
#include <sys/mman.h>

static unsigned char toybox[65536] __attribute__((aligned(4096)));

#define LOAD_ADDR	0x1000

static void setup_ldt(void)
{
	const struct user_desc code16_desc = {
		.entry_number	 = 0,
		.base_addr	 = (unsigned long)toybox,
		.limit		 = 0xffff,
		.seg_32bit	 = 0,
		.contents	 = 2, /* Code, R/X */
		.read_exec_only	 = 0,
		.limit_in_pages	 = 0,
		.seg_not_present = 0,
		.useable	 = 0
	};
	const struct user_desc data16_desc = {
		.entry_number	 = 1,
		.base_addr	 = (unsigned long)toybox,
		.limit		 = 0xffff,
		.seg_32bit	 = 0,
		.contents	 = 0, /* Data, R/W */
		.read_exec_only	 = 0,
		.limit_in_pages	 = 0,
		.seg_not_present = 0,
		.useable	 = 0
	};

	syscall(SYS_modify_ldt, 1, &code16_desc, sizeof code16_desc);
	syscall(SYS_modify_ldt, 1, &data16_desc, sizeof data16_desc);
}

static void load_file(const char *file, unsigned int trampoline_addr)
{
	FILE *f = fopen(file, "rb");

	if (!f) {
		fprintf(stderr, "run16: could not open: %s\n", file);
		exit(127);
	}

	fread(toybox + LOAD_ADDR, 1, trampoline_addr - LOAD_ADDR, f);
	fclose(f);
}

static void jump16(uint32_t offset)
{
	struct far_ptr {
		uint32_t offset;
		uint16_t segment;
	} target;

	target.offset = offset;
	target.segment = 7;

	asm volatile("ljmpl *%0" : : "m" (target),
		     "a" (LOAD_ADDR), "d" (offset));
}

static void run(const char *file)
{
	unsigned int trampoline_len;
	const void *trampoline;
	unsigned char *entrypoint;
	uint32_t offset;

	setup_ldt();

	/* No points for style */
	asm volatile(
		" .pushsection \".rodata\",\"a\"\n"
		" .code16\n"
		"1:\n"
		" movw $15,%%cx\n"
		" movw %%cx,%%ss\n"
		" movl %%edx,%%esp\n"
		" movw %%cx,%%ds\n"
		" movw %%cx,%%es\n"
		" movw %%cx,%%fs\n"
		" movw %%cx,%%gs\n"
		" calll *%%eax\n"
		" movzbl %%al,%%ebx\n"
		" movl %2,%%eax\n"
		" int $0x80\n"
		"2:\n"
		" .code32\n"
		" .popsection\n"
		" movl $1b,%0\n"
		" movl $(2b - 1b),%1\n"
		: "=r" (trampoline), "=r" (trampoline_len)
		: "i" (__NR_exit));

	offset = (sizeof toybox - trampoline_len - 4) & ~15;
	entrypoint = toybox + offset;
	memcpy(entrypoint, trampoline, trampoline_len);

	/* Save the segment base address in the highest dword */
	*(uint32_t *)(toybox + sizeof toybox - 4) = (uint32_t)toybox;

	load_file(file, offset);

	/* Catch null pointer references */
	mprotect(toybox, LOAD_ADDR, PROT_NONE);

	/* The rest is fully permissive */
	mprotect(toybox + LOAD_ADDR, sizeof toybox - LOAD_ADDR,
		 PROT_READ|PROT_WRITE|PROT_EXEC);

	jump16(offset);
	abort();		/* Does not return */
}

int main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s filename\n", argv[0]);
		return 127;
	}

	run(argv[1]);
	return 127;
}