~dricottone/huttese-apk

ref: 58f3e035c73c01c6fa5853fff0fd13686cb7b15a huttese-apk/sr.ht/qemu-minimal-static/qemu-chroot.c -rw-r--r-- 819 bytes
58f3e035 — Drew DeVault Add LICENSE (BSD) 4 years ago
                                                                                
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
#define _XOPEN_SOURCE
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>

int chroot(char *path);

void check(int p, char *ctx) {
	if (p) {
		return;
	}
	fprintf(stderr, "%s: %s\n", ctx, strerror(errno));
	exit(1);
}

int main(int argc, char *argv[]) {
	struct passwd *nobody = getpwnam("nobody");
	check(nobody != NULL, "getpwnam");

	int r;
	r = chroot("/usr/lib/qemu-minimal-static");
	check(r == 0, "chroot");

	r = setgid(nobody->pw_gid);
	check(r == 0, "setgid");
	r = setegid(nobody->pw_gid);
	check(r == 0, "setegid");
	r = setuid(nobody->pw_uid);
	check(r == 0, "setuid");
	r = seteuid(nobody->pw_uid);
	check(r == 0, "seteuid");

	r = execv(argv[1], &argv[1]);
	check(r == 0, "execv");
	return 1;
}