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

ensure etc-host file permission is 644 whatever umask is #113209

Merged
merged 1 commit into from
May 16, 2023

Conversation

luozhiwenn
Copy link
Contributor

@luozhiwenn luozhiwenn commented Oct 20, 2022

What type of PR is this?

/kind bug

What this PR does / why we need it:

ensure etc-host file permission is 644 whatever umask of kubelet is

Which issue(s) this PR fixes:

Fixes #96342, #80668

Special notes for your reviewer:

When the umask of kubelet is not 000, the permission on the etc-hosts files created by kubelet is unexpected, which causes non-root applications to be unable to access these files.
Explicitly modify the files permissions to ensure that the files permissions meet the expected 644.

Does this PR introduce a user-facing change?

kubelet will ensure /etc/hosts file is mode 0644 regardless of umask.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added the release-note-none Denotes a PR that doesn't merit a release note. label Oct 20, 2022
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 20, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: luozhiwenn / name: zhwn (76c8765)

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 20, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @luozhiwenn!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 20, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @luozhiwenn. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 20, 2022
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 20, 2022
@tanjing2020
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 21, 2022
@BenTheElder
Copy link
Member

Is there a reason to add this specific chmod vs say, altering umask in the process? What's the use case where we'd want to have a custom umask only for some of kubelet's files?

@luozhiwenn
Copy link
Contributor Author

Is there a reason to add this specific chmod vs say, altering umask in the process? What's the use case where we'd want to have a custom umask only for some of kubelet's files?

Hi, is the following change what you refer to altering umask in the process?

func ensureHostsFile(fileName string, hostIPs []string, hostName, hostDomainName string, hostAliases []v1.HostAlias, useHostNetwork bool) error {
        // ...
        oldMask, _ := util.Umask(0)
	err = ioutil.WriteFile(fileName, hostsFileContent, 0644)
	util.Umask(oldMask)
        return err
}

There is a concurrency problem in altering umask in the process because method ensureHostsFile is invoked concurrently:

  1. kubelet with umask 027
  2. Creating Pod A etc-hosts file, save oldMask 027 and set umask to 000
  3. Concurrently creating Pod B etc-hosts file, save oldMask 000 and set umask to 000
  4. Creates Pod A etc-hosts file complete, set umask back to oldMask 027
  5. Creates Pod B etc-hosts file complete, set umask back to oldMask 000
  6. kubelet with umask 000

This concurrency problem causes the umask of the kubelet to be modified, therefore, I prefer to explicitly correct permissions with chmod rather than adding lock in this process.

Inspired by

if err := m.osInterface.Chmod(containerLogPath, 0666); err != nil {

@BenTheElder
Copy link
Member

BenTheElder commented Oct 22, 2022

No I am referring to syscall.Umask early in the process and leaving it altered.

Instead of scoped only to writing this particular file.

@BenTheElder
Copy link
Member

Or alternately:

What is the use case to not permanently alter the umask of kubelet early in the process?

What use case requires a umask for kubelet that does not require it here?

If we're going to intentionally ignore umask for this file, why not for the whole kubelet process?

@luozhiwenn
Copy link
Contributor Author

Apologies for the delayed response.

Or alternately:

What is the use case to not permanently alter the umask of kubelet early in the process?

What use case requires a umask for kubelet that does not require it here?

If we're going to intentionally ignore umask for this file, why not for the whole kubelet process?

  • For security purposes, umask is configured by default(022 or 027?) in many Linux systems. We hope that the kubelet conforms to this configuration, so that files with excessive permissions are not created inadvertently. In particular, kubelet is usually run as the root user, and the owner of the files created by kubelet is also root, which shuld be careful.
  • Different from other files created by kubelet (apps are not interested in), applications read the /etc/hosts file in a typical scenario. Therefore, we want to ensure that the permission on the file is readable by other users regardless of the umask of kubelet.

@bart0sh
Copy link
Contributor

bart0sh commented Dec 14, 2022

/priority important-longterm
/triage accepted
/assign @BenTheElder

@k8s-ci-robot k8s-ci-robot added the priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. label Dec 14, 2022
@matthyx
Copy link
Contributor

matthyx commented Dec 19, 2022

@luozhiwenn as I understand it, the bug occurs only when there is no /etc/hosts file, which forces os.WriteFile to create it with 0644 + umask?
I hope there isn't a possible race condition between the time you create the file, and when you set the proper permissions?

@BenTheElder
Copy link
Member

Forgot to comment: I unassigned because I'm not an approver here and someone who is needs to decide on the approach.

@BenTheElder
Copy link
Member

BenTheElder commented Dec 19, 2022

I hope there isn't a possible race condition between the time you create the file, and when you set the proper permissions?

I don't think that's an issue because we're relaxing permissions, not tightening them.

This is unavoidable I think, unless maybe we write it to a temp location, chmod, then move it (like projected configmaps)

@bart0sh
Copy link
Contributor

bart0sh commented Jan 2, 2023

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 2, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 8fbeee55acf3094a0c8b4a7b31b8affb55967c74

@bart0sh bart0sh moved this from Needs Reviewer to Needs Approver in SIG Node PR Triage Jan 2, 2023
@bart0sh
Copy link
Contributor

bart0sh commented Jan 2, 2023

/assign @derekwaynecarr @dchen1107 @mrunalp

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 2, 2023
@BenTheElder
Copy link
Member

/remove-lifecycle stale
@kubernetes/sig-node-pr-reviews

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 2, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: luozhiwenn, mrunalp

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 16, 2023
@BenTheElder
Copy link
Member

I think maybe this warrants a release note? It's a behavior change.

@BenTheElder
Copy link
Member

release note: "kubelet will ensure /etc/hosts file is mode 0644 regardless of umask" or something along those lines?

@BenTheElder
Copy link
Member

In case we're updating the release notes, which you can do by editing the PR body:
/hold

Comment /hold cancel to remove the hold and merge when your ready (including if we're not changing release note and going ahead and merging as-is).

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels May 16, 2023
@luozhiwenn
Copy link
Contributor Author

In case we're updating the release notes, which you can do by editing the PR body: /hold

Comment /hold cancel to remove the hold and merge when your ready (including if we're not changing release note and going ahead and merging as-is).

done

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 16, 2023
@k8s-ci-robot k8s-ci-robot merged commit 03b2e84 into kubernetes:master May 16, 2023
11 checks passed
SIG Node PR Triage automation moved this from Needs Approver to Done May 16, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.28 milestone May 16, 2023
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/kubelet cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/node Categorizes an issue or PR as relevant to SIG Node. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Pod cannot work due to failing to access /etc/hosts inside container if umask of kubelet is configured as 027
10 participants