Skip to content

Commit 85d31d0

Browse files
authored
Merge pull request from GHSA-78hx-gp6g-7mj6
Fix memory leak in setupEVP and newCipherCtx
2 parents 576fe0d + 6e2197a commit 85d31d0

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

cipher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,12 @@ func sliceForAppend(in []byte, n int) (head, tail []byte) {
533533
return
534534
}
535535

536-
func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []byte) (ctx C.GO_EVP_CIPHER_CTX_PTR, err error) {
536+
func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []byte) (_ C.GO_EVP_CIPHER_CTX_PTR, err error) {
537537
cipher := loadCipher(kind, mode)
538538
if cipher == nil {
539539
panic("crypto/cipher: unsupported cipher: " + kind.String())
540540
}
541-
ctx = C.go_openssl_EVP_CIPHER_CTX_new()
541+
ctx := C.go_openssl_EVP_CIPHER_CTX_new()
542542
if ctx == nil {
543543
return nil, fail("unable to create EVP cipher ctx")
544544
}

evp.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ type verifyFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, C.size_t, *C.uchar, C.size
149149

150150
func setupEVP(withKey withKeyFunc, padding C.int,
151151
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
152-
init initFunc) (ctx C.GO_EVP_PKEY_CTX_PTR, err error) {
152+
init initFunc) (_ C.GO_EVP_PKEY_CTX_PTR, err error) {
153+
var ctx C.GO_EVP_PKEY_CTX_PTR
154+
withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
155+
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
156+
return 1
157+
})
158+
if ctx == nil {
159+
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
160+
}
153161
defer func() {
154162
if err != nil {
155163
if ctx != nil {
@@ -158,14 +166,6 @@ func setupEVP(withKey withKeyFunc, padding C.int,
158166
}
159167
}
160168
}()
161-
162-
withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
163-
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
164-
return 1
165-
})
166-
if ctx == nil {
167-
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
168-
}
169169
if err := init(ctx); err != nil {
170170
return nil, err
171171
}

0 commit comments

Comments
 (0)