-
Notifications
You must be signed in to change notification settings - Fork 159
/
filterchecks.cpp
9672 lines (8581 loc) · 263 KB
/
filterchecks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2021 The Falco Authors.
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 <time.h>
#include <math.h>
#ifndef _WIN32
#include <algorithm>
#endif
#include <filesystem>
#include "sinsp.h"
#include "sinsp_int.h"
#include "dns_manager.h"
#include "filter.h"
#include "filterchecks.h"
#include "plugin.h"
#include "plugin_manager.h"
#include "protodecoder.h"
#include "tracers.h"
#include "value_parser.h"
#include "strl.h"
using namespace std;
extern sinsp_evttables g_infotables;
int32_t g_screen_w = -1;
bool g_filterchecks_force_raw_times = false;
#define RETURN_EXTRACT_VAR(x) do { \
*len = sizeof((x)); \
return (uint8_t*) &(x); \
} while(0)
#define RETURN_EXTRACT_PTR(x) do { \
*len = sizeof(*(x)); \
return (uint8_t*) (x); \
} while(0)
#define RETURN_EXTRACT_STRING(x) do { \
*len = (x).size(); \
return (uint8_t*) (x).c_str(); \
} while(0)
#define RETURN_EXTRACT_CSTR(x) do { \
if((x)) \
{ \
*len = strlen((char *) ((x))); \
} \
return (uint8_t*) ((x)); \
} while(0)
///////////////////////////////////////////////////////////////////////////////
// Helper functions
///////////////////////////////////////////////////////////////////////////////
int32_t gmt2local(time_t t)
{
int dt, dir;
struct tm *gmt, *tmp_gmt, *loc;
struct tm sgmt;
if(t == 0)
{
t = time(NULL);
}
gmt = &sgmt;
tmp_gmt = gmtime(&t);
if (tmp_gmt == NULL)
{
throw sinsp_exception("cannot get gmtime");
}
*gmt = *tmp_gmt;
loc = localtime(&t);
if(loc == NULL)
{
throw sinsp_exception("cannot get localtime");
}
dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + (loc->tm_min - gmt->tm_min) * 60;
dir = loc->tm_year - gmt->tm_year;
if(dir == 0)
{
dir = loc->tm_yday - gmt->tm_yday;
}
dt += dir * 24 * 60 * 60;
return dt;
}
static inline bool str_match_start(const std::string& val, size_t len, const char* m)
{
return val.compare(0, len, m) == 0;
}
#define STR_MATCH(s) str_match_start(val, sizeof (s) -1, s)
///////////////////////////////////////////////////////////////////////////////
// sinsp_filter_check_fspath implementation
///////////////////////////////////////////////////////////////////////////////
const filtercheck_field_info sinsp_filter_check_fspath_fields[] =
{
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.name", "Path for Filesystem-related operation", "For any event type that deals with a filesystem path, the path the file syscall is operating on. This path is always fully resolved, prepending the thread cwd when needed."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.nameraw", "Raw path for Filesystem-related operation", "For any event type that deals with a filesystem path, the path the file syscall is operating on. This path is always the path provided to the syscall and may not be fully resolved."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.source", "Source path for Filesystem-related operation", "For any event type that deals with a filesystem path, and specifically for a source and target like mv, cp, etc, the source path the file syscall is operating on. This path is always fully resolved, prepending the thread cwd when needed."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.sourceraw", "Source path for Filesystem-related operation", "For any event type that deals with a filesystem path, and specifically for a source and target like mv, cp, etc, the source path the file syscall is operating on. This path is always the path provided to the syscall and may not be fully resolved."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.target", "Target path for Filesystem-related operation", "For any event type that deals with a filesystem path, and specifically for a target and target like mv, cp, etc, the target path the file syscall is operating on. This path is always fully resolved, prepending the thread cwd when needed."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fs.path.targetraw", "Target path for Filesystem-related operation", "For any event type that deals with a filesystem path, and specifically for a target and target like mv, cp, etc, the target path the file syscall is operating on. This path is always the path provided to the syscall and may not be fully resolved."},
};
sinsp_filter_check_fspath::sinsp_filter_check_fspath()
// These will either be populated when calling
// create_fspath_checks or copied from another filtercheck
// when calling set_fspath_checks().
: m_success_checks(new filtercheck_map_t()),
m_path_checks(new filtercheck_map_t()),
m_source_checks(new filtercheck_map_t()),
m_target_checks(new filtercheck_map_t())
{
m_info.m_name = "fs.path";
m_info.m_desc = "Every syscall that has a filesystem path in its arguments has these fields set with information related to the path arguments. This differs from the fd.* fields as it includes syscalls like unlink, rename, etc. that act directly on filesystem paths as compared to opened file descriptors.";
m_info.m_fields = sinsp_filter_check_fspath_fields;
m_info.m_nfields = sizeof(sinsp_filter_check_fspath_fields) / sizeof(sinsp_filter_check_fspath_fields[0]);
m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE;
};
std::shared_ptr<sinsp_filter_check> sinsp_filter_check_fspath::create_event_check(const char *name,
cmpop cop,
const char *value)
{
std::shared_ptr<sinsp_filter_check> chk(new sinsp_filter_check_event());
chk->m_inspector = m_inspector;
chk->m_cmpop = cop;
chk->m_boolop = BO_NONE;
chk->parse_field_name(name, true, true);
if(value)
{
chk->add_filter_value(value, strlen(value), 0);
}
return chk;
}
std::shared_ptr<sinsp_filter_check> sinsp_filter_check_fspath::create_fd_check(const char *name)
{
std::shared_ptr<sinsp_filter_check> chk(new sinsp_filter_check_fd());
chk->m_inspector = m_inspector;
chk->m_cmpop = CO_NONE;
chk->m_boolop = BO_NONE;
chk->parse_field_name(name, true, true);
return chk;
}
void sinsp_filter_check_fspath::create_fspath_checks()
{
std::shared_ptr<sinsp_filter_check> evt_arg_path = create_event_check("evt.rawarg.path");
std::shared_ptr<sinsp_filter_check> evt_arg_pathname = create_event_check("evt.rawarg.pathname");
std::shared_ptr<sinsp_filter_check> evt_arg_res_eq_0 = create_event_check("evt.rawarg.res", CO_EQ, "0");
std::shared_ptr<sinsp_filter_check> evt_arg_name = create_event_check("evt.rawarg.name");
std::shared_ptr<sinsp_filter_check> evt_fd_name = create_fd_check("fd.name");
std::shared_ptr<sinsp_filter_check> evt_arg_fd_ne_neg1 = create_event_check("evt.rawarg.fd", CO_NE, "-1");
std::shared_ptr<sinsp_filter_check> evt_arg_oldpath = create_event_check("evt.rawarg.oldpath");
std::shared_ptr<sinsp_filter_check> evt_arg_newpath = create_event_check("evt.rawarg.newpath");
std::shared_ptr<sinsp_filter_check> evt_arg_linkpath = create_event_check("evt.rawarg.linkpath");
std::shared_ptr<sinsp_filter_check> evt_arg_target = create_event_check("evt.rawarg.target");
std::shared_ptr<sinsp_filter_check> evt_arg_filename = create_event_check("evt.rawarg.filename");
std::shared_ptr<sinsp_filter_check> evt_arg_special = create_event_check("evt.rawarg.special");
std::shared_ptr<sinsp_filter_check> evt_arg_dev = create_event_check("evt.rawarg.dev");
std::shared_ptr<sinsp_filter_check> evt_arg_dir = create_event_check("evt.rawarg.dir");
m_success_checks->emplace(PPME_SYSCALL_MKDIR_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_MKDIR_2_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_MKDIR_2_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_MKDIRAT_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_MKDIRAT_X, evt_arg_res_eq_0);
m_success_checks->emplace(PPME_SYSCALL_RMDIR_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_RMDIR_2_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_RMDIR_2_X, evt_arg_res_eq_0);
m_success_checks->emplace(PPME_SYSCALL_UNLINK_X, evt_arg_res_eq_0);
m_success_checks->emplace(PPME_SYSCALL_UNLINKAT_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_UNLINK_2_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_UNLINK_2_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_UNLINKAT_2_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_UNLINKAT_2_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_OPEN_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_OPEN_X, evt_arg_fd_ne_neg1);
m_success_checks->emplace(PPME_SYSCALL_OPENAT_X, evt_arg_fd_ne_neg1);
m_path_checks->emplace(PPME_SYSCALL_OPENAT_2_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_OPENAT_2_X, evt_arg_fd_ne_neg1);
m_path_checks->emplace(PPME_SYSCALL_OPENAT2_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_OPENAT2_X, evt_arg_fd_ne_neg1);
m_path_checks->emplace(PPME_SYSCALL_FCHMODAT_X, evt_arg_filename);
m_success_checks->emplace(PPME_SYSCALL_FCHMODAT_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_CHMOD_X, evt_arg_filename);
m_success_checks->emplace(PPME_SYSCALL_CHMOD_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_FCHMOD_X, evt_fd_name);
m_success_checks->emplace(PPME_SYSCALL_FCHMOD_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_CHOWN_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_CHOWN_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_LCHOWN_X, evt_arg_path);
m_success_checks->emplace(PPME_SYSCALL_LCHOWN_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_FCHOWN_X, evt_fd_name);
m_success_checks->emplace(PPME_SYSCALL_FCHOWN_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_FCHOWNAT_X, evt_arg_pathname);
m_success_checks->emplace(PPME_SYSCALL_FCHOWNAT_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_QUOTACTL_X, evt_arg_special);
m_success_checks->emplace(PPME_SYSCALL_QUOTACTL_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_RENAME_X, evt_arg_oldpath);
m_target_checks->emplace(PPME_SYSCALL_RENAME_X, evt_arg_newpath);
m_success_checks->emplace(PPME_SYSCALL_RENAME_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_RENAMEAT_X, evt_arg_oldpath);
m_target_checks->emplace(PPME_SYSCALL_RENAMEAT_X, evt_arg_newpath);
m_success_checks->emplace(PPME_SYSCALL_RENAMEAT_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_RENAMEAT2_X, evt_arg_oldpath);
m_target_checks->emplace(PPME_SYSCALL_RENAMEAT2_X, evt_arg_newpath);
m_success_checks->emplace(PPME_SYSCALL_RENAMEAT2_X, evt_arg_res_eq_0);
m_success_checks->emplace(PPME_SYSCALL_LINK_X, evt_arg_res_eq_0);
m_success_checks->emplace(PPME_SYSCALL_LINKAT_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_LINK_2_X, evt_arg_newpath);
m_target_checks->emplace(PPME_SYSCALL_LINK_2_X, evt_arg_oldpath);
m_success_checks->emplace(PPME_SYSCALL_LINK_2_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_LINKAT_2_X, evt_arg_newpath);
m_target_checks->emplace(PPME_SYSCALL_LINKAT_2_X, evt_arg_oldpath);
m_success_checks->emplace(PPME_SYSCALL_LINKAT_2_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_SYMLINK_X, evt_arg_linkpath);
m_target_checks->emplace(PPME_SYSCALL_SYMLINK_X, evt_arg_target);
m_success_checks->emplace(PPME_SYSCALL_SYMLINK_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_SYMLINKAT_X, evt_arg_linkpath);
m_target_checks->emplace(PPME_SYSCALL_SYMLINKAT_X, evt_arg_target);
m_success_checks->emplace(PPME_SYSCALL_SYMLINKAT_X, evt_arg_res_eq_0);
m_source_checks->emplace(PPME_SYSCALL_MOUNT_X, evt_arg_dev);
m_target_checks->emplace(PPME_SYSCALL_MOUNT_X, evt_arg_dir);
m_success_checks->emplace(PPME_SYSCALL_MOUNT_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_UMOUNT_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_UMOUNT_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_UMOUNT_1_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_UMOUNT_1_X, evt_arg_res_eq_0);
m_path_checks->emplace(PPME_SYSCALL_UMOUNT2_X, evt_arg_name);
m_success_checks->emplace(PPME_SYSCALL_UMOUNT2_X, evt_arg_res_eq_0);
}
void sinsp_filter_check_fspath::set_fspath_checks(std::shared_ptr<filtercheck_map_t> success_checks,
std::shared_ptr<filtercheck_map_t> path_checks,
std::shared_ptr<filtercheck_map_t> source_checks,
std::shared_ptr<filtercheck_map_t> target_checks)
{
m_success_checks = success_checks;
m_path_checks = path_checks;
m_source_checks = source_checks;
m_target_checks = target_checks;
}
sinsp_filter_check* sinsp_filter_check_fspath::allocate_new()
{
// If not yet populated, do so now. The maps will be empty
// *only* for the initial filtercheck created in
// filter_check_list.
if(m_path_checks->empty())
{
create_fspath_checks();
}
sinsp_filter_check_fspath* ret = new sinsp_filter_check_fspath();
ret->set_fspath_checks(m_success_checks, m_path_checks, m_source_checks, m_target_checks);
return ret;
}
uint8_t* sinsp_filter_check_fspath::extract(sinsp_evt* evt, OUT uint32_t* len, bool sanitize_strings)
{
*len = 0;
ASSERT(evt);
// First check the success conditions.
auto it = m_success_checks->find(evt->get_type());
if(it == m_success_checks->end() || !it->second->compare(evt))
{
return NULL;
}
std::optional<std::reference_wrapper<std::string>> enter_param;
std::vector<extract_value_t> extract_values;
switch(m_field_id)
{
case TYPE_NAME:
case TYPE_NAMERAW:
// For some event types we need to get the values from the enter event instead.
switch(evt->get_type())
{
case PPME_SYSCALL_MKDIR_X:
case PPME_SYSCALL_RMDIR_X:
case PPME_SYSCALL_UNLINK_X:
enter_param = evt->get_enter_evt_param("path");
if(!enter_param.has_value())
{
return NULL;
}
m_tstr = enter_param.value();
break;
case PPME_SYSCALL_UNLINKAT_X:
case PPME_SYSCALL_OPENAT_X:
enter_param = evt->get_enter_evt_param("name");
if(!enter_param.has_value())
{
return NULL;
}
m_tstr = enter_param.value();
break;
default:
if (!extract_fspath(evt, extract_values, m_path_checks))
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
};
break;
case TYPE_SOURCE:
case TYPE_SOURCERAW:
// For some event types we need to get the values from the enter event instead.
switch(evt->get_type())
{
case PPME_SYSCALL_LINK_X:
case PPME_SYSCALL_LINKAT_X:
enter_param = evt->get_enter_evt_param("newpath");
if(!enter_param.has_value())
{
return NULL;
}
m_tstr = enter_param.value();
break;
default:
if(!extract_fspath(evt, extract_values, m_source_checks))
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
};
break;
case TYPE_TARGET:
case TYPE_TARGETRAW:
// For some event types we need to get the values from the enter event instead.
switch(evt->get_type())
{
case PPME_SYSCALL_LINK_X:
case PPME_SYSCALL_LINKAT_X:
enter_param = evt->get_enter_evt_param("oldpath");
if(!enter_param.has_value())
{
return NULL;
}
m_tstr = enter_param.value();
break;
default:
if (!extract_fspath(evt, extract_values, m_target_checks))
{
return NULL;
}
m_tstr.assign((char *) extract_values[0].ptr, extract_values[0].len);
};
break;
default:
return NULL;
}
// For the non-raw fields, if the path is not absolute,
// prepend the cwd of the threadinfo to the path.
if((m_field_id == TYPE_NAME ||
m_field_id == TYPE_SOURCE ||
m_field_id == TYPE_TARGET)
&& m_tstr.front() != '/')
{
sinsp_threadinfo* tinfo = evt->get_thread_info();
std::filesystem::path tstr = tinfo->get_cwd() + m_tstr;
m_tstr = std::filesystem::absolute(tstr).lexically_normal().string();
}
// If m_tstr ends in a c-style \0, remove it to be
// consistent. Generally, the evt.rawarg fields above *will*
// end in c-style \0 characters, while the ones that extract
// from the enter event or have values populated by parsers
// (e.g. fchmod/fchown) will not.
if(m_tstr.back() == '\0')
{
m_tstr.pop_back();
}
RETURN_EXTRACT_STRING(m_tstr);
}
bool sinsp_filter_check_fspath::extract_fspath(sinsp_evt* evt,
OUT std::vector<extract_value_t>& values,
std::shared_ptr<filtercheck_map_t> checks)
{
sinsp_evt* extract_evt = evt;
auto it = checks->find(extract_evt->get_type());
if(it == checks->end())
{
return false;
}
if(!it->second->extract(extract_evt, values, true) ||
values.size() != 1)
{
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_filter_check_fd implementation
///////////////////////////////////////////////////////////////////////////////
const filtercheck_field_info sinsp_filter_check_fd_fields[] =
{
{PT_INT64, EPF_NONE, PF_ID, "fd.num", "FD Number", "the unique number identifying the file descriptor."},
{PT_CHARBUF, EPF_NONE, PF_DEC, "fd.type", "FD Type", "type of FD. Can be 'file', 'directory', 'ipv4', 'ipv6', 'unix', 'pipe', 'event', 'signalfd', 'eventpoll', 'inotify' 'signalfd' or 'memfd'."},
{PT_CHARBUF, EPF_NONE, PF_DEC, "fd.typechar", "FD Type Char", "type of FD as a single character. Can be 'f' for file, 4 for IPv4 socket, 6 for IPv6 socket, 'u' for unix socket, p for pipe, 'e' for eventfd, 's' for signalfd, 'l' for eventpoll, 'i' for inotify, 'b' for bpf, 'u' for userfaultd, 'r' for io_uring, 'm' for memfd ,'o' for unknown."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.name", "FD Name", "FD full name. If the fd is a file, this field contains the full path. If the FD is a socket, this field contain the connection tuple."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.directory", "FD Directory", "If the fd is a file, the directory that contains it."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.filename", "FD Filename", "If the fd is a file, the filename without the path."},
{PT_IPADDR, EPF_FILTER_ONLY, PF_NA, "fd.ip", "FD IP Address", "matches the ip address (client or server) of the fd."},
{PT_IPADDR, EPF_NONE, PF_NA, "fd.cip", "FD Client Address", "client IP address."},
{PT_IPADDR, EPF_NONE, PF_NA, "fd.sip", "FD Server Address", "server IP address."},
{PT_IPADDR, EPF_NONE, PF_NA, "fd.lip", "FD Local Address", "local IP address."},
{PT_IPADDR, EPF_NONE, PF_NA, "fd.rip", "FD Remote Address", "remote IP address."},
{PT_PORT, EPF_FILTER_ONLY, PF_DEC, "fd.port", "FD Port", "matches the port (either client or server) of the fd."},
{PT_PORT, EPF_NONE, PF_DEC, "fd.cport", "FD Client Port", "for TCP/UDP FDs, the client port."},
{PT_PORT, EPF_NONE, PF_DEC, "fd.sport", "FD Server Port", "for TCP/UDP FDs, server port."},
{PT_PORT, EPF_NONE, PF_DEC, "fd.lport", "FD Local Port", "for TCP/UDP FDs, the local port."},
{PT_PORT, EPF_NONE, PF_DEC, "fd.rport", "FD Remote Port", "for TCP/UDP FDs, the remote port."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.l4proto", "FD IP Protocol", "the IP protocol of a socket. Can be 'tcp', 'udp', 'icmp' or 'raw'."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.sockfamily", "FD Socket Family", "the socket family for socket events. Can be 'ip' or 'unix'."},
{PT_BOOL, EPF_NONE, PF_NA, "fd.is_server", "FD Server", "'true' if the process owning this FD is the server endpoint in the connection."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.uid", "FD ID", "a unique identifier for the FD, created by chaining the FD number and the thread ID."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.containername", "FD Container Name", "chaining of the container ID and the FD name. Useful when trying to identify which container an FD belongs to."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.containerdirectory", "FD Container Directory", "chaining of the container ID and the directory name. Useful when trying to identify which container a directory belongs to."},
{PT_PORT, EPF_FILTER_ONLY, PF_NA, "fd.proto", "FD Protocol", "matches the protocol (either client or server) of the fd."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.cproto", "FD Client Protocol", "for TCP/UDP FDs, the client protocol."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.sproto", "FD Server Protocol", "for TCP/UDP FDs, server protocol."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.lproto", "FD Local Protocol", "for TCP/UDP FDs, the local protocol."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.rproto", "FD Remote Protocol", "for TCP/UDP FDs, the remote protocol."},
{PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.net", "FD IP Network", "matches the IP network (client or server) of the fd."},
{PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.cnet", "FD Client Network", "matches the client IP network of the fd."},
{PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.snet", "FD Server Network", "matches the server IP network of the fd."},
{PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.lnet", "FD Local Network", "matches the local IP network of the fd."},
{PT_IPNET, EPF_FILTER_ONLY, PF_NA, "fd.rnet", "FD Remote Network", "matches the remote IP network of the fd."},
{PT_BOOL, EPF_NONE, PF_NA, "fd.connected", "FD Connected", "for TCP/UDP FDs, 'true' if the socket is connected."},
{PT_BOOL, EPF_NONE, PF_NA, "fd.name_changed", "FD Name Changed", "True when an event changes the name of an fd used by this event. This can occur in some cases such as udp connections where the connection tuple changes."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.cip.name", "FD Client Domain Name", "Domain name associated with the client IP address."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.sip.name", "FD Server Domain Name", "Domain name associated with the server IP address."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.lip.name", "FD Local Domain Name", "Domain name associated with the local IP address."},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.rip.name", "FD Remote Domain Name", "Domain name associated with the remote IP address."},
{PT_INT32, EPF_NONE, PF_HEX, "fd.dev", "FD Device", "device number (major/minor) containing the referenced file"},
{PT_INT32, EPF_NONE, PF_DEC, "fd.dev.major", "FD Major Device", "major device number containing the referenced file"},
{PT_INT32, EPF_NONE, PF_DEC, "fd.dev.minor", "FD Minor Device", "minor device number containing the referenced file"},
{PT_INT64, EPF_NONE, PF_DEC, "fd.ino", "FD Inode Number", "inode number of the referenced file"},
{PT_CHARBUF, EPF_NONE, PF_NA, "fd.nameraw", "FD Name Raw", "FD full name raw. Just like fd.name, but only used if fd is a file path. File path is kept raw with limited sanitization and without deriving the absolute path."},
{PT_CHARBUF, EPF_IS_LIST, PF_DEC, "fd.types", "FD Type", "List of FD types in used. Can be passed an fd number e.g. fd.types[0] to get the type of stdout as a single item list."},
};
sinsp_filter_check_fd::sinsp_filter_check_fd()
{
m_tinfo = NULL;
m_fdinfo = NULL;
m_argid = -1;
m_info.m_name = "fd";
m_info.m_desc = "Every syscall that has a file descriptor in its arguments has these fields set with information related to the file.";
m_info.m_fields = sinsp_filter_check_fd_fields;
m_info.m_nfields = sizeof(sinsp_filter_check_fd_fields) / sizeof(sinsp_filter_check_fd_fields[0]);
m_info.m_flags = filter_check_info::FL_WORKS_ON_THREAD_TABLE;
}
sinsp_filter_check* sinsp_filter_check_fd::allocate_new()
{
return (sinsp_filter_check*) new sinsp_filter_check_fd();
}
int32_t sinsp_filter_check_fd::extract_arg(string fldname, string val)
{
uint32_t parsed_len = 0;
//
// 'arg' and 'resarg' are handled in a custom way
//
if(val[fldname.size()] == '[')
{
parsed_len = (uint32_t)val.find(']');
string numstr = val.substr(fldname.size() + 1, parsed_len - fldname.size() - 1);
m_argid = sinsp_numparser::parsed64(numstr);
parsed_len++;
}
return parsed_len;
}
int32_t sinsp_filter_check_fd::parse_field_name(const char* str, bool alloc_state, bool needed_for_filtering)
{
string val(str);
if(STR_MATCH("fd.types"))
{
m_field_id = TYPE_FDTYPES;
m_field = &m_info.m_fields[m_field_id];
int32_t res = 0;
res = extract_arg("fd.types", val);
if(res == 0)
{
m_argid = -1;
res = (int32_t)val.size();
}
return res;
}
return sinsp_filter_check::parse_field_name(str, alloc_state, needed_for_filtering);
}
bool sinsp_filter_check_fd::extract_fdname_from_creator(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings, bool fd_nameraw)
{
const char* resolved_argstr;
uint16_t etype = evt->get_type();
if(PPME_IS_ENTER(etype))
{
return false;
}
switch(etype)
{
case PPME_SYSCALL_OPEN_X:
case PPME_SOCKET_ACCEPT_X:
case PPME_SOCKET_ACCEPT_5_X:
case PPME_SOCKET_ACCEPT4_X:
case PPME_SOCKET_ACCEPT4_5_X:
case PPME_SOCKET_ACCEPT4_6_X:
case PPME_SYSCALL_CREAT_X:
{
const char* argstr = evt->get_param_as_str(1, &resolved_argstr,
m_inspector->get_buffer_format());
if(resolved_argstr[0] != 0)
{
m_tstr = resolved_argstr;
}
else
{
m_tstr = argstr;
}
return true;
}
case PPME_SOCKET_CONNECT_X:
{
const char* argstr = evt->get_param_as_str(1, &resolved_argstr,
m_inspector->get_buffer_format());
if(resolved_argstr[0] != 0)
{
m_tstr = resolved_argstr;
}
else
{
m_tstr = argstr;
}
return true;
}
case PPME_SYSCALL_OPENAT_X:
case PPME_SYSCALL_OPENAT_2_X:
case PPME_SYSCALL_OPENAT2_X:
{
sinsp_evt enter_evt;
sinsp_evt_param *parinfo;
char *name;
uint32_t namelen;
string sdir;
if(etype == PPME_SYSCALL_OPENAT_X)
{
//
// XXX This is highly inefficient, as it re-requests the enter event and then
// does unnecessary allocations and copies. We assume that failed openat() happen
// rarely enough that we don't care.
//
if(!m_inspector->get_parser()->retrieve_enter_event(&enter_evt, evt))
{
return false;
}
}
parinfo = etype == PPME_SYSCALL_OPENAT_X ? enter_evt.get_param(1) : evt->get_param(2);
name = parinfo->m_val;
namelen = parinfo->m_len;
parinfo = etype == PPME_SYSCALL_OPENAT_X ? enter_evt.get_param(0) : evt->get_param(1);
ASSERT(parinfo->m_len == sizeof(int64_t));
int64_t dirfd = *(int64_t *)parinfo->m_val;
sinsp_parser::parse_dirfd(evt, name, dirfd, &sdir);
char fullpath[SCAP_MAX_PATH_SIZE];
sinsp_utils::concatenate_paths(fullpath, SCAP_MAX_PATH_SIZE,
sdir.c_str(),
(uint32_t)sdir.length(),
name,
namelen);
if(fd_nameraw)
{
m_tstr = name;
}
else
{
m_tstr = fullpath;
}
if(sanitize_strings)
{
sanitize_string(m_tstr);
}
return true;
}
case PPME_SYSCALL_OPEN_BY_HANDLE_AT_X:
{
sinsp_evt_param *parinfo;
char *fullpath;
parinfo = evt->get_param(3);
fullpath = parinfo->m_val;
m_tstr = fullpath;
if(sanitize_strings)
{
sanitize_string(m_tstr);
}
return true;
}
default:
m_tstr = "";
return true;
}
}
uint8_t* sinsp_filter_check_fd::extract_from_null_fd(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings)
{
*len = 0;
//
// Even is there's no fd, we still try to extract a name from exit events that create
// one. With these events, the fact that there's no FD means that the call failed,
// but even if that happened we still want to collect the name.
//
switch(m_field_id)
{
case TYPE_FDNAME:
{
if(extract_fdname_from_creator(evt, len, sanitize_strings) == true)
{
RETURN_EXTRACT_STRING(m_tstr);
}
else
{
return NULL;
}
}
case TYPE_CONTAINERNAME:
{
if(extract_fdname_from_creator(evt, len, sanitize_strings) == true)
{
m_tstr = m_tinfo->m_container_id + ':' + m_tstr;
RETURN_EXTRACT_STRING(m_tstr);
}
else
{
return NULL;
}
}
case TYPE_DIRECTORY:
case TYPE_CONTAINERDIRECTORY:
{
if(extract_fdname_from_creator(evt, len, sanitize_strings) == true)
{
if(sanitize_strings)
{
sanitize_string(m_tstr);
}
size_t pos = m_tstr.rfind('/');
if(pos != string::npos && pos != 0)
{
if(pos < m_tstr.size() - 1)
{
m_tstr.resize(pos);
}
}
else
{
m_tstr = "/";
}
if(m_field_id == TYPE_CONTAINERDIRECTORY)
{
m_tstr = m_tinfo->m_container_id + ':' + m_tstr;
}
RETURN_EXTRACT_STRING(m_tstr);
}
else
{
return NULL;
}
}
case TYPE_FILENAME:
{
if(evt->get_type() != PPME_SYSCALL_OPEN_E && evt->get_type() != PPME_SYSCALL_OPENAT_E &&
evt->get_type() != PPME_SYSCALL_OPENAT_2_E && evt->get_type() != PPME_SYSCALL_OPENAT2_E &&
evt->get_type() != PPME_SYSCALL_CREAT_E)
{
return NULL;
}
if(extract_fdname_from_creator(evt, len, sanitize_strings) == true)
{
if(sanitize_strings)
{
sanitize_string(m_tstr);
}
size_t pos = m_tstr.rfind('/');
if(pos != string::npos)
{
if(pos < m_tstr.size() - 1)
{
m_tstr = m_tstr.substr(pos + 1, string::npos);
}
}
RETURN_EXTRACT_STRING(m_tstr);
}
else
{
return NULL;
}
}
case TYPE_FDTYPECHAR:
*len = 1;
switch(PPME_MAKE_ENTER(evt->get_type()))
{
case PPME_SYSCALL_OPEN_E:
case PPME_SYSCALL_OPENAT_E:
case PPME_SYSCALL_OPENAT_2_E:
case PPME_SYSCALL_OPENAT2_E:
case PPME_SYSCALL_CREAT_E:
m_tcstr[0] = CHAR_FD_FILE;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SOCKET_SOCKET_E:
case PPME_SOCKET_ACCEPT_E:
case PPME_SOCKET_ACCEPT_5_E:
case PPME_SOCKET_ACCEPT4_E:
case PPME_SOCKET_ACCEPT4_5_E:
case PPME_SOCKET_ACCEPT4_6_E:
//
// Note, this is not accurate, because it always
// returns IPv4 even if this could be IPv6 or unix.
// For the moment, I assume it's better than nothing, and doing
// real event parsing here would be a pain.
//
m_tcstr[0] = CHAR_FD_IPV4_SOCK;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SYSCALL_PIPE_E:
case PPME_SYSCALL_PIPE2_E:
m_tcstr[0] = CHAR_FD_FIFO;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SYSCALL_EVENTFD_E:
case PPME_SYSCALL_EVENTFD2_E:
m_tcstr[0] = CHAR_FD_EVENT;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SYSCALL_SIGNALFD_E:
case PPME_SYSCALL_SIGNALFD4_E:
m_tcstr[0] = CHAR_FD_SIGNAL;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SYSCALL_TIMERFD_CREATE_E:
m_tcstr[0] = CHAR_FD_TIMERFD;
m_tcstr[1] = 0;
return m_tcstr;
case PPME_SYSCALL_INOTIFY_INIT_E:
case PPME_SYSCALL_INOTIFY_INIT1_E:
m_tcstr[0] = CHAR_FD_INOTIFY;
m_tcstr[1] = 0;
return m_tcstr;
default:
m_tcstr[0] = 'o';
m_tcstr[1] = 0;
return m_tcstr;
}
case TYPE_FDNAMERAW:
{
if(extract_fdname_from_creator(evt, len, sanitize_strings, true) == true)
{
remove_duplicate_path_separators(m_tstr);
RETURN_EXTRACT_STRING(m_tstr);
}
else
{
return NULL;
}
}
default:
return NULL;
}
}
bool sinsp_filter_check_fd::extract(sinsp_evt *evt, OUT std::vector<extract_value_t>& values, bool sanitize_strings)
{
values.clear();
if(!extract_fd(evt))
{
return false;
}
if(m_field_id == TYPE_FDTYPES && m_argid == -1)
{
// We are of the form fd.types so gather all open file
// descriptor types into a (de-duplicated) list
//
// Note that fd.types[num] handling is in the following
// implementation of sinsp_filter_check_fd::extract
// All of the pointers come from the fd_typesting() function so
// we shouldn't have the situation of two distinct pointers to
// the same string literal and we can just compare based on pointer
std::unordered_set<char*> fd_types;
// Iterate over the list of open file descriptors and add all
// unique file descriptor types to the vector for comparison
auto fd_type_gather = [this, &fd_types, &values](uint64_t, const sinsp_fdinfo_t& fdinfo)
{
char* type = fdinfo.get_typestring();
if (fd_types.emplace(type).second)
{
extract_value_t val;
val.ptr = (uint8_t*)type;
val.len = strlen(type);
values.push_back(val);
}
return true;
};
m_tinfo->loop_fds(fd_type_gather);
return true;
}
return sinsp_filter_check::extract(evt, values, sanitize_strings);
}
uint8_t* sinsp_filter_check_fd::extract(sinsp_evt *evt, OUT uint32_t* len, bool sanitize_strings)
{
*len = 0;
ASSERT(evt);
if(!extract_fd(evt))
{
return NULL;
}
//
// TYPE_FDNUM doesn't need fdinfo
//
if(m_field_id == TYPE_FDNUM)
{
RETURN_EXTRACT_VAR(m_tinfo->m_lastevent_fd);
}
switch(m_field_id)
{
case TYPE_FDNAME:
case TYPE_CONTAINERNAME:
if(m_fdinfo == NULL)
{
return extract_from_null_fd(evt, len, sanitize_strings);
}
if(evt->get_type() == PPME_SOCKET_CONNECT_X)
{
sinsp_evt_param *parinfo;
parinfo = evt->get_param(0);
ASSERT(parinfo->m_len == sizeof(uint64_t));
int64_t retval = *(int64_t*)parinfo->m_val;
if(retval < 0)
{
return extract_from_null_fd(evt, len, sanitize_strings);
}
}
if(m_field_id == TYPE_CONTAINERNAME)
{
ASSERT(m_tinfo != NULL);
m_tstr = m_tinfo->m_container_id + ':' + m_fdinfo->m_name;
}
else
{
m_tstr = m_fdinfo->m_name;
}
if(sanitize_strings)
{
sanitize_string(m_tstr);
}
RETURN_EXTRACT_STRING(m_tstr);
break;
case TYPE_FDTYPES:
case TYPE_FDTYPE:
if(m_fdinfo == NULL)
{
return NULL;
}
else
{
uint8_t *typestr = (uint8_t*)m_fdinfo->get_typestring();