~dricottone/huttese-apk

ref: 2f15413f0d65dc26e67f525a9f4ba6538dfb8a11 huttese-apk/sr.ht/qemu-minimal-static/qemu-chroot.c -rw-r--r-- 819 bytes
2f15413f — Drew DeVault qemu-minimal-static: new aport 4 years ago
                                                                                
2f15413f Drew DeVault
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;
}