From 6c9b82e72ac067207b1d66a3112485ad8d690f32 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 11 Jun 2011 12:50:15 -0400 Subject: dload: handle irregular URLs URLs might end with a slash and follow redirects, or could be a generated by a script such as /getpkg.php?id=12345. In both cases, we may have a better filename that we can write to, taken from either content-disposition header, or the effective URL. Specific to the first case, we write to a temporary file of the format 'alpmtmp.XXXXXX', where XXXXXX is randomized by mkstemp(3). Since this is a randomly generated file, we cannot support resuming and the file is unlinked in the event of an interrupt. We also run into the possibility of changing out the filename from under alpm on a -U operation, so callers of _alpm_download can optionally pass a pointer to a *char to be filled in by curl_download_internal with the actual filename we wrote to. Any sync operation will pass a NULL pointer here, as we rely on specific names for packages from a mirror. Fixes FS#22645. Signed-off-by: Dave Reisner --- lib/libalpm/sync.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/libalpm/sync.c') diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 1d712797..54b96a7a 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -805,7 +805,7 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) CALLOC(fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); snprintf(fileurl, len, "%s/%s", server_url, filename); - ret = _alpm_download(handle, fileurl, cachedir, 0, 1, 0); + ret = _alpm_download(handle, fileurl, cachedir, NULL, 0, 1, 0); FREE(fileurl); if(ret != -1) { break; -- cgit v1.2.3-24-g4f1b From 6dc71926f9b16ebcf11b924941092d6eab204224 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Fri, 24 Jun 2011 00:18:01 -0400 Subject: lib/dload: prevent large file attacks This means creating a new struct which can pass more descriptive data from the back end sync functions to the downloader. In particular, we're interested in the download size read from the sync DB. When the remote server reports a size larger than this (via a content-length header), abort the transfer. In cases where the size is unknown, we set a hard upper limit of: * 25MiB for a sync DB * 16KiB for a signature For reference, 25MiB is more than twice the size of all of the current binary repos (with files) combined, and 16KiB is a truly gargantuan signature. Signed-off-by: Dave Reisner --- lib/libalpm/sync.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'lib/libalpm/sync.c') diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 54b96a7a..969af3c0 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -765,10 +765,6 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) alpm_pkg_t *spkg = j->data; if(spkg->origin != PKG_FROM_FILE && current == spkg->origin_data.db) { - const char *fname = NULL; - - fname = alpm_pkg_get_filename(spkg); - ASSERT(fname != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1)); alpm_list_t *delta_path = spkg->delta_path; if(delta_path) { /* using deltas */ @@ -776,14 +772,27 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) for(dlts = delta_path; dlts; dlts = dlts->next) { alpm_delta_t *delta = dlts->data; if(delta->download_size != 0) { - files = alpm_list_add(files, strdup(delta->delta)); + struct dload_payload *dpayload; + + CALLOC(dpayload, 1, sizeof(*dpayload), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + STRDUP(dpayload->filename, delta->delta, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + dpayload->max_size = delta->download_size; + + files = alpm_list_add(files, dpayload); } /* keep a list of all the delta files for md5sums */ *deltas = alpm_list_add(*deltas, delta); } } else if(spkg->download_size != 0) { - files = alpm_list_add(files, strdup(fname)); + struct dload_payload *payload; + + ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1)); + CALLOC(payload, 1, sizeof(*payload), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + STRDUP(payload->filename, spkg->filename, RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + payload->max_size = alpm_pkg_get_size(spkg); + + files = alpm_list_add(files, payload); } } @@ -792,21 +801,19 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) if(files) { EVENT(handle->trans, ALPM_TRANS_EVT_RETRIEVE_START, current->treename, NULL); for(j = files; j; j = j->next) { - const char *filename = j->data; + struct dload_payload *payload = j->data; alpm_list_t *server; int ret = -1; for(server = current->servers; server; server = server->next) { const char *server_url = server->data; - char *fileurl; size_t len; /* print server + filename into a buffer */ - len = strlen(server_url) + strlen(filename) + 2; - CALLOC(fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); - snprintf(fileurl, len, "%s/%s", server_url, filename); + len = strlen(server_url) + strlen(payload->filename) + 2; + CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); + snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename); - ret = _alpm_download(handle, fileurl, cachedir, NULL, 0, 1, 0); - FREE(fileurl); + ret = _alpm_download(handle, payload, cachedir, NULL, 0, 1, 0); if(ret != -1) { break; } @@ -816,7 +823,9 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) } } - FREELIST(files); + alpm_list_free_inner(files, (alpm_list_fn_free)_alpm_dload_payload_free); + alpm_list_free(files); + files = NULL; if(errors) { _alpm_log(handle, ALPM_LOG_WARNING, _("failed to retrieve some files from %s\n"), current->treename); -- cgit v1.2.3-24-g4f1b From 3eec745910bb908717a8b4ed7f5b630a92a5c9eb Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 30 Jun 2011 21:19:25 -0400 Subject: absorb some _alpm_download params into payload struct Restore some sanity to the number of arguments passed to _alpm_download and curl_download_internal. Signed-off-by: Dave Reisner --- lib/libalpm/sync.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/libalpm/sync.c') diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 969af3c0..ca7e6579 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -812,8 +812,10 @@ static int download_files(alpm_handle_t *handle, alpm_list_t **deltas) len = strlen(server_url) + strlen(payload->filename) + 2; CALLOC(payload->fileurl, len, sizeof(char), RET_ERR(handle, ALPM_ERR_MEMORY, -1)); snprintf(payload->fileurl, len, "%s/%s", server_url, payload->filename); + payload->handle = handle; + payload->allow_resume = 1; - ret = _alpm_download(handle, payload, cachedir, NULL, 0, 1, 0); + ret = _alpm_download(payload, cachedir, NULL); if(ret != -1) { break; } -- cgit v1.2.3-24-g4f1b