Skip to content

Commit 4d5fc5b

Browse files
committed
PG-1373 Do not use strcpy() for binary KMIP keys
This code is not only a potential security issue also a correctness issue for pincipal keys which contain NUL bytes. So instead we first check that the lenght is exactly correct before copying.
1 parent ed90a0a commit 4d5fc5b

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

contrib/pg_tde/src/keyring/keyring_kmip.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ get_key_by_name(GenericKeyring *keyring, const char *key_name, bool throw_error,
240240
key = palloc(sizeof(keyInfo));
241241

242242
{
243-
244243
char *keyp = NULL;
245244
int result = kmip_bio_get_symmetric_key(ctx.bio, id, strlen(id), &keyp, (int *) &key->data.len);
246245

@@ -254,7 +253,17 @@ get_key_by_name(GenericKeyring *keyring, const char *key_name, bool throw_error,
254253
return NULL;
255254
}
256255

257-
strncpy((char *) key->data.data, keyp, MAX_KEY_DATA_SIZE);
256+
if (key->data.len > sizeof(key->data.data))
257+
{
258+
kmip_ereport(throw_error, "Unexpected KMIP key length %d", key->data.len);
259+
pfree(key);
260+
BIO_free_all(ctx.bio);
261+
SSL_CTX_free(ctx.ssl);
262+
free(keyp);
263+
return NULL;
264+
}
265+
266+
memcpy(key->data.data, keyp, key->data.len);
258267
free(keyp);
259268
}
260269

0 commit comments

Comments
 (0)