xbps{,-static}: fix races in the cache connection code of libfetch.

This commit is contained in:
Juan RP 2014-12-23 10:54:27 +01:00
parent 2db41dec99
commit 6cdd2708b3
4 changed files with 140 additions and 86 deletions

View File

@ -3,7 +3,7 @@
# NOTE: keep this package synchronized with "srcpkgs/xbps".
pkgname=xbps-static
version=0.42
revision=2
revision=3
build_style=configure
short_desc="The XBPS package system utilities - static binaries"
maintainer="Juan RP <xtraeme@gmail.com>"

View File

@ -0,0 +1,138 @@
From 4ee6f943ddb0d97fada7144f47780cc1d7f20f11 Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
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 <signal.h>
#endif
+#include <pthread.h>
+
#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);
}
/*
diff --git a/lib/plist_fetch.c b/lib/plist_fetch.c
index d9908ef..a153551 100644
--- a/lib/plist_fetch.c
+++ b/lib/plist_fetch.c
@@ -30,7 +30,6 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#include <pthread.h>
#include "xbps_api_impl.h"
@@ -44,7 +43,6 @@ struct fetch_archive {
struct url *url;
struct fetchIO *fetch;
char buffer[32768];
- pthread_mutex_t mtx;
};
static int
@@ -52,9 +50,7 @@ fetch_archive_open(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
- pthread_mutex_lock(&f->mtx);
f->fetch = fetchGet(f->url, NULL);
- pthread_mutex_unlock(&f->mtx);
if (f->fetch == NULL)
return ENOENT;
@@ -66,13 +62,9 @@ static ssize_t
fetch_archive_read(struct archive *a _unused, void *client_data, const void **buf)
{
struct fetch_archive *f = client_data;
- ssize_t res;
*buf = f->buffer;
- pthread_mutex_lock(&f->mtx);
- res = fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
- pthread_mutex_unlock(&f->mtx);
- return res;
+ return fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
}
static int
@@ -80,11 +72,8 @@ fetch_archive_close(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
- pthread_mutex_lock(&f->mtx);
if (f->fetch != NULL)
fetchIO_close(f->fetch);
- pthread_mutex_unlock(&f->mtx);
- pthread_mutex_destroy(&f->mtx);
free(f);
return 0;
@@ -101,7 +90,6 @@ open_archive_by_url(struct url *url)
return NULL;
f->url = url;
- pthread_mutex_init(&f->mtx, NULL);
if ((a = archive_read_new()) == NULL) {
free(f);
--
2.2.1

View File

@ -1,84 +0,0 @@
From 6a985190aa129f2a90a20d3f76205d092dffe76a Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Mon, 22 Dec 2014 18:18:20 +0100
Subject: [PATCH] xbps_archive_fetch_xxx: avoid races with multiple threads in
the libfetch code.
Protect our critical sections with a mutex for now, until libfetch
is really fixed to work correctly with multiple threads.
---
NEWS | 4 ++++
lib/plist_fetch.c | 17 +++++++++++++++--
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/lib/plist_fetch.c b/lib/plist_fetch.c
index 517c796..d9908ef 100644
--- lib/plist_fetch.c
+++ lib/plist_fetch.c
@@ -30,6 +30,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <pthread.h>
#include "xbps_api_impl.h"
@@ -43,6 +44,7 @@ struct fetch_archive {
struct url *url;
struct fetchIO *fetch;
char buffer[32768];
+ pthread_mutex_t mtx;
};
static int
@@ -50,7 +52,10 @@ fetch_archive_open(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
+ pthread_mutex_lock(&f->mtx);
f->fetch = fetchGet(f->url, NULL);
+ pthread_mutex_unlock(&f->mtx);
+
if (f->fetch == NULL)
return ENOENT;
@@ -61,10 +66,13 @@ static ssize_t
fetch_archive_read(struct archive *a _unused, void *client_data, const void **buf)
{
struct fetch_archive *f = client_data;
+ ssize_t res;
*buf = f->buffer;
-
- return fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
+ pthread_mutex_lock(&f->mtx);
+ res = fetchIO_read(f->fetch, f->buffer, sizeof(f->buffer));
+ pthread_mutex_unlock(&f->mtx);
+ return res;
}
static int
@@ -72,8 +80,11 @@ fetch_archive_close(struct archive *a _unused, void *client_data)
{
struct fetch_archive *f = client_data;
+ pthread_mutex_lock(&f->mtx);
if (f->fetch != NULL)
fetchIO_close(f->fetch);
+ pthread_mutex_unlock(&f->mtx);
+ pthread_mutex_destroy(&f->mtx);
free(f);
return 0;
@@ -90,6 +101,8 @@ open_archive_by_url(struct url *url)
return NULL;
f->url = url;
+ pthread_mutex_init(&f->mtx, NULL);
+
if ((a = archive_read_new()) == NULL) {
free(f);
return NULL;
--
2.2.1

View File

@ -1,7 +1,7 @@
# Template file for 'xbps'
pkgname=xbps
version=0.42
revision=2
revision=3
bootstrap=yes
build_style=configure
short_desc="The XBPS package system utilities"