Blame elfutils-0.188-debuginfod-client-lifetime.patch

Mark Wielaard 34ba07
commit c424e5f3d24f76e01242d15ba361dc6234706fed
Mark Wielaard 34ba07
Author: Frank Ch. Eigler <fche@redhat.com>
Mark Wielaard 34ba07
Date:   Thu Nov 3 10:07:31 2022 -0400
Mark Wielaard 34ba07
Mark Wielaard 34ba07
    debuginfod.cxx: fix coverity-found use-after-release error
Mark Wielaard 34ba07
    
Mark Wielaard 34ba07
    The debuginfod_client object lifetime needs more careful handling,
Mark Wielaard 34ba07
    made easier with the defer_dtor<> gadget.
Mark Wielaard 34ba07
    
Mark Wielaard 34ba07
    Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
Mark Wielaard 34ba07
Mark Wielaard 34ba07
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
Mark Wielaard 34ba07
index f46da6ef..02a11477 100644
Mark Wielaard 34ba07
--- a/debuginfod/debuginfod.cxx
Mark Wielaard 34ba07
+++ b/debuginfod/debuginfod.cxx
Mark Wielaard 34ba07
@@ -2249,85 +2249,82 @@ handle_buildid (MHD_Connection* conn,
Mark Wielaard 34ba07
 
Mark Wielaard 34ba07
   int fd = -1;
Mark Wielaard 34ba07
   debuginfod_client *client = debuginfod_pool_begin ();
Mark Wielaard 34ba07
-  if (client != NULL)
Mark Wielaard 34ba07
-    {
Mark Wielaard 34ba07
-      debuginfod_set_progressfn (client, & debuginfod_find_progress);
Mark Wielaard 34ba07
+  if (client == NULL)
Mark Wielaard 34ba07
+    throw libc_exception(errno, "debuginfod client pool alloc");
Mark Wielaard 34ba07
+  defer_dtor<debuginfod_client*,void> client_closer (client, debuginfod_pool_end);
Mark Wielaard 34ba07
+  
Mark Wielaard 34ba07
+  debuginfod_set_progressfn (client, & debuginfod_find_progress);
Mark Wielaard 34ba07
 
Mark Wielaard 34ba07
-      if (conn)
Mark Wielaard 34ba07
-        {
Mark Wielaard 34ba07
-          // Transcribe incoming User-Agent:
Mark Wielaard 34ba07
-          string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
Mark Wielaard 34ba07
-          string ua_complete = string("User-Agent: ") + ua;
Mark Wielaard 34ba07
-          debuginfod_add_http_header (client, ua_complete.c_str());
Mark Wielaard 34ba07
-
Mark Wielaard 34ba07
-          // Compute larger XFF:, for avoiding info loss during
Mark Wielaard 34ba07
-          // federation, and for future cyclicity detection.
Mark Wielaard 34ba07
-          string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
Mark Wielaard 34ba07
-          if (xff != "")
Mark Wielaard 34ba07
-            xff += string(", "); // comma separated list
Mark Wielaard 34ba07
-
Mark Wielaard 34ba07
-          unsigned int xff_count = 0;
Mark Wielaard 34ba07
-          for (auto&& i : xff){
Mark Wielaard 34ba07
-            if (i == ',') xff_count++;
Mark Wielaard 34ba07
-          }
Mark Wielaard 34ba07
+  if (conn)
Mark Wielaard 34ba07
+    {
Mark Wielaard 34ba07
+      // Transcribe incoming User-Agent:
Mark Wielaard 34ba07
+      string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
Mark Wielaard 34ba07
+      string ua_complete = string("User-Agent: ") + ua;
Mark Wielaard 34ba07
+      debuginfod_add_http_header (client, ua_complete.c_str());
Mark Wielaard 34ba07
+      
Mark Wielaard 34ba07
+      // Compute larger XFF:, for avoiding info loss during
Mark Wielaard 34ba07
+      // federation, and for future cyclicity detection.
Mark Wielaard 34ba07
+      string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
Mark Wielaard 34ba07
+      if (xff != "")
Mark Wielaard 34ba07
+        xff += string(", "); // comma separated list
Mark Wielaard 34ba07
+      
Mark Wielaard 34ba07
+      unsigned int xff_count = 0;
Mark Wielaard 34ba07
+      for (auto&& i : xff){
Mark Wielaard 34ba07
+        if (i == ',') xff_count++;
Mark Wielaard 34ba07
+      }
Mark Wielaard 34ba07
 
Mark Wielaard 34ba07
-          // if X-Forwarded-For: exceeds N hops,
Mark Wielaard 34ba07
-          // do not delegate a local lookup miss to upstream debuginfods.
Mark Wielaard 34ba07
-          if (xff_count >= forwarded_ttl_limit)
Mark Wielaard 34ba07
-            throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
Mark Wielaard 34ba07
+      // if X-Forwarded-For: exceeds N hops,
Mark Wielaard 34ba07
+      // do not delegate a local lookup miss to upstream debuginfods.
Mark Wielaard 34ba07
+      if (xff_count >= forwarded_ttl_limit)
Mark Wielaard 34ba07
+        throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
Mark Wielaard 34ba07
 and will not query the upstream servers");
Mark Wielaard 34ba07
 
Mark Wielaard 34ba07
-          // Compute the client's numeric IP address only - so can't merge with conninfo()
Mark Wielaard 34ba07
-          const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
Mark Wielaard 34ba07
-                                                                       MHD_CONNECTION_INFO_CLIENT_ADDRESS);
Mark Wielaard 34ba07
-          struct sockaddr *so = u ? u->client_addr : 0;
Mark Wielaard 34ba07
-          char hostname[256] = ""; // RFC1035
Mark Wielaard 34ba07
-          if (so && so->sa_family == AF_INET) {
Mark Wielaard 34ba07
-            (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
-                                NI_NUMERICHOST);
Mark Wielaard 34ba07
-          } else if (so && so->sa_family == AF_INET6) {
Mark Wielaard 34ba07
-            struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
Mark Wielaard 34ba07
-            if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
Mark Wielaard 34ba07
-              struct sockaddr_in addr4;
Mark Wielaard 34ba07
-              memset (&addr4, 0, sizeof(addr4));
Mark Wielaard 34ba07
-              addr4.sin_family = AF_INET;
Mark Wielaard 34ba07
-              addr4.sin_port = addr6->sin6_port;
Mark Wielaard 34ba07
-              memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
Mark Wielaard 34ba07
-              (void) getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
Mark Wielaard 34ba07
-                                  hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
-                                  NI_NUMERICHOST);
Mark Wielaard 34ba07
-            } else {
Mark Wielaard 34ba07
-              (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
-                                  NI_NUMERICHOST);
Mark Wielaard 34ba07
-            }
Mark Wielaard 34ba07
-          }
Mark Wielaard 34ba07
-          
Mark Wielaard 34ba07
-          string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
Mark Wielaard 34ba07
-          debuginfod_add_http_header (client, xff_complete.c_str());
Mark Wielaard 34ba07
+      // Compute the client's numeric IP address only - so can't merge with conninfo()
Mark Wielaard 34ba07
+      const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
Mark Wielaard 34ba07
+                                                                   MHD_CONNECTION_INFO_CLIENT_ADDRESS);
Mark Wielaard 34ba07
+      struct sockaddr *so = u ? u->client_addr : 0;
Mark Wielaard 34ba07
+      char hostname[256] = ""; // RFC1035
Mark Wielaard 34ba07
+      if (so && so->sa_family == AF_INET) {
Mark Wielaard 34ba07
+        (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
+                            NI_NUMERICHOST);
Mark Wielaard 34ba07
+      } else if (so && so->sa_family == AF_INET6) {
Mark Wielaard 34ba07
+        struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
Mark Wielaard 34ba07
+        if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
Mark Wielaard 34ba07
+          struct sockaddr_in addr4;
Mark Wielaard 34ba07
+          memset (&addr4, 0, sizeof(addr4));
Mark Wielaard 34ba07
+          addr4.sin_family = AF_INET;
Mark Wielaard 34ba07
+          addr4.sin_port = addr6->sin6_port;
Mark Wielaard 34ba07
+          memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
Mark Wielaard 34ba07
+          (void) getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
Mark Wielaard 34ba07
+                              hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
+                              NI_NUMERICHOST);
Mark Wielaard 34ba07
+        } else {
Mark Wielaard 34ba07
+          (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
Mark Wielaard 34ba07
+                              NI_NUMERICHOST);
Mark Wielaard 34ba07
         }
Mark Wielaard 34ba07
-
Mark Wielaard 34ba07
-      if (artifacttype == "debuginfo")
Mark Wielaard 34ba07
-	fd = debuginfod_find_debuginfo (client,
Mark Wielaard 34ba07
-					(const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
-					0, NULL);
Mark Wielaard 34ba07
-      else if (artifacttype == "executable")
Mark Wielaard 34ba07
-	fd = debuginfod_find_executable (client,
Mark Wielaard 34ba07
-					 (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
-					 0, NULL);
Mark Wielaard 34ba07
-      else if (artifacttype == "source")
Mark Wielaard 34ba07
-	fd = debuginfod_find_source (client,
Mark Wielaard 34ba07
-				     (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
-				     0, suffix.c_str(), NULL);
Mark Wielaard 34ba07
-      else if (artifacttype == "section")
Mark Wielaard 34ba07
-	fd = debuginfod_find_section (client,
Mark Wielaard 34ba07
-				      (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
-				      0, section.c_str(), NULL);
Mark Wielaard 34ba07
-
Mark Wielaard 34ba07
+      }
Mark Wielaard 34ba07
+          
Mark Wielaard 34ba07
+      string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
Mark Wielaard 34ba07
+      debuginfod_add_http_header (client, xff_complete.c_str());
Mark Wielaard 34ba07
     }
Mark Wielaard 34ba07
-  else
Mark Wielaard 34ba07
-    fd = -errno; /* Set by debuginfod_begin.  */
Mark Wielaard 34ba07
-  debuginfod_pool_end (client);
Mark Wielaard 34ba07
-
Mark Wielaard 34ba07
+  
Mark Wielaard 34ba07
+  if (artifacttype == "debuginfo")
Mark Wielaard 34ba07
+    fd = debuginfod_find_debuginfo (client,
Mark Wielaard 34ba07
+                                    (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
+                                    0, NULL);
Mark Wielaard 34ba07
+  else if (artifacttype == "executable")
Mark Wielaard 34ba07
+    fd = debuginfod_find_executable (client,
Mark Wielaard 34ba07
+                                     (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
+                                     0, NULL);
Mark Wielaard 34ba07
+  else if (artifacttype == "source")
Mark Wielaard 34ba07
+    fd = debuginfod_find_source (client,
Mark Wielaard 34ba07
+                                 (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
+                                 0, suffix.c_str(), NULL);
Mark Wielaard 34ba07
+  else if (artifacttype == "section")
Mark Wielaard 34ba07
+    fd = debuginfod_find_section (client,
Mark Wielaard 34ba07
+                                  (const unsigned char*) buildid.c_str(),
Mark Wielaard 34ba07
+                                  0, section.c_str(), NULL);
Mark Wielaard 34ba07
+  
Mark Wielaard 34ba07
   if (fd >= 0)
Mark Wielaard 34ba07
     {
Mark Wielaard 34ba07
       if (conn != 0)