Commit c41c7227b84 for php
commit c41c7227b84a29bfa81042143f368290c27f530f
Merge: 8ca3c1d3200 f59f30cbc53
Author: David Carlier <devnexen@gmail.com>
Date: Mon Nov 23 20:50:31 2026 +0000
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5:
ext/phar: unset() of an entry with an open handle use-after-free.
# Conflicts:
# ext/phar/phar_internal.h
# ext/phar/tar.c
# ext/phar/zip.c
diff --cc ext/phar/phar_internal.h
index 0eaa65942cc,10e6a234edd..b78a8b0176d
--- a/ext/phar/phar_internal.h
+++ b/ext/phar/phar_internal.h
@@@ -261,6 -268,9 +261,8 @@@ struct _phar_archive_data
php_stream *fp;
/* decompressed file contents are stored here */
php_stream *ufp;
+ /* number of open entry handles per fp/ufp stream, current or replaced by a flush */
+ HashTable *fp_refs;
- int refcount;
uint32_t sig_flags;
uint32_t sig_len;
char *signature;
@@@ -488,16 -499,17 +490,18 @@@ extern HashTable cached_alias
bool phar_archive_delref(phar_archive_data *phar);
void phar_entry_delref(phar_entry_data *idata);
+ void phar_fp_addref(phar_archive_data *phar, php_stream *fp);
+ bool phar_fp_in_use(const phar_archive_data *phar, const php_stream *fp);
-phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security);
-phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security);
-phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
-zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security);
-int phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
-int phar_flush(phar_archive_data *archive, char **error);
-zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete);
-zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create);
+phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, bool security);
+phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, bool security);
+ZEND_ATTRIBUTE_NONNULL phar_entry_data *phar_get_or_create_entry_data(zend_string *fname, char *path, size_t path_len, const char *mode, char allow_dir, char **error, bool security, uint32_t timestamp);
+ZEND_ATTRIBUTE_NONNULL zend_result phar_get_entry_data(phar_entry_data **ret, const zend_string *fname, char *path, size_t path_len, const char *mode, char allow_dir, char **error, bool security);
+ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) int phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
+ZEND_ATTRIBUTE_NONNULL int phar_flush(phar_archive_data *archive, char **error);
+zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, bool is_complete);
+zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create, const char **error);
+zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create);
typedef enum {
pcr_use_query,
diff --cc ext/phar/tar.c
index ae1a6e46c09,daf2a756b0c..d47aa8ff926
--- a/ext/phar/tar.c
+++ b/ext/phar/tar.c
@@@ -1181,12 -1173,10 +1166,10 @@@ nostub
pass.old = oldfile;
pass.new = newfile;
pass.error = error;
- pass.free_fp = true;
- pass.free_ufp = true;
if (phar_metadata_tracker_has_data(&phar->metadata_tracker, phar->is_persistent)) {
- phar_entry_info *mentry;
- if (NULL != (mentry = zend_hash_str_find_ptr(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1))) {
+ phar_entry_info *mentry = zend_hash_str_find_ptr(&(phar->manifest), ZEND_STRL(".phar/.metadata.bin"));
+ if (mentry) {
if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) {
if (must_close_old_file) {
php_stream_close(oldfile);
@@@ -1324,7 -1316,13 +1307,7 @@@
php_stream_close(oldfile);
}
- if (phar->fp && pass.free_fp) {
- /* on error in the hash iterator above, error is set */
- if (error && *error) {
- php_stream_close(newfile);
- return EOF;
- }
-
+ if (phar->fp && !phar_fp_in_use(phar, phar->fp)) {
php_stream_close(phar->fp);
}
diff --cc ext/phar/zip.c
index 13c34d8a3b0,cca65b31908..d5764c76a95
--- a/ext/phar/zip.c
+++ b/ext/phar/zip.c
@@@ -1139,21 -1141,9 +1138,9 @@@ continue_dir
entry->is_modified = 0;
} else {
entry->is_modified = 0;
- if (entry->fp_refcount) {
- /* open file pointers refer to this fp, do not free the stream */
- switch (entry->fp_type) {
- case PHAR_FP:
- p->free_fp = false;
- break;
- case PHAR_UFP:
- p->free_ufp = false;
- default:
- break;
- }
- }
if (!entry->is_dir && entry->compressed_filesize && (!p->old || SUCCESS != php_stream_copy_to_stream_ex(p->old, p->filefp, entry->compressed_filesize, NULL))) {
- spprintf(p->error, 0, "unable to copy contents of file \"%s\" while creating zip-based phar \"%s\"", ZSTR_VAL(entry->filename), entry->phar->fname);
+ spprintf(p->error, 0, "unable to copy contents of file \"%s\" while creating zip-based phar \"%s\"", ZSTR_VAL(entry->filename), ZSTR_VAL(entry->phar->fname));
return ZEND_HASH_APPLY_STOP;
}
}
@@@ -1440,6 -1442,13 +1422,9 @@@ nocentralerror
return EOF;
}
- if (FAILURE == phar_zip_applysignature(phar, &pass)) {
- goto temperror;
- }
-
+ PHAR_SET_16(eocd.counthere, pass.count);
+ PHAR_SET_16(eocd.count, pass.count);
+
/* save zip */
cdir_size = php_stream_tell(pass.centralfp);
cdir_offset = php_stream_tell(pass.filefp);