aboutsummaryrefslogtreecommitdiffstats
path: root/git-protection.c
blob: 6084c7441070d22dc584439fc1765e611ad19006 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include <sys/mount.h>
#include <sched.h>
#include <libsimple.h>
#include <libsimple-arg.h>

NUSAGE(125, "utility [argument] ...");


int
main(int argc, char *argv[])
{
	int fds[2], status;
	pid_t pid;
	char *dir = NULL;
	size_t dirsize = 0;
	size_t dirlen = 0;
	ssize_t r;

	libsimple_default_failure_exit = 125;

	ARGBEGIN {
	default:
		usage();
	} ARGEND;

	if (!argc)
		usage();

	if (unshare(CLONE_NEWNS))
		eprintf("unshare CLONE_NEWNS:");
	if (mount("none", "/", NULL, MS_REC | MS_SLAVE, NULL))
		eprintf("mount none / NULL MS_REC|MS_SLAVE NULL:");

	if (pipe(fds))
		eprintf("pipe:");
	pid = fork();
	if (pid < 0)
		eprintf("fork:");
	if (pid == 0) {
		if (setegid(getgid()))
			eprintf("setegid <real group>:");
		if (seteuid(getuid()))
			eprintf("seteuid <real user>:");

		close(fds[0]);
		if (fds[1] != STDOUT_FILENO) {
			if (dup2(fds[1], STDOUT_FILENO) != STDOUT_FILENO)
				eprintf("dup2 <pipe> STDOUT_FILENO:");
			close(fds[1]);
		}
		execlp(GIT_PATH, "git", "rev-parse", "--show-toplevel", NULL);
		eprintf("execlp %s:", GIT_PATH);
	}
	close(fds[1]);
	for (;;) {
		if (dirlen == dirsize)
			dir = erealloc(dir, dirsize += 512u);
		r = read(fds[0], &dir[dirlen], dirsize - dirlen);
		if (r <= 0) {
			if (!r)
				break;
			if (errno == EINTR)
				continue;
			eprintf("read <pipe>:");
		}
		dirlen += (size_t)r;
		if (!dirlen || dir[--dirlen] != '\n')
			eprintf("received invalid output from `git rev-parse --show-toplevel`");
	}
	close(fds[0]);
	if (waitpid(pid, &status, 0) != pid)
		eprintf("waitpid <subprocess>:");
	if (status) {
		if (WIFSIGNALED(status))
			eprintf("subprocess git was killed by signal %i\n", WTERMSIG(status));
		exit(libsimple_default_failure_exit);
	}
	if (dirsize - dirlen < sizeof("/.git"))
		dir = erealloc(dir, dirlen + sizeof("/.git"));
	stpcpy(&dir[dirlen], "/.git");

	if (mount(dir, dir, NULL, MS_BIND, NULL))
		eprintf("mount %s %s NULL MS_BIND NULL:", dir, dir);
	if (mount("none", dir, NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL))
		eprintf("mount none %s NULL MS_BIND|MS_REMOUNT|MS_RDONLY NULL:", dir);

	free(dir);

	if (setegid(getgid()))
		eprintf("setegid <real group>:");
	if (seteuid(getuid()))
		eprintf("seteuid <real user>:");

	execvp(argv[0], argv);
	enprintf(errno == ENOENT ? 127 : 126, "execvp %s:", argv[0]);
	return 0;
}