-
Notifications
You must be signed in to change notification settings - Fork 324
/
kernel.go
1366 lines (1262 loc) · 41.7 KB
/
kernel.go
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package selectors
import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/api/processapi"
gt "github.com/cilium/tetragon/pkg/generictypes"
"github.com/cilium/tetragon/pkg/idtable"
"github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
"github.com/cilium/tetragon/pkg/kernels"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/reader/network"
)
const (
ActionTypeInvalid = -1
ActionTypePost = 0
ActionTypeFollowFd = 1
ActionTypeSigKill = 2
ActionTypeUnfollowFd = 3
ActionTypeOverride = 4
ActionTypeCopyFd = 5
ActionTypeGetUrl = 6
ActionTypeDnsLookup = 7
ActionTypeNoPost = 8
ActionTypeSignal = 9
ActionTypeTrackSock = 10
ActionTypeUntrackSock = 11
ActionTypeNotifyEnforcer = 12
)
var actionTypeTable = map[string]uint32{
"post": ActionTypePost,
"followfd": ActionTypeFollowFd,
"unfollowfd": ActionTypeUnfollowFd,
"sigkill": ActionTypeSigKill,
"override": ActionTypeOverride,
"copyfd": ActionTypeCopyFd,
"geturl": ActionTypeGetUrl,
"dnslookup": ActionTypeDnsLookup,
"nopost": ActionTypeNoPost,
"signal": ActionTypeSignal,
"tracksock": ActionTypeTrackSock,
"untracksock": ActionTypeUntrackSock,
"notifyenforcer": ActionTypeNotifyEnforcer,
}
var actionTypeStringTable = map[uint32]string{
ActionTypePost: "post",
ActionTypeFollowFd: "followfd",
ActionTypeUnfollowFd: "unfollowfd",
ActionTypeSigKill: "sigkill",
ActionTypeOverride: "override",
ActionTypeCopyFd: "copyfd",
ActionTypeGetUrl: "geturl",
ActionTypeDnsLookup: "dnslookup",
ActionTypeNoPost: "nopost",
ActionTypeSignal: "signal",
ActionTypeTrackSock: "tracksock",
ActionTypeUntrackSock: "untracksock",
ActionTypeNotifyEnforcer: "notifyenforcer",
}
const (
ActionRateLimitScopeThread = iota
ActionRateLimitScopeProcess
ActionRateLimitScopeGlobal
)
var actionRateLimitScope = map[string]uint32{
"thread": ActionRateLimitScopeThread,
"process": ActionRateLimitScopeProcess,
"global": ActionRateLimitScopeGlobal,
}
// Action argument table entry (for URL and FQDN arguments)
type ActionArgEntry struct {
arg string
tableId idtable.EntryID
}
func (g *ActionArgEntry) SetID(id idtable.EntryID) {
g.tableId = id
}
func (g *ActionArgEntry) GetArg() string {
return g.arg
}
func MatchActionSigKill(spec interface{}) bool {
var sels []v1alpha1.KProbeSelector
switch s := spec.(type) {
case *v1alpha1.KProbeSpec:
sels = s.Selectors
case *v1alpha1.TracepointSpec:
sels = s.Selectors
default:
return false
}
for _, s := range sels {
for _, act := range s.MatchActions {
if strings.ToLower(act.Action) == actionTypeStringTable[ActionTypeSigKill] {
return true
}
}
}
return false
}
const (
namespaceTypeUts = 0
namespaceTypeIpc = 1
namespaceTypeMnt = 2
namespaceTypePid = 3
namespaceTypePidForChildren = 4
namespaceTypeNet = 5
namespaceTypeTime = 6
namespaceTypeTimeForChildren = 7
namespaceTypeCgroup = 8
namespaceTypeUser = 9
)
var namespaceTypeTable = map[string]uint32{
"uts": namespaceTypeUts,
"ipc": namespaceTypeIpc,
"mnt": namespaceTypeMnt,
"pid": namespaceTypePid,
"pidforchildren": namespaceTypePidForChildren,
"net": namespaceTypeNet,
"time": namespaceTypeTime,
"timeforchildren": namespaceTypeTimeForChildren,
"cgroup": namespaceTypeCgroup,
"user": namespaceTypeUser,
}
const (
capsPermitted = 0
capsEffective = 1
capsInheritable = 2
)
var capabilitiesTypeTable = map[string]uint32{
"effective": capsEffective,
"inheritable": capsInheritable,
"permitted": capsPermitted,
}
const (
SelectorOpGT = 1
SelectorOpLT = 2
SelectorOpEQ = 3
SelectorOpNEQ = 4
// Pid and Namespace ops
SelectorOpIn = 5
SelectorOpNotIn = 6
// String ops
SelectorOpPrefix = 8
SelectorOpPostfix = 9
// Map ops
SelectorInMap = 10
SelectorNotInMap = 11
SelectorOpMASK = 12
// socket ops
SelectorOpSaddr = 13
SelectorOpDaddr = 14
SelectorOpSport = 15
SelectorOpDport = 16
SelectorOpProtocol = 17
SelectorOpNotSport = 18
SelectorOpNotDport = 19
SelectorOpSportPriv = 20
SelectorOpNotSportPriv = 21
SelectorOpDportPriv = 22
SelectorOpNotDportPriv = 23
SelectorOpNotSaddr = 24
SelectorOpNotDaddr = 25
// file ops
SelectorOpNotPrefix = 26
SelectorOpNotPostfix = 27
// more socket ops
SelectorOpFamily = 28
SelectorOpState = 29
)
var selectorOpStringTable = map[uint32]string{
SelectorOpGT: "gt",
SelectorOpLT: "lt",
SelectorOpEQ: "Equal",
SelectorOpNEQ: "NotEqual",
SelectorOpIn: "In",
SelectorOpNotIn: "NotIn",
SelectorOpPrefix: "Prefix",
SelectorOpPostfix: "Postfix",
SelectorInMap: "InMap",
SelectorNotInMap: "NotInMap",
SelectorOpMASK: "Mask",
SelectorOpSaddr: "SAddr",
SelectorOpDaddr: "DAddr",
SelectorOpSport: "SPort",
SelectorOpDport: "DPort",
SelectorOpProtocol: "Protocol",
SelectorOpNotSport: "NotSPort",
SelectorOpNotDport: "NotDPort",
SelectorOpSportPriv: "SPortPriv",
SelectorOpNotSportPriv: "NotSPortPriv",
SelectorOpDportPriv: "DPortPriv",
SelectorOpNotDportPriv: "NotDPortPriv",
SelectorOpNotSaddr: "NotSAddr",
SelectorOpNotDaddr: "NotDAddr",
SelectorOpNotPrefix: "NotPrefix",
SelectorOpNotPostfix: "NotPostfix",
SelectorOpFamily: "Family",
SelectorOpState: "State",
}
func SelectorOp(op string) (uint32, error) {
switch op {
case "gt", "GT", "GreaterThan":
return SelectorOpGT, nil
case "lt", "LT", "LessThan":
return SelectorOpLT, nil
case "eq", "Equal":
return SelectorOpEQ, nil
case "neq", "NotEqual":
return SelectorOpNEQ, nil
case "In":
return SelectorOpIn, nil
case "NotIn":
return SelectorOpNotIn, nil
case "prefix", "Prefix":
return SelectorOpPrefix, nil
case "notprefix", "NotPrefix":
return SelectorOpNotPrefix, nil
case "postfix", "Postfix":
return SelectorOpPostfix, nil
case "notpostfix", "NotPostfix":
return SelectorOpNotPostfix, nil
case "InMap":
return SelectorInMap, nil
case "NotInMap":
return SelectorNotInMap, nil
case "mask", "Mask":
return SelectorOpMASK, nil
case "saddr", "Saddr", "SAddr":
return SelectorOpSaddr, nil
case "daddr", "Daddr", "DAddr":
return SelectorOpDaddr, nil
case "notsaddr", "NotSaddr", "NotSAddr":
return SelectorOpNotSaddr, nil
case "notdaddr", "NotDaddr", "NotDAddr":
return SelectorOpNotDaddr, nil
case "sport", "Sport", "SPort":
return SelectorOpSport, nil
case "dport", "Dport", "DPort":
return SelectorOpDport, nil
case "protocol", "Protocol":
return SelectorOpProtocol, nil
case "notsport", "NotSport", "NotSPort":
return SelectorOpNotSport, nil
case "notdport", "NotDport", "NotDPort":
return SelectorOpNotDport, nil
case "sportpriv", "SportPriv", "SPortPriv":
return SelectorOpSportPriv, nil
case "dportpriv", "DportPriv", "DPortPriv":
return SelectorOpDportPriv, nil
case "notsportpriv", "NotSportPriv", "NotSPortPriv":
return SelectorOpNotSportPriv, nil
case "notdportpriv", "NotDportPriv", "NotDPortPriv":
return SelectorOpNotDportPriv, nil
case "family", "Family":
return SelectorOpFamily, nil
case "state", "State":
return SelectorOpState, nil
}
return 0, fmt.Errorf("Unknown op '%s'", op)
}
const (
pidNamespacePid = 0x1
pidFollowForks = 0x2
)
func pidSelectorFlags(pid *v1alpha1.PIDSelector) uint32 {
flags := uint32(0)
if pid.IsNamespacePID {
flags |= pidNamespacePid
}
if pid.FollowForks {
flags |= pidFollowForks
}
return flags
}
func pidSelectorValue(pid *v1alpha1.PIDSelector) ([]byte, uint32) {
b := make([]byte, len(pid.Values)*4)
for i, v := range pid.Values {
off := i * 4
binary.LittleEndian.PutUint32(b[off:], v)
}
return b, uint32(len(b))
}
func ParseMatchPid(k *KernelSelectorState, pid *v1alpha1.PIDSelector) error {
op, err := SelectorOp(pid.Operator)
if err != nil {
return fmt.Errorf("matchpid error: %w", err)
}
WriteSelectorUint32(&k.data, op)
flags := pidSelectorFlags(pid)
WriteSelectorUint32(&k.data, flags)
value, size := pidSelectorValue(pid)
WriteSelectorUint32(&k.data, size/4)
WriteSelectorByteArray(&k.data, value, size)
return nil
}
func ParseMatchPids(k *KernelSelectorState, matchPids []v1alpha1.PIDSelector) error {
loff := AdvanceSelectorLength(&k.data)
for _, p := range matchPids {
if err := ParseMatchPid(k, &p); err != nil {
return err
}
}
WriteSelectorLength(&k.data, loff)
return nil
}
func ActionTypeFromString(action string) int32 {
act, ok := actionTypeTable[strings.ToLower(action)]
if !ok {
return ActionTypeInvalid
}
return int32(act)
}
func argSelectorType(arg *v1alpha1.ArgSelector, sig []v1alpha1.KProbeArg) (uint32, error) {
for _, s := range sig {
if arg.Index == s.Index {
// TBD: We shouldn't get this far with invalid KProbe args
// KProbe args have already been validated
return uint32(gt.GenericTypeFromString(s.Type)), nil
}
}
return 0, fmt.Errorf("argFilter for unknown index")
}
func writeRangeInMap(v string, ty uint32, op uint32, m *ValueMap) error {
// We store the start and end of the range as uint64s for unsigned values, and as int64s
// for signed values. This is to allow both a signed range from -5 to 5, and also an
// unsigned range where at least limit exceeds max(int64). If we only stored the range
// limits as uint64s, then the range from -5 to 5 would be interpretted as being from
// 5 to uint64(-5), which is the literal opposite of what was intended. If we only stored
// the range as int64s, then we couldn't correctly accommodate values that exceed max(int64),
// for similar reasons.
var uRangeVal [2]uint64
var sRangeVal [2]int64
rangeStr := strings.Split(v, ":")
if len(rangeStr) > 2 {
return fmt.Errorf("MatchArgs value %s invalid: range should be 'min:max'", v)
} else if len(rangeStr) == 1 {
// If only one value in the string, e.g. "5", then add it a second time to simulate
// a range that starts and ends with itself, e.g. as if "5:5" had been specified.
rangeStr = append(rangeStr, rangeStr[0])
// Special actions for particular network ops
switch op {
case SelectorOpProtocol:
protocol, err := network.InetProtocolNumber(v)
if err == nil {
protocolStr := fmt.Sprintf("%d", protocol)
rangeStr = []string{protocolStr, protocolStr}
}
case SelectorOpFamily:
family, err := network.InetFamilyNumber(v)
if err == nil {
familyStr := fmt.Sprintf("%d", family)
rangeStr = []string{familyStr, familyStr}
}
case SelectorOpState:
state, err := network.TcpStateNumber(v)
if err == nil {
stateStr := fmt.Sprintf("%d", state)
rangeStr = []string{stateStr, stateStr}
}
}
}
for idx := 0; idx < 2; idx++ {
switch ty {
case gt.GenericIntType, gt.GenericS64Type, gt.GenericS32Type, gt.GenericS16Type, gt.GenericS8Type, gt.GenericSyscall64, gt.GenericSizeType:
i, err := strconv.ParseInt(rangeStr[idx], 10, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
sRangeVal[idx] = i
case gt.GenericU64Type, gt.GenericU32Type, gt.GenericU16Type, gt.GenericU8Type:
i, err := strconv.ParseUint(rangeStr[idx], 10, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
uRangeVal[idx] = i
default:
return fmt.Errorf("Unknown type: %d", ty)
}
}
switch ty {
case gt.GenericIntType, gt.GenericS64Type, gt.GenericS32Type, gt.GenericS16Type, gt.GenericS8Type, gt.GenericSyscall64, gt.GenericSizeType:
if sRangeVal[0] > sRangeVal[1] {
sRangeVal[0], sRangeVal[1] = sRangeVal[1], sRangeVal[0]
}
for val := sRangeVal[0]; val <= sRangeVal[1]; val++ {
var valByte [8]byte
binary.LittleEndian.PutUint64(valByte[:], uint64(val))
m.Data[valByte] = struct{}{}
}
case gt.GenericU64Type, gt.GenericU32Type, gt.GenericU16Type, gt.GenericU8Type:
if uRangeVal[0] > uRangeVal[1] {
uRangeVal[0], uRangeVal[1] = uRangeVal[1], uRangeVal[0]
}
for val := uRangeVal[0]; val <= uRangeVal[1]; val++ {
var valByte [8]byte
binary.LittleEndian.PutUint64(valByte[:], val)
m.Data[valByte] = struct{}{}
}
}
return nil
}
func writeMatchRangesInMap(k *KernelSelectorState, values []string, ty uint32, op uint32) error {
mid, m := k.newValueMap()
for _, v := range values {
err := writeRangeInMap(v, ty, op, &m)
if err != nil {
return err
}
}
// write the map id into the selector
WriteSelectorUint32(&k.data, mid)
return nil
}
func writeListValuesInMap(k *KernelSelectorState, v string, ty uint32, m *ValueMap) error {
if k.listReader == nil {
return fmt.Errorf("failed list values loading is not supported")
}
values, err := k.listReader.Read(v, ty)
if err != nil {
return err
}
for idx := range values {
var val [8]byte
switch ty {
case gt.GenericIntType, gt.GenericS64Type, gt.GenericS32Type, gt.GenericS16Type, gt.GenericS8Type, gt.GenericSyscall64, gt.GenericSizeType:
binary.LittleEndian.PutUint64(val[:], uint64(values[idx]))
case gt.GenericU64Type, gt.GenericU32Type, gt.GenericU16Type, gt.GenericU8Type:
binary.LittleEndian.PutUint64(val[:], uint64(values[idx]))
default:
return fmt.Errorf("Unknown type: %d", ty)
}
m.Data[val] = struct{}{}
}
return nil
}
func writeMatchValuesInMap(k *KernelSelectorState, values []string, ty uint32, op uint32) error {
mid, m := k.newValueMap()
for _, v := range values {
var val [8]byte
if strings.HasPrefix(v, "list:") {
err := writeListValuesInMap(k, v[len("list:"):], ty, &m)
if err != nil {
return err
}
continue
}
// if not list, most likely port range
if strings.Contains(v, ":") {
err := writeRangeInMap(v, ty, op, &m)
if err != nil {
return err
}
continue
}
switch ty {
case gt.GenericIntType, gt.GenericS64Type, gt.GenericS32Type, gt.GenericS16Type, gt.GenericS8Type, gt.GenericSyscall64, gt.GenericSizeType:
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
binary.LittleEndian.PutUint64(val[:], uint64(i))
case gt.GenericU64Type, gt.GenericU32Type, gt.GenericU16Type, gt.GenericU8Type:
i, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
binary.LittleEndian.PutUint64(val[:], uint64(i))
default:
return fmt.Errorf("Unknown type: %d", ty)
}
m.Data[val] = struct{}{}
}
// write the map id into the selector
WriteSelectorUint32(&k.data, mid)
return nil
}
func writeMatchAddrsInMap(k *KernelSelectorState, values []string) error {
m4 := k.createAddr4Map()
m6 := k.createAddr6Map()
for _, v := range values {
addr, maskLen, err := parseAddr(v)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
if len(addr) == 4 {
val := KernelLPMTrie4{prefixLen: maskLen, addr: binary.LittleEndian.Uint32(addr)}
m4[val] = struct{}{}
} else if len(addr) == 16 {
val := KernelLPMTrie6{prefixLen: maskLen}
copy(val.addr[:], addr)
m6[val] = struct{}{}
} else {
return fmt.Errorf("MatchArgs value %s invalid: should be either 4 or 16 bytes long", v)
}
}
// write the map ids into the selector
if len(m4) != 0 {
m4id := k.insertAddr4Map(m4)
WriteSelectorUint32(&k.data, m4id)
} else {
WriteSelectorUint32(&k.data, 0xffffffff)
}
if len(m6) != 0 {
m6id := k.insertAddr6Map(m6)
WriteSelectorUint32(&k.data, m6id)
} else {
WriteSelectorUint32(&k.data, 0xffffffff)
}
return nil
}
func getBase(v string) int {
if strings.HasPrefix(v, "0x") {
return 16
}
if strings.HasPrefix(v, "0") {
return 8
}
return 10
}
func parseAddr(v string) ([]byte, uint32, error) {
ipaddr := net.ParseIP(v)
if ipaddr != nil {
ipaddr4 := ipaddr.To4()
if ipaddr4 != nil {
return ipaddr4, 32, nil
}
ipaddr6 := ipaddr.To16()
if ipaddr6 != nil {
return ipaddr6, 128, nil
}
return nil, 0, fmt.Errorf("IP address is not valid: does not parse as IPv4 or IPv6")
}
vParts := strings.Split(v, "/")
if len(vParts) != 2 {
return nil, 0, fmt.Errorf("IP address is not valid: should be in format ADDR or ADDR/MASKLEN")
}
ipaddr = net.ParseIP(vParts[0])
if ipaddr == nil {
return nil, 0, fmt.Errorf("IP CIDR is not valid: address part does not parse as IPv4 or IPv6")
}
maskLen, err := strconv.ParseUint(vParts[1], 10, 32)
if err != nil {
return nil, 0, fmt.Errorf("IP CIDR is not valid: mask part does not parse")
}
ipaddr4 := ipaddr.To4()
if ipaddr4 != nil {
if maskLen <= 32 {
return ipaddr4, uint32(maskLen), nil
}
return nil, 0, fmt.Errorf("IP CIDR is not valid: IPv4 mask len must be <= 32")
}
ipaddr6 := ipaddr.To16()
if ipaddr6 != nil {
if maskLen <= 128 {
return ipaddr6, uint32(maskLen), nil
}
return nil, 0, fmt.Errorf("IP CIDR is not valid: IPv6 mask len must be <= 128")
}
return nil, 0, fmt.Errorf("IP CIDR is not valid: address part does not parse")
}
func writeMatchValues(k *KernelSelectorState, values []string, ty, op uint32) error {
for _, v := range values {
base := getBase(v)
switch ty {
case gt.GenericIntType, gt.GenericS32Type, gt.GenericSizeType:
i, err := strconv.ParseInt(v, base, 32)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
WriteSelectorInt32(&k.data, int32(i))
case gt.GenericU32Type:
i, err := strconv.ParseUint(v, base, 32)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
WriteSelectorUint32(&k.data, uint32(i))
case gt.GenericS64Type, gt.GenericSyscall64:
i, err := strconv.ParseInt(v, base, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
WriteSelectorInt64(&k.data, int64(i))
case gt.GenericU64Type:
i, err := strconv.ParseUint(v, base, 64)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
WriteSelectorUint64(&k.data, uint64(i))
case gt.GenericSockType, gt.GenericSkbType, gt.GenericNetDev:
return fmt.Errorf("MatchArgs type sock, skb and net_device do not support operator %s", selectorOpStringTable[op])
case gt.GenericCharIovec:
return fmt.Errorf("MatchArgs values %s unsupported", v)
}
}
return nil
}
func writeMatchStrings(k *KernelSelectorState, values []string, ty uint32) error {
maps := k.createStringMaps()
for _, v := range values {
trimNulSuffix := ty == gt.GenericStringType
value, size, err := ArgStringSelectorValue(v, trimNulSuffix)
if err != nil {
return fmt.Errorf("MatchArgs value %s invalid: %w", v, err)
}
numSubMaps := StringMapsNumSubMaps
if !kernels.MinKernelVersion("5.11") {
numSubMaps = StringMapsNumSubMapsSmall
}
for sizeIdx := 0; sizeIdx < numSubMaps; sizeIdx++ {
stringMapSize := StringMapsSizes[sizeIdx]
if sizeIdx == 7 && !kernels.MinKernelVersion("5.11") {
stringMapSize = StringMapSize7a
}
if size == stringMapSize {
maps[sizeIdx][value] = struct{}{}
break
}
}
}
// write the map ids into the selector
mapDetails := k.insertStringMaps(maps)
for _, md := range mapDetails {
WriteSelectorUint32(&k.data, md)
}
return nil
}
func writePrefix(k *KernelSelectorState, values []string, selector string) (uint32, error) {
mid, m := k.newStringPrefixMap()
for _, v := range values {
value, size := ArgSelectorValue(v)
if size > StringPrefixMaxLength {
return 0, fmt.Errorf("%s value %s invalid: string is longer than %d characters", selector, v, StringPrefixMaxLength)
}
val := KernelLPMTrieStringPrefix{prefixLen: size * 8} // prefix is in bits, but size is in bytes
copy(val.data[:], value)
m[val] = struct{}{}
}
return mid, nil
// write the map id into the selector
}
func writePrefixBinaries(k *KernelSelectorState, values []string) (uint32, error) {
return writePrefix(k, values, "MatchBinaries")
}
func writePrefixStrings(k *KernelSelectorState, values []string) error {
mid, err := writePrefix(k, values, "MatchArgs")
if err != nil {
return err
}
WriteSelectorUint32(&k.data, mid)
return nil
}
func writePostfixStrings(k *KernelSelectorState, values []string, ty uint32) error {
mid, m := k.newStringPostfixMap()
for _, v := range values {
var value []byte
var size uint32
if ty == gt.GenericCharBuffer {
value, size = ArgPostfixSelectorValue(v, false)
} else {
value, size = ArgPostfixSelectorValue(v, true)
}
// Due to the constraints of the reverse copy in BPF, we will not be able to match a postfix
// longer than 127 characters, so throw an error if the user specified one.
if size >= StringPostfixMaxLength {
return fmt.Errorf("MatchArgs value %s invalid: string is longer than %d characters", v, StringPostfixMaxLength-1)
}
val := KernelLPMTrieStringPostfix{prefixLen: size * 8} // postfix is in bits, but size is in bytes
// Copy postfix in reverse order, so that it can be used in LPM map
for i := 0; i < len(value); i++ {
val.data[len(value)-i-1] = value[i]
}
m[val] = struct{}{}
}
// write the map id into the selector
WriteSelectorUint32(&k.data, mid)
return nil
}
func checkOp(op uint32) error {
switch op {
case SelectorOpGT, SelectorOpLT:
if !kernels.EnableLargeProgs() {
return fmt.Errorf("GT/LT operators are only supported in kernels >= 5.3")
}
}
return nil
}
func ParseMatchArg(k *KernelSelectorState, arg *v1alpha1.ArgSelector, sig []v1alpha1.KProbeArg) error {
WriteSelectorUint32(&k.data, arg.Index)
op, err := SelectorOp(arg.Operator)
if err != nil {
return fmt.Errorf("matcharg error: %w", err)
}
err = checkOp(op)
if err != nil {
return fmt.Errorf("matcharg error: %w", err)
}
WriteSelectorUint32(&k.data, op)
moff := AdvanceSelectorLength(&k.data)
ty, err := argSelectorType(arg, sig)
if err != nil {
return fmt.Errorf("argSelector error: %w", err)
}
WriteSelectorUint32(&k.data, ty)
switch op {
case SelectorInMap, SelectorNotInMap:
err := writeMatchValuesInMap(k, arg.Values, ty, op)
if err != nil {
return fmt.Errorf("writeMatchRangesInMap error: %w", err)
}
case SelectorOpEQ, SelectorOpNEQ:
switch ty {
case gt.GenericFdType, gt.GenericFileType, gt.GenericPathType, gt.GenericStringType, gt.GenericCharBuffer, gt.GenericLinuxBinprmType, gt.GenericDataLoc, gt.GenericNetDev:
err := writeMatchStrings(k, arg.Values, ty)
if err != nil {
return fmt.Errorf("writeMatchStrings error: %w", err)
}
default:
err = writeMatchValues(k, arg.Values, ty, op)
if err != nil {
return fmt.Errorf("writeMatchValues error: %w", err)
}
}
case SelectorOpPrefix, SelectorOpNotPrefix:
err := writePrefixStrings(k, arg.Values)
if err != nil {
return fmt.Errorf("writePrefixStrings error: %w", err)
}
case SelectorOpPostfix, SelectorOpNotPostfix:
err := writePostfixStrings(k, arg.Values, ty)
if err != nil {
return fmt.Errorf("writePostfixStrings error: %w", err)
}
case SelectorOpSport, SelectorOpDport, SelectorOpNotSport, SelectorOpNotDport, SelectorOpProtocol, SelectorOpFamily, SelectorOpState:
if ty != gt.GenericSockType && ty != gt.GenericSkbType {
return fmt.Errorf("sock/skb operators specified for non-sock/skb type")
}
err := writeMatchRangesInMap(k, arg.Values, gt.GenericU64Type, op) // force type for ports and protocols as ty is sock/skb
if err != nil {
return fmt.Errorf("writeMatchRangesInMap error: %w", err)
}
case SelectorOpSaddr, SelectorOpDaddr, SelectorOpNotSaddr, SelectorOpNotDaddr:
if ty != gt.GenericSockType && ty != gt.GenericSkbType {
return fmt.Errorf("sock/skb operators specified for non-sock/skb type")
}
err := writeMatchAddrsInMap(k, arg.Values)
if err != nil {
return fmt.Errorf("writeMatchAddrsInMap error: %w", err)
}
case SelectorOpSportPriv, SelectorOpDportPriv, SelectorOpNotSportPriv, SelectorOpNotDportPriv:
// These selectors do not take any values, but we do check that they are only used for sock/skb.
if ty != gt.GenericSockType && ty != gt.GenericSkbType {
return fmt.Errorf("sock/skb operators specified for non-sock/skb type")
}
default:
err = writeMatchValues(k, arg.Values, ty, op)
if err != nil {
return fmt.Errorf("writeMatchValues error: %w", err)
}
}
WriteSelectorLength(&k.data, moff)
return nil
}
func ParseMatchArgs(k *KernelSelectorState, args []v1alpha1.ArgSelector, sig []v1alpha1.KProbeArg) error {
max_args := 1
if kernels.EnableLargeProgs() {
max_args = 5 // we support up 5 argument filters under matchArgs with kernels >= 5.3, otherwise 1 argument
}
if len(args) > max_args {
return fmt.Errorf("parseMatchArgs: supports up to %d filters (%d provided)", max_args, len(args))
}
actionOffset := GetCurrentOffset(&k.data)
loff := AdvanceSelectorLength(&k.data)
argOff := make([]uint32, 5)
for i := 0; i < 5; i++ {
argOff[i] = AdvanceSelectorLength(&k.data)
WriteSelectorOffsetUint32(&k.data, argOff[i], 0)
}
for i, a := range args {
WriteSelectorOffsetUint32(&k.data, argOff[i], GetCurrentOffset(&k.data)-actionOffset)
if err := ParseMatchArg(k, &a, sig); err != nil {
return err
}
}
WriteSelectorLength(&k.data, loff)
return nil
}
// User specifies rateLimit in seconds, minutes or hours, but we store it in milliseconds.
func parseRateLimit(str string, scopeStr string) (uint32, uint32, error) {
multiplier := uint32(0)
switch str[len(str)-1] {
case 's', 'S':
multiplier = 1
case 'm', 'M':
multiplier = 60
case 'h', 'H':
multiplier = 60 * 60
}
var rateLimit uint64
var err error
if multiplier != 0 {
if len(str) == 1 {
return 0, 0, fmt.Errorf("parseRateLimit: rateLimit value %s is invalid", str)
}
rateLimit, err = strconv.ParseUint(str[:len(str)-1], 10, 32)
} else {
rateLimit, err = strconv.ParseUint(str, 10, 32)
multiplier = 1
}
if err != nil {
return 0, 0, fmt.Errorf("parseRateLimit: rateLimit value %s is invalid", str)
}
scope := uint32(ActionRateLimitScopeThread)
if scopeStr != "" {
var ok bool
scope, ok = actionRateLimitScope[scopeStr]
if !ok {
return 0, 0, fmt.Errorf("parseRateLimit: rateLimitScope value %s is invalid", scopeStr)
}
}
rateLimit = rateLimit * uint64(multiplier) * 1000
if rateLimit > 0xffffffff {
rateLimit = 0xffffffff
}
return uint32(rateLimit), scope, nil
}
func ParseMatchAction(k *KernelSelectorState, action *v1alpha1.ActionSelector, actionArgTable *idtable.Table) error {
act, ok := actionTypeTable[strings.ToLower(action.Action)]
if !ok {
return fmt.Errorf("parseMatchAction: ActionType %s unknown", action.Action)
}
WriteSelectorUint32(&k.data, act)
rateLimit := uint32(0)
rateLimitScope := uint32(0)
if action.RateLimit != "" {
if act != ActionTypePost {
return fmt.Errorf("rate limiting can only applied to post action (was applied to '%s')", action.Action)
}
var err error
rateLimit, rateLimitScope, err = parseRateLimit(action.RateLimit, action.RateLimitScope)
if err != nil {
return err
}
}
switch act {
case ActionTypeFollowFd, ActionTypeCopyFd:
WriteSelectorUint32(&k.data, action.ArgFd)
WriteSelectorUint32(&k.data, action.ArgName)
case ActionTypeUnfollowFd:
WriteSelectorUint32(&k.data, action.ArgFd)
WriteSelectorUint32(&k.data, action.ArgName)
case ActionTypeOverride:
WriteSelectorInt32(&k.data, action.ArgError)
case ActionTypeGetUrl, ActionTypeDnsLookup:
actionArg := ActionArgEntry{
tableId: idtable.UninitializedEntryID,
}
switch act {
case ActionTypeGetUrl:
actionArg.arg = action.ArgUrl
case ActionTypeDnsLookup:
actionArg.arg = action.ArgFqdn
}
actionArgTable.AddEntry(&actionArg)
WriteSelectorUint32(&k.data, uint32(actionArg.tableId.ID))
case ActionTypeSignal:
WriteSelectorUint32(&k.data, action.ArgSig)
case ActionTypeTrackSock, ActionTypeUntrackSock:
WriteSelectorUint32(&k.data, action.ArgSock)
case ActionTypePost:
WriteSelectorUint32(&k.data, rateLimit)
WriteSelectorUint32(&k.data, rateLimitScope)
kernelStackTrace := uint32(0)
if action.KernelStackTrace {
kernelStackTrace = 1
}
WriteSelectorUint32(&k.data, kernelStackTrace)
userStackTrace := uint32(0)
if action.UserStackTrace {
userStackTrace = 1
}
WriteSelectorUint32(&k.data, userStackTrace)
case ActionTypeNoPost:
// no arguments
case ActionTypeSigKill:
// no arguments
// NB: we should deprecate this action and just use ActionTypeSignal with SIGKILL
case ActionTypeNotifyEnforcer:
WriteSelectorInt32(&k.data, action.ArgError)
WriteSelectorUint32(&k.data, action.ArgSig)
default:
return fmt.Errorf("ParseMatchAction: act %d (%s) is missing a handler", act, actionTypeStringTable[act])
}
return nil
}
func ParseMatchActions(k *KernelSelectorState, actions []v1alpha1.ActionSelector, actionArgTable *idtable.Table) error {
if len(actions) > 3 {
return fmt.Errorf("only %d actions are support for selector (current number of values is %d)", 3, len(actions))
}
loff := AdvanceSelectorLength(&k.data)
for _, a := range actions {
if err := ParseMatchAction(k, &a, actionArgTable); err != nil {
return err
}
}
// No action (size value 4) defaults to post action.
WriteSelectorLength(&k.data, loff)
return nil
}
func namespaceSelectorValue(ns *v1alpha1.NamespaceSelector, nstype string) ([]byte, uint32, error) {
b := make([]byte, len(ns.Values)*4)
if len(ns.Values) > 4 { // 4 should match the number of iterations in selector_match() in pfilter.h
return b, 0, fmt.Errorf("matchNamespace supports up to 4 values per filter (current number of values is %d)", len(ns.Values))
}
for i, v := range ns.Values {
val, err := strconv.ParseUint(v, 10, 32)
if err != nil {
// the only case that we can accept and is not a uint32 is "<host_ns>"
// in this case we should replace that with the approproate value
if v == "host_ns" {
n, err := namespace.GetHostNsInode(nstype)
if err != nil {
return b, 0, fmt.Errorf("matchNamespace reading host '%s' namespace failed: %v", nstype, err)
}
val = uint64(n)