Skip to content

Commit c71271e

Browse files
kongfanshenmy-ship-it
authored andcommitted
Fix: tde encrypt buffer context don't use share memory
Now we Fix: tde encrypt buffer context don't use share memory We did not calculate the size of shared memory for BufEncCtx and BufDecCtx in the CreateSharedMemoryAndMemories function, which is a potential issue. There is actually no need to use shared memory here, as the parent process does not read the values of BufEncCtx and BufDecCtx. When we use AES algorithm the BufEncCtx and BufDecCtx also does not use shared memory. In order to maintain a consistent code style,we use malloc. cann't use palloc to apply for memory here, as TopmemoryContext is NULL during initialization and the memoryContext has not been setted yet.
1 parent 66eedec commit c71271e

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/backend/crypto/bufenc.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static unsigned char buf_encryption_iv[BUFENC_IV_SIZE];
3636

3737
void *BufEncCtx = NULL;
3838
void *BufDecCtx = NULL;
39+
static sm4_ctx sm4_enc_ctx;
40+
static sm4_ctx sm4_dec_ctx;
3941

4042
static void set_buffer_encryption_iv(Page page, BlockNumber blkno);
4143

@@ -47,16 +49,28 @@ InitializeBufferEncryption(void)
4749
if (!FileEncryptionEnabled)
4850
return;
4951

52+
/*
53+
* To avoid memory leaks, when the postmaster resets the cluster,
54+
* need to release the memory which was alloced at last time.
55+
*/
56+
if (BufEncCtx && BufEncCtx != &sm4_enc_ctx)
57+
{
58+
pg_cipher_ctx_free(BufEncCtx);
59+
BufEncCtx = NULL;
60+
}
61+
if (BufDecCtx && BufDecCtx != &sm4_dec_ctx)
62+
{
63+
pg_cipher_ctx_free(BufDecCtx);
64+
BufDecCtx = NULL;
65+
}
66+
5067
key = KmgrGetKey(KMGR_KEY_ID_REL);
5168

5269
if (CheckIsSM4Method())
5370
{
54-
bool found;
55-
BufEncCtx = ShmemInitStruct("sm4 encryption method encrypt ctx",
56-
sizeof(sm4_ctx), &found);
71+
BufEncCtx = &sm4_enc_ctx;
72+
BufDecCtx = &sm4_dec_ctx;
5773

58-
BufDecCtx = ShmemInitStruct("sm4 encryption method decrypt ctx",
59-
sizeof(sm4_ctx), &found);
6074
sm4_ofb_setkey_enc((sm4_ctx *)BufEncCtx, (unsigned char *)key->key);
6175
sm4_ofb_setkey_dec((sm4_ctx *)BufDecCtx, (unsigned char *)key->key);
6276
}

0 commit comments

Comments
 (0)