aboutsummaryrefslogtreecommitdiffstats
path: root/git-protection.c
blob: bc201450e612c7032dffe0652fa54b5764744c6e (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
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
/* 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] ...");


static char *
getdir(const char *option, const char *suffix)
{
	int fds[2], status;
	pid_t pid;
	char *dir = NULL;
	size_t dirsize = 0;
	size_t dirlen = 0;
	ssize_t r;

	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", option, 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 %s`", option);
	}
	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 < strlen(suffix) + 1u)
		dir = erealloc(dir, dirlen + strlen(suffix) + 1u);
	stpcpy(&dir[dirlen], suffix);

	return dir;
}


static void
protect(const char *option, const char *suffix, char **dirs, size_t *ndirsp)
{
	char *dir = getdir(option, suffix);
	size_t i;

	if (!*dir) {
	dont_protect:
		free(dir);
		return;
	}

	for (i = 0u; i < *ndirsp; i++)
		if (!strcmp(dir, dirs[i]))
			goto dont_protect;

	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);

	dirs[(*ndirsp)++] = dir;
}


int
main(int argc, char *argv[])
{
	char *dirs[4];
	size_t ndirs = 0u;
	size_t i;

	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:");

	protect("--show-toplevel", "/.git", dirs, &ndirs);
	protect("--git-common-dir", "", dirs, &ndirs);
	protect("--show-superproject-working-tree", "", dirs, &ndirs);
	protect("--shared-index-path", "", dirs, &ndirs);

	for (i = 0u; i < ndirs; i++)
		free(dirs[i]);

	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;
}