forked from kata-containers/kata-containers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.rego
1261 lines (936 loc) · 37.8 KB
/
rules.rego
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) 2023 Microsoft Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
package agent_policy
import future.keywords.in
import future.keywords.every
# Default values, returned by OPA when rules cannot be evaluated to true.
default AddARPNeighborsRequest := false
default AddSwapRequest := false
default CloseStdinRequest := false
default CopyFileRequest := false
default CreateContainerRequest := false
default CreateSandboxRequest := false
default DestroySandboxRequest := true
default ExecProcessRequest := false
default GetOOMEventRequest := true
default GuestDetailsRequest := true
default ListInterfacesRequest := false
default ListRoutesRequest := false
default MemHotplugByProbeRequest := false
default OnlineCPUMemRequest := true
default PauseContainerRequest := false
default ReadStreamRequest := false
default RemoveContainerRequest := true
default RemoveStaleVirtiofsShareMountsRequest := true
default ReseedRandomDevRequest := false
default ResumeContainerRequest := false
default SetGuestDateTimeRequest := false
default SetPolicyRequest := false
default SignalProcessRequest := true
default StartContainerRequest := true
default StartTracingRequest := false
default StatsContainerRequest := true
default StopTracingRequest := false
default TtyWinResizeRequest := true
default UpdateContainerRequest := false
default UpdateEphemeralMountsRequest := false
default UpdateInterfaceRequest := true
default UpdateRoutesRequest := true
default WaitProcessRequest := true
default WriteStreamRequest := false
# AllowRequestsFailingPolicy := true configures the Agent to *allow any
# requests causing a policy failure*. This is an unsecure configuration
# but is useful for allowing unsecure pods to start, then connect to
# them and inspect OPA logs for the root cause of a failure.
default AllowRequestsFailingPolicy := false
# Constants
S_NAME_KEY = "io.kubernetes.cri.sandbox-name"
S_NAMESPACE_KEY = "io.kubernetes.cri.sandbox-namespace"
CreateContainerRequest:= {"ops": ops, "allowed": true} {
# Check if the input request should be rejected even before checking the
# policy_data.containers information.
allow_create_container_input
i_oci := input.OCI
i_storages := input.storages
i_devices := input.devices
# array of possible state operations
ops_builder := []
# check sandbox name
sandbox_name = i_oci.Annotations[S_NAME_KEY]
add_sandbox_name_to_state := state_allows("sandbox_name", sandbox_name)
ops_builder1 := concat_op_if_not_null(ops_builder, add_sandbox_name_to_state)
# Check if any element from the policy_data.containers array allows the input request.
some p_container in policy_data.containers
print("======== CreateContainerRequest: trying next policy container")
p_pidns := p_container.sandbox_pidns
i_pidns := input.sandbox_pidns
print("CreateContainerRequest: p_pidns =", p_pidns, "i_pidns =", i_pidns)
p_pidns == i_pidns
p_oci := p_container.OCI
# check namespace
p_namespace := p_oci.Annotations[S_NAMESPACE_KEY]
i_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
print ("CreateContainerRequest: p_namespace =", p_namespace, "i_namespace =", i_namespace)
add_namespace_to_state := allow_namespace(p_namespace, i_namespace)
ops := concat_op_if_not_null(ops_builder1, add_namespace_to_state)
print("CreateContainerRequest: p Version =", p_oci.Version, "i Version =", i_oci.Version)
p_oci.Version == i_oci.Version
print("CreateContainerRequest: p Readonly =", p_oci.Root.Readonly, "i Readonly =", i_oci.Root.Readonly)
p_oci.Root.Readonly == i_oci.Root.Readonly
allow_anno(p_oci, i_oci)
p_storages := p_container.storages
allow_by_anno(p_oci, i_oci, p_storages, i_storages)
p_devices := p_container.devices
allow_devices(p_devices, i_devices)
allow_linux(p_oci, i_oci)
print("CreateContainerRequest: true")
}
allow_create_container_input {
print("allow_create_container_input: input =", input)
count(input.shared_mounts) == 0
is_null(input.string_user)
i_oci := input.OCI
is_null(i_oci.Hooks)
is_null(i_oci.Solaris)
is_null(i_oci.Windows)
i_linux := i_oci.Linux
count(i_linux.GIDMappings) == 0
count(i_linux.MountLabel) == 0
count(i_linux.Resources.Devices) == 0
count(i_linux.RootfsPropagation) == 0
count(i_linux.UIDMappings) == 0
is_null(i_linux.IntelRdt)
is_null(i_linux.Resources.BlockIO)
is_null(i_linux.Resources.Network)
is_null(i_linux.Resources.Pids)
is_null(i_linux.Seccomp)
i_linux.Sysctl == {}
i_process := i_oci.Process
count(i_process.SelinuxLabel) == 0
count(i_process.User.Username) == 0
print("allow_create_container_input: true")
}
allow_namespace(p_namespace, i_namespace) = add_namespace {
p_namespace == i_namespace
add_namespace := null
print("allow_namespace 1: input namespace matches policy data")
}
allow_namespace(p_namespace, i_namespace) = add_namespace {
p_namespace == ""
print("allow_namespace 2: no namespace found on policy data")
add_namespace := state_allows("namespace", i_namespace)
}
# value hasn't been seen before, save it to state
state_allows(key, value) = action {
state := get_state()
not state[key]
print("state_allows: saving to state key =", key, "value =", value)
path := get_state_path(key)
action := {
"op": "add",
"path": path,
"value": value,
}
}
# value matches what's in state, allow it
state_allows(key, value) = action {
state := get_state()
value == state[key]
print("state_allows: found key =", key, "value =", value, " in state")
action := null
}
# helper functions to interact with the state
get_state() = state {
state := data["pstate"]
}
get_state_path(key) = path {
# prepend "/pstate/" to key
path := concat("/", ["/pstate", key])
}
# Helper functions to conditionally concatenate if op is not null
concat_op_if_not_null(ops, op) = result {
op == null
result := ops
}
concat_op_if_not_null(ops, op) = result {
op != null
result := array.concat(ops, [op])
}
# Reject unexpected annotations.
allow_anno(p_oci, i_oci) {
print("allow_anno 1: start")
not i_oci.Annotations
print("allow_anno 1: true")
}
allow_anno(p_oci, i_oci) {
print("allow_anno 2: p Annotations =", p_oci.Annotations)
print("allow_anno 2: i Annotations =", i_oci.Annotations)
i_keys := object.keys(i_oci.Annotations)
print("allow_anno 2: i keys =", i_keys)
every i_key in i_keys {
allow_anno_key(i_key, p_oci)
}
print("allow_anno 2: true")
}
allow_anno_key(i_key, p_oci) {
print("allow_anno_key 1: i key =", i_key)
startswith(i_key, "io.kubernetes.cri.")
print("allow_anno_key 1: true")
}
allow_anno_key(i_key, p_oci) {
print("allow_anno_key 2: i key =", i_key)
some p_key, _ in p_oci.Annotations
p_key == i_key
print("allow_anno_key 2: true")
}
# Get the value of the S_NAME_KEY annotation and
# correlate it with other annotations and process fields.
allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
print("allow_by_anno 1: start")
not p_oci.Annotations[S_NAME_KEY]
i_s_name := i_oci.Annotations[S_NAME_KEY]
print("allow_by_anno 1: i_s_name =", i_s_name)
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name)
print("allow_by_anno 1: true")
}
allow_by_anno(p_oci, i_oci, p_storages, i_storages) {
print("allow_by_anno 2: start")
p_s_name := p_oci.Annotations[S_NAME_KEY]
i_s_name := i_oci.Annotations[S_NAME_KEY]
print("allow_by_anno 2: i_s_name =", i_s_name, "p_s_name =", p_s_name)
allow_sandbox_name(p_s_name, i_s_name)
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, i_s_name)
print("allow_by_anno 2: true")
}
allow_by_sandbox_name(p_oci, i_oci, p_storages, i_storages, s_name) {
print("allow_by_sandbox_name: start")
i_namespace := i_oci.Annotations[S_NAMESPACE_KEY]
allow_by_container_types(p_oci, i_oci, s_name, i_namespace)
allow_by_bundle_or_sandbox_id(p_oci, i_oci, p_storages, i_storages)
allow_process(p_oci, i_oci, s_name)
print("allow_by_sandbox_name: true")
}
allow_sandbox_name(p_s_name, i_s_name) {
print("allow_sandbox_name 1: start")
p_s_name == i_s_name
print("allow_sandbox_name 1: true")
}
allow_sandbox_name(p_s_name, i_s_name) {
print("allow_sandbox_name 2: start")
# TODO: should generated names be handled differently?
contains(p_s_name, "$(generated-name)")
print("allow_sandbox_name 2: true")
}
# Check that the "io.kubernetes.cri.container-type" and
# "io.katacontainers.pkg.oci.container_type" annotations designate the
# expected type - either a "sandbox" or a "container". Then, validate
# other annotations based on the actual "sandbox" or "container" value
# from the input container.
allow_by_container_types(p_oci, i_oci, s_name, s_namespace) {
print("allow_by_container_types: checking io.kubernetes.cri.container-type")
c_type := "io.kubernetes.cri.container-type"
p_cri_type := p_oci.Annotations[c_type]
i_cri_type := i_oci.Annotations[c_type]
print("allow_by_container_types: p_cri_type =", p_cri_type, "i_cri_type =", i_cri_type)
p_cri_type == i_cri_type
allow_by_container_type(i_cri_type, p_oci, i_oci, s_name, s_namespace)
print("allow_by_container_types: true")
}
allow_by_container_type(i_cri_type, p_oci, i_oci, s_name, s_namespace) {
print("allow_by_container_type 1: i_cri_type =", i_cri_type)
i_cri_type == "sandbox"
i_kata_type := i_oci.Annotations["io.katacontainers.pkg.oci.container_type"]
print("allow_by_container_type 1: i_kata_type =", i_kata_type)
i_kata_type == "pod_sandbox"
allow_sandbox_container_name(p_oci, i_oci)
allow_sandbox_net_namespace(p_oci, i_oci)
allow_sandbox_log_directory(p_oci, i_oci, s_name, s_namespace)
print("allow_by_container_type 1: true")
}
allow_by_container_type(i_cri_type, p_oci, i_oci, s_name, s_namespace) {
print("allow_by_container_type 2: i_cri_type =", i_cri_type)
i_cri_type == "container"
i_kata_type := i_oci.Annotations["io.katacontainers.pkg.oci.container_type"]
print("allow_by_container_type 2: i_kata_type =", i_kata_type)
i_kata_type == "pod_container"
allow_container_name(p_oci, i_oci)
allow_net_namespace(p_oci, i_oci)
allow_log_directory(p_oci, i_oci)
print("allow_by_container_type 2: true")
}
# "io.kubernetes.cri.container-name" annotation
allow_sandbox_container_name(p_oci, i_oci) {
print("allow_sandbox_container_name: start")
container_annotation_missing(p_oci, i_oci, "io.kubernetes.cri.container-name")
print("allow_sandbox_container_name: true")
}
allow_container_name(p_oci, i_oci) {
print("allow_container_name: start")
allow_container_annotation(p_oci, i_oci, "io.kubernetes.cri.container-name")
print("allow_container_name: true")
}
container_annotation_missing(p_oci, i_oci, key) {
print("container_annotation_missing:", key)
not p_oci.Annotations[key]
not i_oci.Annotations[key]
print("container_annotation_missing: true")
}
allow_container_annotation(p_oci, i_oci, key) {
print("allow_container_annotation: key =", key)
p_value := p_oci.Annotations[key]
i_value := i_oci.Annotations[key]
print("allow_container_annotation: p_value =", p_value, "i_value =", i_value)
p_value == i_value
print("allow_container_annotation: true")
}
# "nerdctl/network-namespace" annotation
allow_sandbox_net_namespace(p_oci, i_oci) {
print("allow_sandbox_net_namespace: start")
key := "nerdctl/network-namespace"
p_namespace := p_oci.Annotations[key]
i_namespace := i_oci.Annotations[key]
print("allow_sandbox_net_namespace: p_namespace =", p_namespace, "i_namespace =", i_namespace)
regex.match(p_namespace, i_namespace)
print("allow_sandbox_net_namespace: true")
}
allow_net_namespace(p_oci, i_oci) {
print("allow_net_namespace: start")
key := "nerdctl/network-namespace"
not p_oci.Annotations[key]
not i_oci.Annotations[key]
print("allow_net_namespace: true")
}
# "io.kubernetes.cri.sandbox-log-directory" annotation
allow_sandbox_log_directory(p_oci, i_oci, s_name, s_namespace) {
print("allow_sandbox_log_directory: start")
key := "io.kubernetes.cri.sandbox-log-directory"
p_dir := p_oci.Annotations[key]
regex1 := replace(p_dir, "$(sandbox-name)", s_name)
regex2 := replace(regex1, "$(sandbox-namespace)", s_namespace)
print("allow_sandbox_log_directory: regex2 =", regex2)
i_dir := i_oci.Annotations[key]
print("allow_sandbox_log_directory: i_dir =", i_dir)
regex.match(regex2, i_dir)
print("allow_sandbox_log_directory: true")
}
allow_log_directory(p_oci, i_oci) {
print("allow_log_directory: start")
key := "io.kubernetes.cri.sandbox-log-directory"
not p_oci.Annotations[key]
not i_oci.Annotations[key]
print("allow_log_directory: true")
}
allow_devices(p_devices, i_devices) {
print("allow_devices: start")
every i_device in i_devices {
print("allow_devices: i_device =", i_device)
some p_device in p_devices
p_device.container_path == i_device.container_path
}
print("allow_devices: true")
}
allow_linux(p_oci, i_oci) {
p_namespaces := p_oci.Linux.Namespaces
print("allow_linux: p namespaces =", p_namespaces)
i_namespaces := i_oci.Linux.Namespaces
print("allow_linux: i namespaces =", i_namespaces)
p_namespaces == i_namespaces
allow_masked_paths(p_oci, i_oci)
allow_readonly_paths(p_oci, i_oci)
allow_linux_devices(p_oci.Linux.Devices, i_oci.Linux.Devices)
print("allow_linux: true")
}
allow_masked_paths(p_oci, i_oci) {
p_paths := p_oci.Linux.MaskedPaths
print("allow_masked_paths 1: p_paths =", p_paths)
i_paths := i_oci.Linux.MaskedPaths
print("allow_masked_paths 1: i_paths =", i_paths)
allow_masked_paths_array(p_paths, i_paths)
print("allow_masked_paths 1: true")
}
allow_masked_paths(p_oci, i_oci) {
print("allow_masked_paths 2: start")
not p_oci.Linux.MaskedPaths
not i_oci.Linux.MaskedPaths
print("allow_masked_paths 2: true")
}
# All the policy masked paths must be masked in the input data too.
# Input is allowed to have more masked paths than the policy.
allow_masked_paths_array(p_array, i_array) {
every p_elem in p_array {
allow_masked_path(p_elem, i_array)
}
}
allow_masked_path(p_elem, i_array) {
print("allow_masked_path: p_elem =", p_elem)
some i_elem in i_array
p_elem == i_elem
print("allow_masked_path: true")
}
allow_readonly_paths(p_oci, i_oci) {
p_paths := p_oci.Linux.ReadonlyPaths
print("allow_readonly_paths 1: p_paths =", p_paths)
i_paths := i_oci.Linux.ReadonlyPaths
print("allow_readonly_paths 1: i_paths =", i_paths)
allow_readonly_paths_array(p_paths, i_paths, i_oci.Linux.MaskedPaths)
print("allow_readonly_paths 1: true")
}
allow_readonly_paths(p_oci, i_oci) {
print("allow_readonly_paths 2: start")
not p_oci.Linux.ReadonlyPaths
not i_oci.Linux.ReadonlyPaths
print("allow_readonly_paths 2: true")
}
# All the policy readonly paths must be either:
# - Present in the input readonly paths, or
# - Present in the input masked paths.
# Input is allowed to have more readonly paths than the policy.
allow_readonly_paths_array(p_array, i_array, masked_paths) {
every p_elem in p_array {
allow_readonly_path(p_elem, i_array, masked_paths)
}
}
allow_readonly_path(p_elem, i_array, masked_paths) {
print("allow_readonly_path 1: p_elem =", p_elem)
some i_elem in i_array
p_elem == i_elem
print("allow_readonly_path 1: true")
}
allow_readonly_path(p_elem, i_array, masked_paths) {
print("allow_readonly_path 2: p_elem =", p_elem)
some i_masked in masked_paths
p_elem == i_masked
print("allow_readonly_path 2: true")
}
allow_linux_devices(p_devices, i_devices) {
print("allow_linux_devices: start")
every i_device in i_devices {
print("allow_linux_devices: i_device =", i_device)
some p_device in p_devices
i_device.Path == p_device.Path
}
print("allow_linux_devices: true")
}
# Check the consistency of the input "io.katacontainers.pkg.oci.bundle_path"
# and io.kubernetes.cri.sandbox-id" values with other fields.
allow_by_bundle_or_sandbox_id(p_oci, i_oci, p_storages, i_storages) {
print("allow_by_bundle_or_sandbox_id: start")
bundle_path := i_oci.Annotations["io.katacontainers.pkg.oci.bundle_path"]
bundle_id := replace(bundle_path, "/run/containerd/io.containerd.runtime.v2.task/k8s.io/", "")
key := "io.kubernetes.cri.sandbox-id"
p_regex := p_oci.Annotations[key]
sandbox_id := i_oci.Annotations[key]
print("allow_by_bundle_or_sandbox_id: sandbox_id =", sandbox_id, "regex =", p_regex)
regex.match(p_regex, sandbox_id)
allow_root_path(p_oci, i_oci, bundle_id)
every i_mount in input.OCI.Mounts {
allow_mount(p_oci, i_mount, bundle_id, sandbox_id)
}
# TODO: enable allow_storages() after fixing https://github.com/kata-containers/kata-containers/issues/8833
# allow_storages(p_storages, i_storages, bundle_id, sandbox_id)
print("allow_by_bundle_or_sandbox_id: true")
}
allow_process(p_oci, i_oci, s_name) {
p_process := p_oci.Process
i_process := i_oci.Process
print("allow_process: i terminal =", i_process.Terminal, "p terminal =", p_process.Terminal)
p_process.Terminal == i_process.Terminal
print("allow_process: i cwd =", i_process.Cwd, "i cwd =", p_process.Cwd)
p_process.Cwd == i_process.Cwd
print("allow_process: i noNewPrivileges =", i_process.NoNewPrivileges, "p noNewPrivileges =", p_process.NoNewPrivileges)
p_process.NoNewPrivileges == i_process.NoNewPrivileges
allow_caps(p_process.Capabilities, i_process.Capabilities)
allow_user(p_process, i_process)
allow_args(p_process, i_process, s_name)
allow_env(p_process, i_process, s_name)
print("allow_process: true")
}
allow_user(p_process, i_process) {
p_user := p_process.User
i_user := i_process.User
print("allow_user: input uid =", i_user.UID, "policy uid =", p_user.UID)
p_user.UID == i_user.UID
# TODO: track down the reason for registry.k8s.io/pause:3.9 being
# executed with gid = 0 despite having "65535:65535" in its container image
# config.
#print("allow_user: input gid =", i_user.GID, "policy gid =", p_user.GID)
#p_user.GID == i_user.GID
# TODO: compare the additionalGids field too after computing its value
# based on /etc/passwd and /etc/group from the container image.
}
allow_args(p_process, i_process, s_name) {
print("allow_args 1: no args")
not p_process.Args
not i_process.Args
print("allow_args 1: true")
}
allow_args(p_process, i_process, s_name) {
print("allow_args 2: policy args =", p_process.Args)
print("allow_args 2: input args =", i_process.Args)
count(p_process.Args) == count(i_process.Args)
every i, i_arg in i_process.Args {
allow_arg(i, i_arg, p_process, s_name)
}
print("allow_args 2: true")
}
allow_arg(i, i_arg, p_process, s_name) {
p_arg := p_process.Args[i]
print("allow_arg 1: i =", i, "i_arg =", i_arg, "p_arg =", p_arg)
p_arg2 := replace(p_arg, "$$", "$")
p_arg2 == i_arg
print("allow_arg 1: true")
}
allow_arg(i, i_arg, p_process, s_name) {
p_arg := p_process.Args[i]
print("allow_arg 2: i =", i, "i_arg =", i_arg, "p_arg =", p_arg)
# TODO: can $(node-name) be handled better?
contains(p_arg, "$(node-name)")
print("allow_arg 2: true")
}
allow_arg(i, i_arg, p_process, s_name) {
p_arg := p_process.Args[i]
print("allow_arg 3: i =", i, "i_arg =", i_arg, "p_arg =", p_arg)
p_arg2 := replace(p_arg, "$$", "$")
p_arg3 := replace(p_arg2, "$(sandbox-name)", s_name)
print("allow_arg 3: p_arg3 =", p_arg3)
p_arg3 == i_arg
print("allow_arg 3: true")
}
# OCI process.Env field
allow_env(p_process, i_process, s_name) {
print("allow_env: p env =", p_process.Env)
print("allow_env: i env =", i_process.Env)
every i_var in i_process.Env {
print("allow_env: i_var =", i_var)
allow_var(p_process, i_process, i_var, s_name)
}
print("allow_env: true")
}
# Allow input env variables that are present in the policy data too.
allow_var(p_process, i_process, i_var, s_name) {
some p_var in p_process.Env
p_var == i_var
print("allow_var 1: true")
}
# Match input with one of the policy variables, after substituting $(sandbox-name).
allow_var(p_process, i_process, i_var, s_name) {
some p_var in p_process.Env
p_var2 := replace(p_var, "$(sandbox-name)", s_name)
print("allow_var 2: p_var2 =", p_var2)
p_var2 == i_var
print("allow_var 2: true")
}
# Allow input env variables that match with a request_defaults regex.
allow_var(p_process, i_process, i_var, s_name) {
some p_regex1 in policy_data.request_defaults.CreateContainerRequest.allow_env_regex
p_regex2 := replace(p_regex1, "$(ipv4_a)", policy_data.common.ipv4_a)
p_regex3 := replace(p_regex2, "$(ip_p)", policy_data.common.ip_p)
p_regex4 := replace(p_regex3, "$(svc_name)", policy_data.common.svc_name)
p_regex5 := replace(p_regex4, "$(dns_label)", policy_data.common.dns_label)
print("allow_var 3: p_regex5 =", p_regex5)
regex.match(p_regex5, i_var)
print("allow_var 3: true")
}
# Allow fieldRef "fieldPath: status.podIP" values.
allow_var(p_process, i_process, i_var, s_name) {
name_value := split(i_var, "=")
count(name_value) == 2
is_ip(name_value[1])
some p_var in p_process.Env
allow_pod_ip_var(name_value[0], p_var)
print("allow_var 4: true")
}
# Allow common fieldRef variables.
allow_var(p_process, i_process, i_var, s_name) {
name_value := split(i_var, "=")
count(name_value) == 2
some p_var in p_process.Env
p_name_value := split(p_var, "=")
count(p_name_value) == 2
p_name_value[0] == name_value[0]
# TODO: should these be handled in a different way?
always_allowed := ["$(host-name)", "$(node-name)", "$(pod-uid)"]
some allowed in always_allowed
contains(p_name_value[1], allowed)
print("allow_var 5: true")
}
# Allow fieldRef "fieldPath: status.hostIP" values.
allow_var(p_process, i_process, i_var, s_name) {
name_value := split(i_var, "=")
count(name_value) == 2
is_ip(name_value[1])
some p_var in p_process.Env
allow_host_ip_var(name_value[0], p_var)
print("allow_var 6: true")
}
# Allow resourceFieldRef values (e.g., "limits.cpu").
allow_var(p_process, i_process, i_var, s_name) {
name_value := split(i_var, "=")
count(name_value) == 2
some p_var in p_process.Env
p_name_value := split(p_var, "=")
count(p_name_value) == 2
p_name_value[0] == name_value[0]
# TODO: should these be handled in a different way?
always_allowed = ["$(resource-field)", "$(todo-annotation)"]
some allowed in always_allowed
contains(p_name_value[1], allowed)
print("allow_var 7: true")
}
allow_pod_ip_var(var_name, p_var) {
print("allow_pod_ip_var: var_name =", var_name, "p_var =", p_var)
p_name_value := split(p_var, "=")
count(p_name_value) == 2
p_name_value[0] == var_name
p_name_value[1] == "$(pod-ip)"
print("allow_pod_ip_var: true")
}
allow_host_ip_var(var_name, p_var) {
print("allow_host_ip_var: var_name =", var_name, "p_var =", p_var)
p_name_value := split(p_var, "=")
count(p_name_value) == 2
p_name_value[0] == var_name
p_name_value[1] == "$(host-ip)"
print("allow_host_ip_var: true")
}
is_ip(value) {
bytes = split(value, ".")
count(bytes) == 4
is_ip_first_byte(bytes[0])
is_ip_other_byte(bytes[1])
is_ip_other_byte(bytes[2])
is_ip_other_byte(bytes[3])
}
is_ip_first_byte(component) {
number = to_number(component)
number >= 1
number <= 255
}
is_ip_other_byte(component) {
number = to_number(component)
number >= 0
number <= 255
}
# OCI root.Path
allow_root_path(p_oci, i_oci, bundle_id) {
i_path := i_oci.Root.Path
p_path1 := p_oci.Root.Path
print("allow_root_path: i_path =", i_path, "p_path1 =", p_path1)
p_path2 := replace(p_path1, "$(cpath)", policy_data.common.cpath)
print("allow_root_path: p_path2 =", p_path2)
p_path3 := replace(p_path2, "$(bundle-id)", bundle_id)
print("allow_root_path: p_path3 =", p_path3)
p_path3 == i_path
print("allow_root_path: true")
}
# device mounts
allow_mount(p_oci, i_mount, bundle_id, sandbox_id) {
print("allow_mount: i_mount =", i_mount)
some p_mount in p_oci.Mounts
print("allow_mount: p_mount =", p_mount)
check_mount(p_mount, i_mount, bundle_id, sandbox_id)
# TODO: are there any other required policy checks for mounts - e.g.,
# multiple mounts with same source or destination?
print("allow_mount: true")
}
check_mount(p_mount, i_mount, bundle_id, sandbox_id) {
p_mount == i_mount
print("check_mount 1: true")
}
check_mount(p_mount, i_mount, bundle_id, sandbox_id) {
p_mount.destination == i_mount.destination
p_mount.type_ == i_mount.type_
p_mount.options == i_mount.options
mount_source_allows(p_mount, i_mount, bundle_id, sandbox_id)
print("check_mount 2: true")
}
mount_source_allows(p_mount, i_mount, bundle_id, sandbox_id) {
regex1 := p_mount.source
regex2 := replace(regex1, "$(sfprefix)", policy_data.common.sfprefix)
regex3 := replace(regex2, "$(cpath)", policy_data.common.mount_source_cpath)
regex4 := replace(regex3, "$(bundle-id)", bundle_id)
print("mount_source_allows 1: regex4 =", regex4)
regex.match(regex4, i_mount.source)
print("mount_source_allows 1: true")
}
mount_source_allows(p_mount, i_mount, bundle_id, sandbox_id) {
regex1 := p_mount.source
regex2 := replace(regex1, "$(sfprefix)", policy_data.common.sfprefix)
regex3 := replace(regex2, "$(cpath)", policy_data.common.mount_source_cpath)
regex4 := replace(regex3, "$(sandbox-id)", sandbox_id)
print("mount_source_allows 2: regex4 =", regex4)
regex.match(regex4, i_mount.source)
print("mount_source_allows 2: true")
}
######################################################################
# Create container Storages
allow_storages(p_storages, i_storages, bundle_id, sandbox_id) {
p_count := count(p_storages)
i_count := count(i_storages)
print("allow_storages: p_count =", p_count, "i_count =", i_count)
p_count == i_count
# Get the container image layer IDs and verity root hashes, from the "overlayfs" storage.
some overlay_storage in p_storages
overlay_storage.driver == "overlayfs"
print("allow_storages: overlay_storage =", overlay_storage)
count(overlay_storage.options) == 2
layer_ids := split(overlay_storage.options[0], ":")
print("allow_storages: layer_ids =", layer_ids)
root_hashes := split(overlay_storage.options[1], ":")
print("allow_storages: root_hashes =", root_hashes)
every i_storage in i_storages {
allow_storage(p_storages, i_storage, bundle_id, sandbox_id, layer_ids, root_hashes)
}
print("allow_storages: true")
}
allow_storage(p_storages, i_storage, bundle_id, sandbox_id, layer_ids, root_hashes) {
some p_storage in p_storages
print("allow_storage: p_storage =", p_storage)
print("allow_storage: i_storage =", i_storage)
p_storage.driver == i_storage.driver
p_storage.driver_options == i_storage.driver_options
p_storage.fs_group == i_storage.fs_group
allow_storage_options(p_storage, i_storage, layer_ids, root_hashes)
allow_mount_point(p_storage, i_storage, bundle_id, sandbox_id, layer_ids)
# TODO: validate the source field too.
print("allow_storage: true")
}
allow_storage_options(p_storage, i_storage, layer_ids, root_hashes) {
print("allow_storage_options 1: start")
p_storage.driver != "blk"
p_storage.driver != "overlayfs"
p_storage.options == i_storage.options
print("allow_storage_options 1: true")
}
allow_storage_options(p_storage, i_storage, layer_ids, root_hashes) {
print("allow_storage_options 2: start")
p_storage.driver == "overlayfs"
count(p_storage.options) == 2
policy_ids := split(p_storage.options[0], ":")
print("allow_storage_options 2: policy_ids =", policy_ids)
policy_ids == layer_ids
policy_hashes := split(p_storage.options[1], ":")
print("allow_storage_options 2: policy_hashes =", policy_hashes)
p_count := count(policy_ids)
print("allow_storage_options 2: p_count =", p_count)
p_count >= 1
p_count == count(policy_hashes)
i_count := count(i_storage.options)
print("allow_storage_options 2: i_count =", i_count)
i_count == p_count + 3
print("allow_storage_options 2: i_storage.options[0] =", i_storage.options[0])
i_storage.options[0] == "io.katacontainers.fs-opt.layer-src-prefix=/var/lib/containerd/io.containerd.snapshotter.v1.tardev/layers"
print("allow_storage_options 2: i_storage.options[i_count - 2] =", i_storage.options[i_count - 2])
i_storage.options[i_count - 2] == "io.katacontainers.fs-opt.overlay-rw"
lowerdir := concat("=", ["lowerdir", p_storage.options[0]])
print("allow_storage_options 2: lowerdir =", lowerdir)
i_storage.options[i_count - 1] == lowerdir
print("allow_storage_options 2: i_storage.options[i_count - 1] =", i_storage.options[i_count - 1])
every i, policy_id in policy_ids {
allow_overlay_layer(policy_id, policy_hashes[i], i_storage.options[i + 1])
}
print("allow_storage_options 2: true")
}
allow_storage_options(p_storage, i_storage, layer_ids, root_hashes) {
print("allow_storage_options 3: start")
p_storage.driver == "blk"
count(p_storage.options) == 1
startswith(p_storage.options[0], "$(hash")
hash_suffix := trim_left(p_storage.options[0], "$(hash")
endswith(hash_suffix, ")")
hash_index := trim_right(hash_suffix, ")")
i := to_number(hash_index)
print("allow_storage_options 3: i =", i)
hash_option := concat("=", ["io.katacontainers.fs-opt.root-hash", root_hashes[i]])
print("allow_storage_options 3: hash_option =", hash_option)
count(i_storage.options) == 4