1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:04:57 -07:00
go/src
Filippo Valsorda 6f52790a20 crypto/x509: use Security.framework without cgo for roots on macOS
+----------------------------------------------------------------------+
| Hello, if you are reading this and run macOS, please test this code: |
|                                                                      |
| $ GO111MODULE=on go get golang.org/dl/gotip@latest                   |
| $ gotip download                                              |
| $ GODEBUG=x509roots=1 gotip test crypto/x509 -v -run TestSystemRoots |
+----------------------------------------------------------------------+

We currently have two code paths to extract system roots on macOS: one
uses cgo to invoke a maze of Security.framework APIs; the other is a
horrible fallback that runs "/usr/bin/security verify-cert" on every
root that has custom policies to check if it's trusted for SSL.

The fallback is not only terrifying because it shells out to a binary,
but also because it lets in certificates that are not trusted roots but
are signed by trusted roots, and because it applies some filters (EKUs
and expiration) only to roots with custom policies, as the others are
not passed to verify-cert. The other code path, of course, requires cgo,
so can't be used when cross-compiling and involves a large ball of C.

It's all a mess, and it broke oh-so-many times (#14514, #16532, #19436,
 #20990, #21416, #24437, #24652, #25649, #26073, #27958, #28025, #28092,
 #29497, #30471, #30672, #30763, #30889, #32891, #38215, #38365, ...).

Since macOS does not have a stable syscall ABI, we already dynamically
link and invoke libSystem.dylib regardless of cgo availability (#17490).

How that works is that functions in package syscall (like syscall.Open)
take the address of assembly trampolines (like libc_open_trampoline)
that jump to symbols imported with cgo_import_dynamic (like libc_open),
and pass them along with arguments to syscall.syscall (which is
implemented as runtime.syscall_syscall). syscall_syscall informs the
scheduler and profiler, and then uses asmcgocall to switch to a system
stack and invoke runtime.syscall. The latter is an assembly trampoline
that unpacks the Go ABI arguments passed to syscall.syscall, finally
calls the remote function, and puts the return value on the Go stack.
(This last bit is the part that cgo compiles from a C wrapper.)

We can do something similar to link and invoke Security.framework!

The one difference is that runtime.syscall and friends check errors
based on the errno convention, which Security doesn't follow, so I added
runtime.syscallNoErr which just skips interpreting the return value.
We only need a variant with six arguments because the calling convention
is register-based, and extra arguments simply zero out some registers.

That's plumbed through as crypto/x509/internal/macOS.syscall. The rest
of that package is a set of wrappers for Security.framework and Core
Foundation functions, like syscall is for libSystem. In theory, as long
as macOS respects ABI backwards compatibility (a.k.a. as long as
binaries built for a previous OS version keep running) this should be
stable, as the final result is not different from what a C compiler
would make. (One exception might be dictionary key strings, which we
make our own copy of instead of using the dynamic symbol. If they change
the value of those strings things might break. But why would they.)

Finally, I rewrote the crypto/x509 cgo logic in Go using those wrappers.
It works! I tried to make it match 1:1 the old logic, so that
root_darwin_amd64.go can be reviewed by comparing it to
root_cgo_darwin_amd64.go. The only difference is that we do proper error
handling now, and assume that if there is no error the return values are
there, while before we'd just check for nil pointers and move on.

I kept the cgo logic to help with review and testing, but we should
delete it once we are confident the new code works.

The nocgo logic is gone and we shall never speak of it again.

Fixes #32604
Fixes #19561
Fixes #38365
Awakens Cthulhu

Change-Id: Id850962bad667f71e3af594bdfebbbb1edfbcbb4
Reviewed-on: https://go-review.googlesource.com/c/go/+/227037
Reviewed-by: Katie Hockman <katie@golang.org>
2020-05-07 19:22:19 +00:00
..
archive
bufio
builtin
bytes bytes: simpler and faster FieldsFunc (apply same changes as for strings) 2020-05-01 03:49:21 +00:00
cmd cmd/compile: optimize make+copy pattern to avoid memclr 2020-05-07 17:50:24 +00:00
compress
container
context
crypto crypto/x509: use Security.framework without cgo for roots on macOS 2020-05-07 19:22:19 +00:00
database/sql
debug
encoding encoding/asn1: only accept minimally encoded base 128 integers 2020-05-07 07:06:11 +00:00
errors errors: add example for Is 2020-04-13 20:04:56 +00:00
expvar
flag
fmt
go crypto/x509: use Security.framework without cgo for roots on macOS 2020-05-07 19:22:19 +00:00
hash hash/crc32: simplify hasVX checking on s390x 2020-04-27 21:18:58 +00:00
html html/template,text/template: switch to Unicode escapes for JSON compatibility 2020-04-16 17:13:33 +00:00
image
index/suffixarray
internal syscall, internal/syscall/windows: remove utf16PtrToString parameter 2020-05-03 07:23:32 +00:00
io
log
math math/big: add (*Int).FillBytes 2020-05-05 00:36:44 +00:00
mime
net net/http/cgi: reject invalid header names 2020-05-06 17:06:02 +00:00
os syscall, internal/syscall/windows: remove utf16PtrToString parameter 2020-05-03 07:23:32 +00:00
path
plugin
reflect reflect: keep RO flags unchanged in Value.Addr 2020-05-04 18:16:49 +00:00
regexp regexp/syntax: fix comment on p.literal and simplify 2020-04-17 22:12:02 +00:00
runtime crypto/x509: use Security.framework without cgo for roots on macOS 2020-05-07 19:22:19 +00:00
sort
strconv strconv: fix for parseFloatPrefix 2020-04-30 21:34:51 +00:00
strings bytes, strings: align requirements for functions passed to FieldFuncs 2020-04-29 19:58:47 +00:00
sync
syscall syscall, internal/syscall/windows: remove utf16PtrToString parameter 2020-05-03 07:23:32 +00:00
testdata
testing testing: fix reported caller name for funcs passed to Cleanup 2020-05-05 22:44:36 +00:00
text
time lib/time, time/tzdata: update tz data to 2020a 2020-04-30 08:07:39 +00:00
unicode
unsafe
vendor all: update vendored dependencies for Go 1.15 release 2020-05-04 22:52:07 +00:00
all.bash
all.bat
all.rc
bootstrap.bash bootstrap.bash: preserve file times when copying 2019-04-27 14:36:00 +00:00
buildall.bash
clean.bash
clean.bat
clean.rc
cmp.bash
go.mod all: update vendored dependencies for Go 1.15 release 2020-05-04 22:52:07 +00:00
go.sum all: update vendored dependencies for Go 1.15 release 2020-05-04 22:52:07 +00:00
iostest.bash
make.bash
make.bat
Make.dist
make.rc
race.bash
race.bat
README.vendor
run.bash
run.bat
run.rc

Vendoring in std and cmd
========================

The Go command maintains copies of external packages needed by the
standard library in the src/vendor and src/cmd/vendor directories.

In GOPATH mode, imports of vendored packages are resolved to these
directories following normal vendor directory logic
(see golang.org/s/go15vendor).

In module mode, std and cmd are modules (defined in src/go.mod and
src/cmd/go.mod). When a package outside std or cmd is imported
by a package inside std or cmd, the import path is interpreted
as if it had a "vendor/" prefix. For example, within "crypto/tls",
an import of "golang.org/x/crypto/cryptobyte" resolves to
"vendor/golang.org/x/crypto/cryptobyte". When a package with the
same path is imported from a package outside std or cmd, it will
be resolved normally. Consequently, a binary may be built with two
copies of a package at different versions if the package is
imported normally and vendored by the standard library.

Vendored packages are internally renamed with a "vendor/" prefix
to preserve the invariant that all packages have distinct paths.
This is necessary to avoid compiler and linker conflicts. Adding
a "vendor/" prefix also maintains the invariant that standard
library packages begin with a dotless path element.

The module requirements of std and cmd do not influence version
selection in other modules. They are only considered when running
module commands like 'go get' and 'go mod vendor' from a directory
in GOROOT/src.

Maintaining vendor directories
==============================

Before updating vendor directories, ensure that module mode is enabled.
Make sure GO111MODULE=off is not set ('on' or 'auto' should work).

Requirements may be added, updated, and removed with 'go get'.
The vendor directory may be updated with 'go mod vendor'.
A typical sequence might be:

    cd src
    go get -d golang.org/x/net@latest
    go mod tidy
    go mod vendor

Use caution when passing '-u' to 'go get'. The '-u' flag updates
modules providing all transitively imported packages, not only
the module providing the target package.

Note that 'go mod vendor' only copies packages that are transitively
imported by packages in the current module. If a new package is needed,
it should be imported before running 'go mod vendor'.