Skip to content

Commit

Permalink
Merge pull request #153 from microsoft/danmihai1/run-as-user
Browse files Browse the repository at this point in the history
genpolicy: add support for runAsUser
  • Loading branch information
danmihai1 authored Jan 24, 2024
2 parents 4f4d264 + 3e01311 commit 5799fdf
Show file tree
Hide file tree
Showing 66 changed files with 127 additions and 82 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.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/kubernetes/fixtures/job.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-exec.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-lifecycle.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.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-same-containers.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-spark.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-ubuntu.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/replica-set/replica2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod1.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod3.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod4.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod5.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod6.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod7.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod10.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod11.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod12.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod13.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod8.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod9.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/dns-test.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/many-layers.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 5799fdf

Please sign in to comment.