Skip to content

Commit 7e33ad2

Browse files
committed
Prevent segmentation fault
pcre_exec internally calls match function which can recurse a lot depending on the pattern and the string. We should set a recursion limit to prevent segmentation faults.
1 parent bcddef2 commit 7e33ad2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/common/util.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#endif
4848

4949
#define PATH_SEPARATOR '/'
50+
#define OSCAP_PCRE_EXEC_RECURSION_LIMIT 1000
5051

5152
int oscap_string_to_enum(const struct oscap_string_map *map, const char *str)
5253
{
@@ -366,10 +367,13 @@ int oscap_get_substrings(char *str, int *ofs, pcre *re, int want_substrs, char *
366367
ovector[i] = -1;
367368
}
368369

370+
struct pcre_extra extra;
371+
extra.match_limit_recursion = OSCAP_PCRE_EXEC_RECURSION_LIMIT;
372+
extra.flags = PCRE_EXTRA_MATCH_LIMIT_RECURSION;
369373
#if defined(OS_SOLARIS)
370-
rc = pcre_exec(re, NULL, str, strlen(str), *ofs, PCRE_NO_UTF8_CHECK, ovector, ovector_len);
374+
rc = pcre_exec(re, &extra, str, strlen(str), *ofs, PCRE_NO_UTF8_CHECK, ovector, ovector_len);
371375
#else
372-
rc = pcre_exec(re, NULL, str, strlen(str), *ofs, 0, ovector, ovector_len);
376+
rc = pcre_exec(re, &extra, str, strlen(str), *ofs, 0, ovector, ovector_len);
373377
#endif
374378

375379
if (rc < -1) {

0 commit comments

Comments
 (0)