Skip to content

Commit aae20cd

Browse files
authored
ext/phar: Fix recently introduced potential NULL dereferencement segfaults (#11065)
1 parent 71ff744 commit aae20cd

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

ext/phar/func_interceptors.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ PHAR_FUNC(phar_opendir) /* {{{ */
4747

4848
/* we are checking for existence of a file within the relative path. Chances are good that this is
4949
retrieving something from within the phar archive */
50-
if (!zend_string_starts_with_literal_ci(fname, "phar://")) {
50+
if (!fname || !zend_string_starts_with_literal_ci(fname, "phar://")) {
5151
goto skip_phar;
5252
}
5353

@@ -96,7 +96,7 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
9696

9797
/* we are checking for existence of a file within the relative path. Chances are good that this is
9898
retrieving something from within the phar archive */
99-
if (!zend_string_starts_with_literal_ci(fname, "phar://")) {
99+
if (!fname || !zend_string_starts_with_literal_ci(fname, "phar://")) {
100100
return NULL;
101101
}
102102

@@ -497,7 +497,7 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
497497

498498
/* we are checking for existence of a file within the relative path. Chances are good that this is
499499
retrieving something from within the phar archive */
500-
if (!zend_string_starts_with_literal_ci(fname, "phar://")) {
500+
if (!fname || !zend_string_starts_with_literal_ci(fname, "phar://")) {
501501
goto skip_phar;
502502
}
503503

@@ -748,7 +748,7 @@ PHAR_FUNC(phar_is_file) /* {{{ */
748748

749749
/* we are checking for existence of a file within the relative path. Chances are good that this is
750750
retrieving something from within the phar archive */
751-
if (!zend_string_starts_with_literal_ci(fname, "phar://")) {
751+
if (!fname || !zend_string_starts_with_literal_ci(fname, "phar://")) {
752752
goto skip_phar;
753753
}
754754

@@ -814,7 +814,7 @@ PHAR_FUNC(phar_is_link) /* {{{ */
814814

815815
/* we are checking for existence of a file within the relative path. Chances are good that this is
816816
retrieving something from within the phar archive */
817-
if (!zend_string_starts_with_literal_ci(fname, "phar://")) {
817+
if (!fname || !zend_string_starts_with_literal_ci(fname, "phar://")) {
818818
goto skip_phar;
819819
}
820820

ext/phar/phar.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,17 +2324,17 @@ int phar_open_executed_filename(char *alias, size_t alias_len, char **error) /*
23242324

23252325
zend_string *fname = zend_get_executed_filename_ex();
23262326

2327-
if (phar_open_parsed_phar(ZSTR_VAL(fname), ZSTR_LEN(fname), alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
2328-
return SUCCESS;
2329-
}
2330-
2331-
if (zend_string_equals_literal(fname, "[no active file]")) {
2327+
if (!fname) {
23322328
if (error) {
23332329
spprintf(error, 0, "cannot initialize a phar outside of PHP execution");
23342330
}
23352331
return FAILURE;
23362332
}
23372333

2334+
if (phar_open_parsed_phar(ZSTR_VAL(fname), ZSTR_LEN(fname), alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
2335+
return SUCCESS;
2336+
}
2337+
23382338
if (0 == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
23392339
if (error) {
23402340
spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar");

ext/phar/phar_object.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ PHP_METHOD(Phar, running)
402402
}
403403

404404
fname = zend_get_executed_filename_ex();
405+
if (!fname) {
406+
RETURN_EMPTY_STRING();
407+
}
405408

406409
if (
407410
zend_string_starts_with_literal_ci(fname, "phar://")
@@ -445,8 +448,13 @@ PHP_METHOD(Phar, mount)
445448
}
446449

447450
zend_string *zend_file_name = zend_get_executed_filename_ex();
448-
fname = ZSTR_VAL(zend_file_name);
449-
fname_len = ZSTR_LEN(zend_file_name);
451+
if (UNEXPECTED(!zend_file_name)) {
452+
fname = "";
453+
fname_len = 0;
454+
} else {
455+
fname = ZSTR_VAL(zend_file_name);
456+
fname_len = ZSTR_LEN(zend_file_name);
457+
}
450458

451459
#ifdef PHP_WIN32
452460
save_fname = fname;
@@ -577,6 +585,10 @@ PHP_METHOD(Phar, webPhar)
577585
}
578586

579587
zend_string *zend_file_name = zend_get_executed_filename_ex();
588+
if (UNEXPECTED(!zend_file_name)) {
589+
return;
590+
}
591+
580592
fname = ZSTR_VAL(zend_file_name);
581593
fname_len = ZSTR_LEN(zend_file_name);
582594

@@ -1298,7 +1310,8 @@ PHP_METHOD(Phar, unlinkArchive)
12981310
zend_string *zend_file_name = zend_get_executed_filename_ex();
12991311

13001312
if (
1301-
zend_string_starts_with_literal_ci(zend_file_name, "phar://")
1313+
zend_file_name
1314+
&& zend_string_starts_with_literal_ci(zend_file_name, "phar://")
13021315
&& SUCCESS == phar_split_fname(ZSTR_VAL(zend_file_name), ZSTR_LEN(zend_file_name), &arch, &arch_len, &entry, &entry_len, 2, 0)
13031316
) {
13041317
if (arch_len == fname_len && !memcmp(arch, fname, arch_len)) {

ext/phar/util.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ zend_string *phar_find_in_include_path(zend_string *filename, phar_archive_data
257257
}
258258

259259
zend_string *fname = zend_get_executed_filename_ex();
260+
if (!fname) {
261+
return NULL;
262+
}
263+
260264
bool is_file_a_phar_wrapper = zend_string_starts_with_literal_ci(fname, "phar://");
261265
size_t length_phar_protocol = strlen("phar://");
262266

0 commit comments

Comments
 (0)