-
Notifications
You must be signed in to change notification settings - Fork 4
/
hooks.c
1271 lines (1076 loc) · 36.2 KB
/
hooks.c
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
/*
fenris - program execution path analysis tool
---------------------------------------------
Copyright (C) 2001, 2002 by Bindview Corporation
Portions copyright (C) 2001, 2002 by their respective contributors
Developed and maintained by Michal Zalewski <[email protected]>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
Here are breakpoint handling routines and communication code needed
to talk with Aegir or any other debugger. This file exists so that
modification to Fenris can be minimized.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/fcntl.h>
#include <time.h>
#include <alloca.h>
#include <string.h>
#include <signal.h>
#include <dlfcn.h>
#include <sys/socket.h>
#include <linux/un.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <errno.h>
#include <bfd.h>
#include "fenris.h"
#include "fdebug.h"
#include "hooks.h"
#include "libfnprints.h"
// FIXME:NIX odd, ostream NEVER created, only extern'd
#undef debug
#define debug(x...) fprintf(ostream,x)
// FIXME:NIX only extern'd here, also, fatal never created that I can find. We'll see
extern FILE *ostream;
extern void fatal(const char *x, const int err);
extern struct signed_user_regs_struct r; // Current process:
// registers
extern struct fenris_process *current; // Currently traced process
extern int T_dostep;
extern char T_goaway;
int was_blocking;
// FIXME:NIX hardcoded?!? more than 300+ for i386
extern char *scnames[256];
int break_stopped; // Is the code stopped?
int sd; // Our socket to the shell
int curmode; // Current mode (see MODE_* in hooks.h)
int modepar; // Current mode parameter, if any.
int break_continuing; // Recovering from stopped inside
// singlestep()?
extern char is_static;
#define BP_SYS 1
#define BP_SIG 2
#define BP_ADDR 3
#define BP_R 4
#define BP_W 5
struct break_bpnt {
char type; // BP_*
unsigned int param;
unsigned int param2;
};
struct break_bpnt bp[MAXBREAK];
char *break_getname(unsigned int addr);
char break_shutup;
int blocking_syscall;
void send_message(int type, void *data, int len)
{
int q;
struct dmsg_header d;
if (sd <= 0)
return;
d.magic1 = DMSG_MAGIC1;
d.magic2 = DMSG_MAGIC2;
if ((type != DMSG_REPLY) && (type != DMSG_ASYNC))
fatal("unknown message type in send_message", 0);
d.type = type;
if (!blocking_syscall)
d.code_running = !break_stopped;
else
d.code_running = -(blocking_syscall);
errno = 0;
q = write(sd, &d, sizeof(struct dmsg_header));
if (q != sizeof(struct dmsg_header)) {
if (break_shutup)
return;
fatal("cannot send message to debugger", errno);
}
if (data && len) {
q = write(sd, data, len);
if (q != len)
fatal("short write to debugger", errno);
}
};
char str_buf[MAXFENT];
char *get_string_sock(int sock)
{
char t[2];
t[1] = 0;
str_buf[0] = 0;
fcntl(sock, F_SETFL, O_SYNC);
while (1) {
if (read(sock, t, 1) != 1)
fatal("short read in get_string_sock from client", 0);
if (!t[0]) {
fcntl(sock, F_SETFL, O_NONBLOCK);
return str_buf;
}
strcat(str_buf, t);
if (strlen(str_buf) >= sizeof(str_buf) - 2)
fatal("string from client is of excessive length", 0);
}
fatal("Another broken Turing machine. Rhubarb.", 0);
}
#define send_asynctext(x) send_message(DMSG_ASYNC,(x),strlen(x)+1)
#define send_synctext(x) send_message(DMSG_REPLY,(x),strlen(x)+1)
void del_break(int i)
{
char buf[1000];
if (i < 0 || i >= MAXBREAK) {
send_synctext("Breakpoint number of out range.\n");
return;
}
switch (bp[i].type) {
case 0:
send_synctext("There is no such breakpoint active.\n");
break;
// FIXME:NIX hardcoded?!? more than 300+ for i386
case BP_SYS:
sprintf(buf,
"Deleted on-syscall breakpoint previously set to '%s'.\n",
scnames[bp[i].param & 0xff]);
send_synctext(buf);
break;
case BP_SIG:
sprintf(buf,
"Deleted on-signal breakpoint previously set to signal %d.\n",
bp[i].param);
send_synctext(buf);
break;
case BP_ADDR:
sprintf(buf, "Deleted breakpoint at 0x%x.\n", bp[i].param);
send_synctext(buf);
break;
case BP_R:
sprintf(buf, "Deleted read watchpoint at 0x%x-0x%x.\n",
bp[i].param, bp[i].param2);
send_synctext(buf);
break;
case BP_W:
sprintf(buf, "Deleted write watchpoint at 0x%x-0x%x.\n",
bp[i].param, bp[i].param2);
send_synctext(buf);
break;
default:
send_synctext("Fascinating programming glitch.\n");
}
bp[i].type = 0;
}
void add_break(int type, int p1, int p2)
{
int i;
char buf[1000];
for (i = 0; i < MAXBREAK; i++)
if (!bp[i].type)
break;
if (i == MAXBREAK) {
send_synctext
("No free breakpoint slots (delete some old first).\n");
return;
}
bp[i].type = type;
bp[i].param = p1;
bp[i].param2 = p2;
sprintf(buf, "Breakpoint #%d added.\n", i);
send_synctext(buf);
}
void list_break(void)
{
int i, tot = 0;
char buf[1000];
char obuf[sizeof(buf) * MAXBREAK];
obuf[0] = 0;
for (i = 0; i < MAXBREAK; i++) {
if (!bp[i].type)
continue;
tot++;
switch (bp[i].type) {
// FIXME:NIX hardcoded?!? more than 300+ for i386
case BP_SYS:
sprintf(buf, "%02d: stop on syscall %s (%d)\n", i,
scnames[bp[i].param & 0xff], bp[i].param);
strcat(obuf, buf);
break;
case BP_SIG:
sprintf(buf, "%02d: stop on signal %d\n", i, bp[i].param);
strcat(obuf, buf);
break;
case BP_ADDR:
sprintf(buf, "%02d: stop at address 0x%x.\n", i,
bp[i].param);
strcat(obuf, buf);
break;
case BP_R:
sprintf(buf, "%02d: stop on read 0x%x-0x%x.\n", i,
bp[i].param, bp[i].param2);
strcat(obuf, buf);
break;
case BP_W:
sprintf(buf, "%02d: stop on write 0x%x-0x%x.\n", i,
bp[i].param, bp[i].param2);
strcat(obuf, buf);
break;
default:
sprintf(buf, "%02d: amazingly unknown breakpoint.\n", i);
strcat(obuf, buf);
}
}
if (!tot)
sprintf(obuf, "No active breakpoints.\n");
send_synctext(obuf);
}
void break_listen(char *where, const char **argv)
{
struct sockaddr_un sun;
unlink(where);
if ((sd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
fatal("cannot create a socket", errno);
sun.sun_family = AF_LOCAL;
strncpy(sun.sun_path, where, UNIX_PATH_MAX);
if (bind(sd, (struct sockaddr *)&sun, sizeof(sun)))
fatal("cannot bind", errno);
if (listen(sd, 1))
fatal("cannot listen", errno);
debug("# Ready to accept debug session at %s.\n", where);
if ((sd = accept(sd, 0, 0)) < 0)
fatal("accept failed", errno);
debug("# Connection accepted! Entering interactive session.\n");
{
char buf[MAXFENT];
int n = 1;
char *data;
int q = time(0);
data = (void *)ctime((void *)&q);
if (data[strlen(data) - 1] == '\n')
data[strlen(data) - 1] = 0;
sprintf(buf,
"Welcome to Fenris debugger %s build %s running at PID %d.\n"
"Copyright (C) 2001, 2002 by Michal Zalewski <[email protected]>\n\n"
"Cur. time : %s\n" "Executable: %s\n" "Arguments : ",
VERSION, BUILD, getpid(), data, argv[1]);
if (!argv[n + 1])
strcat(buf, "<NULL>");
else
while (argv[++n]) {
strcat(buf, argv[n]);
strcat(buf, " ");
}
strcat(buf, "\n\n");
send_asynctext(buf);
}
}
extern int pid;
extern unsigned int get_handler(int i);
void get_mem(unsigned int start, unsigned int end)
{
unsigned int re = 0, i = 0;
unsigned int totlen = end - start;
int *first;
int *buf;
char *cbuf;
if (end <= start)
fatal("malformed GETMEM request", 0);
if (totlen > MAXFENT - 8)
totlen = MAXFENT - 8;
first = buf = alloca(totlen + 16);
if (!first)
fatal("alloca failed", 0);
buf++;
cbuf = (char *)buf;
while (re < totlen) {
errno = 0;
*buf = ptrace(PTRACE_PEEKDATA, pid, start + re, 0);
if (errno)
break;
buf++;
re += 4;
}
// Restore RET or NOP as needed.
for (i = 0; i < MAXSIG; i++) {
unsigned int h = get_handler(i);
if (h) {
h--;
}
if ((h >= start) && (h <= end)) {
if (current->shret[i]) {
cbuf[h - start] = 0xc3;
} else {
cbuf[h - start] = 0x90;
}
}
}
*first = re > totlen ? totlen : re;
send_message(DMSG_REPLY, first, *first + 4);
}
void get_regs(void)
{
struct signed_user_regs_struct x;
if (ptrace(PTRACE_GETREGS, pid, 0, &x))
memcpy(&x, &r, sizeof(x));
send_message(DMSG_REPLY, &x, sizeof(x));
}
void set_regs(void)
{
struct signed_user_regs_struct x;
if (read(sd, &x, sizeof(x)) != sizeof(x))
fatal("short read from client", 0);
ptrace(PTRACE_SETREGS, pid, 0, &x);
ptrace(PTRACE_GETREGS, pid, 0, &r);
if (x.rax != r.rax) { send_synctext("Failed to modify eax (blame ptrace).\n"); }
if (x.rbx != r.rbx) { send_synctext("Failed to modify ebx (blame ptrace).\n"); }
if (x.rcx != r.rcx) { send_synctext("Failed to modify ecx (blame ptrace).\n"); }
if (x.rdx != r.rdx) { send_synctext("Failed to modify edx (blame ptrace).\n"); }
if (x.rsp != r.rsp) { send_synctext("Failed to modify esp (blame ptrace).\n"); }
if (x.rip != r.rip) { send_synctext("Failed to modify eip (blame ptrace).\n"); }
if (x.rbp != r.rbp) { send_synctext("Failed to modify ebp (blame ptrace).\n"); }
if (x.rsi != r.rsi) { send_synctext("Failed to modify esi (blame ptrace).\n"); }
if (x.rdi != r.rdi) { send_synctext("Failed to modify edi (blame ptrace).\n"); }
if (x.eflags != r.eflags) { send_synctext("Failed to modify eflags (blame ptrace).\n"); }
if (x.ds != r.ds) { send_synctext("Failed to modify ds (blame ptrace).\n"); }
if (x.ss != r.ss) { send_synctext("Failed to modify ss (blame ptrace).\n"); }
if (x.gs != r.gs) { send_synctext("Failed to modify ss (blame ptrace).\n"); }
if (x.fs != r.fs) { send_synctext("Failed to modify fs (blame ptrace).\n"); }
if (x.es != r.es) { send_synctext("Failed to modify es (blame ptrace).\n"); }
if (x.cs != r.cs) { send_synctext("Failed to modify cs (blame ptrace).\n"); }
send_synctext("Register modified successfully.\n");
}
char getnamebuf[1024];
// From Fenris.
extern char *find_id_off(unsigned int c);
extern char *find_name_ex(unsigned int c, char prec, char non);
extern char *lookup_fnct(unsigned int c, unsigned int add, char prec);
const char *my_siglist[] = { "none", "sighup", "sigint", "sigquit",
"sigill", "sigtrap", "sigabrt", "sigbus", "sigfpe", "sigkill",
"sigusr1",
"sigsegv", "sigusr2", "sigpipe", "sigalrm", "sigterm", "sigchld",
"sigcont",
"sigstop", "sigtstp", "sigttin", "sigttou", "sigurg", "sigxcpu",
"sigxfsz",
"sigvtalrm", "sigprof", "sigwinch", "sigio", "sigpwr", "sigsys", 0
};
char *break_getname(unsigned int addr)
{
char *x;
getnamebuf[0] = 0;
// First, try library name.
x = find_name_ex(addr, 0, 1);
// Then, try local symbol.
if (!x)
x = lookup_fnct(addr, 123456, 0);
// Then, try our own function.
if (!x)
x = find_id_off(addr);
if (x)
strcpy(getnamebuf, x);
return getnamebuf;
}
extern unsigned int CODESEG;
extern int lookup_fnname(char *name);
extern char *get_addrdescr(const unsigned int q);
extern char *get_fddescr(const int fd);
extern char already_main;
void break_messenger(void)
{
int q;
char buf[1000];
int p[2];
char *fif;
struct dmsg_header d;
if (break_stopped)
if (!blocking_syscall && was_blocking)
send_asynctext(">> Successfully stopped after a while.\n");
was_blocking = blocking_syscall;
loopover:
// Check for messages.
fcntl(sd, F_SETFL, O_NONBLOCK);
errno = 0;
if (break_stopped && (!current || !current->syscall)) {
fd_set f;
FD_ZERO(&f);
FD_SET(sd, &f);
select(sd + 1, &f, 0, &f, 0);
}
q = read(sd, &d, sizeof(struct dmsg_header));
if (q <= 0) {
if (errno != EAGAIN)
fatal("connection dropped by remote client", -1);
else {
fcntl(sd, F_SETFL, O_SYNC);
return;
}
}
fcntl(sd, F_SETFL, O_SYNC);
if (d.magic1 != DMSG_MAGIC1)
fatal("magic1 incorrect in packet from client", 0);
if (d.magic2 != DMSG_MAGIC2)
fatal("magic2 incorrect in packet from client", 0);
switch (d.type) {
case DMSG_FOO:
send_message(DMSG_REPLY, 0, 0);
break;
case DMSG_SBREAK:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
if (T_goaway) {
break_goaway();
break;
}
add_break(BP_SYS, p[0], 0);
break;
case DMSG_DEL:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
del_break(p[0]);
break;
case DMSG_ABREAK:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
add_break(BP_ADDR, p[0], 0);
break;
case DMSG_IBREAK:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
add_break(BP_SIG, p[0], 0);
break;
case DMSG_RWATCH:
if (read(sd, &p[0], 8) != 8)
fatal("short read from client", 0);
if (T_goaway) {
break_goaway();
break;
}
add_break(BP_R, p[0], p[1]);
break;
case DMSG_WWATCH:
if (read(sd, &p[0], 8) != 8)
fatal("short read from client", 0);
if (T_goaway) {
break_goaway();
break;
}
add_break(BP_W, p[0], p[1]);
break;
case DMSG_LISTBREAK:
list_break();
break;
case DMSG_GETMEM:
if (read(sd, &p[0], 8) != 8)
fatal("short read from client", 0);
get_mem(p[0], p[1]);
break;
case DMSG_GETREGS:
get_regs();
break;
case DMSG_SETREGS:
set_regs();
break;
case DMSG_STEP:
errno = 0;
if (read(sd, &modepar, 4) != 4)
fatal("short read from client", 0);
break_stopped = 0;
curmode = MODE_SINGLE;
sprintf(buf, "At 0x%llx, advancing by %d local code instruction(s)...\n", r.rip, modepar);
if (INLIBC(r.rip))
strcat(buf, "NOTE: you were in libc. Continuing to to local code. Hold on.\n");
send_synctext(buf);
break;
case DMSG_GETNAME:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
send_synctext(break_getname(p[0]));
break;
case DMSG_STOP:
break_stopped = 1;
curmode = MODE_NONE;
if (blocking_syscall) {
sprintf(buf,
"Trying to stop at 0x%llx, but in blocking call %d [%s]...\n"
"Send SIGTRAP to pid %d or try 'halt' command to stop immediately.\n",
r.rip, blocking_syscall,
// FIXME:NIX hardcoded?!? more than 300+ for i386
scnames[blocking_syscall & 0xff], pid);
} else {
sprintf(buf, ">> Successfully stopped at 0x%llx...\n", r.rip);
}
send_synctext(buf);
break;
case DMSG_HALT:
break_stopped = 1;
curmode = MODE_NONE;
if (blocking_syscall) {
kill(pid, SIGTRAP);
current->syscalldone = 1;
sprintf(buf,
">> Forced stop at 0x%llx in blocking call %d [%s].\n",
r.rip, blocking_syscall,
// FIXME:NIX hardcoded?!? more than 300+ for i386
scnames[blocking_syscall & 0xff]);
blocking_syscall = 0;
} else {
sprintf(buf, ">> Successfully stopped at 0x%llx...\n", r.rip);
}
send_synctext(buf);
break;
case DMSG_RUN:
break_stopped = 0;
curmode = MODE_RUN;
sprintf(buf, "Resuming at 0x%llx...\n", r.rip);
send_synctext(buf);
break;
case DMSG_GETADDR:
fif = get_string_sock(sd);
p[0] = lookup_fnname(fif);
send_message(DMSG_REPLY, &p[0], 4);
break;
case DMSG_GETBACK:
if (T_goaway) {
break_goaway();
break;
}
{
char back[MAXFENT];
char small[1000];
unsigned int i;
if (current->fntop < 1)
sprintf(back,
"No local function calls recorded (you are in main).\n");
else
sprintf(back,
"Local function calls history (oldest to most recent calls):\n");
for (i = 1; i <= current->fntop; i++) {
sprintf(small, "From %x [%s]: fnct_%d ",
current->fnrip[i],
break_getname(current->fnrip[i]),
current->fnid[i]);
sprintf(&small[strlen(small)],
"[%s] %x, stack %x -> %x\n",
break_getname((*current->fnaddr)[current->
fnid[i] - 1]),
(*current->fnaddr)[current->fnid[i] - 1],
current->frend[i], current->frstart[i]);
strcat(back, small);
}
send_synctext(back);
}
break;
case DMSG_DESCADDR:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
fif = get_addrdescr(p[0]);
if (!strlen(fif))
sprintf(fif =
buf,
"No additional information about address %x [%s].\n",
p[0], break_getname(p[0]));
send_synctext(fif);
break;
case DMSG_DESCFD:
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
fif = get_fddescr(p[0]);
if (!strlen(fif))
sprintf(fif =
buf, "No additional information about fd %d.\n",
p[0]);
send_synctext(fif);
break;
case DMSG_TORET:
if (read(sd, &modepar, 4) != 4)
fatal("short read from client", 0);
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_RET;
sprintf(buf, "At 0x%llx, continuing to local RET no. %d...\n",
r.rip, modepar);
if (INLIBC(r.rip))
strcat(buf, "NOTE: you were in libc. Continuing to to local code. Hold on.\n");
send_synctext(buf);
break;
case DMSG_DYNAMIC:
if (T_goaway) {
break_goaway();
break;
}
if (is_static) {
send_synctext
("This is a statically linked application.\n");
return;
}
if (already_main) {
send_synctext("You are already in the main code.\n");
return;
}
break_stopped = 0;
curmode = MODE_DYN;
sprintf(buf, "At 0x%llx, continuing to the main code...\n", r.rip);
send_synctext(buf);
break;
case DMSG_TOLIBCALL:
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_LIBCALL;
sprintf(buf, "At 0x%llx, continuing to next libcall...\n", r.rip);
if (INLIBC(r.rip)) {
strcat(buf, "NOTE: you were in libc. Continuing to to local code. Hold on.\n");
}
send_synctext(buf);
break;
case DMSG_TOSYSCALL:
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_SYSCALL;
sprintf(buf, "At 0x%llx, continuing to next syscall...\n", r.rip);
send_synctext(buf);
break;
case DMSG_TOLOCALCALL:
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_CALL;
sprintf(buf, "At 0x%llx, continuing to next local call...\n", r.rip);
if (INLIBC(r.rip)) {
strcat(buf, "NOTE: you were in libc. Continuing to to local code. Hold on.\n");
}
send_synctext(buf);
break;
case DMSG_TOLOWERNEST:
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_NEST;
modepar = current->nest;
sprintf(buf, "At 0x%llx, continuing to lower nest...\n", r.rip);
send_synctext(buf);
break;
case DMSG_TONEXT:
if (T_goaway) {
break_goaway();
break;
}
break_stopped = 0;
curmode = MODE_LINE;
sprintf(buf, "At 0x%llx, continuing to next output line...\n", r.rip);
send_synctext(buf);
break;
case DMSG_SIGNALS:
if (T_goaway) {
break_goaway();
break;
}
{
int i;
char buf[MAXFENT];
char small[1000];
int got = 0;
buf[0] = 0;
for (i = 1; i < MAXMYSIG; i++)
if (get_handler(i)) {
got = 1;
sprintf(small,
"- signal %d (%s) handled by 0x%x [%s]\n",
i, my_siglist[i], get_handler(i),
break_getname(get_handler(i)));
strcat(buf, small);
}
if (got)
strcat(buf,
"All remaining signals have default OS settings.\n");
else
strcpy(buf,
"No syscall handlers defined, all signals set to default.\n");
send_synctext(buf);
}
break;
case DMSG_SETMEM:
{
unsigned int x;
if (read(sd, &p[0], 8) != 8)
fatal("short read from client", 0);
errno = 0;
x = ptrace(PTRACE_PEEKDATA, pid, p[0], 0);
if (errno) {
sprintf(buf, "Cannot access memory at address 0x%x.\n",
p[0]);
send_synctext(buf);
return;
}
x = (x & 0xffffff00) + (p[1] & 0xff);
errno = 0;
ptrace(PTRACE_POKEDATA, pid, p[0], x);
if (errno) {
sprintf(buf, "Cannot modify memory at address 0x%x.\n",
p[0]);
send_synctext(buf);
return;
}
sprintf(buf, "Memory at address 0x%x modified.\n", p[0]);
send_synctext(buf);
}
break;
case DMSG_KILL:
fatal("DMSG_KILL received from client", -1);
break;
case DMSG_FPRINT:
{
unsigned char sig[SIGNATSIZE + 4];
int i;
unsigned int gotsig;
char buf[MAXFENT];
buf[0] = 0;
if (read(sd, &p[0], 4) != 4)
fatal("short read from client", 0);
errno = 0;
for (i = 0; i < SIGNATSIZE / 4; i++)
AS_UINT(sig[i * 4]) =
ptrace(PTRACE_PEEKDATA, pid, p[0] + i * 4, 0);
if (errno) {
sprintf(buf,
"Cannot read memory at address 0x%x - 0x%x.\n",
p[0], p[0] + SIGNATSIZE);
send_synctext(buf);
return;
}
gotsig = fnprint_compute(sig);
{
#define MAXTORET 50
unsigned short sht = gotsig & 0xffff;
int got = 0;
struct fenris_fndb *cur;
cur = fndb[gotsig >> 16];
while (cur) {
if (cur->a == sht) {
if (!got) {
sprintf(buf,
"Matches for signature %08X: %s",
gotsig, cur->name);
} else {
if (got == MAXTORET) {
strcat(buf, ", ...");
} else if (got < MAXTORET) {
strcat(buf, ", ");
strcat(buf, cur->name);
}
}
got++;
}
cur = cur->next;
}
if (!got)
sprintf(buf, "Signature %08X, no matches.\n",
gotsig);
else
strcat(buf, "\n");
}
send_synctext(buf);
}
break;
case DMSG_GETMAP:
case DMSG_FDMAP:
case DMSG_FNLIST:
if (T_goaway) {
break_goaway();
break;
}
sprintf(buf, "This functionality is not yet implemented.\n");
send_synctext(buf);
break;
default:
fatal("unrecognized message from client", 0);
}
goto loopover;
}
int clearentity;
char break_entity[MAXFENT];
extern char T_nolast;
void break_sendentity(void)
{
if (!T_nolast)
send_asynctext(break_entity);
break_entity[0] = 0;
clearentity = 0;
}
void break_sendentity_force(void)
{
send_asynctext(break_entity);
break_entity[0] = 0;
clearentity = 0;
}
void break_newline(void)
{
char buf[1000];
clearentity = 1;
if (curmode == MODE_LINE)
if (strlen(break_entity)) {
curmode = MODE_NONE;
break_stopped = 1;
break_sendentity();
sprintf(buf, ">> New line stop at 0x%llx [%s].\n", r.rip, break_getname(r.rip));
send_asynctext(buf);
}
}
void break_append(char *fmt)
{
if (clearentity) {
break_entity[0] = 0;
clearentity = 0;
}