Debian Codegraph @

src/common/oscap_pcre.c

raw symbols SCIP
1 /*
2 * Copyright 2023 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Evgenii Kolesnikov <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <memory.h>
28
29 #define OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT 3500
30
31 #define PCRE2_CODE_UNIT_WIDTH 8
32 #define PCRE2_ERR_BUF_SIZE 127
33 #include <pcre2.h>
34
35
36 #include "debug_priv.h"
37 #include "oscap_pcre.h"
38
39
40 struct oscap_pcre {
41 pcre2_code_8 *re;
42 pcre2_match_context_8 *re_ctx;
43 };
44
45
46 static inline int _oscap_pcre_opts_to_pcre(oscap_pcre_options_t opts)
47 {
48 int res = 0;
49 if (opts & OSCAP_PCRE_OPTS_UTF8)
50 res |= PCRE2_UTF;
51 if (opts & OSCAP_PCRE_OPTS_MULTILINE)
52 res |= PCRE2_MULTILINE;
53 if (opts & OSCAP_PCRE_OPTS_DOTALL)
54 res |= PCRE2_DOTALL;
55 if (opts & OSCAP_PCRE_OPTS_CASELESS)
56 res |= PCRE2_CASELESS;
57 if (opts & OSCAP_PCRE_OPTS_NO_UTF8_CHECK)
58 res |= PCRE2_NO_UTF_CHECK;
59 if (opts & OSCAP_PCRE_OPTS_PARTIAL)
60 res |= PCRE2_PARTIAL_SOFT;
61
62 return res;
63 };
64
65 static inline oscap_pcre_error_t _pcre_error_to_oscap_pcre(int res)
66 {
67 switch (res) {
68 case PCRE2_ERROR_NOMATCH:
69 return OSCAP_PCRE_ERR_NOMATCH;
70 case PCRE2_ERROR_PARTIAL:
71 return OSCAP_PCRE_ERR_PARTIAL;
72 case PCRE2_ERROR_UTF8_ERR1:
73 case PCRE2_ERROR_UTF8_ERR2:
74 case PCRE2_ERROR_UTF8_ERR3:
75 case PCRE2_ERROR_UTF8_ERR4:
76 case PCRE2_ERROR_UTF8_ERR5:
77 case PCRE2_ERROR_UTF8_ERR6:
78 case PCRE2_ERROR_UTF8_ERR7:
79 case PCRE2_ERROR_UTF8_ERR8:
80 case PCRE2_ERROR_UTF8_ERR9:
81 case PCRE2_ERROR_UTF8_ERR10:
82 case PCRE2_ERROR_UTF8_ERR11:
83 case PCRE2_ERROR_UTF8_ERR12:
84 case PCRE2_ERROR_UTF8_ERR13:
85 case PCRE2_ERROR_UTF8_ERR14:
86 case PCRE2_ERROR_UTF8_ERR15:
87 case PCRE2_ERROR_UTF8_ERR16:
88 case PCRE2_ERROR_UTF8_ERR17:
89 case PCRE2_ERROR_UTF8_ERR18:
90 case PCRE2_ERROR_UTF8_ERR19:
91 case PCRE2_ERROR_UTF8_ERR20:
92 case PCRE2_ERROR_UTF8_ERR21:
93 return OSCAP_PCRE_ERR_BADUTF8;
94 case PCRE2_ERROR_RECURSIONLIMIT:
95 return OSCAP_PCRE_ERR_RECURSIONLIMIT;
96 default:
97 dW("Unknown PCRE error code: %d", res);
98 return OSCAP_PCRE_ERR_UNKNOWN;
99 }
100 }
101
102 oscap_pcre_t *oscap_pcre_compile(const char *pattern, oscap_pcre_options_t options,
103 char **errptr, int *erroffset)
104 {
105 oscap_pcre_t *res = malloc(sizeof(oscap_pcre_t));
106 int errno;
107 PCRE2_SIZE erroffset2;
108 res->re_ctx = NULL;
109 res->re = pcre2_compile_8((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, _oscap_pcre_opts_to_pcre(options), &errno, &erroffset2, NULL);
110 if (res->re == NULL) {
111 PCRE2_UCHAR8 errmsg[PCRE2_ERR_BUF_SIZE];
112 pcre2_get_error_message_8(errno, errmsg, PCRE2_ERR_BUF_SIZE);
113 dW("pcre2_compile_8: error (at offset %d): %s", erroffset2, errmsg);
114 *erroffset = erroffset2;
115 *errptr = strdup((const char*)errmsg);
116 }
117
118 if (res->re == NULL) {
119 free(res);
120 res = NULL;
121 }
122 return res;
123 }
124
125 void oscap_pcre_set_match_limit_recursion(oscap_pcre_t *opcre, unsigned long limit)
126 {
127 if (opcre->re_ctx == NULL) {
128 opcre->re_ctx = pcre2_match_context_create_8(NULL);
129 }
130 pcre2_set_depth_limit_8(opcre->re_ctx, limit);
131 }
132
133 int oscap_pcre_exec(const oscap_pcre_t *opcre, const char *subject,
134 int length, int startoffset, oscap_pcre_options_t options,
135 int *ovector, int ovecsize)
136 {
137 int rc = 0;
138 // The ovecsize is multiplied by 3 in the code for compatibility with PCRE1
139 int ovecsize2 = ovecsize/3;
140 pcre2_match_data_8 *mdata = pcre2_match_data_create_8(ovecsize2, NULL);
141 rc = pcre2_match_8(opcre->re, (PCRE2_SPTR8)subject, length, startoffset, _oscap_pcre_opts_to_pcre(options), mdata, opcre->re_ctx);
142 if (rc > PCRE2_ERROR_NOMATCH) {
143 PCRE2_SIZE *ovecp = pcre2_get_ovector_pointer_8(mdata);
144 for (int i = 0; i < rc; i++) {
145 if (i < ovecsize2) {
146 ovector[i*2] = ovecp[i*2];
147 ovector[i*2+1] = ovecp[i*2+1];
148 }
149 }
150 }
151 pcre2_match_data_free_8(mdata);
152
153 return rc >= 0 ? rc : _pcre_error_to_oscap_pcre(rc);
154 }
155
156 void oscap_pcre_free(oscap_pcre_t *opcre)
157 {
158 if (opcre != NULL) {
159 if (opcre->re_ctx != NULL)
160 pcre2_match_context_free_8(opcre->re_ctx);
161 pcre2_code_free_8(opcre->re);
162 free(opcre);
163 }
164 }
165
166 int oscap_pcre_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings) {
167 int i, ret, rc;
168 int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
169 char **substrs;
170
171 // todo: max match count check
172
173 for (i = 0; i < ovector_len; ++i) {
174 ovector[i] = -1;
175 }
176
177 unsigned long limit = OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULT;
178 char *limit_str = getenv("OSCAP_PCRE_EXEC_RECURSION_LIMIT");
179 if (limit_str != NULL)
180 if (sscanf(limit_str, "%lu", &limit) <= 0)
181 dW("Unable to parse OSCAP_PCRE_EXEC_RECURSION_LIMIT value");
182 oscap_pcre_set_match_limit_recursion(re, limit);
183 size_t str_len = strlen(str);
184 #if defined(OS_SOLARIS)
185 rc = oscap_pcre_exec(re, str, str_len, *ofs, OSCAP_PCRE_OPTS_NO_UTF8_CHECK, ovector, ovector_len);
186 #else
187 rc = oscap_pcre_exec(re, str, str_len, *ofs, 0, ovector, ovector_len);
188 #endif
189
190 if (rc < OSCAP_PCRE_ERR_NOMATCH) {
191 if (str_len < 100)
192 dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%s'.", rc, str);
193 else
194 dE("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%.100s' (truncated, showing first 100 characters).", rc, str);
195 return rc;
196 } else if (rc == OSCAP_PCRE_ERR_NOMATCH) {
197 /* no match */
198 return 0;
199 }
200
201 *ofs = (*ofs == ovector[1]) ? ovector[1] + 1 : ovector[1];
202
203 if (!want_substrs) {
204 /* just report successful match */
205 return 1;
206 }
207
208 ret = 0;
209 if (rc == 0) {
210 /* vector too small */
211 // todo: report partial results
212 rc = ovector_len / 3;
213 }
214
215 substrs = malloc(rc * sizeof (char *));
216 for (i = 0; i < rc; ++i) {
217 int len;
218 char *buf;
219
220 if (ovector[2 * i] == -1) {
221 continue;
222 }
223 len = ovector[2 * i + 1] - ovector[2 * i];
224 buf = malloc(len + 1);
225 memcpy(buf, str + ovector[2 * i], len);
226 buf[len] = '\0';
227 substrs[ret] = buf;
228 ++ret;
229 }
230
231 *substrings = substrs;
232
233 return ret;
234 }
235
236 void oscap_pcre_err_free(char *err)
237 {
238 if (err != NULL) {
239 free(err);
240 }
241 }
242