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

os/user: Lookup fails to find users when running as root on darwin-arm64 cross compiled from darwin-amd64 #43543

Open
JBirdVegas opened this issue Jan 6, 2021 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Milestone

Comments

@JBirdVegas
Copy link

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

$ go version
go version go1.16beta1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

Compiled using darwin, amd64. Executed on darwin, arm64

Build env:

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user-js/Library/Caches/go-build"
GOENV="/Users/user-js/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user-js/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user-js/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/user-js/go/go1.16beta1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/user-js/go/go1.16beta1/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16beta1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/s6/fv1xpj4n7j9c9l1flw1j4lxr0000gn/T/go-build580121957=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.16beta1 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.16beta1
uname -v: Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H15
lldb --version: lldb-1200.0.41
Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
gdb --version: GNU gdb (GDB) 8.3

Execution env:

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/user-es/Library/Caches/go-build"
GOENV="/Users/user-es/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user-es/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/user-es/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.15.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.15.6/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16beta1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v1/1y48b2yx6tndh56_yn2fqx4c0000gn/T/go-build161273293=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.16beta1 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.16beta1
uname -v: Darwin Kernel Version 20.1.0: Sat Oct 31 00:07:10 PDT 2020; root:xnu-7195.50.7~2/RELEASE_ARM64_T8101
ProductName:	macOS
ProductVersion:	11.0.1
BuildVersion:	20B29
lldb --version: lldb-1200.0.44.2
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
### What did you do?

cross compile code on darwin amd64 then execute on darwin arm64.

% cat main.go
package main

import (
	"log"
	"os"
	"os/user"
	"runtime"
)

func main() {
	log.Printf("GOOS: %s, GOARCH: %s", runtime.GOOS, runtime.GOARCH)
	lookup, err := user.Lookup(os.Args[1])
	if err != nil {
		log.Panic(err)
	}
	log.Printf("User: %v\n", lookup)
}

Compile on darwin amd64 for arm64
GOOS=darwin GOARCH=arm64 go build main.go

Download to darwin arm64 computer then execute without and with sudo.

% whoami
user-es
% ./main $(whoami)
2021/01/06 09:08:50 GOOS: darwin, GOARCH: arm64
2021/01/06 09:08:50 User: &{501 20 user-es  /Users/user-es}
% sudo ./main $(whoami)
2021/01/06 09:08:50 GOOS: darwin, GOARCH: arm64
2021/01/06 09:08:50 user: unknown user user-es
panic: user: unknown user user-es

goroutine 1 [running]:
log.Panic(0x1400010ff38, 0x1, 0x1)
	/Users/user-js/go/go1.16beta1/src/log/log.go:354 +0x98
main.main()
	/Users/user-js/dev/go1.16beta1-maybebug/main.go:14 +0xe8

What did you expect to see?

I expected the user user-es found by the function user.Lookup(string) when running as root, same as it does without root.

I also expected the same error when the code is compiled on darwin-arm64 and executed on darwin-arm64. However if compiled on the target machine the function user.Lookup(string) works as expected when ran as both user and root.

What did you see instead?

The function user.Lookup(string) fails to find the user user-es when running as root, when cross compiled from darwin-amd64.

@bcmills bcmills changed the title func user.Lookup(string) fails to find users when running as root on darwin-arm64 cross compiled from darwin-amd64. os/user: Lookup fails to find users when running as root on darwin-arm64 cross compiled from darwin-amd64. Jan 6, 2021
@bcmills bcmills changed the title os/user: Lookup fails to find users when running as root on darwin-arm64 cross compiled from darwin-amd64. os/user: Lookup fails to find users when running as root on darwin-arm64 cross compiled from darwin-amd64 Jan 6, 2021
@bcmills bcmills added OS-Darwin NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 6, 2021
@bcmills bcmills added this to the Backlog milestone Jan 6, 2021
@bcmills
Copy link
Contributor

bcmills commented Jan 6, 2021

CC @cherrymui

@ianlancetaylor
Copy link
Contributor

You probably have to set CGO_ENABLED=1. But that may fail due to #43476.

@JBirdVegas
Copy link
Author

Yes @ianlancetaylor that is correct however building with CGO_ENABLED=0 produces the same error.

#43476 refers to building on arm64 and executing on amd64. My error is the opposite, GOHOSTARCH=amd64 GOARCH=arm64

@ianlancetaylor
Copy link
Contributor

To be clear, I'm suggesting that you need to build with CGO_ENABLED=1. The default is CGO_ENABLED=0.

@whess96
Copy link

whess96 commented Feb 11, 2023

Building w/ CGO enabled should work. However, the os/user docco claims to provide "pure go" implementations for when cgo is disabled and when the osusergo build tag is enabled. Unfortunately the pure go implementation directly parses the /etc/passwd file which doesn't work on darwin 😕.

Since cgo can be a real cross-compilation headache, a pure go implementation for darwin would be extremely convenient.

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. OS-Darwin
Projects
None yet
Development

No branches or pull requests

4 participants