Skip to content

Commit

Permalink
genpolicy: add support for runAsUser
Browse files Browse the repository at this point in the history
Add policy support for SecurityContext and PodSecurityContext
runAsUser.

Also, remove outdated UID rule workaround.

Fixes: kata-containers#8879

Signed-off-by: Dan Mihai <dmihai@microsoft.com>
  • Loading branch information
danmihai1 committed Jan 22, 2024
1 parent 4f4d264 commit f16fbd1
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm1.yaml

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm2.yaml

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/agent/samples/policy/yaml/pod/pod-one-container.yaml

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/tools/genpolicy/rules.rego
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,8 @@ allow_user(p_process, i_process) {
p_user := p_process.User
i_user := i_process.User

# TODO: track down the reason for mcr.microsoft.com/oss/bitnami/redis:6.0.8 being
# executed with uid = 0 despite having "User": "1001" in its container image
# config.
#print("allow_user: input uid =", i_user.UID, "policy uid =", p_user.UID)
#p_user.UID == i_user.UID
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
Expand Down
2 changes: 1 addition & 1 deletion src/tools/genpolicy/src/containerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn get_process(privileged_container: bool, common: &policy::CommonData) -> p
Env: Vec::new(),
Cwd: "/".to_string(),
Capabilities: capabilities,
NoNewPrivileges: true,
NoNewPrivileges: false,
}
}

Expand Down
45 changes: 33 additions & 12 deletions src/tools/genpolicy/src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub struct PodSpec {

#[serde(skip_serializing_if = "Option::is_none")]
topologySpreadConstraints: Option<Vec<TopologySpreadConstraint>>,

#[serde(skip_serializing_if = "Option::is_none")]
securityContext: Option<PodSecurityContext>,
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
Expand Down Expand Up @@ -224,7 +227,7 @@ struct Probe {

#[serde(skip_serializing_if = "Option::is_none")]
tcpSocket: Option<TCPSocketAction>,
// TODO: additional fiels.
// TODO: additional fields.
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
Expand Down Expand Up @@ -252,7 +255,7 @@ struct HTTPGetAction {

#[serde(skip_serializing_if = "Option::is_none")]
httpHeaders: Option<Vec<HTTPHeader>>,
// TODO: additional fiels.
// TODO: additional fields.
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
Expand Down Expand Up @@ -281,6 +284,14 @@ struct SecurityContext {
runAsUser: Option<i64>,
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct PodSecurityContext {
#[serde(skip_serializing_if = "Option::is_none")]
runAsUser: Option<i64>,
// TODO: additional fields.
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Lifecycle {
Expand All @@ -296,7 +307,7 @@ struct Lifecycle {
struct LifecycleHandler {
#[serde(skip_serializing_if = "Option::is_none")]
exec: Option<ExecAction>,
// TODO: additional fiels.
// TODO: additional fields.
}

/// See Reference / Kubernetes API / Workload Resources / Pod.
Expand Down Expand Up @@ -585,15 +596,6 @@ impl Container {
false
}

pub fn allow_privilege_escalation(&self) -> bool {
if let Some(context) = &self.securityContext {
if let Some(allow) = context.allowPrivilegeEscalation {
return allow;
}
}
true
}

pub fn read_only_root_filesystem(&self) -> bool {
if let Some(context) = &self.securityContext {
if let Some(read_only) = context.readOnlyRootFilesystem {
Expand Down Expand Up @@ -849,6 +851,14 @@ impl yaml::K8sResource for Pod {
}
false
}

fn get_process_fields(&self, process: &mut policy::KataProcess) {
if let Some(context) = &self.spec.securityContext {
if let Some(uid) = context.runAsUser {
process.User.UID = uid.try_into().unwrap();
}
}
}
}

impl Container {
Expand Down Expand Up @@ -896,6 +906,17 @@ impl Container {
}
compress_default_capabilities(capabilities, defaults);
}

pub fn get_process_fields(&self, process: &mut policy::KataProcess) {
if let Some(context) = &self.securityContext {
if let Some(uid) = context.runAsUser {
process.User.UID = uid.try_into().unwrap();
}
if let Some(allow) = context.allowPrivilegeEscalation {
process.NoNewPrivileges = !allow
}
}
}
}

fn compress_default_capabilities(
Expand Down
4 changes: 3 additions & 1 deletion src/tools/genpolicy/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,10 @@ impl AgentPolicy {

substitute_env_variables(&mut process.Env);
substitute_args_env_variables(&mut process.Args, &process.Env);

c_settings.get_process_fields(&mut process);
process.NoNewPrivileges = !yaml_container.allow_privilege_escalation();
resource.get_process_fields(&mut process);
yaml_container.get_process_fields(&mut process);

process
}
Expand Down
4 changes: 4 additions & 0 deletions src/tools/genpolicy/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub trait K8sResource {
fn get_annotations(&self) -> &Option<BTreeMap<String, String>>;
fn use_host_network(&self) -> bool;
fn use_sandbox_pidns(&self) -> bool;
fn get_process_fields(&self, _process: &mut policy::KataProcess) {
// Just Pods can have a PodSecurityContext field, so the other
// resources can use this default get_process_fields implementation.
}
}

/// See Reference / Kubernetes API / Common Definitions / LabelSelector.
Expand Down

0 comments on commit f16fbd1

Please sign in to comment.