Browse Source

[Fix] Do not save multipatterns to FS in certain cases

pull/4873/head
Vsevolod Stakhov 2 months ago
parent
commit
1d0ff299f5
No account linked to committer's email address

+ 1
- 1
src/libmime/lang_detection.c View File

rspamd_language_detector_process_chain(cfg, chain); 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", msg_err_config("cannot compile stop words for %z language group: %e",
i, err); i, err);
g_error_free(err); g_error_free(err);

+ 1
- 1
src/libmime/message.c View File

RSPAMD_MULTIPATTERN_DEFAULT); RSPAMD_MULTIPATTERN_DEFAULT);


GError *err = NULL; GError *err = NULL;
rspamd_multipattern_compile(gtube_matcher, &err);
rspamd_multipattern_compile(gtube_matcher, RSPAMD_MULTIPATTERN_COMPILE_NO_FS, &err);


if (err != NULL) { if (err != NULL) {
/* It will be expensive, but I don't care, still better than to abort */ /* It will be expensive, but I don't care, still better than to abort */

+ 4
- 4
src/libmime/mime_parser.c View File

/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
rspamd_multipattern_add_pattern(lib_ctx->mp_boundary, "\n--", 0); rspamd_multipattern_add_pattern(lib_ctx->mp_boundary, "\n--", 0);


GError *err = NULL; 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); msg_err("fatal error: cannot compile multipattern for mime parser boundaries: %e", err);
g_error_free(err); g_error_free(err);
g_abort(); g_abort();

+ 5
- 3
src/libserver/url.c View File

/* /*
* Copyright 2023 Vsevolod Stakhov
* Copyright 2024 Vsevolod Stakhov
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
{ {
GError *err = NULL; GError *err = NULL;
gboolean ret = TRUE; gboolean ret = TRUE;
int mp_compile_flags = 0;


if (url_scanner != NULL) { if (url_scanner != NULL) {
rspamd_url_deinit(); rspamd_url_deinit();
url_scanner->matchers_full = NULL; url_scanner->matchers_full = NULL;
url_scanner->search_trie_full = NULL; url_scanner->search_trie_full = NULL;
url_scanner->has_tld_file = false; url_scanner->has_tld_file = false;
mp_compile_flags |= RSPAMD_MULTIPATTERN_COMPILE_NO_FS;
} }


rspamd_url_add_static_matchers(url_scanner); rspamd_url_add_static_matchers(url_scanner);
url_scanner->matchers_full->len); 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); msg_err("cannot compile url matcher static patterns, fatal error: %e", err);
abort(); abort();
} }


if (url_scanner->search_trie_full) { 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 " msg_err("cannot compile tld patterns, url matching will be "
"incomplete: %e", "incomplete: %e",
err); err);

+ 18
- 13
src/libutil/multipattern.c View File

/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
#endif #endif


gboolean 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 != NULL);
g_assert(!mp->compiled); g_assert(!mp->compiled);
rspamd_cryptobox_hash_update(&mp->hash_state, (void *) &plt, sizeof(plt)); rspamd_cryptobox_hash_update(&mp->hash_state, (void *) &plt, sizeof(plt));
rspamd_cryptobox_hash_final(&mp->hash_state, hash); 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; hs_database_t *db = NULL;


if (hs_compile_multi((const char *const *) mp->hs_pats->data, if (hs_compile_multi((const char *const *) mp->hs_pats->data,
return FALSE; 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 { else {
/* Should not happen in the real life */
mp->hs_db = rspamd_hyperscan_from_raw_db(db, NULL); mp->hs_db = rspamd_hyperscan_from_raw_db(db, NULL);
} }

rspamd_multipattern_try_save_hs(mp, hash);
} }


for (i = 0; i < MAX_SCRATCH; i++) { for (i = 0; i < MAX_SCRATCH; i++) {

+ 6
- 3
src/libutil/multipattern.h View File

/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
void rspamd_multipattern_add_pattern_len(struct rspamd_multipattern *mp, void rspamd_multipattern_add_pattern_len(struct rspamd_multipattern *mp,
const gchar *pattern, gsize patlen, gint flags); const gchar *pattern, gsize patlen, gint flags);



#define RSPAMD_MULTIPATTERN_COMPILE_NO_FS (0x1u << 0u)
/** /**
* Compiles multipattern structure * Compiles multipattern structure
* @param mp * @param mp
* @return * @return
*/ */
gboolean rspamd_multipattern_compile(struct rspamd_multipattern *mp, gboolean rspamd_multipattern_compile(struct rspamd_multipattern *mp,
int flags,
GError **err); GError **err);


/** /**

+ 4
- 4
src/lua/lua_trie.c View File

/*-
* Copyright 2016 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,


lua_pop(L, 1); /* table */ 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); msg_err("cannot compile multipattern: %e", err);
g_error_free(err); g_error_free(err);
rspamd_multipattern_destroy(trie); rspamd_multipattern_destroy(trie);

Loading…
Cancel
Save