1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/*-
* Copyright 2022 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lua/lua_common.h"
#include "symcache_internal.hxx"
#include "symcache_item.hxx"
#include "fmt/core.h"
#include "libserver/task.h"
namespace rspamd::symcache {
auto cache_item::get_parent(const symcache &cache) const -> const cache_item *
{
if (is_virtual()) {
const auto &virtual_sp = std::get<virtual_item>(specific);
return virtual_sp.get_parent(cache);
}
return nullptr;
}
auto cache_item::get_parent_mut(const symcache &cache) -> cache_item *
{
if (is_virtual()) {
auto &virtual_sp = std::get<virtual_item>(specific);
return virtual_sp.get_parent_mut(cache);
}
return nullptr;
}
auto cache_item::process_deps(const symcache &cache) -> void
{
/* Allow logging macros to work */
auto log_tag = [&]() { return cache.log_tag(); };
for (auto &dep: deps) {
msg_debug_cache ("process real dependency %s on %s", symbol.c_str(), dep.sym.c_str());
auto *dit = cache.get_item_by_name_mut(dep.sym, true);
if (dep.vid >= 0) {
/* Case of the virtual symbol that depends on another (maybe virtual) symbol */
const auto *vdit = cache.get_item_by_name(dep.sym, false);
if (!vdit) {
if (dit) {
msg_err_cache("cannot add dependency from %s on %s: no dependency symbol registered",
dep.sym.c_str(), dit->symbol.c_str());
}
}
else {
msg_debug_cache("process virtual dependency %s(%d) on %s(%d)", symbol.c_str(),
dep.vid, vdit->symbol.c_str(), vdit->id);
unsigned nids = 0;
/* Propagate ids */
msg_debug_cache("check id propagation for dependency %s from %s",
symbol.c_str(), dit->symbol.c_str());
const auto *ids = dit->allowed_ids.get_ids(nids);
if (nids > 0) {
msg_debug_cache("propagate allowed ids from %s to %s",
dit->symbol.c_str(), symbol.c_str());
allowed_ids.set_ids(ids, nids, cache.get_pool());
}
ids = dit->forbidden_ids.get_ids(nids);
if (nids > 0) {
msg_debug_cache("propagate forbidden ids from %s to %s",
dit->symbol.c_str(), symbol.c_str());
forbidden_ids.set_ids(ids, nids, cache.get_pool());
}
}
}
if (dit != nullptr) {
if (!dit->is_filter()) {
/*
* Check sanity:
* - filters -> prefilter dependency is OK and always satisfied
* - postfilter -> (filter, prefilter) dep is ok
* - idempotent -> (any) dep is OK
*
* Otherwise, emit error
* However, even if everything is fine this dep is useless ¯\_(ツ)_/¯
*/
auto ok_dep = false;
if (dit->get_type() == type) {
ok_dep = true;
}
else if (type < dit->get_type()) {
ok_dep = true;
}
if (!ok_dep) {
msg_err_cache ("cannot add dependency from %s on %s: invalid symbol types",
dep.sym.c_str(), symbol.c_str());
continue;
}
}
else {
if (dit->id == id) {
msg_err_cache ("cannot add dependency on self: %s -> %s "
"(resolved to %s)",
symbol.c_str(), dep.sym.c_str(), dit->symbol.c_str());
}
else {
/* Create a reverse dep */
if (is_virtual()) {
auto *parent = get_parent_mut(cache);
if (parent) {
dit->rdeps.emplace_back(parent->getptr(), dep.sym, parent->id, -1);
dep.item = dit->getptr();
dep.id = dit->id;
msg_debug_cache ("added reverse dependency from %d on %d", parent->id,
dit->id);
}
}
else {
dep.item = dit->getptr();
dep.id = dit->id;
dit->rdeps.emplace_back(getptr(), dep.sym, id, -1);
msg_debug_cache ("added reverse dependency from %d on %d", id,
dit->id);
}
}
}
}
else if (dep.id >= 0) {
msg_err_cache ("cannot find dependency on symbol %s for symbol %s",
dep.sym.c_str(), symbol.c_str());
continue;
}
}
// Remove empty deps
deps.erase(std::remove_if(std::begin(deps), std::end(deps),
[](const auto &dep) { return !dep.item; }), std::end(deps));
}
auto cache_item::resolve_parent(const symcache &cache) -> bool
{
auto log_tag = [&]() { return cache.log_tag(); };
if (is_virtual()) {
auto &virt = std::get<virtual_item>(specific);
if (virt.get_parent(cache)) {
msg_warn_cache("trying to resolve parent twice for %s", symbol.c_str());
return false;
}
return virt.resolve_parent(cache);
}
else {
msg_warn_cache("trying to resolve a parent for non-virtual symbol %s", symbol.c_str());
}
return false;
}
auto cache_item::update_counters_check_peak(lua_State *L,
struct ev_loop *ev_loop,
double cur_time,
double last_resort) -> bool
{
auto ret = false;
static const double decay_rate = 0.25;
st->total_hits += st->hits;
g_atomic_int_set(&st->hits, 0);
if (last_count > 0) {
auto cur_value = (st->total_hits - last_count) /
(cur_time - last_resort);
rspamd_set_counter_ema(&st->frequency_counter,
cur_value, decay_rate);
st->avg_frequency = st->frequency_counter.mean;
st->stddev_frequency = st->frequency_counter.stddev;
auto cur_err = (st->avg_frequency - cur_value);
cur_err *= cur_err;
if (st->frequency_counter.number > 10 &&
cur_err > ::sqrt(st->stddev_frequency) * 3) {
frequency_peaks++;
ret = true;
}
}
last_count = st->total_hits;
if (cd->number > 0) {
if (!is_virtual()) {
st->avg_time = cd->mean;
rspamd_set_counter_ema(&st->time_counter,
st->avg_time, decay_rate);
st->avg_time = st->time_counter.mean;
memset(cd, 0, sizeof(*cd));
}
}
return ret;
}
auto cache_item::get_type_str() const -> const char *
{
switch (type) {
case symcache_item_type::CONNFILTER:
return "connfilter";
case symcache_item_type::FILTER:
return "filter";
case symcache_item_type::IDEMPOTENT:
return "idempotent";
case symcache_item_type::PREFILTER:
return "prefilter";
case symcache_item_type::POSTFILTER:
return "postfilter";
case symcache_item_type::COMPOSITE:
return "composite";
case symcache_item_type::CLASSIFIER:
return "classifier";
case symcache_item_type::VIRTUAL:
return "virtual";
}
RSPAMD_UNREACHABLE;
}
auto cache_item::is_allowed(struct rspamd_task *task, bool exec_only) const -> bool
{
const auto *what = "execution";
if (!exec_only) {
what = "symbol insertion";
}
/* Static checks */
if (!enabled ||
(RSPAMD_TASK_IS_EMPTY(task) && !(flags & SYMBOL_TYPE_EMPTY)) ||
(flags & SYMBOL_TYPE_MIME_ONLY && !RSPAMD_TASK_IS_MIME(task))) {
if (!enabled) {
msg_debug_cache_task("skipping %s of %s as it is permanently disabled",
what, symbol.c_str());
return false;
}
else {
/*
* If we check merely execution (not insertion), then we disallow
* mime symbols for non mime tasks and vice versa
*/
if (exec_only) {
msg_debug_cache_task("skipping check of %s as it cannot be "
"executed for this task type",
symbol.c_str());
return FALSE;
}
}
}
/* Settings checks */
if (task->settings_elt != nullptr) {
if (forbidden_ids.check_id(task->settings_elt->id)) {
msg_debug_cache_task ("deny %s of %s as it is forbidden for "
"settings id %ud",
what,
symbol.c_str(),
task->settings_elt->id);
return false;
}
if (!(flags & SYMBOL_TYPE_EXPLICIT_DISABLE)) {
if (!allowed_ids.check_id(task->settings_elt->id)) {
if (task->settings_elt->policy == RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW) {
msg_debug_cache_task("allow execution of %s settings id %ud "
"allows implicit execution of the symbols;",
symbol.c_str(),
id);
return true;
}
if (exec_only) {
/*
* Special case if any of our virtual children are enabled
*/
if (exec_only_ids.check_id(task->settings_elt->id)) {
return true;
}
}
msg_debug_cache_task ("deny %s of %s as it is not listed "
"as allowed for settings id %ud",
what,
symbol.c_str(),
task->settings_elt->id);
return false;
}
}
else {
msg_debug_cache_task ("allow %s of %s for "
"settings id %ud as it can be only disabled explicitly",
what,
symbol.c_str(),
task->settings_elt->id);
}
}
else if (flags & SYMBOL_TYPE_EXPLICIT_ENABLE) {
msg_debug_cache_task ("deny %s of %s as it must be explicitly enabled",
what,
symbol.c_str());
return false;
}
/* Allow all symbols with no settings id */
return true;
}
auto virtual_item::get_parent(const symcache &cache) const -> const cache_item *
{
if (parent) {
return parent.get();
}
return cache.get_item_by_id(parent_id, false);
}
auto virtual_item::get_parent_mut(const symcache &cache) -> cache_item *
{
if (parent) {
return parent.get();
}
return const_cast<cache_item *>(cache.get_item_by_id(parent_id, false));
}
auto virtual_item::resolve_parent(const symcache &cache) -> bool
{
if (parent) {
return false;
}
auto item_ptr = cache.get_item_by_id(parent_id, true);
if (item_ptr) {
parent = const_cast<cache_item *>(item_ptr)->getptr();
return true;
}
return false;
}
auto item_type_from_c(enum rspamd_symbol_type type) -> tl::expected<std::pair<symcache_item_type, int>, std::string>
{
constexpr const auto trivial_types = SYMBOL_TYPE_CONNFILTER | SYMBOL_TYPE_PREFILTER
| SYMBOL_TYPE_POSTFILTER | SYMBOL_TYPE_IDEMPOTENT
| SYMBOL_TYPE_COMPOSITE | SYMBOL_TYPE_CLASSIFIER
| SYMBOL_TYPE_VIRTUAL;
constexpr auto all_but_one_ty = [&](int type, int exclude_bit) -> auto {
return (type & trivial_types) & (trivial_types & ~exclude_bit);
};
if (type & trivial_types) {
auto check_trivial = [&](auto flag,
symcache_item_type ty) -> tl::expected<std::pair<symcache_item_type, int>, std::string> {
if (all_but_one_ty(type, flag)) {
return tl::make_unexpected(fmt::format("invalid flags for a symbol: {}", type));
}
return std::make_pair(ty, type & ~flag);
};
if (type & SYMBOL_TYPE_CONNFILTER) {
return check_trivial(SYMBOL_TYPE_CONNFILTER, symcache_item_type::CONNFILTER);
}
else if (type & SYMBOL_TYPE_PREFILTER) {
return check_trivial(SYMBOL_TYPE_PREFILTER, symcache_item_type::PREFILTER);
}
else if (type & SYMBOL_TYPE_POSTFILTER) {
return check_trivial(SYMBOL_TYPE_POSTFILTER, symcache_item_type::POSTFILTER);
}
else if (type & SYMBOL_TYPE_IDEMPOTENT) {
return check_trivial(SYMBOL_TYPE_IDEMPOTENT, symcache_item_type::IDEMPOTENT);
}
else if (type & SYMBOL_TYPE_COMPOSITE) {
return check_trivial(SYMBOL_TYPE_COMPOSITE, symcache_item_type::COMPOSITE);
}
else if (type & SYMBOL_TYPE_CLASSIFIER) {
return check_trivial(SYMBOL_TYPE_CLASSIFIER, symcache_item_type::CLASSIFIER);
}
else if (type & SYMBOL_TYPE_VIRTUAL) {
return check_trivial(SYMBOL_TYPE_VIRTUAL, symcache_item_type::VIRTUAL);
}
return tl::make_unexpected(fmt::format("internal error: impossible flags combination", type));
}
/* Maybe check other flags combination here? */
return std::make_pair(symcache_item_type::FILTER, type);
}
bool operator<(symcache_item_type lhs, symcache_item_type rhs)
{
auto ret = false;
switch (lhs) {
case symcache_item_type::CONNFILTER:
break;
case symcache_item_type::PREFILTER:
if (rhs == symcache_item_type::CONNFILTER) {
ret = true;
}
break;
case symcache_item_type::FILTER:
if (rhs == symcache_item_type::CONNFILTER || rhs == symcache_item_type::PREFILTER) {
ret = true;
}
break;
case symcache_item_type::POSTFILTER:
if (rhs != symcache_item_type::IDEMPOTENT) {
ret = true;
}
break;
case symcache_item_type::IDEMPOTENT:
default:
break;
}
return ret;
}
item_condition::~item_condition()
{
if (cb != -1 && L != nullptr) {
luaL_unref(L, LUA_REGISTRYINDEX, cb);
}
}
auto item_condition::check(std::string_view sym_name, struct rspamd_task *task) const -> bool
{
if (cb != -1 && L != nullptr) {
auto ret = false;
lua_pushcfunction (L, &rspamd_lua_traceback);
auto err_idx = lua_gettop(L);
lua_rawgeti(L, LUA_REGISTRYINDEX, cb);
rspamd_lua_task_push(L, task);
if (lua_pcall(L, 1, 1, err_idx) != 0) {
msg_info_task("call to condition for %s failed: %s",
sym_name.data(), lua_tostring(L, -1));
}
else {
ret = lua_toboolean(L, -1);
}
lua_settop(L, err_idx - 1);
return ret;
}
return true;
}
}
|