Do not allocate internal keys in top memory context

Since all places which use the keys first copy them into long-lived
memory we do not need to do that when allocating.
pull/209/head
Andreas Karlsson 7 months ago committed by Andreas Karlsson
parent 6baab0917f
commit 00d2056bee
  1. 8
      contrib/pg_tde/src/access/pg_tde_tdemap.c
  2. 13
      contrib/pg_tde/src/encryption/enc_tde.c
  3. 1
      contrib/pg_tde/src/include/access/pg_tde_tdemap.h

@ -123,6 +123,7 @@ RelKeyCache tde_rel_key_cache = {
static WALKeyCacheRec *tde_wal_key_cache = NULL;
static WALKeyCacheRec *tde_wal_key_last_rec = NULL;
static InternalKey *pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool no_map_ok);
static int32 pg_tde_process_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, off_t *offset, bool should_delete);
static InternalKey *pg_tde_read_keydata(char *db_keydata_path, int32 key_index, TDEPrincipalKey *principal_key);
static InternalKey *tde_decrypt_rel_key(TDEPrincipalKey *principal_key, InternalKey *enc_rel_key_data, Oid dbOid);
@ -978,12 +979,11 @@ pg_tde_wal_last_key_set_lsn(XLogRecPtr lsn, const char *keyfile_path)
* Reads the key of the required relation. It identifies its map entry and then simply
* reads the key data from the keydata file.
*/
InternalKey *
static InternalKey *
pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool no_map_ok)
{
int32 key_index = 0;
TDEPrincipalKey *principal_key;
InternalKey *rel_key_data;
InternalKey *enc_rel_key_data;
off_t offset = 0;
LWLock *lock_pk = tde_lwlock_enc_keys();
@ -1037,9 +1037,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool n
enc_rel_key_data = pg_tde_read_keydata(db_keydata_path, key_index, principal_key);
LWLockRelease(lock_pk);
rel_key_data = tde_decrypt_rel_key(principal_key, enc_rel_key_data, rlocator->dbOid);
return rel_key_data;
return tde_decrypt_rel_key(principal_key, enc_rel_key_data, rlocator->dbOid);
}
/*

@ -1,7 +1,6 @@
#include "pg_tde_defines.h"
#include "postgres.h"
#include "utils/memutils.h"
#include "access/pg_tde_tdemap.h"
#include "encryption/enc_tde.h"
@ -194,25 +193,13 @@ AesDecryptKey(const TDEPrincipalKey *principal_key, Oid dbOid, InternalKey **p_r
{
unsigned char iv[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
#ifndef FRONTEND
MemoryContext oldcontext;
#endif
/* Ensure we are getting a valid pointer here */
Assert(principal_key);
memcpy(iv, &dbOid, sizeof(Oid));
#ifndef FRONTEND
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
#endif
*p_rel_key_data = (InternalKey *) palloc(sizeof(InternalKey));
#ifndef FRONTEND
MemoryContextSwitchTo(oldcontext);
#endif
/* Fill in the structure */
memcpy(*p_rel_key_data, enc_rel_key_data, sizeof(InternalKey));
(*p_rel_key_data)->ctx = NULL;

@ -108,7 +108,6 @@ extern TDEPrincipalKeyInfo *pg_tde_get_principal_key_info(Oid dbOid);
extern bool pg_tde_save_principal_key(TDEPrincipalKeyInfo *principal_key_info, bool truncate_existing, bool update_header);
extern bool pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_principal_key);
extern bool pg_tde_write_map_keydata_files(off_t map_size, char *m_file_data, off_t keydata_size, char *k_file_data);
extern InternalKey *pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool no_map_ok);
const char *tde_sprint_key(InternalKey *k);

Loading…
Cancel
Save