Fix $regex<n> use-after-free. Bug 2915
[exim.git] / src / src / regex.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /*
6  * Copyright (c) The Exim Maintainers 2016 - 2022
7  * Copyright (c) Tom Kistner <tom@duncanthrax.net> 2003-2015
8  * License: GPL
9  */
10
11 /* Code for matching regular expressions against headers and body.
12  Called from acl.c. */
13
14 #include "exim.h"
15 #ifdef WITH_CONTENT_SCAN
16 #include <unistd.h>
17 #include <sys/mman.h>
18
19 /* Structure to hold a list of Regular expressions */
20 typedef struct pcre_list {
21   const pcre2_code *    re;
22   uschar *              pcre_text;
23   struct pcre_list *    next;
24 } pcre_list;
25
26 uschar regex_match_string_buffer[1024];
27
28 extern FILE *mime_stream;
29 extern uschar *mime_current_boundary;
30
31
32 static pcre_list *
33 compile(const uschar * list, BOOL cacheable)
34 {
35 int sep = 0;
36 uschar * regex_string;
37 pcre_list * re_list_head = NULL;
38 pcre_list * 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     }
61 return re_list_head;
62 }
63
64 static int
65 matcher(pcre_list * re_list_head, uschar * linebuffer, int len)
66 {
67 pcre2_match_data * md = pcre2_match_data_create(REGEX_VARS + 1, pcre_gen_ctx);
68
69 for (pcre_list * ri = re_list_head; ri; ri = ri->next)
70   {
71   int n;
72
73   /* try matcher on the line */
74   if ((n = pcre2_match(ri->re, (PCRE2_SPTR)linebuffer, len, 0, 0, md, pcre_gen_mtc_ctx)) > 0)
75     {
76     Ustrncpy(regex_match_string_buffer, ri->pcre_text,
77               sizeof(regex_match_string_buffer)-1);
78     regex_match_string = regex_match_string_buffer;
79
80     for (int nn = 1; nn < n; nn++)
81       {
82       PCRE2_UCHAR * cstr;
83       PCRE2_SIZE cslen;
84       pcre2_substring_get_bynumber(md, nn, &cstr, &cslen);      /* uses same ctx as md */
85       regex_vars[nn-1] = CUS cstr;
86       }
87
88     return OK;
89     }
90   }
91 /* pcre2_match_data_free(md);   gen ctx needs no free */
92 return FAIL;
93 }
94
95
96 /* reset expansion variables */
97 void
98 regex_vars_clear(void)
99 {
100 regex_match_string = NULL;
101 for (int i = 0; i < REGEX_VARS; i++) regex_vars[i] = NULL;
102 }
103
104
105
106 int
107 regex(const uschar ** listptr, BOOL cacheable)
108 {
109 unsigned long mbox_size;
110 FILE * mbox_file;
111 pcre_list * re_list_head;
112 uschar * linebuffer;
113 long f_pos = 0;
114 int ret = FAIL;
115
116 regex_vars_clear();
117
118 if (!mime_stream)                               /* We are in the DATA ACL */
119   {
120   if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
121     {                                           /* error while spooling */
122     log_write(0, LOG_MAIN|LOG_PANIC,
123            "regex acl condition: error while creating mbox spool file");
124     return DEFER;
125     }
126   }
127 else
128   {
129   if ((f_pos = ftell(mime_stream)) < 0)
130     {
131     log_write(0, LOG_MAIN|LOG_PANIC,
132            "regex acl condition: mime_stream: %s", strerror(errno));
133     return DEFER;
134     }
135   mbox_file = mime_stream;
136   }
137
138 /* precompile our regexes */
139 if (!(re_list_head = compile(*listptr, cacheable)))
140   return FAIL;                  /* no regexes -> nothing to do */
141
142 /* match each line against all regexes */
143 linebuffer = store_get(32767, GET_TAINTED);
144 while (fgets(CS linebuffer, 32767, mbox_file))
145   {
146   if (  mime_stream && mime_current_boundary            /* check boundary */
147      && Ustrncmp(linebuffer, "--", 2) == 0
148      && Ustrncmp((linebuffer+2), mime_current_boundary,
149                   Ustrlen(mime_current_boundary)) == 0)
150       break;                                            /* found boundary */
151
152   if ((ret = matcher(re_list_head, linebuffer, (int)Ustrlen(linebuffer))) == OK)
153     goto done;
154   }
155 /* no matches ... */
156
157 done:
158 if (!mime_stream)
159   (void)fclose(mbox_file);
160 else
161   {
162   clearerr(mime_stream);
163   if (fseek(mime_stream, f_pos, SEEK_SET) == -1)
164     {
165     log_write(0, LOG_MAIN|LOG_PANIC,
166            "regex acl condition: mime_stream: %s", strerror(errno));
167     clearerr(mime_stream);
168     }
169   }
170
171 return ret;
172 }
173
174
175 int
176 mime_regex(const uschar **listptr, BOOL cacheable)
177 {
178 pcre_list * re_list_head = NULL;
179 FILE * f;
180 uschar * mime_subject = NULL;
181 int mime_subject_len = 0;
182 int ret;
183
184 regex_vars_clear();
185
186 /* precompile our regexes */
187 if (!(re_list_head = compile(*listptr, cacheable)))
188   return FAIL;                  /* no regexes -> nothing to do */
189
190 /* check if the file is already decoded */
191 if (!mime_decoded_filename)
192   {                             /* no, decode it first */
193   const uschar *empty = US"";
194   mime_decode(&empty);
195   if (!mime_decoded_filename)
196     {                           /* decoding failed */
197     log_write(0, LOG_MAIN,
198        "mime_regex acl condition warning - could not decode MIME part to file");
199     return DEFER;
200     }
201   }
202
203 /* open file */
204 if (!(f = fopen(CS mime_decoded_filename, "rb")))
205   {
206   log_write(0, LOG_MAIN,
207        "mime_regex acl condition warning - can't open '%s' for reading",
208        mime_decoded_filename);
209   return DEFER;
210   }
211
212 /* get 32k memory, tainted */
213 mime_subject = store_get(32767, GET_TAINTED);
214
215 mime_subject_len = fread(mime_subject, 1, 32766, f);
216
217 ret = matcher(re_list_head, mime_subject, mime_subject_len);
218 (void)fclose(f);
219 return ret;
220 }
221
222 #endif /* WITH_CONTENT_SCAN */