-
Notifications
You must be signed in to change notification settings - Fork 150
/
linuxKludges.C
1143 lines (1007 loc) · 33.3 KB
/
linuxKludges.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
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common/src/headers.h"
#include "common/src/parseauxv.h"
#include "common/src/linuxKludges.h"
#include "common/src/Types.h"
#include <elf.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
/**** process_vm_readv / process_vm_writev
* Added in kernel 3.2 and some backports -- try it and check ENOSYS.
* The wrappers are defined in glibc 2.15, otherwise make our own.
*/
#if !__GLIBC_PREREQ(2,15)
static ssize_t process_vm_readv(pid_t pid,
const struct iovec *local_iov, unsigned long liovcnt,
const struct iovec *remote_iov, unsigned long riovcnt,
unsigned long flags)
{
#ifdef SYS_process_vm_readv
return syscall(SYS_process_vm_readv,
pid, local_iov, liovcnt, remote_iov, riovcnt, flags);
#else
errno = ENOSYS;
return -1;
#endif
}
static ssize_t process_vm_writev(pid_t pid,
const struct iovec *local_iov, unsigned long liovcnt,
const struct iovec *remote_iov, unsigned long riovcnt,
unsigned long flags)
{
#ifdef SYS_process_vm_writev
return syscall(SYS_process_vm_writev,
pid, local_iov, liovcnt, remote_iov, riovcnt, flags);
#else
errno = ENOSYS;
return -1;
#endif
}
#endif /* !__GLIBC_PREREQ(2,15) */
typedef int (*intKludge)();
int P_getopt(int argc, char *argv[], const char *optstring)
{
/* On linux we prepend a + character */
char newopt[strlen(optstring)+5];
strcpy(newopt, "+");
strcat(newopt, optstring);
return getopt(argc, argv, newopt);
}
int P_copy(const char *from, const char *to) {
std::ifstream src(from, std::ios::binary);
std::ofstream dst(to, std::ios::binary | std::ios::trunc);
dst << src.rdbuf();
dst.close();
src.close();
return (src && dst) ? 0 : -1;
}
unsigned long long PDYN_div1000(unsigned long long in) {
/* Divides by 1000 without an integer division instruction or library call, both of
* which are slow.
* We do only shifts, adds, and subtracts.
*
* We divide by 1000 in this way:
* multiply by 1/1000, or multiply by (1/1000)*2^30 and then right-shift by 30.
* So what is 1/1000 * 2^30?
* It is 1,073,742. (actually this is rounded)
* So we can multiply by 1,073,742 and then right-shift by 30 (neat, eh?)
*
* Now for multiplying by 1,073,742...
* 1,073,742 = (1,048,576 + 16384 + 8192 + 512 + 64 + 8 + 4 + 2)
* or, slightly optimized:
* = (1,048,576 + 16384 + 8192 + 512 + 64 + 16 - 2)
* for a total of 8 shifts and 6 add/subs, or 14 operations.
*
*/
unsigned long long temp = in << 20; // multiply by 1,048,576
// beware of overflow; left shift by 20 is quite a lot.
// If you know that the input fits in 32 bits (4 billion) then
// no problem. But if it's much bigger then start worrying...
temp += in << 14; // 16384
temp += in << 13; // 8192
temp += in << 9; // 512
temp += in << 6; // 64
temp += in << 4; // 16
temp -= in >> 2; // 2
return (temp >> 30); // divide by 2^30
}
unsigned long long PDYN_divMillion(unsigned long long in) {
/* Divides by 1,000,000 without an integer division instruction or library call,
* both of which are slow.
* We do only shifts, adds, and subtracts.
*
* We divide by 1,000,000 in this way:
* multiply by 1/1,000,000, or multiply by (1/1,000,000)*2^30 and then right-shift
* by 30. So what is 1/1,000,000 * 2^30?
* It is 1,074. (actually this is rounded)
* So we can multiply by 1,074 and then right-shift by 30 (neat, eh?)
*
* Now for multiplying by 1,074
* 1,074 = (1024 + 32 + 16 + 2)
* for a total of 4 shifts and 4 add/subs, or 8 operations.
*
* Note: compare with div1000 -- it's cheaper to divide by a million than
* by a thousand (!)
*
*/
unsigned long long temp = in << 10; // multiply by 1024
// beware of overflow...if the input arg uses more than 52 bits
// than start worrying about whether (in << 10) plus the smaller additions
// we're gonna do next will fit in 64...
temp += in << 5; // 32
temp += in << 4; // 16
temp += in << 1; // 2
return (temp >> 30); // divide by 2^30
}
unsigned long long PDYN_mulMillion(unsigned long long in) {
unsigned long long result = in;
/* multiply by 125 by multiplying by 128 and subtracting 3x */
result = (result << 7) - result - result - result;
/* multiply by 125 again, for a total of 15625x */
result = (result << 7) - result - result - result;
/* multiply by 64, for a total of 1,000,000x */
result <<= 6;
/* cost was: 3 shifts and 6 subtracts
* cost of calling mul1000(mul1000()) would be: 6 shifts and 4 subtracts
*
* Another algorithm is to multiply by 2^6 and then 5^6.
* The former is super-cheap (one shift); the latter is more expensive.
* 5^6 = 15625 = 16384 - 512 - 256 + 8 + 1
* so multiplying by 5^6 means 4 shift operations and 4 add/sub ops
* so multiplying by 1000000 means 5 shift operations and 4 add/sub ops.
* That may or may not be cheaper than what we're doing (3 shifts; 6 subtracts);
* I'm not sure. --ari
*/
return result;
}
#if defined(cap_gnu_demangler)
#include <cxxabi.h>
using namespace abi;
#endif
char * P_cplus_demangle( const char * symbol, bool nativeCompiler,
bool includeTypes )
{
static char* last_symbol = NULL;
static bool last_native = false;
static bool last_typed = false;
static char* last_demangled = NULL;
if(last_symbol && last_demangled && (nativeCompiler == last_native)
&& (includeTypes == last_typed) && (strcmp(symbol, last_symbol) == 0))
{
return strdup(last_demangled);
}
#if defined(cap_gnu_demangler)
int status;
char *demangled = __cxa_demangle(symbol, NULL, NULL, &status);
if (status == -1) {
//Memory allocation failure.
return NULL;
}
if (status == -2) {
//Not a C++ name
return NULL;
}
assert(status == 0); //Success
#else
int opts = 0;
opts |= includeTypes ? DMGL_PARAMS | DMGL_ANSI : 0;
// [ pgcc/CC are the "native" compilers on Linux. Go figure. ]
// pgCC's mangling scheme most closely resembles that of the Annotated
// C++ Reference Manual, only with "some exceptions" (to quote the PGI
// documentation). I guess we'll demangle names with "some exceptions".
opts |= nativeCompiler ? DMGL_ARM : 0;
char * demangled = cplus_demangle( const_cast< char *>(symbol), opts);
#endif
if( demangled == NULL ) { return NULL; }
if( ! includeTypes ) {
/* de-demangling never increases the length */
char * dedemangled = strdup( demangled );
assert( dedemangled != NULL );
dedemangle( demangled, dedemangled );
assert( dedemangled != NULL );
free( demangled );
demangled = dedemangled;
}
free(last_symbol);
free(last_demangled);
last_native = nativeCompiler;
last_typed = includeTypes;
last_symbol = strdup(symbol);
last_demangled = strdup(demangled);
return demangled;
} /* end P_cplus_demangle() */
bool PtraceBulkRead(Address inTraced, unsigned size, void *inSelf, int pid)
{
static bool have_process_vm_readv = true;
const unsigned char *ap = (const unsigned char*) inTraced;
unsigned char *dp = (unsigned char *) inSelf;
Address w = 0x0; /* ptrace I/O buffer */
int len = sizeof(void *);
unsigned cnt;
if (0 == size) {
return true;
}
/* If process_vm_readv is available, we may be able to read it all in one syscall. */
if (have_process_vm_readv) {
struct iovec local_iov = { inSelf, size };
struct iovec remote_iov = { (void*)inTraced, size };
ssize_t ret = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
if (ret == -1) {
if (errno == ENOSYS) {
have_process_vm_readv = false;
} else if (errno == EFAULT || errno == EPERM) {
/* Could be a no-read page -- ptrace may be allowed to
* peek anyway, so fallthrough and let ptrace try.
* It may also be denied by kernel.yama.ptrace_scope=1 if we're
* no longer a direct ancestor thanks to pid re-parenting. */
} else {
return false;
}
} else if (ret < size) {
/* partial reads won't split an iovec, but we only have one... huh?! */
return false;
} else {
return true;
}
}
cnt = inTraced % len;
if (cnt) {
/* Start of request is not aligned. */
unsigned char *p = (unsigned char*) &w;
/* Read the segment containing the unaligned portion, and
copy what was requested to DP. */
errno = 0;
w = P_ptrace(PTRACE_PEEKDATA, pid, (Address) (ap-cnt), w, len);
if (errno) {
return false;
}
for (unsigned i = 0; i < len-cnt && i < size; i++)
dp[i] = p[cnt+i];
if (len-cnt >= size) {
return true; /* done */
}
dp += len-cnt;
ap += len-cnt;
size -= len-cnt;
}
/* Copy aligned portion */
while (size >= (u_int)len) {
errno = 0;
w = P_ptrace(PTRACE_PEEKTEXT, pid, (Address) ap, 0, len);
if (errno) {
return false;
}
memcpy(dp, &w, len);
dp += len;
ap += len;
size -= len;
}
if (size > 0) {
/* Some unaligned data remains */
unsigned char *p = (unsigned char *) &w;
/* Read the segment containing the unaligned portion, and
copy what was requested to DP. */
errno = 0;
w = P_ptrace(PTRACE_PEEKTEXT, pid, (Address) ap, 0, len);
if (errno) {
return false;
}
for (unsigned i = 0; i < size; i++)
dp[i] = p[i];
}
return true;
}
bool PtraceBulkWrite(Dyninst::Address inTraced, unsigned nbytes,
const void *inSelf, int pid)
{
static bool have_process_vm_writev = true;
unsigned char *ap = (unsigned char*) inTraced;
const unsigned char *dp = (const unsigned char*) inSelf;
Address w = 0x0; /* ptrace I/O buffer */
int len = sizeof(Address); /* address alignment of ptrace I/O requests */
unsigned cnt;
if (0 == nbytes) {
return true;
}
/* If process_vm_writev is available, we may be able to write it all in one syscall. */
if (have_process_vm_writev) {
struct iovec local_iov = { const_cast<void*>(inSelf), nbytes };
struct iovec remote_iov = { (void*)inTraced, nbytes };
ssize_t ret = process_vm_writev(pid, &local_iov, 1, &remote_iov, 1, 0);
if (ret == -1) {
if (errno == ENOSYS) {
have_process_vm_writev = false;
} else if (errno == EFAULT || errno == EPERM) {
/* Could be a read-only page -- ptrace may be allowed to
* poke anyway, so fallthrough and let ptrace try.
* It may also be denied by kernel.yama.ptrace_scope=1 if we're
* no longer a direct ancestor thanks to pid re-parenting. */
} else {
return false;
}
} else if (ret < nbytes) {
/* partial writes won't split an iovec, but we only have one... huh?! */
return false;
} else {
return true;
}
}
if ((cnt = ((Address)ap) % len)) {
/* Start of request is not aligned. */
unsigned char *p = (unsigned char*) &w;
/* Read the segment containing the unaligned portion, edit
in the data from DP, and write the segment back. */
errno = 0;
w = P_ptrace(PTRACE_PEEKTEXT, pid, (Address) (ap-cnt), 0);
if (errno) {
return false;
}
for (unsigned i = 0; i < len-cnt && i < nbytes; i++)
p[cnt+i] = dp[i];
if (0 > P_ptrace(PTRACE_POKETEXT, pid, (Address) (ap-cnt), w)) {
return false;
}
if (len-cnt >= nbytes) {
return true; /* done */
}
dp += len-cnt;
ap += len-cnt;
nbytes -= len-cnt;
}
/* Copy aligned portion */
while (nbytes >= (u_int)len) {
assert(0 == ((Address)ap) % len);
memcpy(&w, dp, len);
int retval = P_ptrace(PTRACE_POKETEXT, pid, (Address) ap, w);
if (retval < 0) {
return false;
}
// Check...
dp += len;
ap += len;
nbytes -= len;
}
if (nbytes > 0) {
/* Some unaligned data remains */
unsigned char *p = (unsigned char *) &w;
/* Read the segment containing the unaligned portion, edit
in the data from DP, and write it back. */
errno = 0;
w = P_ptrace(PTRACE_PEEKTEXT, pid, (Address) ap, 0);
if (errno) {
return false;
}
for (unsigned i = 0; i < nbytes; i++)
p[i] = dp[i];
if (0 > P_ptrace(PTRACE_POKETEXT, pid, (Address) ap, w)) {
return false;
}
}
return true;
}
// These constants are not defined in all versions of elf.h
#ifndef AT_BASE
#define AT_BASE 7
#endif
#ifndef AT_NULL
#define AT_NULL 0
#endif
#ifndef AT_SYSINFO
#define AT_SYSINFO 32
#endif
#ifndef AT_SYSINFO_EHDR
#define AT_SYSINFO_EHDR 33
#endif
static bool couldBeVsyscallPage(map_entries *entry, bool strict, Address) {
if (strict) {
if (entry->prems != PREMS_PRIVATE)
return false;
if (entry->path[0] != '\0')
return false;
}
if (entry->offset != 0)
return false;
if (entry->dev_major != 0 || entry->dev_minor != 0)
return false;
if (entry->inode != 0)
return false;
return true;
}
bool AuxvParser::readAuxvInfo()
{
/**
* The location of the vsyscall is stored in /proc/PID/auxv in Linux 2.6.
* auxv consists of a list of name/value pairs, ending with the AT_NULL
* name. There isn't a direct way to get the vsyscall info on Linux 2.4
**/
uint32_t *buffer32 = NULL;
uint64_t *buffer64 = NULL;
unsigned pos = 0;
Address dso_start = 0x0, text_start = 0x0;
struct {
unsigned long type;
unsigned long value;
} auxv_entry;
/**
* Try to read from /proc/%d/auxv. On Linux 2.4 systems auxv
* doesn't exist, which is okay because vsyscall isn't used.
* On latter 2.6 kernels the AT_SYSINFO field isn't present,
* so we have to resort to more "extreme" measures.
**/
buffer64 = (uint64_t *) readAuxvFromProc();
if (!buffer64) {
buffer64 = (uint64_t *) readAuxvFromStack();
}
if (!buffer64) {
return false;
}
buffer32 = (uint32_t *) buffer64;
do {
/**Fill in the auxv_entry structure. We may have to do different
* size reads depending on the address space. No matter which
* size we read, we'll fill the data in to auxv_entry, which may
* involve a size shift up.
**/
if (addr_size == 4) {
auxv_entry.type = (unsigned long) buffer32[pos];
pos++;
auxv_entry.value = (unsigned long) buffer32[pos];
pos++;
}
else {
auxv_entry.type = (unsigned long) buffer64[pos];
pos++;
auxv_entry.value = (unsigned long) buffer64[pos];
pos++;
}
switch(auxv_entry.type) {
case AT_SYSINFO:
text_start = auxv_entry.value;
break;
case AT_SYSINFO_EHDR:
dso_start = auxv_entry.value;
break;
case AT_PAGESZ:
page_size = auxv_entry.value;
break;
case AT_BASE:
interpreter_base = auxv_entry.value;
break;
case AT_PHDR:
phdr = auxv_entry.value;
break;
}
} while (auxv_entry.type != AT_NULL);
if (buffer64)
free(buffer64);
if (!page_size)
page_size = getpagesize();
//#if !defined(arch_x86) && !defined(arch_x86_64)
#if !defined(arch_x86) && !defined(arch_x86_64) && !defined(arch_aarch64)
//No vsyscall page needed or present
return true;
#endif
/**
* Even if we found dso_start in /proc/pid/auxv, the vsyscall 'page'
* can be larger than a single page. Thus we look through /proc/pid/maps
* for known, default, or guessed start address(es).
**/
std::vector<Address> guessed_addrs;
/* The first thing to check is the auxvinfo, if we have any. */
if( dso_start != 0x0 )
guessed_addrs.push_back( dso_start );
/**
* We'll make several educatbed attempts at guessing an address
* for the vsyscall page. After deciding on a guess, we'll try to
* verify that using /proc/pid/maps.
**/
// Guess some constants that we've seen before.
#if defined(arch_x86)
guessed_addrs.push_back(0xffffe000); //Many early 2.6 systems
guessed_addrs.push_back(0xffffd000); //RHEL4
#endif
#if defined(arch_x86_64)
guessed_addrs.push_back(0xffffffffff600000);
#endif
/**
* Look through every entry in /proc/maps, and compare it to every
* entry in guessed_addrs. If a guessed_addr looks like the right
* thing, then we'll go ahead and call it the vsyscall page.
**/
unsigned num_maps;
map_entries *secondary_match = NULL;
map_entries *maps = getVMMaps(pid, num_maps);
for (unsigned i=0; i<guessed_addrs.size(); i++) {
Address addr = guessed_addrs[i];
for (unsigned j=0; j<num_maps; j++) {
map_entries *entry = &(maps[j]);
if (addr < entry->start || addr >= entry->end)
continue;
if (dso_start == entry->start ||
couldBeVsyscallPage(entry, true, page_size)) {
//We found a possible page using a strict check.
// This is really likely to be it.
vsyscall_base = entry->start;
vsyscall_end = entry->end;
vsyscall_text = text_start;
found_vsyscall = true;
free(maps);
return true;
}
if (couldBeVsyscallPage(entry, false, page_size)) {
//We found an entry that loosely looks like the
// vsyscall page. Let's hang onto this and return
// it if we find nothing else.
secondary_match = entry;
}
}
}
/**
* There were no hits using our guessed_addrs scheme. Let's
* try to look at every entry in the maps table (not just the
* guessed addresses), and see if any of those look like a vsyscall page.
**/
for (unsigned i=0; i<num_maps; i++) {
if (couldBeVsyscallPage(&(maps[i]), true, page_size)) {
vsyscall_base = maps[i].start;
vsyscall_end = maps[i].end;
vsyscall_text = text_start;
found_vsyscall = true;
free(maps);
return true;
}
}
/**
* Return any secondary possiblitiy pages we found in our earlier search.
**/
if (secondary_match) {
vsyscall_base = secondary_match->start;
vsyscall_end = secondary_match->end;
vsyscall_text = text_start;
found_vsyscall = true;
free(maps);
return true;
}
/**
* Time to give up. Sigh.
**/
found_vsyscall = false;
free(maps);
return false;
}
#if 0
/**
* get_word_at is a helper function for readAuxvFromStack. It reads
* a word out of the mutatee's stack via the debugger interface, and
* it keeps the word cached for future reads.
* The gwa_* global variables are basically parameters to get_word_at
* and should be reset before every call
*
* gwa_buffer is a cache of data we've read before. It's backwards
* for convience, higher addresses are cached towards the base of gwa_buffer
* and lower addresses are cached at the top. This is because we read from
* high addresses to low ones, but we want to start caching at the start of
* gwa_buffer.
**/
static unsigned long *gwa_buffer = NULL;
static unsigned gwa_size = 0;
static unsigned gwa_pos = 0;
static unsigned long gwa_base_addr = 0;
static unsigned long get_word_at(process *p, unsigned long addr, bool &err) {
bool result;
unsigned word_size = p->getAddressWidth();
unsigned long word;
/**
* On AMD64 controlling 32-bit mutatee words are 32 bits long.
* We don't want to deal with this now, so treat as a 64 bit read
* (from aligned_addr) and then pick the correct 32 bits to return
* at the end of this function.
**/
unsigned long aligned_addr = addr;
if (word_size == 4 && sizeof(long) == 8 && addr % 8 == 4)
aligned_addr -= 4;
/**
* Allocate gwa_buffer on first call
**/
if (gwa_buffer == NULL) {
gwa_buffer = (unsigned long *) malloc(gwa_size);
}
/**
* If gwa_buffer isn't big enough, grow it.
**/
if (gwa_base_addr - gwa_size >= aligned_addr) {
while (gwa_base_addr - gwa_size >= aligned_addr)
gwa_size = gwa_size * 2;
gwa_buffer = (unsigned long *) realloc(gwa_buffer, gwa_size);
}
/**
* Keep adding words to the cache (gwa_buffer) until we've cached
* the word the user is interested in.
**/
while (gwa_base_addr - (gwa_pos * sizeof(long)) >= aligned_addr) {
result = p->readDataSpace((void *) aligned_addr, sizeof(long), &word, false);
if (!result) {
err = true;
return 0x0;
}
gwa_buffer[gwa_pos] = word;
gwa_pos++;
}
/**
* Return the word the user wants out of the cache. 'word' is the
* long value we want to return. On 64-bit mutator/32-bit mutatees
* we may need to return a specific 32-bits of word.
**/
word = gwa_buffer[(gwa_base_addr - aligned_addr) / sizeof(long)];
if (word_size == 4 && sizeof(long) == 8 && addr % 8 == 4) {
//64-bit mutator, 32 bit mutatee, looking for unaligned word
uint32_t *words = (uint32_t *) &word;
return (long) words[1];
}
else if (word_size == 4 && sizeof(long) == 8)
{
//64-bit mutator, 32 bit mutatee, looking for aligned word
uint32_t *words = (uint32_t *) &word;
return (long) words[0];
}
else
{
//mutator and mutatee are same size
return word;
}
}
/**
* Another helper function for readAuxvInfoFromStack. We want to know
* the top byte of the stack. Unfortunately, if we're running this it's
* probably because /proc/PID/ isn't reliable, so we can't use maps.
* Check the machine's stack pointer, page align it, and start walking
* back looking for an unaccessible page.
**/
static Address getStackTop(AddrSpaceReader *proc, bool &err) {
Address stack_pointer;
Address pagesize = getpagesize();
bool result;
long word;
err = false;
stack_pointer = proc->readRegContents(PTRACE_REG_SP);
dyn_lwp *init_lwp = proc->getInitialLwp();
if (!init_lwp) {
err = true;
return 0x0;
}
Frame frame = init_lwp->getActiveFrame();
stack_pointer = frame.getSP();
if (!stack_pointer) {
err = true;
return 0x0;
}
//Align sp to pagesize
stack_pointer = (stack_pointer & ~(pagesize - 1)) + pagesize;
//Read pages until we get to an unmapped page
for (;;) {
result = proc->readDataSpace((void *) stack_pointer, sizeof(long), &word,
false);
if (!result) {
break;
}
stack_pointer += pagesize;
}
//The vsyscall page sometimes hangs out above the stack. Test if this
// page is it, then move back down one if it is.
char pagestart[4];
result = proc->readDataSpace((void *) (stack_pointer - pagesize), 4, pagestart,
false);
if (result) {
if (pagestart[0] == 0x7F && pagestart[1] == 'E' &&
pagestart[2] == 'L' && pagestart[3] == 'F')
{
stack_pointer -= pagesize;
}
}
return stack_pointer;
}
/**
* We can't read /proc/PID/auxv for some reason (BProc is a likely candidate).
* We'll instead pull this data from the mutatee's stack. On Linux the top of
* the stack at process startup is arranged like the following:
* -------------------------------------
* esp -> | argc |
* | argv[0] |
* | ... |
* | argv[n] |
* | |
* | envp[0] |
* | ... |
* | envp[n] |
* | NULL |
* | |
* | { auxv[0].type, auxv[0].value } |
* | ... |
* | { auxv[n].type, auxv[n].value } |
* | { NULL , NULL } |
* | |
* | Some number of NULL words |
* | Strings for argv[] |
* | Strings for envp[] |
* | NULL |
* -------------------------------------
*
* We want to get at the name/value pairs of auxv. Unfortunately,
* if we're attaching the stack pointer has probably moved. Instead
* we'll try to read the from the bottom up, which is more difficult.
* argv[] and envp[] are pointers to the strings at the bottom of
* the stack. We'll search backwards for these pointers, then move back
* down until we think we have the auxv array. Yea us.
**/
void *AuxvParser::readAuxvFromStack(process *proc) {
gwa_buffer = NULL;
gwa_size = 1024 * 1024; //One megabyte default
gwa_pos = 0;
unsigned word_size = proc->getAddressWidth();
bool err = false;
// Get the base address of the mutatee's stack. For example,
// on many standard linux/x86 machines this will return
// 0xc0000000
gwa_base_addr = getStackTop(proc, err);
if (err)
return NULL;
gwa_base_addr -= word_size;
unsigned long current = gwa_base_addr;
unsigned long strings_start, strings_end;
unsigned long l1, l2, auxv_start, word;
unsigned char *buffer = NULL;
unsigned bytes_to_read;
// Go through initial NULL word
while (get_word_at(proc, current, err) == 0x0) {
if (err) goto done_err;
current -= word_size;
}
// Go through the auxv[] and envp[] strings
strings_end = current;
while (get_word_at(proc, current, err) != 0x0) {
if (err) goto done_err;
current -= word_size;
}
strings_start = current + word_size;
//Read until we find a pair of pointers into the strings
// section, this should mean we're now above the auxv vector
// and in envp or argv
for (;;) {
l1 = get_word_at(proc, current, err);
if (err) goto done_err;
l2 = get_word_at(proc, current - word_size, err);
if (err) goto done_err;
if (l1 >= strings_start && l1 < strings_end &&
l2 >= strings_start && l2 < strings_end)
break;
current -= word_size;
}
//Read back down until we get to the end of envp[]
while (get_word_at(proc, current, err) != 0x0) {
if (err) goto done_err;
current += word_size;
}
//Through the NULL byte before auxv..
while (get_word_at(proc, current, err) == 0x0) {
if (err) goto done_err;
current += word_size;
}
//Success. Found the start of auxv.
auxv_start = current;
//Read auxv into buffer
bytes_to_read = strings_start - auxv_start;
buffer = (unsigned char *) malloc(bytes_to_read + word_size*2);
if (!buffer)
goto done_err;
for (unsigned pos = 0; pos < bytes_to_read; pos += word_size) {
word = get_word_at(proc, auxv_start + pos, err);
if (err) goto done_err;
if (word_size == 4)
*((uint32_t *) (buffer + pos)) = (uint32_t) word;
else
*((unsigned long *) (buffer + pos)) = word;
}
goto done;
done_err:
if (buffer)
free(buffer);
buffer = NULL;
done:
if (gwa_buffer)
free(gwa_buffer);
return (void *) buffer;
}
#else
void *AuxvParser::readAuxvFromStack() {
/**
* Disabled, for now. Re-enable if /proc/pid/auxv doesn't exist.
**/
return NULL;
}
#endif
#define READ_BLOCK_SIZE (1024 * 5)
void *AuxvParser::readAuxvFromProc() {
char filename[64];
unsigned char *buffer = NULL;
unsigned char *temp;
unsigned buffer_size = READ_BLOCK_SIZE;
unsigned pos = 0;
ssize_t result = 0;
int fd = -1;
sprintf(filename, "/proc/%d/auxv", pid);
fd = open(filename, O_RDONLY, 0);
if (fd == -1)
goto done_err;
buffer = (unsigned char *) malloc(buffer_size);
if (!buffer) {
goto done_err;
}
for (;;) {
result = read(fd, buffer + pos, READ_BLOCK_SIZE);
if (result == -1) {
perror("Couldn't read auxv entry");
goto done_err;
}
else if (!result && !pos) {
//Didn't find any data to read
perror("Could read auxv entry");
goto done_err;
}
else if (result < READ_BLOCK_SIZE) {
//Success
goto done;
}
else if (result == READ_BLOCK_SIZE) {
//WTF... 5k wasn't enough for auxv?
buffer_size *= 2;
temp = (unsigned char *) realloc(buffer, buffer_size);
if (!temp)
goto done_err;
buffer = temp;
pos += READ_BLOCK_SIZE;
}