Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ PHP NEWS
ignored. (ndossche)
. Fixed a bypass of the magic ".phar" directory protection in
Phar::addEmptyDir() for paths starting with "/.phar". (Weilin Du)
. Fixed an integer underflow when parsing ZIP extra fields. (Weilin Du)
. Phar::addEmptyDir() now allows non-magic directory names that merely
share the ".phar" prefix. (Weilin Du)
. Support overridden methods in SplFileInfo for getMTime() and getPathname()
Expand Down Expand Up @@ -254,6 +255,8 @@ PHP NEWS
(Weilin Du)
. getenv() and putenv() now raises a ValueError when the first argument
contains null bytes. (Weilin Du)
. dl() now raises a ValueError when the $extension_filename argument
contains null bytes. (Weilin Du)
. parse_str() now raises a ValueError when the $string argument contains
null bytes. (Weilin Du)
. proc_open() now raises a ValueError when the $cwd argument contains
Expand Down
6 changes: 6 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ PHP 8.6 UPGRADE NOTES
argument value is passed.
. getenv() and putenv() now raises a ValueError when the first argument
contains null bytes.
. dl() now raises a ValueError when the $extension_filename argument
contains null bytes.
. parse_str() now raises a ValueError when the $string argument contains
null bytes.
. linkinfo() now raises a ValueError when the $path argument is empty.
Expand Down Expand Up @@ -233,6 +235,10 @@ PHP 8.6 UPGRADE NOTES
tcp_keepintvl and tcp_keepcnt that allow setting socket keepalive
options.
. Allowed casting casting filtered streams as file descriptor for select.
. Added the "write_seek_mode stream" filter parameter for the bz2, iconv,
zlib, and string stream filters. This parameter must be set via an
associative array where the key is "write_seek_mode stream" and the
value is one of the following strings "preserve", "reset", or "strict".

- URI:
. Added Uri\Rfc3986\Uri:getUriType() and Uri\WhatWg\Url:isSpecialScheme().
Expand Down
91 changes: 91 additions & 0 deletions ext/phar/tests/zip/zip_extra_underflow.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
--TEST--
Phar: ZIP extra field length must not underflow
--EXTENSIONS--
phar
--FILE--
<?php
function uint16($value) {
return pack('v', $value);
}

function uint32($value) {
return pack('V', $value);
}

$filename = __DIR__ . '/zip_extra_underflow.zip';
$entry = 'test.txt';
$contents = 'hello';
$crc = crc32($contents);

$local = uint32(0x04034b50)
. uint16(20)
. uint16(0)
. uint16(0)
. uint16(0)
. uint16(0)
. uint32($crc)
. uint32(strlen($contents))
. uint32(strlen($contents))
. uint16(strlen($entry))
. uint16(0)
. $entry
. $contents;

$extra = 'XX' . uint16(1);

/* Old code seeks one byte past the extra field and parses this as another extra header. */
$commentPrefix = 'A'
. 'UT'
. uint16(5)
. "\x01"
. uint32(946684800)
. 'ZZ'
. uint16(65522);
$comment = $commentPrefix . str_repeat('B', 65535 - strlen($commentPrefix));

$central = uint32(0x02014b50)
. uint16(20)
. uint16(20)
. uint16(0)
. uint16(0)
. uint16(0)
. uint16(0)
. uint32($crc)
. uint32(strlen($contents))
. uint32(strlen($contents))
. uint16(strlen($entry))
. uint16(strlen($extra))
. uint16(strlen($comment))
. uint16(0)
. uint16(0)
. uint32(0)
. uint32(0)
. $entry
. $extra
. $comment;

$eocd = uint32(0x06054b50)
. uint16(0)
. uint16(0)
. uint16(1)
. uint16(1)
. uint32(strlen($central))
. uint32(strlen($local))
. uint16(0);

file_put_contents($filename, $local . $central . $eocd);

try {
$phar = new PharData($filename);
echo "Loaded corrupt ZIP\n";
echo $phar[$entry]->getMTime(), "\n";
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}
?>
--CLEAN--
<?php
@unlink(__DIR__ . '/zip_extra_underflow.zip');
?>
--EXPECTF--
phar error: Unable to process extra field header for file in central directory in zip-based phar "%szip_extra_underflow.zip"
52 changes: 37 additions & 15 deletions ext/phar/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,30 @@ static inline void phar_write_16(char buffer[2], uint32_t value)
# define PHAR_SET_32(var, value) phar_write_32(var, (uint32_t) (value));
# define PHAR_SET_16(var, value) phar_write_16(var, (uint16_t) (value));

static zend_result phar_zip_process_extra(php_stream *fp, phar_entry_info *entry, uint16_t len) /* {{{ */
static zend_result phar_zip_process_extra(php_stream *fp, phar_entry_info *entry, uint16_t extra_len) /* {{{ */
{
union {
phar_zip_extra_field_header header;
phar_zip_unix3 unix3;
phar_zip_unix_time time;
} h;
size_t len = extra_len;
size_t read;

do {
while (len) {
size_t header_size;

if (len < sizeof(h.header)) {
return FAILURE;
}
if (sizeof(h.header) != php_stream_read(fp, (char *) &h.header, sizeof(h.header))) {
return FAILURE;
}
len -= sizeof(h.header);
header_size = PHAR_GET_16(h.header.size);
if (header_size > len) {
return FAILURE;
}

if (h.header.tag[0] == 'U' && h.header.tag[1] == 'T') {
/* Unix timestamp header found.
Expand All @@ -60,7 +71,6 @@ static zend_result phar_zip_process_extra(php_stream *fp, phar_entry_info *entry
* We only store the modification time in the entry, so only read that.
*/
const size_t min_size = 5;
uint16_t header_size = PHAR_GET_16(h.header.size);
if (header_size >= min_size) {
read = php_stream_read(fp, &h.time.flags, min_size);
if (read != min_size) {
Expand All @@ -71,36 +81,48 @@ static zend_result phar_zip_process_extra(php_stream *fp, phar_entry_info *entry
entry->timestamp = PHAR_GET_32(h.time.time);
}

len -= header_size + 4;

/* Consume remaining bytes */
if (header_size != read) {
php_stream_seek(fp, header_size - read, SEEK_CUR);
if (header_size != read && -1 == php_stream_seek(fp, header_size - read, SEEK_CUR)) {
return FAILURE;
}
len -= header_size;
continue;
}
/* Fallthrough to next if to skip header */
}

if (h.header.tag[0] != 'n' || h.header.tag[1] != 'u') {
/* skip to next header */
php_stream_seek(fp, PHAR_GET_16(h.header.size), SEEK_CUR);
len -= PHAR_GET_16(h.header.size) + 4;
if (header_size && -1 == php_stream_seek(fp, header_size, SEEK_CUR)) {
return FAILURE;
}
len -= header_size;
continue;
}

/* unix3 header found */
read = php_stream_read(fp, (char *) &(h.unix3.crc32), sizeof(h.unix3) - sizeof(h.header));
len -= read + 4;
size_t unix3_size = sizeof(h.unix3) - sizeof(h.header);
size_t field_size = header_size;
if (field_size == unix3_size - sizeof(h.unix3.crc32)) {
/* Some archives omit the CRC32 from the unix3 size field. */
field_size = unix3_size;
}
if (field_size < unix3_size || field_size > len) {
return FAILURE;
}

if (sizeof(h.unix3) - sizeof(h.header) != read) {
read = php_stream_read(fp, (char *) &(h.unix3.crc32), unix3_size);
if (unix3_size != read) {
return FAILURE;
}

if (PHAR_GET_16(h.unix3.size) > sizeof(h.unix3) - 4) {
if (field_size > unix3_size) {
/* skip symlink filename - we may add this support in later */
php_stream_seek(fp, PHAR_GET_16(h.unix3.size) - sizeof(h.unix3.size), SEEK_CUR);
if (-1 == php_stream_seek(fp, field_size - unix3_size, SEEK_CUR)) {
return FAILURE;
}
}
len -= field_size;

/* set permissions */
entry->flags &= PHAR_ENT_COMPRESSION_MASK;
Expand All @@ -111,7 +133,7 @@ static zend_result phar_zip_process_extra(php_stream *fp, phar_entry_info *entry
entry->flags |= PHAR_GET_16(h.unix3.perms) & PHAR_ENT_PERM_MASK;
}

} while (len);
}

return SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PHPAPI PHP_FUNCTION(dl)
size_t filename_len;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(filename, filename_len)
Z_PARAM_PATH(filename, filename_len)
ZEND_PARSE_PARAMETERS_END();

if (!PG(enable_dl)) {
Expand Down
68 changes: 63 additions & 5 deletions ext/standard/io_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static zend_object_handlers php_io_poll_watcher_object_handlers;
static zend_object_handlers php_io_poll_handle_object_handlers;

/* Watcher object structure */
typedef struct {
typedef struct php_io_poll_watcher_object {
php_poll_handle_object *handle;
uint32_t watched_events;
uint32_t triggered_events;
Expand All @@ -55,15 +55,16 @@ typedef struct {
} php_io_poll_watcher_object;

/* Context object structure */
typedef struct {
typedef struct php_io_poll_context_object {
php_poll_ctx *ctx;
HashTable *watchers; /* Maps handle pointer -> watcher object */
zend_object std;
} php_io_poll_context_object;

/* Stream poll handle specific data */
typedef struct {
typedef struct php_stream_poll_handle_data {
php_stream *stream;
zend_resource *res;
} php_stream_poll_handle_data;

/* Accessor macros */
Expand Down Expand Up @@ -250,7 +251,9 @@ static void php_stream_poll_handle_cleanup(php_poll_handle_object *handle)
{
php_stream_poll_handle_data *data = (php_stream_poll_handle_data *) handle->handle_data;
if (data) {
/* Don't close the stream - user still owns it */
if (data->res) {
zend_list_delete(data->res);
}
efree(data);
handle->handle_data = NULL;
}
Expand Down Expand Up @@ -331,6 +334,15 @@ static void php_io_poll_context_free_object(zend_object *obj)
{
php_io_poll_context_object *intern = PHP_POLL_CONTEXT_OBJ_FROM_ZOBJ(obj);

if (intern->watchers) {
zval *zv;
ZEND_HASH_FOREACH_VAL(intern->watchers, zv) {
php_io_poll_watcher_object *watcher = PHP_POLL_WATCHER_OBJ_FROM_ZOBJ(Z_OBJ_P(zv));
watcher->active = false;
watcher->poll_ctx = NULL;
} ZEND_HASH_FOREACH_END();
}

if (intern->ctx) {
php_poll_destroy(intern->ctx);
}
Expand All @@ -343,6 +355,36 @@ static void php_io_poll_context_free_object(zend_object *obj)
zend_object_std_dtor(&intern->std);
}

static HashTable *php_io_poll_watcher_get_gc(zend_object *obj, zval **table, int *n)
{
php_io_poll_watcher_object *intern = PHP_POLL_WATCHER_OBJ_FROM_ZOBJ(obj);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();

zend_get_gc_buffer_add_zval(gc_buffer, &intern->data);
if (intern->handle) {
zend_get_gc_buffer_add_obj(gc_buffer, &intern->handle->std);
}

zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}

static HashTable *php_io_poll_context_get_gc(zend_object *obj, zval **table, int *n)
{
php_io_poll_context_object *intern = PHP_POLL_CONTEXT_OBJ_FROM_ZOBJ(obj);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();

if (intern->watchers) {
zval *zv;
ZEND_HASH_FOREACH_VAL(intern->watchers, zv) {
zend_get_gc_buffer_add_zval(gc_buffer, zv);
} ZEND_HASH_FOREACH_END();
}

zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}

/* Utility functions */

static zend_always_inline zend_ulong php_io_poll_compute_ptr_key(void *ptr)
Expand Down Expand Up @@ -448,13 +490,19 @@ PHP_METHOD(StreamPollHandle, __construct)

php_poll_handle_object *intern = PHP_POLL_HANDLE_OBJ_FROM_ZV(getThis());

if (intern->handle_data) {
zend_throw_error(NULL, "StreamPollHandle object is already constructed");
RETURN_THROWS();
}

/* Set up stream-specific data */
php_stream_poll_handle_data *data = emalloc(sizeof(php_stream_poll_handle_data));
data->stream = stream;
data->res = stream->res;
intern->handle_data = data;

/* Add reference to stream */
GC_ADDREF(stream->res);
GC_ADDREF(data->res);
}

PHP_METHOD(StreamPollHandle, getStream)
Expand Down Expand Up @@ -657,6 +705,11 @@ PHP_METHOD(Io_Poll_Context, __construct)

php_io_poll_context_object *intern = PHP_POLL_CONTEXT_OBJ_FROM_ZV(getThis());

if (intern->ctx) {
zend_throw_error(NULL, "Io\\Poll\\Context object is already constructed");
RETURN_THROWS();
}

php_poll_backend_type backend_type = PHP_POLL_BACKEND_AUTO;
if (backend_obj != NULL) {
backend_type = php_io_poll_backend_enum_to_type(Z_OBJ_P(backend_obj));
Expand Down Expand Up @@ -861,6 +914,7 @@ PHP_MINIT_FUNCTION(poll)
memcpy(&php_io_poll_handle_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_io_poll_handle_object_handlers.offset = offsetof(php_poll_handle_object, std);
php_io_poll_handle_object_handlers.free_obj = php_poll_handle_object_free;
php_io_poll_handle_object_handlers.clone_obj = NULL;
php_stream_poll_handle_class_entry->default_object_handlers = &php_io_poll_handle_object_handlers;

/* Register Watcher class */
Expand All @@ -871,6 +925,8 @@ PHP_MINIT_FUNCTION(poll)
sizeof(zend_object_handlers));
php_io_poll_watcher_object_handlers.offset = offsetof(php_io_poll_watcher_object, std);
php_io_poll_watcher_object_handlers.free_obj = php_io_poll_watcher_free_object;
php_io_poll_watcher_object_handlers.get_gc = php_io_poll_watcher_get_gc;
php_io_poll_watcher_object_handlers.clone_obj = NULL;
php_io_poll_watcher_class_entry->default_object_handlers = &php_io_poll_watcher_object_handlers;

/* Register Context class */
Expand All @@ -881,6 +937,8 @@ PHP_MINIT_FUNCTION(poll)
sizeof(zend_object_handlers));
php_io_poll_context_object_handlers.offset = offsetof(php_io_poll_context_object, std);
php_io_poll_context_object_handlers.free_obj = php_io_poll_context_free_object;
php_io_poll_context_object_handlers.get_gc = php_io_poll_context_get_gc;
php_io_poll_context_object_handlers.clone_obj = NULL;
php_io_poll_context_class_entry->default_object_handlers = &php_io_poll_context_object_handlers;

/* Register exception hierarchy */
Expand Down
Loading