From 96d2d2418ac6b5d810ddbbcdf60e044e3b40374e Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Mon, 7 Oct 2024 18:13:50 +0200 Subject: [PATCH] Lock key during rpmGetSubkeys() This should be thread save as it still is in the public API. The internal use would be fine otherwise. So the lock can be removed if this gets made internal only. Resolves: #3351 --- rpmio/rpmkeyring.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/rpmio/rpmkeyring.cc b/rpmio/rpmkeyring.cc index 58f5bc90ee..0d8f922593 100644 --- a/rpmio/rpmkeyring.cc +++ b/rpmio/rpmkeyring.cc @@ -179,15 +179,21 @@ rpmPubkey *rpmGetSubkeys(rpmPubkey primarykey, int *count) int pgpsubkeysCount = 0; int i; - if (primarykey && !pgpPrtParamsSubkeys(primarykey->pkt.data(), primarykey->pkt.size(), - primarykey->pgpkey, &pgpsubkeys, &pgpsubkeysCount)) { - /* Returned to C, can't use new */ - subkeys = (rpmPubkey *)xmalloc(pgpsubkeysCount * sizeof(*subkeys)); - for (i = 0; i < pgpsubkeysCount; i++) { - subkeys[i] = rpmPubkeyNewSubkey(primarykey, pgpsubkeys[i]); - primarykey = pubkeyLink(primarykey); + if (primarykey) { + + rdlock lock(primarykey->mutex); + + if (!pgpPrtParamsSubkeys( + primarykey->pkt.data(), primarykey->pkt.size(), + primarykey->pgpkey, &pgpsubkeys, &pgpsubkeysCount)) { + /* Returned to C, can't use new */ + subkeys = (rpmPubkey *)xmalloc(pgpsubkeysCount * sizeof(*subkeys)); + for (i = 0; i < pgpsubkeysCount; i++) { + subkeys[i] = rpmPubkeyNewSubkey(primarykey, pgpsubkeys[i]); + primarykey = pubkeyLink(primarykey); + } + free(pgpsubkeys); } - free(pgpsubkeys); } *count = pgpsubkeysCount;