From: Frank Ch. Eigler Date: Wed, 4 May 2022 14:26:42 +0000 (-0400) Subject: PR29117: fix fd leak in debuginfod client for cache-miss files X-Git-Tag: elfutils-0.188~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59158656f3b0b99d8784ddc82c15778813000edc;p=thirdparty%2Felfutils.git 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 Signed-off-by: Frank Ch. Eigler --- diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog index 93c8ae114..619ebd8c9 100644 --- a/debuginfod/ChangeLog +++ b/debuginfod/ChangeLog @@ -1,3 +1,12 @@ +2022-05-04 Frank Ch. Eigler + Mark Wielaard + + * debuginfod-client.c (debuginfod_query_server): Correct fd leak + for cache negative-hit unlink case. + (debuginfod_config_cache, debuginfod_init_cache): Correct + minor fd leaks. + * debuginfod-find.c (main): Ditto. + 2022-04-22 Mark Wielaard * Makefile.am (libdebuginfod): Add -lpthread. diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c index ea6e461a7..521972e4a 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 3e8ab203d..f60b5463c 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);