Skip to content

Commit 506dd6c

Browse files
committed
Merged PR 12878106: Small rego fixes
[cherry-picked from 0ca40bb4f130b3508f4a130011463070328d40d0] - rego: Fix missing error reason when mounting a rw device to an existing mount point. This fixes a missing error message introduced in the last round of security fixes. It's not hugely important, but eases debugging if we get policy denials on mounting the scratch, for whatever reason. Also adds test for it. - Remove a no-op from rego Checked with @<Matthew Johnson (AR)> earlier that this basically does nothing and is just something left over. However I will not actually add a remove op for `metadata.started` for now. This PR is targeting the conf-aci branch on ADO because the commit being fixed is not on main yet. This should be backported to main together with the fixes from last month. Signed-off-by: Tingmao Wang <[email protected]>
1 parent 2766a36 commit 506dd6c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pkg/securitypolicy/framework.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ exec_in_container := {"metadata": [updateMatches],
901901

902902
default shutdown_container := {"allowed": false}
903903

904-
shutdown_container := {"started": remove, "metadata": [remove], "allowed": true} {
904+
shutdown_container := {"metadata": [remove], "allowed": true} {
905905
container_started
906906
remove := {
907907
"name": "matches",
@@ -1313,7 +1313,7 @@ errors["deviceHash not found"] {
13131313
}
13141314

13151315
errors["device already mounted at path"] {
1316-
input.rule == "mount_device"
1316+
input.rule in ["mount_device", "rw_mount_device"]
13171317
device_mounted(input.target)
13181318
}
13191319

pkg/securitypolicy/regopolicy_linux_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func Test_Rego_EnforceDeviceMountPolicy_InvalidMountTarget_PathTraversal(t *test
378378
assertDecisionJSONContains(t, err, "mountpoint invalid")
379379
}
380380

381-
func deviceMountUnmountTest(t *testing.T, p *generatedConstraints, policy *regoEnforcer, mountScratchFirst, unmountScratchFirst, testInvalidUnmount bool) bool {
381+
func deviceMountUnmountTest(t *testing.T, p *generatedConstraints, policy *regoEnforcer, mountScratchFirst, unmountScratchFirst, testDenials bool) bool {
382382
container := selectContainerFromContainerList(p.containers, testRand)
383383
containerID := testDataGenerator.uniqueContainerID()
384384
rotarget := testDataGenerator.uniqueLayerMountTarget()
@@ -414,6 +414,18 @@ func deviceMountUnmountTest(t *testing.T, p *generatedConstraints, policy *regoE
414414
}
415415
}
416416

417+
if testDenials {
418+
err = policy.EnforceRWDeviceMountPolicy(p.ctx, rwtarget, true, true, "xfs")
419+
if !assertDecisionJSONContains(t, err, "device already mounted at path") {
420+
return false
421+
}
422+
423+
err = policy.EnforceDeviceMountPolicy(p.ctx, rotarget, container.Layers[0])
424+
if !assertDecisionJSONContains(t, err, "device already mounted at path") {
425+
return false
426+
}
427+
}
428+
417429
unmountScratch := func() bool {
418430
err = policy.EnforceRWDeviceUnmountPolicy(p.ctx, rwtarget)
419431
if err != nil {
@@ -442,7 +454,7 @@ func deviceMountUnmountTest(t *testing.T, p *generatedConstraints, policy *regoE
442454
}
443455
}
444456

445-
if testInvalidUnmount {
457+
if testDenials {
446458
err = policy.EnforceDeviceUnmountPolicy(p.ctx, rotarget)
447459
if !assertDecisionJSONContains(t, err, "no device at path to unmount") {
448460
return false

0 commit comments

Comments
 (0)