summaryrefslogtreecommitdiffstats
path: root/run16.c
blob: 518419ec7ee2cc9f580b53821035ffc88ad72e98 (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
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
/*
 * 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>

#include "include16/sys16.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));
}

#define ALIGN_UP(x,y) (((x) + (y) - 1) & ((y) - 1))

static void run(const char *file, char **argv)
{
	unsigned int trampoline_len;
	const void *trampoline;
	unsigned char *entrypoint;
	uint32_t offset;
	struct system_struct *SYS;
	char **argp;
	uint32_t argptr, argstr;

	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 %2,%%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 %3,%%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" (SYS_STRUCT_ADDR), "i" (__NR_exit));

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

	/* Set up the system structure */
	SYS = (struct system_struct *)(toybox + SYS_STRUCT_ADDR);
	SYS->seg_base = (unsigned int)toybox;
	for (argp = argv; argp; argp++)
		SYS->argc++;

	argptr = ALIGN_UP(SYS_STRUCT_ADDR + sizeof(struct system_struct), 4);
	argstr = argptr + (SYS->argc + 1)*sizeof(uint32_t);

	SYS->argv = argptr;

	for (argp = argv; argp; argp++) {
		int len = strlen(*argp)+1;

		if (argstr + len >= offset) {
			fprintf(stderr, "run16: %s: command line too long\n",
				file);
			exit(127);
		}

		*(uint32_t *)(toybox + argptr) = argstr;
		argptr += sizeof(uint32_t);
		memcpy(toybox + argstr, *argp, len);
		argstr += len;
	}

	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 [args...]\n", argv[0]);
		return 127;
	}

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