Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take read-only transaction lock for keyring load #3406

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions lib/keystore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@

using std::string;

enum {
KEYRING_RPMDB = 1,
KEYRING_FS = 2,
};

static int makePubkeyHeader(rpmts ts, rpmPubkey key, Header * hdrp);

static int rpmtsLoadKeyringFromFiles(rpmts ts, rpmKeyring keyring)
static int rpmtsLoadKeyringFromFiles(rpmtxn txn, rpmKeyring keyring)
{
ARGV_t files = NULL;
/* XXX TODO: deal with chroot path issues */
char *pkpath = rpmGetPath(ts->rootDir, "%{_keyringpath}/*.key", NULL);
char *pkpath = rpmGetPath(rpmtxnRootDir(txn), "%{_keyringpath}/*.key", NULL);
int nkeys = 0;

rpmlog(RPMLOG_DEBUG, "loading keyring from pubkeys in %s\n", pkpath);
Expand Down Expand Up @@ -138,14 +133,14 @@ static rpmRC rpmtsImportFSKey(rpmtxn txn, rpmPubkey key, rpmFlags flags, int rep
return rc;
}

static int rpmtsLoadKeyringFromDB(rpmts ts, rpmKeyring keyring)
static int rpmtsLoadKeyringFromDB(rpmtxn txn, rpmKeyring keyring)
{
Header h;
rpmdbMatchIterator mi;
int nkeys = 0;

rpmlog(RPMLOG_DEBUG, "loading keyring from rpmdb\n");
mi = rpmtsInitIterator(ts, RPMDBI_NAME, "gpg-pubkey", 0);
mi = rpmtsInitIterator(rpmtxnTs(txn), RPMDBI_NAME, "gpg-pubkey", 0);
while ((h = rpmdbNextIterator(mi)) != NULL) {
struct rpmtd_s pubkeys;
const char *key;
Expand Down Expand Up @@ -410,32 +405,14 @@ rpmRC rpmKeystoreDeletePubkey(rpmtxn txn, rpmPubkey key)
return rc;
}

static int getKeyringType(void)
{
int kt = KEYRING_RPMDB;
char *krtype = rpmExpand("%{?_keyring}", NULL);

if (rstreq(krtype, "fs")) {
kt = KEYRING_FS;
} else if (*krtype && !rstreq(krtype, "rpmdb")) {
/* Fall back to using rpmdb if unknown, for now at least */
rpmlog(RPMLOG_WARNING,
_("unknown keyring type: %s, using rpmdb\n"), krtype);
}
free(krtype);

return kt;
}

int rpmKeystoreLoad(rpmts ts, rpmKeyring keyring)
int rpmKeystoreLoad(rpmtxn txn, rpmKeyring keyring)
{
int nkeys = 0;
if (!ts->keyringtype)
ts->keyringtype = getKeyringType();
rpmts ts = rpmtxnTs(txn);
if (ts->keyringtype == KEYRING_FS) {
nkeys = rpmtsLoadKeyringFromFiles(ts, keyring);
nkeys = rpmtsLoadKeyringFromFiles(txn, keyring);
} else {
nkeys = rpmtsLoadKeyringFromDB(ts, keyring);
nkeys = rpmtsLoadKeyringFromDB(txn, keyring);
}
return nkeys;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/keystore.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
#include <rpm/rpmtypes.h>
#include <rpm/rpmutil.h>

enum {
KEYRING_RPMDB = 1,
KEYRING_FS = 2,
};

RPM_GNUC_INTERNAL
int rpmKeystoreLoad(rpmts ts, rpmKeyring keyring);
int rpmKeystoreLoad(rpmtxn txn, rpmKeyring keyring);

RPM_GNUC_INTERNAL
rpmRC rpmKeystoreImportPubkey(rpmtxn txn, rpmPubkey key, int replace = 0);
Expand Down
25 changes: 24 additions & 1 deletion lib/rpmts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,36 @@ int rpmtsSetKeyring(rpmts ts, rpmKeyring keyring)
return 0;
}

static int getKeyringType(void)
{
int kt = KEYRING_RPMDB;
char *krtype = rpmExpand("%{?_keyring}", NULL);

if (rstreq(krtype, "fs")) {
kt = KEYRING_FS;
} else if (*krtype && !rstreq(krtype, "rpmdb")) {
/* Fall back to using rpmdb if unknown, for now at least */
rpmlog(RPMLOG_WARNING,
_("unknown keyring type: %s, using rpmdb\n"), krtype);
}
free(krtype);

return kt;
}

static void loadKeyring(rpmts ts)
{
/* Never load the keyring if signature checking is disabled */
if ((rpmtsVSFlags(ts) & RPMVSF_MASK_NOSIGNATURES) !=
RPMVSF_MASK_NOSIGNATURES) {
if (!ts->keyringtype)
ts->keyringtype = getKeyringType();
ts->keyring = rpmKeyringNew();
rpmKeystoreLoad(ts, ts->keyring);
rpmtxn txn = rpmtxnBegin(ts, RPMTXN_READ);
if (txn) {
rpmKeystoreLoad(txn, ts->keyring);
rpmtxnEnd(txn);
}
}
}

Expand Down
Loading