Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. guint max_hits;
  64. gint flags;
  65. gint pcre_flags;
  66. gint ncaptures;
  67. gint nbackref;
  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 (const gchar *pattern, const gchar *flags,
  271. GError **err)
  272. {
  273. const gchar *start = pattern, *end, *flags_str = 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 (flags == NULL) {
  288. /* We need to parse pattern and detect flags set */
  289. if (*start == '/') {
  290. sep = '/';
  291. }
  292. else if (*start == 'm') {
  293. start ++;
  294. sep = *start;
  295. /* Paired braces */
  296. if (sep == '{') {
  297. sep = '}';
  298. }
  299. rspamd_flags |= RSPAMD_REGEXP_FLAG_FULL_MATCH;
  300. }
  301. if (sep == '\0' || g_ascii_isalnum (sep)) {
  302. /* We have no flags, no separators and just use all line as expr */
  303. start = pattern;
  304. end = start + strlen (pattern);
  305. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_FULL_MATCH;
  306. }
  307. else {
  308. end = strrchr (pattern, sep);
  309. if (end == NULL || end <= start) {
  310. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  311. "pattern is not enclosed with %c: %s",
  312. sep, pattern);
  313. return NULL;
  314. }
  315. flags_str = end + 1;
  316. start ++;
  317. }
  318. }
  319. else {
  320. /* Strictly check all flags */
  321. strict_flags = TRUE;
  322. start = pattern;
  323. end = pattern + strlen (pattern);
  324. flags_str = flags;
  325. }
  326. rspamd_flags |= RSPAMD_REGEXP_FLAG_RAW;
  327. #ifndef WITH_PCRE2
  328. regexp_flags &= ~PCRE_FLAG(UTF8);
  329. regexp_flags |= PCRE_FLAG(NEWLINE_ANYCRLF);
  330. #else
  331. regexp_flags &= ~PCRE_FLAG(UTF);
  332. #endif
  333. if (flags_str != NULL) {
  334. while (*flags_str) {
  335. switch (*flags_str) {
  336. case 'i':
  337. regexp_flags |= PCRE_FLAG(CASELESS);
  338. break;
  339. case 'm':
  340. regexp_flags |= PCRE_FLAG(MULTILINE);
  341. break;
  342. case 's':
  343. regexp_flags |= PCRE_FLAG(DOTALL);
  344. break;
  345. case 'x':
  346. regexp_flags |= PCRE_FLAG(EXTENDED);
  347. break;
  348. case 'u':
  349. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_RAW;
  350. rspamd_flags |= RSPAMD_REGEXP_FLAG_UTF;
  351. #ifndef WITH_PCRE2
  352. regexp_flags |= PCRE_FLAG(UTF8);
  353. #else
  354. regexp_flags |= PCRE_FLAG(UTF);
  355. #endif
  356. explicit_utf = TRUE;
  357. break;
  358. case 'O':
  359. /* We optimize all regexps by default */
  360. rspamd_flags |= RSPAMD_REGEXP_FLAG_NOOPT;
  361. break;
  362. case 'r':
  363. rspamd_flags |= RSPAMD_REGEXP_FLAG_RAW;
  364. rspamd_flags &= ~RSPAMD_REGEXP_FLAG_UTF;
  365. #ifndef WITH_PCRE2
  366. regexp_flags &= ~PCRE_FLAG(UTF8);
  367. #else
  368. regexp_flags &= ~PCRE_FLAG(UTF);
  369. #endif
  370. break;
  371. default:
  372. if (strict_flags) {
  373. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  374. "invalid regexp flag: %c in pattern %s",
  375. *flags_str, pattern);
  376. return NULL;
  377. }
  378. msg_warn ("invalid flag '%c' in pattern %s", *flags_str, pattern);
  379. goto fin;
  380. break;
  381. }
  382. flags_str++;
  383. }
  384. }
  385. fin:
  386. real_pattern = g_malloc (end - start + 1);
  387. rspamd_strlcpy (real_pattern, start, end - start + 1);
  388. #ifndef WITH_PCRE2
  389. r = pcre_compile (real_pattern, regexp_flags,
  390. (const char **)&err_str, &err_off, NULL);
  391. (void)err_code;
  392. #else
  393. r = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED,
  394. regexp_flags,
  395. &err_code, &err_off, pcre2_ctx);
  396. if (r == NULL) {
  397. err_str = g_alloca (1024);
  398. memset (err_str, 0, 1024);
  399. pcre2_get_error_message (err_code, err_str, 1024);
  400. }
  401. #endif
  402. if (r == NULL) {
  403. g_set_error (err, rspamd_regexp_quark(), EINVAL,
  404. "regexp parsing error: '%s' at position %d; pattern: %s",
  405. err_str, (gint)err_off, real_pattern);
  406. g_free (real_pattern);
  407. return NULL;
  408. }
  409. /* Now allocate the target structure */
  410. res = g_malloc0 (sizeof (*res));
  411. REF_INIT_RETAIN (res, rspamd_regexp_dtor);
  412. res->flags = rspamd_flags;
  413. res->pattern = real_pattern;
  414. res->cache_id = RSPAMD_INVALID_ID;
  415. res->pcre_flags = regexp_flags;
  416. res->max_hits = 0;
  417. res->re = r;
  418. if (rspamd_flags & RSPAMD_REGEXP_FLAG_RAW) {
  419. res->raw_re = r;
  420. }
  421. else if (!explicit_utf) {
  422. #ifndef WITH_PCRE2
  423. res->raw_re = pcre_compile (real_pattern, regexp_flags & ~PCRE_FLAG(UTF8),
  424. (const char **)&err_str, &err_off, NULL);
  425. (void)err_code;
  426. #else
  427. res->raw_re = pcre2_compile (real_pattern, PCRE2_ZERO_TERMINATED,
  428. regexp_flags & ~PCRE_FLAG(UTF),
  429. &err_code, &err_off, pcre2_ctx);
  430. if (res->raw_re == NULL) {
  431. err_str = g_alloca (1024);
  432. memset (err_str, 0, 1024);
  433. pcre2_get_error_message (err_code, err_str, 1024);
  434. }
  435. #endif
  436. if (res->raw_re == NULL) {
  437. msg_warn ("raw regexp parsing error: '%s': '%s' at position %d",
  438. err_str, real_pattern, (gint)err_off);
  439. }
  440. }
  441. rspamd_regexp_post_process (res);
  442. rspamd_regexp_generate_id (pattern, flags, res->id);
  443. #ifndef WITH_PCRE2
  444. /* Check number of captures */
  445. if (pcre_fullinfo (res->raw_re, res->extra, PCRE_INFO_CAPTURECOUNT,
  446. &ncaptures) == 0) {
  447. res->ncaptures = ncaptures;
  448. }
  449. /* Check number of backrefs */
  450. if (pcre_fullinfo (res->raw_re, res->extra, PCRE_INFO_BACKREFMAX,
  451. &ncaptures) == 0) {
  452. res->nbackref = ncaptures;
  453. }
  454. #else
  455. /* Check number of captures */
  456. if (pcre2_pattern_info (res->raw_re, PCRE2_INFO_CAPTURECOUNT,
  457. &ncaptures) == 0) {
  458. res->ncaptures = ncaptures;
  459. }
  460. /* Check number of backrefs */
  461. if (pcre2_pattern_info (res->raw_re, PCRE2_INFO_BACKREFMAX,
  462. &ncaptures) == 0) {
  463. res->nbackref = ncaptures;
  464. }
  465. #endif
  466. return res;
  467. }
  468. #ifndef WITH_PCRE2
  469. gboolean
  470. rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len,
  471. const gchar **start, const gchar **end, gboolean raw,
  472. GArray *captures)
  473. {
  474. pcre *r;
  475. pcre_extra *ext;
  476. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  477. pcre_jit_stack *st = NULL;
  478. #endif
  479. const gchar *mt;
  480. gsize remain = 0;
  481. gint rc, match_flags = 0, *ovec, ncaptures, i;
  482. g_assert (re != NULL);
  483. g_assert (text != NULL);
  484. if (len == 0) {
  485. len = strlen (text);
  486. }
  487. if (end != NULL && *end != NULL) {
  488. /* Incremental search */
  489. mt = (*end);
  490. if ((gint)len > (mt - text)) {
  491. remain = len - (mt - text);
  492. }
  493. }
  494. else {
  495. mt = text;
  496. remain = len;
  497. }
  498. if (remain == 0) {
  499. return FALSE;
  500. }
  501. match_flags = PCRE_NEWLINE_ANYCRLF;
  502. if ((re->flags & RSPAMD_REGEXP_FLAG_RAW) || raw) {
  503. r = re->raw_re;
  504. ext = re->raw_extra;
  505. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  506. st = global_re_cache->jstack;
  507. #endif
  508. }
  509. else {
  510. r = re->re;
  511. ext = re->extra;
  512. #if defined(HAVE_PCRE_JIT) && defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  513. if (rspamd_fast_utf8_validate (mt, remain) == 0) {
  514. st = global_re_cache->jstack;
  515. }
  516. else {
  517. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  518. return FALSE;
  519. }
  520. #endif
  521. }
  522. if (r == NULL) {
  523. /* Invalid regexp type for the specified input */
  524. return FALSE;
  525. }
  526. ncaptures = (re->ncaptures + 1) * 3;
  527. ovec = g_alloca (sizeof (gint) * ncaptures);
  528. if (!(re->flags & RSPAMD_REGEXP_FLAG_NOOPT)) {
  529. #ifdef HAVE_PCRE_JIT
  530. # if defined(HAVE_PCRE_JIT_FAST) && !defined(DISABLE_JIT_FAST)
  531. /* XXX: flags seems to be broken with jit fast path */
  532. g_assert (remain > 0);
  533. g_assert (mt != NULL);
  534. if (st != NULL && !(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  535. rc = pcre_jit_exec (r, ext, mt, remain, 0, 0, ovec,
  536. ncaptures, st);
  537. }
  538. else {
  539. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  540. ncaptures);
  541. }
  542. # else
  543. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  544. ncaptures);
  545. #endif
  546. #else
  547. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  548. ncaptures);
  549. #endif
  550. }
  551. else {
  552. rc = pcre_exec (r, ext, mt, remain, 0, match_flags, ovec,
  553. ncaptures);
  554. }
  555. if (rc >= 0) {
  556. if (start) {
  557. *start = mt + ovec[0];
  558. }
  559. if (end) {
  560. *end = mt + ovec[1];
  561. }
  562. if (captures != NULL && rc >= 1) {
  563. struct rspamd_re_capture *elt;
  564. g_assert (g_array_get_element_size (captures) ==
  565. sizeof (struct rspamd_re_capture));
  566. g_array_set_size (captures, rc);
  567. for (i = 0; i < rc; i ++) {
  568. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  569. elt->p = mt + ovec[i * 2];
  570. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  571. }
  572. }
  573. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  574. /* We also ensure that the match is full */
  575. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  576. return FALSE;
  577. }
  578. }
  579. return TRUE;
  580. }
  581. return FALSE;
  582. }
  583. #else
  584. /* PCRE 2 version */
  585. gboolean
  586. rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len,
  587. const gchar **start, const gchar **end, gboolean raw,
  588. GArray *captures)
  589. {
  590. pcre2_match_data *match_data;
  591. pcre2_match_context *mcontext;
  592. PCRE_T *r;
  593. const gchar *mt;
  594. gsize remain = 0, *ovec;
  595. gint rc, match_flags, novec, i;
  596. gboolean ret = FALSE;
  597. g_assert (re != NULL);
  598. g_assert (text != NULL);
  599. if (len == 0) {
  600. len = strlen (text);
  601. }
  602. if (end != NULL && *end != NULL) {
  603. /* Incremental search */
  604. mt = (*end);
  605. if ((gint)len > (mt - text)) {
  606. remain = len - (mt - text);
  607. }
  608. }
  609. else {
  610. mt = text;
  611. remain = len;
  612. }
  613. if (remain == 0) {
  614. return FALSE;
  615. }
  616. match_flags = 0;
  617. if (raw || re->re == re->raw_re) {
  618. r = re->raw_re;
  619. mcontext = re->raw_mcontext;
  620. }
  621. else {
  622. r = re->re;
  623. mcontext = re->mcontext;
  624. }
  625. if (r == NULL) {
  626. /* Invalid regexp type for the specified input */
  627. return FALSE;
  628. }
  629. match_data = pcre2_match_data_create (re->ncaptures + 1, NULL);
  630. #ifdef HAVE_PCRE_JIT
  631. if (!(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT) && can_jit) {
  632. if (re->re != re->raw_re && rspamd_fast_utf8_validate (mt, remain) != 0) {
  633. msg_err ("bad utf8 input for JIT re '%s'", re->pattern);
  634. return FALSE;
  635. }
  636. rc = pcre2_jit_match (r, mt, remain, 0, match_flags, match_data,
  637. mcontext);
  638. }
  639. else {
  640. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  641. mcontext);
  642. }
  643. #else
  644. rc = pcre2_match (r, mt, remain, 0, match_flags, match_data,
  645. mcontext);
  646. #endif
  647. if (rc >= 0) {
  648. novec = pcre2_get_ovector_count (match_data);
  649. ovec = pcre2_get_ovector_pointer (match_data);
  650. if (start) {
  651. *start = mt + ovec[0];
  652. }
  653. if (end) {
  654. *end = mt + ovec[1];
  655. }
  656. if (captures != NULL && novec >= 1) {
  657. struct rspamd_re_capture *elt;
  658. g_assert (g_array_get_element_size (captures) ==
  659. sizeof (struct rspamd_re_capture));
  660. g_array_set_size (captures, novec);
  661. for (i = 0; i < novec; i ++) {
  662. elt = &g_array_index (captures, struct rspamd_re_capture, i);
  663. elt->p = mt + ovec[i * 2];
  664. elt->len = (mt + ovec[i * 2 + 1]) - elt->p;
  665. }
  666. }
  667. ret = TRUE;
  668. if (re->flags & RSPAMD_REGEXP_FLAG_FULL_MATCH) {
  669. /* We also ensure that the match is full */
  670. if (ovec[0] != 0 || (guint)ovec[1] < len) {
  671. ret = FALSE;
  672. }
  673. }
  674. }
  675. pcre2_match_data_free (match_data);
  676. return ret;
  677. }
  678. #endif
  679. const char*
  680. rspamd_regexp_get_pattern (rspamd_regexp_t *re)
  681. {
  682. g_assert (re != NULL);
  683. return re->pattern;
  684. }
  685. guint
  686. rspamd_regexp_set_flags (rspamd_regexp_t *re, guint new_flags)
  687. {
  688. guint old_flags;
  689. g_assert (re != NULL);
  690. old_flags = re->flags;
  691. re->flags = new_flags;
  692. return old_flags;
  693. }
  694. guint
  695. rspamd_regexp_get_flags (rspamd_regexp_t *re)
  696. {
  697. g_assert (re != NULL);
  698. return re->flags;
  699. }
  700. guint
  701. rspamd_regexp_get_pcre_flags (rspamd_regexp_t *re)
  702. {
  703. g_assert (re != NULL);
  704. return re->pcre_flags;
  705. }
  706. gint
  707. rspamd_regexp_get_nbackrefs (rspamd_regexp_t *re)
  708. {
  709. g_assert (re != NULL);
  710. return re->nbackref;
  711. }
  712. gint
  713. rspamd_regexp_get_ncaptures (rspamd_regexp_t *re)
  714. {
  715. g_assert (re != NULL);
  716. return re->ncaptures;
  717. }
  718. guint
  719. rspamd_regexp_get_maxhits (rspamd_regexp_t *re)
  720. {
  721. g_assert (re != NULL);
  722. return re->max_hits;
  723. }
  724. guint
  725. rspamd_regexp_set_maxhits (rspamd_regexp_t *re, guint new_maxhits)
  726. {
  727. guint old_hits;
  728. g_assert (re != NULL);
  729. old_hits = re->max_hits;
  730. re->max_hits = new_maxhits;
  731. return old_hits;
  732. }
  733. guint64
  734. rspamd_regexp_get_cache_id (rspamd_regexp_t *re)
  735. {
  736. g_assert (re != NULL);
  737. return re->cache_id;
  738. }
  739. guint64
  740. rspamd_regexp_set_cache_id (rspamd_regexp_t *re, guint64 id)
  741. {
  742. guint64 old;
  743. g_assert (re != NULL);
  744. old = re->cache_id;
  745. re->cache_id = id;
  746. return old;
  747. }
  748. gboolean
  749. rspamd_regexp_match (rspamd_regexp_t *re, const gchar *text, gsize len,
  750. gboolean raw)
  751. {
  752. const gchar *start = NULL, *end = NULL;
  753. g_assert (re != NULL);
  754. g_assert (text != NULL);
  755. if (len == 0 && text != NULL) {
  756. len = strlen (text);
  757. }
  758. if (rspamd_regexp_search (re, text, len, &start, &end, raw, NULL)) {
  759. if (start == text && end == text + len) {
  760. return TRUE;
  761. }
  762. }
  763. return FALSE;
  764. }
  765. void
  766. rspamd_regexp_unref (rspamd_regexp_t *re)
  767. {
  768. REF_RELEASE (re);
  769. }
  770. rspamd_regexp_t*
  771. rspamd_regexp_ref (rspamd_regexp_t *re)
  772. {
  773. g_assert (re != NULL);
  774. REF_RETAIN (re);
  775. return re;
  776. }
  777. void
  778. rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud)
  779. {
  780. g_assert (re != NULL);
  781. re->ud = ud;
  782. }
  783. gpointer
  784. rspamd_regexp_get_ud (rspamd_regexp_t *re)
  785. {
  786. g_assert (re != NULL);
  787. return re->ud;
  788. }
  789. gboolean
  790. rspamd_regexp_equal (gconstpointer a, gconstpointer b)
  791. {
  792. const guchar *ia = a, *ib = b;
  793. return (memcmp (ia, ib, sizeof (regexp_id_t)) == 0);
  794. }
  795. guint32
  796. rspamd_regexp_hash (gconstpointer a)
  797. {
  798. const guchar *ia = a;
  799. guint32 res;
  800. memcpy (&res, ia, sizeof (res));
  801. return res;
  802. }
  803. gboolean
  804. rspamd_regexp_cmp (gconstpointer a, gconstpointer b)
  805. {
  806. const guchar *ia = a, *ib = b;
  807. return memcmp (ia, ib, sizeof (regexp_id_t));
  808. }
  809. struct rspamd_regexp_cache*
  810. rspamd_regexp_cache_new (void)
  811. {
  812. struct rspamd_regexp_cache *ncache;
  813. ncache = g_malloc0 (sizeof (*ncache));
  814. ncache->tbl = g_hash_table_new_full (rspamd_regexp_hash, rspamd_regexp_equal,
  815. NULL, (GDestroyNotify)rspamd_regexp_unref);
  816. #ifdef HAVE_PCRE_JIT
  817. #ifdef WITH_PCRE2
  818. ncache->jstack = pcre2_jit_stack_create (32 * 1024, 1024 * 1024, NULL);
  819. #else
  820. ncache->jstack = pcre_jit_stack_alloc (32 * 1024, 1024 * 1024);
  821. #endif
  822. #endif
  823. return ncache;
  824. }
  825. rspamd_regexp_t*
  826. rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache,
  827. const gchar *pattern,
  828. const gchar *flags)
  829. {
  830. rspamd_regexp_t *res = NULL;
  831. regexp_id_t id;
  832. if (cache == NULL) {
  833. rspamd_regexp_library_init (NULL);
  834. cache = global_re_cache;
  835. }
  836. g_assert (cache != NULL);
  837. rspamd_regexp_generate_id (pattern, flags, id);
  838. res = g_hash_table_lookup (cache->tbl, id);
  839. return res;
  840. }
  841. rspamd_regexp_t*
  842. rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache,
  843. const gchar *pattern,
  844. const gchar *flags, GError **err)
  845. {
  846. rspamd_regexp_t *res;
  847. if (cache == NULL) {
  848. rspamd_regexp_library_init (NULL);
  849. cache = global_re_cache;
  850. }
  851. g_assert (cache != NULL);
  852. res = rspamd_regexp_cache_query (cache, pattern, flags);
  853. if (res != NULL) {
  854. return res;
  855. }
  856. res = rspamd_regexp_new (pattern, flags, err);
  857. if (res) {
  858. /* REF_RETAIN (res); */
  859. g_hash_table_insert (cache->tbl, res->id, res);
  860. }
  861. return res;
  862. }
  863. void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache,
  864. const gchar *pattern,
  865. const gchar *flags, rspamd_regexp_t *re)
  866. {
  867. g_assert (re != NULL);
  868. g_assert (pattern != NULL);
  869. if (cache == NULL) {
  870. rspamd_regexp_library_init (NULL);
  871. cache = global_re_cache;
  872. }
  873. g_assert (cache != NULL);
  874. /* Generate custom id */
  875. rspamd_regexp_generate_id (pattern, flags, re->id);
  876. REF_RETAIN (re);
  877. g_hash_table_insert (cache->tbl, re->id, re);
  878. }
  879. gboolean
  880. rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache,
  881. rspamd_regexp_t *re)
  882. {
  883. if (cache == NULL) {
  884. cache = global_re_cache;
  885. }
  886. g_assert (cache != NULL);
  887. g_assert (re != NULL);
  888. return g_hash_table_remove (cache->tbl, re->id);
  889. }
  890. void
  891. rspamd_regexp_cache_destroy (struct rspamd_regexp_cache *cache)
  892. {
  893. if (cache != NULL) {
  894. g_hash_table_destroy (cache->tbl);
  895. #ifdef HAVE_PCRE_JIT
  896. #ifdef WITH_PCRE2
  897. if (cache->jstack) {
  898. pcre2_jit_stack_free (cache->jstack);
  899. }
  900. #else
  901. if (cache->jstack) {
  902. pcre_jit_stack_free (cache->jstack);
  903. }
  904. #endif
  905. #endif
  906. g_free (cache);
  907. }
  908. }
  909. RSPAMD_CONSTRUCTOR (rspamd_re_static_pool_ctor)
  910. {
  911. global_re_cache = rspamd_regexp_cache_new ();
  912. #ifdef WITH_PCRE2
  913. pcre2_ctx = pcre2_compile_context_create (NULL);
  914. pcre2_set_newline (pcre2_ctx, PCRE_FLAG(NEWLINE_ANY));
  915. #endif
  916. }
  917. RSPAMD_DESTRUCTOR (rspamd_re_static_pool_dtor)
  918. {
  919. rspamd_regexp_cache_destroy (global_re_cache);
  920. #ifdef WITH_PCRE2
  921. pcre2_compile_context_free (pcre2_ctx);
  922. #endif
  923. }
  924. void
  925. rspamd_regexp_library_init (struct rspamd_config *cfg)
  926. {
  927. if (cfg) {
  928. if (cfg->disable_pcre_jit) {
  929. can_jit = FALSE;
  930. check_jit = FALSE;
  931. }
  932. else if (!can_jit) {
  933. check_jit = TRUE;
  934. }
  935. }
  936. if (check_jit) {
  937. #ifdef HAVE_PCRE_JIT
  938. gint jit, rc;
  939. gchar *str;
  940. #ifndef WITH_PCRE2
  941. rc = pcre_config (PCRE_CONFIG_JIT, &jit);
  942. #else
  943. rc = pcre2_config (PCRE2_CONFIG_JIT, &jit);
  944. #endif
  945. if (rc == 0 && jit == 1) {
  946. #ifndef WITH_PCRE2
  947. #ifdef PCRE_CONFIG_JITTARGET
  948. pcre_config (PCRE_CONFIG_JITTARGET, &str);
  949. msg_info ("pcre is compiled with JIT for %s", str);
  950. #else
  951. msg_info ("pcre is compiled with JIT for unknown target");
  952. #endif
  953. #else
  954. rc = pcre2_config (PCRE2_CONFIG_JITTARGET, NULL);
  955. if (rc > 0) {
  956. str = g_alloca (rc);
  957. pcre2_config (PCRE2_CONFIG_JITTARGET, str);
  958. msg_info ("pcre2 is compiled with JIT for %s", str);
  959. }
  960. else {
  961. msg_info ("pcre2 is compiled with JIT for unknown");
  962. }
  963. #endif /* WITH_PCRE2 */
  964. if (getenv ("VALGRIND") == NULL) {
  965. can_jit = TRUE;
  966. } else {
  967. msg_info ("disabling PCRE jit as it does not play well with valgrind");
  968. can_jit = FALSE;
  969. }
  970. } else {
  971. msg_info ("pcre is compiled without JIT support, so many optimizations"
  972. " are impossible");
  973. can_jit = FALSE;
  974. }
  975. #else
  976. msg_info ("pcre is too old and has no JIT support, so many optimizations"
  977. " are impossible");
  978. can_jit = FALSE;
  979. #endif
  980. check_jit = FALSE;
  981. }
  982. }
  983. gpointer
  984. rspamd_regexp_get_id (rspamd_regexp_t *re)
  985. {
  986. g_assert (re != NULL);
  987. return re->id;
  988. }
  989. gpointer
  990. rspamd_regexp_get_class (rspamd_regexp_t *re)
  991. {
  992. g_assert (re != NULL);
  993. return re->re_class;
  994. }
  995. gpointer
  996. rspamd_regexp_set_class (rspamd_regexp_t *re, gpointer re_class)
  997. {
  998. gpointer old_class;
  999. g_assert (re != NULL);
  1000. old_class = re->re_class;
  1001. re->re_class = re_class;
  1002. return old_class;
  1003. }
  1004. rspamd_regexp_t *
  1005. rspamd_regexp_from_glob (const gchar *gl, gsize sz, GError **err)
  1006. {
  1007. GString *out;
  1008. rspamd_regexp_t *re;
  1009. const gchar *end;
  1010. gboolean escaping = FALSE;
  1011. gint nbraces = 0;
  1012. g_assert (gl != NULL);
  1013. if (sz == 0) {
  1014. sz = strlen (gl);
  1015. }
  1016. end = gl + sz;
  1017. out = g_string_sized_new (sz + 2);
  1018. g_string_append_c (out, '^');
  1019. while (gl < end) {
  1020. switch (*gl) {
  1021. case '*':
  1022. if (escaping) {
  1023. g_string_append (out, "\\*");
  1024. }
  1025. else {
  1026. g_string_append (out, ".*");
  1027. }
  1028. escaping = FALSE;
  1029. break;
  1030. case '?':
  1031. if (escaping) {
  1032. g_string_append (out, "\\?");
  1033. }
  1034. else {
  1035. g_string_append (out, ".");
  1036. }
  1037. escaping = FALSE;
  1038. break;
  1039. case '.':
  1040. case '(':
  1041. case ')':
  1042. case '+':
  1043. case '|':
  1044. case '^':
  1045. case '$':
  1046. case '@':
  1047. case '%':
  1048. g_string_append_c (out, '\\');
  1049. g_string_append_c (out, *gl);
  1050. escaping = FALSE;
  1051. break;
  1052. case '\\':
  1053. if (escaping) {
  1054. g_string_append (out, "\\\\");
  1055. escaping = FALSE;
  1056. }
  1057. else {
  1058. escaping = TRUE;
  1059. }
  1060. break;
  1061. case '{':
  1062. if (escaping) {
  1063. g_string_append (out, "\\{");
  1064. }
  1065. else {
  1066. g_string_append_c (out, '(');
  1067. nbraces++;
  1068. }
  1069. escaping = FALSE;
  1070. break;
  1071. case '}':
  1072. if (nbraces > 0 && !escaping) {
  1073. g_string_append_c (out, ')');
  1074. nbraces--;
  1075. }
  1076. else if (escaping) {
  1077. g_string_append (out, "\\}");
  1078. }
  1079. else {
  1080. g_string_append (out, "}");
  1081. }
  1082. escaping = FALSE;
  1083. break;
  1084. case ',':
  1085. if (nbraces > 0 && !escaping) {
  1086. g_string_append_c (out, '|');
  1087. }
  1088. else if (escaping) {
  1089. g_string_append (out, "\\,");
  1090. }
  1091. else {
  1092. g_string_append_c (out, ',');
  1093. }
  1094. break;
  1095. default:
  1096. escaping = FALSE;
  1097. g_string_append_c (out, *gl);
  1098. break;
  1099. }
  1100. gl ++;
  1101. }
  1102. g_string_append_c (out, '$');
  1103. re = rspamd_regexp_new (out->str, "i", err);
  1104. g_string_free (out, TRUE);
  1105. return re;
  1106. }