aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/firejail/caps.c77
-rw-r--r--src/firejail/firejail.h2
-rw-r--r--src/firejail/fs.c3
-rw-r--r--src/firejail/main.c6
-rw-r--r--src/firejail/profile.c6
-rw-r--r--src/lib/common.c19
-rw-r--r--src/lib/libnetlink.c46
-rw-r--r--src/lib/pid.c34
-rwxr-xr-xsrc/tools/unchrootbin9720 -> 0 bytes
-rw-r--r--src/tools/unchroot.c125
10 files changed, 74 insertions, 244 deletions
diff --git a/src/firejail/caps.c b/src/firejail/caps.c
index ba811cada..6cfa36629 100644
--- a/src/firejail/caps.c
+++ b/src/firejail/caps.c
@@ -181,12 +181,10 @@ static int caps_find_name(const char *name) {
181} 181}
182 182
183// return 1 if error, 0 if OK 183// return 1 if error, 0 if OK
184int caps_check_list(const char *clist, void (*callback)(int)) { 184void caps_check_list(const char *clist, void (*callback)(int)) {
185 // don't allow empty lists 185 // don't allow empty lists
186 if (clist == NULL || *clist == '\0') { 186 if (clist == NULL || *clist == '\0')
187 fprintf(stderr, "Error: empty capabilities lists are not allowed\n"); 187 goto errexit;
188 return -1;
189 }
190 188
191 // work on a copy of the string 189 // work on a copy of the string
192 char *str = strdup(clist); 190 char *str = strdup(clist);
@@ -201,11 +199,8 @@ int caps_check_list(const char *clist, void (*callback)(int)) {
201 else if (*ptr == ',') { 199 else if (*ptr == ',') {
202 *ptr = '\0'; 200 *ptr = '\0';
203 int nr = caps_find_name(start); 201 int nr = caps_find_name(start);
204 if (nr == -1) { 202 if (nr == -1)
205 fprintf(stderr, "Error: capability %s not found\n", start); 203 goto errexit;
206 free(str);
207 return -1;
208 }
209 else if (callback != NULL) 204 else if (callback != NULL)
210 callback(nr); 205 callback(nr);
211 206
@@ -215,17 +210,18 @@ int caps_check_list(const char *clist, void (*callback)(int)) {
215 } 210 }
216 if (*start != '\0') { 211 if (*start != '\0') {
217 int nr = caps_find_name(start); 212 int nr = caps_find_name(start);
218 if (nr == -1) { 213 if (nr == -1)
219 fprintf(stderr, "Error: capability %s not found\n", start); 214 goto errexit;
220 free(str);
221 return -1;
222 }
223 else if (callback != NULL) 215 else if (callback != NULL)
224 callback(nr); 216 callback(nr);
225 } 217 }
226 218
227 free(str); 219 free(str);
228 return 0; 220 return;
221
222errexit:
223 fprintf(stderr, "Error: capability \"%s\" not found\n", start);
224 exit(1);
229} 225}
230 226
231void caps_print(void) { 227void caps_print(void) {
@@ -256,49 +252,53 @@ void caps_print(void) {
256// enabled by default 252// enabled by default
257int caps_default_filter(void) { 253int caps_default_filter(void) {
258 // drop capabilities 254 // drop capabilities
259 if (prctl(PR_CAPBSET_DROP, CAP_SYS_MODULE, 0, 0, 0) && arg_debug) 255 if (prctl(PR_CAPBSET_DROP, CAP_SYS_MODULE, 0, 0, 0))
260 fprintf(stderr, "Warning: cannot drop CAP_SYS_MODULE"); 256 goto errexit;
261 else if (arg_debug) 257 else if (arg_debug)
262 printf("Drop CAP_SYS_MODULE\n"); 258 printf("Drop CAP_SYS_MODULE\n");
263 259
264 if (prctl(PR_CAPBSET_DROP, CAP_SYS_RAWIO, 0, 0, 0) && arg_debug) 260 if (prctl(PR_CAPBSET_DROP, CAP_SYS_RAWIO, 0, 0, 0))
265 fprintf(stderr, "Warning: cannot drop CAP_SYS_RAWIO"); 261 goto errexit;
266 else if (arg_debug) 262 else if (arg_debug)
267 printf("Drop CAP_SYS_RAWIO\n"); 263 printf("Drop CAP_SYS_RAWIO\n");
268 264
269 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0) && arg_debug) 265 if (prctl(PR_CAPBSET_DROP, CAP_SYS_BOOT, 0, 0, 0))
270 fprintf(stderr, "Warning: cannot drop CAP_SYS_BOOT"); 266 goto errexit;
271 else if (arg_debug) 267 else if (arg_debug)
272 printf("Drop CAP_SYS_BOOT\n"); 268 printf("Drop CAP_SYS_BOOT\n");
273 269
274 if (prctl(PR_CAPBSET_DROP, CAP_SYS_NICE, 0, 0, 0) && arg_debug) 270 if (prctl(PR_CAPBSET_DROP, CAP_SYS_NICE, 0, 0, 0))
275 fprintf(stderr, "Warning: cannot drop CAP_SYS_NICE"); 271 goto errexit;
276 else if (arg_debug) 272 else if (arg_debug)
277 printf("Drop CAP_SYS_NICE\n"); 273 printf("Drop CAP_SYS_NICE\n");
278 274
279 if (prctl(PR_CAPBSET_DROP, CAP_SYS_TTY_CONFIG, 0, 0, 0) && arg_debug) 275 if (prctl(PR_CAPBSET_DROP, CAP_SYS_TTY_CONFIG, 0, 0, 0))
280 fprintf(stderr, "Warning: cannot drop CAP_SYS_TTY_CONFIG"); 276 goto errexit;
281 else if (arg_debug) 277 else if (arg_debug)
282 printf("Drop CAP_SYS_TTY_CONFIG\n"); 278 printf("Drop CAP_SYS_TTY_CONFIG\n");
283 279
284#ifdef CAP_SYSLOG 280#ifdef CAP_SYSLOG
285 if (prctl(PR_CAPBSET_DROP, CAP_SYSLOG, 0, 0, 0) && arg_debug) 281 if (prctl(PR_CAPBSET_DROP, CAP_SYSLOG, 0, 0, 0))
286 fprintf(stderr, "Warning: cannot drop CAP_SYSLOG"); 282 goto errexit;
287 else if (arg_debug) 283 else if (arg_debug)
288 printf("Drop CAP_SYSLOG\n"); 284 printf("Drop CAP_SYSLOG\n");
289#endif 285#endif
290 286
291 if (prctl(PR_CAPBSET_DROP, CAP_MKNOD, 0, 0, 0) && arg_debug) 287 if (prctl(PR_CAPBSET_DROP, CAP_MKNOD, 0, 0, 0))
292 fprintf(stderr, "Warning: cannot drop CAP_MKNOD"); 288 goto errexit;
293 else if (arg_debug) 289 else if (arg_debug)
294 printf("Drop CAP_MKNOD\n"); 290 printf("Drop CAP_MKNOD\n");
295 291
296 if (prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN, 0, 0, 0) && arg_debug) 292 if (prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN, 0, 0, 0))
297 fprintf(stderr, "Warning: cannot drop CAP_SYS_ADMIN"); 293 goto errexit;
298 else if (arg_debug) 294 else if (arg_debug)
299 printf("Drop CAP_SYS_ADMIN\n"); 295 printf("Drop CAP_SYS_ADMIN\n");
300 296
301 return 0; 297 return 0;
298
299errexit:
300 fprintf(stderr, "Error: cannot drop capabilities\n");
301 exit(1);
302} 302}
303 303
304void caps_drop_all(void) { 304void caps_drop_all(void) {
@@ -359,19 +359,14 @@ static uint64_t extract_caps(int pid) {
359 EUID_ASSERT(); 359 EUID_ASSERT();
360 360
361 char *file; 361 char *file;
362 if (asprintf(&file, "/proc/%d/status", pid) == -1) { 362 if (asprintf(&file, "/proc/%d/status", pid) == -1)
363 errExit("asprintf"); 363 errExit("asprintf");
364 exit(1);
365 }
366 364
367 EUID_ROOT(); // grsecurity 365 EUID_ROOT(); // grsecurity
368 FILE *fp = fopen(file, "r"); 366 FILE *fp = fopen(file, "r");
369 EUID_USER(); // grsecurity 367 EUID_USER(); // grsecurity
370 if (!fp) { 368 if (!fp)
371 printf("Error: cannot open %s\n", file); 369 goto errexit;
372 free(file);
373 exit(1);
374 }
375 370
376 char buf[MAXBUF]; 371 char buf[MAXBUF];
377 while (fgets(buf, MAXBUF, fp)) { 372 while (fgets(buf, MAXBUF, fp)) {
@@ -385,6 +380,8 @@ static uint64_t extract_caps(int pid) {
385 } 380 }
386 } 381 }
387 fclose(fp); 382 fclose(fp);
383
384errexit:
388 free(file); 385 free(file);
389 fprintf(stderr, "Error: cannot read caps configuration\n"); 386 fprintf(stderr, "Error: cannot read caps configuration\n");
390 exit(1); 387 exit(1);
diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h
index 2e031ce04..4ae3cfd9f 100644
--- a/src/firejail/firejail.h
+++ b/src/firejail/firejail.h
@@ -508,7 +508,7 @@ int caps_default_filter(void);
508void caps_print(void); 508void caps_print(void);
509void caps_drop_all(void); 509void caps_drop_all(void);
510void caps_set(uint64_t caps); 510void caps_set(uint64_t caps);
511int caps_check_list(const char *clist, void (*callback)(int)); 511void caps_check_list(const char *clist, void (*callback)(int));
512void caps_drop_list(const char *clist); 512void caps_drop_list(const char *clist);
513void caps_keep_list(const char *clist); 513void caps_keep_list(const char *clist);
514void caps_print_filter(pid_t pid); 514void caps_print_filter(pid_t pid);
diff --git a/src/firejail/fs.c b/src/firejail/fs.c
index 5774ebf6a..8c776bad5 100644
--- a/src/firejail/fs.c
+++ b/src/firejail/fs.c
@@ -1042,6 +1042,9 @@ void fs_chroot(const char *rootdir) {
1042 if (chroot(rootdir) < 0) 1042 if (chroot(rootdir) < 0)
1043 errExit("chroot"); 1043 errExit("chroot");
1044 1044
1045 // create all other /run/firejail files and directories
1046 preproc_build_firejail_dir();
1047
1045 if (checkcfg(CFG_CHROOT_DESKTOP)) { 1048 if (checkcfg(CFG_CHROOT_DESKTOP)) {
1046 // update /var directory in order to support multiple sandboxes running on the same root directory 1049 // update /var directory in order to support multiple sandboxes running on the same root directory
1047// if (!arg_private_dev) 1050// if (!arg_private_dev)
diff --git a/src/firejail/main.c b/src/firejail/main.c
index ff7b762cd..111a1d751 100644
--- a/src/firejail/main.c
+++ b/src/firejail/main.c
@@ -1186,8 +1186,7 @@ int main(int argc, char **argv) {
1186 if (!arg_caps_list) 1186 if (!arg_caps_list)
1187 errExit("strdup"); 1187 errExit("strdup");
1188 // verify caps list and exit if problems 1188 // verify caps list and exit if problems
1189 if (caps_check_list(arg_caps_list, NULL)) 1189 caps_check_list(arg_caps_list, NULL);
1190 return 1;
1191 } 1190 }
1192 else if (strncmp(argv[i], "--caps.keep=", 12) == 0) { 1191 else if (strncmp(argv[i], "--caps.keep=", 12) == 0) {
1193 arg_caps_keep = 1; 1192 arg_caps_keep = 1;
@@ -1195,8 +1194,7 @@ int main(int argc, char **argv) {
1195 if (!arg_caps_list) 1194 if (!arg_caps_list)
1196 errExit("strdup"); 1195 errExit("strdup");
1197 // verify caps list and exit if problems 1196 // verify caps list and exit if problems
1198 if (caps_check_list(arg_caps_list, NULL)) 1197 caps_check_list(arg_caps_list, NULL);
1199 return 1;
1200 } 1198 }
1201 1199
1202 1200
diff --git a/src/firejail/profile.c b/src/firejail/profile.c
index 688fa9609..abb8bd9b6 100644
--- a/src/firejail/profile.c
+++ b/src/firejail/profile.c
@@ -570,8 +570,7 @@ int profile_check_line(char *ptr, int lineno, const char *fname) {
570 if (!arg_caps_list) 570 if (!arg_caps_list)
571 errExit("strdup"); 571 errExit("strdup");
572 // verify caps list and exit if problems 572 // verify caps list and exit if problems
573 if (caps_check_list(arg_caps_list, NULL)) 573 caps_check_list(arg_caps_list, NULL);
574 exit(1);
575 return 0; 574 return 0;
576 } 575 }
577 576
@@ -582,8 +581,7 @@ int profile_check_line(char *ptr, int lineno, const char *fname) {
582 if (!arg_caps_list) 581 if (!arg_caps_list)
583 errExit("strdup"); 582 errExit("strdup");
584 // verify caps list and exit if problems 583 // verify caps list and exit if problems
585 if (caps_check_list(arg_caps_list, NULL)) 584 caps_check_list(arg_caps_list, NULL);
586 exit(1);
587 return 0; 585 return 0;
588 } 586 }
589 587
diff --git a/src/lib/common.c b/src/lib/common.c
index 2f2340963..add4ff087 100644
--- a/src/lib/common.c
+++ b/src/lib/common.c
@@ -39,22 +39,23 @@ int join_namespace(pid_t pid, char *type) {
39 errExit("asprintf"); 39 errExit("asprintf");
40 40
41 int fd = open(path, O_RDONLY); 41 int fd = open(path, O_RDONLY);
42 if (fd < 0) { 42 if (fd < 0)
43 free(path); 43 goto errout;
44 fprintf(stderr, "Error: cannot open /proc/%u/ns/%s.\n", pid, type);
45 return -1;
46 }
47 44
48 if (syscall(__NR_setns, fd, 0) < 0) { 45 if (syscall(__NR_setns, fd, 0) < 0) {
49 free(path);
50 fprintf(stderr, "Error: cannot join namespace %s.\n", type);
51 close(fd); 46 close(fd);
52 return -1; 47 goto errout;
53 } 48 }
54 49
55 close(fd); 50 close(fd);
56 free(path); 51 free(path);
57 return 0; 52 return 0;
53
54errout:
55 free(path);
56 fprintf(stderr, "Error: cannot join namespace %s\\n", type);
57 return -1;
58
58} 59}
59 60
60// return 1 if error 61// return 1 if error
@@ -187,8 +188,6 @@ char *pid_proc_cmdline(const pid_t pid) {
187 for (i = 0; i < len; i++) { 188 for (i = 0; i < len; i++) {
188 if (buffer[i] == '\0') 189 if (buffer[i] == '\0')
189 buffer[i] = ' '; 190 buffer[i] = ' ';
190// if (buffer[i] >= 0x80) // execv in progress!!!
191// return NULL;
192 } 191 }
193 192
194 // return a malloc copy of the command line 193 // return a malloc copy of the command line
diff --git a/src/lib/libnetlink.c b/src/lib/libnetlink.c
index 836cf417d..417ef2c5f 100644
--- a/src/lib/libnetlink.c
+++ b/src/lib/libnetlink.c
@@ -105,6 +105,7 @@ int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
105 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE); 105 return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
106} 106}
107 107
108#if 0
108int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type) 109int rtnl_wilddump_request(struct rtnl_handle *rth, int family, int type)
109{ 110{
110 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF); 111 return rtnl_wilddump_req_filter(rth, family, type, RTEXT_FILTER_VF);
@@ -303,6 +304,7 @@ int rtnl_dump_filter(struct rtnl_handle *rth,
303 304
304 return rtnl_dump_filter_l(rth, a); 305 return rtnl_dump_filter_l(rth, a);
305} 306}
307#endif
306 308
307int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer, 309int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
308 unsigned groups, struct nlmsghdr *answer) 310 unsigned groups, struct nlmsghdr *answer)
@@ -422,6 +424,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
422 } 424 }
423} 425}
424 426
427#if 0
425int rtnl_listen(struct rtnl_handle *rtnl, 428int rtnl_listen(struct rtnl_handle *rtnl,
426 rtnl_filter_t handler, 429 rtnl_filter_t handler,
427 void *jarg) 430 void *jarg)
@@ -580,7 +583,7 @@ int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
580{ 583{
581 return addattr_l(n, maxlen, type, str, strlen(str)+1); 584 return addattr_l(n, maxlen, type, str, strlen(str)+1);
582} 585}
583 586#endif
584 587
585 588
586int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data, 589int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
@@ -632,46 +635,8 @@ printf("\tdata length: %d\n", alen);
632 return 0; 635 return 0;
633} 636}
634 637
635#if 0
636int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
637 int alen)
638{
639printf("%s: adding type %d, length %d ", __FUNCTION__, type, alen);
640if (type == IFLA_INFO_KIND) {
641if (alen)
642 printf("(IFLA_INFO_KIND %s)\n", (char *)data);
643else
644printf("(VETH_INFO_PEER)\n");
645}
646else if (type == IFLA_IFNAME) {
647printf("(IFLA_IFNAME %s)\n", (char *) data);
648}
649else if (type == IFLA_NET_NS_PID) {
650printf("(IFLA_NET_NS_PID %u)\n", *((unsigned *) data));
651}
652else if (type == IFLA_LINKINFO)
653printf("(IFLA_LINKINFO)\n");
654else if (type == IFLA_INFO_DATA)
655printf("(IFLA_INFO_DATA)\n");
656else
657 printf("\n");
658
659 int len = RTA_LENGTH(alen);
660 struct rtattr *rta;
661
662 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
663 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
664 return -1;
665 }
666 rta = NLMSG_TAIL(n);
667 rta->rta_type = type;
668 rta->rta_len = len;
669 memcpy(RTA_DATA(rta), data, alen);
670 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
671 return 0;
672}
673#endif
674 638
639#if 0
675int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len) 640int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
676{ 641{
677 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len)) > maxlen) { 642 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len)) > maxlen) {
@@ -802,3 +767,4 @@ int __parse_rtattr_nested_compat(struct rtattr *tb[], int max, struct rtattr *rt
802 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); 767 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
803 return 0; 768 return 0;
804} 769}
770#endif
diff --git a/src/lib/pid.c b/src/lib/pid.c
index ed583c51d..42687274e 100644
--- a/src/lib/pid.c
+++ b/src/lib/pid.c
@@ -34,10 +34,9 @@ int max_pids=32769;
34void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) { 34void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) {
35 // open stat file 35 // open stat file
36 char *file; 36 char *file;
37 if (asprintf(&file, "/proc/%u/statm", pid) == -1) { 37 if (asprintf(&file, "/proc/%u/statm", pid) == -1)
38 perror("asprintf"); 38 errExit("asprintf");
39 exit(1); 39
40 }
41 FILE *fp = fopen(file, "r"); 40 FILE *fp = fopen(file, "r");
42 if (!fp) { 41 if (!fp) {
43 free(file); 42 free(file);
@@ -59,10 +58,9 @@ void pid_getmem(unsigned pid, unsigned *rss, unsigned *shared) {
59void pid_get_cpu_time(unsigned pid, unsigned *utime, unsigned *stime) { 58void pid_get_cpu_time(unsigned pid, unsigned *utime, unsigned *stime) {
60 // open stat file 59 // open stat file
61 char *file; 60 char *file;
62 if (asprintf(&file, "/proc/%u/stat", pid) == -1) { 61 if (asprintf(&file, "/proc/%u/stat", pid) == -1)
63 perror("asprintf"); 62 errExit("asprintf");
64 exit(1); 63
65 }
66 FILE *fp = fopen(file, "r"); 64 FILE *fp = fopen(file, "r");
67 if (!fp) { 65 if (!fp) {
68 free(file); 66 free(file);
@@ -93,10 +91,9 @@ myexit:
93unsigned long long pid_get_start_time(unsigned pid) { 91unsigned long long pid_get_start_time(unsigned pid) {
94 // open stat file 92 // open stat file
95 char *file; 93 char *file;
96 if (asprintf(&file, "/proc/%u/stat", pid) == -1) { 94 if (asprintf(&file, "/proc/%u/stat", pid) == -1)
97 perror("asprintf"); 95 errExit("asprintf");
98 exit(1); 96
99 }
100 FILE *fp = fopen(file, "r"); 97 FILE *fp = fopen(file, "r");
101 if (!fp) { 98 if (!fp) {
102 free(file); 99 free(file);
@@ -138,10 +135,8 @@ uid_t pid_get_uid(pid_t pid) {
138 135
139 // open status file 136 // open status file
140 char *file; 137 char *file;
141 if (asprintf(&file, "/proc/%u/status", pid) == -1) { 138 if (asprintf(&file, "/proc/%u/status", pid) == -1)
142 perror("asprintf"); 139 errExit("asprintf");
143 exit(1);
144 }
145 140
146 FILE *fp = fopen(file, "r"); 141 FILE *fp = fopen(file, "r");
147 if (!fp) { 142 if (!fp) {
@@ -316,10 +311,9 @@ void pid_read(pid_t mon_pid) {
316 311
317 // open stat file 312 // open stat file
318 char *file; 313 char *file;
319 if (asprintf(&file, "/proc/%u/status", pid) == -1) { 314 if (asprintf(&file, "/proc/%u/status", pid) == -1)
320 perror("asprintf"); 315 errExit("asprintf");
321 exit(1); 316
322 }
323 FILE *fp = fopen(file, "r"); 317 FILE *fp = fopen(file, "r");
324 if (!fp) { 318 if (!fp) {
325 free(file); 319 free(file);
diff --git a/src/tools/unchroot b/src/tools/unchroot
deleted file mode 100755
index d32ce2682..000000000
--- a/src/tools/unchroot
+++ /dev/null
Binary files differ
diff --git a/src/tools/unchroot.c b/src/tools/unchroot.c
deleted file mode 100644
index 21731296e..000000000
--- a/src/tools/unchroot.c
+++ /dev/null
@@ -1,125 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <string.h>
6#include <unistd.h>
7#include <sys/stat.h>
8#include <sys/types.h>
9
10/*
11 ** You should set NEED_FCHDIR to 1 if the chroot() on your
12 ** system changes the working directory of the calling
13 ** process to the same directory as the process was chroot()ed
14 ** to.
15 **
16 ** It is known that you do not need to set this value if you
17 ** running on Solaris 2.7 and below.
18 **
19 */
20#define NEED_FCHDIR 0
21
22#define TEMP_DIR "waterbuffalo"
23
24/* Break out of a chroot() environment in C */
25
26int main() {
27 int x; /* Used to move up a directory tree */
28 int done=0; /* Are we done yet ? */
29#ifdef NEED_FCHDIR
30 int dir_fd; /* File descriptor to directory */
31#endif
32 struct stat sbuf; /* The stat() buffer */
33
34 /*
35 ** First we create the temporary directory if it doesn't exist
36 */
37 if (stat(TEMP_DIR,&sbuf)<0) {
38 if (errno==ENOENT) {
39 if (mkdir(TEMP_DIR,0755)<0) {
40 fprintf(stderr,"Failed to create %s - %s\n", TEMP_DIR,
41 strerror(errno));
42 exit(1);
43 }
44 }
45 else {
46 fprintf(stderr,"Failed to stat %s - %s\n", TEMP_DIR,
47 strerror(errno));
48 exit(1);
49 }
50 }
51 else if (!S_ISDIR(sbuf.st_mode)) {
52 fprintf(stderr,"Error - %s is not a directory!\n",TEMP_DIR);
53 exit(1);
54 }
55
56#ifdef NEED_FCHDIR
57 /*
58 ** Now we open the current working directory
59 **
60 ** Note: Only required if chroot() changes the calling program's
61 ** working directory to the directory given to chroot().
62 **
63 */
64 if ((dir_fd=open(".",O_RDONLY))<0) {
65 fprintf(stderr,"Failed to open \".\" for reading - %s\n",
66 strerror(errno));
67 exit(1);
68 }
69#endif
70
71 /*
72 ** Next we chroot() to the temporary directory
73 */
74 if (chroot(TEMP_DIR)<0) {
75 fprintf(stderr,"Failed to chroot to %s - %s\n",TEMP_DIR,
76 strerror(errno));
77 exit(1);
78 }
79
80#ifdef NEED_FCHDIR
81 /*
82 ** Partially break out of the chroot by doing an fchdir()
83 **
84 ** This only partially breaks out of the chroot() since whilst
85 ** our current working directory is outside of the chroot() jail,
86 ** our root directory is still within it. Thus anything which refers
87 ** to "/" will refer to files under the chroot() point.
88 **
89 ** Note: Only required if chroot() changes the calling program's
90 ** working directory to the directory given to chroot().
91 **
92 */
93 if (fchdir(dir_fd)<0) {
94 fprintf(stderr,"Failed to fchdir - %s\n",
95 strerror(errno));
96 exit(1);
97 }
98 close(dir_fd);
99#endif
100
101 /*
102 ** Completely break out of the chroot by recursing up the directory
103 ** tree and doing a chroot to the current working directory (which will
104 ** be the real "/" at that point). We just do a chdir("..") lots of
105 ** times (1024 times for luck :). If we hit the real root directory before
106 ** we have finished the loop below it doesn't matter as .. in the root
107 ** directory is the same as . in the root.
108 **
109 ** We do the final break out by doing a chroot(".") which sets the root
110 ** directory to the current working directory - at this point the real
111 ** root directory.
112 */
113 for(x=0;x<1024;x++) {
114 chdir("..");
115 }
116 chroot(".");
117
118 /*
119 ** We're finally out - so exec a shell in interactive mode
120 */
121 if (execl("/bin/sh","-i",NULL)<0) {
122 fprintf(stderr,"Failed to exec - %s\n",strerror(errno));
123 exit(1);
124 }
125}