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

Change ordering of user lookup vs. password hashing #5614

Merged
merged 2 commits into from
Oct 27, 2018
Merged
Changes from 1 commit
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
53 changes: 39 additions & 14 deletions builtin/credential/userpass/path_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/helper/cidrutil"
"github.com/hashicorp/vault/helper/policyutil"
"github.com/hashicorp/vault/logical"
Expand Down Expand Up @@ -62,32 +63,56 @@ func (b *backend) pathLogin(ctx context.Context, req *logical.Request, d *framew
}

// Get the user and validate auth
user, err := b.user(ctx, req.Storage, username)
if err != nil {
return nil, err
}
if user == nil {
return logical.ErrorResponse("invalid username or password"), nil
}

// Check for a CIDR match.
if !cidrutil.RemoteAddrIsOk(req.Connection.RemoteAddr, user.BoundCIDRs) {
return logical.ErrorResponse("login request originated from invalid CIDR"), nil
user, userError := b.user(ctx, req.Storage, username)

var userPassword []byte
var err error
var legacyPassword bool
// If there was an error or it's nil, we fake a password for the bcrypt
// check so as not to have a timing leak. Specifics of the underlying
// storage still leaks a bit but generally much more in the noise compared
// to bcrypt.
if user != nil && userError == nil {
if user.PasswordHash == nil {
userPassword = []byte(user.Password)
legacyPassword = true
} else {
userPassword = user.PasswordHash
}
} else {
userPassword, err = uuid.GenerateRandomBytes(16)
if err != nil {
// This is still acceptable as bcrypt will still make sure it takes
// a long time, it's just nicer to be random if possible
userPassword = []byte("dummy")
}
}

// Check for a password match. Check for a hash collision for Vault 0.2+,
// but handle the older legacy passwords with a constant time comparison.
passwordBytes := []byte(password)
if user.PasswordHash != nil {
if err := bcrypt.CompareHashAndPassword(user.PasswordHash, passwordBytes); err != nil {
if !legacyPassword {
if err := bcrypt.CompareHashAndPassword(userPassword, passwordBytes); err != nil {
return logical.ErrorResponse("invalid username or password"), nil
}
} else {
if subtle.ConstantTimeCompare([]byte(user.Password), passwordBytes) != 1 {
if subtle.ConstantTimeCompare(userPassword, passwordBytes) != 1 {
return logical.ErrorResponse("invalid username or password"), nil
}
}

if userError != nil {
return nil, userError
}
if user == nil {
return logical.ErrorResponse("invalid username or password"), nil
}

// Check for a CIDR match.
if !cidrutil.RemoteAddrIsOk(req.Connection.RemoteAddr, user.BoundCIDRs) {
return logical.ErrorResponse("login request originated from invalid CIDR"), nil
}

return &logical.Response{
Auth: &logical.Auth{
Policies: user.Policies,
Expand Down