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

Fix race accessing b.crls within cert auth #18945

Merged
merged 3 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions builtin/credential/cert/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Backend() *backend {
pathListCRLs(&b),
pathCRLs(&b),
},
AuthRenew: b.pathLoginRenew,
AuthRenew: b.loginPathWrapper(b.pathLoginRenew),
Invalidate: b.invalidate,
BackendType: logical.TypeCredential,
InitializeFunc: b.initialize,
Expand Down Expand Up @@ -101,12 +101,12 @@ func (b *backend) initOCSPClient(cacheSize int) {
}, cacheSize)
}

func (b *backend) updatedConfig(config *config) error {
func (b *backend) updatedConfig(config *config) {
b.ocspClientMutex.Lock()
defer b.ocspClientMutex.Unlock()
b.initOCSPClient(config.OcspCacheSize)
b.configUpdated.Store(false)
return nil
return
}

func (b *backend) fetchCRL(ctx context.Context, storage logical.Storage, name string, crl *CRLInfo) error {
Expand Down
10 changes: 10 additions & 0 deletions builtin/credential/cert/path_crls.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ using the same name as specified here.`,
}
}

func (b *backend) populateCrlsIfNil(ctx context.Context, storage logical.Storage) error {
b.crlUpdateMutex.RLock()
if b.crls == nil {
b.crlUpdateMutex.RUnlock()
return b.lockThenpopulateCRLs(ctx, storage)
}
b.crlUpdateMutex.RUnlock()
return nil
}

func (b *backend) lockThenpopulateCRLs(ctx context.Context, storage logical.Storage) error {
b.crlUpdateMutex.Lock()
defer b.crlUpdateMutex.Unlock()
Expand Down
20 changes: 13 additions & 7 deletions builtin/credential/cert/path_crls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"io/ioutil"
"math/big"
"net/http"
Expand All @@ -14,6 +15,8 @@ import (
"testing"
"time"

"github.com/hashicorp/vault/helper/testhelpers/corehelpers"

"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/certutil"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -162,7 +165,7 @@ func TestCRLFetch(t *testing.T) {

b.crlUpdateMutex.Lock()
if len(b.crls["testcrl"].Serials) != 1 {
t.Fatalf("wrong number of certs in CRL")
t.Fatalf("wrong number of certs in CRL got %d, expected 1", len(b.crls["testcrl"].Serials))
}
b.crlUpdateMutex.Unlock()

Expand All @@ -188,11 +191,14 @@ func TestCRLFetch(t *testing.T) {

// Give ourselves a little extra room on slower CI systems to ensure we
// can fetch the new CRL.
time.Sleep(150 * time.Millisecond)
corehelpers.RetryUntil(t, 2*time.Second, func() error {
b.crlUpdateMutex.Lock()
defer b.crlUpdateMutex.Unlock()

b.crlUpdateMutex.Lock()
if len(b.crls["testcrl"].Serials) != 2 {
t.Fatalf("wrong number of certs in CRL")
}
b.crlUpdateMutex.Unlock()
serialCount := len(b.crls["testcrl"].Serials)
if serialCount != 2 {
return fmt.Errorf("CRL refresh did not occur serial count %d", serialCount)
}
return nil
})
}
29 changes: 14 additions & 15 deletions builtin/credential/cert/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,27 @@ func pathLogin(b *backend) *framework.Path {
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathLogin,
logical.UpdateOperation: b.loginPathWrapper(b.pathLogin),
logical.AliasLookaheadOperation: b.pathLoginAliasLookahead,
logical.ResolveRoleOperation: b.pathLoginResolveRole,
logical.ResolveRoleOperation: b.loginPathWrapper(b.pathLoginResolveRole),
},
}
}

func (b *backend) loginPathWrapper(wrappedOp func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error)) framework.OperationFunc {
return func(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
// Make sure that the CRLs have been loaded before processing a login request,
// they might have been nil'd by an invalidate func call.
if err := b.populateCrlsIfNil(ctx, req.Storage); err != nil {
return nil, err
}
return wrappedOp(ctx, req, data)
}
}

func (b *backend) pathLoginResolveRole(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
var matched *ParsedCert

if verifyResp, resp, err := b.verifyCredentials(ctx, req, data); err != nil {
return nil, err
} else if resp != nil {
Expand Down Expand Up @@ -90,13 +102,6 @@ func (b *backend) pathLogin(ctx context.Context, req *logical.Request, data *fra
b.updatedConfig(config)
}

if b.crls == nil {
// Probably invalidated due to replication, but we need these to proceed
if err := b.populateCRLs(ctx, req.Storage); err != nil {
return nil, err
}
}

var matched *ParsedCert
if verifyResp, resp, err := b.verifyCredentials(ctx, req, data); err != nil {
return nil, err
Expand Down Expand Up @@ -173,12 +178,6 @@ func (b *backend) pathLoginRenew(ctx context.Context, req *logical.Request, d *f
b.updatedConfig(config)
}

if b.crls == nil {
if err := b.populateCRLs(ctx, req.Storage); err != nil {
return nil, err
}
}

if !config.DisableBinding {
var matched *ParsedCert
if verifyResp, resp, err := b.verifyCredentials(ctx, req, d); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions changelog/18945.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
auth/cert: Address a race condition accessing the loaded crls without a lock
```