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

Run Control Plane components as nonroot user 65532 #8690

Merged
merged 4 commits into from
Nov 8, 2023

Conversation

AleksandarSavchev
Copy link
Contributor

@AleksandarSavchev AleksandarSavchev commented Oct 24, 2023

How to categorize this PR?

/area security quality
/kind enhancement

What this PR does / why we need it:
This PR modifies Control Plane components kube-apiserver, kube-controller-manager and kube-scheduler to run as nonroot user 65532 (id of nonroot user in distroless images ref). The aim of this PR is to adhere to the principles of least privilege and reduce potential attack surface.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Release note:

Control plane components `kube-apiserver`, `kube-controller-manager` and `kube-scheduler` now run as `nonroot` user and group `65532`.

@gardener-prow gardener-prow bot added area/security Security related area/quality Output qualification (tests, checks, scans, automation in general, etc.) related kind/enhancement Enhancement, improvement, extension cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. labels Oct 24, 2023
@gardener-prow gardener-prow bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 24, 2023
@AleksandarSavchev
Copy link
Contributor Author

/cc @dimityrmirchev

Copy link
Member

@dimityrmirchev dimityrmirchev left a comment

Choose a reason for hiding this comment

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

/lgtm

@gardener-prow gardener-prow bot added the lgtm Indicates that a PR is ready to be merged. label Oct 24, 2023
@gardener-prow
Copy link
Contributor

gardener-prow bot commented Oct 24, 2023

LGTM label has been added.

Git tree hash: 9bb9b3bb5f4c786b63b88a5231f60e65fb01e004

@AleksandarSavchev AleksandarSavchev marked this pull request as draft October 25, 2023 08:31
@gardener-prow gardener-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 25, 2023
@AleksandarSavchev
Copy link
Contributor Author

Converted to draft in order to research setting runAsGroup: 65532 as well.

@gardener-prow gardener-prow bot removed the lgtm Indicates that a PR is ready to be merged. label Oct 27, 2023
@gardener-prow gardener-prow bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 27, 2023
@AleksandarSavchev AleksandarSavchev marked this pull request as ready for review October 27, 2023 09:11
@gardener-prow gardener-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 27, 2023
Copy link
Contributor

@ScheererJ ScheererJ left a comment

Choose a reason for hiding this comment

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

Did you check with HA vpn? In the HA-mode of vpn, there are vpn side-car containers added to kube-apiserver. Do they also work with this user change?

@AleksandarSavchev
Copy link
Contributor Author

AleksandarSavchev commented Oct 27, 2023

Did you check with HA vpn? In the HA-mode of vpn, there are vpn side-car containers added to kube-apiserver. Do they also work with this user change?

I have not checked the HA scenarios. I will look into them, if the vpn side-car containers are problematic the easiest fix would be to set their containers to run as root.

@AleksandarSavchev
Copy link
Contributor Author

After validating locally the vpn containers cannot run as nonroot and would error with the following: panic: open /tmp/acquired-ip: permission denied.
I tested adding the nonroot user to the image used in this Dockerfile and got the following error: /network-connection.sh: line 47: /proc/sys/net/ipv4/tcp_keepalive_time: Permission denied ref. The /proc/sys/net/ipv4 files have 0644 permissions and their owners cannot be changed from root.
I have decided to leave the vpn containers running as root.

@AleksandarSavchev
Copy link
Contributor Author

/test pull-gardener-e2e-kind

1 similar comment
@AleksandarSavchev
Copy link
Contributor Author

/test pull-gardener-e2e-kind

@AleksandarSavchev
Copy link
Contributor Author

/test pull-gardener-e2e-kind-migration
/test pull-gardener-e2e-kind-migration-ha-single-zone

Copy link
Member

@dimityrmirchev dimityrmirchev left a comment

Choose a reason for hiding this comment

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

/lgtm

@gardener-prow gardener-prow bot added the lgtm Indicates that a PR is ready to be merged. label Nov 6, 2023
Copy link
Contributor

gardener-prow bot commented Nov 6, 2023

LGTM label has been added.

Git tree hash: b1930fd5bae2bab580140ff89d8782dfc423ec23

Comment on lines +839 to +840
RunAsNonRoot: pointer.Bool(false),
RunAsUser: pointer.Int64(0),
Copy link
Member

Choose a reason for hiding this comment

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

What happens when a Pod has a securityContext set at Pod level and then some container overwrite it? Does the container securityContext is considered only or does kubernetes internally merges the container securityContext with the pod securityContext (where container securityContext values take precedence)?
I am asking because if a merge happens under the hood, then the merged securityContext would be

securityContext:
  runAsNonRoot: false
  runAsUser: 0
  runAsGroup: 65532
  fsGroup: 65532

which I think is not what we want.
The docs write

Security settings that you specify for a Container apply only to the individual Container, and they override settings made at the Pod level when there is overlap.

but I am not 100% sure.

Copy link
Member

Choose a reason for hiding this comment

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

It seems that a merge happens under the hood indeed.

A small isolated setup to prove the merge:

  1. Create the following yaml:
apiVersion: v1
kind: Pod
metadata:
  name: alpine
  namespace: default
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 65532
    runAsGroup: 65532
    fsGroup: 65532
  containers:
  - image: alpine:3.13.2
    command:
      - /bin/sh
      - "-c"
      - "sleep 60m"
    imagePullPolicy: IfNotPresent
    name: alpine
    securityContext:
      runAsNonRoot: false
      runAsUser: 0
  restartPolicy: Always
  1. Verify the merge
% k exec -ti alpine -- sh
/ # id
uid=0(root) gid=65532 groups=1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video),65532

Again, maybe this is not what we want? Maybe it is better to move the Pod level securityContext to the kube-apiserver container?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the Pod's securityContext setting will be used by every container and only the specified settings will be overridden by the container's securityContext. It is important to note that fsGroup can be set only at the Pod level settings so the only question is if we should explicitly set runAsGroup at the Container level.

I do not think that it is required since we already set root as the user which grants full permissions on all files in the container.

Copy link
Contributor

gardener-prow bot commented Nov 7, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dimityrmirchev, ialidzhikov

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gardener-prow gardener-prow bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 7, 2023
@gardener-prow gardener-prow bot merged commit 760571e into gardener:master Nov 8, 2023
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/quality Output qualification (tests, checks, scans, automation in general, etc.) related area/security Security related cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. kind/enhancement Enhancement, improvement, extension lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants