-
Notifications
You must be signed in to change notification settings - Fork 1
/
scap_fds.c
2051 lines (1816 loc) · 50.4 KB
/
scap_fds.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
/*
Copyright (C) 2013-2018 Draios Inc dba Sysdig.
This file is part of sysdig.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include "scap.h"
#include "scap-int.h"
#include "scap_savefile.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "uthash.h"
#include "compat/misc.h"
#ifdef _WIN32
#include <Ws2tcpip.h>
#elif defined(__APPLE__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#else
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <unistd.h>
#include <sys/param.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <netinet/tcp.h>
#if defined(__linux__)
#if HAVE_SYS_MKDEV_H
#include <sys/mkdev.h>
#endif
#ifdef HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
//#include <linux/sock_diag.h>
//#include <linux/unix_diag.h>
#endif
#endif
#define SOCKET_SCAN_BUFFER_SIZE 1024 * 1024
int32_t scap_fd_print_ipv6_socket_info(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen)
{
char source_address[100];
char destination_address[100];
if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6info.sip,source_address,100))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE,
"Could not convert IPv6 source address 0x%x%x%x%x (%s)",
fdi->info.ipv6info.sip[0],
fdi->info.ipv6info.sip[1],
fdi->info.ipv6info.sip[2],
fdi->info.ipv6info.sip[3],
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6info.dip,destination_address,100))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE,
"Could not convert IPv6 source address 0x%x%x%x%x (%s)",
fdi->info.ipv6info.dip[0],
fdi->info.ipv6info.dip[1],
fdi->info.ipv6info.dip[2],
fdi->info.ipv6info.dip[3],
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
snprintf(str,stlen,"%s:%u->%s:%u",source_address,fdi->info.ipv6info.sport,destination_address,fdi->info.ipv6info.dport);
return SCAP_SUCCESS;
}
int32_t scap_fd_print_ipv6_server_socket_info(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen)
{
char address[100];
if(NULL == inet_ntop(AF_INET6,fdi->info.ipv6serverinfo.ip,address,100))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE,
"Could not convert IPv6 source address 0x%x%x%x%x (%s)",
fdi->info.ipv6serverinfo.ip[0],
fdi->info.ipv6serverinfo.ip[1],
fdi->info.ipv6serverinfo.ip[2],
fdi->info.ipv6serverinfo.ip[3],
scap_strerror(handle, errno));
return SCAP_FAILURE;
}
snprintf(str,stlen,"%s:%u->:::*",address,fdi->info.ipv6serverinfo.port);
return SCAP_SUCCESS;
}
//
// Convert an fd entry's info into a string
//
int32_t scap_fd_info_to_string(scap_t *handle, scap_fdinfo *fdi, OUT char *str, uint32_t stlen)
{
//
// Input validation
//
if((fdi)->type == SCAP_FD_UNKNOWN)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "fd type unknown");
return SCAP_FAILURE;
}
switch(fdi->type)
{
case SCAP_FD_IPV4_SOCK:
snprintf(str, stlen, "%u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
fdi->info.ipv4info.sip >> 24,
fdi->info.ipv4info.sip >> 16 & 0xff,
fdi->info.ipv4info.sip >> 8 & 0xff,
fdi->info.ipv4info.sip & 0xff,
(uint32_t)fdi->info.ipv4info.sport,
fdi->info.ipv4info.dip >> 24,
fdi->info.ipv4info.dip >> 16 & 0xff,
fdi->info.ipv4info.dip >> 8 & 0xff,
fdi->info.ipv4info.dip & 0xff,
(uint32_t)fdi->info.ipv4info.dport);
break;
case SCAP_FD_IPV4_SERVSOCK:
snprintf(str, stlen, "%u.%u.%u.%u:%u",
fdi->info.ipv4serverinfo.ip >> 24,
fdi->info.ipv4serverinfo.ip >> 16 & 0xff,
fdi->info.ipv4serverinfo.ip >> 8 & 0xff,
fdi->info.ipv4serverinfo.ip & 0xff,
(uint32_t)fdi->info.ipv4serverinfo.port);
break;
case SCAP_FD_IPV6_SOCK:
return scap_fd_print_ipv6_socket_info(handle,fdi,str,stlen);
break;
case SCAP_FD_IPV6_SERVSOCK:
return scap_fd_print_ipv6_server_socket_info(handle,fdi,str,stlen);
break;
case SCAP_FD_FIFO:
snprintf(str, stlen, "<PIPE>");
break;
case SCAP_FD_SIGNALFD:
snprintf(str, stlen, "<SIGNAL>");
break;
case SCAP_FD_EVENTPOLL:
snprintf(str, stlen, "<POLL>");
break;
case SCAP_FD_TIMERFD:
snprintf(str, stlen, "<TIMER>");
break;
case SCAP_FD_EVENT:
snprintf(str, stlen, "<EVENT>");
break;
case SCAP_FD_INOTIFY:
snprintf(str, stlen, "<INOTIFY>");
break;
case SCAP_FD_UNIX_SOCK:
snprintf(str, stlen, "%"PRIi64" %"PRIu64" %"PRIX64"-> %"PRIX64" %s", fdi->fd,fdi->ino, fdi->info.unix_socket_info.source,fdi->info.unix_socket_info.destination, fdi->info.unix_socket_info.fname);
break;
case SCAP_FD_FILE_V2:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
break;
case SCAP_FD_UNSUPPORTED:
snprintf(str, stlen, "<UNSUPPORTED>");
break;
case SCAP_FD_NETLINK:
snprintf(str, stlen, "<NETLINK>");
break;
default:
ASSERT(false);
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "fd type unrecognized");
return SCAP_FAILURE;
}
return SCAP_SUCCESS;
}
//
// Calculate the length on disk of an fd entry's info
//
uint32_t scap_fd_info_len(scap_fdinfo *fdi)
{
//
// NB: new fields must be appended
//
uint32_t res = sizeof(uint32_t) + sizeof(fdi->ino) + 1 + sizeof(fdi->fd);
switch(fdi->type)
{
case SCAP_FD_IPV4_SOCK:
res += 4 + // sip
4 + // dip
2 + // sport
2 + // dport
1; // l4proto
break;
case SCAP_FD_IPV4_SERVSOCK:
res += 4 + // ip
2 + // port
1; // l4proto
break;
case SCAP_FD_IPV6_SOCK:
res += sizeof(uint32_t) * 4 + // sip
sizeof(uint32_t) * 4 + // dip
sizeof(uint16_t) + // sport
sizeof(uint16_t) + // dport
sizeof(uint8_t); // l4proto
break;
case SCAP_FD_IPV6_SERVSOCK:
res += sizeof(uint32_t) * 4 + // ip
sizeof(uint16_t) + // port
sizeof(uint8_t); // l4proto
break;
case SCAP_FD_UNIX_SOCK:
res +=
sizeof(uint64_t) + // unix source
sizeof(uint64_t) + // unix destination
(uint32_t)strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE) + 2;
break;
case SCAP_FD_FILE_V2:
res += sizeof(uint32_t) + // open_flags
(uint32_t)strnlen(fdi->info.regularinfo.fname, SCAP_MAX_PATH_SIZE) + 2 +
sizeof(uint32_t); // dev
break;
case SCAP_FD_FIFO:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
case SCAP_FD_UNSUPPORTED:
case SCAP_FD_EVENT:
case SCAP_FD_SIGNALFD:
case SCAP_FD_EVENTPOLL:
case SCAP_FD_INOTIFY:
case SCAP_FD_TIMERFD:
case SCAP_FD_NETLINK:
res += (uint32_t)strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE) + 2; // 2 is the length field before the string
break;
default:
ASSERT(false);
break;
}
return res;
}
int scap_dump_write(scap_dumper_t *d, void* buf, unsigned len);
//
// Write the given fd info to disk
//
int32_t scap_fd_write_to_disk(scap_t *handle, scap_fdinfo *fdi, scap_dumper_t *d, uint32_t len)
{
uint8_t type = (uint8_t)fdi->type;
uint16_t stlen;
if(scap_dump_write(d, &(len), sizeof(uint32_t)) != sizeof(uint32_t) ||
scap_dump_write(d, &(fdi->fd), sizeof(uint64_t)) != sizeof(uint64_t) ||
scap_dump_write(d, &(fdi->ino), sizeof(uint64_t)) != sizeof(uint64_t) ||
scap_dump_write(d, &(type), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)");
return SCAP_FAILURE;
}
switch(fdi->type)
{
case SCAP_FD_IPV4_SOCK:
if(scap_dump_write(d, &(fdi->info.ipv4info.sip), sizeof(uint32_t)) != sizeof(uint32_t) ||
scap_dump_write(d, &(fdi->info.ipv4info.dip), sizeof(uint32_t)) != sizeof(uint32_t) ||
scap_dump_write(d, &(fdi->info.ipv4info.sport), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv4info.dport), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv4info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi2)");
return SCAP_FAILURE;
}
break;
case SCAP_FD_IPV4_SERVSOCK:
if(scap_dump_write(d, &(fdi->info.ipv4serverinfo.ip), sizeof(uint32_t)) != sizeof(uint32_t) ||
scap_dump_write(d, &(fdi->info.ipv4serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv4serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi3)");
return SCAP_FAILURE;
}
break;
case SCAP_FD_IPV6_SOCK:
if(scap_dump_write(d, (char*)fdi->info.ipv6info.sip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 ||
scap_dump_write(d, (char*)fdi->info.ipv6info.dip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 ||
scap_dump_write(d, &(fdi->info.ipv6info.sport), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv6info.dport), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv6info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi7)");
}
break;
case SCAP_FD_IPV6_SERVSOCK:
if(scap_dump_write(d, &(fdi->info.ipv6serverinfo.ip), sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 ||
scap_dump_write(d, &(fdi->info.ipv6serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) ||
scap_dump_write(d, &(fdi->info.ipv6serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi8)");
}
break;
case SCAP_FD_UNIX_SOCK:
if(scap_dump_write(d, &(fdi->info.unix_socket_info.source), sizeof(uint64_t)) != sizeof(uint64_t) ||
scap_dump_write(d, &(fdi->info.unix_socket_info.destination), sizeof(uint64_t)) != sizeof(uint64_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi4)");
return SCAP_FAILURE;
}
stlen = (uint16_t)strnlen(fdi->info.unix_socket_info.fname, SCAP_MAX_PATH_SIZE);
if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) ||
(stlen > 0 && scap_dump_write(d, fdi->info.unix_socket_info.fname, stlen) != stlen))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi5)");
return SCAP_FAILURE;
}
break;
case SCAP_FD_FILE_V2:
if(scap_dump_write(d, &(fdi->info.regularinfo.open_flags), sizeof(uint32_t)) != sizeof(uint32_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)");
return SCAP_FAILURE;
}
stlen = (uint16_t)strnlen(fdi->info.regularinfo.fname, SCAP_MAX_PATH_SIZE);
if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) ||
(stlen > 0 && scap_dump_write(d, fdi->info.regularinfo.fname, stlen) != stlen))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi1)");
return SCAP_FAILURE;
}
if(scap_dump_write(d, &(fdi->info.regularinfo.dev), sizeof(uint32_t)) != sizeof(uint32_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (dev)");
return SCAP_FAILURE;
}
break;
case SCAP_FD_FIFO:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
case SCAP_FD_UNSUPPORTED:
case SCAP_FD_EVENT:
case SCAP_FD_SIGNALFD:
case SCAP_FD_EVENTPOLL:
case SCAP_FD_INOTIFY:
case SCAP_FD_TIMERFD:
case SCAP_FD_NETLINK:
stlen = (uint16_t)strnlen(fdi->info.fname, SCAP_MAX_PATH_SIZE);
if(scap_dump_write(d, &stlen, sizeof(uint16_t)) != sizeof(uint16_t) ||
(stlen > 0 && scap_dump_write(d, fdi->info.fname, stlen) != stlen))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi6)");
return SCAP_FAILURE;
}
break;
case SCAP_FD_UNKNOWN:
// Ignore UNKNOWN fds without failing
ASSERT(false);
break;
default:
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Unknown fdi type %d", fdi->type);
ASSERT(false);
return SCAP_FAILURE;
}
return SCAP_SUCCESS;
}
uint32_t scap_fd_read_prop_from_disk(scap_t *handle, OUT void *target, size_t expected_size, OUT size_t *nbytes, gzFile f)
{
size_t readsize;
readsize = gzread(f, target, (unsigned int)expected_size);
CHECK_READ_SIZE(readsize, expected_size);
(*nbytes) += readsize;
return SCAP_SUCCESS;
}
uint32_t scap_fd_read_fname_from_disk(scap_t* handle, char* fname,OUT size_t* nbytes, gzFile f)
{
size_t readsize;
uint16_t stlen;
readsize = gzread(f, &(stlen), sizeof(uint16_t));
CHECK_READ_SIZE(readsize, sizeof(uint16_t));
if(stlen >= SCAP_MAX_PATH_SIZE)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "invalid filename len %"PRId32, stlen);
return SCAP_FAILURE;
}
(*nbytes) += readsize;
readsize = gzread(f, fname, stlen);
CHECK_READ_SIZE(readsize, stlen);
(*nbytes) += stlen;
// NULL-terminate the string
fname[stlen] = 0;
return SCAP_SUCCESS;
}
//
// Populate the given fd by reading the info from disk
// Returns the number of read bytes.
//
uint32_t scap_fd_read_from_disk(scap_t *handle, OUT scap_fdinfo *fdi, OUT size_t *nbytes, uint32_t block_type, gzFile f)
{
uint8_t type;
uint32_t toread;
int fseekres;
uint32_t sub_len = 0;
uint32_t res = SCAP_SUCCESS;
*nbytes = 0;
if((block_type == FDL_BLOCK_TYPE_V2 && scap_fd_read_prop_from_disk(handle, &sub_len, sizeof(uint32_t), nbytes, f)) ||
scap_fd_read_prop_from_disk(handle, &(fdi->fd), sizeof(fdi->fd), nbytes, f) ||
scap_fd_read_prop_from_disk(handle, &(fdi->ino), sizeof(fdi->ino), nbytes, f) ||
scap_fd_read_prop_from_disk(handle, &type, sizeof(uint8_t), nbytes, f))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "Could not read prop block for fd");
return SCAP_FAILURE;
}
// If new parameters are added, sub_len can be used to
// see if they are available in the current capture.
// For example, for a 32bit parameter:
//
// if(sub_len && (*nbytes + sizeof(uint32_t)) <= sub_len)
// {
// ...
// }
fdi->type = (scap_fd_type)type;
switch(fdi->type)
{
case SCAP_FD_IPV4_SOCK:
if(gzread(f, &(fdi->info.ipv4info.sip), sizeof(uint32_t)) != sizeof(uint32_t) ||
gzread(f, &(fdi->info.ipv4info.dip), sizeof(uint32_t)) != sizeof(uint32_t) ||
gzread(f, &(fdi->info.ipv4info.sport), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv4info.dport), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv4info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (1)");
return SCAP_FAILURE;
}
(*nbytes) += (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint8_t));
break;
case SCAP_FD_IPV4_SERVSOCK:
if(gzread(f, &(fdi->info.ipv4serverinfo.ip), sizeof(uint32_t)) != sizeof(uint32_t) ||
gzread(f, &(fdi->info.ipv4serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv4serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (2)");
return SCAP_FAILURE;
}
(*nbytes) += (sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint8_t));
break;
case SCAP_FD_IPV6_SOCK:
if(gzread(f, (char*)fdi->info.ipv6info.sip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 ||
gzread(f, (char*)fdi->info.ipv6info.dip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4 ||
gzread(f, &(fdi->info.ipv6info.sport), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv6info.dport), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv6info.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi3)");
}
(*nbytes) += (sizeof(uint32_t) * 4 + // sip
sizeof(uint32_t) * 4 + // dip
sizeof(uint16_t) + // sport
sizeof(uint16_t) + // dport
sizeof(uint8_t)); // l4proto
break;
case SCAP_FD_IPV6_SERVSOCK:
if(gzread(f, (char*)fdi->info.ipv6serverinfo.ip, sizeof(uint32_t) * 4) != sizeof(uint32_t) * 4||
gzread(f, &(fdi->info.ipv6serverinfo.port), sizeof(uint16_t)) != sizeof(uint16_t) ||
gzread(f, &(fdi->info.ipv6serverinfo.l4proto), sizeof(uint8_t)) != sizeof(uint8_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error writing to file (fi4)");
}
(*nbytes) += (sizeof(uint32_t) * 4 + // ip
sizeof(uint16_t) + // port
sizeof(uint8_t)); // l4proto
break;
case SCAP_FD_UNIX_SOCK:
if(gzread(f, &(fdi->info.unix_socket_info.source), sizeof(uint64_t)) != sizeof(uint64_t) ||
gzread(f, &(fdi->info.unix_socket_info.destination), sizeof(uint64_t)) != sizeof(uint64_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (fi5)");
return SCAP_FAILURE;
}
(*nbytes) += (sizeof(uint64_t) + sizeof(uint64_t));
res = scap_fd_read_fname_from_disk(handle, fdi->info.unix_socket_info.fname, nbytes, f);
break;
case SCAP_FD_FILE_V2:
if(gzread(f, &(fdi->info.regularinfo.open_flags), sizeof(uint32_t)) != sizeof(uint32_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (fi1)");
return SCAP_FAILURE;
}
(*nbytes) += sizeof(uint32_t);
res = scap_fd_read_fname_from_disk(handle, fdi->info.regularinfo.fname, nbytes, f);
if (!sub_len || (sub_len < *nbytes + sizeof(uint32_t)))
{
break;
}
if(gzread(f, &(fdi->info.regularinfo.dev), sizeof(uint32_t)) != sizeof(uint32_t))
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file (dev)");
return SCAP_FAILURE;
}
(*nbytes) += sizeof(uint32_t);
break;
case SCAP_FD_FIFO:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
case SCAP_FD_UNSUPPORTED:
case SCAP_FD_EVENT:
case SCAP_FD_SIGNALFD:
case SCAP_FD_EVENTPOLL:
case SCAP_FD_INOTIFY:
case SCAP_FD_TIMERFD:
case SCAP_FD_NETLINK:
res = scap_fd_read_fname_from_disk(handle, fdi->info.fname,nbytes,f);
break;
case SCAP_FD_UNKNOWN:
ASSERT(false);
break;
default:
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "error reading the fd info from file, wrong fd type %u", (uint32_t)fdi->type);
return SCAP_FAILURE;
}
if(sub_len && *nbytes != sub_len)
{
if(*nbytes > sub_len)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Had read %zu bytes, but fdlist entry have length %u.",
*nbytes, sub_len);
return SCAP_FAILURE;
}
toread = sub_len - *nbytes;
fseekres = (int)gzseek(f, (long)toread, SEEK_CUR);
if(fseekres == -1)
{
snprintf(handle->m_lasterr, SCAP_LASTERR_SIZE, "corrupted input file. Can't skip %u bytes.",
(unsigned int)toread);
return SCAP_FAILURE;
}
*nbytes = sub_len;
}
return res;
}
void scap_fd_free_ns_sockets_list(scap_t *handle, struct scap_ns_socket_list **sockets)
{
struct scap_ns_socket_list *fdi;
struct scap_ns_socket_list *tfdi;
if(*sockets)
{
HASH_ITER(hh, *sockets, fdi, tfdi)
{
HASH_DEL(*sockets, fdi);
scap_fd_free_table(handle, &fdi->sockets);
free(fdi);
}
*sockets = NULL;
}
}
void scap_fd_free_table(scap_t *handle, scap_fdinfo **fds)
{
struct scap_fdinfo *fdi;
struct scap_fdinfo *tfdi;
if(*fds)
{
HASH_ITER(hh, *fds, fdi, tfdi)
{
HASH_DEL(*fds, fdi);
free(fdi);
}
*fds = NULL;
}
}
void scap_fd_free_proc_fd_table(scap_t *handle, scap_threadinfo *tinfo)
{
if(tinfo->fdlist)
{
scap_fd_free_table(handle, &tinfo->fdlist);
}
}
//
// remove an fd from a process table
//
void scap_fd_remove(scap_t *handle, scap_threadinfo *tinfo, int64_t fd)
{
scap_fdinfo *fdi;
//
// Find the fd descriptor
//
HASH_FIND_INT64(tinfo->fdlist, &(fd), fdi);
if(fdi == NULL)
{
//
// Looks like there's no fd to remove.
// Likely, the fd creation event was dropped.
//
//scap_proc_print_info(handle, tinfo);
// ASSERT(false);
return;
}
HASH_DEL(tinfo->fdlist, fdi);
free(fdi);
}
//
// Add the file descriptor info pointed by fdi to the fd table for process tinfo.
// Note: silently skips if fdi->type is SCAP_FD_UNKNOWN.
//
int32_t scap_add_fd_to_proc_table(scap_t *handle, scap_threadinfo *tinfo, scap_fdinfo *fdi, char *error)
{
int32_t uth_status = SCAP_SUCCESS;
scap_fdinfo *tfdi;
//
// Make sure this fd doesn't already exist
//
HASH_FIND_INT64(tinfo->fdlist, &(fdi->fd), tfdi);
if(tfdi != NULL)
{
//
// This can happen if:
// - a close() has been dropped when capturing
// - an fd has been closed by clone() or execve() (it happens when the fd is opened with the FD_CLOEXEC flag,
// which we don't currently parse.
// In either case, removing the old fd, replacing it with the new one and keeping going is a reasonable
// choice.
//
HASH_DEL(tinfo->fdlist, tfdi);
free(tfdi);
}
//
// Add the fd to the table, or fire the notification callback
//
if(handle->m_proc_callback == NULL)
{
HASH_ADD_INT64(tinfo->fdlist, fd, fdi);
if(uth_status != SCAP_SUCCESS)
{
snprintf(error, SCAP_LASTERR_SIZE, "process table allocation error (2)");
return SCAP_FAILURE;
}
}
else
{
handle->m_proc_callback(handle->m_proc_callback_context, handle, tinfo->tid, tinfo, fdi);
}
return SCAP_SUCCESS;
}
//
// Delete a device entry
//
void scap_dev_delete(scap_t* handle, scap_mountinfo* dev)
{
//
// First, remove the process descriptor from the table
//
HASH_DEL(handle->m_dev_list, dev);
//
// Second, free the memory
//
free(dev);
}
//
// Free the device table
//
void scap_free_device_table(scap_t* handle)
{
scap_mountinfo *dev, *tdev;
HASH_ITER(hh, handle->m_dev_list, dev, tdev)
{
scap_dev_delete(handle, dev);
}
}
#if defined(HAS_CAPTURE)
int32_t scap_fd_handle_pipe(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, char *error)
{
char link_name[SCAP_MAX_PATH_SIZE];
ssize_t r;
uint64_t ino;
struct stat sb;
r = readlink(fname, link_name, SCAP_MAX_PATH_SIZE);
if (r <= 0)
{
snprintf(error, SCAP_LASTERR_SIZE, "Could not read link %s (%s)",
fname, scap_strerror(handle, errno));
return SCAP_FAILURE;
}
link_name[r] = '\0';
if(1 != sscanf(link_name, "pipe:[%"PRIi64"]", &ino))
{
// in this case we've got a named pipe
// and we've got to call stat on the link name
if(-1 == stat(link_name, &sb))
{
return SCAP_SUCCESS;
}
ino = sb.st_ino;
}
strncpy(fdi->info.fname, link_name, SCAP_MAX_PATH_SIZE);
fdi->ino = ino;
return scap_add_fd_to_proc_table(handle, tinfo, fdi, error);
}
static inline uint32_t open_flags_to_scap(unsigned long flags)
{
uint32_t res = 0;
switch (flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
case O_WRONLY:
res |= PPM_O_WRONLY;
break;
case O_RDWR:
res |= PPM_O_RDWR;
break;
default:
res |= PPM_O_RDONLY;
break;
}
if (flags & O_CREAT)
res |= PPM_O_CREAT;
if (flags & O_TMPFILE)
res |= PPM_O_TMPFILE;
if (flags & O_APPEND)
res |= PPM_O_APPEND;
#ifdef O_DSYNC
if (flags & O_DSYNC)
res |= PPM_O_DSYNC;
#endif
if (flags & O_EXCL)
res |= PPM_O_EXCL;
if (flags & O_NONBLOCK)
res |= PPM_O_NONBLOCK;
if (flags & O_SYNC)
res |= PPM_O_SYNC;
if (flags & O_TRUNC)
res |= PPM_O_TRUNC;
#ifdef O_DIRECT
if (flags & O_DIRECT)
res |= PPM_O_DIRECT;
#endif
#ifdef O_DIRECTORY
if (flags & O_DIRECTORY)
res |= PPM_O_DIRECTORY;
#endif
#ifdef O_LARGEFILE
if (flags & O_LARGEFILE)
res |= PPM_O_LARGEFILE;
#endif
#ifdef O_CLOEXEC
if (flags & O_CLOEXEC)
res |= PPM_O_CLOEXEC;
#endif
return res;
}
uint32_t scap_get_device_by_mount_id(scap_t *handle, const char *procdir, unsigned long requested_mount_id)
{
char fd_dir_name[SCAP_MAX_PATH_SIZE];
char line[SCAP_MAX_PATH_SIZE];
FILE *finfo;
scap_mountinfo *mountinfo;
HASH_FIND_INT64(handle->m_dev_list, &requested_mount_id, mountinfo);
if(mountinfo != NULL)
{
return mountinfo->dev;
}
snprintf(fd_dir_name, SCAP_MAX_PATH_SIZE, "%smountinfo", procdir);
finfo = fopen(fd_dir_name, "r");
if(finfo == NULL)
{
return 0;
}
while(fgets(line, sizeof(line), finfo) != NULL)
{
uint32_t mount_id, major, minor;
if(sscanf(line, "%u %*u %u:%u", &mount_id, &major, &minor) != 3)
{
continue;
}
if(mount_id == requested_mount_id)
{
uint32_t dev = makedev(major, minor);
mountinfo = malloc(sizeof(*mountinfo));
if(mountinfo)
{
int32_t uth_status = SCAP_SUCCESS;
mountinfo->mount_id = mount_id;
mountinfo->dev = dev;
HASH_ADD_INT64(handle->m_dev_list, mount_id, mountinfo);
if(uth_status != SCAP_SUCCESS)
{
free(mountinfo);
}
}
fclose(finfo);
return dev;
}
}
fclose(finfo);
return 0;
}
void scap_fd_flags_file(scap_t *handle, scap_fdinfo *fdi, const char *procdir)
{
char fd_dir_name[SCAP_MAX_PATH_SIZE];
char line[SCAP_MAX_PATH_SIZE];
FILE *finfo;
snprintf(fd_dir_name, SCAP_MAX_PATH_SIZE, "%sfdinfo/%" PRId64, procdir, fdi->fd);
finfo = fopen(fd_dir_name, "r");
if(finfo == NULL)
{
return;
}
fdi->info.regularinfo.mount_id = 0;
fdi->info.regularinfo.dev = 0;
while(fgets(line, sizeof(line), finfo) != NULL)
{
// We are interested in the flags and the mnt_id.
//
// The format of the file is:
// pos: XXXX
// flags: YYYYYYYY
// mnt_id: ZZZ
if(!strncmp(line, "flags:\t", sizeof("flags:\t") - 1))
{
uint32_t open_flags;
errno = 0;
unsigned long flags = strtoul(line + sizeof("flags:\t") - 1, NULL, 8);
if(errno == ERANGE)
{
open_flags = PPM_O_NONE;
}
else
{
open_flags = open_flags_to_scap(flags);
}
fdi->info.regularinfo.open_flags = open_flags;
}
else if(!strncmp(line, "mnt_id:\t", sizeof("mnt_id:\t") - 1))
{
errno = 0;
unsigned long mount_id = strtoul(line + sizeof("mnt_id:\t") - 1, NULL, 10);
if(errno != ERANGE)
{
fdi->info.regularinfo.mount_id = mount_id;
}
}
}
fclose(finfo);
}
int32_t scap_fd_handle_regular_file(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, const char *procdir, char *error)
{
char link_name[SCAP_MAX_PATH_SIZE];
ssize_t r;
r = readlink(fname, link_name, SCAP_MAX_PATH_SIZE);
if (r <= 0)
{
return SCAP_SUCCESS;
}
link_name[r] = '\0';
if(SCAP_FD_UNSUPPORTED == fdi->type)
{
// try to classify by link name
if(0 == strcmp(link_name,"anon_inode:[eventfd]"))
{
fdi->type = SCAP_FD_EVENT;
}
else if(0 == strcmp(link_name,"anon_inode:[signalfd]"))
{
fdi->type = SCAP_FD_SIGNALFD;
}
else if(0 == strcmp(link_name,"anon_inode:[eventpoll]"))
{
fdi->type = SCAP_FD_EVENTPOLL;
}
else if(0 == strcmp(link_name,"anon_inode:inotify"))
{
fdi->type = SCAP_FD_INOTIFY;
}
else if(0 == strcmp(link_name,"anon_inode:[timerfd]"))
{
fdi->type = SCAP_FD_TIMERFD;
}
if(SCAP_FD_UNSUPPORTED == fdi->type)
{
// still not able to classify
// printf("unsupported %s -> %s\n",fname,link_name);
}
fdi->info.fname[0] = '\0';
}
else if(fdi->type == SCAP_FD_FILE_V2)
{
scap_fd_flags_file(handle, fdi, procdir);
strncpy(fdi->info.regularinfo.fname, link_name, SCAP_MAX_PATH_SIZE);
}
else
{
strncpy(fdi->info.fname, link_name, SCAP_MAX_PATH_SIZE);
}
return scap_add_fd_to_proc_table(handle, tinfo, fdi, error);
}
int32_t scap_fd_handle_socket(scap_t *handle, char *fname, scap_threadinfo *tinfo, scap_fdinfo *fdi, char* procdir, uint64_t net_ns, struct scap_ns_socket_list **sockets_by_ns, char *error)
{
char link_name[SCAP_MAX_PATH_SIZE];
ssize_t r;
scap_fdinfo *tfdi;
uint64_t ino;
struct scap_ns_socket_list* sockets = NULL;
int32_t uth_status = SCAP_SUCCESS;
if(*sockets_by_ns == (void*)-1)