-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdiscoverOffsets.c
More file actions
1951 lines (1701 loc) · 61.7 KB
/
discoverOffsets.c
File metadata and controls
1951 lines (1701 loc) · 61.7 KB
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
/*
SysinternalsEBPF
Copyright (c) Microsoft Corporation
All rights reserved.
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
*/
//====================================================================
//
// discoverOffsets.cpp
//
// Use eBPF to find kernel struct offsets via memory forensics.
//
//====================================================================
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/personality.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <libbpf.h>
#include <bpf.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <signal.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/limits.h>
#include "hexdump.h"
#include "libsysinternalsEBPF.h"
#include "sysinternalsEBPF.h"
#include "discoverOffsets.h"
#define MAP_PAGE_SIZE (16 * 1024)
#define DEBUGFS "/sys/kernel/debug/tracing/"
#define KERN_MEM_DUMP_OBJ "sysinternalsEBPFmemDump.o"
#define TEMPDIR_MODE 0700
#define TEMPUID 12345
#define TEMPGID 67890
#define LOGINUID_FILE "/proc/self/loginuid"
#define SESSIONID_FILE "/proc/self/sessionid"
#define EXEPATH_FILE "/proc/self/exe"
#define COMM_FILE "/proc/self/comm"
#define PDEATH_SIG SIGUSR1
#define TMPDIR1 "/tmp/"
#define TMPDIR2 "sysinternalsEBPFtmp"
#define SEM_NAME "/sysinternalsEBPF-getoffsets"
#define MIN_SAMPLE_SIZE 16
#define MAX_POINTER_DIFF (1L << 36)
#define RINGBUF_TIMEOUT 10 // milliseconds to wait for an event in the ring buffer
#define RINGBUF_REPEAT 10 // number of times to poll ring buffer to wait for an event
#define DUMP_SIZE 4096 // this is rather arbitrary but needs to be large enough to
// accommodate each of the structs
#define COMM_LEN 16
#define UDP_ADDR "127.0.0.1"
#define UDP_PORT 60606
enum direction {forwards, backwards};
extern double g_bootSecSinceEpoch;
static int eventMapFd = 0;
static int configMapFd = 0;
static struct bpf_object *bpfObj = NULL;
static struct bpf_program *bpfSysExit = NULL;
static struct bpf_program *bpfConsumeSkb = NULL;
static struct bpf_link *bpfSysExitLink = NULL;
static struct bpf_link *bpfConsumeSkbLink = NULL;
bool stopping = false;
pid_t thisPid = 0;
const char tmpdir[] = TMPDIR1 TMPDIR2;
const char *sysinternalsEBPFtmp = tmpdir + strlen(TMPDIR1);
time_t creation_time = 0;
char *memDumps[MAX_MEM_DUMP_TYPE];
uint64_t memAddrs[MAX_MEM_DUMP_TYPE];
uint32_t memSizes[MAX_MEM_DUMP_TYPE];
//--------------------------------------------------------------------
//
// memDumpEventCb
//
// Callback for receiving memory dumps from the kernel space.
//
//--------------------------------------------------------------------
static void memDumpEventCb(void *ctx, int cpu, void *data, __u32 size)
{
if (data == NULL) {
logMessage("memDumpEventCb invalid params\n");
return;
}
memDump *d = NULL;
if (size > MIN_SAMPLE_SIZE) {
d = (memDump *)data;
if (memDumps[d->type] != NULL) {
free(memDumps[d->type]);
}
memDumps[d->type] = (char *)malloc(d->size);
if (memDumps[d->type] == NULL) {
logMessage("Out of memory\n");
exit(1);
}
memcpy(memDumps[d->type], d->data, d->size);
memSizes[d->type] = d->size;
memAddrs[d->type] = (uint64_t)d->addr;
stopping = true;
}
}
//--------------------------------------------------------------------
//
// memDumpCloseAll
//
// Free all memory dumps and close down eBPF programs.
//
//--------------------------------------------------------------------
void memDumpCloseAll()
{
for (int i=0; i<MAX_MEM_DUMP_TYPE; i++) {
if (memDumps[i] != NULL) {
free(memDumps[i]);
}
}
bpf_link__destroy(bpfSysExitLink);
bpf_link__destroy(bpfConsumeSkbLink);
bpf_object__close(bpfObj);
}
//--------------------------------------------------------------------
//
// isPointer
//
// Checks if value is a pointer by comparing it with the address of
// the task struct.
//
//--------------------------------------------------------------------
bool isPointer(uint64_t ptr)
{
if (labs(ptr - memAddrs[task]) < MAX_POINTER_DIFF) {
return true;
} else {
return false;
}
}
//--------------------------------------------------------------------
//
// near
//
// Checks if the difference between two values is within the specified
// range.
//
//--------------------------------------------------------------------
bool near(uint64_t a, uint64_t b, uint64_t range)
{
if (labs(a - b) <= range) {
return true;
} else {
return false;
}
}
//--------------------------------------------------------------------
//
// align
//
// Aligns an offset to a particular alignment in the specified
// direction.
//
//--------------------------------------------------------------------
unsigned int align(unsigned int offset, unsigned int a,
enum direction dir)
{
unsigned int mod = offset % a;
if (mod == 0) {
return offset;
}
if (dir == forwards) {
offset += a;
}
return offset - mod;
}
//--------------------------------------------------------------------
//
// get16
//
// Retrieves the unsigned 16 bit int from the given offset of the
// named memory dump.
//
//--------------------------------------------------------------------
uint16_t get16(enum memDumpType type, uint32_t offset)
{
if (offset >= memSizes[type]) {
logMessage("get16 invalid params\n");
return 0;
}
return *(uint16_t *)&memDumps[type][offset];
}
//--------------------------------------------------------------------
//
// get32
//
// Retrieves the unsigned 32 bit int from the given offset of the
// named memory dump.
//
//--------------------------------------------------------------------
uint32_t get32(enum memDumpType type, uint32_t offset)
{
if (offset >= memSizes[type]) {
logMessage("get32 invalid params\n");
return 0;
}
return *(uint32_t *)&memDumps[type][offset];
}
//--------------------------------------------------------------------
//
// get64
//
// Retrieves the unsigned 64 bit int from the given offset of the
// named memory dump.
//
//--------------------------------------------------------------------
uint64_t get64(enum memDumpType type, uint32_t offset)
{
if (offset >= memSizes[type]) {
logMessage("get64 invalid params\n");
return 0;
}
return *(uint64_t *)&memDumps[type][offset];
}
//--------------------------------------------------------------------
//
// numElements
//
// Check whether there are enough elements remaining in buffer and
// return proprosed number if there are, or the number available if
// not.
//
//--------------------------------------------------------------------
uint32_t numElements(uint32_t proposed, enum memDumpType type,
enum direction dir, uint32_t offset, uint32_t elementSize)
{
if (dir == forwards) {
if (proposed * elementSize < memSizes[type] - offset) {
return proposed;
} else {
return (memSizes[type] - offset) / elementSize;
}
} else {
if (proposed * elementSize < offset) {
return proposed;
} else {
return offset / elementSize;
}
}
}
//--------------------------------------------------------------------
//
// searchUint16
//
// Searches the named memory dump in the given direction from the
// given offset (16 bit aligned), for a 16 bit value that is within
// the given difference of the given target, over a maximum of given
// 16 bit elements.
//
//--------------------------------------------------------------------
bool searchUint16(unsigned int *out, enum direction dir,
enum memDumpType type, uint32_t startOffset, uint32_t numElem,
uint16_t target, uint16_t diff)
{
if (out == NULL) {
logMessage("searchUint16 invalid params\n");
return false;
}
numElem = numElements(numElem, type, dir, startOffset, sizeof(uint16_t));
startOffset = align(startOffset, sizeof(uint16_t), dir);
uint32_t mod = numElem * sizeof(uint16_t);
uint32_t step = sizeof(uint32_t);
if (dir == backwards) {
mod = -mod;
step = -step;
}
for (uint32_t i = startOffset; i != startOffset + mod; i += step) {
if (near(get16(type, i), target, diff)) {
out[0] = i;
out[1] = -1;
return true;
}
}
return false;
}
//--------------------------------------------------------------------
//
// searchUint32
//
// Searches the named memory dump in the given direction from the
// given offset (32 bit aligned), for a 32 bit value that is within
// the given difference of the given target, over a specified maximum
// number of 32 bit elements.
//
//--------------------------------------------------------------------
bool searchUint32(unsigned int *out, enum direction dir,
enum memDumpType type, uint32_t startOffset, uint32_t numElem,
uint32_t target, uint32_t diff)
{
if (out == NULL) {
logMessage("searchUint32 invalid params\n");
return false;
}
numElem = numElements(numElem, type, dir, startOffset, sizeof(uint32_t));
startOffset = align(startOffset, sizeof(uint32_t), dir);
uint32_t mod = numElem * sizeof(uint32_t);
uint32_t step = sizeof(uint32_t);
if (dir == backwards) {
mod = -mod;
step = -step;
}
for (uint32_t i = startOffset; i != startOffset + mod; i += step) {
if (near(get32(type, i), target, diff)) {
out[0] = i;
out[1] = -1;
return true;
}
}
return false;
}
//--------------------------------------------------------------------
//
// searchUin64
//
// Searches the named memory dump in the given direction from the
// given offset (64 bit aligned), for a 64 bit value that is within
// the given difference of the given target, over a specified maximum
// number of 64 bit elements.
//
//--------------------------------------------------------------------
bool searchUint64(unsigned int *out, enum direction dir,
enum memDumpType type, uint32_t startOffset, uint32_t numElem,
uint64_t target, uint64_t diff)
{
if (out == NULL) {
logMessage("searchUint64 invalid params\n");
return false;
}
numElem = numElements(numElem, type, dir, startOffset, sizeof(uint64_t));
startOffset = align(startOffset, sizeof(uint64_t), dir);
uint32_t mod = numElem * sizeof(uint64_t);
uint32_t step = sizeof(uint64_t);
if (dir == backwards) {
mod = -mod;
step = -step;
}
for (uint32_t i = startOffset; i != startOffset + mod; i += step) {
if (near(get64(type, i), target, diff)) {
out[0] = i;
out[1] = -1;
return true;
}
}
return false;
}
//--------------------------------------------------------------------
//
// searchPtr
//
// Searches the named memory dump in the given direction from the
// given offset (64 bit aligned), for a 64 bit value that is
// considered to be a pointer, over a specified maximum number of 64
// bit elements.
//
//--------------------------------------------------------------------
bool searchPtr(unsigned int *out, enum direction dir,
enum memDumpType type, uint32_t startOffset, uint32_t numElem)
{
if (out == NULL) {
logMessage("searchPtr invalid params\n");
return false;
}
return searchUint64(out, dir, type, startOffset, numElem, memAddrs[task], MAX_POINTER_DIFF);
}
//--------------------------------------------------------------------
//
// searchStr
//
// Searches the named memory dump in the given direction from the
// given offset, for a given string, over a specified number of bytes.
//
//--------------------------------------------------------------------
bool searchStr(unsigned int *out, enum direction dir,
enum memDumpType type, uint32_t startOffset, uint32_t numElem,
const char *target)
{
if (out == NULL || target == NULL) {
logMessage("searchStr invalid params\n");
return false;
}
numElem = numElements(numElem, type, dir, startOffset, sizeof(char));
uint32_t mod = numElem;
uint32_t step = 1;
if (dir == backwards) {
mod = -mod;
step = -step;
}
for (uint32_t i = startOffset; i != startOffset + mod; i+= step) {
if (strcmp(&memDumps[type][i], target) == 0) {
out[0] = i;
out[1] = -1;
return true;
}
}
return false;
}
//--------------------------------------------------------------------
//
// setConfigPid
//
// Set the config map to specify the kernel memory to dump for the
// specified process.
// Note, addr can be NULL.
//
//--------------------------------------------------------------------
bool setConfigPid(pid_t pid, enum memDumpType type, const void *addr,
unsigned int size)
{
unsigned int configEntry = 0;
memDumpConfig config;
config.userlandPid = pid;
config.type = type;
config.addr = addr;
config.size = size;
if (bpf_map_update_elem(configMapFd, &configEntry, &config, BPF_ANY)) {
logMessage("ERROR: failed to set config: '%s'\n", strerror(errno));
return false;
}
return true;
}
//--------------------------------------------------------------------
//
// setConfig
//
// Set the config map to specify the kernel memory to dump for the
// current process.
// Note, addr can be NULL.
//
//--------------------------------------------------------------------
bool setConfig(enum memDumpType type, const void *addr,
unsigned int size)
{
return setConfigPid(thisPid, type, addr, size);
}
//--------------------------------------------------------------------
//
// triggerTp
//
// Trigger the tracepoint to call the EBPF program.
//
//--------------------------------------------------------------------
void triggerTp()
{
struct utsname unameTmp = { 0 };
uname(&unameTmp);
}
//--------------------------------------------------------------------
//
// getMem
//
// Poll the perf ring buffer to receive the memory dump.
//
//--------------------------------------------------------------------
bool getMem(enum memDumpType type, struct perf_buffer *pb)
{
stopping = false;
unsigned int i = 0;
if (pb == NULL)
return false;
while (!stopping && perf_buffer__poll(pb, RINGBUF_TIMEOUT) >= 0 && i < RINGBUF_REPEAT) {
usleep(1000); // 1ms
i++;
}
if (memDumps[type] == NULL) {
return false;
}
return true;
}
//--------------------------------------------------------------------
//
// dumpStructPid
//
// Request memory dump from specified process.
// Note, addr can be NULL.
//
//--------------------------------------------------------------------
bool dumpStructPid(pid_t pid, enum memDumpType type, const void *addr,
unsigned int size, struct perf_buffer *pb)
{
// dump target struct
if (!setConfigPid(pid, type, addr, size)) {
return false;
}
triggerTp();
if (!getMem(type, pb)) {
return false;
}
return true;
}
//--------------------------------------------------------------------
//
// dumpStruct
//
// Request memory dump from current process.
// Note, addr can be NULL.
//
//--------------------------------------------------------------------
bool dumpStruct(enum memDumpType type, const void *addr,
unsigned int size, struct perf_buffer *pb)
{
return dumpStructPid(thisPid, type, addr, size, pb);
}
//--------------------------------------------------------------------
//
// searchDerefUint32
//
// Searches the first named memory dump in the given direction from
// the given offset (64 bit aligned), for a pointer, over a specified
// maximum number of 64 bit elements, which is then dumped as the
// second named memory dump, and the 32 bit value at the specified
// offset within it is checked whether it is within the given
// difference of the given target.
//
//--------------------------------------------------------------------
bool searchDerefUint32(unsigned int *out, enum direction dir,
struct perf_buffer *pb, enum memDumpType from,
enum memDumpType to, uint64_t startOffset, uint64_t numElem,
uint32_t secondOffset, uint32_t target, uint32_t diff)
{
if (out == NULL || pb == NULL) {
logMessage("searchDerefUint32 invalid params\n");
return false;
}
unsigned int off[2];
if (!searchPtr(off, dir, from, startOffset, numElem)) {
logMessage("Did not find pointer\n");
return false;
}
if (memDumps[to] == NULL) {
if (!dumpStruct(to, (void *)get64(from, off[0]), DUMP_SIZE, pb)) {
logMessage("Did not get struct memory\n");
return false;
}
}
if (near(get32(to, secondOffset), target, diff)) {
out[0] = off[0];
out[1] = secondOffset;
out[2] = -1;
return true;
} else {
return false;
}
}
//--------------------------------------------------------------------
//
// printOffset
//
// Print the specified number of offsets parameters for the given
// named offset array.
//
//--------------------------------------------------------------------
void printOffset(const char *name, const unsigned int *o, int num)
{
#if 0
if (name == NULL || o == NULL) {
logMessage("printOffset invalid params\n");
return;
}
logMessage("%s = ", name);
for (int i=0; i<num; i++) {
logMessage("%d, ", o[i]);
}
logMessage("\n");
#endif
}
//--------------------------------------------------------------------
//
// getPidOffset
//
// Get the PID, parent and parent PID offsets.
//
//--------------------------------------------------------------------
bool getPidOffset(Offsets *offsets, struct perf_buffer *pb)
{
if (offsets == NULL || pb == NULL) {
logMessage("getPidOffset invalid params\n");
return false;
}
// search for TID and PID
if (searchUint32(offsets->pid, forwards, task, 0, memSizes[task] / sizeof(uint32_t), thisPid, 0)) {
// first match is the TID; PID follows
offsets->pid[0] += sizeof(uint32_t);
printOffset("pid", offsets->pid, 2);
} else {
logMessage("pid offset not found\n");
return false;
}
// real_parent should be the next pointer - certainly within 32 uint64_ts
// confirm by checking PPID against parent->pid
if (searchDerefUint32(offsets->ppid, forwards, pb, task, ptask, offsets->pid[0] + sizeof(uint32_t), 32,
offsets->pid[0], getppid(), 0)) {
offsets->parent[0] = offsets->ppid[0];
printOffset("parent", offsets->parent, 2);
printOffset("ppid", offsets->ppid, 3);
return true;
} else {
logMessage("parent offset not found\n");
return false;
}
}
//--------------------------------------------------------------------
//
// getStartTimeOffset
//
// Get the start_bootime offset.
//
//--------------------------------------------------------------------
bool getStartTimeOffset(Offsets *offsets, time_t procStartTime)
{
if (offsets == NULL) {
logMessage("getStartTimeOffset invalid params\n");
return false;
}
if (offsets->parent[0] == -1) {
return false;
}
// find start_time (nanoseconds since boot) by searching forwards
const time_t startTimeGuess = (procStartTime - (long)g_bootSecSinceEpoch) * 1000 * 1000 * 1000;
if (searchUint64(offsets->start_time, forwards, task, offsets->parent[0] + sizeof(uint64_t), 128, startTimeGuess,
2 * 1000 * 1000 * 1000)) {
// first match is start_time; start_boottime / real_start_time (includes time in suspend) follows
offsets->start_time[0] += sizeof(uint64_t);
printOffset("start_time", offsets->start_time, 2);
return true;
} else {
logMessage("start_time offset not found\n");
return false;
}
}
//--------------------------------------------------------------------
//
// getCommOffset
//
// Get the comm offset.
//
//--------------------------------------------------------------------
bool getCommOffset(Offsets *offsets, const char *comm)
{
if (offsets == NULL || comm == NULL) {
logMessage("getCommOffset invalid params\n");
return false;
}
if (offsets->start_time[0] == -1) {
return false;
}
// find comm by searching forwards
if (searchStr(offsets->comm, forwards, task, offsets->start_time[0] + sizeof(uint64_t), 1024, comm)) {
printOffset("comm", offsets->comm, 2);
return true;
} else {
logMessage("comm offset not found\n");
return false;
}
}
//--------------------------------------------------------------------
//
// getCredsOffsets
//
// Get the creds offsets.
//
//--------------------------------------------------------------------
bool getCredsOffsets(Offsets *offsets, struct perf_buffer *pb)
{
if (offsets == NULL || pb == NULL) {
logMessage("getCredOffsets invalid param\n");
return false;
}
sem_t *sem = NULL;
if (offsets->comm[0] == -1) {
return false;
}
// find cred struct by searching backwards
// real_cred and cred both point to the same struct
offsets->cred[0] = -1;
uint64_t startOffset = offsets->comm[0] - sizeof(uint64_t);
while (offsets->cred[0] == -1) {
if (!searchPtr(offsets->cred, backwards, task, startOffset, 32)) {
logMessage("cred offset not found\n");
return false;
}
if (get64(task, offsets->cred[0]) != get64(task, offsets->cred[0] - sizeof(uint64_t))) {
startOffset = offsets->cred[0] - sizeof(uint64_t);
offsets->cred[0] = -1;
}
}
if (offsets->cred[0] != -1) {
printOffset("cred", offsets->cred, 2);
} else {
logMessage("cred offset not found\n");
return false;
}
// save the task info
const char *myTask = memDumps[task];
uint64_t myAddr = memAddrs[task];
uint32_t mySize = memSizes[task];
memDumps[task] = NULL;
memAddrs[task] = 0;
memSizes[task] = 0;
sem_unlink(SEM_NAME);
pid_t child = fork();
if (child == -1) {
logMessage("Cannot fork to get cred struct\n");
return false;
}
if (child == 0) {
// set child process creds to known values and trigger EBPF program
if(setgid(TEMPGID) < 0)
{
logMessage("Failed in call to setgid\n");
exit(1);
}
if(setuid(TEMPUID) < 0)
{
logMessage("Failed in call to setuid\n");
exit(1);
}
// create semaphore
sem = sem_open(SEM_NAME, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, 0);
if (sem == SEM_FAILED) {
logMessage("Child cannot create semaphore\n");
exit(1);
}
sem_wait(sem);
triggerTp();
sem_wait(sem);
triggerTp();
exit(0);
}
while ((sem = sem_open(SEM_NAME, 0)) == SEM_FAILED) {
usleep(1000);
}
if (!setConfigPid(child, task, 0, DUMP_SIZE)) {
logMessage("Cannot set config for child for task\n");
return false;
}
sem_post(sem);
if (!getMem(task, pb)) {
logMessage("Did not get child task struct\n");
return false;
}
if (!setConfigPid(child, cred, (void *)get64(task, offsets->cred[0]), DUMP_SIZE)) {
logMessage("Cannot set config for child for cred\n");
return false;
}
sem_post(sem);
if (!getMem(cred, pb)) {
logMessage("Did not get child cred struct\n");
return false;
}
// cred struct starts with 32-bit atomic_t, then optional debug info,
// then UID, GID, SUID, SGID, EUID, EGID, etc
if (searchUint32(offsets->cred_uid, forwards, cred, 0, memSizes[cred] / sizeof(uint32_t), TEMPUID, 0)) {
printOffset("cred_uid", offsets->cred_uid, 2);
} else {
return false;
}
// could search and confirm, but expectation is that any new cred types will be added
// after existing ones, and that the existing order won't change
offsets->cred_gid[0] = offsets->cred_uid[0] + sizeof(uint32_t);
offsets->cred_suid[0] = offsets->cred_gid[0] + sizeof(uint32_t);
offsets->cred_sgid[0] = offsets->cred_suid[0] + sizeof(uint32_t);
offsets->cred_euid[0] = offsets->cred_sgid[0] + sizeof(uint32_t);
offsets->cred_egid[0] = offsets->cred_euid[0] + sizeof(uint32_t);
offsets->cred_fsuid[0] = offsets->cred_egid[0] + sizeof(uint32_t);
offsets->cred_fsgid[0] = offsets->cred_fsuid[0] + sizeof(uint32_t);
printOffset("cred_gid", offsets->cred_gid, 2);
printOffset("cred_suid", offsets->cred_suid, 2);
printOffset("cred_sgid", offsets->cred_sgid, 2);
printOffset("cred_euid", offsets->cred_euid, 2);
printOffset("cred_egid", offsets->cred_egid, 2);
printOffset("cred_fsuid", offsets->cred_fsuid, 2);
printOffset("cred_fsgid", offsets->cred_fsgid, 2);
free(memDumps[task]);
memDumps[task] = (void *)myTask;
memAddrs[task] = myAddr;
memSizes[task] = mySize;
sem_unlink(SEM_NAME);
kill(child, SIGHUP);
return true;
}
//--------------------------------------------------------------------
//
// getPwdPathOffset
//
// Get pwd path offset.
//
//--------------------------------------------------------------------
bool getPwdPathOffset(Offsets *offsets, struct perf_buffer *pb,
size_t commLen)
{
if (offsets == NULL || pb == NULL) {
logMessage("getPwdPathOffset invalid params\n");
return false;
}
if (offsets->comm[0] == -1) {
return false;
}
// after comm there are optional semaphore pointers, which will point to themselves or
// very nearby as we haven't enabled any, and optional counters, that don't look like pointers,
// followed by the pointer to the fs struct
uint32_t startOffset = align(offsets->comm[0] + commLen, sizeof(uint64_t), forwards);
while (!isPointer(get64(task, startOffset)) || near(get64(task, startOffset), memAddrs[task] + startOffset, 32)) {
startOffset += sizeof(uint64_t);
}
offsets->pwd_path[0] = startOffset;
if (!dumpStruct(fs, (void *)get64(task, startOffset), DUMP_SIZE, pb)) {
logMessage("Could not dump fs struct\n");
return false;
}
// fs struct contains various values, followed by 2 (embedded) struct paths,
// the first for the root fs and the second for the pwd. A struct path consists
// of 2 pointers, one to the vfsmount and one to the dentry.
if (searchPtr(&offsets->pwd_path[1], forwards, fs, 0, memSizes[fs] / sizeof(uint64_t))) {
if (isPointer(get64(fs, offsets->pwd_path[1])) &&
isPointer(get64(fs, offsets->pwd_path[1] + sizeof(uint64_t))) &&
isPointer(get64(fs, offsets->pwd_path[1] + (2 * sizeof(uint64_t)))) &&
isPointer(get64(fs, offsets->pwd_path[1] + (3 * sizeof(uint64_t))))) {
offsets->pwd_path[1] = offsets->pwd_path[1] + (2 * sizeof(uint64_t));
printOffset("pwd_path", offsets->pwd_path, 3);
} else {
logMessage("pwd_path offset not found (1)\n");
logMessage("pwd_path[0] = %d\n", offsets->pwd_path[0]);
logMessage("0 = 0x%016lx\n", get64(fs, offsets->pwd_path[1]));
logMessage("1 = 0x%016lx\n", get64(fs, offsets->pwd_path[1] + sizeof(uint64_t)));
logMessage("2 = 0x%016lx\n", get64(fs, offsets->pwd_path[1] + (2 * sizeof(uint64_t))));
logMessage("3 = 0x%016lx\n", get64(fs, offsets->pwd_path[1] + (3 * sizeof(uint64_t))));
return false;
}
} else {
logMessage("pwd_path offset not found (2)\n");
return false;
}
offsets->path_vfsmount[0] = 0;
printOffset("path_vfsmount", offsets->path_vfsmount, 2);
offsets->path_dentry[0] = sizeof(uint64_t);
printOffset("path_dentry", offsets->path_dentry, 2);
return true;
}
//--------------------------------------------------------------------
//
// getDentryNameOffset
//
// Get the dentry name offset.
//
//--------------------------------------------------------------------
bool getDentryNameOffset(Offsets *offsets, struct perf_buffer *pb)
{
if (offsets == NULL || pb == NULL) {
logMessage("getDentryNameOffset invalid params\n");
return false;
}
if (offsets->pwd_path[1] == -1 || offsets->path_dentry[0] == -1) {
return false;
}
if (!dumpStruct(dentry, (void *)get64(fs, offsets->pwd_path[1] + offsets->path_dentry[0]), DUMP_SIZE, pb)) {
logMessage("Could not dump dentry struct\n");
return false;
}
uint32_t startOffset;
// search for dentry iname (short name)
if (searchStr(offsets->dentry_iname, forwards, dentry, 0, memSizes[dentry], sysinternalsEBPFtmp)) {
// search backwards for the qstr struct