[SOLVED] Segfault due to misconfigured unix user?

#1

Hi,

I’d like to let you guys know about a segfault I stumbled across.
While restoring my server from backups some errors were made in the restoring of certain files.
I ended up with the following:

regent:~ # grep gitea /etc/passwd
gitea:x:477:477::/home/gitea:/bin/bash
regent:~ # grep gitea /etc/shadow
gitea:!:17996:0:99999:7:::
gitea:!:17689::::::
regent:~ # grep gitea /etc/group
gitea:x:477:

which then, I thought, led to the following segfault stacktrace:

gitea@regent:~> ./bin/gitea
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7faa78ad931c]

runtime stack:
runtime.throw(0x19c3d99, 0x2a)
        /usr/local/go/src/runtime/panic.go:617 +0x72
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:374 +0x4a9

goroutine 1 [syscall]:
runtime.cgocall(0x1578c20, 0xc000609180, 0xc000010170)
        /usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc000609150 sp=0xc000609118 pc=0x407f8b
os/user._Cfunc_mygetpwuid_r(0x1dd, 0xc0004e5fb0, 0x50272e0, 0x400, 0xc000010170, 0x0)
        _cgo_gotypes.go:173 +0x4d fp=0xc000609180 sp=0xc000609150 pc=0x86ffdd
os/user.lookupUnixUid.func1.1(0x1dd, 0xc0004e5fb0, 0xc0004e8f90, 0xc000010170, 0x50272e001578c90)
        /usr/local/go/src/os/user/cgo_lookup_unix.go:103 +0x14c fp=0xc0006091d0 sp=0xc000609180 pc=0x8716ac
os/user.lookupUnixUid.func1(0x8)
        /usr/local/go/src/os/user/cgo_lookup_unix.go:103 +0x45 fp=0xc000609208 sp=0xc0006091d0 pc=0x871745
os/user.retryWithBuffer(0xc0004e8f90, 0xc000609300, 0xc0004e8f90, 0x110)
        /usr/local/go/src/os/user/cgo_lookup_unix.go:251 +0x3b fp=0xc000609270 sp=0xc000609208 pc=0x870abb
os/user.lookupUnixUid(0x1dd, 0x0, 0x0, 0x0)
        /usr/local/go/src/os/user/cgo_lookup_unix.go:96 +0x130 fp=0xc000609338 sp=0xc000609270 pc=0x870400
os/user.current(0xc0006093b0, 0x10, 0xc0006093a8)
        /usr/local/go/src/os/user/cgo_lookup_unix.go:49 +0x4a fp=0xc000609378 sp=0xc000609338 pc=0x87029a
os/user.Current.func1()
        /usr/local/go/src/os/user/lookup.go:15 +0x22 fp=0xc0006093a0 sp=0xc000609378 pc=0x8714f2
sync.(*Once).Do(0x3cf05e0, 0x19fad80)
        /usr/local/go/src/sync/once.go:44 +0xb3 fp=0xc0006093d0 sp=0xc0006093a0 pc=0x46b4f3
os/user.Current(0x0, 0x0, 0x0)
        /usr/local/go/src/os/user/lookup.go:15 +0x3d fp=0xc000609400 sp=0xc0006093d0 pc=0x86fc6d
code.gitea.io/gitea/modules/user.CurrentUsername(0xc000233ab0, 0x197439d)
        /go/src/code.gitea.io/gitea/modules/user/user.go:16 +0x22 fp=0xc000609428 sp=0xc000609400 pc=0xb3db92
code.gitea.io/gitea/modules/setting.NewContext()
        /go/src/code.gitea.io/gitea/modules/setting/setting.go:790 +0x30ab fp=0xc0006098f0 sp=0xc000609428 pc=0xb50cbb
code.gitea.io/gitea/routers.GlobalInit()
        /go/src/code.gitea.io/gitea/routers/init.go:67 +0x37 fp=0xc000609a00 sp=0xc0006098f0 pc=0x12b2b67
code.gitea.io/gitea/cmd.runWeb(0xc00027e3c0, 0x0, 0x0)
        /go/src/code.gitea.io/gitea/cmd/web.go:121 +0xa4 fp=0xc000609be8 sp=0xc000609a00 pc=0x14c4c24
code.gitea.io/gitea/vendor/github.com/urfave/cli.HandleAction(0x1758940, 0x19f1fe8, 0xc00027e3c0, 0xc0000a1140, 0x0)
        /go/src/code.gitea.io/gitea/vendor/github.com/urfave/cli/app.go:471 +0xad fp=0xc000609c10 sp=0xc000609be8 pc=0x10ec45d
code.gitea.io/gitea/vendor/github.com/urfave/cli.(*App).Run(0xc000256b60, 0xc0000b6170, 0x1, 0x1, 0x0, 0x0)
        /go/src/code.gitea.io/gitea/vendor/github.com/urfave/cli/app.go:246 +0x574 fp=0xc000609ed8 sp=0xc000609c10 pc=0x10ea4c4
main.main()
        /go/src/code.gitea.io/gitea/main.go:57 +0x426 fp=0xc000609f98 sp=0xc000609ed8 pc=0x14d25c6
runtime.main()
        /usr/local/go/src/runtime/proc.go:200 +0x20c fp=0xc000609fe0 sp=0xc000609f98 pc=0x4342ec
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc000609fe8 sp=0xc000609fe0 pc=0x462121

goroutine 23 [select]:
code.gitea.io/gitea/modules/log.(*Logger).StartLogger(0xc0000d8b40)
        /go/src/code.gitea.io/gitea/modules/log/log.go:303 +0xf5
created by code.gitea.io/gitea/modules/log.newLogger
        /go/src/code.gitea.io/gitea/modules/log/log.go:233 +0xba

goroutine 50 [select]:
code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.AnalysisWorker(0xc0005cee40, 0xc0005ceea0)
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:102 +0x102
created by code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.NewAnalysisQueue
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:94 +0xc8

goroutine 51 [select]:
code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.AnalysisWorker(0xc0005cee40, 0xc0005ceea0)
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:102 +0x102
created by code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.NewAnalysisQueue
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:94 +0xc8

goroutine 52 [select]:
code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.AnalysisWorker(0xc0005cee40, 0xc0005ceea0)
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:102 +0x102
created by code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.NewAnalysisQueue
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:94 +0xc8

goroutine 53 [select]:
code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.AnalysisWorker(0xc0005cee40, 0xc0005ceea0)
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:102 +0x102
created by code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index.NewAnalysisQueue
        /go/src/code.gitea.io/gitea/vendor/github.com/blevesearch/bleve/index/analysis.go:94 +0xc8

goroutine 66 [chan receive]:
code.gitea.io/gitea/modules/notification/ui.(*notificationService).Run(0xc000010048)
        /go/src/code.gitea.io/gitea/modules/notification/ui/ui.go:40 +0x4d
created by code.gitea.io/gitea/modules/notification.RegisterNotifier
        /go/src/code.gitea.io/gitea/modules/notification/notification.go:22 +0x4a

goroutine 71 [syscall]:
os/signal.signal_recv(0x462126)
        /usr/local/go/src/runtime/sigqueue.go:139 +0x9c
os/signal.loop()
        /usr/local/go/src/os/signal/signal_unix.go:23 +0x22
created by os/signal.init.0
        /usr/local/go/src/os/signal/signal_unix.go:29 +0x41

But even after fixing the /etc/shadow file, the segfault persists.
I’ve not been able to find anything in the docs, though a tangentially related blog post mentioned that linking to different versions of the glibc could lead to such errors.
I downloaded the latest 1.8.0 build from the download site, specifically
https://dl.gitea.io/gitea/1.8.0/gitea-1.8.0-linux-amd64
to test whether it has anything to do with the version, but it does not change anything.

I’ve tried increasing logging, but the log never gets written to, the crash seems to happen before logging init.
I’ve tried further understanding the stack trace, but other than the getpwuid call to the libc, nothing stands out to me, as I’m not familiar with go.

Has anyone come across this before?

#2

It seems GetCurrentUser panic.

#3

It would appear, that the following fails:

err := retryWithBuffer(buf, func() syscall.Errno {
    // mygetpwuid_r is a wrapper around getpwuid_r to avoid using uid_t
    // because C.uid_t(uid) for unknown reasons doesn't work on linux.
    return syscall.Errno(C.mygetpwuid_r(C.int(uid),
	&pwd,
	(*C.char)(buf.ptr),
	C.size_t(buf.size),
	&result))
    })

But I’ve been unable to figure out why.
From what I’ve been able to gather, this calls the glibc-function getpwuid_r, which returns a pointer to a struct, so all that makes sense to me is that it returns a NULL.
But again, I don’t know why.
The entries in /etc/passwd, /etc/group, and /etc/shadow seem well-formed, as I’m able to log in with this account and everything works.

#4

After moving the whole application to a newly created used, everything works.
It’s a rather unsatisfying solve, but a solve nonetheless.

closed #5