Blob Blame History Raw
commit 59158656f3b0b99d8784ddc82c15778813000edc
Author: Frank Ch. Eigler <fche@redhat.com>
Date:   Wed May 4 10:26:42 2022 -0400

    PR29117: fix fd leak in debuginfod client for cache-miss files
    
    Correct a nasty fd leak and a few less nasty leaks in the debuginfod
    client code.  The nasty one impacts long-lived apps such as debuginfod
    servers.
    
    Signed-off-by: Mark Wielaard  <mark@klomp.org>
    Signed-off-by: Frank Ch. Eigler <fche@redhat.com>

diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index ea6e461a..521972e4 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -243,7 +243,13 @@ debuginfod_config_cache(char *config_path,
         return -errno;
 
       if (dprintf(fd, "%ld", cache_config_default_s) < 0)
-        return -errno;
+	{
+	  int ret = -errno;
+	  close (fd);
+	  return ret;
+	}
+
+      close (fd);
     }
 
   long cache_config;
@@ -284,7 +290,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_clean_default_interval_s) < 0)
-    return -errno;
+    {
+      int ret = -errno;
+      close (fd);
+      return ret;
+    }
+
+  close (fd);
 
   /* init max age config file.  */
   if (stat(maxage_path, &st) != 0
@@ -292,8 +304,13 @@ debuginfod_init_cache (char *cache_path, char *interval_path, char *maxage_path)
     return -errno;
 
   if (dprintf(fd, "%ld", cache_default_max_unused_age_s) < 0)
-    return -errno;
+    {
+      int ret = -errno;
+      close (fd);
+      return ret;
+    }
 
+  close (fd);
   return 0;
 }
 
@@ -812,18 +829,17 @@ debuginfod_query_server (debuginfod_client *c,
              has passed since the last attempt. */
           time_t cache_miss;
           time_t target_mtime = st.st_mtime;
+
+          close(fd); /* no need to hold onto the negative-hit file descriptor */
+          
           rc = debuginfod_config_cache(cache_miss_path,
                                        cache_miss_default_s, &st);
           if (rc < 0)
-            {
-              close(fd);
-              goto out;
-            }
+            goto out;
 
           cache_miss = (time_t)rc;
           if (time(NULL) - target_mtime <= cache_miss)
             {
-              close(fd);
               rc = -ENOENT;
               goto out;
             }
diff --git a/debuginfod/debuginfod-find.c b/debuginfod/debuginfod-find.c
index 3e8ab203..f60b5463 100644
--- a/debuginfod/debuginfod-find.c
+++ b/debuginfod/debuginfod-find.c
@@ -231,6 +231,8 @@ main(int argc, char** argv)
       fprintf(stderr, "Server query failed: %s\n", strerror(-rc));
       return 1;
     }
+  else
+    close (rc);
 
   printf("%s\n", cache_name);
   free (cache_name);