Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ PHP NEWS
. Fixed stack buffer overflow with long connection user names, passwords,
charsets, or roles. (Ilia Alshanetsky)

- PCRE:
. Fixed \C being allowed in patterns enabling UTF mode with (*UTF) instead
of the u modifier. (Jorg Sowa)

- SNMP:
. Fixed use-after-free of the context name set by SNMP::setSecurity().
(lacatoire)
Expand Down
18 changes: 18 additions & 0 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
uint32_t coptions = 0;
#endif
uint32_t eoptions = PHP_PCRE_DEFAULT_EXTRA_COPTIONS;
uint32_t all_options;
uint32_t has_backslash_c;
PCRE2_UCHAR error[128];
PCRE2_SIZE erroffset;
int errnumber;
Expand Down Expand Up @@ -785,6 +787,22 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
/* Compile pattern and display a warning if compilation failed. */
re = pcre2_compile((PCRE2_SPTR)pattern, pattern_len, coptions, &errnumber, &erroffset, cctx);

/* In-pattern verbs such as (*UTF) can enable PCRE2_UTF, in which \C is unsafe. */
if (re != NULL && !(coptions & PCRE2_NEVER_BACKSLASH_C)
&& pcre2_pattern_info(re, PCRE2_INFO_HASBACKSLASHC, &has_backslash_c) >= 0
&& has_backslash_c
&& pcre2_pattern_info(re, PCRE2_INFO_ALLOPTIONS, &all_options) >= 0
&& (all_options & PCRE2_UTF)) {
pcre2_code_free(re);
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL, E_WARNING, "Compilation failed: using \\C is incompatible with UTF-8 mode");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to improve the error message to mention the (*UTF) pattern verb?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I prefer improve it more on master.

pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
efree(pattern);
return NULL;
}

if (re == NULL) {
if (key != regex) {
zend_string_release_ex(key, 0);
Expand Down
41 changes: 41 additions & 0 deletions ext/pcre/tests/preg_inline_utf_backslash_c.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
In-pattern (*UTF) forbids \C like the /u modifier
--FILE--
<?php
$patterns = [
'/(*UTF)\C/',
'/(*UTF8)a\C/',
'/(*LIMIT_MATCH=100)(*UTF)a\C/',
'/\C/u',
];
foreach ($patterns as $pattern) {
var_dump(preg_match($pattern, "a\u{E4}"));
}

// GH-21134 reproducer using (*UTF) instead of /u
var_dump(preg_match_all("/(*UTF)(.*)\\C/", "\u{E0}", $m), $m);

// \C is still allowed outside UTF mode, and an escaped backslash is not \C
var_dump(preg_match('/a\C/', "a\u{E4}", $m), bin2hex($m[0]));
var_dump(preg_match('/(*UTF)\\\\C/', 'x\C'));
?>
--EXPECTF--

Warning: preg_match(): Compilation failed: using \C is incompatible with UTF-8 mode in %s on line %d
bool(false)

Warning: preg_match(): Compilation failed: using \C is incompatible with UTF-8 mode in %s on line %d
bool(false)

Warning: preg_match(): Compilation failed: using \C is incompatible with UTF-8 mode in %s on line %d
bool(false)

Warning: preg_match(): Compilation failed: using \C is incompatible with the 'u' modifier at offset 2 in %s on line %d
bool(false)

Warning: preg_match_all(): Compilation failed: using \C is incompatible with UTF-8 mode in %s on line %d
bool(false)
NULL
int(1)
string(4) "61c3"
int(1)
Loading