-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmount.c
222 lines (193 loc) · 5.15 KB
/
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* tizian - better chrooting with containers
* Copyright (C) 2017 Salvatore Mesoraca <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include "mount_over.h"
#include "utils.h"
#define TMP_DIR_MOUNT "/tmp/mnttmp.XXXXXXX"
#define TMP_DIR_OLD_ROOT "/old_root.XXXXXX"
static int pivot_root(const char *new_root, const char *put_old)
{
return syscall(SYS_pivot_root, new_root, put_old);
}
int pre_mount_proc(const char *new_root, int proc_mount, uid_t new_root_uid)
{
char tmp_new_root[] = TMP_DIR_MOUNT;
char old_root[] = TMP_DIR_MOUNT TMP_DIR_OLD_ROOT;
char new_in_old_root[] = TMP_DIR_OLD_ROOT TMP_DIR_MOUNT;
struct stat s;
char mode_buf[128];
int ignored __attribute__((unused));
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
print_error("Can't remount / as private");
return -1;
}
if (!mkdtemp(tmp_new_root)) {
print_error("Can't make new root tmpdir");
return -1;
}
memcpy(old_root, tmp_new_root, strlen(tmp_new_root));
if (mount(new_root, tmp_new_root, NULL, MS_REC | MS_BIND | MS_PRIVATE, NULL)) {
print_error("Can't mount new root");
return -1;
}
if (!mkdtemp(old_root)) {
print_error("Can't make old root tmpdir");
return -1;
}
memcpy(new_in_old_root,
old_root+sizeof(TMP_DIR_MOUNT)-1,
sizeof(TMP_DIR_OLD_ROOT));
memcpy(new_in_old_root+sizeof(TMP_DIR_OLD_ROOT)-1,
tmp_new_root,
strlen(tmp_new_root));
if (pivot_root(tmp_new_root, old_root)) {
print_error("Can't pivot root");
return -1;
}
if (chdir("/")) {
print_error("Can't chdir in new root");
return -1;
}
if (rmdir(new_in_old_root)) {
print_error("Can't rmdir new root tmpdir");
return -1;
}
if (umount2(old_root+sizeof(TMP_DIR_MOUNT)-1, MNT_DETACH)) {
print_error("Can't umount old root");
return -1;
}
if (rmdir(old_root+sizeof(TMP_DIR_MOUNT)-1)) {
print_error("Can't rmdir old root tmpdir");
return -1;
}
if (!proc_mount)
return 0;
mkdir("/dev", 0755);
mkdir("/dev/pts", 0755);
mkdir("/proc", 0555);
mkdir("/etc", 0755);
unlink("/dev/ptmx");
ignored = symlink("/dev/pts/ptmx", "/dev/ptmx");
mknod("/dev/null", 0666, makedev(1, 3));
mknod("/dev/zero", 0666, makedev(1, 5));
mknod("/dev/full", 0666, makedev(1, 7));
mknod("/dev/random", 0666, makedev(1, 8));
mknod("/dev/urandom", 0666, makedev(1, 9));
mknod("/dev/tty", 0666, makedev(5, 0));
if (stat("/dev/tty", &s)) {
print_error("Can't stat /dev/tty");
return -1;
}
snprintf(mode_buf, sizeof(mode_buf),
"uid=%d,gid=%d,mode=0620,ptmxmode=0666,newinstance",
new_root_uid,
s.st_gid);
if (mount("devpts", "/dev/pts", "devpts", MS_NOSUID|MS_NOEXEC, mode_buf)) {
print_error("devpts impossible");
return -1;
}
return 0;
}
static int mount_over_dir(const char *target)
{
if (access(target, F_OK ))
return 0;
if (mount("tmpfs", target, "tmpfs",
MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_NOATIME|MS_RDONLY,
"size=1,mode=0000")) {
print_error("proc mount impossible");
return -1;
}
return 0;
}
static int mount_over_file(const char *target)
{
if (access(target, F_OK ))
return 0;
if (mount("/proc/cpuinfo", target, "none",
MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_NOATIME|MS_RDONLY|MS_BIND,
NULL)) {
print_error("proc mount impossible");
return -1;
}
return 0;
}
static int mount_over(const char *dirs[], const char *files[])
{
int i;
i = 0;
while (dirs[i] != NULL) {
if (mount_over_dir(dirs[i]))
return -1;
++i;
}
i = 0;
while (files[i] != NULL) {
if (mount_over_file(files[i]))
return -1;
++i;
}
return 0;
}
int mount_proc(uid_t new_root_uid)
{
int fd1, fd2, ret = 0;
char mode_buf[128];
snprintf(mode_buf, sizeof(mode_buf),
"gid=%d,hidepid=2",
new_root_uid);
if (mount("proc", "/proc", "proc", MS_NOSUID|MS_NOEXEC|MS_NODEV, mode_buf)) {
print_error("proc mount impossible");
return -1;
}
if (mount_over(mount_over_dirs, mount_over_files))
return -1;
fd1 = open("/proc/mounts", O_RDONLY);
if (fd1 == -1) {
print_error("Can't open /proc/mounts");
return -1;
}
unlink("/etc/mtab");
fd2 = open("/etc/mtab", O_TRUNC|O_CREAT|O_WRONLY, 0644);
if (fd2 == -1) {
print_error("Can't open /etc/mtab");
ret = -1;
goto out_fd1;
}
if (sendfile(fd2, fd1, NULL, UINT_MAX) == -1) {
print_error("Can't sendfile /etc/mtab");
ret = -1;
goto out;
}
out:
close(fd2);
out_fd1:
close(fd1);
return ret;
}