-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparent.cpp
1239 lines (1045 loc) · 41.4 KB
/
parent.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 <unordered_map>
#include <string.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include "arch/platform.h"
#include "parent.h"
#include "syscalls.h"
#include "process.h"
#include "daemon.h"
// forward declaration of function
static bool handle_memory_allocation( int sc_num, pid_t pid, pid_state *state );
// Keep track of handled syscalls
static std::unordered_map<int, syscall_hook> syscalls;
// Keep track of the states for the various processes
template <class key, class data> class map_class : public std::unordered_map<key, data>
{
// Inherit everything, just disable the dangerous operator[]
public:
data &operator[] (const key &k)
{
return std::unordered_map<key,data>::operator[] (k);
}
const data &operator[] ( const key &k) const
{
return std::unordered_map<key,data>::operator[] (k);
}
};
static map_class<pid_t, pid_state> state;
size_t static_mem_size, shared_mem_size;
static std::unordered_map<pid_t, int> root_children; // Map of all root children
static int num_processes; // Number of running processes
// Definitions of some methods
pid_state::process_memory::~process_memory()
{
if( shared_mem_local!=MAP_FAILED ) {
if( munmap( (void*)(((int_ptr)shared_mem_local)-shared_overhead), shared_size )<0 ) {
// Log the error, but do not otherwise do anything interesting ...
dlog("~process_memory: munmap( %p, %lu ) failed with %s\n", (void*)(((int_ptr)shared_mem_local)-shared_overhead),
(unsigned long)shared_size, strerror(errno) );
// ... unless we're in debug mode :-)
dlog(NULL);
assert(false);
}
shared_mem_local=MAP_FAILED;
}
}
static void init_handlers()
{
// A macro for defining a system call with different syscall and handler names
#define DEF_SYS2( syscall, function ) syscalls[SYS_##syscall]=syscall_hook(sys_##function, #syscall)
// A macro fro defining a system call with the same syscall and handler names
#define DEF_SYS1( syscall ) DEF_SYS2( syscall, syscall )
DEF_SYS1(geteuid);
#if defined(SYS_geteuid32)
DEF_SYS2(geteuid32, geteuid);
#endif
DEF_SYS1(getuid);
#if defined(SYS_getuid32)
DEF_SYS2(getuid32, getuid);
#endif
DEF_SYS1(getegid);
#if defined(SYS_getegid32)
DEF_SYS2(getegid32, getegid);
#endif
DEF_SYS1(getgid);
#if defined(SYS_getgid32)
DEF_SYS2(getgid32, getgid);
#endif
DEF_SYS1(getresuid);
#if defined(SYS_getresuid32)
DEF_SYS2(getresuid32, getresuid);
#endif
DEF_SYS1(getresgid);
#if defined(SYS_getresgid32)
DEF_SYS2(getresgid32, getresgid);
#endif
DEF_SYS1(getgroups);
#if defined(SYS_getgroups32)
DEF_SYS2(getgroups32, getgroups);
#endif
DEF_SYS1(setuid);
#ifdef SYS_seteuid
DEF_SYS1(seteuid);
#endif
#ifdef SYS_seteuid32
DEF_SYS2(seteuid32, seteuid);
#endif
#ifdef SYS_setfsuid
DEF_SYS1(setfsuid);
#endif
#ifdef SYS_setfsuid32
DEF_SYS2(setfsuid32, setfsuid);
#endif
#ifdef SYS_setresuid
DEF_SYS1(setresuid);
#endif
#ifdef SYS_setresuid32
DEF_SYS2(setresuid32, setresuid);
#endif
DEF_SYS1(setreuid);
#ifdef SYS_setreuid32
DEF_SYS2(setreuid32, setreuid);
#endif
DEF_SYS1(setgid);
#ifdef SYS_setegid
DEF_SYS1(setegid);
#endif
#ifdef SYS_setegid32
DEF_SYS2(setegid32, setegid);
#endif
#ifdef SYS_setfsgid
DEF_SYS1(setfsgid);
#endif
#ifdef SYS_setfsgid32
DEF_SYS2(setfsgid32, setfsgid);
#endif
#ifdef SYS_setresgid
DEF_SYS1(setresgid);
#endif
#ifdef SYS_setresgid32
DEF_SYS2(setresgid32, setresgid);
#endif
DEF_SYS1(setregid);
#ifdef SYS_setregid32
DEF_SYS2(setregid32, setregid);
#endif
DEF_SYS1(setgroups);
#if defined(SYS_setgroups32)
DEF_SYS2(setgroups32, setgroups);
#endif
DEF_SYS1(fork);
DEF_SYS1(vfork);
#if defined(SYS_clone)
DEF_SYS1(clone);
#endif
// Execve is special cased
// DEF_SYS1(execve);
#if defined(SYS_sigreturn)
DEF_SYS1(sigreturn);
#endif
#if defined(SYS_rt_sigreturn)
DEF_SYS2(rt_sigreturn, sigreturn);
#endif
DEF_SYS1(setsid);
#if defined(SYS_wait4)
DEF_SYS1(wait4);
#endif
#if defined(SYS_waitpid)
DEF_SYS1(waitpid);
#endif
DEF_SYS1(ptrace);
DEF_SYS1(kill);
DEF_SYS1(stat);
#ifdef SYS_stat64
DEF_SYS2(stat64, stat);
#endif
DEF_SYS2(fstat, stat);
#ifdef SYS_fstat64
DEF_SYS2(fstat64, stat);
#endif
DEF_SYS2(lstat, stat);
#ifdef SYS_lstat64
DEF_SYS2(lstat64, stat);
#endif
#if defined(SYS_newfstatat) && HAVE_OPENAT
DEF_SYS2(newfstatat, fstatat64);
#endif
#if defined(SYS_fstatat64) && HAVE_OPENAT
DEF_SYS1(fstatat64);
#endif
DEF_SYS1(chown);
#if defined(SYS_chown32)
DEF_SYS2(chown32, chown);
#endif
DEF_SYS1(fchown);
#if defined(SYS_fchown32)
DEF_SYS2(fchown32, fchown);
#endif
DEF_SYS1(lchown);
#if defined(SYS_lchown32)
DEF_SYS2(lchown32, lchown);
#endif
#if defined(SYS_fchownat) && HAVE_OPENAT
DEF_SYS1(fchownat);
#endif
DEF_SYS1(chmod);
DEF_SYS1(fchmod);
#if defined(SYS_fchmodat) && HAVE_OPENAT
DEF_SYS1(fchmodat);
#endif
DEF_SYS1(mknod);
#if defined(SYS_mknodat) && HAVE_OPENAT
DEF_SYS1(mknodat);
#endif
DEF_SYS1(open);
#if defined(SYS_openat) && HAVE_OPENAT
DEF_SYS1(openat);
#endif
DEF_SYS1(mkdir);
#if defined(SYS_mkdirat) && HAVE_OPENAT
DEF_SYS1(mkdirat);
#endif
DEF_SYS1(symlink);
#if defined(SYS_mkdirat) && HAVE_OPENAT
DEF_SYS1(symlinkat);
#endif
DEF_SYS1(link);
#if defined(SYS_linkat) && HAVE_OPENAT
DEF_SYS1(linkat);
#endif
DEF_SYS1(unlink);
#if defined(SYS_unlinkat) && HAVE_OPENAT
DEF_SYS1(unlinkat);
#endif
DEF_SYS1(rename);
#if defined(SYS_renameat) && HAVE_OPENAT
DEF_SYS1(renameat);
#endif
DEF_SYS1(rmdir);
DEF_SYS2(readlink, generic_chroot_support_link_param1);
#if defined(SYS_renameat) && HAVE_OPENAT
DEF_SYS2(readlinkat, generic_chroot_link_at);
#endif
DEF_SYS2(truncate, generic_chroot_support_param1);
#ifdef SYS_truncate64
DEF_SYS2(truncate64, generic_chroot_support_param1);
#endif
DEF_SYS2(statfs, generic_chroot_support_param1); // XXX Should last link be resolved?
#ifdef SYS_statfs64
DEF_SYS2(statfs64, generic_chroot_support_param1); // XXX Should last link be resolved?
#endif
DEF_SYS2(chdir, generic_chroot_support_param1);
DEF_SYS2(access, generic_chroot_support_param1);
#if defined(SYS_faccessat) && HAVE_OPENAT
DEF_SYS2(faccessat, generic_chroot_at_link4);
#endif
DEF_SYS2(utime, generic_chroot_support_param1);
DEF_SYS2(utimes, generic_chroot_support_param1);
#ifdef SYS_setxattr
DEF_SYS2(setxattr, generic_chroot_support_param1);
DEF_SYS2(getxattr, generic_chroot_support_param1);
DEF_SYS2(listxattr, generic_chroot_support_param1);
DEF_SYS2(removexattr, generic_chroot_support_param1);
#endif
#ifdef SYS_lsetxattr
DEF_SYS2(lsetxattr, generic_chroot_support_link_param1);
DEF_SYS2(lgetxattr, generic_chroot_support_link_param1);
DEF_SYS2(llistxattr, generic_chroot_support_link_param1);
DEF_SYS2(lremovexattr, generic_chroot_support_link_param1);
#endif
#ifdef SYS_uselib
DEF_SYS2(uselib, generic_chroot_support_param1);
#endif
#ifdef SYS_inotify_add_watch
DEF_SYS2(inotify_add_watch, generic_chroot_support_param1);
#endif
#if defined(SYS_futimesat) && HAVE_OPENAT
DEF_SYS2(futimesat, generic_chroot_at);
#endif
#if defined(SYS_utimensat) && HAVE_OPENAT
DEF_SYS2(utimensat, generic_chroot_at_link4);
#endif
DEF_SYS1(chroot);
DEF_SYS1(getcwd);
DEF_SYS1(mmap);
#ifdef SYS_mmap2
DEF_SYS2(mmap2, mmap);
#endif
DEF_SYS1(munmap);
}
void init_globals()
{
size_t page_size=sysconf(_SC_PAGESIZE);
static_mem_size=page_size;
shared_mem_size=2*PATH_MAX+ptlib_prepare_memory_len();
// Round this to the higher page size
shared_mem_size+=page_size-1;
shared_mem_size-=shared_mem_size%page_size;
}
// Debug related functions
static const char *sig2str( int signum )
{
static char buffer[64];
switch(signum) {
#define SIGNAME(a) case a: return #a;
SIGNAME(SIGHUP);
SIGNAME(SIGINT);
SIGNAME(SIGQUIT);
SIGNAME(SIGILL);
SIGNAME(SIGTRAP);
SIGNAME(SIGABRT);
SIGNAME(SIGBUS);
SIGNAME(SIGFPE);
SIGNAME(SIGKILL);
SIGNAME(SIGSEGV);
SIGNAME(SIGPIPE);
SIGNAME(SIGALRM);
SIGNAME(SIGTERM);
SIGNAME(SIGCHLD);
SIGNAME(SIGCONT);
SIGNAME(SIGSTOP);
#undef SIGNAME
default:
sprintf(buffer, "signal %d", signum);
}
return buffer;
}
static const char *state2str( pid_state::states state )
{
static char buffer[64];
switch(state) {
#define STATENAME(a) case pid_state::a: return #a;
STATENAME(INIT)
STATENAME(NONE)
STATENAME(RETURN)
STATENAME(REDIRECT1)
STATENAME(REDIRECT2)
STATENAME(REDIRECT3)
STATENAME(ALLOCATE)
STATENAME(ALLOC_RETURN)
STATENAME(WAITING)
STATENAME(ZOMBIE)
#undef STATENAME
}
sprintf(buffer, "Unknown state %d", state);
return buffer;
}
void dump_registers( pid_t pid )
{
if( log_level>0 ) {
void *state[PTLIB_STATE_SIZE];
ptlib_save_state( pid, state );
for( unsigned int i=0; i<PTLIB_STATE_SIZE; ++i )
dlog("state[%d]=%p\n", i, state[i]);
}
}
// State handling functions
static void notify_parent( pid_t parent, const pid_state::wait_state &waiting )
{
if( parent==1 || parent==0 ) {
// This process has no parent, or had a parent that already quit
return;
}
dlog("notify_parent: " PID_F " sent a notify about " PID_F "(%x)\n", parent, waiting.pid(), waiting.status());
pid_state *proc_state=lookup_state(parent);
assert(proc_state!=NULL);
proc_state->waiting_signals.push_back( waiting );
// Is the parent currently waiting?
if( proc_state->state==pid_state::WAITING ) {
// Call the original function handler, now that it has something to do
if( syscalls[proc_state->orig_sc].func( -1, parent, proc_state ) ) {
dlog("notify_parent: " PID_F " released from wait\n", parent);
ptlib_continue(PTRACE_SYSCALL, parent, 0);
}
}
}
static void handle_exit( pid_t pid, int status, const struct rusage &usage )
{
// Let's see if the process doing the exiting is even registered
pid_state *proc_state=lookup_state(pid);
dlog(NULL);
assert(proc_state!=NULL);
// Set the process state to ZOMBIE with usage count of 1
proc_state->state=pid_state::ZOMBIE;
proc_state->context_state[0]=1;
dlog("%s: " PID_F " is now a zombie\n", __func__, pid );
pid_state *parent_state=lookup_state(proc_state->parent);
// Notify the parent
#if PTLIB_PARENT_CAN_WAIT
// If a parent can wait on a debugged child we need to notify it even if the child is being debugged,
// but only if it actually has a parent (i.e. - was not reparented to init)
// Of course, if the debugger IS the parent, there is no need to notify it twice
if( proc_state->parent!=0 && proc_state->parent!=1 )
#else
// If a parent cannot wait, we need to let it know ourselves only if it's not being debugged
if( (proc_state->debugger==0 || proc_state->debugger==proc_state->parent) && proc_state->parent!=0 && proc_state->parent!=1 )
#endif
{
proc_state->context_state[0]++; // Update use count
notify_parent( proc_state->parent, pid_state::wait_state( pid, status, &usage, false ) );
}
// Regardless of whether it is being notified or not, the parent's child num needs to be decreased
if( parent_state!=NULL ) {
parent_state->num_children--;
}
if( proc_state->debugger!=0 && proc_state->debugger!=proc_state->parent ) {
// The process was being debugged - notify the debugger as well
proc_state->context_state[0]++; // Update use count
notify_parent( proc_state->parent, pid_state::wait_state( pid, status, &usage, true ) );
state[proc_state->debugger].num_debugees--;
}
// Is any process a child of this process?
// We need to delete all child zombie processes. This means changing the list while scanning it.
// Instead, create a list of pids to delete
std::set<pid_t> need_delete;
for( std::unordered_map<pid_t, pid_state>::iterator i=state.begin(); i!=state.end(); ++i ) {
if( i->second.parent==pid ) {
dlog("Reparenting process %d to init from %d\n", i->first, pid);
i->second.parent=1;
if( i->second.state==pid_state::ZOMBIE ) {
// "init" should release it
need_delete.insert(i->first);
}
}
if( i->second.debugger==pid ) {
dlog("Detaching process %d from recursive debugger %d\n", i->first, pid );
i->second.debugger=0;
if( i->second.state==pid_state::ZOMBIE && i->second.parent!=pid ) {
// The process is in zombie state, pid is its debugger but not parent
need_delete.insert(i->first);
}
}
}
for( std::set<pid_t>::iterator i=need_delete.begin(); i!=need_delete.end(); ++i ) {
delete_state(*i);
}
// Delete the state from our end. The state is reference counted, so it may not actually be deleted just yet
delete_state(pid);
}
void handle_new_process( pid_t parent_id, pid_t child_id )
{
// Copy the session information
pid_state *child=&state[child_id]; // We actually want to create the state if it did not already exist
if( child->state!=pid_state::INIT ) {
// Due to platform incompatibilities and other issues, we may be called several times over the same
// child. Don't make a fuss - just return.
dlog("%s: Process " PID_F " already registered - not performing any operation\n", __FUNCTION__, child_id );
return;
}
dlog("%s: Registering " PID_F " with parent " PID_F "\n", __FUNCTION__, child_id, parent_id );
// The platform may want to init the process in some way
ptlib_prepare(child_id);
// If this is a new root process, we do not actually start monitoring it just yet.
if( parent_id!=-1 )
child->state=pid_state::NONE;
pid_state *parent=lookup_state(parent_id);
if( parent!=NULL ) {
// If this assert fails, we somehow created a -1 process - not good
dlog(NULL);
assert(parent_id!=-1);
// This process is not a root process - it has a parent
// Copy everything from the parent, except what you don't copy
*child=*parent;
int_ptr process_type=parent->context_state[0];
if( (process_type&NEW_PROCESS_SAME_PARENT)==0 )
child->parent=parent_id;
pid_state *child_parent=lookup_state(child->parent);
if( child_parent!=NULL ) {
child_parent->num_children++;
}
child->num_children=0;
child->num_debugees=0;
// Whether the VM was copied or shared, the new process has the same static and shared memory
// If the VM is not shared, setting shared_memory but not shared_mem_local is an indication that the
// old memory needs to be freed
if( (process_type&NEW_PROCESS_SAME_VM)==0 ) {
// The processes do not share the same VM
child->mem=ref_count<pid_state::process_memory>(new pid_state::process_memory);
child->mem->set_remote_static(parent->mem->get_mem());
/*
The remote shared pointer for the parent is also valid for the child, but it is the same memory,
not a copy.
Keep the "shared_memory" pointer valid, but the "shared_mem_local" will be NULL to signify this is
memory we need to munmap.
*/
child->mem->set_remote_shared( parent->mem->get_shared() );
}
if( (process_type&NEW_PROCESS_SAME_DEBUGGER)==0 ) {
// The process inherits the debugger from the parent
child->debugger=0;
child->trace_mode=TRACE_DETACHED;
}
} else {
// This is a root process - no parent. Set it with the real session ID
child->session_id=getsid(child_id);
child->root=ref_count<std::string>(new std::string());
}
num_processes++;
}
int process_sigchld( pid_t pid, enum PTLIB_WAIT_RET wait_state, int status, long ret )
{
long sig=0;
pid_state *proc_state=lookup_state(pid);
if( wait_state!=NEWPROCESS && proc_state==NULL ) {
// The process does not exist!
// Register it
pid_t parent_pid=ptlib_get_parent(pid);
dlog("Caught unknown new process " PID_F ", detected parent " PID_F "\n", pid, parent_pid );
dlog(NULL);
assert( parent_pid==0 || parent_pid==1 || state.find(parent_pid)!=state.end() ); // Make sure the parent is, indeed, ours
// Handle the process creation before handling the syscall return
process_sigchld( parent_pid, NEWPROCESS, status, pid );
// Handle the rest of the syscall as a return from a syscall
wait_state=SYSCALL;
proc_state=lookup_state(pid);
assert(proc_state!=NULL);
ret=proc_state->orig_sc;
}
switch(wait_state) {
case SYSCALL:
{
bool posttrap_always=false;
if( proc_state->state==pid_state::REDIRECT1 ) {
// REDIRECT1 is just a filler state between the previous call, where the arguments were set up and
// the call initiated, and the call's return (REDIRECT2). No need to actually call the handler
dlog(PID_F ": Calling syscall %ld redirected from %s\n", pid, ret, syscalls[proc_state->orig_sc].name );
proc_state->state=pid_state::REDIRECT2;
} else if( proc_state->state==pid_state::REDIRECT2 || proc_state->state==pid_state::REDIRECT3 ) {
// REDIRECT2 means a return from a syscall generated by us.
// REDIRECT3 means entering a syscall generated by us, but for which the handler function would like
// to be notified (unlike REDIRECT1 above, which is short circuited)
if( proc_state->orig_sc!=SYS_execve ) {
dlog(PID_F ": Called syscall %ld, redirected from %s\n", pid, ret, syscalls[proc_state->orig_sc].name );
if( !syscalls[proc_state->orig_sc].func( ret, pid, proc_state ) )
sig=-1; // Mark for ptrace not to continue the process
} else {
// Special handling of the execve case
dlog(PID_F ": Called syscall %ld, redirected from execve\n", pid, ret );
if( !sys_execve( ret, pid, proc_state, posttrap_always ) )
sig=-1;
}
} else {
if( proc_state->state==pid_state::ALLOCATE ) {
if( !handle_memory_allocation( ret, pid, proc_state ) )
sig=-1;
}
if( proc_state->state!=pid_state::ALLOCATE ) {
// Sanity check - returning from same syscall that got us in
if( proc_state->state==pid_state::RETURN && ret!=proc_state->orig_sc ) {
dlog("process " PID_F " orig_sc=%d actual sc=%ld state=%s\n", pid, proc_state->orig_sc, ret,
state2str(proc_state->state));
dlog(NULL);
assert( proc_state->state!=pid_state::RETURN || ret==proc_state->orig_sc );
}
if( proc_state->state==pid_state::NONE && proc_state->debugger!=0 && proc_state->trace_mode==TRACE_SYSCALL ) {
dlog(PID_F ": pre-syscall hook called for debugger " PID_F "\n", pid, proc_state->debugger );
// Notify the debugger before the syscall
proc_state->context_state[0]=wait_state;
proc_state->context_state[1]=status;
proc_state->context_state[2]=ret;
proc_state->trace_mode=TRACE_STOPPED1;
pid_state::wait_state waiting;
waiting.pid()=pid;
waiting.status()=status;
getrusage( RUSAGE_CHILDREN, &waiting.usage() ); // XXX BUG This is the wrong function!
waiting.debugonly()=true;
notify_parent( proc_state->debugger, waiting );
sig=-1; // We'll halt the program until the "debugger" decides what to do with it
} else if( !proc_state->mem->get_loc() && proc_state->state==pid_state::NONE && ret!=SYS_execve && ret!=SYS_exit ) {
// We need to allocate memory
// No point in allocating memory when we are just entering an execve that will get rid of it
if( !allocate_process_mem( pid, proc_state, ret ) )
sig=-1;
} else {
// No debugger or otherwise we need to go ahead with this syscall
if( (proc_state->trace_mode&TRACE_MASK2)==TRACE_STOPPED1 ) {
proc_state->trace_mode&=TRACE_MASK1;
// The debugger may have changed the system call to execute - we will respect it
ret=ptlib_get_syscall( pid );
}
if( proc_state->state==pid_state::NONE )
// Store the syscall type here (we are not in override)
proc_state->orig_sc=ret;
if( syscalls.find(ret)!=syscalls.end() ) {
dlog(PID_F ": Called %s(%s)\n", pid, syscalls[ret].name, state2str(proc_state->state));
if( !syscalls[ret].func( ret, pid, proc_state ) ) {
sig=-1; // Mark for ptrace not to continue the process
}
} else if( ret==SYS_execve ) {
dlog(PID_F ": Called execve(%s)\n", pid, state2str(proc_state->state));
if( !sys_execve(ret, pid, proc_state, posttrap_always ) )
sig=-1;
} else {
dlog(PID_F ": Unknown syscall %ld(%s)\n", pid, ret, state2str(proc_state->state));
if( proc_state->state==pid_state::NONE ) {
proc_state->state=pid_state::RETURN;
} else if( proc_state->state==pid_state::RETURN ) {
proc_state->state=pid_state::NONE;
}
}
}
}
}
// Check for post-syscall debugger callback
// If the system sends a SIGTRAP after a successful execve, the logic is entirely different
if( proc_state->debugger!=0 && (
(proc_state->state==pid_state::NONE && proc_state->trace_mode==TRACE_SYSCALL) ||
posttrap_always )
)
{
dlog(PID_F ": notify debugger " PID_F " about post-syscall hook\n", pid, proc_state->debugger );
proc_state->trace_mode=TRACE_STOPPED2;
pid_state::wait_state waiting;
waiting.pid()=pid;
waiting.status()=status;
getrusage( RUSAGE_CHILDREN, &waiting.usage() ); // XXX BUG This is the wrong function!
waiting.debugonly()=true;
notify_parent( proc_state->debugger, waiting );
sig=-1; // Halt process until "debugger" decides it can keep on going
}
}
break;
case SIGNAL:
dlog(PID_F ": Signal %s\n", pid, sig2str(ret));
if( proc_state->debugger==0 ) {
if( proc_state->state==pid_state::INIT ) {
// When a process is being debugged, it appears to receive a SIGSTOP after the PTRACE_ATTACH. We use
// that signal to synchronize the states of the debugee and us
if( ret==SIGSTOP ) {
dlog(PID_F ": initial signal of process\n", pid );
sig=0;
proc_state->state=pid_state::NONE;
} else {
dlog(PID_F ": received unexpected signal while in INIT mode\n", pid);
// Deliver the signal and continue the process - the SIGSTOP may yet be coming
ptrace( PTRACE_CONT, pid, ret, 0 );
sig=-1;
}
} else {
sig=ret;
}
} else {
// Pass the signal to the debugger
pid_state::wait_state waiting;
waiting.pid()=pid;
waiting.status()=status;
getrusage( RUSAGE_CHILDREN, &waiting.usage() ); // XXX BUG this is the wrong function!
waiting.debugonly()=true;
proc_state->trace_mode=TRACE_STOPPED2;
notify_parent( proc_state->debugger, waiting );
sig=-1;
}
break;
case EXIT:
case SIGEXIT:
{
if( wait_state==EXIT ) {
dlog(PID_F ": Exit with return code %ld\n", pid, ret);
} else {
dlog(PID_F ": Exit with %s\n", pid, sig2str(ret));
}
struct rusage rusage;
getrusage( RUSAGE_CHILDREN, &rusage );
handle_exit(pid, status, rusage );
// If this was a root child, we may need to perform notification of exit status
std::unordered_map<pid_t, int>::iterator root_child=root_children.find(pid);
if( root_child!=root_children.end() ) {
if( root_child->second!=-1 ) {
write( root_child->second, &status, sizeof(status) );
close( root_child->second );
}
root_children.erase(root_child);
}
num_processes--;
}
break;
case NEWPROCESS:
{
dlog(PID_F ": Created new child process %ld\n", pid, ret);
handle_new_process( pid, ret );
}
}
return sig;
}
bool attach_debugger( pid_t child )
{
dlog(NULL);
// Attach a debugger to the child
if( ptrace(PTRACE_ATTACH, child, 0, 0)!=0 ) {
dlog("Could not start trace of process " PID_F ": %s\n", child, strerror(errno) );
throw errno_exception( "Could not start trace of process" );
}
dlog("Debugger successfully attached to process " PID_F "\n", child );
// Child has started, and is debugged
// root_children[child]=socket; // Mark this as a root child
// XXX Above line disabled pending rewrite of "parent can't wait" code
handle_new_process( -1, child ); // No parent - a root process
return true;
}
// Do nothing signal handler for sigchld
static void sigchld_handler(int signum)
{
}
// Signify whether an alarm was received while we were waiting
static bool alarm_happened=false;
static void sigalrm_handler(int signum)
{
alarm_happened=true;
}
int process_children( daemonProcess *daemon )
{
// Initialize the ptlib library
ptlib_init();
init_handlers();
init_globals();
dlog( "Begin the process loop\n" );
// Prepare the signal masks so we do not lose SIGCHLD while we wait
struct sigaction action;
memset( &action, 0, sizeof( action ) );
action.sa_handler=sigchld_handler;
sigemptyset( &action.sa_mask );
action.sa_flags=0;
sigaction( SIGCHLD, &action, NULL );
action.sa_handler=sigalrm_handler;
sigaction( SIGALRM, &action, NULL );
sigset_t orig_signals, child_signals;
sigemptyset( &child_signals );
sigaddset( &child_signals, SIGCHLD );
sigaddset( &child_signals, SIGALRM );
sigprocmask( SIG_BLOCK, &child_signals, &orig_signals );
sigdelset( &orig_signals, SIGCHLD );
sigdelset( &orig_signals, SIGALRM );
bool clientsockets=true;
while(num_processes>0 || clientsockets) {
int status;
pid_t pid;
long ret;
ptlib_extra_data data;
enum PTLIB_WAIT_RET wait_state;
if( !ptlib_wait( &pid, &status, &data, true ) ) {
if( errno==EAGAIN || (errno==ECHILD && num_processes==0) ) {
clientsockets=daemon->handle_request( &orig_signals, num_processes>0 );
// Did an alarm signal arrive?
if( alarm_happened ) {
alarm_happened=false;
dump_states();
}
} else if( errno==ECHILD ) {
// We should never get here. If we have no more children, we should have known about it already
dlog( "BUG - ptlib_wait failed with %s while numchildren is still %d\n", strerror(errno), num_processes );
dlog(NULL);
num_processes=0;
} else {
dlog("ptlib_wait failed %d: %s\n", errno, strerror(errno) );
}
continue;
}
ret=ptlib_parse_wait( pid, status, &wait_state );
long sig=process_sigchld( pid, wait_state, status, ret );
// The show must go on
if( sig>=0 )
ptlib_continue(PTRACE_SYSCALL, pid, sig);
}
return 0;
}
bool allocate_process_mem( pid_t pid, pid_state *state, int sc_num )
{
dlog("allocate_process_mem: " PID_F " running syscall %d needs process memory\n", pid, sc_num );
state->orig_sc=sc_num;
// Save the old state
ptlib_save_state( pid, state->saved_state );
state->state=pid_state::ALLOCATE;
if( state->mem->get_shared()!=0 )
state->context_state[0]=20; // Internal allocation state
else
state->context_state[0]=0; // Internal allocation state
return handle_memory_allocation( sc_num, pid, state );
}
static bool allocate_shared_mem( pid_t pid, pid_state *state )
{
char filename[PATH_MAX];
const char *tmpdir=getenv("FAKEROOT_TMPDIR");
if( tmpdir==NULL )
tmpdir=getenv("TMPDIR");
if( tmpdir==NULL || strlen(tmpdir)>=PATH_MAX-sizeof("/fakeroot-ng.XXXXXX") )
tmpdir=DEFAULT_TMPDIR;
sprintf(filename, "%s/fakeroot-ng.XXXXXX", tmpdir);
int fd=mkstemp(filename);
if( fd==-1 ) {
dlog("allocate_shared_mem: " PID_F " Failed to create file %s: %s\n", pid, filename, strerror(errno) );
// We'll kill the process
ptlib_continue( PTRACE_KILL, pid, 0 );
return false; // Freeze the process until the signal arrives
}
// Make sure that the file is big enough, but create it sparse
ftruncate( fd, shared_mem_size );
// Map the file into the local address space
char *memory=(char *)mmap( NULL, shared_mem_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
if( memory==MAP_FAILED ) {
dlog("allocate_shared_mem: " PID_F " filed to map file %s into memory: %s\n", pid, filename, strerror(errno) );
// Cleanup
close(fd);
unlink(filename);
ptlib_continue( PTRACE_KILL, pid, 0 );
return false;
}
// Fill in the memory with necessary commands and adjust the pointer
memcpy( memory, ptlib_prepare_memory(), ptlib_prepare_memory_len() );
// We need to remember the name of the temporary file so we can unlink it
strcpy(memory+ptlib_prepare_memory_len(), filename);
// Cleanup
close(fd);
// Set the shared memory class to know who we are
state->mem->set_local_addr(memory, shared_mem_size, ptlib_prepare_memory_len());
// The local shared memory is mapped. Now we need to map the remote end
// Generate a new system call
// Copy the instructions for generating a syscall to the newly created memory
ptlib_set_mem( pid, ptlib_prepare_memory(), state->mem->get_mem(), ptlib_prepare_memory_len() );
// Fill in the parameters to open the same file
ptlib_set_argument( pid, 1, state->mem->get_mem()+ptlib_prepare_memory_len() );
ptlib_set_string( pid, state->mem->get_loc_c(), state->mem->get_mem()+ptlib_prepare_memory_len() );
ptlib_set_argument( pid, 2, O_RDONLY );
return true;
}
// Table of states:
// Start states - if have nothing - 0
// if have static buffer and old shared buffer - 20
// First we define the various stages of the state machine:
static bool hma_state0( int sc_num, pid_t pid, pid_state *state )
{
// Translate the whatever call into an mmap to allocate the process local memory
ptlib_set_syscall( pid, PREF_MMAP );
ptlib_set_argument( pid, 1, 0 ); // start pointer
ptlib_set_argument( pid, 2, static_mem_size ); // Length of page(s)
ptlib_set_argument( pid, 3, (PROT_EXEC|PROT_READ|PROT_WRITE) ); // Protection - allow execute
ptlib_set_argument( pid, 4, (MAP_PRIVATE|MAP_ANONYMOUS) ); // Flags - anonymous memory allocation
ptlib_set_argument( pid, 5, -1 ); // File descriptor
ptlib_set_argument( pid, 6, 0 ); // Offset
return true;
}
static bool hma_state1( int sc_num, pid_t pid, pid_state *state )
{
// First step - mmap just returned