From: Bishop Bettini Date: Fri, 2 Feb 2018 02:29:37 +0000 (-0500) Subject: Fixed bug #65414 X-Git-Tag: php-7.2.3RC1~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4765ba7dc3fbae657288ae1095f111a136746cde;p=php Fixed bug #65414 --- diff --git a/NEWS b/NEWS index 722bb2c472..676f887a09 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,8 @@ PHP NEWS - Phar: . Fixed bug #54289 (Phar::extractTo() does not accept specific directories to be extracted). (bishop) + . Fixed bug #65414 (deal with leading slash while adding files correctly). + (bishopb) - ODBC: . Fixed bug #73725 (Unable to retrieve value of varchar(max) type). (Anatol) diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 5a85bf7ad9..c8767af0de 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -3682,14 +3682,18 @@ PHP_METHOD(Phar, offsetGet) */ static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, size_t cont_len, zval *zresource) { + int start_pos=0; char *error; size_t contents_len; phar_entry_data *data; php_stream *contents_file; - if (filename_len >= (int)sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1) && (filename[5] == '/' || filename[5] == '\\' || filename[5] == '\0')) { - zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory"); - return; + if (filename_len >= (int)sizeof(".phar")-1) { + start_pos = ('/' == filename[0] ? 1 : 0); /* account for any leading slash: multiple-leads handled elsewhere */ + if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory"); + return; + } } if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1))) { diff --git a/ext/phar/tests/bug65414.phpt b/ext/phar/tests/bug65414.phpt new file mode 100644 index 0000000000..964ec72870 --- /dev/null +++ b/ext/phar/tests/bug65414.phpt @@ -0,0 +1,36 @@ +--TEST-- +Bug #65414 Injection (A1) in .phar files magic .phar directory +--SKIPIF-- + +--INI-- +phar.readonly = 0 +--FILE-- +addFromString($bad, 'this content is injected'); + echo 'Failed to throw expected exception'; + } catch (BadMethodCallException $ex) { + echo $ex->getMessage() . PHP_EOL; + } +} +echo 'done' . PHP_EOL; +?> +--CLEAN-- + +--EXPECT-- +.phar/injected-1.txt:Cannot create any files in magic ".phar" directory +/.phar/injected-2.txt:Cannot create any files in magic ".phar" directory +//.phar/injected-3.txt:Entry //.phar/injected-3.txt does not exist and cannot be created: phar error: invalid path "//.phar/injected-3.txt" contains double slash +/.phar/:Cannot create any files in magic ".phar" directory +done