Skip to content

Commit 5b7f841

Browse files
cpugopherbot
authored andcommitted
acme/autocert: fix data race in Manager.createCert
Previously Manager.createCert() read state.locked without holding any lock, while the owner goroutine wrote state.locked = false under the state's write lock. Concurrent goroutines for the same domain raced on that field. We fix the issue by removing the certState.locked field and instead having Manager.certState() return an additional owner bool computed under the m.stateMu lock. This allows the Manager.createCert() caller to know whether it must block on the read lock and return the state handled by another goroutine, or instead do the work itself and release the write lock. A regression unit test is included that fails under -race without the fix, and passes with the fix in-place. Fixes golang/go#80119 Change-Id: I0f4c5c98f7e6bcf1ab71a3c0707bb0c7ce73415b Reviewed-on: https://go-review.googlesource.com/c/crypto/+/793840 Reviewed-by: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Daniel McCarney <[email protected]>
1 parent 0b316e7 commit 5b7f841

2 files changed

Lines changed: 48 additions & 23 deletions

File tree

‎acme/autocert/autocert.go‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -564,24 +564,21 @@ func encodeECDSAKey(w io.Writer, key *ecdsa.PrivateKey) error {
564564
// If the domain is already being verified, it waits for the existing verification to complete.
565565
// Either way, createCert blocks for the duration of the whole process.
566566
func (m *Manager) createCert(ctx context.Context, ck certKey) (*tls.Certificate, error) {
567-
// TODO: maybe rewrite this whole piece using sync.Once
568-
state, err := m.certState(ck)
567+
state, owner, err := m.certState(ck)
569568
if err != nil {
570569
return nil, err
571570
}
572-
// state may exist if another goroutine is already working on it
573-
// in which case just wait for it to finish
574-
if !state.locked {
571+
// If another goroutine is already working on this state, wait for it
572+
// to finish by taking the read lock
573+
if !owner {
575574
state.RLock()
576575
defer state.RUnlock()
577576
return state.tlscert()
578577
}
579578

580-
// We are the first; state is locked.
581-
// Unblock the readers when domain ownership is verified
582-
// and we got the cert or the process failed.
579+
// We are the first to work on this certKey, so state is write-locked.
580+
// Unblock the readers when our work is complete.
583581
defer state.Unlock()
584-
state.locked = false
585582

586583
der, leaf, err := m.authorizedCert(ctx, state.key, ck)
587584
if err != nil {
@@ -611,18 +608,23 @@ func (m *Manager) createCert(ctx context.Context, ck certKey) (*tls.Certificate,
611608
return state.tlscert()
612609
}
613610

614-
// certState returns a new or existing certState.
615-
// If a new certState is returned, state.exist is false and the state is locked.
611+
// certState returns a new or existing certState along with a boolean
612+
// indicating whether the caller is the owner of the state.
613+
//
614+
// The owner of the state is responsible for performing the ACME work and
615+
// must unlock the state's write lock when done. Non-owner callers should
616+
// wait on the state's read lock for the owner to finish.
617+
//
616618
// The returned error is non-nil only in the case where a new state could not be created.
617-
func (m *Manager) certState(ck certKey) (*certState, error) {
619+
func (m *Manager) certState(ck certKey) (*certState, bool, error) {
618620
m.stateMu.Lock()
619621
defer m.stateMu.Unlock()
620622
if m.state == nil {
621623
m.state = make(map[certKey]*certState)
622624
}
623625
// existing state
624626
if state, ok := m.state[ck]; ok {
625-
return state, nil
627+
return state, false, nil
626628
}
627629

628630
// new locked state
@@ -636,16 +638,13 @@ func (m *Manager) certState(ck certKey) (*certState, error) {
636638
key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
637639
}
638640
if err != nil {
639-
return nil, err
641+
return nil, false, err
640642
}
641643

642-
state := &certState{
643-
key: key,
644-
locked: true,
645-
}
644+
state := &certState{key: key}
646645
state.Lock() // will be unlocked by m.certState caller
647646
m.state[ck] = state
648-
return state, nil
647+
return state, true, nil
649648
}
650649

651650
// authorizedCert starts the domain ownership verification process and requests a new cert upon success.
@@ -1035,10 +1034,9 @@ func (m *Manager) now() time.Time {
10351034
// certState is ready when its mutex is unlocked for reading.
10361035
type certState struct {
10371036
sync.RWMutex
1038-
locked bool // locked for read/write
1039-
key crypto.Signer // private key for cert
1040-
cert [][]byte // DER encoding
1041-
leaf *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
1037+
key crypto.Signer // private key for cert
1038+
cert [][]byte // DER encoding
1039+
leaf *x509.Certificate // parsed cert[0]; always non-nil if cert != nil
10421040
}
10431041

10441042
// tlscert creates a tls.Certificate from s.key and s.cert.

‎acme/autocert/autocert_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,33 @@ func TestGetCertificate_failedAttempt(t *testing.T) {
513513
}
514514
}
515515

516+
// TestGetCertificate_concurrent guards against a data race on certState
517+
// fields between the goroutine performing the ACME work for a domain and
518+
// the other goroutines waiting on the result. See golang/go#80119.
519+
func TestGetCertificate_concurrent(t *testing.T) {
520+
// An unreachable directory URL makes the owner goroutine fail
521+
// quickly inside createCert, so its writes overlap with the
522+
// non-owner goroutines reaching the same code path.
523+
m := &Manager{
524+
Prompt: AcceptTOS,
525+
Client: &acme.Client{DirectoryURL: "http://127.0.0.1:1/"},
526+
}
527+
defer m.stopRenew()
528+
529+
start := make(chan struct{})
530+
var wg sync.WaitGroup
531+
for i := 0; i < 64; i++ {
532+
wg.Add(1)
533+
go func() {
534+
defer wg.Done()
535+
<-start
536+
m.GetCertificate(clientHelloInfo(exampleDomain, algECDSA))
537+
}()
538+
}
539+
close(start)
540+
wg.Wait()
541+
}
542+
516543
func TestRevokeFailedAuthz(t *testing.T) {
517544
ca := acmetest.NewCAServer(t)
518545
// Make the authz unfulfillable on the client side, so it will be left

0 commit comments

Comments
 (0)