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

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