Skip to content

Fix infinite recursion on deprecated attribute evaluation #17712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
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
28 changes: 28 additions & 0 deletions Zend/tests/attributes/deprecated/class_constants/gh17711.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
GH-17711: Infinite recursion through deprecated class constants self-referencing through deprecation message
--FILE--
<?php

class C {
#[\Deprecated(self::C)]
const C = TEST;
}

const TEST = 'Message';
var_dump(C::C);

class D {
#[\Deprecated(Alias::C)]
const C = 'test';
}

class_alias('D', 'Alias');
var_dump(D::C);

?>
--EXPECTF--
Deprecated: Constant C::C is deprecated, Message in %s on line %d
string(7) "Message"

Deprecated: Constant D::C is deprecated, test in %s on line %d
string(4) "test"
2 changes: 1 addition & 1 deletion Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_

ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
if (c->ce == class_type) {
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
if (Z_TYPE(c->value) == IS_CONSTANT_AST || (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
memcpy(new_c, c, sizeof(zend_class_constant));
c = new_c;
Expand Down
4 changes: 4 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8822,6 +8822,10 @@ static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_as

if (deprecated) {
ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
/* For deprecated constants, we need to flag the zval for recursion
* detection. Make sure the zval is separated out of shm. */
ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,10 @@ ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *
}

if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0 && !CONST_IS_RECURSIVE(c)) {
CONST_PROTECT_RECURSION(c);
zend_deprecated_class_constant(c, constant_name);
CONST_UNPROTECT_RECURSION(c);
if (EG(exception)) {
goto failure;
}
Expand Down
11 changes: 11 additions & 0 deletions Zend/zend_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
#define CONST_NO_FILE_CACHE (1<<1) /* Can't be saved in file cache */
#define CONST_DEPRECATED (1<<2) /* Deprecated */
#define CONST_OWNED (1<<3) /* constant should be destroyed together with class */
#define CONST_RECURSIVE (1<<4) /* Recursion protection for constant evaluation */

#define CONST_IS_RECURSIVE(c) (Z_CONSTANT_FLAGS((c)->value) & CONST_RECURSIVE)
#define CONST_PROTECT_RECURSION(c) \
do { \
Z_CONSTANT_FLAGS((c)->value) |= CONST_RECURSIVE; \
} while (0)
#define CONST_UNPROTECT_RECURSION(c) \
do { \
Z_CONSTANT_FLAGS((c)->value) &= ~CONST_RECURSIVE; \
} while (0)

#define PHP_USER_CONSTANT 0x7fffff /* a constant defined in user space */

Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -6094,8 +6094,10 @@ ZEND_VM_HANDLER(181, ZEND_FETCH_CLASS_CONSTANT, VAR|CONST|UNUSED|CLASS_FETCH, CO
}

bool is_constant_deprecated = ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED;
if (UNEXPECTED(is_constant_deprecated)) {
if (UNEXPECTED(is_constant_deprecated) && !CONST_IS_RECURSIVE(c)) {
CONST_PROTECT_RECURSION(c);
zend_deprecated_class_constant(c, constant_name);
CONST_UNPROTECT_RECURSION(c);

if (EG(exception)) {
ZVAL_UNDEF(EX_VAR(opline->result.var));
Expand Down
24 changes: 18 additions & 6 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -3800,6 +3800,11 @@ static bool preload_try_resolve_constants(zend_class_entry *ce)
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
val = &c->value;
if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
/* For deprecated constants, we need to flag the zval for recursion
* detection. Make sure the zval is separated out of shm. */
if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
ok = false;
}
if (EXPECTED(zend_update_class_constant(c, key, c->ce) == SUCCESS)) {
was_changed = changed = true;
} else {
Expand Down
Loading