Skip to content

Commit d189fd0

Browse files
kongfanshen-0801kongfanshen
andauthored
Fix: make enough out data buffer when call EVP_DecryptUpdate (#479) (#408)
If padding is enabled the decrypted data buffer out passed to EVP_DecryptUpdate() should have sufficient room for (inl + cipher_block_size) bytes. More detail information in https://www.openssl.org/docs/man3.1/man3/EVP_DecryptUpdate.html Co-authored-by: kongfanshen <kongfanshen@hashdata.cn>
1 parent 7c0423c commit d189fd0

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/backend/crypto/kmgr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef struct KmgrShmemData
4545
{
4646
CryptoKey intlKeys[KMGR_NUM_DATA_KEYS];
4747
} KmgrShmemData;
48-
static KmgrShmemData *KmgrShmem;
48+
static KmgrShmemData *KmgrShmem = NULL;
4949

5050
/* GUC variables */
5151
char *cluster_key_command = NULL;
@@ -218,7 +218,7 @@ BootStrapKmgr(void)
218218
Size
219219
KmgrShmemSize(void)
220220
{
221-
if (!FileEncryptionEnabled)
221+
if (!tde_force_switch)
222222
return 0;
223223

224224
return MAXALIGN(sizeof(KmgrShmemData));
@@ -230,7 +230,7 @@ KmgrShmemInit(void)
230230
{
231231
bool found;
232232

233-
if (!FileEncryptionEnabled)
233+
if (!tde_force_switch)
234234
return;
235235

236236
KmgrShmem = (KmgrShmemData *) ShmemInitStruct("File encryption key manager",

src/common/kmgr_utils.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,29 @@ bool
102102
kmgr_unwrap_data_key(PgCipherCtx *ctx, unsigned char *in, int inlen, CryptoKey *out)
103103
{
104104
int outlen;
105+
int out_buffer_len;
106+
unsigned char *out_buffer;
107+
108+
/*
109+
* When call EVP_DecryptUpdate,
110+
* We need to alloc enough buffer
111+
* More detail info see
112+
* https://www.openssl.org/docs/man3.1/man3/EVP_DecryptUpdate.html
113+
*/
114+
out_buffer_len = pg_cipher_blocksize(ctx) + inlen;
115+
out_buffer = (unsigned char *)palloc0(out_buffer_len);
105116

106117
Assert(ctx && in && out);
107118

108-
if (!pg_cipher_keyunwrap(ctx, in, inlen, (unsigned char *) out, &outlen))
119+
if (!pg_cipher_keyunwrap(ctx, in, inlen, (unsigned char *) out_buffer, &outlen))
109120
return false;
110121

111122
Assert(outlen == sizeof(CryptoKey));
112123

124+
memcpy(out, out_buffer, sizeof(CryptoKey));
125+
126+
pfree(out_buffer);
127+
113128
return true;
114129
}
115130

0 commit comments

Comments
 (0)