From ee9e07541f9f07762e3ee781102eea3a4190787c Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Fri, 21 May 2021 15:00:41 +0200 Subject: [PATCH] Fix #76359: open_basedir bypass through adding ".." We explicitly forbid adding paths with a leading `..` to `open_basedir` at runtime. Closes GH-7024. --- NEWS | 1 + main/fopen_wrappers.c | 5 +++++ tests/security/bug76359.phpt | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/security/bug76359.phpt diff --git a/NEWS b/NEWS index 847a9af27f4..01235592d04 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,7 @@ PHP NEWS - Core: . Fixed bug #81068 (Double free in realpath_cache_clean()). (Dimitry Andric) + . Fixed bug #76359 (open_basedir bypass through adding ".."). (cmb) - Standard: . Fixed bug #81048 (phpinfo(INFO_VARIABLES) "Array to string conversion"). diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index db4be878d81..27135020fa3 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -110,6 +110,11 @@ PHPAPI ZEND_INI_MH(OnUpdateBaseDir) *end = '\0'; end++; } + if (ptr[0] == '.' && ptr[1] == '.' && (ptr[2] == '\0' || IS_SLASH(ptr[2]))) { + /* Don't allow paths with a leading .. path component to be set at runtime */ + efree(pathbuf); + return FAILURE; + } if (php_check_open_basedir_ex(ptr, 0) != 0) { /* At least one portion of this open_basedir is less restrictive than the prior one, FAIL */ efree(pathbuf); diff --git a/tests/security/bug76359.phpt b/tests/security/bug76359.phpt new file mode 100644 index 00000000000..df35d67d097 --- /dev/null +++ b/tests/security/bug76359.phpt @@ -0,0 +1,19 @@ +--TEST-- +Bug #76359 (open_basedir bypass through adding "..") +--FILE-- + +--EXPECTF-- +bool(false) + +Warning: chdir(): open_basedir restriction in effect. File(..) is not within the allowed path(s): (%s) in %s on line %d +--CLEAN-- +