Skip to content

Conversation

@soltysh
Copy link
Contributor

@soltysh soltysh commented Apr 18, 2019

What type of PR is this?
/kind bug

What this PR does / why we need it:
This PR cleans up the tar code used in kubectl cp.

Special notes for your reviewer:
/assign @tallclair

Does this PR introduce a user-facing change?:

Clean links handling in cp's tar code

@k8s-ci-robot k8s-ci-robot added the release-note-none Denotes a PR that doesn't merit a release note. label Apr 18, 2019
@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Apr 18, 2019
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. labels Apr 18, 2019
@soltysh
Copy link
Contributor Author

soltysh commented Apr 18, 2019

/sig cli
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Apr 18, 2019
Copy link
Member

Choose a reason for hiding this comment

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

how come you removed this? (I'm not sure I disagree, just curious)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The entire code was introduced in #46762 to deal with copying a file into a dir, which is now much simpler. I've wanted to clean this so with this significant changes it was a good time to do it.

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 here if the destination file is a directory? Can this get confused if the path ends in a /?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on https://golang.org/pkg/path/filepath/#Split it should be solid.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the implementation, it seems like the destination file would be included in the directory if the path end sin a slash, and the file would be "" I think.

I think it's fine, but might be worth adding a test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added directory test, but that's actually handled a few lines earlier in

destFileName := path.Join(destDir, header.Name[len(prefix):])

destFileName will get cleaned (path.Join calls Clean internally) even if it ends with /.

@soltysh soltysh force-pushed the tar-fixes branch 3 times, most recently from 9baef94 to a93a18c Compare April 23, 2019 20:02
@soltysh
Copy link
Contributor Author

soltysh commented Apr 23, 2019

@tallclair added that one last test case and also fixed the verify failure, ptal

@soltysh
Copy link
Contributor Author

soltysh commented Apr 24, 2019

/retest

Copy link
Member

@tallclair tallclair left a comment

Choose a reason for hiding this comment

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

lgtm, just fix the comment.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
file{ // Absolute file
file{ // Relative directory path with terminating /

@soltysh
Copy link
Contributor Author

soltysh commented Apr 26, 2019

@tallclair updated, ptal

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: soltysh

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

The pull request process is described here

Details 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 Apr 26, 2019
@soltysh
Copy link
Contributor Author

soltysh commented Apr 26, 2019

Now with fixed e2e.

@soltysh
Copy link
Contributor Author

soltysh commented Apr 30, 2019

Self-tagging based on previous approval.

@soltysh soltysh added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 30, 2019
@k8s-ci-robot k8s-ci-robot added 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 Apr 30, 2019
@k8s-ci-robot k8s-ci-robot merged commit b95fca0 into kubernetes:master Apr 30, 2019
@soltysh soltysh deleted the tar-fixes branch May 2, 2019 09:05
k8s-ci-robot added a commit that referenced this pull request May 2, 2019
…8-upstream-release-1.14

Automated cherry pick of #76788: Test kubectl cp escape
k8s-ci-robot added a commit that referenced this pull request May 3, 2019
…8-upstream-release-1.13

Automated cherry pick of #76788: Test kubectl cp escape
k8s-ci-robot added a commit that referenced this pull request May 8, 2019
…8-upstream-release-1.12

Automated cherry pick of #76788: Test kubectl cp escape
}
// For scrutiny we verify both the actual destination as well as we follow
// all the links that might lead outside of the destination directory.
if !isDestRelative(destDir, destFileName) || !isDestRelative(destDir, filepath.Join(evaledPath, file)) {
Copy link
Contributor

@abursavich abursavich Jun 24, 2019

Choose a reason for hiding this comment

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

I haven't had enough time to go through everything to understand the intention here, but I think the symlink checking is probably more aggressive than it should be. Picking up this change breaks existing scripts that copy files to temp directories on Mac.

Please be aware that Mac's default readlink doesn't support canonicalizing a path, so it's not as easy to portably fix on the user's side as you might think (see: stackoverflow).

$ FILE=path/to/file
$ TMPD=$(mktemp -d)
$ kubectl cp --no-preserve "$NAMESPACE/$POD:$FILE" "$TMPD/$FILE" -c $CONTAINER
warning: link "/var/folders/h7/ydd9wql57gdf6883cj9779sm0zsnn7/T/tmp.3TVUBz8Mqe/path/to/file" is pointing to "" which is outside target destination, skipping

$ echo $TMPD
/var/folders/h7/ydd9wql57gdf6883cj9779sm0zsnn7/T/tmp.3TVUBz8Mqe
$ readlink -f $TMPD  # using GNU readlink
/private/var/folders/h7/ydd9wql57gdf6883cj9779sm0zsnn7/T/tmp.3TVUBz8Mqe

@liggitt liggitt changed the title Clean links handling in cp's tar code CVE-2019-11246: Clean links handling in cp's tar code Jul 3, 2019
honkiko pushed a commit to honkiko/kubernetes that referenced this pull request Dec 5, 2019
kubectl cp potential directory traversal - CVE-2019-11246

kubectl cp potential directory traversal - CVE-2019-11246

kubernetes#75037
kubernetes#76788

See merge request !53
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/kubectl area/security 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. 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/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants