commit 312c615: [Fix] Fix reading of the cached maps

Vsevolod Stakhov vsevolod at rspamd.com
Sun Jul 10 09:49:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-07-10 10:42:55 +0100
URL: https://github.com/rspamd/rspamd/commit/312c615323107e00101534be69197271e14d4e5e (HEAD -> master)

[Fix] Fix reading of the cached maps
Issue: #4213

---
 src/libserver/maps/map.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/src/libserver/maps/map.c b/src/libserver/maps/map.c
index 995509ded..f9933cee6 100644
--- a/src/libserver/maps/map.c
+++ b/src/libserver/maps/map.c
@@ -1319,13 +1319,13 @@ static gboolean
 rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk,
 		struct map_periodic_cbdata *periodic, const gchar *host)
 {
-	gsize len;
+	gsize mmap_len, len;
 	gpointer in;
 	struct http_map_data *data;
 
 	data = bk->data.hd;
 
-	in = rspamd_shmem_xmap (data->cache->shmem_name, PROT_READ, &len);
+	in = rspamd_shmem_xmap (data->cache->shmem_name, PROT_READ, &mmap_len);
 
 	if (in == NULL) {
 		msg_err ("cannot map cache from %s: %s", data->cache->shmem_name,
@@ -1333,15 +1333,24 @@ rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk,
 		return FALSE;
 	}
 
-	if (len < data->cache->len) {
-		msg_err ("cannot map cache from %s: bad length %z, %z expected",
+	if (mmap_len < data->cache->len) {
+		msg_err ("cannot map cache from %s: truncated length %z, %z expected",
 				data->cache->shmem_name,
-				len, data->cache->len);
-		munmap (in, len);
+				mmap_len, data->cache->len);
+		munmap (in, mmap_len);
 
 		return FALSE;
 	}
 
+	/*
+	 * Len is taken from the shmem file size that can be larger than the
+	 * actual data length, as we use shared memory as a growing buffer for the
+	 * HTTP input.
+	 * Hence, we need to use len from the saved cache data, counting that it is
+	 * at least not more than the cached file length (this is checked above).
+	 */
+	len = data->cache->len;
+
 	if (bk->is_compressed) {
 		ZSTD_DStream *zstream;
 		ZSTD_inBuffer zin;
@@ -1375,7 +1384,7 @@ rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk,
 						ZSTD_getErrorName (r));
 				ZSTD_freeDStream (zstream);
 				g_free (out);
-				munmap (in, len);
+				munmap (in, mmap_len);
 				return FALSE;
 			}
 
@@ -1395,12 +1404,11 @@ rspamd_map_read_cached (struct rspamd_map *map, struct rspamd_map_backend *bk,
 		g_free (out);
 	}
 	else {
-		msg_info_map ("%s: read map data cached %z bytes", bk->uri,
-				len);
+		msg_info_map ("%s: read map data cached %z bytes", bk->uri, len);
 		map->read_callback (in, len, &periodic->cbdata, TRUE);
 	}
 
-	munmap (in, len);
+	munmap (in, mmap_len);
 
 	return TRUE;
 }


More information about the Commits mailing list