From f4f89705b3ce5ddbd297f1e82272e20049b855f2 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 3 Apr 2024 15:47:50 +0100 Subject: [Fix] Fix shared memory proxying when compression is set --- src/libserver/task.c | 135 +++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 68 deletions(-) (limited to 'src/libserver') diff --git a/src/libserver/task.c b/src/libserver/task.c index 13f4fa1e6..f81f34e47 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -436,86 +436,89 @@ rspamd_task_load_message(struct rspamd_task *task, fp, shmem_size, offset, fd); rspamd_mempool_add_destructor(task->task_pool, rspamd_task_unmapper, m); - - return TRUE; } + else { + /* Try file */ + tok = rspamd_task_get_request_header(task, "file"); - tok = rspamd_task_get_request_header(task, "file"); + if (tok == NULL) { + tok = rspamd_task_get_request_header(task, "path"); + } - if (tok == NULL) { - tok = rspamd_task_get_request_header(task, "path"); - } + if (tok) { + debug_task("want to scan file %T", tok); - if (tok) { - debug_task("want to scan file %T", tok); + r = rspamd_strlcpy(filepath, tok->begin, + MIN(sizeof(filepath), tok->len + 1)); - r = rspamd_strlcpy(filepath, tok->begin, - MIN(sizeof(filepath), tok->len + 1)); - - rspamd_url_decode(filepath, filepath, r + 1); - flen = strlen(filepath); + rspamd_url_decode(filepath, filepath, r + 1); + flen = strlen(filepath); - if (filepath[0] == '"' && flen > 2) { - /* We need to unquote filepath */ - fp = &filepath[1]; - fp[flen - 2] = '\0'; - } - else { - fp = &filepath[0]; - } - - if (stat(fp, &st) == -1) { - g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, - "Invalid file (%s): %s", fp, strerror(errno)); - return FALSE; - } - - if (G_UNLIKELY(st.st_size == 0)) { - /* Empty file */ - task->flags |= RSPAMD_TASK_FLAG_EMPTY; - task->msg.begin = rspamd_mempool_strdup(task->task_pool, ""); - task->msg.len = 0; - } - else { - fd = open(fp, O_RDONLY); + if (filepath[0] == '"' && flen > 2) { + /* We need to unquote filepath */ + fp = &filepath[1]; + fp[flen - 2] = '\0'; + } + else { + fp = &filepath[0]; + } - if (fd == -1) { - g_set_error(&task->err, rspamd_task_quark(), - RSPAMD_PROTOCOL_ERROR, - "Cannot open file (%s): %s", fp, strerror(errno)); + if (stat(fp, &st) == -1) { + g_set_error(&task->err, rspamd_task_quark(), RSPAMD_PROTOCOL_ERROR, + "Invalid file (%s): %s", fp, strerror(errno)); return FALSE; } - map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (G_UNLIKELY(st.st_size == 0)) { + /* Empty file */ + task->flags |= RSPAMD_TASK_FLAG_EMPTY; + task->msg.begin = rspamd_mempool_strdup(task->task_pool, ""); + task->msg.len = 0; + } + else { + fd = open(fp, O_RDONLY); + if (fd == -1) { + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Cannot open file (%s): %s", fp, strerror(errno)); + return FALSE; + } - if (map == MAP_FAILED) { - close(fd); - g_set_error(&task->err, rspamd_task_quark(), - RSPAMD_PROTOCOL_ERROR, - "Cannot mmap file (%s): %s", fp, strerror(errno)); - return FALSE; - } + map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); - task->msg.begin = map; - task->msg.len = st.st_size; - m = rspamd_mempool_alloc(task->task_pool, sizeof(*m)); - m->begin = map; - m->len = st.st_size; - m->fd = fd; - rspamd_mempool_add_destructor(task->task_pool, rspamd_task_unmapper, m); - } + if (map == MAP_FAILED) { + close(fd); + g_set_error(&task->err, rspamd_task_quark(), + RSPAMD_PROTOCOL_ERROR, + "Cannot mmap file (%s): %s", fp, strerror(errno)); + return FALSE; + } - task->msg.fpath = rspamd_mempool_strdup(task->task_pool, fp); - task->flags |= RSPAMD_TASK_FLAG_FILE; + task->msg.begin = map; + task->msg.len = st.st_size; + m = rspamd_mempool_alloc(task->task_pool, sizeof(*m)); + m->begin = map; + m->len = st.st_size; + m->fd = fd; - msg_info_task("loaded message from file %s", fp); + rspamd_mempool_add_destructor(task->task_pool, rspamd_task_unmapper, m); + } - return TRUE; + task->msg.fpath = rspamd_mempool_strdup(task->task_pool, fp); + task->flags |= RSPAMD_TASK_FLAG_FILE; + + msg_info_task("loaded message from file %s", fp); + } + else { + /* Plain data */ + task->msg.begin = start; + task->msg.len = len; + } } - /* Plain data */ + debug_task("got input of length %z", task->msg.len); /* Check compression */ @@ -573,10 +576,10 @@ rspamd_task_load_message(struct rspamd_task *task, zstream = task->cfg->libs_ctx->in_zstream; zin.pos = 0; - zin.src = start; - zin.size = len; + zin.src = task->msg.begin; + zin.size = task->msg.len; - if ((outlen = ZSTD_getDecompressedSize(start, len)) == 0) { + if ((outlen = ZSTD_getDecompressedSize(task->msg.begin, task->msg.len)) == 0) { outlen = ZSTD_DStreamOutSize(); } @@ -618,10 +621,6 @@ rspamd_task_load_message(struct rspamd_task *task, return FALSE; } } - else { - task->msg.begin = start; - task->msg.len = len; - } if (task->msg.len == 0) { task->flags |= RSPAMD_TASK_FLAG_EMPTY; -- cgit v1.2.3