Skip to content

Latest commit

 

History

History
253 lines (215 loc) · 15.3 KB

pod-security.md

File metadata and controls

253 lines (215 loc) · 15.3 KB

Pod Security and file ownership

Treebind runs under the restricted Pod Security level. This page says what its two pods set, and what a volume needs when a user other than the agent has written to it.

What the pods set

The operator's Deployment and the sync pod that the operator creates for each Treebind use the same settings, and both are admitted in a namespace that enforces pod-security.kubernetes.io/enforce=restricted:

Setting Value
runAsNonRoot true
seccompProfile RuntimeDefault
allowPrivilegeEscalation false
capabilities drop ALL
readOnlyRootFilesystem true

Neither pod sets runAsUser, runAsGroup or fsGroup, so each container runs as the user its image names, unless the platform gives the pod another. The operator image is built on distroless nonroot, which is UID 65532 and GID 65532. The agent image is built on Chainguard's git image, which runs as the user git, also UID 65532 and GID 65532. It carries Git and OpenSSH. What is in the image and how it is kept current is in releasing.

Any user ID

On a platform that gives the pod a user ID of its own, such as the user ID from the range of the namespace with group 0 that OpenShift's restricted-v2 assigns, ssh does not run in the sync pod: it refuses a user that has no passwd entry (No user exists for uid ...), and the image has one only for 65532. Synced then says that the repository cannot be asked for its branches. Tell the operator to give the sync pods a passwd file for their user:

  • with the chart, set agent.arbitraryUserID=true;
  • with install.yaml, set the variable AGENT_ARBITRARY_USER_ID of the operator's Deployment to "true", in the file or afterwards: kubectl -n treebind-system set env deployment/treebind-controller-manager AGENT_ARBITRARY_USER_ID=true.

Each sync pod then has an init container, passwd, of the agent image. It runs as the same user, before the agent, and writes a copy of the image's /etc/passwd, with an entry for its user ID when the copy has none, into an emptyDir. The sync container sees that file at /etc/passwd, read-only. Both containers start in /. The reasons are in ADR 0042.

What to know before you turn it on:

  • Turning it on, or off again, changes the pod template of every Treebind, so every sync pod is replaced once (upgrading the operator).
  • With it on, the sync pods have an emptyDir, so kubectl drain refuses them unless you pass --delete-emptydir-data. Nothing of value is lost: the file is written again when the pod starts. The pods carry the annotation cluster-autoscaler.kubernetes.io/safe-to-evict-local-volumes: passwd, so the cluster autoscaler may still take their node down.
  • It needs an agent image of the release that brought it or newer. An older agent ignores the arguments of the init container and starts as the agent, so the init container never finishes and the sync pod never starts. That matters when you pin agent.image or AGENT_IMAGE to a digest or a mirror, and when you upgrade with helm upgrade --reuse-values, which keeps the agent image of the release before.

The agent writes only to the claim. Git and ssh read no home directory and write nothing to /tmp, so the sync container has no volume for either, and its root filesystem stays read-only.

When the user ID changes

The files the agent makes belong to the user ID it runs as. Its state directory, .treebind at the root of the claim, is private to that user (mode 0700). If the pod later runs as another user ID, after a move to a platform that assigns one or a change of the user, the agent refuses the directory with GitDirUnsafe, and the message names both users. Give the directory to the new user, from a pod that mounts the whole claim and runs as root, because only root may give a file to another user (on OpenShift that takes a security context constraint such as anyuid):

chown -R <new uid>:<new gid> "$MOUNT/.treebind"

The agent then carries on with the checkout as it was. Removing .treebind instead also clears the check, but it loses the commits that were not pushed, and the agent makes the checkout as on a first start: it takes the files of the synced directory when they match the branch and refuses them with DirectoryNotEmpty when they do not (the Git directory). The files that it synced are group-writable, as below, so they stay writable for a user in the same group.

spec.agent.runAsUser changes the user ID of the sync pod, and the same applies: see the overrides of spec.agent.

The overrides of spec.agent

A Treebind can change some of its sync pod with spec.agent. What the operator sets itself stays: runAsNonRoot, the seccomp profile, the security context of the container, the labels that select the pod and the annotations the operator puts on it. ADR 0044 has the reasons. Each field that is set changes the pod template, so the sync pod is replaced once when you set or change one.

Field What it does
resources The requests and limits of the sync container. Each one takes the place of the default of its resource (requests of 10m CPU and 32Mi memory, a limit of 128Mi memory). A default limit below a request that you set is raised to it, and a default request above a limit that you set is lowered to it. A request above a limit, both set, is refused by the API server. claims is not applied
nodeSelector, tolerations, priorityClassName, imagePullSecrets Those of the pod, as set
labels, annotations Added to the pod. A key that the operator sets keeps the operator's value. The annotations of the deprecated security fields, container.apparmor.security.beta.kubernetes.io/*, seccomp.security.alpha.kubernetes.io/* and container.seccomp.security.alpha.kubernetes.io/*, are left out, so that they cannot loosen the profile of the pod
runAsUser, runAsGroup The user and the group of the pod, the init container's too. 0 is refused, and a user without a group gets group 65532 (below)
fsGroup Not applied yet, and reported in SpecSupported (below)
  • A user other than 65532, the image's, gives that sync pod the passwd init container of any user ID, whatever the operator's setting, because ssh refuses a user without a passwd entry. Its emptyDir means that kubectl drain of the node needs --delete-emptydir-data. The init container needs an agent image of this release or newer, as with the setting: mind a pinned agent.image or AGENT_IMAGE and helm upgrade --reuse-values.
  • A user without a group runs in group 65532, the image's, and not in group 0, which is what the runtime gives a pod that sets only runAsUser. The files of the claim are shared with that group (below). Set runAsGroup for another.
  • A group without a user runs as user 65532, the image's, because a pod that names a group and no user does not start. With any user ID turned on, the user is left to the platform, which has to give one.
  • runAsUser: 0 gets no sync pod: the kubelet would refuse to start a pod that is non-root as root. Ready is False with UnsupportedAgentUser, the operator creates or changes no sync object, and a sync pod that the Treebind had runs on as it was until you set another user.
  • A change of the user on a claim that the agent has used is refused by the agent with GitDirUnsafe, because its state directory belongs to the user before: see when the user ID changes.
  • fsGroup is not applied yet (#96). For a volume that supports it, the kubelet gives every file of the claim the group, with group read and write, also the agent's own .treebind, which the agent then takes back at its start: see what the volume needs for the same walk from the application's pod.
  • What the API server refuses, such as a label value that a label cannot have, makes Ready False with ReconcileFailed and the API server's words. A pod that is refused only when it is made, for a priority class that does not exist or a quota, shows as FailedCreate in the events of the StatefulSet (what you see).

The SSH key

The Secret with the SSH key is mounted at /etc/treebind/ssh with mode 0444. The kubelet makes Secret files owned by root, and the agent runs as a non-root user without an fsGroup, so a stricter mode such as 0400 would leave the agent unable to read its key. OpenSSH accepts a key that root owns even when others can read it. The file is on a memory-backed volume that only the agent container mounts. The reasons and the alternatives are in ADR 0007.

What the volume needs

The agent writes to the claim as UID 65532 and GID 65532, or as the user and group that the platform gives the pod, with umask 0002, so the files and directories it creates are group-writable (0664 and 0775). On a platform that assigns user IDs, read the user ID of the pod where this section says UID 65532, and see below for the group.

For a claim that your application wrote to first:

  • The agent must be able to write to the mount point. A new volume is often owned by root and not group-writable, so the agent gets "permission denied" on its first write. Make the root of the volume owned by 65532, or writable by a group the agent is in.
  • Files your application wrote must be writable by UID 65532 or GID 65532, or the agent cannot update them when the remote changes.

For your application's pod:

  • Run it with fsGroup: 65532. Kubernetes then makes the volume group-owned by that GID, so the application can change what the agent wrote. On drivers that honour it, the volume's directories are also setgid, so new files keep the group. Not every storage driver supports this, NFS for one: on those the setting has no effect, so set the owner, the group and the mode on the storage side instead.

  • Set fsGroupChangePolicy: OnRootMismatch beside it. Where Kubernetes applies fsGroup (a local PersistentVolume and most block volumes), it otherwise walks the whole claim at every start of the pod and gives every file the group with group read and write, the agent's .treebind too, which then has mode 2770. With OnRootMismatch Kubernetes skips the walk once the root of the volume has the group and its mode, but the first mount of a root that does not match still walks once.

  • After a walk, the agent takes .treebind back at its next start (#313, ADR 0045): it closes it to the group again, as chmod -R g-rwxs would, without following a link, copies the files outside the object store into new ones, and then checks what is in the Git directory as always. The message of the check GitDir begins with the agent took back .treebind, which a walk for an fsGroup had opened to group .... It does that only when the directories have exactly the mode that a walk gives them. While .treebind was open, a member of the group could change what is in it, and the content check does not catch everything: it could have moved the branch to a commit of its own, which the agent would push. Keep the walk rare with OnRootMismatch, and give the Treebind a subPath, so that the application does not mount .treebind at all (where the agent keeps its Git directory).

  • When the take-back refuses, the check GitDir fails with GitDirUnsafe, and the message names the file and what a walk does not make of it: a link, a file of another user, a file with a second name (a hard link) outside the object store or outside .treebind. Someone put it there while the directory was open. For a hard link the message names the other names in .treebind and gives the find -inum that lists the ones outside it: remove the name that was planted and keep the one that Git or the agent made, which for the config, HEAD, a ref or an object is its name in .treebind/git. Anything else that it names, remove. For an entry in .treebind/git you can instead empty .treebind/git for the agent to make it again, which loses the commits that were not pushed; for one in .treebind/lock, remove it while no sync pod of the claim runs. A .treebind that is open to the group in another way, such as 0770 or 2775, is refused as before. To close one by hand, run, as UID 65532 (or the user the agent runs as), in a pod that mounts the claim:

    chmod -R g-rwxs "$MOUNT/.treebind"

    The check GitDir then passes, and the agent goes on by itself.

  • A walk while the agent runs is not noticed until the agent starts again.

  • Give the application a umask of 0002 as well, or have it create files with group write. With the default 0022 the agent cannot change what the application creates, even with the same group.

On a platform that assigns the user ID with group 0, such as OpenShift's restricted-v2, do not set fsGroup: 65532, and do not set fsGroup: 0 either: restricted-v2 refuses both. It gives a pod that sets no fsGroup the first group of the range of the namespace, the annotation openshift.io/sa.scc.supplemental-groups, so leave fsGroup out of your application's pod, and the sync pod sets none. Both pods of the namespace then share that group on the volume. If you set one, take a value of that range.

See the Kubernetes documentation on configuring volume permissions for how fsGroup and fsGroupChangePolicy behave on your storage driver.

What the agent checks of the volume

When the sync pod starts, the agent checks that it can write to the volume as the user above: it creates, writes to and removes a hidden file at the mount point of the claim (the check Writable). A claim that is mounted read-only fails it with ReadOnlyVolume, a directory that the agent's user cannot write to with NotWritable, and any other failure with WriteFailed. The message of NotWritable names the user, and the owner and mode of the directory. The agent stays up and checks again, with a longer wait each time up to a minute, so it carries on once you put the volume right. The operator shows a failed check in the condition Synced (the conditions, the checks of the report).

Git's refusal of a directory that another user owns does not stop the agent: every Git command of the agent sets safe.directory to the work tree of the checkout, and to nothing else (ADR 0013).

What is still to come

  • spec.agent.fsGroup (FR-28, #96). The agent now takes back a state directory that a walk opened (#313), so the operator can apply it.