Blame SOURCES/0004-Remove-killGpgAgent-function-RhBug1781601.patch

bf4004
From dc3b2a2b22106fa4c0033a5557584f3b08c942e2 Mon Sep 17 00:00:00 2001
bf4004
From: Marek Blaha <mblaha@redhat.com>
bf4004
Date: Fri, 3 Jan 2020 12:35:33 +0100
bf4004
Subject: [PATCH] Remove killGpgAgent function (RhBug:1781601)
bf4004
bf4004
Instead ensure that /run/user/$UID directory exists so gpgagent could
bf4004
create its socket in it.
bf4004
The solution with KILLAGENT caused race condition with gpgme_release()
bf4004
call and that resulted in dnf being possibly terminated by SIGPIPE after
bf4004
importing the first repository gpg key.
bf4004
bf4004
https://bugzilla.redhat.com/show_bug.cgi?id=1781601
bf4004
---
bf4004
 libdnf/repo/Repo.cpp | 56 +++++++++++++++++++++++---------------------
bf4004
 1 file changed, 29 insertions(+), 27 deletions(-)
bf4004
bf4004
diff --git a/libdnf/repo/Repo.cpp b/libdnf/repo/Repo.cpp
bf4004
index 850e5b4a8..c1891cce9 100644
bf4004
--- a/libdnf/repo/Repo.cpp
bf4004
+++ b/libdnf/repo/Repo.cpp
bf4004
@@ -846,27 +846,35 @@ std::vector<Key> Repo::Impl::retrieve(const std::string & url)
bf4004
     return keyInfos;
bf4004
 }
bf4004
 
bf4004
-static void killGpgAgent(gpgme_ctx_t context, const std::string & gpgDir)
bf4004
-{
bf4004
+/*
bf4004
+ * Creates the '/run/user/$UID' directory if it doesn't exist. If this
bf4004
+ * directory exists, gpgagent will create its sockets under
bf4004
+ * '/run/user/$UID/gnupg'.
bf4004
+ *
bf4004
+ * If this directory doesn't exist, gpgagent will create its sockets in gpg
bf4004
+ * home directory, which is under '/var/cache/yum/metadata/' and this was
bf4004
+ * causing trouble with container images, see [1].
bf4004
+ *
bf4004
+ * Previous solution was to send the agent a "KILLAGENT" message, but that
bf4004
+ * would cause a race condition with calling gpgme_release(), see [2], [3],
bf4004
+ * [4].
bf4004
+ *
bf4004
+ * Since the agent doesn't clean up its sockets properly, by creating this
bf4004
+ * directory we make sure they are in a place that is not causing trouble with
bf4004
+ * container images.
bf4004
+ *
bf4004
+ * [1] https://bugzilla.redhat.com/show_bug.cgi?id=1650266
bf4004
+ * [2] https://bugzilla.redhat.com/show_bug.cgi?id=1769831
bf4004
+ * [3] https://github.com/rpm-software-management/microdnf/issues/50
bf4004
+ * [4] https://bugzilla.redhat.com/show_bug.cgi?id=1781601
bf4004
+ */
bf4004
+static void ensure_socket_dir_exists() {
bf4004
     auto logger(Log::getLogger());
bf4004
-
bf4004
-    auto gpgErr = gpgme_set_protocol(context, GPGME_PROTOCOL_ASSUAN);
bf4004
-    if (gpgErr != GPG_ERR_NO_ERROR) {
bf4004
-        auto msg = tfm::format(_("%s: gpgme_set_protocol(): %s"), __func__, gpgme_strerror(gpgErr));
bf4004
-        logger->warning(msg);
bf4004
-        return;
bf4004
-    }
bf4004
-    std::string gpgAgentSock = gpgDir + "/S.gpg-agent";
bf4004
-    gpgErr = gpgme_ctx_set_engine_info(context, GPGME_PROTOCOL_ASSUAN, gpgAgentSock.c_str(), gpgDir.c_str());
bf4004
-    if (gpgErr != GPG_ERR_NO_ERROR) {
bf4004
-        auto msg = tfm::format(_("%s: gpgme_ctx_set_engine_info(): %s"), __func__, gpgme_strerror(gpgErr));
bf4004
-        logger->warning(msg);
bf4004
-        return;
bf4004
-    }
bf4004
-    gpgErr = gpgme_op_assuan_transact_ext(context, "KILLAGENT", NULL, NULL, NULL, NULL, NULL, NULL, NULL);
bf4004
-    if (gpgErr != GPG_ERR_NO_ERROR) {
bf4004
-        auto msg = tfm::format(_("%s: gpgme_op_assuan_transact_ext(): %s"), __func__, gpgme_strerror(gpgErr));
bf4004
-        logger->debug(msg);
bf4004
+    std::string dirname = "/run/user/" + std::to_string(getuid());
bf4004
+    int res = mkdir(dirname.c_str(), 0700);
bf4004
+    if (res != 0 && errno != EEXIST) {
bf4004
+        logger->debug(tfm::format("Failed to create directory \"%s\": %d - %s",
bf4004
+                                  dirname, errno, strerror(errno)));
bf4004
     }
bf4004
 }
bf4004
 
bf4004
@@ -876,6 +884,7 @@ void Repo::Impl::importRepoKeys()
bf4004
 
bf4004
     auto gpgDir = getCachedir() + "/pubring";
bf4004
     auto knownKeys = keyidsFromPubring(gpgDir);
bf4004
+    ensure_socket_dir_exists();
bf4004
     for (const auto & gpgkeyUrl : conf->gpgkey().getValue()) {
bf4004
         auto keyInfos = retrieve(gpgkeyUrl);
bf4004
         for (auto & keyInfo : keyInfos) {
bf4004
@@ -908,13 +917,6 @@ void Repo::Impl::importRepoKeys()
bf4004
 
bf4004
             gpgImportKey(ctx, keyInfo.rawKey);
bf4004
 
bf4004
-            // Running gpg-agent kept opened sockets on the system.
bf4004
-            // It tries to exit gpg-agent. Path to the communication socket is derived from homedir.
bf4004
-            // The gpg-agent automaticaly removes all its socket before exit.
bf4004
-            // Newer gpg-agent creates sockets under [/var]/run/user/{pid}/... if directory exists.
bf4004
-            // In this case gpg-agent will not be exited.
bf4004
-            killGpgAgent(ctx, gpgDir);
bf4004
-
bf4004
             logger->debug(tfm::format(_("repo %s: imported key 0x%s."), id, keyInfo.getId()));
bf4004
         }
bf4004