Blame elfutils-0.187-debuginfod-client-fd-leak.patch

Mark Wielaard da3f26
commit 59158656f3b0b99d8784ddc82c15778813000edc
Mark Wielaard da3f26
Author: Frank Ch. Eigler <fche@redhat.com>
Mark Wielaard da3f26
Date:   Wed May 4 10:26:42 2022 -0400
Mark Wielaard da3f26
Mark Wielaard da3f26
    PR29117: fix fd leak in debuginfod client for cache-miss files
Mark Wielaard da3f26
    
Mark Wielaard da3f26
    Correct a nasty fd leak and a few less nasty leaks in the debuginfod
Mark Wielaard da3f26
    client code.  The nasty one impacts long-lived apps such as debuginfod
Mark Wielaard da3f26
    servers.
Mark Wielaard da3f26
    
Mark Wielaard da3f26
    Signed-off-by: Mark Wielaard  <mark@klomp.org>
Mark Wielaard da3f26
    Signed-off-by: Frank Ch. Eigler <fche@redhat.com>
Mark Wielaard da3f26
Mark Wielaard da3f26
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
Mark Wielaard da3f26
index ea6e461a..521972e4 100644
Mark Wielaard da3f26
--- a/debuginfod/debuginfod-client.c
Mark Wielaard da3f26
+++ b/debuginfod/debuginfod-client.c
Mark Wielaard da3f26
@@ -243,7 +243,13 @@ debuginfod_config_cache(char *config_path,
Mark Wielaard da3f26
         return -errno;
Mark Wielaard da3f26
 
Mark Wielaard da3f26
       if (dprintf(fd, "%ld", cache_config_default_s) < 0)
Mark Wielaard da3f26
-        return -errno;
Mark Wielaard da3f26
+	{
Mark Wielaard da3f26
+	  int ret = -errno;
Mark Wielaard da3f26
+	  close (fd);
Mark Wielaard da3f26
+	  return ret;
Mark Wielaard da3f26
+	}
Mark Wielaard da3f26
+
Mark Wielaard da3f26
+      close (fd);
Mark Wielaard da3f26
     }
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   long cache_config;
Mark Wielaard da3f26
@@ -284,7 +290,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
Mark Wielaard da3f26
     return -errno;
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   if (dprintf(fd, "%ld", cache_clean_default_interval_s) < 0)
Mark Wielaard da3f26
-    return -errno;
Mark Wielaard da3f26
+    {
Mark Wielaard da3f26
+      int ret = -errno;
Mark Wielaard da3f26
+      close (fd);
Mark Wielaard da3f26
+      return ret;
Mark Wielaard da3f26
+    }
Mark Wielaard da3f26
+
Mark Wielaard da3f26
+  close (fd);
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   /* init max age config file.  */
Mark Wielaard da3f26
   if (stat(maxage_path, &st) != 0
Mark Wielaard da3f26
@@ -292,8 +304,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
Mark Wielaard da3f26
     return -errno;
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
Mark Wielaard da3f26
-    return -errno;
Mark Wielaard da3f26
+    {
Mark Wielaard da3f26
+      int ret = -errno;
Mark Wielaard da3f26
+      close (fd);
Mark Wielaard da3f26
+      return ret;
Mark Wielaard da3f26
+    }
Mark Wielaard da3f26
 
Mark Wielaard da3f26
+  close (fd);
Mark Wielaard da3f26
   return 0;
Mark Wielaard da3f26
 }
Mark Wielaard da3f26
 
Mark Wielaard da3f26
@@ -812,18 +829,17 @@ debuginfod_query_server (debuginfod_client *c,
Mark Wielaard da3f26
              has passed since the last attempt. */
Mark Wielaard da3f26
           time_t cache_miss;
Mark Wielaard da3f26
           time_t target_mtime = st.st_mtime;
Mark Wielaard da3f26
+
Mark Wielaard da3f26
+          close(fd); /* no need to hold onto the negative-hit file descriptor */
Mark Wielaard da3f26
+          
Mark Wielaard da3f26
           rc = debuginfod_config_cache(cache_miss_path,
Mark Wielaard da3f26
                                        cache_miss_default_s, &st);
Mark Wielaard da3f26
           if (rc < 0)
Mark Wielaard da3f26
-            {
Mark Wielaard da3f26
-              close(fd);
Mark Wielaard da3f26
-              goto out;
Mark Wielaard da3f26
-            }
Mark Wielaard da3f26
+            goto out;
Mark Wielaard da3f26
 
Mark Wielaard da3f26
           cache_miss = (time_t)rc;
Mark Wielaard da3f26
           if (time(NULL) - target_mtime <= cache_miss)
Mark Wielaard da3f26
             {
Mark Wielaard da3f26
-              close(fd);
Mark Wielaard da3f26
               rc = -ENOENT;
Mark Wielaard da3f26
               goto out;
Mark Wielaard da3f26
             }
Mark Wielaard da3f26
diff --git a/debuginfod/debuginfod-find.c b/debuginfod/debuginfod-find.c
Mark Wielaard da3f26
index 3e8ab203..f60b5463 100644
Mark Wielaard da3f26
--- a/debuginfod/debuginfod-find.c
Mark Wielaard da3f26
+++ b/debuginfod/debuginfod-find.c
Mark Wielaard da3f26
@@ -231,6 +231,8 @@ main(int argc, char** argv)
Mark Wielaard da3f26
       fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
Mark Wielaard da3f26
       return 1;
Mark Wielaard da3f26
     }
Mark Wielaard da3f26
+  else
Mark Wielaard da3f26
+    close (rc);
Mark Wielaard da3f26
 
Mark Wielaard da3f26
   printf("%s\n", cache_name);
Mark Wielaard da3f26
   free (cache_name);