Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

regexp.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "regexp.h"
  18. #include "cryptobox.h"
  19. #include "ref.h"
  20. #include "util.h"
  21. #include "rspamd.h"
  22. #include "contrib/fastutf8/fastutf8.h"
  23. #ifndef WITH_PCRE2
  24. /* Normal pcre path */
  25. #include <pcre.h>
  26. #define PCRE_T pcre
  27. #define PCRE_EXTRA_T pcre_extra
  28. #define PCRE_JIT_T pcre_jit_stack
  29. #define PCRE_FREE pcre_free
  30. #define PCRE_JIT_STACK_FREE pcre_jit_stack_free
  31. #define PCRE_FLAG(x) G_PASTE(PCRE_, x)
  32. #else
  33. /* PCRE 2 path */
  34. #ifndef PCRE2_CODE_UNIT_WIDTH
  35. #define PCRE2_CODE_UNIT_WIDTH 8
  36. #endif
  37. #include <pcre2.h>
  38. #define PCRE_T pcre2_code
  39. #define PCRE_JIT_T pcre2_jit_stack
  40. #define PCRE_FREE pcre2_code_free
  41. #define PCRE_JIT_STACK_FREE pcre2_jit_stack_free
  42. #define PCRE_FLAG(x) G_PASTE(PCRE2_, x)
  43. #endif
  44. typedef guchar regexp_id_t[rspamd_cryptobox_HASHBYTES];
  45. #undef DISABLE_JIT_FAST
  46. struct rspamd_regexp_s {
  47. gdouble exec_time;
  48. gchar *pattern;
  49. PCRE_T *re;
  50. PCRE_T *raw_re;
  51. #ifndef WITH_PCRE2
  52. PCRE_EXTRA_T *extra;
  53. PCRE_EXTRA_T *raw_extra;
  54. #else
  55. pcre2_match_context *mcontext;
  56. pcre2_match_context *raw_mcontext;
  57. #endif
  58. regexp_id_t id;
  59. ref_entry_t ref;
  60. gpointer ud;
  61. gpointer re_class;
  62. guint64 cache_id;
  63. gsize match_limit;
  64. guint max_hits;
  65. gint flags;
  66. gint pcre_flags;
  67. gint ncaptures;
  68. };
  69. struct rspamd_regexp_cache {
  70. GHashTable *tbl;
  71. #ifdef HAVE_PCRE_JIT
  72. PCRE_JIT_T *jstack;
  73. #endif
  74. };
  75. static struct rspamd_regexp_cache *global_re_cache = NULL;
  76. static gboolean can_jit = FALSE;
  77. static gboolean check_jit = TRUE;
  78. #ifdef WITH_PCRE2
  79. static pcre2_compile_context *pcre2_ctx = NULL;
  80. #endif
  81. static GQuark
  82. rspamd_regexp_quark (void)
  83. {
  84. return g_quark_from_static_string ("rspamd-regexp");
  85. }
  86. static void
  87. rspamd_regexp_generate_id (const gchar *pattern, const gchar *flags,
  88. regexp_id_t out)
  89. {
  90. rspamd_cryptobox_hash_state_t st;
  91. rspamd_cryptobox_hash_init (&st, NULL, 0);
  92. if (flags) {
  93. rspamd_cryptobox_hash_update (&st, flags, strlen (flags));
  94. }
  95. rspamd_cryptobox_hash_update (&st, pattern, strlen (pattern));
  96. rspamd_cryptobox_hash_final (&st, out);
  97. }
  98. static void
  99. rspamd_regexp_dtor (rspamd_regexp_t *re)
  100. {
  101. if (re) {
  102. if (re->raw_re && re->raw_re != re->re) {
  103. #ifndef WITH_PCRE2
  104. #ifdef HAVE_PCRE_JIT
  105. if (re->raw_extra) {
  106. pcre_free_study (re->raw_extra);
  107. }
  108. #endif
  109. #else
  110. if (re->mcontext) {
  111. pcre2_match_context_free (re->mcontext);
  112. }
  113. #endif
  114. PCRE_FREE (re->raw_re);
  115. }
  116. if (re->re) {
  117. #ifndef WITH_PCRE2
  118. #ifdef HAVE_PCRE_JIT
  119. if (re->extra) {
  120. pcre_free_study (re->extra);
  121. }
  122. #endif
  123. #else
  124. if (re->raw_mcontext) {
  125. pcre2_match_context_free (re->raw_mcontext);
  126. }
  127. #endif
  128. PCRE_FREE (re->re);
  129. }
  130. if (re->pattern) {
  131. g_free (re->pattern);
  132. }
  133. g_free (re);
  134. }
  135. }
  136. static void
  137. rspamd_regexp_post_process (rspamd_regexp_t *r)
  138. {
  139. if (global_re_cache == NULL) {
  140. rspamd_regexp_library_init (NULL);
  141. }
  142. #if defined(WITH_PCRE2)
  143. gsize jsz;
  144. static const guint max_recursion_depth = 100000, max_backtrack = 1000000;
  145. guint jit_flags = can_jit ? PCRE2_JIT_COMPLETE : 0;
  146. /* Create match context */
  147. r->mcontext = pcre2_match_context_create (NULL);
  148. g_assert (r->mcontext != NULL);
  149. pcre2_set_recursion_limit (r->mcontext, max_recursion_depth);
  150. pcre2_set_match_limit (r->mcontext, max_backtrack);
  151. if (r->re != r->raw_re) {
  152. r->raw_mcontext = pcre2_match_context_create (NULL);
  153. g_assert (r->raw_mcontext != NULL);
  154. pcre2_set_recursion_limit (r->raw_mcontext, max_recursion_depth);
  155. pcre2_set_match_limit (r->raw_mcontext, max_backtrack);
  156. }
  157. else {
  158. r->raw_mcontext = r->mcontext;
  159. }
  160. #ifdef HAVE_PCRE_JIT
  161. if (can_jit) {
  162. if (pcre2_jit_compile (r->re, jit_flags) < 0) {
  163. msg_err ("jit compilation of %s is not supported: %d", r->pattern, jit_flags);
  164. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  165. }
  166. else {
  167. if (!(pcre2_pattern_info (r->re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) {
  168. msg_err ("jit compilation of %s is not supported", r->pattern);
  169. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  170. }
  171. }
  172. }
  173. else {
  174. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  175. }
  176. if (!(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
  177. pcre2_jit_stack_assign (r->mcontext, NULL, global_re_cache->jstack);
  178. }
  179. if (r->raw_re && r->re != r->raw_re && !(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
  180. if (pcre2_jit_compile (r->raw_re, jit_flags) < 0) {
  181. msg_debug ("jit compilation of %s is not supported", r->pattern);
  182. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  183. }
  184. if (!(pcre2_pattern_info (r->raw_re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0)) {
  185. msg_debug ("jit compilation of raw %s is not supported", r->pattern);
  186. }
  187. else if (!(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
  188. g_assert (r->raw_mcontext != NULL);
  189. pcre2_jit_stack_assign (r->raw_mcontext, NULL, global_re_cache->jstack);
  190. }
  191. }
  192. #endif
  193. #else
  194. const gchar *err_str = "unknown";
  195. gboolean try_jit = TRUE, try_raw_jit = TRUE;
  196. gint study_flags = 0;
  197. #if defined(HAVE_PCRE_JIT)
  198. study_flags |= PCRE_STUDY_JIT_COMPILE;
  199. #endif
  200. /* Pcre 1 needs study */
  201. if (r->re) {
  202. r->extra = pcre_study (r->re, study_flags, &err_str);
  203. if (r->extra == NULL) {
  204. msg_debug ("cannot optimize regexp pattern: '%s': %s",
  205. r->pattern, err_str);
  206. try_jit = FALSE;
  207. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  208. }
  209. }
  210. else {
  211. g_assert_not_reached ();
  212. }
  213. if (r->raw_re && r->raw_re != r->re) {
  214. r->raw_extra = pcre_study (r->re, study_flags, &err_str);
  215. }
  216. else if (r->raw_re == r->re) {
  217. r->raw_extra = r->extra;
  218. }
  219. if (r->raw_extra == NULL) {
  220. msg_debug ("cannot optimize raw regexp pattern: '%s': %s",
  221. r->pattern, err_str);
  222. try_raw_jit = FALSE;
  223. }
  224. /* JIT path */
  225. if (try_jit) {
  226. #ifdef HAVE_PCRE_JIT
  227. gint jit, n;
  228. if (can_jit) {
  229. jit = 0;
  230. n = pcre_fullinfo (r->re, r->extra,
  231. PCRE_INFO_JIT, &jit);
  232. if (n != 0 || jit != 1) {
  233. msg_debug ("jit compilation of %s is not supported", r->pattern);
  234. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  235. }
  236. else {
  237. pcre_assign_jit_stack (r->extra, NULL, global_re_cache->jstack);
  238. }
  239. }
  240. #endif
  241. }
  242. else {
  243. msg_debug ("cannot optimize regexp pattern: '%s': %s",
  244. r->pattern, err_str);
  245. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  246. }
  247. if (try_raw_jit) {
  248. #ifdef HAVE_PCRE_JIT
  249. gint jit, n;
  250. if (can_jit) {
  251. if (r->raw_re != r->re) {
  252. jit = 0;
  253. n = pcre_fullinfo (r->raw_re, r->raw_extra,
  254. PCRE_INFO_JIT, &jit);
  255. if (n != 0 || jit != 1) {
  256. msg_debug ("jit compilation of %s is not supported", r->pattern);
  257. r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
  258. }
  259. else {
  260. pcre_assign_jit_stack (r->raw_extra, NULL,
  261. global_re_cache->jstack);
  262. }
  263. }
  264. }
  265. #endif
  266. }
  267. #endif /* WITH_PCRE2 */
  268. }
  269. rspamd_regexp_t*
  270. rspamd_regexp_new_len (const gchar *pattern, gsize len, const gchar *flags,
  271. GError **err)
  272. {
  273. const gchar *start = pattern, *end = start + len, *flags_str = NULL, *flags_end = NULL;
  274. gchar *err_str;
  275. rspamd_regexp_t *res;
  276. gboolean explicit_utf = FALSE;
  277. PCRE_T *r;
  278. gchar sep = 0, *real_pattern;
  279. #ifndef WITH_PCRE2
  280. gint err_off;
  281. #else
  282. gsize err_off;
  283. #endif
  284. gint regexp_flags = 0, rspamd_flags = 0, err_code, ncaptures;
  285. gboolean strict_flags = FALSE;
  286. rspamd_regexp_library_init (NULL);
  287. if (pattern == NULL) {
  288. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  289. "cannot create regexp from a NULL pattern");
  290. return NULL;
  291. }
  292. if (flags == NULL && start + 1 < end) {
  293. /* We need to parse pattern and detect flags set */
  294. if (*start == '/') {
  295. sep = '/';
  296. }
  297. else if (*start == 'm' && !g_ascii_isalnum(start[1])) {
  298. start ++;
  299. sep = *start;
  300. /* Paired braces */
  301. if (sep == '{') {
  302. sep = '}';
  303. }
  304. rspamd_flags |= RSPAMD_REGEXP_FLAG_FULL_MATCH;
  305. }
  306. if (sep == 0) {
  307. /* We have no flags, no separators and just use all line as expr */
  308. start = pattern;
  309. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_FULL_MATCH;
  310. }
  311. else {
  312. gchar *last_sep = rspamd_memrchr(pattern, sep, len);
  313. if (last_sep == NULL || last_sep <= start) {
  314. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  315. "pattern is not enclosed with %c: %s",
  316. sep, pattern);
  317. return NULL;
  318. }
  319. flags_str = last_sep + 1;
  320. flags_end = end;
  321. end = last_sep;
  322. start ++;
  323. }
  324. }
  325. else {
  326. /* Strictly check all flags */
  327. strict_flags = TRUE;
  328. start = pattern;
  329. flags_str = flags;
  330. if (flags) {
  331. flags_end = flags + strlen(flags);
  332. }
  333. }
  334. rspamd_flags |= RSPAMD_REGEXP_FLAG_RAW;
  335. #ifndef WITH_PCRE2
  336. regexp_flags &= ~PCRE_FLAG(UTF8);
  337. regexp_flags |= PCRE_FLAG(NEWLINE_ANYCRLF);
  338. #else
  339. regexp_flags &= ~PCRE_FLAG(UTF);
  340. #endif
  341. if (flags_str != NULL) {
  342. while (flags_str < flags_end) {
  343. switch (*flags_str) {
  344. case 'i':
  345. regexp_flags |= PCRE_FLAG(CASELESS);
  346. break;
  347. case 'm':
  348. regexp_flags |= PCRE_FLAG(MULTILINE);
  349. break;
  350. case 's':
  351. regexp_flags |= PCRE_FLAG(DOTALL);
  352. break;
  353. case 'x':
  354. regexp_flags |= PCRE_FLAG(EXTENDED);
  355. break;
  356. case 'u':
  357. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_RAW;
  358. rspamd_flags |= RSPAMD_REGEXP_FLAG_UTF;
  359. #ifndef WITH_PCRE2
  360. regexp_flags |= PCRE_FLAG(UTF8);
  361. #else
  362. regexp_flags |= PCRE_FLAG(UTF);
  363. #endif
  364. explicit_utf = TRUE;
  365. break;
  366. case 'O':
  367. /* We optimize all regexps by default */
  368. rspamd_flags |= RSPAMD_REGEXP_FLAG_NOOPT;
  369. break;
  370. case 'L':
  371. /* SOM_LEFTMOST hyperscan flag */
  372. rspamd_flags |= RSPAMD_REGEXP_FLAG_LEFTMOST;
  373. break;
  374. case 'r':
  375. rspamd_flags |= RSPAMD_REGEXP_FLAG_RAW;
  376. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_UTF;
  377. #ifndef WITH_PCRE2
  378. regexp_flags &= ~PCRE_FLAG(UTF8);
  379. #else
  380. regexp_flags &= ~PCRE_FLAG(UTF);
  381. #endif
  382. break;
  383. default:
  384. if (strict_flags) {
  385. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  386. "invalid regexp flag: %c in pattern %s",
  387. *flags_str, pattern);
  388. return NULL;
  389. }
  390. msg_warn ("invalid flag '%c' in pattern %s", *flags_str, pattern);
  391. goto fin;
  392. break;
  393. }
  394. flags_str++;
  395. }
  396. }
  397. fin:
  398. real_pattern = g_malloc (end - start + 1);
  399. rspamd_strlcpy (real_pattern, start, end - start + 1);
  400. #ifndef WITH_PCRE2
  401. r = pcre_compile (real_pattern, regexp_flags,
  402. (const char **)&err_str, &err_off, NULL);
  403. (void)err_code;
  404. #else
  405. r = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED,
  406. regexp_flags,
  407. &err_code, &err_off, pcre2_ctx);
  408. if (r == NULL) {
  409. err_str = g_alloca (1024);
  410. memset (err_str, 0, 1024);
  411. pcre2_get_error_message (err_code, err_str, 1024);
  412. }
  413. #endif
  414. if (r == NULL) {
  415. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  416. "regexp parsing error: '%s' at position %d; pattern: %s",
  417. err_str, (gint)err_off, real_pattern);
  418. g_free (real_pattern);
  419. return NULL;
  420. }
  421. /* Now allocate the target structure */
  422. res = g_malloc0 (sizeof (*res));
  423. REF_INIT_RETAIN (res, rspamd_regexp_dtor);
  424. res->flags = rspamd_flags;
  425. res->pattern = real_pattern;
  426. res->cache_id = RSPAMD_INVALID_ID;
  427. res->pcre_flags = regexp_flags;
  428. res->max_hits = 0;
  429. res->re = r;
  430. if (rspamd_flags & RSPAMD_REGEXP_FLAG_RAW) {
  431. res->raw_re = r;
  432. }
  433. else if (!explicit_utf) {
  434. #ifndef WITH_PCRE2
  435. res->raw_re = pcre_compile (real_pattern, regexp_flags & ~PCRE_FLAG(UTF8),
  436. (const char **)&err_str, &err_off, NULL);
  437. (void)err_code;
  438. #else
  439. res->raw_re = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED,
  440. regexp_flags & ~PCRE_FLAG(UTF),
  441. &err_code, &err_off, pcre2_ctx);
  442. if (res->raw_re == NULL) {
  443. err_str = g_alloca (1024);
  444. memset (err_str, 0, 1024);
  445. pcre2_get_error_message (err_code, err_str, 1024);
  446. }
  447. #endif
  448. if (res->raw_re == NULL) {
  449. msg_warn ("raw regexp parsing error: '%s': '%s' at position %d",
  450. err_str, real_pattern, (gint)err_off);
  451. }
  452. }
  453. rspamd_regexp_post_process (res);
  454. rspamd_regexp_generate_id (pattern, flags, res->id);
  455. #ifndef WITH_PCRE2
  456. /* Check number of captures */
  457. if (pcre_fullinfo (res->raw_re, res->extra, PCRE_INFO_CAPTURECOUNT,
  458. &ncaptures) == 0) {
  459. res->ncaptures = ncaptures;
  460. }
  461. #else
  462. /* Check number of captures */
  463. if (pcre2_pattern_info (res->raw_re, PCRE2_INFO_CAPTURECOUNT,
  464. &ncaptures) == 0) {
  465. res->ncaptures = ncaptures;
  466. }
  467. #endif
  468. return res;
  469. }
  470. rspamd_regexp_t *
  471. rspamd_regexp_new (const gchar *pattern, const gchar *flags,
  472. GError **err)
  473. {
  474. return rspamd_regexp_new_len(pattern, strlen(pattern), flags, err);
  475. }
  476. #ifndef WITH_PCRE2
  477. gboolean
  478. rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len,
  479. const gchar **start, const gchar **end, gboolean raw,
  480. GArray *captures)
  481. {
  482. pcre *r;
  483. pcre_extra *ext;
  484. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  485. pcre_jit_stack *st = NULL;
  486. #endif
  487. const gchar *mt;
  488. gsize remain = 0;
  489. gint rc, match_flags = 0, *ovec, ncaptures, i;
  490. g_assert (re != NULL);
  491. g_assert (text != NULL);
  492. if (len == 0) {
  493. len = strlen (text);
  494. }
  495. if (re->match_limit > 0 && len > re->match_limit) {
  496. len = re->match_limit;
  497. }
  498. if (end != NULL && *end != NULL) {
  499. /* Incremental search */
  500. mt = (*end);
  501. if ((gint)len > (mt - text)) {
  502. remain = len - (mt - text);
  503. }
  504. }
  505. else {
  506. mt = text;
  507. remain = len;
  508. }
  509. if (remain == 0) {
  510. return FALSE;
  511. }
  512. match_flags = PCRE_NEWLINE_ANYCRLF;
  513. if ((re->flags & RSPAMD_REGEXP_FLAG_RAW) || raw) {
  514. r = re->raw_re;
  515. ext = re->raw_extra;
  516. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  517. st = global_re_cache->jstack;
  518. #endif
  519. }
  520. else {
  521. r = re->re;
  522. ext = re->extra;
  523. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  524. if (rspamd_fast_utf8_validate (mt, remain) == 0) {
  525. st = global_re_cache->jstack;
  526. }
  527. else {
  528. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  529. return FALSE;
  530. }
  531. #endif
  532. }
  533. if (r == NULL) {
  534. /* Invalid regexp type for the specified input */
  535. return FALSE;
  536. }
  537. ncaptures = (re->ncaptures + 1) * 3;
  538. ovec = g_alloca (sizeof (gint) * ncaptures);
  539. if (!(re->flags & RSPAMD_REGEXP_FLAG_NOOPT)) {
  540. #ifdef HAVE_PCRE_JIT
  541. # if defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  542. /* XXX: flags seems to be broken with jit fast path */
  543. g_assert (remain > 0);
  544. g_assert (mt != NULL);
  545. if (st != NULL && !(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  546. rc = pcre_jit_exec (r, ext, mt, remain, 0, 0, ovec,
  547. ncaptures, st);
  548. }
  549. else {
  550. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  551. ncaptures);
  552. }
  553. # else
  554. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  555. ncaptures);
  556. #endif
  557. #else
  558. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  559. ncaptures);
  560. #endif
  561. }
  562. else {
  563. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  564. ncaptures);
  565. }
  566. if (rc >= 0) {
  567. if (start) {
  568. *start = mt + ovec[0];
  569. }
  570. if (end) {
  571. *end = mt + ovec[1];
  572. }
  573. if (captures != NULL && rc >= 1) {
  574. struct rspamd_re_capture *elt;
  575. g_assert (g_array_get_element_size (captures) ==
  576. sizeof (struct rspamd_re_capture));
  577. g_array_set_size (captures, rc);
  578. for (i = 0; i < rc; i ++) {
  579. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  580. elt->p = mt + ovec[i * 2];
  581. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  582. }
  583. }
  584. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  585. /* We also ensure that the match is full */
  586. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  587. return FALSE;
  588. }
  589. }
  590. return TRUE;
  591. }
  592. return FALSE;
  593. }
  594. #else
  595. /* PCRE 2 version */
  596. gboolean
  597. rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len,
  598. const gchar **start, const gchar **end, gboolean raw,
  599. GArray *captures)
  600. {
  601. pcre2_match_data *match_data;
  602. pcre2_match_context *mcontext;
  603. PCRE_T *r;
  604. const gchar *mt;
  605. gsize remain = 0, *ovec;
  606. gint rc, match_flags, novec, i;
  607. gboolean ret = FALSE;
  608. g_assert (re != NULL);
  609. g_assert (text != NULL);
  610. if (len == 0) {
  611. len = strlen (text);
  612. }
  613. if (end != NULL && *end != NULL) {
  614. /* Incremental search */
  615. mt = (*end);
  616. if ((gint)len > (mt - text)) {
  617. remain = len - (mt - text);
  618. }
  619. }
  620. else {
  621. mt = text;
  622. remain = len;
  623. }
  624. if (remain == 0) {
  625. return FALSE;
  626. }
  627. match_flags = 0;
  628. if (raw || re->re == re->raw_re) {
  629. r = re->raw_re;
  630. mcontext = re->raw_mcontext;
  631. }
  632. else {
  633. r = re->re;
  634. mcontext = re->mcontext;
  635. }
  636. if (r == NULL) {
  637. /* Invalid regexp type for the specified input */
  638. return FALSE;
  639. }
  640. match_data = pcre2_match_data_create (re->ncaptures + 1, NULL);
  641. #ifdef HAVE_PCRE_JIT
  642. if (!(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  643. if (re->re != re->raw_re && rspamd_fast_utf8_validate (mt, remain) != 0) {
  644. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  645. return FALSE;
  646. }
  647. rc = pcre2_jit_match (r, mt, remain, 0, match_flags, match_data,
  648. mcontext);
  649. }
  650. else {
  651. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  652. mcontext);
  653. }
  654. #else
  655. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  656. mcontext);
  657. #endif
  658. if (rc >= 0) {
  659. novec = pcre2_get_ovector_count (match_data);
  660. ovec = pcre2_get_ovector_pointer (match_data);
  661. if (start) {
  662. *start = mt + ovec[0];
  663. }
  664. if (end) {
  665. *end = mt + ovec[1];
  666. }
  667. if (captures != NULL && novec >= 1) {
  668. struct rspamd_re_capture *elt;
  669. g_assert (g_array_get_element_size (captures) ==
  670. sizeof (struct rspamd_re_capture));
  671. g_array_set_size (captures, novec);
  672. for (i = 0; i < novec; i ++) {
  673. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  674. elt->p = mt + ovec[i * 2];
  675. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  676. }
  677. }
  678. ret = TRUE;
  679. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  680. /* We also ensure that the match is full */
  681. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  682. ret = FALSE;
  683. }
  684. }
  685. }
  686. pcre2_match_data_free (match_data);
  687. return ret;
  688. }
  689. #endif
  690. const char*
  691. rspamd_regexp_get_pattern (const rspamd_regexp_t *re)
  692. {
  693. g_assert (re != NULL);
  694. return re->pattern;
  695. }
  696. guint
  697. rspamd_regexp_set_flags (rspamd_regexp_t *re, guint new_flags)
  698. {
  699. guint old_flags;
  700. g_assert (re != NULL);
  701. old_flags = re->flags;
  702. re->flags = new_flags;
  703. return old_flags;
  704. }
  705. guint
  706. rspamd_regexp_get_flags (const rspamd_regexp_t *re)
  707. {
  708. g_assert (re != NULL);
  709. return re->flags;
  710. }
  711. guint
  712. rspamd_regexp_get_pcre_flags (const rspamd_regexp_t *re)
  713. {
  714. g_assert (re != NULL);
  715. return re->pcre_flags;
  716. }
  717. guint
  718. rspamd_regexp_get_maxhits (const rspamd_regexp_t *re)
  719. {
  720. g_assert (re != NULL);
  721. return re->max_hits;
  722. }
  723. guint
  724. rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits)
  725. {
  726. guint old_hits;
  727. g_assert (re != NULL);
  728. old_hits = re->max_hits;
  729. re->max_hits = new_maxhits;
  730. return old_hits;
  731. }
  732. guint64
  733. rspamd_regexp_get_cache_id (const rspamd_regexp_t *re)
  734. {
  735. g_assert (re != NULL);
  736. return re->cache_id;
  737. }
  738. guint64
  739. rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id)
  740. {
  741. guint64 old;
  742. g_assert (re != NULL);
  743. old = re->cache_id;
  744. re->cache_id = id;
  745. return old;
  746. }
  747. gsize
  748. rspamd_regexp_get_match_limit (const rspamd_regexp_t *re)
  749. {
  750. g_assert (re != NULL);
  751. return re->match_limit;
  752. }
  753. gsize
  754. rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim)
  755. {
  756. gsize old;
  757. g_assert (re != NULL);
  758. old = re->match_limit;
  759. re->match_limit = lim;
  760. return old;
  761. }
  762. gboolean
  763. rspamd_regexp_match (const rspamd_regexp_t *re, const gchar *text, gsize len,
  764. gboolean raw)
  765. {
  766. const gchar *start = NULL, *end = NULL;
  767. g_assert (re != NULL);
  768. g_assert (text != NULL);
  769. if (len == 0) {
  770. len = strlen (text);
  771. }
  772. if (rspamd_regexp_search (re, text, len, &start, &end, raw, NULL)) {
  773. if (start == text && end == text + len) {
  774. return TRUE;
  775. }
  776. }
  777. return FALSE;
  778. }
  779. void
  780. rspamd_regexp_unref (rspamd_regexp_t *re)
  781. {
  782. REF_RELEASE (re);
  783. }
  784. rspamd_regexp_t*
  785. rspamd_regexp_ref (rspamd_regexp_t *re)
  786. {
  787. g_assert (re != NULL);
  788. REF_RETAIN (re);
  789. return re;
  790. }
  791. void
  792. rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud)
  793. {
  794. g_assert (re != NULL);
  795. re->ud = ud;
  796. }
  797. gpointer
  798. rspamd_regexp_get_ud (const rspamd_regexp_t *re)
  799. {
  800. g_assert (re != NULL);
  801. return re->ud;
  802. }
  803. gboolean
  804. rspamd_regexp_equal (gconstpointer a, gconstpointer b)
  805. {
  806. const guchar *ia = a, *ib = b;
  807. return (memcmp (ia, ib, sizeof (regexp_id_t)) == 0);
  808. }
  809. guint32
  810. rspamd_regexp_hash (gconstpointer a)
  811. {
  812. const guchar *ia = a;
  813. guint32 res;
  814. memcpy (&res, ia, sizeof (res));
  815. return res;
  816. }
  817. gboolean
  818. rspamd_regexp_cmp (gconstpointer a, gconstpointer b)
  819. {
  820. const guchar *ia = a, *ib = b;
  821. return memcmp (ia, ib, sizeof (regexp_id_t));
  822. }
  823. struct rspamd_regexp_cache*
  824. rspamd_regexp_cache_new (void)
  825. {
  826. struct rspamd_regexp_cache *ncache;
  827. ncache = g_malloc0 (sizeof (*ncache));
  828. ncache->tbl = g_hash_table_new_full (rspamd_regexp_hash, rspamd_regexp_equal,
  829. NULL, (GDestroyNotify)rspamd_regexp_unref);
  830. #ifdef HAVE_PCRE_JIT
  831. #ifdef WITH_PCRE2
  832. ncache->jstack = pcre2_jit_stack_create (32 * 1024, 1024 * 1024, NULL);
  833. #else
  834. ncache->jstack = pcre_jit_stack_alloc (32 * 1024, 1024 * 1024);
  835. #endif
  836. #endif
  837. return ncache;
  838. }
  839. rspamd_regexp_t*
  840. rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache,
  841. const gchar *pattern,
  842. const gchar *flags)
  843. {
  844. rspamd_regexp_t *res = NULL;
  845. regexp_id_t id;
  846. if (cache == NULL) {
  847. rspamd_regexp_library_init (NULL);
  848. cache = global_re_cache;
  849. }
  850. g_assert (cache != NULL);
  851. rspamd_regexp_generate_id (pattern, flags, id);
  852. res = g_hash_table_lookup (cache->tbl, id);
  853. return res;
  854. }
  855. rspamd_regexp_t*
  856. rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
  857. const gchar *pattern,
  858. const gchar *flags, GError **err)
  859. {
  860. rspamd_regexp_t *res;
  861. if (cache == NULL) {
  862. rspamd_regexp_library_init (NULL);
  863. cache = global_re_cache;
  864. }
  865. g_assert (cache != NULL);
  866. res = rspamd_regexp_cache_query (cache, pattern, flags);
  867. if (res != NULL) {
  868. return res;
  869. }
  870. res = rspamd_regexp_new (pattern, flags, err);
  871. if (res) {
  872. /* REF_RETAIN (res); */
  873. g_hash_table_insert (cache->tbl, res->id, res);
  874. }
  875. return res;
  876. }
  877. void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache,
  878. const gchar *pattern,
  879. const gchar *flags, rspamd_regexp_t *re)
  880. {
  881. g_assert (re != NULL);
  882. g_assert (pattern != NULL);
  883. if (cache == NULL) {
  884. rspamd_regexp_library_init (NULL);
  885. cache = global_re_cache;
  886. }
  887. g_assert (cache != NULL);
  888. /* Generate custom id */
  889. rspamd_regexp_generate_id (pattern, flags, re->id);
  890. REF_RETAIN (re);
  891. g_hash_table_insert (cache->tbl, re->id, re);
  892. }
  893. gboolean
  894. rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache,
  895. rspamd_regexp_t *re)
  896. {
  897. if (cache == NULL) {
  898. cache = global_re_cache;
  899. }
  900. g_assert (cache != NULL);
  901. g_assert (re != NULL);
  902. return g_hash_table_remove (cache->tbl, re->id);
  903. }
  904. void
  905. rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
  906. {
  907. if (cache != NULL) {
  908. g_hash_table_destroy (cache->tbl);
  909. #ifdef HAVE_PCRE_JIT
  910. #ifdef WITH_PCRE2
  911. if (cache->jstack) {
  912. pcre2_jit_stack_free (cache->jstack);
  913. }
  914. #else
  915. if (cache->jstack) {
  916. pcre_jit_stack_free (cache->jstack);
  917. }
  918. #endif
  919. #endif
  920. g_free (cache);
  921. }
  922. }
  923. RSPAMD_CONSTRUCTOR (rspamd_re_static_pool_ctor)
  924. {
  925. global_re_cache = rspamd_regexp_cache_new ();
  926. #ifdef WITH_PCRE2
  927. pcre2_ctx = pcre2_compile_context_create (NULL);
  928. pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY));
  929. #endif
  930. }
  931. RSPAMD_DESTRUCTOR (rspamd_re_static_pool_dtor)
  932. {
  933. rspamd_regexp_cache_destroy (global_re_cache);
  934. #ifdef WITH_PCRE2
  935. pcre2_compile_context_free (pcre2_ctx);
  936. #endif
  937. }
  938. void
  939. rspamd_regexp_library_init (struct rspamd_config *cfg)
  940. {
  941. if (cfg) {
  942. if (cfg->disable_pcre_jit) {
  943. can_jit = FALSE;
  944. check_jit = FALSE;
  945. }
  946. else if (!can_jit) {
  947. check_jit = TRUE;
  948. }
  949. }
  950. if (check_jit) {
  951. #ifdef HAVE_PCRE_JIT
  952. gint jit, rc;
  953. gchar *str;
  954. #ifndef WITH_PCRE2
  955. rc = pcre_config (PCRE_CONFIG_JIT, &jit);
  956. #else
  957. rc = pcre2_config (PCRE2_CONFIG_JIT, &jit);
  958. #endif
  959. if (rc == 0 && jit == 1) {
  960. #ifndef WITH_PCRE2
  961. #ifdef PCRE_CONFIG_JITTARGET
  962. pcre_config (PCRE_CONFIG_JITTARGET, &str);
  963. msg_info ("pcre is compiled with JIT for %s", str);
  964. #else
  965. msg_info ("pcre is compiled with JIT for unknown target");
  966. #endif
  967. #else
  968. rc = pcre2_config (PCRE2_CONFIG_JITTARGET, NULL);
  969. if (rc > 0) {
  970. str = g_alloca (rc);
  971. pcre2_config (PCRE2_CONFIG_JITTARGET, str);
  972. msg_info ("pcre2 is compiled with JIT for %s", str);
  973. }
  974. else {
  975. msg_info ("pcre2 is compiled with JIT for unknown");
  976. }
  977. #endif /* WITH_PCRE2 */
  978. if (getenv ("VALGRIND") == NULL) {
  979. can_jit = TRUE;
  980. } else {
  981. msg_info ("disabling PCRE jit as it does not play well with valgrind");
  982. can_jit = FALSE;
  983. }
  984. } else {
  985. msg_info ("pcre is compiled without JIT support, so many optimizations"
  986. " are impossible");
  987. can_jit = FALSE;
  988. }
  989. #else
  990. msg_info ("pcre is too old and has no JIT support, so many optimizations"
  991. " are impossible");
  992. can_jit = FALSE;
  993. #endif
  994. check_jit = FALSE;
  995. }
  996. }
  997. gpointer
  998. rspamd_regexp_get_id (const rspamd_regexp_t *re)
  999. {
  1000. g_assert (re != NULL);
  1001. return (gpointer)re->id;
  1002. }
  1003. gpointer
  1004. rspamd_regexp_get_class (const rspamd_regexp_t *re)
  1005. {
  1006. g_assert (re != NULL);
  1007. return re->re_class;
  1008. }
  1009. gpointer
  1010. rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class)
  1011. {
  1012. gpointer old_class;
  1013. g_assert (re != NULL);
  1014. old_class = re->re_class;
  1015. re->re_class = re_class;
  1016. return old_class;
  1017. }
  1018. rspamd_regexp_t *
  1019. rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err)
  1020. {
  1021. GString *out;
  1022. rspamd_regexp_t *re;
  1023. const gchar *end;
  1024. gboolean escaping = FALSE;
  1025. gint nbraces = 0;
  1026. g_assert (gl != NULL);
  1027. if (sz == 0) {
  1028. sz = strlen (gl);
  1029. }
  1030. end = gl + sz;
  1031. out = g_string_sized_new (sz + 2);
  1032. g_string_append_c (out, '^');
  1033. while (gl < end) {
  1034. switch (*gl) {
  1035. case '*':
  1036. if (escaping) {
  1037. g_string_append (out, "\\*");
  1038. }
  1039. else {
  1040. g_string_append (out, ".*");
  1041. }
  1042. escaping = FALSE;
  1043. break;
  1044. case '?':
  1045. if (escaping) {
  1046. g_string_append (out, "\\?");
  1047. }
  1048. else {
  1049. g_string_append (out, ".");
  1050. }
  1051. escaping = FALSE;
  1052. break;
  1053. case '.':
  1054. case '(':
  1055. case ')':
  1056. case '+':
  1057. case '|':
  1058. case '^':
  1059. case '$':
  1060. case '@':
  1061. case '%':
  1062. g_string_append_c (out, '\\');
  1063. g_string_append_c (out, *gl);
  1064. escaping = FALSE;
  1065. break;
  1066. case '\\':
  1067. if (escaping) {
  1068. g_string_append (out, "\\\\");
  1069. escaping = FALSE;
  1070. }
  1071. else {
  1072. escaping = TRUE;
  1073. }
  1074. break;
  1075. case '{':
  1076. if (escaping) {
  1077. g_string_append (out, "\\{");
  1078. }
  1079. else {
  1080. g_string_append_c (out, '(');
  1081. nbraces++;
  1082. }
  1083. escaping = FALSE;
  1084. break;
  1085. case '}':
  1086. if (nbraces > 0 && !escaping) {
  1087. g_string_append_c (out, ')');
  1088. nbraces--;
  1089. }
  1090. else if (escaping) {
  1091. g_string_append (out, "\\}");
  1092. }
  1093. else {
  1094. g_string_append (out, "}");
  1095. }
  1096. escaping = FALSE;
  1097. break;
  1098. case ',':
  1099. if (nbraces > 0 && !escaping) {
  1100. g_string_append_c (out, '|');
  1101. }
  1102. else if (escaping) {
  1103. g_string_append (out, "\\,");
  1104. }
  1105. else {
  1106. g_string_append_c (out, ',');
  1107. }
  1108. break;
  1109. default:
  1110. escaping = FALSE;
  1111. g_string_append_c (out, *gl);
  1112. break;
  1113. }
  1114. gl ++;
  1115. }
  1116. g_string_append_c (out, '$');
  1117. re = rspamd_regexp_new (out->str, "i", err);
  1118. g_string_free (out, TRUE);
  1119. return re;
  1120. }