Skip to content

Commit

Permalink
Fix a possible race with concurrent encryption with the same config
Browse files Browse the repository at this point in the history
append() can allocate arrays with cap(s) > len(s), and future
append() calls would then just write to the free slots; doing that
from multiple goroutines would race.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Oct 20, 2023
1 parent ffc163c commit 0e0e1a1
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion keywrap/pkcs11/keywrapper_pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func NewKeyWrapper() keywrap.KeyWrapper {
// WrapKeys wraps the session key for recpients and encrypts the optsData, which
// describe the symmetric key used for encrypting the layer
func (kw *pkcs11KeyWrapper) WrapKeys(ec *config.EncryptConfig, optsData []byte) ([]byte, error) {
pkcs11Recipients, err := addPubKeys(&ec.DecryptConfig, append(ec.Parameters["pkcs11-pubkeys"], ec.Parameters["pkcs11-yamls"]...))
// append({}, ...) allocates a fresh backing array, and that's necessary to guarantee concurrent calls to WrapKeys (as in c/image/copy.Image)
// can't race writing to the same backing array.
pubKeys := append([][]byte{}, ec.Parameters["pkcs11-pubkeys"]...) // In Go 1.21, slices.Clone(ec.Parameters["pkcs11-pubkeys"])
pubKeys = append(pubKeys, ec.Parameters["pkcs11-yamls"]...)
pkcs11Recipients, err := addPubKeys(&ec.DecryptConfig, pubKeys)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0e0e1a1

Please sign in to comment.