]> Cypherpunks repositories - gostls13.git/commit
os/user: zero-initialize C structs returned to Go
authorBryan C. Mills <bcmills@google.com>
Thu, 8 Dec 2022 15:47:03 +0000 (10:47 -0500)
committerGopher Robot <gobot@golang.org>
Fri, 9 Dec 2022 04:05:17 +0000 (04:05 +0000)
commit80f7484af76773b7591d57f8f5e4479603089b90
treea579bafb3d1d9270e4a74f756acab2f3fbb4ee7b
parente738a2f19b0375662abc8a9f293b003b6a46019b
os/user: zero-initialize C structs returned to Go

In the wrappers for getgrnam_r and similar, the structs to be returned
are allocated on the C stack and may be uninitialized. If the call to
the wrapped C function returns an error (such as ERANGE), it may leave
the struct uninitialized, expecting that the caller will not read it.

However, when that struct is returned to Go, it may be read by the Go
garbage collector. If the uninitialized struct fields happen to
contain wild pointers, the Go garbage collector will throw an error.
(Prior to CL 449335, the Go runtime would not scan the struct fields
because they did not reside in Go memory.)

Fix this by always zeroing the struct before the C call.

Fixes #57170.

Change-Id: I241ae8e4added6f9a406dac37a7f6452341aa0cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/456121
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
src/os/user/cgo_lookup_cgo.go