]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Do not save multipatterns to FS in certain cases
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 15 Mar 2024 13:18:34 +0000 (13:18 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 15 Mar 2024 13:18:34 +0000 (13:18 +0000)
src/libmime/lang_detection.c
src/libmime/message.c
src/libmime/mime_parser.c
src/libserver/url.c
src/libutil/multipattern.c
src/libutil/multipattern.h
src/lua/lua_trie.c

index bdd0aad74b71bc359e87f5b933e3dc6f9b25ec5e..c485de5adc39576824daca9cee8331a2b37d23c0 100644 (file)
@@ -898,7 +898,7 @@ rspamd_language_detector_init(struct rspamd_config *cfg)
                        rspamd_language_detector_process_chain(cfg, chain);
                });
 
-               if (!rspamd_multipattern_compile(ret->stop_words[i].mp, &err)) {
+               if (!rspamd_multipattern_compile(ret->stop_words[i].mp, 0, &err)) {
                        msg_err_config("cannot compile stop words for %z language group: %e",
                                                   i, err);
                        g_error_free(err);
index 4fb118e60da5c80630ac7b108651ce77f2fe177c..f73c1ee35b52ee523aa88ae40129c4339fbb993b 100644 (file)
@@ -674,7 +674,7 @@ rspamd_check_gtube(struct rspamd_task *task, struct rspamd_mime_text_part *part)
                                                                                RSPAMD_MULTIPATTERN_DEFAULT);
 
                GError *err = NULL;
-               rspamd_multipattern_compile(gtube_matcher, &err);
+               rspamd_multipattern_compile(gtube_matcher, RSPAMD_MULTIPATTERN_COMPILE_NO_FS, &err);
 
                if (err != NULL) {
                        /* It will be expensive, but I don't care, still better than to abort */
index 217f0b87dba8a2fdc2756ebcea0aad47ccc80eb1..562a9d7de2ee6de131b191ced06d744c76e2639e 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -169,7 +169,7 @@ rspamd_mime_parser_init_lib(void)
        rspamd_multipattern_add_pattern(lib_ctx->mp_boundary, "\n--", 0);
 
        GError *err = NULL;
-       if (!rspamd_multipattern_compile(lib_ctx->mp_boundary, &err)) {
+       if (!rspamd_multipattern_compile(lib_ctx->mp_boundary, RSPAMD_MULTIPATTERN_COMPILE_NO_FS, &err)) {
                msg_err("fatal error: cannot compile multipattern for mime parser boundaries: %e", err);
                g_error_free(err);
                g_abort();
index 0842a1ebd7ed4a9f7bb825a615f7394a85750cd6..ff75e3c764c16ecf13cd7d8ca46c720788c62aba 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2023 Vsevolod Stakhov
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -539,6 +539,7 @@ void rspamd_url_init(const gchar *tld_file)
 {
        GError *err = NULL;
        gboolean ret = TRUE;
+       int mp_compile_flags = 0;
 
        if (url_scanner != NULL) {
                rspamd_url_deinit();
@@ -564,6 +565,7 @@ void rspamd_url_init(const gchar *tld_file)
                url_scanner->matchers_full = NULL;
                url_scanner->search_trie_full = NULL;
                url_scanner->has_tld_file = false;
+               mp_compile_flags |= RSPAMD_MULTIPATTERN_COMPILE_NO_FS;
        }
 
        rspamd_url_add_static_matchers(url_scanner);
@@ -577,13 +579,13 @@ void rspamd_url_init(const gchar *tld_file)
                                 url_scanner->matchers_full->len);
        }
 
-       if (!rspamd_multipattern_compile(url_scanner->search_trie_strict, &err)) {
+       if (!rspamd_multipattern_compile(url_scanner->search_trie_strict, mp_compile_flags, &err)) {
                msg_err("cannot compile url matcher static patterns, fatal error: %e", err);
                abort();
        }
 
        if (url_scanner->search_trie_full) {
-               if (!rspamd_multipattern_compile(url_scanner->search_trie_full, &err)) {
+               if (!rspamd_multipattern_compile(url_scanner->search_trie_full, mp_compile_flags, &err)) {
                        msg_err("cannot compile tld patterns, url matching will be "
                                        "incomplete: %e",
                                        err);
index 630b1f92183607ca3e326e4dc93263520c27e21a..bf3c7ad9a64f92159f44f496101eb275f6b917c2 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -466,7 +466,7 @@ rspamd_multipattern_try_save_hs(struct rspamd_multipattern *mp,
 #endif
 
 gboolean
-rspamd_multipattern_compile(struct rspamd_multipattern *mp, GError **err)
+rspamd_multipattern_compile(struct rspamd_multipattern *mp, int flags, GError **err)
 {
        g_assert(mp != NULL);
        g_assert(!mp->compiled);
@@ -483,7 +483,7 @@ rspamd_multipattern_compile(struct rspamd_multipattern *mp, GError **err)
                        rspamd_cryptobox_hash_update(&mp->hash_state, (void *) &plt, sizeof(plt));
                        rspamd_cryptobox_hash_final(&mp->hash_state, hash);
 
-                       if (!rspamd_multipattern_try_load_hs(mp, hash)) {
+                       if ((flags & RSPAMD_MULTIPATTERN_COMPILE_NO_FS) || !rspamd_multipattern_try_load_hs(mp, hash)) {
                                hs_database_t *db = NULL;
 
                                if (hs_compile_multi((const char *const *) mp->hs_pats->data,
@@ -504,18 +504,23 @@ rspamd_multipattern_compile(struct rspamd_multipattern *mp, GError **err)
                                        return FALSE;
                                }
 
-                               if (hs_cache_dir != NULL) {
-                                       char fpath[PATH_MAX];
-                                       rspamd_snprintf(fpath, sizeof(fpath), "%s/%*xs.hsmp", hs_cache_dir,
-                                                                       (gint) rspamd_cryptobox_HASHBYTES / 2, hash);
-                                       mp->hs_db = rspamd_hyperscan_from_raw_db(db, fpath);
+                               if (!(flags & RSPAMD_MULTIPATTERN_COMPILE_NO_FS)) {
+                                       if (hs_cache_dir != NULL) {
+                                               char fpath[PATH_MAX];
+                                               rspamd_snprintf(fpath, sizeof(fpath), "%s/%*xs.hsmp", hs_cache_dir,
+                                                                               (gint) rspamd_cryptobox_HASHBYTES / 2, hash);
+                                               mp->hs_db = rspamd_hyperscan_from_raw_db(db, fpath);
+                                       }
+                                       else {
+                                               /* Should not happen in the real life */
+                                               mp->hs_db = rspamd_hyperscan_from_raw_db(db, NULL);
+                                       }
+
+                                       rspamd_multipattern_try_save_hs(mp, hash);
                                }
                                else {
-                                       /* Should not happen in the real life */
                                        mp->hs_db = rspamd_hyperscan_from_raw_db(db, NULL);
                                }
-
-                               rspamd_multipattern_try_save_hs(mp, hash);
                        }
 
                        for (i = 0; i < MAX_SCRATCH; i++) {
index 93027661d7732bc39d5721b751f288e2b9e081d9..15099aaca7e9a4ccaafd349bceb59d74489326df 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -117,12 +117,15 @@ void rspamd_multipattern_add_pattern(struct rspamd_multipattern *mp,
 void rspamd_multipattern_add_pattern_len(struct rspamd_multipattern *mp,
                                                                                 const gchar *pattern, gsize patlen, gint flags);
 
+
+#define RSPAMD_MULTIPATTERN_COMPILE_NO_FS (0x1u << 0u)
 /**
  * Compiles multipattern structure
  * @param mp
  * @return
  */
 gboolean rspamd_multipattern_compile(struct rspamd_multipattern *mp,
+                                                                        int flags,
                                                                         GError **err);
 
 /**
index b928329285e4a7dc886861d236d8e3f4f9e28cd8..70a685da28a94f69cf06adb7cc295ac432e23397 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -142,7 +142,7 @@ lua_trie_create(lua_State *L)
 
                lua_pop(L, 1); /* table */
 
-               if (!rspamd_multipattern_compile(trie, &err)) {
+               if (!rspamd_multipattern_compile(trie, 0, &err)) {
                        msg_err("cannot compile multipattern: %e", err);
                        g_error_free(err);
                        rspamd_multipattern_destroy(trie);