Skip to content

Commit edfc0da

Browse files
committed
ext/phar: unset() of an entry with an open handle use-after-free.
Flushing closed the archive stream while a handle on a deleted entry still referenced it. The open handle check now covers deleted entries. Close GH-25649
1 parent c93b3d1 commit edfc0da

17 files changed

Lines changed: 507 additions & 65 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ PHP NEWS
146146
- Phar:
147147
. Fixed bug GH-25528 (Phar::setSignatureAlgorithm(): OpenSSL signing reads a
148148
freed private key). (Artem Ukrainskiy)
149+
. Fixed use-after-free when unsetting an entry with an open handle.
150+
(David Carlier)
149151

150152
- POSIX:
151153
. Fixed bug GH-25530 (posix_get_last_error() returns an error from a

‎ext/phar/phar.c‎

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
226226

227227
phar_metadata_tracker_free(&phar->metadata_tracker, phar->is_persistent);
228228

229+
if (phar->fp_refs) {
230+
zend_ulong key;
231+
232+
ZEND_HASH_FOREACH_NUM_KEY(phar->fp_refs, key) {
233+
php_stream *stream = (php_stream *) (uintptr_t) key;
234+
235+
if (stream != phar->fp && stream != phar->ufp) {
236+
php_stream_close(stream);
237+
}
238+
} ZEND_HASH_FOREACH_END();
239+
zend_hash_destroy(phar->fp_refs);
240+
FREE_HASHTABLE(phar->fp_refs);
241+
phar->fp_refs = NULL;
242+
}
243+
229244
if (phar->fp) {
230245
php_stream_close(phar->fp);
231246
phar->fp = 0;
@@ -395,16 +410,67 @@ void destroy_phar_manifest_entry(zval *zv) /* {{{ */
395410
}
396411
/* }}} */
397412

413+
void phar_fp_addref(phar_archive_data *phar, php_stream *fp) /* {{{ */
414+
{
415+
zval *count, tmp;
416+
417+
if (!phar->fp_refs) {
418+
ALLOC_HASHTABLE(phar->fp_refs);
419+
zend_hash_init(phar->fp_refs, 2, NULL, NULL, 0);
420+
}
421+
422+
count = zend_hash_index_find(phar->fp_refs, (zend_ulong) (uintptr_t) fp);
423+
if (count) {
424+
Z_LVAL_P(count)++;
425+
} else {
426+
ZVAL_LONG(&tmp, 1);
427+
zend_hash_index_add_new(phar->fp_refs, (zend_ulong) (uintptr_t) fp, &tmp);
428+
}
429+
}
430+
/* }}} */
431+
432+
bool phar_fp_in_use(const phar_archive_data *phar, const php_stream *fp) /* {{{ */
433+
{
434+
return phar->fp_refs && zend_hash_index_exists(phar->fp_refs, (zend_ulong) (uintptr_t) fp);
435+
}
436+
/* }}} */
437+
438+
static void phar_entry_data_release_fp(phar_entry_data *idata) /* {{{ */
439+
{
440+
phar_archive_data *phar = idata->phar;
441+
php_stream *fp = idata->fp;
442+
zval *count;
443+
444+
if (!fp) {
445+
return;
446+
}
447+
448+
count = phar->fp_refs ? zend_hash_index_find(phar->fp_refs, (zend_ulong) (uintptr_t) fp) : NULL;
449+
if (count) {
450+
if (--Z_LVAL_P(count) > 0) {
451+
return;
452+
}
453+
zend_hash_index_del(phar->fp_refs, (zend_ulong) (uintptr_t) fp);
454+
if (fp != phar->fp && fp != phar->ufp) {
455+
php_stream_close(fp);
456+
}
457+
return;
458+
}
459+
460+
if (fp != phar->fp && fp != phar->ufp && fp != idata->internal_file->fp) {
461+
php_stream_close(fp);
462+
}
463+
}
464+
/* }}} */
465+
398466
void phar_entry_delref(phar_entry_data *idata) /* {{{ */
399467
{
400468
if (idata->internal_file && !idata->internal_file->is_persistent) {
401469
if (--idata->internal_file->fp_refcount < 0) {
402470
idata->internal_file->fp_refcount = 0;
403471
}
404472

405-
if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
406-
php_stream_close(idata->fp);
407-
}
473+
phar_entry_data_release_fp(idata);
408474
/* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
409475
if (idata->internal_file->is_temp_dir) {
410476
destroy_phar_manifest_entry_int(idata->internal_file);
@@ -427,9 +493,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
427493
phar = idata->phar;
428494

429495
if (idata->internal_file->fp_refcount < 2 && idata->internal_file->fileinfo_lock_count == 0) {
430-
if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
431-
php_stream_close(idata->fp);
432-
}
496+
phar_entry_data_release_fp(idata);
433497
zend_hash_str_del(&idata->phar->manifest, idata->internal_file->filename, idata->internal_file->filename_len);
434498
idata->phar->refcount--;
435499
efree(idata);
@@ -2534,8 +2598,6 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
25342598
php_stream_filter *filter;
25352599
php_serialize_data_t metadata_hash;
25362600
smart_str main_metadata_str = {0};
2537-
bool free_fp = true;
2538-
bool free_ufp = true;
25392601
bool manifest_hack = false;
25402602
php_stream *shared_cfp = NULL;
25412603

@@ -2681,18 +2743,6 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
26812743
/* remove this from the new phar */
26822744
continue;
26832745
}
2684-
if (entry->fp_refcount) {
2685-
/* open file pointers refer to this fp, do not free the stream */
2686-
switch (entry->fp_type) {
2687-
case PHAR_FP:
2688-
free_fp = false;
2689-
break;
2690-
case PHAR_UFP:
2691-
free_ufp = false;
2692-
default:
2693-
break;
2694-
}
2695-
}
26962746
/* after excluding deleted files, calculate manifest size in bytes and number of entries */
26972747
++new_manifest_count;
26982748
phar_add_virtual_dirs(phar, entry->filename, entry->filename_len);
@@ -3116,12 +3166,12 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
31163166
/* finally, close the temp file, rename the original phar,
31173167
move the temp to the old phar, unlink the old phar, and reload it into memory
31183168
*/
3119-
if (phar->fp && free_fp) {
3169+
if (phar->fp && !phar_fp_in_use(phar, phar->fp)) {
31203170
php_stream_close(phar->fp);
31213171
}
31223172

31233173
if (phar->ufp) {
3124-
if (free_ufp) {
3174+
if (!phar_fp_in_use(phar, phar->ufp)) {
31253175
php_stream_close(phar->ufp);
31263176
}
31273177
phar->ufp = NULL;

‎ext/phar/phar_internal.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ struct _phar_archive_data {
269269
php_stream *fp;
270270
/* decompressed file contents are stored here */
271271
php_stream *ufp;
272+
/* number of open entry handles per fp/ufp stream, current or replaced by a flush */
273+
HashTable *fp_refs;
272274
int refcount;
273275
uint32_t sig_flags;
274276
uint32_t sig_len;
@@ -493,6 +495,8 @@ extern HashTable cached_alias;
493495

494496
bool phar_archive_delref(phar_archive_data *phar);
495497
void phar_entry_delref(phar_entry_data *idata);
498+
void phar_fp_addref(phar_archive_data *phar, php_stream *fp);
499+
bool phar_fp_in_use(const phar_archive_data *phar, const php_stream *fp);
496500

497501
phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
498502
phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);

‎ext/phar/stream.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
281281
idata->internal_file = entry;
282282
if (!phar->is_persistent) {
283283
++(entry->phar->refcount);
284+
phar_fp_addref(phar, stream);
284285
}
285286
++(entry->fp_refcount);
286287
php_url_free(resource);

‎ext/phar/tar.c‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,6 @@ zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, ch
776776
struct _phar_pass_tar_info {
777777
php_stream *old;
778778
php_stream *new;
779-
bool free_fp;
780-
bool free_ufp;
781779
char **error;
782780
};
783781

@@ -904,19 +902,6 @@ static int phar_tar_writeheaders_int(phar_entry_info *entry, void *argument) /*
904902
php_stream_write(fp->new, padding, ((entry->uncompressed_filesize +511)&~511) - entry->uncompressed_filesize);
905903
}
906904

907-
if (entry->fp_refcount) {
908-
/* open file pointers refer to this fp, do not free the stream */
909-
switch (entry->fp_type) {
910-
case PHAR_FP:
911-
fp->free_fp = 0;
912-
break;
913-
case PHAR_UFP:
914-
fp->free_ufp = 0;
915-
default:
916-
break;
917-
}
918-
}
919-
920905
entry->is_modified = 0;
921906

922907
if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp) {
@@ -1188,8 +1173,6 @@ int phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
11881173
pass.old = oldfile;
11891174
pass.new = newfile;
11901175
pass.error = error;
1191-
pass.free_fp = 1;
1192-
pass.free_ufp = 1;
11931176

11941177
if (phar_metadata_tracker_has_data(&phar->metadata_tracker, phar->is_persistent)) {
11951178
phar_entry_info *mentry;
@@ -1338,12 +1321,12 @@ int phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
13381321
return EOF;
13391322
}
13401323

1341-
if (phar->fp && pass.free_fp) {
1324+
if (phar->fp && !phar_fp_in_use(phar, phar->fp)) {
13421325
php_stream_close(phar->fp);
13431326
}
13441327

13451328
if (phar->ufp) {
1346-
if (pass.free_ufp) {
1329+
if (!phar_fp_in_use(phar, phar->ufp)) {
13471330
php_stream_close(phar->ufp);
13481331
}
13491332
phar->ufp = NULL;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Phar: writing to an archive with open file handles
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
phar.require_hash=0
8+
--FILE--
9+
<?php
10+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
11+
12+
$phar = new Phar($fname);
13+
$phar['a'] = 'aaaa';
14+
$phar['b'] = 'bbbb';
15+
16+
$fp = fopen('phar://' . $fname . '/a', 'r');
17+
$fp2 = fopen('phar://' . $fname . '/b', 'r');
18+
$phar['c'] = 'cccc';
19+
20+
$streams = count(get_resources('stream'));
21+
for ($i = 0; $i < 5; $i++) {
22+
$phar['d' . $i] = 'dddd';
23+
}
24+
var_dump(count(get_resources('stream')) === $streams);
25+
26+
var_dump(fclose($fp));
27+
var_dump(fclose($fp2));
28+
var_dump(count($phar));
29+
var_dump($phar['c']->getContent());
30+
?>
31+
--CLEAN--
32+
<?php
33+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
34+
?>
35+
--EXPECT--
36+
bool(true)
37+
bool(true)
38+
bool(true)
39+
int(8)
40+
string(4) "cccc"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Phar: writing to an archive with open file handles on a compressed entry
3+
--EXTENSIONS--
4+
phar
5+
zlib
6+
--INI--
7+
phar.readonly=0
8+
phar.require_hash=0
9+
--FILE--
10+
<?php
11+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
12+
$copy = __DIR__ . '/' . basename(__FILE__, '.php') . '.copy.phar';
13+
14+
$phar = new Phar($fname);
15+
$phar['a'] = 'aaaa';
16+
$phar['b'] = 'bbbb';
17+
$phar['a']->compress(Phar::GZ);
18+
$phar['b']->compress(Phar::GZ);
19+
20+
$fp = fopen('phar://' . $fname . '/b', 'r');
21+
$fp2 = fopen('phar://' . $fname . '/b', 'r');
22+
var_dump(fread($fp, 4));
23+
$phar['c'] = 'cccc';
24+
unset($phar['b']);
25+
$phar['d'] = 'dddd';
26+
var_dump(fclose($fp));
27+
var_dump(fclose($fp2));
28+
29+
copy($fname, $copy);
30+
$saved = new Phar($copy);
31+
var_dump(count($saved));
32+
var_dump($saved['a']->getContent());
33+
var_dump($saved['d']->getContent());
34+
?>
35+
--CLEAN--
36+
<?php
37+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
38+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.copy.phar');
39+
?>
40+
--EXPECT--
41+
string(4) "bbbb"
42+
bool(true)
43+
bool(true)
44+
int(3)
45+
string(4) "aaaa"
46+
string(4) "dddd"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Phar: unsetting an entry with an open file handle
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
phar.require_hash=0
8+
--FILE--
9+
<?php
10+
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
11+
$copy = __DIR__ . '/' . basename(__FILE__, '.php') . '.copy.phar';
12+
13+
$phar = new Phar($fname);
14+
$phar['a'] = 'aaaa';
15+
$phar['b'] = 'bbbb';
16+
17+
$fp = fopen('phar://' . $fname . '/b', 'r');
18+
$fp2 = fopen('phar://' . $fname . '/b', 'r');
19+
unset($phar['b']);
20+
var_dump(fread($fp, 4));
21+
var_dump(fclose($fp));
22+
var_dump(fclose($fp2));
23+
var_dump(isset($phar['b']));
24+
var_dump($phar['a']->getContent());
25+
26+
copy($fname, $copy);
27+
$saved = new Phar($copy);
28+
var_dump(count($saved));
29+
var_dump($saved['a']->getContent());
30+
?>
31+
--CLEAN--
32+
<?php
33+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
34+
@unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.copy.phar');
35+
?>
36+
--EXPECT--
37+
bool(false)
38+
bool(true)
39+
bool(true)
40+
bool(false)
41+
string(4) "aaaa"
42+
int(1)
43+
string(4) "aaaa"

0 commit comments

Comments
 (0)