From 4ee6f943ddb0d97fada7144f47780cc1d7f20f11 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 23 Dec 2014 10:52:54 +0100 Subject: [PATCH] libfetch: fix races in the cache connection code. Tested by @Gottox. --- NEWS | 4 +--- lib/fetch/common.c | 9 +++++++++ lib/plist_fetch.c | 14 +------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/fetch/common.c b/lib/fetch/common.c index 1ea017b..edd8e89 100644 --- lib/fetch/common.c +++ lib/fetch/common.c @@ -66,6 +66,8 @@ #include #endif +#include + #include "fetch.h" #include "common.h" @@ -333,6 +335,7 @@ fetch_connect(struct url *url, int af, int verbose) return (conn); } +static pthread_mutex_t cache_mtx = PTHREAD_MUTEX_INITIALIZER; static conn_t *connection_cache; static int cache_global_limit = 0; static int cache_per_host_limit = 0; @@ -379,6 +382,7 @@ fetch_cache_get(const struct url *url, int af) { conn_t *conn, *last_conn = NULL; + pthread_mutex_lock(&cache_mtx); for (conn = connection_cache; conn; conn = conn->next_cached) { if (conn->cache_url->port == url->port && strcmp(conn->cache_url->scheme, url->scheme) == 0 && @@ -391,9 +395,12 @@ fetch_cache_get(const struct url *url, int af) last_conn->next_cached = conn->next_cached; else connection_cache = conn->next_cached; + + pthread_mutex_unlock(&cache_mtx); return conn; } } + pthread_mutex_unlock(&cache_mtx); return NULL; } @@ -414,6 +421,7 @@ fetch_cache_put(conn_t *conn, int (*closecb)(conn_t *)) return; } + pthread_mutex_lock(&cache_mtx); global_count = host_count = 0; last = NULL; for (iter = connection_cache; iter; @@ -435,6 +443,7 @@ fetch_cache_put(conn_t *conn, int (*closecb)(conn_t *)) conn->cache_close = closecb; conn->next_cached = connection_cache; connection_cache = conn; + pthread_mutex_unlock(&cache_mtx); } /* -- 2.2.1