Skip to content

Fix open_basedir leak #11780

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 1 commit 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
9 changes: 9 additions & 0 deletions Zend/tests/oss_fuzz_60741.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
oss-fuzz #60741: Leak in open_basedir when using non-constant string
--INI--
open_basedir="{TMP}"
--FILE--
<?php
ini_set('open_basedir', ini_get('open_basedir'));
?>
--EXPECT--
18 changes: 0 additions & 18 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,29 +1271,11 @@ void zend_call_destructors(void) /* {{{ */
}
/* }}} */

static void zend_release_open_basedir(void)
{
/* Release custom open_basedir config, this needs to happen before ini shutdown */
if (PG(open_basedir)) {
zend_ini_entry *ini_entry = zend_hash_str_find_ptr(EG(ini_directives), "open_basedir", strlen("open_basedir"));
/* ini_entry->modified is unreliable, it might also be set when on_update has failed. */
if (ini_entry
&& ini_entry->modified
&& ini_entry->value != ini_entry->orig_value) {
efree(PG(open_basedir));
PG(open_basedir) = NULL;
}
}
}

ZEND_API void zend_deactivate(void) /* {{{ */
{
/* we're no longer executing anything */
EG(current_execute_data) = NULL;

/* Needs to run before zend_ini_deactivate(). */
zend_release_open_basedir();

zend_try {
shutdown_scanner();
} zend_end_try();
Expand Down
18 changes: 10 additions & 8 deletions main/fopen_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
char *pathbuf, *ptr, *end;

if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) {
if (PG(open_basedir_modified)) {
efree(*p);
}
/* We're in a PHP_INI_SYSTEM context, no restrictions */
*p = new_value ? ZSTR_VAL(new_value) : NULL;
PG(open_basedir_modified) = false;
return SUCCESS;
}

Expand Down Expand Up @@ -117,15 +121,13 @@ PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
efree(pathbuf);

/* Everything checks out, set it */
if (*p) {
/* Unfortunately entry->modified has already been set to true so we compare entry->value
* against entry->orig_value. */
if (entry->modified && entry->value != entry->orig_value) {
efree(*p);
}
}
zend_string *tmp = smart_str_extract(&buf);
*p = estrdup(ZSTR_VAL(tmp));
char *result = estrdup(ZSTR_VAL(tmp));
if (PG(open_basedir_modified)) {
efree(*p);
}
*p = result;
PG(open_basedir_modified) = true;
zend_string_release(tmp);

return SUCCESS;
Expand Down
1 change: 1 addition & 0 deletions main/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct _php_core_globals {
char *user_dir;
char *include_path;
char *open_basedir;
bool open_basedir_modified;
Copy link
Member

Choose a reason for hiding this comment

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

I know @nielsdos did some struct packing at one time, but there was also one case where it wasn't super worth it to do it compared to keeping the relevant bits nearby. Might have been the globals struct

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. I prefer keeping related globals together, as it's easier to understand but also potentially improves cache locality. I'm not going to die on a hill for this though.

Copy link
Member

Choose a reason for hiding this comment

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

I indeed did a bit of struct packing here too. When I did it I made sure that fields that were in the same cacheline were still in the same line after reordering. However, packing is not important here, this is fine as-is imo.

char *extension_dir;
char *php_binary;
char *sys_temp_dir;
Expand Down