-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchroot.cpp
321 lines (252 loc) · 10.8 KB
/
chroot.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Fakeroot Next Generation - run command with fake root privileges
This program is copyrighted. Copyright information is available at the
AUTHORS file at the root of the source tree for the fakeroot-ng project
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include "syscalls.h"
#include "parent.h"
#include "chroot.h"
// These are the Linux kernel parameters
static const int GLOBAL_LINKS=40, // How many links are globally allowed in the dir part of the resolution
LOCAL_LINKS=8, // How many links each directory level is allowed to have nested
LAST_MILE_LINKS=32; // How many links are allowed in the file part of the link
bool chroot_is_chrooted( const pid_state *state )
{
return *state->root!="" && *state->root!="/";
}
// XXX The memory efficiency of the implementation can use a DRASTIC improvement
static std::string chroot_parse_path_recursion( const pid_state *state, char *path, const std::string &wd, struct stat *stat,
int &global_link_count, int dir_link_count, bool resolve_links )
{
std::string partial_path;
// Body of function - find out the last path component position
int last_slash=-1;
for( int i=0; path[i]!='\0'; ++i )
if( path[i]=='/' )
last_slash=i;
std::string file_part( path+last_slash+1 );
// Remove redundant slashes
while( last_slash>0 && path[last_slash-1]=='/' )
last_slash--;
std::string dir_part;
if( last_slash>0 ) {
// A slash appears in the file name, and it is not the first character
path[last_slash]='\0'; // Chop off the file part
dir_part=chroot_parse_path_recursion( state, path, wd, stat, global_link_count, LOCAL_LINKS, true ); // Translate the rest of the path
if( (int)stat->st_ino==-1 ) {
// Pass the error on - no further processing
return dir_part+"/"+file_part;
}
} else if( last_slash==0 ) {
// Need to process the leading slash
dir_part=*state->root;
} else {
// A file part is all we have to begin with - see what the we are relative to
dir_part=wd;
}
// At this point we have the leading directory in our (debugger) context, and the file part in debugee context
if( file_part=="." || file_part=="" ) {
// Same directory - do nothing more
return dir_part;
}
if( file_part==".." ) {
// We need to go one directory up. This is not as simple as it sounds
// Are we currently at the root (either real or jail)?
if( dir_part==*state->root || dir_part=="/" ) {
// Going up is the same as staying at the same place
return dir_part;
}
// Again - find the last slash in the string, and strip everything after it
size_t last=dir_part.rfind('/');
if( last==std::string::npos ) {
dlog("chroot_parse_path: dir part \"%s\" with file part \"..\" has no way to go one up\n", dir_part.c_str());
dlog(NULL);
assert( last!=std::string::npos );
}
// Eliminate repeats
while( last>0 && dir_part[last-1]=='/' )
last--;
if( last==0 ) // Leave one slash in place if that's all there is
last++;
return dir_part.substr(0, last);
}
// We now, finally, have an honest to god, non empty, non special, file part to handle
std::string combined_path=dir_part+"/"+file_part;
if( lstat( combined_path.c_str(), stat )==-1 ) {
// We failed to stat the file. Just bounce the error to the caller
stat->st_ino=-1;
return combined_path;
}
// Is this a symbolic link?
while( resolve_links && dir_link_count>0 && S_ISLNK(stat->st_mode) ) {
if( (dir_link_count--)==0 || (global_link_count--)==0 ) {
// We are out of patience for link processing
errno=ELOOP;
stat->st_ino=-1;
return "";
}
char buffer[PATH_MAX+1];
ssize_t link_len=readlink( combined_path.c_str(), buffer, sizeof(buffer) );
if( link_len==-1 ) {
dlog("chroot_parse_path_recursion: lstat succeeded for %s, but readlink failed: %s\n", combined_path.c_str(),
strerror(errno) );
return combined_path;
}
if( link_len==sizeof(buffer) ) {
// Symbolic link's content is too long
errno=ENAMETOOLONG;
stat->st_ino=-1;
return "";
}
buffer[link_len]='\0';
if( strchr( buffer, '/' )==NULL ) {
// The / character does not appear - just replace file_part with the new version
file_part=buffer;
combined_path=dir_part+"/"+file_part;
if( lstat( combined_path.c_str(), stat )==-1 ) {
// We failed to stat the file. Just bounce the error to the caller
stat->st_ino=-1;
return combined_path;
}
} else {
// The symlink is a complex path - use ourselves recursively to figure out where it actually links to
return chroot_parse_path_recursion( state, buffer, dir_part, stat, global_link_count, dir_link_count, false );
}
}
return combined_path;
}
// Actual implementation - some sanity checking, and then call the recursive version
std::string chroot_parse_path( const pid_state *state, char *path, const std::string &wd, struct stat *stat, bool resolve_last_link )
{
stat->st_ino=-2; // Mark this as a no-op. If we later do run a stat, it will be overridden
if( path==NULL || path[0]=='\0' ) {
stat->st_ino=-1;
errno=ENOENT;
return "";
}
int total_links=GLOBAL_LINKS;
if( log_level==0 )
return chroot_parse_path_recursion( state, path, wd, stat, total_links, LAST_MILE_LINKS, resolve_last_link );
else {
dlog("chroot_parse_path: translating %s with work dir of %s\n", path, wd.c_str());
std::string ret=chroot_parse_path_recursion( state, path, wd, stat, total_links, LAST_MILE_LINKS, resolve_last_link );
dlog("chroot_parse_path: translated path is %s\n", ret.c_str() );
return ret;
}
}
std::string chroot_translate_addr( pid_t pid, const pid_state *state, struct stat *stat, int dirfd, int_ptr addr, bool resolve_last_link )
{
char filename[PATH_MAX], wd[PATH_MAX];
ptlib_get_string( pid, addr, filename, sizeof(filename) );
strcpy( wd, "/" );
// Get the process' working dir
if( dirfd==CHROOT_PWD )
ptlib_get_cwd( pid, wd, sizeof(wd) );
else
ptlib_get_fd( pid, dirfd, wd, sizeof(wd) );
return chroot_parse_path( state, filename, wd, stat, resolve_last_link );
}
bool chroot_translate_param( pid_t pid, const pid_state *state, int param_num, bool resolve_last_link, bool abort_error, int_ptr offset )
{
return chroot_translate_paramat( pid, state, CHROOT_PWD, param_num, resolve_last_link, abort_error, offset );
}
// Same as chroot_translate_param, only for the *at family of functions
bool chroot_translate_paramat( pid_t pid, const pid_state *state, int dirfd, int param_num, bool resolve_last_link,
bool abort_error, int_ptr offset )
{
// Short path if we are not chrooted
if( !chroot_is_chrooted(state) )
return true;
int_ptr path_addr=ptlib_get_argument( pid, param_num );
if( path_addr==(int_ptr)NULL ) {
// The process asked to work directly on the file descriptor - do not touch the path
return true;
}
struct stat stat;
std::string newpath=chroot_translate_addr( pid, state, &stat, dirfd, path_addr, resolve_last_link );
if( stat.st_ino!=(ino_t)-1 || !abort_error ) {
strcpy( state->mem->get_loc_c()+offset, newpath.c_str() );
ptlib_set_argument( pid, param_num, state->mem->get_shared()+offset );
}
return stat.st_ino!=(ino_t)-1;
}
bool sys_chroot( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
// Save the path pointer and NOP the call
state->context_state[0]=ptlib_get_argument( pid, 1 );
state->state=pid_state::REDIRECT2;
ptlib_set_syscall( pid, PREF_NOP );
} else if( state->state==pid_state::REDIRECT2 ) {
// We may already be chrooted - need to translate the path
struct stat stat;
ref_count<std::string> newroot(new std::string(
chroot_translate_addr( pid, state, &stat, CHROOT_PWD, state->context_state[0], true )));
if( (int)stat.st_ino!=-1 ) {
// The call succeeded
state->root=newroot;
ptlib_set_retval( pid, 0 ); // Success returns 0
} else {
// The call failed
ptlib_set_error( pid, sc_num, errno );
}
state->state=pid_state::NONE;
}
return true;
}
// This function handles the generic case where the function is one that does not need fake root
// special handling, except we need to translate the first parameter in case we are chrooted
bool sys_generic_chroot_support_param1( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->state=pid_state::RETURN;
chroot_translate_param( pid, state, 1, true );
} else if( state->state==pid_state::RETURN ) {
state->state=pid_state::NONE;
}
return true;
}
// Same as above, only when the last link is not resolved
bool sys_generic_chroot_support_link_param1( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->state=pid_state::RETURN;
chroot_translate_param( pid, state, 1, false );
} else if( state->state==pid_state::RETURN ) {
state->state=pid_state::NONE;
}
return true;
}
bool sys_generic_chroot_support_param2( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->state=pid_state::RETURN;
chroot_translate_param( pid, state, 2, true );
} else if( state->state==pid_state::RETURN ) {
state->state=pid_state::NONE;
}
return true;
}