You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regexp.c 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. const int junk = 0xdeadbabe;
  491. g_assert (re != NULL);
  492. g_assert (text != NULL);
  493. if (len == 0) {
  494. len = strlen (text);
  495. }
  496. if (re->match_limit > 0 && len > re->match_limit) {
  497. len = re->match_limit;
  498. }
  499. if (end != NULL && *end != NULL) {
  500. /* Incremental search */
  501. mt = (*end);
  502. if ((gint)len > (mt - text)) {
  503. remain = len - (mt - text);
  504. }
  505. }
  506. else {
  507. mt = text;
  508. remain = len;
  509. }
  510. if (remain == 0) {
  511. return FALSE;
  512. }
  513. match_flags = PCRE_NEWLINE_ANYCRLF;
  514. if ((re->flags & RSPAMD_REGEXP_FLAG_RAW) || raw) {
  515. r = re->raw_re;
  516. ext = re->raw_extra;
  517. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  518. st = global_re_cache->jstack;
  519. #endif
  520. }
  521. else {
  522. r = re->re;
  523. ext = re->extra;
  524. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  525. if (rspamd_fast_utf8_validate (mt, remain) == 0) {
  526. st = global_re_cache->jstack;
  527. }
  528. else {
  529. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  530. return FALSE;
  531. }
  532. #endif
  533. }
  534. if (r == NULL) {
  535. /* Invalid regexp type for the specified input */
  536. return FALSE;
  537. }
  538. ncaptures = (re->ncaptures + 1) * 3;
  539. ovec = g_alloca (sizeof (gint) * ncaptures);
  540. for (i = 0; i < ncaptures; i ++) {
  541. ovec[i] = junk;
  542. }
  543. if (!(re->flags & RSPAMD_REGEXP_FLAG_NOOPT)) {
  544. #ifdef HAVE_PCRE_JIT
  545. # if defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  546. /* XXX: flags seems to be broken with jit fast path */
  547. g_assert (remain > 0);
  548. g_assert (mt != NULL);
  549. if (st != NULL && !(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  550. rc = pcre_jit_exec (r, ext, mt, remain, 0, 0, ovec,
  551. ncaptures, st);
  552. }
  553. else {
  554. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  555. ncaptures);
  556. }
  557. # else
  558. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  559. ncaptures);
  560. #endif
  561. #else
  562. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  563. ncaptures);
  564. #endif
  565. }
  566. else {
  567. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  568. ncaptures);
  569. }
  570. if (rc >= 0) {
  571. if (rc > 0) {
  572. if (start) {
  573. *start = mt + ovec[0];
  574. }
  575. if (end) {
  576. *end = mt + ovec[1];
  577. }
  578. }
  579. else {
  580. if (start) {
  581. *start = mt;
  582. }
  583. if (end) {
  584. *end = mt + remain;
  585. }
  586. }
  587. if (captures != NULL && rc >= 1) {
  588. struct rspamd_re_capture *elt;
  589. g_assert (g_array_get_element_size (captures) ==
  590. sizeof (struct rspamd_re_capture));
  591. g_array_set_size (captures, rc);
  592. for (i = 0; i < rc; i ++) {
  593. if (ovec[i * 2] != junk && ovec[i * 2] >= 0) {
  594. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  595. elt->p = mt + ovec[i * 2];
  596. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  597. }
  598. else {
  599. /* Runtime match returned fewer captures than expected */
  600. g_array_set_size (captures, i);
  601. break;
  602. }
  603. }
  604. }
  605. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  606. /* We also ensure that the match is full */
  607. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  608. return FALSE;
  609. }
  610. }
  611. return TRUE;
  612. }
  613. return FALSE;
  614. }
  615. #else
  616. /* PCRE 2 version */
  617. gboolean
  618. rspamd_regexp_search (const rspamd_regexp_t *re, const gchar *text, gsize len,
  619. const gchar **start, const gchar **end, gboolean raw,
  620. GArray *captures)
  621. {
  622. pcre2_match_data *match_data;
  623. pcre2_match_context *mcontext;
  624. PCRE_T *r;
  625. const gchar *mt;
  626. PCRE2_SIZE remain = 0, *ovec;
  627. const PCRE2_SIZE junk = 0xdeadbabeeeeeeeeULL;
  628. gint rc, match_flags, novec, i;
  629. gboolean ret = FALSE;
  630. g_assert (re != NULL);
  631. g_assert (text != NULL);
  632. if (len == 0) {
  633. len = strlen (text);
  634. }
  635. if (re->match_limit > 0 && len > re->match_limit) {
  636. len = re->match_limit;
  637. }
  638. if (end != NULL && *end != NULL) {
  639. /* Incremental search */
  640. mt = (*end);
  641. if ((gint)len > (mt - text)) {
  642. remain = len - (mt - text);
  643. }
  644. }
  645. else {
  646. mt = text;
  647. remain = len;
  648. }
  649. if (remain == 0) {
  650. return FALSE;
  651. }
  652. match_flags = 0;
  653. if (raw || re->re == re->raw_re) {
  654. r = re->raw_re;
  655. mcontext = re->raw_mcontext;
  656. }
  657. else {
  658. r = re->re;
  659. mcontext = re->mcontext;
  660. }
  661. if (r == NULL) {
  662. /* Invalid regexp type for the specified input */
  663. return FALSE;
  664. }
  665. match_data = pcre2_match_data_create (re->ncaptures + 1, NULL);
  666. novec = pcre2_get_ovector_count (match_data);
  667. ovec = pcre2_get_ovector_pointer (match_data);
  668. /* Fill ovec with crap, so we can stop if actual matches is less than announced */
  669. for (i = 0; i < novec; i ++) {
  670. ovec[i * 2] = junk;
  671. ovec[i * 2 + 1] = junk;
  672. }
  673. #ifdef HAVE_PCRE_JIT
  674. if (!(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  675. if (re->re != re->raw_re && rspamd_fast_utf8_validate (mt, remain) != 0) {
  676. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  677. return FALSE;
  678. }
  679. rc = pcre2_jit_match (r, mt, remain, 0, match_flags, match_data,
  680. mcontext);
  681. }
  682. else {
  683. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  684. mcontext);
  685. }
  686. #else
  687. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  688. mcontext);
  689. #endif
  690. if (rc >= 0) {
  691. if (novec > 0) {
  692. if (start) {
  693. *start = mt + ovec[0];
  694. }
  695. if (end) {
  696. *end = mt + ovec[1];
  697. }
  698. }
  699. else {
  700. if (start) {
  701. *start = mt;
  702. }
  703. if (end) {
  704. *end = mt + remain;
  705. }
  706. }
  707. if (captures != NULL && novec >= 1) {
  708. struct rspamd_re_capture *elt;
  709. g_assert (g_array_get_element_size (captures) ==
  710. sizeof (struct rspamd_re_capture));
  711. g_array_set_size (captures, novec);
  712. for (i = 0; i < novec; i ++) {
  713. if (ovec[i * 2] != junk && ovec[i * 2] != PCRE2_UNSET) {
  714. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  715. elt->p = mt + ovec[i * 2];
  716. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  717. }
  718. else {
  719. g_array_set_size (captures, i);
  720. break;
  721. }
  722. }
  723. }
  724. ret = TRUE;
  725. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  726. /* We also ensure that the match is full */
  727. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  728. ret = FALSE;
  729. }
  730. }
  731. }
  732. pcre2_match_data_free (match_data);
  733. return ret;
  734. }
  735. #endif
  736. const char*
  737. rspamd_regexp_get_pattern (const rspamd_regexp_t *re)
  738. {
  739. g_assert (re != NULL);
  740. return re->pattern;
  741. }
  742. guint
  743. rspamd_regexp_set_flags (rspamd_regexp_t *re, guint new_flags)
  744. {
  745. guint old_flags;
  746. g_assert (re != NULL);
  747. old_flags = re->flags;
  748. re->flags = new_flags;
  749. return old_flags;
  750. }
  751. guint
  752. rspamd_regexp_get_flags (const rspamd_regexp_t *re)
  753. {
  754. g_assert (re != NULL);
  755. return re->flags;
  756. }
  757. guint
  758. rspamd_regexp_get_pcre_flags (const rspamd_regexp_t *re)
  759. {
  760. g_assert (re != NULL);
  761. return re->pcre_flags;
  762. }
  763. guint
  764. rspamd_regexp_get_maxhits (const rspamd_regexp_t *re)
  765. {
  766. g_assert (re != NULL);
  767. return re->max_hits;
  768. }
  769. guint
  770. rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits)
  771. {
  772. guint old_hits;
  773. g_assert (re != NULL);
  774. old_hits = re->max_hits;
  775. re->max_hits = new_maxhits;
  776. return old_hits;
  777. }
  778. guint64
  779. rspamd_regexp_get_cache_id (const rspamd_regexp_t *re)
  780. {
  781. g_assert (re != NULL);
  782. return re->cache_id;
  783. }
  784. guint64
  785. rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id)
  786. {
  787. guint64 old;
  788. g_assert (re != NULL);
  789. old = re->cache_id;
  790. re->cache_id = id;
  791. return old;
  792. }
  793. gsize
  794. rspamd_regexp_get_match_limit (const rspamd_regexp_t *re)
  795. {
  796. g_assert (re != NULL);
  797. return re->match_limit;
  798. }
  799. gsize
  800. rspamd_regexp_set_match_limit (rspamd_regexp_t *re, gsize lim)
  801. {
  802. gsize old;
  803. g_assert (re != NULL);
  804. old = re->match_limit;
  805. re->match_limit = lim;
  806. return old;
  807. }
  808. gboolean
  809. rspamd_regexp_match (const rspamd_regexp_t *re, const gchar *text, gsize len,
  810. gboolean raw)
  811. {
  812. const gchar *start = NULL, *end = NULL;
  813. g_assert (re != NULL);
  814. g_assert (text != NULL);
  815. if (len == 0) {
  816. len = strlen (text);
  817. }
  818. if (rspamd_regexp_search (re, text, len, &start, &end, raw, NULL)) {
  819. if (start == text && end == text + len) {
  820. return TRUE;
  821. }
  822. }
  823. return FALSE;
  824. }
  825. void
  826. rspamd_regexp_unref (rspamd_regexp_t *re)
  827. {
  828. REF_RELEASE (re);
  829. }
  830. rspamd_regexp_t*
  831. rspamd_regexp_ref (rspamd_regexp_t *re)
  832. {
  833. g_assert (re != NULL);
  834. REF_RETAIN (re);
  835. return re;
  836. }
  837. void
  838. rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud)
  839. {
  840. g_assert (re != NULL);
  841. re->ud = ud;
  842. }
  843. gpointer
  844. rspamd_regexp_get_ud (const rspamd_regexp_t *re)
  845. {
  846. g_assert (re != NULL);
  847. return re->ud;
  848. }
  849. gboolean
  850. rspamd_regexp_equal (gconstpointer a, gconstpointer b)
  851. {
  852. const guchar *ia = a, *ib = b;
  853. return (memcmp (ia, ib, sizeof (regexp_id_t)) == 0);
  854. }
  855. guint32
  856. rspamd_regexp_hash (gconstpointer a)
  857. {
  858. const guchar *ia = a;
  859. guint32 res;
  860. memcpy (&res, ia, sizeof (res));
  861. return res;
  862. }
  863. gboolean
  864. rspamd_regexp_cmp (gconstpointer a, gconstpointer b)
  865. {
  866. const guchar *ia = a, *ib = b;
  867. return memcmp (ia, ib, sizeof (regexp_id_t));
  868. }
  869. struct rspamd_regexp_cache*
  870. rspamd_regexp_cache_new (void)
  871. {
  872. struct rspamd_regexp_cache *ncache;
  873. ncache = g_malloc0 (sizeof (*ncache));
  874. ncache->tbl = g_hash_table_new_full (rspamd_regexp_hash, rspamd_regexp_equal,
  875. NULL, (GDestroyNotify)rspamd_regexp_unref);
  876. #ifdef HAVE_PCRE_JIT
  877. #ifdef WITH_PCRE2
  878. ncache->jstack = pcre2_jit_stack_create (32 * 1024, 1024 * 1024, NULL);
  879. #else
  880. ncache->jstack = pcre_jit_stack_alloc (32 * 1024, 1024 * 1024);
  881. #endif
  882. #endif
  883. return ncache;
  884. }
  885. rspamd_regexp_t*
  886. rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache,
  887. const gchar *pattern,
  888. const gchar *flags)
  889. {
  890. rspamd_regexp_t *res = NULL;
  891. regexp_id_t id;
  892. if (cache == NULL) {
  893. rspamd_regexp_library_init (NULL);
  894. cache = global_re_cache;
  895. }
  896. g_assert (cache != NULL);
  897. rspamd_regexp_generate_id (pattern, flags, id);
  898. res = g_hash_table_lookup (cache->tbl, id);
  899. return res;
  900. }
  901. rspamd_regexp_t*
  902. rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
  903. const gchar *pattern,
  904. const gchar *flags, GError **err)
  905. {
  906. rspamd_regexp_t *res;
  907. if (cache == NULL) {
  908. rspamd_regexp_library_init (NULL);
  909. cache = global_re_cache;
  910. }
  911. g_assert (cache != NULL);
  912. res = rspamd_regexp_cache_query (cache, pattern, flags);
  913. if (res != NULL) {
  914. return res;
  915. }
  916. res = rspamd_regexp_new (pattern, flags, err);
  917. if (res) {
  918. /* REF_RETAIN (res); */
  919. g_hash_table_insert (cache->tbl, res->id, res);
  920. }
  921. return res;
  922. }
  923. void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache,
  924. const gchar *pattern,
  925. const gchar *flags, rspamd_regexp_t *re)
  926. {
  927. g_assert (re != NULL);
  928. g_assert (pattern != NULL);
  929. if (cache == NULL) {
  930. rspamd_regexp_library_init (NULL);
  931. cache = global_re_cache;
  932. }
  933. g_assert (cache != NULL);
  934. /* Generate custom id */
  935. rspamd_regexp_generate_id (pattern, flags, re->id);
  936. REF_RETAIN (re);
  937. g_hash_table_insert (cache->tbl, re->id, re);
  938. }
  939. gboolean
  940. rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache,
  941. rspamd_regexp_t *re)
  942. {
  943. if (cache == NULL) {
  944. cache = global_re_cache;
  945. }
  946. g_assert (cache != NULL);
  947. g_assert (re != NULL);
  948. return g_hash_table_remove (cache->tbl, re->id);
  949. }
  950. void
  951. rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
  952. {
  953. if (cache != NULL) {
  954. g_hash_table_destroy (cache->tbl);
  955. #ifdef HAVE_PCRE_JIT
  956. #ifdef WITH_PCRE2
  957. if (cache->jstack) {
  958. pcre2_jit_stack_free (cache->jstack);
  959. }
  960. #else
  961. if (cache->jstack) {
  962. pcre_jit_stack_free (cache->jstack);
  963. }
  964. #endif
  965. #endif
  966. g_free (cache);
  967. }
  968. }
  969. RSPAMD_CONSTRUCTOR (rspamd_re_static_pool_ctor)
  970. {
  971. global_re_cache = rspamd_regexp_cache_new ();
  972. #ifdef WITH_PCRE2
  973. pcre2_ctx = pcre2_compile_context_create (NULL);
  974. pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY));
  975. #endif
  976. }
  977. RSPAMD_DESTRUCTOR (rspamd_re_static_pool_dtor)
  978. {
  979. rspamd_regexp_cache_destroy (global_re_cache);
  980. #ifdef WITH_PCRE2
  981. pcre2_compile_context_free (pcre2_ctx);
  982. #endif
  983. }
  984. void
  985. rspamd_regexp_library_init (struct rspamd_config *cfg)
  986. {
  987. if (cfg) {
  988. if (cfg->disable_pcre_jit) {
  989. can_jit = FALSE;
  990. check_jit = FALSE;
  991. }
  992. else if (!can_jit) {
  993. check_jit = TRUE;
  994. }
  995. }
  996. if (check_jit) {
  997. #ifdef HAVE_PCRE_JIT
  998. gint jit, rc;
  999. gchar *str;
  1000. #ifndef WITH_PCRE2
  1001. rc = pcre_config (PCRE_CONFIG_JIT, &jit);
  1002. #else
  1003. rc = pcre2_config (PCRE2_CONFIG_JIT, &jit);
  1004. #endif
  1005. if (rc == 0 && jit == 1) {
  1006. #ifndef WITH_PCRE2
  1007. #ifdef PCRE_CONFIG_JITTARGET
  1008. pcre_config (PCRE_CONFIG_JITTARGET, &str);
  1009. msg_info ("pcre is compiled with JIT for %s", str);
  1010. #else
  1011. msg_info ("pcre is compiled with JIT for unknown target");
  1012. #endif
  1013. #else
  1014. rc = pcre2_config (PCRE2_CONFIG_JITTARGET, NULL);
  1015. if (rc > 0) {
  1016. str = g_alloca (rc);
  1017. pcre2_config (PCRE2_CONFIG_JITTARGET, str);
  1018. msg_info ("pcre2 is compiled with JIT for %s", str);
  1019. }
  1020. else {
  1021. msg_info ("pcre2 is compiled with JIT for unknown");
  1022. }
  1023. #endif /* WITH_PCRE2 */
  1024. if (getenv ("VALGRIND") == NULL) {
  1025. can_jit = TRUE;
  1026. } else {
  1027. msg_info ("disabling PCRE jit as it does not play well with valgrind");
  1028. can_jit = FALSE;
  1029. }
  1030. } else {
  1031. msg_info ("pcre is compiled without JIT support, so many optimizations"
  1032. " are impossible");
  1033. can_jit = FALSE;
  1034. }
  1035. #else
  1036. msg_info ("pcre is too old and has no JIT support, so many optimizations"
  1037. " are impossible");
  1038. can_jit = FALSE;
  1039. #endif
  1040. check_jit = FALSE;
  1041. }
  1042. }
  1043. gpointer
  1044. rspamd_regexp_get_id (const rspamd_regexp_t *re)
  1045. {
  1046. g_assert (re != NULL);
  1047. return (gpointer)re->id;
  1048. }
  1049. gpointer
  1050. rspamd_regexp_get_class (const rspamd_regexp_t *re)
  1051. {
  1052. g_assert (re != NULL);
  1053. return re->re_class;
  1054. }
  1055. gpointer
  1056. rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class)
  1057. {
  1058. gpointer old_class;
  1059. g_assert (re != NULL);
  1060. old_class = re->re_class;
  1061. re->re_class = re_class;
  1062. return old_class;
  1063. }
  1064. rspamd_regexp_t *
  1065. rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err)
  1066. {
  1067. GString *out;
  1068. rspamd_regexp_t *re;
  1069. const gchar *end;
  1070. gboolean escaping = FALSE;
  1071. gint nbraces = 0;
  1072. g_assert (gl != NULL);
  1073. if (sz == 0) {
  1074. sz = strlen (gl);
  1075. }
  1076. end = gl + sz;
  1077. out = g_string_sized_new (sz + 2);
  1078. g_string_append_c (out, '^');
  1079. while (gl < end) {
  1080. switch (*gl) {
  1081. case '*':
  1082. if (escaping) {
  1083. g_string_append (out, "\\*");
  1084. }
  1085. else {
  1086. g_string_append (out, ".*");
  1087. }
  1088. escaping = FALSE;
  1089. break;
  1090. case '?':
  1091. if (escaping) {
  1092. g_string_append (out, "\\?");
  1093. }
  1094. else {
  1095. g_string_append (out, ".");
  1096. }
  1097. escaping = FALSE;
  1098. break;
  1099. case '.':
  1100. case '(':
  1101. case ')':
  1102. case '+':
  1103. case '|':
  1104. case '^':
  1105. case '$':
  1106. case '@':
  1107. case '%':
  1108. g_string_append_c (out, '\\');
  1109. g_string_append_c (out, *gl);
  1110. escaping = FALSE;
  1111. break;
  1112. case '\\':
  1113. if (escaping) {
  1114. g_string_append (out, "\\\\");
  1115. escaping = FALSE;
  1116. }
  1117. else {
  1118. escaping = TRUE;
  1119. }
  1120. break;
  1121. case '{':
  1122. if (escaping) {
  1123. g_string_append (out, "\\{");
  1124. }
  1125. else {
  1126. g_string_append_c (out, '(');
  1127. nbraces++;
  1128. }
  1129. escaping = FALSE;
  1130. break;
  1131. case '}':
  1132. if (nbraces > 0 && !escaping) {
  1133. g_string_append_c (out, ')');
  1134. nbraces--;
  1135. }
  1136. else if (escaping) {
  1137. g_string_append (out, "\\}");
  1138. }
  1139. else {
  1140. g_string_append (out, "}");
  1141. }
  1142. escaping = FALSE;
  1143. break;
  1144. case ',':
  1145. if (nbraces > 0 && !escaping) {
  1146. g_string_append_c (out, '|');
  1147. }
  1148. else if (escaping) {
  1149. g_string_append (out, "\\,");
  1150. }
  1151. else {
  1152. g_string_append_c (out, ',');
  1153. }
  1154. break;
  1155. default:
  1156. escaping = FALSE;
  1157. g_string_append_c (out, *gl);
  1158. break;
  1159. }
  1160. gl ++;
  1161. }
  1162. g_string_append_c (out, '$');
  1163. re = rspamd_regexp_new (out->str, "i", err);
  1164. g_string_free (out, TRUE);
  1165. return re;
  1166. }