Navigation Menu

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

path/filepath: EvalSymlinks fails to follow symlink when root entry is a symlink #39818

Open
mildred opened this issue Jun 24, 2020 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mildred
Copy link

mildred commented Jun 24, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mildred/.cache/go-build"
GOENV="/home/mildred/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mildred/Projects/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mildred/Projects/terraform/test-bug-1/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build280616979=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I found a bug in terraform: hashicorp/terraform#25367 and tracked it down to the go runtime

Reproducer:

package main

import (
  "path/filepath"
  "fmt"
  "syscall"
  "os"
)

func main(){
  os.Mkdir("origin", 0777)
  os.Mkdir("origin/var", 0777)
  os.Mkdir("origin/var/home", 0777)
  os.Mkdir("origin/var/home/foo", 0777)
  os.Symlink("var/home", "origin/home")
  os.Symlink("/home/foo", "origin/home/bar")
  os.Chdir("origin")
  syscall.Chroot(".")
  os.Chdir("home")
  fmt.Println(filepath.EvalSymlinks("bar"))
}

Reproduce by compiling this example and running it:

go build -o test-bug .
sudo ./test-bug
sudo strace -e trace=newfstatat,readlinkat ./test-bug

(you need to be root for chroot to work, it works well in a fedora-toolbox container with a user namespace root)

What did you expect to see?

filepath.EvalSymlinks should follow symlink home -> var/home in origin

What did you see instead?

filepath.EvalSymlinks fails to follow the symlink and errors out with lstat var: no such file or directory

@mildred

This comment has been minimized.

mildred added a commit to mildred/go that referenced this issue Jun 24, 2020
When a symlink is at filesystem root (such can be the case on OSTree
systems where /root points to /var/roothome and /home points to
/var/home) EvalSymlinks fails to follow it correctly. Instead of
following it in the context of the root directory, it tries to follow it
in the context of the current directory.

This is because when the last traversed directory is removed to
concatenate the symlink target, "/home" contract to the empty string,
and "var/home" is thus evaluated in the context of the current
directory.

This commit fixes that by detecting if the last removed directory was a
root directory, and does not remove the trailing path separator in that
case.

Fixes golang#39818
@mildred
Copy link
Author

mildred commented Jun 24, 2020

Unfortunately, I might not be able to provide a pull request with that patch as I don't have a google account at hand to sign the CLA (and I won't create one). Please feel free to do whatever you want with the patch I provide, I do not retain any copyright on it.

@ianlancetaylor
Copy link
Contributor

It's fine to not sign the CLA, of course, but in that case please do not send the patch as a GitHub comment. That just complicates the copyright status of the change. Thanks.

@cagedmantis cagedmantis changed the title filepath.EvalSymlinks fails to follow symlink when root entry is a symlink path/filepath: EvalSymlinks fails to follow symlink when root entry is a symlink Jun 25, 2020
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 25, 2020
@cagedmantis cagedmantis added this to the Backlog milestone Jun 25, 2020
@networkimprov
Copy link

EvalSymlinks should almost never be used; see #40180. If Terraform isn't aware of this, could you inform them?

@mildred
Copy link
Author

mildred commented Jul 18, 2020

In any case, I'm ok to sign the CLA using any other means than using a google account. I also provided a PR with the patch, but I did that before knowing I had to sign the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants