~dricottone/huttese-apk

ref: b72ee2b964ef45b6a8b14c7adf46f61218a4ffae huttese-apk/sr.ht/qemu-minimal-static/qemu-chroot.c -rw-r--r-- 819 bytes
b72ee2b9 — Drew DeVault prometheus: upgrade to 2.17.1 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;
}