Skip to content

Commit

Permalink
kms: Add semaphore to limit concurrency
Browse files Browse the repository at this point in the history
generateCipher is memory heavy, so to avoid OOM situations, a semaphore
is added to limit concurrency here.

Fixes: ceph#3472
Signed-off-by: Michael Fritch <mfritch@suse.com>
  • Loading branch information
mgfritch committed Feb 26, 2024
1 parent e6d9139 commit a095506
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -29,6 +29,7 @@ require (
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.19.0
golang.org/x/net v0.21.0
golang.org/x/sync v0.5.0
golang.org/x/sys v0.17.0
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
Expand Down Expand Up @@ -150,7 +151,6 @@ require (
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions internal/kms/secretskms.go
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ceph/ceph-csi/internal/util/k8s"

"golang.org/x/crypto/scrypt"
"golang.org/x/sync/semaphore"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -48,6 +49,8 @@ const (
metadataSecretNamespaceKey = "secretNamespace"
)

var scryptSem = semaphore.NewWeighted(int64(1))

// secretsKMS is default KMS implementation that means no KMS is in use.
type secretsKMS struct {
integratedDEK
Expand Down Expand Up @@ -271,6 +274,13 @@ func (kms secretsMetadataKMS) GetSecret(volumeID string) (string, error) {
// generateCipher returns a AEAD cipher based on a passphrase and salt
// (volumeID). The cipher can then be used to encrypt/decrypt the DEK.
func generateCipher(passphrase, salt string) (cipher.AEAD, error) {
// Note: This is memory heavy!
// Acquire blocks concurrent access so that only 1 worker can call scrypt.Key at a time.
if err := scryptSem.Acquire(context.TODO(), 1); err != nil {
return nil, err
}
defer scryptSem.Release(1)

key, err := scrypt.Key([]byte(passphrase), []byte(salt), 32768, 8, 1, 32)
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions internal/kms/secretskms_test.go
Expand Up @@ -62,6 +62,27 @@ func TestGenerateCipher(t *testing.T) {
assert.NotNil(t, aead)
}

func TestGenerateCipherConcurrent(t *testing.T) {
t.Parallel()
// nolint:gosec // this passphrase is intentionally hardcoded
passphrase := "my-cool-luks-passphrase"
salt := "unique-id-for-the-volume"

runGenerateCipher := func(passphrase string, salt string) {
aead, err := generateCipher(passphrase, salt)
assert.NoError(t, err)
assert.NotNil(t, aead)
}

for i := 0; i < 5; i++ {
go runGenerateCipher(passphrase, salt)
}

for i := 0; i < 5; i++ {
runGenerateCipher(passphrase, salt)
}
}

func TestInitSecretsMetadataKMS(t *testing.T) {
t.Parallel()
args := ProviderInitArgs{
Expand Down
136 changes: 136 additions & 0 deletions vendor/golang.org/x/sync/semaphore/semaphore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Expand Up @@ -681,6 +681,9 @@ golang.org/x/net/websocket
## explicit; go 1.18
golang.org/x/oauth2
golang.org/x/oauth2/internal
# golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
## explicit
golang.org/x/sync/semaphore
# golang.org/x/sync v0.5.0
## explicit; go 1.18
golang.org/x/sync/singleflight
Expand Down

0 comments on commit a095506

Please sign in to comment.