Skip to content

Add support for no_new_privs via AllowPrivilegeEscalation#47019

Merged
k8s-github-robot merged 6 commits intokubernetes:masterfrom
jessfraz:allowPrivilegeEscalation
Jul 31, 2017
Merged

Add support for no_new_privs via AllowPrivilegeEscalation#47019
k8s-github-robot merged 6 commits intokubernetes:masterfrom
jessfraz:allowPrivilegeEscalation

Conversation

@jessfraz
Copy link
Copy Markdown
Contributor

@jessfraz jessfraz commented Jun 6, 2017

What this PR does / why we need it:
Implements kubernetes/community#639
Fixes #38417

Adds AllowPrivilegeEscalation and DefaultAllowPrivilegeEscalation to PodSecurityPolicy.
Adds AllowPrivilegeEscalation to container SecurityContext.

Adds the proposed behavior to kuberuntime, dockershim, and rkt. Adds a bunch of unit tests to ensure the desired default behavior and that when DefaultAllowPrivilegeEscalation is explicitly set.

Tests pass locally with docker and rkt runtimes. There are also a few integration tests with a setuid binary for sanity.

Release note:

Adds AllowPrivilegeEscalation to control whether a process can gain more privileges than it's parent process

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 6, 2017
@k8s-github-robot k8s-github-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jun 6, 2017
@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch from dbcf72c to 7813f76 Compare June 6, 2017 04:10
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 6, 2017
@jessfraz jessfraz added this to the v1.8 milestone Jun 6, 2017
@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch from f948d60 to d4cac92 Compare June 6, 2017 10:33
@dims
Copy link
Copy Markdown
Member

dims commented Jun 6, 2017

@jessfraz : sorry for a dumb question. Is this feature meant to only be active/available when the kubelet --allow-privileged is true?

@jessfraz
Copy link
Copy Markdown
Contributor Author

jessfraz commented Jun 6, 2017

@dims there is a chart in the proposal that might help, no_new_privs will be added when allowPrivilegeEscalation is false, by default this will be when a container is not privileged, adding CAP{_SYS_ADMIN, or when the uid!=0

@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch from d4cac92 to 4e223b6 Compare June 6, 2017 11:49
@dims
Copy link
Copy Markdown
Member

dims commented Jun 6, 2017

@jessfraz got it. thanks!

@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch 4 times, most recently from 12d5ddc to 8e1a8fa Compare June 6, 2017 18:41
@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 6, 2017
@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch from 8e1a8fa to 4b159e1 Compare June 6, 2017 19:03
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 6, 2017
@jessfraz jessfraz force-pushed the allowPrivilegeEscalation branch from 4b159e1 to a8c7682 Compare June 6, 2017 19:19
"gcr.io/google_containers/nginx-slim:0.7",
"gcr.io/google_containers/serve_hostname:v1.4",
"gcr.io/google_containers/netexec:1.7",
"r.j3ss.co/no_new_privs:latest",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will switch this to google_containers before merge don't worry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed the image to be on google container registry

@jessfraz jessfraz requested a review from pweil- June 6, 2017 19:40
@pweil-
Copy link
Copy Markdown

pweil- commented Jun 6, 2017

@php-coder

@spxtr
Copy link
Copy Markdown
Contributor

spxtr commented Jul 24, 2017

/lint

Copy link
Copy Markdown
Contributor

@k8s-ci-robot k8s-ci-robot left a comment

Choose a reason for hiding this comment

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

@spxtr: 7 warnings.

Details

In response to this:

/lint

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.


return nil
}
func Convert_api_SecurityContext_To_v1_SecurityContext(in *api.SecurityContext, out *v1.SecurityContext, s conversion.Scope) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint naming: don't use underscores in Go names; func Convert_api_SecurityContext_To_v1_SecurityContext should be ConvertAPISecurityContextToV1SecurityContext. More info.

return nil
}

func Convert_extensions_PodSecurityPolicySpec_To_v1beta1_PodSecurityPolicySpec(in *extensions.PodSecurityPolicySpec, out *extensionsv1beta1.PodSecurityPolicySpec, s conversion.Scope) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint naming: don't use underscores in Go names; func Convert_extensions_PodSecurityPolicySpec_To_v1beta1_PodSecurityPolicySpec should be ConvertExtensionsPodSecurityPolicySpecToV1beta1PodSecurityPolicySpec. More info.

yyb14 = yyj14 > l
var yyj16 int
var yyb16 bool
var yyhl16 bool = l >= 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint type-inference: should omit type bool from declaration of var yyhl16; it will be inferred from the right-hand side.

}
}

func NewNoNewPrivsAdmitHandler(runtime kubecontainer.Runtime) PodAdmitHandler {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported function NewNoNewPrivsAdmitHandler should have comment or be unexported. More info.


func TestAddNoNewPrivileges(t *testing.T) {
var nonRoot int64 = 1000
var root int64 = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint zero-value: should drop = 0 from declaration of var root; it is the zero value.

return ""
}

func (m *LinuxContainerSecurityContext) GetNoNewPrivs() bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint comments: exported method LinuxContainerSecurityContext.GetNoNewPrivs should have comment or be unexported. More info.

yyb28 = yyj28 > l
var yyj32 int
var yyb32 bool
var yyhl32 bool = l >= 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Golint type-inference: should omit type bool from declaration of var yyhl32; it will be inferred from the right-hand side.

@spxtr
Copy link
Copy Markdown
Contributor

spxtr commented Jul 24, 2017

Lint comments on generated code aren't too useful 😞

@jessfraz
Copy link
Copy Markdown
Contributor Author

this is all updated and green :)

@tallclair
Copy link
Copy Markdown
Member

/lgtm

@jessfraz
Copy link
Copy Markdown
Contributor Author

jessfraz commented Jul 29, 2017 via email

@jessfraz
Copy link
Copy Markdown
Contributor Author

ping @smarterclayton for approval 😇

@smarterclayton
Copy link
Copy Markdown
Contributor

API approved as per the proposal and discussion on the thread.

/approve

@k8s-github-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jessfraz, smarterclayton, tallclair

Associated issue: 639

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

Details Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@jessfraz
Copy link
Copy Markdown
Contributor Author

/test pull-kubernetes-bazel

@k8s-github-robot
Copy link
Copy Markdown

Automatic merge from submit-queue (batch tested with PRs 49651, 49707, 49662, 47019, 49747)

@@ -0,0 +1,22 @@
// Copyright 2017 The Kubernetes Authors.
Copy link
Copy Markdown
Member

@mkumatag mkumatag Aug 1, 2017

Choose a reason for hiding this comment

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

Is there any specif reason why we are using C code? I see even os library in go has Geteuid() which can be used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does it matter?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes. For multi arch test images we are planning to have all images under kubernetes/tests/images directory for supported architectures like amd64, arm, arm64, ppc64le. So its always very easy to cross build the go code over a C code.

@grodrigues3
Copy link
Copy Markdown
Contributor

Which sig does this fall under?

@jessfraz
Copy link
Copy Markdown
Contributor Author

jessfraz commented Aug 16, 2017 via email

@jessfraz
Copy link
Copy Markdown
Contributor Author

jessfraz commented Aug 29, 2017 via email

func TestValidateAllowPrivilegeEscalation(t *testing.T) {
pod := defaultPod()
pe := true
pod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation = &pe
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see that tests don't cover that case of using InitContainers? Does this validation work for them? I assume that it should but I don't see any mentions about them in the code.

return PodAdmitResult{Admit: true}
}

func noNewPrivsRequired(pod *v1.Pod) bool {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jessfraz Should we handle InitContainers here too? Looks like, yes, we should.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Are init containers handled differently that should probably be documented somewhere so it's not overlooked unless I missed it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As far I understand, the most of the code from this PR is handling initContainers correctly because it works only with securityContext. The existing infrastructure is aware about containers/initContainers and executes validation on their SecurityContexts.

But when we're manually inspecting PodSpec we obligated to take care about initContainers. In this particular case, I see that noNewPrivsRequired() will return false for a pod that has initContainers with allowPrivielegeEscalation=false. I'm not sure what will be the consequence of this, perhaps allowPrivielegeEscalation=false won't work for a pod?

// the no_new_privs flag will be set on the container process.
// AllowPrivilegeEscalation is true always when the container is:
// 1) run as Privileged
// 2) has CAP_SYS_ADMIN
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jessfraz Are we missing the case "when a container is not run as root" here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We got rid of that functionality

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also some tests can be deleted then:

"allowPrivilegeEscalation nil nonRoot": {
sc: v1.SecurityContext{
RunAsUser: &nonRoot,
},
},
"allowPrivilegeEscalation nil root": {
sc: v1.SecurityContext{
RunAsUser: &root,
},
},

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.