-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile.cpp
1481 lines (1192 loc) · 55.7 KB
/
file.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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/stat.h>
#include <sys/syscall.h>
#include <sys/ptrace.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <limits.h>
#include <string.h>
#include "syscalls.h"
#include "file_lie.h"
#include "chroot.h"
#include "arch/platform.h"
// Helper function - fill in an override structure from a stat structure
static void stat_override_copy( const ptlib_stat *stat, stat_override *override )
{
override->dev=stat->dev;
override->inode=stat->ino;
override->uid=stat->uid;
override->gid=stat->gid;
override->dev_id=stat->rdev;
override->mode=stat->mode;
}
// Same function for stat, lstat and fstat
bool sys_stat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
// Entering the syscall
state->state=pid_state::RETURN;
state->context_state[0]=ptlib_get_argument( pid, 2 ); // Store the pointer to the stat struct
dlog("stat64: " PID_F " stored pointer at %p\n", pid, (void*)state->context_state[0] );
// If the process is chrooted, we need to translate the file name
int real_sc=ptlib_get_syscall( pid );
if( ( real_sc==PREF_STAT || real_sc==PREF_LSTAT ) ) {
chroot_translate_param( pid, state, 1, real_sc!=PREF_LSTAT );
}
} else if( state->state==pid_state::RETURN ) {
// Returning from the syscall
int returncode=ptlib_get_retval( pid );
dlog("stat64: " PID_F " returned %x\n", pid, returncode);
try {
if( ptlib_success( pid, sc_num ) ) {
struct ptlib_stat ret;
struct stat_override override;
if( !ptlib_get_mem( pid, state->context_state[0], &ret, sizeof(ret) ) )
// Probably page fault - report the error
throw (int)errno;
if( get_map( ret.dev, ret.ino, &override ) ) {
bool ok=true;
ret.uid=override.uid;
ret.gid=override.gid;
if( S_ISBLK(override.mode) || S_ISCHR(override.mode) ) {
// Only turn regular files into devices
if( !S_ISREG( ret.mode ) )
ok=false;
ret.rdev=override.dev_id;
} else {
// If the override is not a device, and the types do not match, this is not a valid entry
ok=(S_IFMT&ret.mode)==(S_IFMT&override.mode);
}
// Override the u=x flag for directories, but not files
if( S_ISDIR(ret.mode) ) {
ret.mode=(ret.mode&(~(07700|S_IFMT))) | (override.mode&(07700|S_IFMT));
} else {
ret.mode=(ret.mode&(~(07600|S_IFMT))) | (override.mode&(07600|S_IFMT));
}
if( ok ) {
dlog("stat64: " PID_F " override dev=" DEV_F " inode=" INODE_F " mode=%o uid=" UID_F " gid=" GID_F "\n",
pid, ret.dev, ret.ino, ret.mode, ret.uid, ret.gid );
if( !ptlib_set_mem( pid, &ret, state->context_state[0], sizeof(struct stat) ) ) {
// Probably page fault - report the error
throw (int)errno;
// No need to remove the map - it is legitimate
}
} else {
dlog("stat64: " PID_F " dev=" DEV_F " inode=" INODE_F " override entry corrupt - removed\n", pid, ret.dev, ret.ino );
remove_map( ret.dev, ret.ino );
}
}
}
} catch( int error ) {
ptlib_set_error( pid, state->orig_sc, error );
}
state->state=pid_state::NONE;
}
return true;
}
#if HAVE_OPENAT
bool sys_fstatat64( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
// Entering the syscall
state->state=pid_state::RETURN;
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1 ), 2, (ptlib_get_argument(pid, 4)&AT_SYMLINK_NOFOLLOW)!=0 );
state->context_state[0]=ptlib_get_argument( pid, 3 ); // Store the pointer to the stat struct
dlog("statat64: " PID_F " stored pointer at %p\n", pid, (void*)state->context_state[0] );
return true;
} else {
return sys_stat( sc_num, pid, state ); // Return code handling is the same as for the regular stat
}
}
#endif
static bool real_chmod( int sc_num, pid_t pid, pid_state *state, int mode_offset, int stat_function, int extra_flags=-1 )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
// First we stat the file to find out what we are up against (dev/inode etc.)
state->state=pid_state::REDIRECT2;
state->orig_sc=sc_num;
state->context_state[1]=ptlib_get_argument( pid, mode_offset+1 ); // Store the requested mode
state->context_state[0]=0; // syscall parts progress
ptlib_set_argument( pid, mode_offset+1, state->mem->get_mem() ); // where to store the stat result
// one anomaly handled with special case. ugly, but not worth the interface complication
if( extra_flags!=-1 ) {
// some of the functions require an extra flag after the usual parameters
dlog("real_chmod: set arg2 to %d\n", extra_flags);
ptlib_set_argument( pid, mode_offset+2, extra_flags );
}
ptlib_set_syscall( pid, stat_function );
} else if( state->state==pid_state::REDIRECT2 && state->context_state[0]==0 ) {
if( ptlib_success( pid, sc_num ) ) {
// Our stat succeeded
struct stat_override override;
struct ptlib_stat stat;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof( stat ) ) ) {
// This should never happen. We passed a buffer we are in charge of!
assert(0);
}
ptlib_save_state( pid, state->saved_state );
// Restore the original parameters to restart the actual chmod call
for( int i=1; i<=mode_offset; ++i )
ptlib_set_argument( pid, i, state->context_state[i+1] );
// one anomaly handled with special case. ugly, but not worth the interface complication
if( extra_flags!=-1 ) {
// some of the functions require an extra flag after the usual parameters
ptlib_set_argument( pid, mode_offset+2, extra_flags );
}
state->state=pid_state::REDIRECT1;
// Modify the chmod mode
mode_t mode=(mode_t)state->context_state[1];
mode&=~07000; // Clear the SUID etc. bits
if( S_ISDIR( stat.mode ) ) {
// The node in question is a directory
mode|=00700; // Make sure we have read, write and execute permission
} else {
// Node is not a directory
mode|=00600; // Set read and write for owner
}
ptlib_set_argument( pid, mode_offset+1, mode );
// Save the stuff we'll need in order to update the lies database
state->context_state[2]=stat.dev;
state->context_state[3]=stat.ino;
state->context_state[0]=1; // Mark this as the actual chmod call
// If we don't already have an entry for this file in the lies database, we will not have
// the complete stat struct later on to create it.
if( !get_map( stat.dev, stat.ino, &override ) ) {
// Create a lie that is identical to the actual file
stat_override_copy( &stat, &override );
set_map( &override );
}
return ptlib_generate_syscall( pid, state->orig_sc, state->mem->get_shared() );
} else {
// stat failed - return that failure to the caller as is
state->state=pid_state::NONE;
}
} else if( state->state==pid_state::REDIRECT2 && state->context_state[0]==1 ) {
if( ptlib_success( pid, sc_num ) ) {
// The chmod call succeeded - update the lies database
struct stat_override override;
if( !get_map( state->context_state[2], state->context_state[3], &override ) ) {
// We explicitly created this map not so long ago - something is wrong
// XXX What can we do except hope these are reasonable values
override.dev=state->context_state[2];
override.inode=state->context_state[3];
override.uid=0;
override.gid=0;
override.dev_id=0;
override.transient=true;
dlog("chmod: " PID_F " error (race?) getting override info for dev " DEV_F " inode " INODE_F "\n",
pid, (dev_t)state->context_state[2], (ptlib_inode_t)state->context_state[3] );
}
override.mode=(override.mode&~07777)|(((mode_t)state->context_state[1])&07777);
dlog("chmod: " PID_F " Setting override mode %o dev " DEV_F " inode " INODE_F "\n", pid, override.mode, override.dev,
override.inode );
set_map( &override );
ptlib_restore_state( pid, state->saved_state );
ptlib_set_retval( pid, 0 );
} else {
// We just need to restore the saved state but keep the chmod error code
int err=ptlib_get_error( pid, sc_num );
ptlib_restore_state( pid, state->saved_state );
ptlib_set_error( pid, sc_num, err );
}
state->state=pid_state::NONE;
} else {
dlog("chmod: " PID_F " unknown state %d\n", pid, state->state );
}
return true;
}
// The actual work is done by "real_chmod".
bool sys_chmod( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_param( pid, state, 1, true );
state->context_state[2]=ptlib_get_argument( pid, 1 ); // Store the file name
}
return real_chmod( sc_num, pid, state, 1, PREF_STAT );
}
bool sys_fchmod( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
state->context_state[2]=ptlib_get_argument( pid, 1 ); // Store the file descriptor
}
return real_chmod( sc_num, pid, state, 1, PREF_FSTAT );
}
#if HAVE_OPENAT
bool sys_fchmodat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
// XXX At some potential future date, Linux may implement AT_SYMLINK_NOFOLLOW, at which point
// this chroot translation will need to be reconsidered
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1), 2, true );
state->context_state[2]=ptlib_get_argument( pid, 1 ); // Store the base dir fd
state->context_state[3]=ptlib_get_argument( pid, 2 ); // Store the file name
state->context_state[4]=ptlib_get_argument( pid, 4 ); // Store the flags
}
return real_chmod( sc_num, pid, state, 2, PREF_FSTATAT, 0 );
}
#endif
// context_state[0] and 1 should contain the desired uid and gid respectively
static bool real_chown( int sc_num, pid_t pid, pid_state *state, int own_offset, int stat_function, int extra_flags=-1 )
{
// XXX Do we handle the mode change following a chown (file and directory) correctly?
if( state->state==pid_state::NONE ) {
// Map this to a stat operation
ptlib_set_argument( pid, own_offset+1, state->mem->get_mem() );
if( extra_flags!=-1 ) {
ptlib_set_argument( pid, own_offset+2, extra_flags );
}
ptlib_set_syscall( pid, stat_function );
dlog("chown: " PID_F " redirected chown call to stat\n", pid );
state->state=pid_state::REDIRECT2;
} else if( state->state==pid_state::REDIRECT2 ) {
if( ptlib_success( pid, sc_num ) ) {
struct ptlib_stat stat;
struct stat_override override;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof( stat ) ) ) {
// This is a syscall we initiated, to a memory buffer we own. It should not have failed.
assert(0);
}
if( !get_map( stat.dev, stat.ino, &override ) ) {
dlog("chown: " PID_F " no override for file - create a new one\n", pid );
stat_override_copy( &stat, &override );
}
if( ((int)state->context_state[0])!=-1 )
override.uid=state->context_state[0];
if( ((int)state->context_state[1])!=-1 )
override.gid=state->context_state[1];
dlog("chown: " PID_F " changing owner of dev " DEV_F " inode " INODE_F "\n", pid, override.dev, override.inode );
set_map( &override );
} else {
dlog("chown: " PID_F " stat call failed with error %s\n", pid, strerror(ptlib_get_error(pid, sc_num)) );
}
state->state=pid_state::NONE;
}
return true;
}
bool sys_chown( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->context_state[0]=ptlib_get_argument(pid, 2);
state->context_state[1]=ptlib_get_argument(pid, 3);
chroot_translate_param( pid, state, 1, true );
}
return real_chown( sc_num, pid, state, 1, PREF_STAT );
}
bool sys_fchown( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->context_state[0]=ptlib_get_argument(pid, 2);
state->context_state[1]=ptlib_get_argument(pid, 3);
}
return real_chown( sc_num, pid, state, 1, PREF_FSTAT );
}
bool sys_lchown( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->context_state[0]=ptlib_get_argument(pid, 2);
state->context_state[1]=ptlib_get_argument(pid, 3);
chroot_translate_param( pid, state, 1, false );
}
return real_chown( sc_num, pid, state, 1, PREF_LSTAT );
}
#if HAVE_OPENAT
bool sys_fchownat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->context_state[0]=ptlib_get_argument(pid, 3);
state->context_state[1]=ptlib_get_argument(pid, 4);
state->context_state[2]=ptlib_get_argument(pid, 5);
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1 ), 2, (state->context_state[2]&AT_SYMLINK_NOFOLLOW)!=0 );
}
return real_chown( sc_num, pid, state, 2, PREF_FSTATAT, state->context_state[2] );
}
#endif
static bool real_mknod( int sc_num, pid_t pid, pid_state *state, int mode_offset, int stat_function, int extra_flags=-1 )
{
if( state->state==pid_state::NONE ) {
mode_t mode=(mode_t)state->context_state[0];
// Remove SUID and add user read and write
mode&=~07000;
mode|= 00600;
if( S_ISCHR(mode) || S_ISBLK(mode) ) {
dlog("mknod: " PID_F " tried to create %s device, turn to regular file\n", pid, S_ISCHR(mode) ? "character" : "block" );
mode=(mode&~S_IFMT) | S_IFREG;
}
ptlib_set_argument( pid, mode_offset+1, mode );
dlog("mknod: %d mode %o\n", pid, (unsigned int)state->context_state[1] );
state->state=pid_state::RETURN;
} else if( state->state==pid_state::RETURN ) {
if( ptlib_success( pid, sc_num ) ) {
// Need to call "stat" on the file to see what inode number it got
ptlib_save_state( pid, state->saved_state );
for( int i=0; i<mode_offset; ++i ) {
ptlib_set_argument( pid, i+1, state->context_state[2+i] ); // File name etc.
}
ptlib_set_argument( pid, mode_offset+1, state->mem->get_mem() ); // Struct stat
if( extra_flags!=-1 ) {
ptlib_set_argument( pid, mode_offset+2, extra_flags );
}
state->state=pid_state::REDIRECT1;
dlog("mknod: " PID_F " Actual node creation successful. Calling stat\n", pid );
return ptlib_generate_syscall( pid, stat_function, state->mem->get_shared() );
} else {
// Nothing to do if the call failed
dlog("mknod: " PID_F " call failed with error %s\n", pid, strerror(ptlib_get_error(pid, sc_num) ) );
}
} else if( state->state==pid_state::REDIRECT2 ) {
if( ptlib_success( pid, sc_num ) ) {
ptlib_stat stat;
stat_override override;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof(stat) ) ) {
// This should never happen. We passed a buffer we are in charge of!
assert(0);
}
// This file was, supposedly, just created. Even if it has an entry in the override DB, that entry is obsolete
stat_override_copy( &stat, &override );
// We created the file, it should have our uid/gid
override.uid=state->fsuid;
override.gid=state->fsgid;
dlog("mknod: " PID_F " registering the new device in the override DB dev " DEV_F " inode " INODE_F "\n", pid,
stat.dev, stat.ino );
mode_t mode=(mode_t)state->context_state[0];
if( S_ISCHR(mode) || S_ISBLK(mode) || (mode&07000)!=0) {
dlog("mknod: " PID_F " overriding the file type and/or mode\n", pid );
override.mode=(override.mode&~(S_IFMT|07000)) | (mode&(S_IFMT|07000));
override.dev_id=(dev_t)state->context_state[1];
}
// use the user read+write from the original, not the actual file
// XXX This code disregards the umask
override.mode&=~00600;
override.mode|= mode&00600;
set_map( &override );
} else {
// mknod succeeded, but stat failed?
dlog("mknod: " PID_F " stat failed. Leave override DB non-updated\n", pid );
}
ptlib_restore_state( pid, state->saved_state );
state->state=pid_state::NONE;
}
return true;
}
bool sys_mknod( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_param( pid, state, 1, false );
state->context_state[0]=ptlib_get_argument( pid, 2 ); // Mode
state->context_state[1]=ptlib_get_argument( pid, 3 ); // Device ID
state->context_state[2]=ptlib_get_argument( pid, 1 ); // File name
}
return real_mknod( sc_num, pid, state, 1, PREF_STAT );
}
#if HAVE_OPENAT
bool sys_mknodat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1 ), 2, false );
state->context_state[0]=ptlib_get_argument( pid, 3 ); // Mode
state->context_state[1]=ptlib_get_argument( pid, 4 ); // Device ID
state->context_state[2]=ptlib_get_argument( pid, 1 ); // Base fd
state->context_state[3]=ptlib_get_argument( pid, 2 ); // File name
}
return real_mknod( sc_num, pid, state, 1, PREF_FSTATAT, 0 );
}
#endif
static bool real_open( int sc_num, pid_t pid, pid_state *state, int mode_argnum )
{
if( state->state==pid_state::NONE ) {
if( (state->context_state[0]&O_CREAT)!=0 ) {
// We are asking to create the file - make sure we give user read and write permission
state->context_state[1]=ptlib_get_argument( pid, mode_argnum );
mode_t mode=state->context_state[1];
// Remove SUID and add user read and write permission
mode&=~07000;
mode|= 00600;
if( mode!=state->context_state[1] ) {
ptlib_set_argument( pid, mode_argnum, mode );
}
}
state->state=pid_state::RETURN;
} else if( state->state==pid_state::RETURN ) {
// Did we request to create a new file?
if( (state->context_state[0]&O_CREAT)!=0 && ptlib_success(pid, sc_num) ) {
int fd=(long)ptlib_get_retval(pid);
dlog("open: " PID_F " opened fd %d, assume we actually created it\n", pid, fd );
ptlib_save_state( pid, state->saved_state );
state->state=pid_state::REDIRECT1;
// Call fstat to find out what we have
ptlib_set_argument( pid, 1, fd );
ptlib_set_argument( pid, 2, state->mem->get_mem() );
return ptlib_generate_syscall( pid, PREF_FSTAT, state->mem->get_shared() );
} else
state->state=pid_state::NONE;
} else if( state->state==pid_state::REDIRECT2 ) {
if( ptlib_success( pid, sc_num ) ) {
ptlib_stat stat;
stat_override override;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof( stat ) ) ) {
// This should never happen. We passed a buffer we are in charge of!
assert(0);
}
// XXX The test whether we just created a new file is not the most accurate in the world
// In particular, if the previous instance was deleted, this will misbehave
// Fixed: if the previous instance was deleted BY US, this should now be ok
// Still, /dev/null is routinely found in the map for no good reason due to this code
if( !get_map( stat.dev, stat.ino, &override ) || override.transient ) {
// If the map already exists, assume we did not create a new file and don't touch the owners
stat_override_copy( &stat, &override );
override.uid=state->fsuid;
override.gid=state->fsgid;
override.mode&=~07600;
override.mode|= state->context_state[1]&07600;
// XXX We are ignoring the umask here!
set_map( &override );
dlog("open: " PID_F " creating override for dev " DEV_F " inode " INODE_F "\n", pid, override.dev, override.inode);
} else {
dlog("open: " PID_F " map for dev " DEV_F " inode " INODE_F " already exists - doing nothing\n", pid, stat.dev, stat.ino );
}
} else {
dlog("open: " PID_F " fstat failed %s\n", pid, strerror( ptlib_get_error( pid, sc_num ) ) );
}
state->state=pid_state::NONE;
ptlib_restore_state( pid, state->saved_state );
}
return true;
}
bool sys_open( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_param( pid, state, 1, true );
state->context_state[0]=ptlib_get_argument( pid, 2 ); //flags
}
return real_open( sc_num, pid, state, 3 );
}
#if HAVE_OPENAT
bool sys_openat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1 ), 2, true );
state->context_state[0]=ptlib_get_argument( pid, 3 ); //flags
}
return real_open( sc_num, pid, state, 4 );
}
#endif
static bool real_mkdir( int sc_num, pid_t pid, pid_state *state, int mode_offset, int stat_function, int extra_flags=-1 )
{
if( state->state==pid_state::NONE ) {
// Make sure user has rwx on the created directory
state->context_state[0]=ptlib_get_argument( pid, mode_offset+1 );
mode_t mode=state->context_state[0];
mode|= 00700;
if( mode!=state->context_state[0] ) {
// We did change the mode
ptlib_set_argument( pid, mode_offset+1, mode );
}
state->state=pid_state::RETURN;
} else if( state->state==pid_state::RETURN ) {
state->state=pid_state::NONE;
if( ptlib_success( pid, sc_num ) ) {
dlog("mkdir: " PID_F " succeeded. Call stat\n", pid );
ptlib_save_state( pid, state->saved_state );
// Perform a stat operation so we can know the directory's dev and inode
for( int i=1; i<=mode_offset; ++i )
ptlib_set_argument( pid, i, state->context_state[i] ); // The original mkdir arguments
ptlib_set_argument( pid, mode_offset+1, state->mem->get_mem() ); // stat structure
if( extra_flags!=-1 ) {
ptlib_set_argument( pid, mode_offset+2, extra_flags );
}
state->orig_sc=sc_num;
state->state=pid_state::REDIRECT1;
return ptlib_generate_syscall( pid, stat_function, state->mem->get_shared() );
} else {
// If mkdir failed, we don't have anything else to do.
dlog("mkdir: " PID_F " failed with error %s\n", pid, strerror(ptlib_get_error( pid, sc_num ) ) );
}
} else if( state->state==pid_state::REDIRECT2 ) {
if( ptlib_success( pid, sc_num ) ) {
ptlib_stat stat;
stat_override override;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof( stat ) ) ) {
// This should never happen. We passed a buffer we are in charge of!
assert(0);
}
// Since mkdir fails if the directory already exists, there is no point to check whether the override already exists
stat_override_copy( &stat, &override );
override.uid=state->fsuid;
override.gid=state->fsgid;
override.mode&=~00700;
override.mode|= state->context_state[0]&00700;
// XXX This code does not take the umask into account
dlog("mkdir: " PID_F " storing override for dev " DEV_F " inode " INODE_F "\n", pid, override.dev, override.inode);
set_map( &override );
} else {
dlog("mkdir: " PID_F " stat failed with error %s\n", pid, strerror(ptlib_get_error(pid, sc_num)));
}
ptlib_restore_state( pid, state->saved_state );
state->state=pid_state::NONE;
}
return true;
}
bool sys_mkdir( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_param( pid, state, 1, true );
state->context_state[1]=ptlib_get_argument( pid, 1 ); // Directory name
if( log_level>0 ) {
char name[PATH_MAX];
ptlib_get_string( pid, state->context_state[0], name, sizeof(name) );
dlog("mkdir: %d creates %s\n", pid, name );
}
}
return real_mkdir( sc_num, pid, state, 1, PREF_STAT );
}
#if HAVE_OPENAT
bool sys_mkdirat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 1 ), 2, true );
state->context_state[1]=ptlib_get_argument( pid, 1 ); // File descriptor
state->context_state[2]=ptlib_get_argument( pid, 2 ); // Directory name
if( log_level>0 ) {
char name[PATH_MAX];
ptlib_get_string( pid, state->context_state[2], name, sizeof(name) );
int fd=(int)state->context_state[1];
dlog("mkdirat: %d creates %s at %x\n", pid, name, fd );
}
}
return real_mkdir( sc_num, pid, state, 2, PREF_FSTATAT, 0 );
}
#endif
static bool real_symlink( int sc_num, pid_t pid, pid_state *state, int mode_offset, int stat_function, int extra_flags=-1 )
{
if( state->state==pid_state::NONE ) {
state->state=pid_state::RETURN;
} else if( state->state==pid_state::RETURN ) {
if( ptlib_success( pid, sc_num ) ) {
dlog("symlink: " PID_F " success. Call stat to mark uid/gid override\n", pid );
ptlib_save_state( pid, state->saved_state );
for( int i=0; i<mode_offset; ++i ) {
ptlib_set_argument( pid, i+1, state->context_state[i] ); // File name
}
ptlib_set_argument( pid, mode_offset+1, state->mem->get_mem() ); // stat structure
if( extra_flags!=-1 ) {
ptlib_set_argument( pid, mode_offset+2, extra_flags );
}
state->state=pid_state::REDIRECT1;
return ptlib_generate_syscall( pid, stat_function, state->mem->get_shared() );
} else {
dlog("symlink: " PID_F " failed with error %s\n", pid, strerror( ptlib_get_error(pid, sc_num) ) );
state->state=pid_state::NONE;
}
} else if( state->state==pid_state::REDIRECT2 ) {
if( ptlib_success( pid, sc_num ) ) {
ptlib_stat stat;
stat_override override;
if( !ptlib_get_mem( pid, state->mem->get_mem(), &stat, sizeof( stat ) ) ) {
// This should never happen. We passed a buffer we are in charge of!
assert(0);
}
// Make sure we got the right file
if( S_ISLNK(stat.mode) ) {
// No need to check the DB as we just created the file
stat_override_copy( &stat, &override );
override.uid=state->fsuid;
override.gid=state->fsgid;
dlog("symlink: " PID_F " set uid/gid override for dev " DEV_F " inode " INODE_F "\n", pid, override.dev, override.inode );
set_map( &override );
} else {
dlog("symlink: " PID_F " acutal file on disk is not a symlink. Type %o dev " DEV_F " inode " INODE_F "\n", pid, stat.mode, stat.dev,
stat.ino );
}
} else {
dlog("symlink: " PID_F " symlink succeeded, but stat failed with %s\n", pid, strerror(ptlib_get_error(pid, sc_num)));
}
ptlib_restore_state( pid, state->saved_state );
state->state=pid_state::NONE;
}
return true;
}
bool sys_symlink( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_param( pid, state, 2, false );
state->context_state[0]=ptlib_get_argument( pid, 2 ); // new path
}
return real_symlink( sc_num, pid, state, 1, PREF_LSTAT );
}
#if HAVE_OPENAT
bool sys_symlinkat( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
chroot_translate_paramat( pid, state, ptlib_get_argument( pid, 2 ), 3, false );
state->context_state[0]=ptlib_get_argument( pid, 2 ); // dirfd
state->context_state[1]=ptlib_get_argument( pid, 3 ); // new path
}
return real_symlink( sc_num, pid, state, 2, PREF_FSTATAT, AT_SYMLINK_NOFOLLOW );
}
#endif
bool sys_getcwd( int sc_num, pid_t pid, pid_state *state )
{
if( state->state==pid_state::NONE ) {
PROC_MEM_LOCK();
state->state=pid_state::RETURN;
// If the process is chrooted, we need to translate the directory name
if( chroot_is_chrooted(state) ) {
// We don't want to report the real current directory, of course!
state->context_state[0]=ptlib_get_argument( pid, 1 ); // Buffer
state->context_state[1]=ptlib_get_argument( pid, 2 ); // Buffer len
// Perform the actual call into our buffer
ptlib_set_argument( pid, 1, state->mem->get_mem() );
ptlib_set_argument( pid, 2, PATH_MAX );
}
} else if( state->state==pid_state::RETURN ) {
state->state=pid_state::NONE;
if( ptlib_success( pid, sc_num ) && chroot_is_chrooted(state) ) {
// We are inside a chroot, and the call was successful
char buffer[PATH_MAX];
ptlib_get_string( pid, state->mem->get_mem(), buffer, sizeof(buffer) );
char tmp=buffer[state->root->length()];
buffer[state->root->length()]='\0';
char *ptr=buffer;
if( ( tmp=='/' || tmp=='\0' ) && *state->root==buffer ) {
// Current directory is inside the chroot jail - need to truncate the prefix
if( tmp=='/' ) {
ptr+=state->root->length();
*ptr=tmp;
} else {
// The current directory is the new root
strcpy( buffer, "/" );
}
} else {
// Current directory is outside of the jail - pass it to the program as is
buffer[state->root->length()]=tmp;
}
// Emulate the actual call
size_t len=strlen(ptr);
if( len<state->context_state[1] ) {
// Buffer is large enough
ptlib_set_string( pid, ptr, state->context_state[0] );
ptlib_set_retval( pid, len+1 );
} else {
// The buffer is not large enough
ptlib_set_error( pid, sc_num, ERANGE );
}
}
}
return true;
}
bool sys_munmap( int sc_num, pid_t pid, pid_state *state )
{
// XXX Does this work? We should never be called with REDIRECT1
if( state->state==pid_state::NONE || state->state==pid_state::REDIRECT1 ) {
if( state->state==pid_state::NONE ) {
// This is our first time entering this munmap
state->context_state[2]=0; // No state to restore
}
state->state=pid_state::RETURN;
state->context_state[0]=0; // 0 is the next start address to unmap, 1 is the new length
state->context_state[1]=0; // len=0 means we have no more mmaps to perform
// Is this an attempt to release one of the memory areas we own?
int_ptr start=ptlib_get_argument( pid, 1 );
int_ptr end=start+ptlib_get_argument( pid, 2 );
// If end wraps around then the kernel will fail this anyways
// Same goes if the page size is not aligned or the length is zero
if( start>end || (start%sysconf(_SC_PAGESIZE))!=0 || start==end )
// XXX We still need to fail this ourselves, just to make sure
return true;
// Put the lower of the two addreses in addr1
int_ptr addr1=state->mem->get_shared(), addr2=state->mem->get_mem();
size_t len1=static_mem_size, len2=shared_mem_size-ptlib_prepare_memory_len();
const char *name1="static", *name2="shared";
if( addr1>addr2 ) {
int_ptr tmp=addr1;
addr1=addr2;
addr2=tmp;
size_t tmplen=len1;
len1=len2;
len2=tmplen;
const char *tmpname=name1;
name1=name2;
name2=tmpname;
}
state->state=pid_state::REDIRECT2;
if( start<addr1 && end>addr1 ) {
// The unmap range covers the lower range
dlog("sys_munmap: " PID_F " tried to unmap range %p-%p, which conflicts with our %s range %p-%p\n",
pid, (void*)start, (void*)end, name1, (void*)addr1, (void*)(addr1+len1) );
if( end>addr1+len1 ) {
// There is an area to unmap above the lower range - mark it for a second syscall
state->context_state[0]=addr1+len1;
state->context_state[1]=end-state->context_state[0];
}
end=addr1;
} else if( start>=addr1 && start<addr1+len1 ) {
// The start pointer is inside the lower memory range
dlog("sys_munmap: " PID_F " tried to unmap range %p-%p, which conflicts with our %s range %p-%p\n",
pid, (void*)start, (void*)end, name1, (void*)addr1, (void*)(addr1+len1) );
start=addr1+len1;
} else if( start<addr2 && end>addr2 ) {
// The unmap area covers the upper memory range
dlog("sys_munmap: " PID_F " tried to unmap range %p-%p, which conflicts with our %s range %p-%p\n",
pid, (void*)start, (void*)end, name2, (void*)addr2, (void*)(addr2+len2) );
if( end>addr2+len2 ) {
// There is an area to unmap above the upper range - mark it for a second (third?) syscall
state->context_state[0]=addr2+len2;
state->context_state[1]=end-state->context_state[0];
}
end=addr2;
} else if( start>=addr2 && start<addr2+len2 ) {
// The start pointer is inside the upper memory range
dlog("sys_munmap: " PID_F " tried to unmap range %p-%p, which conflicts with our %s range %p-%p\n",
pid, (void*)start, (void*)end, name2, (void*)addr2, (void*)(addr2+len2) );
start=addr2+len2;
} else {
// The unmap range was a-ok. No need to touch the syscall at all
state->state=pid_state::RETURN;
return true;
}
// We had to change the parameters because of an overlap
ptlib_set_argument( pid, 1, start );
ptlib_set_argument( pid, 2, end-start );
} else if( state->state==pid_state::RETURN || state->state==pid_state::REDIRECT2 ) {
state->state=pid_state::NONE;
if( ptlib_success( pid, sc_num ) ) {
bool last=(state->context_state[1]==0);
// If we are not done yet, we will need to store this position and come back to it
if( state->context_state[2]==0 && !last ) {
ptlib_save_state( pid, state->saved_state );
state->context_state[2]=1;
}
if( !last ) {
// We split the munmap into several calls - there are more calls to perform
// Use REDIRECT3 rather than NONE so that strace won't see the extra call
state->state=pid_state::REDIRECT3;
ptlib_set_argument( pid, 1, state->context_state[0] );