-
Notifications
You must be signed in to change notification settings - Fork 9
/
gentoo_mount.c
163 lines (137 loc) · 4.19 KB
/
gentoo_mount.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
static inline int xusleep(useconds_t usec) {
struct timespec waittime = { .tv_sec = usec / 1000000L, .tv_nsec = (usec % 1000000L) * 1000 };
return nanosleep(&waittime, NULL);
}
static inline int write_all(int fd, const void* buf, size_t count) {
while (count) {
ssize_t tmp;
errno = 0;
tmp = write(fd, buf, count);
if (tmp > 0) {
count -= tmp;
if (count)
buf = (const void*)((const char*)buf + tmp);
} else if (errno != EINTR && errno != EAGAIN)
return -1;
if (errno == EAGAIN)
xusleep(250000);
}
return 0;
}
static inline __attribute__((__format__(printf, 2, 3))) int err(int ec, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n%s\n", strerror(errno));
_exit(ec);
}
static inline __attribute__((__format__(printf, 2, 3))) int xasprintf(
char** strp, const char* fmt, ...) {
int ret;
va_list args;
va_start(args, fmt);
ret = vasprintf(&(*strp), fmt, args);
va_end(args);
if (ret < 0)
err(EXIT_FAILURE, "cannot allocate string");
return ret;
}
static void setgroups_deny() {
const char* file = "/proc/self/setgroups";
const char* cmd = "deny";
int fd = open(file, O_WRONLY);
if (fd < 0) {
if (errno == ENOENT)
return;
err(EXIT_FAILURE, "cannot open %s", file);
}
if (write_all(fd, cmd, strlen(cmd)))
err(EXIT_FAILURE, "write failed %s", file);
close(fd);
}
void map_id(const char* file, uint32_t from, uint32_t to) {
int fd = open(file, O_WRONLY);
if (fd < 0)
err(EXIT_FAILURE, "cannot open %s", file);
char* buf;
xasprintf(&buf, "%u %u 1", from, to);
if (write_all(fd, buf, strlen(buf)))
err(EXIT_FAILURE, "write failed %s", file);
free(buf);
close(fd);
};
int main(int argc, char* argv[]) {
uid_t real_euid = geteuid();
gid_t real_egid = getegid();
uid_t uid;
gid_t gid;
char* tmp_dir;
char* home_dir;
char* cmd;
if (real_euid == 0) {
if (argc < 6) {
printf("Usage: %s <uid> <gid> <tmp_dir> <home_dir> <cmd> ...\n", argv[0]);
return 1;
}
char *endptr1, *endptr2;
uid = strtol(argv[1], &endptr1, 10);
gid = strtol(argv[2], &endptr2, 10);
if (*endptr1 != '\0' || *endptr2 != '\0') {
err(EXIT_FAILURE, "Parsing error: Non-integer characters detected.");
}
tmp_dir = argv[3];
home_dir = argv[4];
cmd = argv[5];
} else {
if (argc < 4) {
printf("Usage: %s <tmp_dir> <home_dir> <cmd> ...\n", argv[0]);
return 1;
}
tmp_dir = argv[1];
home_dir = argv[2];
cmd = argv[3];
uid = real_euid;
gid = real_egid;
}
if (real_euid == 0) {
if (-1 == unshare(CLONE_NEWNS))
err(EXIT_FAILURE, "unshare failed");
} else {
if (-1 == unshare(CLONE_NEWNS | CLONE_NEWUSER))
err(EXIT_FAILURE, "unshare failed");
}
if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
err(EXIT_FAILURE, "cannot change root filesystem privacy");
if (mount(tmp_dir, "/tmp", NULL, MS_BIND, NULL) != 0)
err(EXIT_FAILURE, "mount gentoo tmp dir failed");
if (mount(home_dir, "/tmp/gentoo/home/amos", NULL, MS_BIND, NULL) != 0)
err(EXIT_FAILURE, "mount gentoo home dir failed");
if (real_euid != 0) {
setgroups_deny();
map_id("/proc/self/uid_map", real_euid, real_euid);
map_id("/proc/self/gid_map", real_egid, real_egid);
}
if (setresgid(gid, gid, gid) != 0)
err(EXIT_FAILURE, "setgid failed %d", gid);
if (setresuid(uid, uid, uid) != 0)
err(EXIT_FAILURE, "setuid failed %d", uid);
if (real_euid != 0) {
execvp(cmd, argv + 3);
} else {
execvp(cmd, argv + 5);
}
}