Use non-releaseable memory for regex match strings. Bug 3047
[exim.git] / src / src / regex.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /*
6  * Copyright (c) The Exim Maintainers 2016 - 2023
7  * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
8  * License: GPL
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 /* Code for matching regular expressions against headers and body.
13  Called from acl.c. */
14
15 #include "exim.h"
16 #ifdef WITH_CONTENT_SCAN
17 #include <unistd.h>
18 #include <sys/mman.h>
19
20 /* Structure to hold a list of Regular expressions */
21 typedef struct pcre_list {
22   const pcre2_code *    re;
23   uschar *              pcre_text;
24   struct pcre_list *    next;
25 } pcre_list;
26
27 uschar regex_match_string_buffer[1024];
28
29 extern FILE *mime_stream;
30 extern uschar *mime_current_boundary;
31
32
33 static pcre_list *
34 compile(const uschar * list, BOOL cacheable, int * cntp)
35 {
36 int sep = 0, cnt = 0;
37 uschar * regex_string;
38 pcre_list * re_list_head = NULL, * ri;
39
40 /* precompile our regexes */
41 while ((regex_string = string_nextinlist(&list, &sep, NULL, 0)))
42   if (strcmpic(regex_string, US"false") != 0 && Ustrcmp(regex_string, "0") != 0)
43     {
44     /* compile our regular expression */
45     uschar * errstr;
46     const pcre2_code * re = regex_compile(regex_string,
47       cacheable ? MCS_CACHEABLE : MCS_NOFLAGS, &errstr, pcre_gen_cmp_ctx);
48
49     if (!re)
50       {
51       log_write(0, LOG_MAIN, "regex acl condition warning - %s, skipped", errstr);
52       continue;
53       }
54
55     ri = store_get(sizeof(pcre_list), GET_UNTAINTED);
56     ri->re = re;
57     ri->pcre_text = regex_string;
58     ri->next = re_list_head;
59     re_list_head = ri;
60     cnt++;
61     }
62 if (cntp) *cntp = cnt;
63 return re_list_head;
64 }
65
66
67 /* Check list of REs against buffer, returning OK for (first) match,
68 else FAIL.  On match return allocated result strings in regex_vars[]. 
69
70 We use the perm-pool for that, so that our caller can release
71 other allocations.
72 */
73 static int
74 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
75 {
76 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
77
78 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
79   {
80   int n;
81
82   /* try matcher on the line */
83   if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
84     {
85     int save_pool = store_pool;
86     store_pool = POOL_PERM;
87
88     Ustrncpy(regex_match_string_buffer, ri->pcre_text,
89               sizeof(regex_match_string_buffer)-1);
90     regex_match_string = regex_match_string_buffer;
91
92     for (int nn = 1; nn < n; nn++)
93       {
94       PCRE2_SIZE * ovec = pcre2_get_ovector_pointer(md);
95       int off = nn * 2;
96       int len = ovec[off + 1] - ovec[off];
97       regex_vars[nn-1] = string_copyn(linebuffer + ovec[off], len);
98       }
99
100     store_pool = save_pool;
101     return OK;
102     }
103   }
104 /* pcre2_match_data_free(md);   gen ctx needs no free */
105 return FAIL;
106 }
107
108
109 /* reset expansion variables */
110 void
111 regex_vars_clear(void)
112 {
113 regex_match_string = NULL;
114 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
115 }
116
117
118
119 int
120 regex(const uschar ** listptr, BOOL cacheable)
121 {
122 unsigned long mbox_size;
123 FILE * mbox_file;
124 pcre_list * re_list_head;
125 uschar * linebuffer;
126 long f_pos = 0;
127 int ret = FAIL, cnt, lcount = REGEX_LOOPCOUNT_STORE_RESET;
128 rmark reset_point;
129
130 regex_vars_clear();
131
132 if (!mime_stream)                               /* We are in the DATA ACL */
133   {
134   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
135     {                                           /* error while spooling */
136     log_write(0, LOG_MAIN|LOG_PANIC,
137            "regex acl condition: error while creating mbox spool file");
138     return DEFER;
139     }
140   }
141 else
142   {
143   if ((f_pos = ftell(mime_stream)) < 0)
144     {
145     log_write(0, LOG_MAIN|LOG_PANIC,
146            "regex acl condition: mime_stream: %s", strerror(errno));
147     return DEFER;
148     }
149   mbox_file = mime_stream;
150   }
151
152 reset_point = store_mark();
153   {
154   /* precompile our regexes */
155   if ((re_list_head = compile(*listptr, cacheable, &cnt)))
156     {
157     /* match each line against all regexes */
158     linebuffer = store_get(32767, GET_TAINTED);
159     while (fgets(CS linebuffer, 32767, mbox_file))
160       {
161       if (  mime_stream && mime_current_boundary                /* check boundary */
162          && Ustrncmp(linebuffer, "--", 2) == 0
163          && Ustrncmp((linebuffer+2), mime_current_boundary,
164                       Ustrlen(mime_current_boundary)) == 0)
165         break;                                          /* found boundary */
166
167       if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
168         break;
169
170       if ((lcount -= cnt) <= 0)
171         {
172         store_reset(reset_point); reset_point = store_mark();
173         lcount = REGEX_LOOPCOUNT_STORE_RESET;
174         }
175       }
176     }
177   }
178 store_reset(reset_point);
179
180 if (!mime_stream)
181   (void)fclose(mbox_file);
182 else
183   {
184   clearerr(mime_stream);
185   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
186     {
187     log_write(0, LOG_MAIN|LOG_PANIC,
188            "regex acl condition: mime_stream: %s", strerror(errno));
189     clearerr(mime_stream);
190     }
191   }
192
193 return ret;
194 }
195
196
197 int
198 mime_regex(const uschar **listptr, BOOL cacheable)
199 {
200 pcre_list * re_list_head = NULL;
201 FILE * f;
202 uschar * mime_subject = NULL;
203 int mime_subject_len = 0;
204 int ret = FAIL;
205 rmark reset_point;
206
207 regex_vars_clear();
208
209 /* check if the file is already decoded */
210 if (!mime_decoded_filename)
211   {                             /* no, decode it first */
212   const uschar *empty = US"";
213   mime_decode(&empty);
214   if (!mime_decoded_filename)
215     {                           /* decoding failed */
216     log_write(0, LOG_MAIN,
217        "mime_regex acl condition warning - could not decode MIME part to file");
218     return DEFER;
219     }
220   }
221
222 /* open file */
223 if (!(f = fopen(CS mime_decoded_filename, "rb")))
224   {
225   log_write(0, LOG_MAIN,
226        "mime_regex acl condition warning - can't open '%s' for reading",
227        mime_decoded_filename);
228   return DEFER;
229   }
230
231 reset_point = store_mark();
232   {
233   /* precompile our regexes */
234   if ((re_list_head = compile(*listptr, cacheable, NULL)))
235     {
236     /* get 32k memory, tainted */
237     mime_subject = store_get(32767, GET_TAINTED);
238
239     mime_subject_len = fread(mime_subject, 1, 32766, f);
240
241     ret = matcher(re_list_head, mime_subject, mime_subject_len);
242     }
243   }
244 store_reset(reset_point);
245 (void)fclose(f);
246 return ret;
247 }
248
249 #endif /* WITH_CONTENT_SCAN */