genpolicy: harden CopyFileRequest checks

CopyFile is invoked by the host's FileSystemShare.ShareFile function,
which puts all files into directories with a common pattern. Copying
files anywhere else is dangerous and must be prevented. Thus, we check
that the target path prefix matches the expected directory pattern of
ShareFile, and that this directory is not escaped by .. traversal.

Signed-off-by: Markus Rudy <mr@edgeless.systems>
This commit is contained in:
Markus Rudy
2024-07-19 16:48:35 +02:00
parent 775f6bdc5c
commit aee23409da
3 changed files with 63 additions and 3 deletions

View File

@@ -296,7 +296,7 @@
] ]
}, },
"CopyFileRequest": [ "CopyFileRequest": [
"^$(cpath)/" "$(sfprefix)"
], ],
"ExecProcessRequest": { "ExecProcessRequest": {
"allowed_commands": [], "allowed_commands": [],

View File

@@ -1092,12 +1092,23 @@ match_caps(p_caps, i_caps) {
} }
###################################################################### ######################################################################
check_directory_traversal(i_path) {
not regex.match("(^|/)..($|/)", i_path)
}
CopyFileRequest { CopyFileRequest {
print("CopyFileRequest: input.path =", input.path) print("CopyFileRequest: input.path =", input.path)
check_directory_traversal(input.path)
some regex1 in policy_data.request_defaults.CopyFileRequest some regex1 in policy_data.request_defaults.CopyFileRequest
regex2 := replace(regex1, "$(cpath)", policy_data.common.cpath) regex2 := replace(regex1, "$(sfprefix)", policy_data.common.sfprefix)
regex.match(regex2, input.path) regex3 := replace(regex2, "$(cpath)", policy_data.common.mount_source_cpath)
regex4 := replace(regex3, "$(bundle-id)", "[a-z0-9]{64}")
print("CopyFileRequest: regex4 =", regex4)
regex.match(regex4, input.path)
print("CopyFileRequest: true") print("CopyFileRequest: true")
} }

View File

@@ -6,11 +6,60 @@
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-resolv.conf" "path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-resolv.conf"
} }
}, },
{
"description": "a dirname can have trailing dots",
"allowed": true,
"request": {
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-foo../bar"
}
},
{ {
"description": "attempt to copy outside of container root", "description": "attempt to copy outside of container root",
"allowed": false, "allowed": false,
"request": { "request": {
"path": "/etc/ssl/cert.pem" "path": "/etc/ssl/cert.pem"
} }
},
{
"description": "attempt to write into container root",
"allowed": false,
"request": {
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc/rootfs/bin/sh"
}
},
{
"description": "attempt to write into container root - guest pull",
"allowed": false,
"request": {
"path": "/run/kata-containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc/rootfs/bin/sh"
}
},
{
"description": "attempted directory traversal",
"allowed": false,
"request": {
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-foo/../../../../../etc/ssl/cert.pem"
}
},
{
"description": "attempted directory traversal - parent directory",
"allowed": false,
"request": {
"path": "/run/kata-containers/shared/containers/81e5f43bc8599c5661e66f959ac28df5bfb30da23c5d583f2dcc6f9e0c5186dc-ce23cfeb91e75aaa-foo/.."
}
},
{
"description": "relative path",
"allowed": false,
"request": {
"path": "etc/ssl/cert.pem"
}
},
{
"description": "relative path - parent directory",
"allowed": false,
"request": {
"path": ".."
}
} }
] ]