Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policy: improve args and env variables validation #308

Draft
wants to merge 3 commits into
base: msft-main
Choose a base branch
from

Conversation

Redent0r
Copy link

@Redent0r Redent0r commented Feb 11, 2025

Merge Checklist
  • Followed patch format from upstream recommendation: https://github.com/kata-containers/community/blob/main/CONTRIBUTING.md#patch-format
    • Included a single commit in a given PR - at least unless there are related commits and each makes sense as a change on its own.
  • Aware about the PR to be merged using "create a merge commit" rather than "squash and merge" (or similar)
  • The upstream/missing label (or upstream/not-needed) has been set on the PR.
Summary

policy: improve args and env variables validation. See commits for details

Test Methodology

Passing all fork samples

@Redent0r Redent0r added the upstream/missing PRs that are yet to be upstreamed label Feb 11, 2025
@Redent0r Redent0r force-pushed the saulparedes/improve_arg_validation branch 2 times, most recently from 1ab8fa6 to 05b6c7b Compare February 19, 2025 21:43
@@ -219,6 +219,8 @@
"dns_label": "[a-zA-Z0-9_\\.\\-]+",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to refine this regex. Per https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names , a dns label name does not allows upper case, nor ., and we could limit it to 63 characters

@@ -55,12 +55,125 @@ default AllowRequestsFailingPolicy := false
S_NAME_KEY = "io.kubernetes.cri.sandbox-name"
S_NAMESPACE_KEY = "io.kubernetes.cri.sandbox-namespace"
BUNDLE_ID = "[a-z0-9]{64}"
# from https://github.com/kubernetes/kubernetes/blob/8294abc599696e0d1b5aa734afa7ae1e4f5059a0/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go#L177
SUBDOMAIN_NAME = "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
ALWAYS_ALLOWED = ["$(resource-field)", "$(todo-annotation)"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: double check we are not allowing the execution of args that include these vars that are always allowed. Before, these placeholders where replaced in the policy and there wasn't a special rule to allow these for args, like there was for node-name.

allow_env_map_entry(key, i_val, p_env_map) {
p_val := p_env_map[key]
p_val == "$(node-name)"
regex.match(policy_data.common.dns_subdomain , i_val)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node-name most match a dns subdomain format

allow_env_map_entry(key, i_val, p_env_map) {
p_val := p_env_map[key]
p_val == "$(host-name)"
regex.match(policy_data.common.dns_label , i_val)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

host-name most match a dns label format

allow_env_map_entry(key, i_val, p_env_map) {
p_val := p_env_map[key]
p_val == "$(pod-uid)"
regex.match(policy_data.common.pod_uid , i_val)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pod-uid most match a UUID format

@Redent0r Redent0r force-pushed the saulparedes/improve_arg_validation branch 3 times, most recently from f486a3f to 45ce34f Compare February 20, 2025 01:06
PolicyCreateContainerRequest will be a wrapper to CreateContainerRequest and will allow
the agent to transform the input such that is easier to validate in the policy.

Signed-off-by: Saul Paredes <[email protected]>
- Add environment variable map to the engine on the agent
- Add environment variable map to the policy on genpolicy
- Validate this environemnt variable map on the rules
- Restrain some environment variable values that we expect to look like a subdomain and use a regex to validate them

Signed-off-by: Saul Paredes <[email protected]>
@Redent0r Redent0r force-pushed the saulparedes/improve_arg_validation branch from 45ce34f to 22d11d2 Compare February 20, 2025 01:43
Add a new rule that validates args by substituting env variables received from the input.

This new rule ensures that the args received from the input are the same as the args received from the policy.

Signed-off-by: Saul Paredes <[email protected]>
@Redent0r Redent0r force-pushed the saulparedes/improve_arg_validation branch from 22d11d2 to d3303fd Compare February 21, 2025 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream/missing PRs that are yet to be upstreamed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants