-
Notifications
You must be signed in to change notification settings - Fork 1
/
encapsulate.c
162 lines (141 loc) · 3.93 KB
/
encapsulate.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
/*
* Copyright (c) 2011, 2013 Patrick Georgi <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <errno.h>
#include <sched.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/sched.h>
static int
cmpstring(const void *p1, const void *p2)
{
return strcmp(*(char **) p1, *(char **) p2);
}
int is_dir(const char *path)
{
struct stat fileinfo;
int res = stat(path, &fileinfo);
if (res != 0) return 0;
if (!S_ISDIR(fileinfo.st_mode)) return 0;
return 1;
}
int mount_idemp(const char *path, const char *into)
{
char *real = realpath(path, NULL);
if (real == NULL) {
printf("%s is not a directory\n", path);
return 1;
}
char *unreal = malloc(strlen(into)+strlen(real)+2);
sprintf(unreal, "%s/%s", into, real);
if (!is_dir(real)) {
printf("%s is not a directory\n", real);
return 1;
}
if (mount(real, unreal, NULL, MS_BIND, NULL) != 0) {
perror("mounting writable tree failed");
return 1;
}
return 0;
}
/* Remounts all the bind-mounted paths read-only.
We mount writable versions on top later.
This function is necessary since Linux doesn't
support remount | bind | recursive. */
int remount_readonly(const char *chroot_path)
{
FILE *file = fopen("/proc/mounts", "r");
while (!feof(file)) {
char *path;
fscanf(file, "%*s %ms %*s %*s %*d %*d\n", &path);
if (strstr(path, chroot_path) == path) {
if (mount("", path, NULL, MS_REMOUNT | MS_RDONLY | MS_BIND, NULL) != 0) {
return -1;
}
}
}
fclose(file);
return 0;
}
// usage: encapsulate writable-dir command-line ...
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "not enough arguments\n");
return 1;
}
char **newargv = malloc(sizeof(char*)*(argc-1));
memset(newargv, 0, sizeof(char*)*(argc-1));
memcpy(newargv, argv+2, sizeof(char*)*(argc-2));
char *chroot_path = mkdtemp(strdup("/tmp/encapsulate.XXXXXX"));
if ((chroot_path == NULL) || !is_dir(chroot_path)) {
printf("chroot directory is incorrect\n");
return 1;
}
int uid = getuid();
int euid = geteuid();
setuid(0);
int pid = fork();
if (pid != 0) {
int status;
wait(&status);
rmdir(chroot_path);
return(WEXITSTATUS(status));
}
/* PID namespace only works on clone(), so do that later */
if (unshare(CLONE_NEWIPC | CLONE_NEWNET | CLONE_NEWNS | CLONE_FILES | CLONE_NEWUTS | CLONE_SYSVSEM) != 0) {
perror("creating private namespace failed.");
return 1;
}
/* make sure recursive mounts terminate */
if (mount("", "/", NULL, MS_PRIVATE | MS_REC, NULL) != 0) {
perror("unsharing mountpoints failed");
return 1;
}
if (mount("/", chroot_path, NULL, MS_BIND | MS_REC, NULL) != 0) {
perror("mount failed");
return 1;
}
if (remount_readonly(chroot_path) != 0) {
perror("remount readonly failed");
return 1;
}
char *writables = argv[1];
char *endp = writables;
while (endp) {
char *mountp = writables;
endp = strchr(writables, '|');
if (endp != NULL) {
*endp = '\0';
writables = endp+1;
}
if (mount_idemp(mountp, chroot_path) != 0) {
return 1;
}
}
const char *current_path = getcwd(NULL, 0);
chroot(chroot_path);
chdir(current_path);
setuid(uid);
seteuid(euid);
execvp(newargv[0], newargv);
return 0;
}