Commit Graph

14002 Commits

Author SHA1 Message Date
Niels Dossche
fc32d39b7f Fix GH-11028: Heap Buffer Overflow in zval_undefined_cv.
For analysis see https://github.com/php/php-src/issues/11028#issuecomment-1508460440

Closes GH-11083.
2023-04-15 18:22:55 +02:00
Niels Dossche
450fcc4c9b [ci skip] UPDATE NEWS
ElliotNB helped me a lot debugging this by constantly testing the
patches. It is only fair that he is mentioned too, as I couldn't have
solved it without his help.
2023-04-11 22:17:47 +02:00
Niels Dossche
51faf04dbd Fix GH-10737: PHP 8.1.16 segfaults on line 597 of sapi/apache2handler/sapi_apache2.c
The TSRM keeps a hashtable mapping the thread IDs to the thread resource pointers.
It's possible that the thread disappears without us knowing, and then another thread
gets spawned some time later with the same ID as the disappeared thread.
Note that since it's a new thread the TSRM key pointer and cached pointer will be NULL.

The Apache request handler `php_handler()` will try to fetch some fields from the SAPI globals.
It uses a lazy thread resource allocation by calling `ts_resource(0);`.
This allocates a thread resource and sets up the TSRM pointers if they haven't been set up yet.

At least, that's what's supposed to happen. But since we are in a situation where the thread ID
still has the resources of the *old* thread associated in the hashtable,
the loop in `ts_resource_ex` will find that thread resource and assume the thread has been setup
already. But this is not the case since this thread is actually a new thread, just reusing the ID
of the old one, without any relation whatsoever to the old thread.
Because of this assumption, the TSRM pointers will not be setup, leading to a
NULL pointer dereference when trying to access the SAPI globals.

We can easily detect this scenario: if we're in the fallback path, and the pointer is NULL,
and we're looking for our own thread resource, we know we're actually reusing a thread ID.
In that case, we'll free up the old thread resources gracefully (gracefully because
there might still be resources open like database connection which need to be
shut down cleanly). After freeing the resources, we'll create the new resources for
this thread as if the stale resources never existed in the first place.
From that point forward, it is as if that situation never occurred.
The fact that this situation happens isn't that bad because a child process containing
threads will eventually be respawned anyway by the SAPI, so the stale thread resources
won't remain forever.

Note that we can't simply assign our own TSRM pointers to the existing
thread resource for our ID, since it was actually from a different thread
(just with the same ID!). Furthermore, the dynamically loaded extensions
have their own pointer, which is only set when their constructor is
called, so we'd have to call their constructor anyway...
I also tried to call the dtor and then the ctor again for those resources
on the pre-existing thread resource to reuse storage, but that didn't work properly
because other code doesn't expect something like that to happen, which breaks assumptions,
and this in turn caused Valgrind to (rightfully) complain about memory bugs.

Note 2: I also had to fix a bug in the core globals destruction because it
always assumed that the thread destroying them was the owning thread,
which on TSRM shutdown isn't always the case. A similar bug was fixed
recently with the JIT globals.

Closes GH-10863.
2023-04-08 16:34:07 +02:00
Niels Dossche
b81ce297a1 Fix GH-9397: exif read : warnings and errors : Potentially invalid endianess, Illegal IFD size and Undefined index
Don't misinterpret DJI info maker note as DJI maker note.

The DJI and DJI info maker note both share the "DJI" make string.
This caused the current code to try to interpret the DJI info maker note
as a DJI maker note. However, the DJI info maker note requires custom
parsing. Therefore, the misinterpretation actually caused the current
code to believe that there was an unrecoverable error in the IFD for the
maker note by returning false in the maker note parser. This in turn
caused the inability to parse other EXIF metadata.

This patch adds the identification of the DJI info maker note so that it
cannot be misinterpreted. Since we don't implement custom parsing, it
achieves this by setting the tag list to a special marker value (in this
case the NULL pointer). When this marker value is detected, the function
will just skip parsing the maker note and return true. Therefore, the
other code will believe that the IFD is not corrupt.

This approach is similar to handing an unrecognised maker note type
(see the loop on top of exif_process_IFD_in_MAKERNOTE() which also
returns true and treats it as a string). The end result of this patch
is that the DJI info maker note is considered as unknown to the caller of
exif_process_IFD_in_MAKERNOTE(), and therefore that the other EXIF
metadata can be parsed successfully.

Also fix debug output typos in exif.

Closes GH-10470.
2023-04-05 21:35:19 +02:00
Niels Dossche
0579beb842 Fix incorrect error handling in dom_zvals_to_fragment()
Discovered this pre-existing problem while testing GH-10682.
Note: this problem existed *before* that PR.

* Not all paths throw a hierarchy request error
* xmlFreeNode must be used instead of xmlFree for the fragment to also
  free its children.
* Free up nodes that couldn't be added when xmlAddChild fails.

I unified the error handling code that's exactly the same with a goto to
prevent at least some of such problems in the future.

Closes GH-10981.
2023-04-03 21:21:35 +02:00
Ilija Tovilo
84b4020eb4
Fix add_function_array() assertion when op2 contains op1
Fixes GH-10085
Closes GH-10975
Co-authored-by: Dmitry Stogov <dmitry@zend.com>
2023-04-03 12:48:46 +02:00
Niels Dossche
79c5b32d15 Fix GH-10990: mail() throws TypeError after iterating over $additional_headers array by reference
We should dereference the values, otherwise references don't work.

Closes GH-10991.
2023-04-01 19:44:43 +02:00
Niels Dossche
cf9b030a57 Fix GH-8841: php-cli core dump calling a badly formed function
It's actually not php-cli specific, nor SAPI specific.
We should delay the registration of the function into the function table
until after the compilation was successful, otherwise the function is
mistakingly registered and a NULL dereference will happen when trying to
call it.

I based my test of Nikita's test, so credits to him for the test:
https://github.com/php/php-src/pull/8933#issuecomment-1259881008

Closes GH-10989.
2023-04-01 19:43:09 +02:00
Ilija Tovilo
66ce205718
Fix incorrect zval type_flags in preg_replace_callback_array() for immutable arrays
The ZVAL_ARR macro always set the zval type_info to IS_ARRAY_EX, even if the
hash table is immutable. Since in preg_replace_callback_array() we can return
the passed array directly, and that passed array can be immutable, we need to
reset the type_flags to keep the VM from performing ref-counting on the array.

Fixes GH-10968
Closes GH-10970
2023-03-31 14:41:41 +02:00
Ilija Tovilo
41bbb116dd
Unary minus const expression consistency
- of 0.0 should result in -0.0

Closes GH-10978
2023-03-31 14:27:54 +02:00
Niels Dossche
5e76c6d264 [ci skip] NEWS 2023-03-30 21:41:11 +02:00
NathanFreeman
2d6decc14c Fix bug #80602: Segfault when using DOMChildNode::before()
This furthermore fixes the logic error explained in
https://github.com/php/php-src/pull/8729#issuecomment-1161737132

Closes GH-10682.
2023-03-30 20:49:05 +02:00
Ben Ramsey
d9df750b22
PHP-8.1 is now for PHP 8.1.19-dev 2023-03-29 19:51:20 -05:00
Niels Dossche
87862835e2 Fix undefined behaviour in unpack()
atoi()'s return value is actually undefined when an underflow or
overflow occurs. For example on 32-bit on my system the overflow test
which inputs "h2147483648" results in repetitions==2147483647 and on
64-bit this gives repetitions==-2147483648. The reason the test works on
32-bit is because there's a second undefined behaviour problem:
in case 'h' when repetitions==2147483647, we add 1 and divide by 2.
This is signed-wrap undefined behaviour and accidentally triggers the
overflow check like we wanted to.

Avoid all this trouble and use strtol with explicit error checking.

This also fixes a semantic bug where repetitions==INT_MAX would result
in the overflow check to trigger, even though there is no overflow.

Closes GH-10943.
2023-03-28 22:43:27 +02:00
Michael Orlitzky
f9cbeaa033
ext/imap/config.m4: -Werror=implicit-function-declaration compatibility.
The recent clang-16 throws errors for implicitly defined functions by
default. In many ./configure tests, an undefined function (which is
"implicitly defined" when you try to call it) is undefined because it
really does not exist. But in one case, utf8_to_mutf7() is undefined
because we forgot to include the header that defines it.

This commit updates the test for utf8_to_mutf7:

  * We now include the header (c-client.h) that defines it.
  * A "checking... yes/no" message was added to the test.
  * The test was switched from PHP_IMAP_TEST_BUILD to AC_COMPILE_IFELSE.
    This was the easiest way to avoid a return-type mismatch that runs
    afoul of -Werror=implicit-int.
  * CPPFLAGS is temporarily amended with the -I flag needed to find
    c-client.h.

Fixes GH-10947.

Closes GH-10948

Signed-off-by: George Peter Banyard <girgias@php.net>
2023-03-28 15:18:18 +01:00
Ilija Tovilo
4e0bd03681
Reset EG(trampoline).op_array.last_var that FFI may modify
Closes GH-10916
2023-03-27 22:59:00 +02:00
Niels Dossche
21e0305f5d Fix GH-10908: Bus error with PDO Firebird on RPI with 64 bit kernel and 32 bit userland
The alignment of sqldata is in most cases only the basic alignment,
so the code type-puns it to a larger type, it *can* crash due to the
misaligned access. This is only an issue for types > 4 bytes because
every sensible system requires an alignment of at least 4 bytes for
allocated data.

Even though this patch uses memcpy, the compiler is smart enough to
optimise it to something more efficient, especially on x86.
This is just the usual approach to solve these alignment problems.

Actually, unaligned memory access is undefined behaviour, so even on x86
platforms, where the bug doesn't cause a crash, this can be problematic.
Furthermore, even though the issue talks about a 64-bit kernel and
32-bit userspace, this doesn't necessarily need to be the case to
trigger this crash.

Test was Co-authored-by: rvk01

Closes GH-10920.
2023-03-27 21:01:06 +02:00
Ilija Tovilo
57029ce92e
Fix buffer-overflow in php_fgetcsv() with \0 delimiter and enclosure
Fixes oss-fuzz #57392
Closes GH-10923
2023-03-25 17:42:39 +01:00
Ilija Tovilo
c2f3a605f0
[skip ci] Add NEWS entry 2023-03-25 16:26:18 +01:00
Niels Dossche
8930bf8c33 Fix GH-8979: Possible Memory Leak with SSL-enabled MySQL connections
The stream context inside `mysqlnd_vio::enable_ssl()` is leaking.
In particular: when `php_stream_context_set()` get called the refcount
of `context` is increased by 1, which means that `context` will now
have a refcount of 2. Later on we remove the context from the stream
by calling `php_stream_context_set(stream, NULL)` but that leaves our
`context` with a refcount of 1, and therefore it's never destroyed.
In my test case this yielded a leak of 1456 bytes per connection
(but could be more depending on your settings ofc).

Annoyingly, Valgrind doesn't find it because the context is still
in the `EG(regular_list)` and will thus be destroyed at the end of
the request. However, I still think this bug needs to be fixed because
as the users in the issue report already mentioned:
there can be long-running PHP scripts.

Fix it by decreasing the refcount to transfer the ownership.

Closes GH-10909.
2023-03-24 18:03:29 +01:00
Derick Rethans
cbac68df6b Fix GH-10583: DateTime modify with tz pattern should not update linked timezone 2023-03-23 11:46:31 +00:00
Niels Dossche
301418284d Fix GH-10521: ftp_get/ftp_nb_get resumepos offset is maximum 10GB
The char arrays were too small for a long on 64-bit systems, which
resulted in cutting off the string at the end with a NUL byte. Use a
size of MAX_LENGTH_OF_LONG to fix this issue instead of a fixed size
of 11 chars.

Closes GH-10525.
2023-03-20 23:20:21 +01:00
Niels Dossche
c407243712 Fix GH-10052: Browscap crashes PHP 8.1.12 on request shutdown (apache2)
get_browser() implements a lazy parse system for the browscap
INI configuration. There are two possible moments when a browscap
configuration can be loaded: during module startup or during request.
In case of module startup, the strings are persistent strings, while for
the request they are not.

The INI parser must therefore know whether to create persistent or
non-persistent strings. It does this by looking at
CG(ini_parser_unbuffered_errors). If that value is 1 it's persistent,
otherwise non-persistent. Note that this also controls how the errors
are reported: if it's 1 then the errors are sent to stderr, otherwise we
get E_WARNINGs.

Currently, a hardcoded value of 1 is always used for that CG value in
browscap_read_file(). This means we'll always create persistent strings
*and* we'll not report parse errors correctly as E_WARNINGs.
We fix both the crash and the lack of warnings by passing the value of
persistent instead of a hardcoded 1.

This is also in line with how other INI parsing code is called in
ext/standard: they also make sure that during request a value of 0 is
passed.

Closes GH-10883.
2023-03-20 20:49:21 +01:00
Ilija Tovilo
122f1287a0
Fix GH-10885: Leaking stream_socket_server context
`php_stream_context_set` already increases the refcount.

Closes GH-10886
2023-03-20 17:42:16 +01:00
Jakub Zelenka
0d4d471847
Fix bug #74129: Incorrect SCRIPT_NAME with apache ProxyPassMatch
This happens when there are spaces are in the path info. The reason is
that Apache decodes the path info part in the SCRIPT_NAME as per CGI
RFC. FPM tries to strip path info from the SCRIPT_NAME but the
comparison is done against SCRIPT_FILENAME which is not decoded. For
that to work we have to decode it before comparison if there is any
encoded character.

Closes GH-10869
2023-03-19 13:11:14 +00:00
Ilija Tovilo
b5726c2cb1
Fix NUL byte in exception string terminating Exception::__toString()
Fixes GH-10810
Closes GH-10873
2023-03-19 10:48:26 +01:00
Niels Dossche
f30e71cbde Destroy file_handle in fpm_main
If it's not in the CG(open_files) list, we need to destroy the file
handle ourselves.

Co-authored-by: Jakub Zelenka <bukka@php.net>

Closes GH-10707.
2023-03-18 20:54:46 +01:00
Niels Dossche
06ae75007a Fix GH-8789 and GH-10015: Fix ZTS zend signal crashes due to NULL globals
Fixes GH-8789.
Fixes GH-10015.

This is one small part of the underlying bug for GH-10737, as in my
attempts to reproduce the issue I constantly hit this crash easily.
(The fix for the other underlying issue for that bug will follow soon.)

It's possible that a signal arrives at a thread that never handled a PHP
request before. This causes the signal globals to dereference a NULL
pointer because the TSRM pointers for the thread aren't set up to point
to the thread resources yet.

PR GH-9766 previously fixed this for master by ignoring the signal if
the thread didn't handle a PHP request yet. While this fixes the crash
bug, I think the solution is suboptimal for 3 reasons:

1) The signal is ignored and a message is printed saying there is a bug.
   However, this is not a bug at all. For example in Apache, the signal
   set up happens on child process creation, and the thread resource
   creation happens lazily when the first request is handled by the
   thread. Hence, the fact that the thread resources aren't set up yet
   is not actually buggy behaviour.

2) I believe since it was believed to be buggy behaviour, that fix was
   only applied to master, so 8.1 & 8.2 keep on crashing.

3) We can do better than ignoring the signal. By just acting in the
   same way as if the signals aren't active. This means we need to
   take the same path as if the TSRM had already shut down.

Closes GH-10861.
2023-03-18 11:44:29 +01:00
David CARLIER
5adeed3051 ext/psql: pg_meta_data, extended mode, fix typo for pseudo typtype.
Closes GH-10865.
2023-03-18 07:09:13 +00:00
nielsdos
c4c8d6ced7 Fix missing and inconsistent error check on SQLAllocHandle
* Missing check: SQLAllocHandle() for the environment wasn't checked in
  pdo_odbc_handle_factory(). Add a check similar to the other ones for
  SQLAllocHandle().
* Inconsistent check: one of the SQLAllocHandle() calls wasn't checked
  for SQL_SUCCESS_WITH_INFO. However, looking at the other uses and the
  documentation we should probably check this as well.

Furthermore, since there was a mix of "SQLAllocHandle: reason" and
"SQLAllocHandle (reason)" in the error reporting, I made them
consistently use the first option as that seems to be the most used for
error reporting in this file.

Closes GH-10740.
2023-03-15 21:36:50 +01:00
Niels Dossche
71c63723c8 Fix GH-10611: fpm_env_init_main leaks environ
Closes GH-10618.
2023-03-11 16:34:41 +01:00
Niels Dossche
2c53d63197 Fix GH-10801: Named arguments in CTE functions cause a segfault
Fixes GH-10801

Named arguments are not supported by the constant evaluation routine, in
the sense that they are ignored. This causes two issues:
  - It causes a crash because not all oplines belonging to the call are
    removed, which results in SEND_VA{L,R} which should've been removed.
  - It causes semantic issues (demonstrated in the test case).

This case never worked anyway, leading to crashes or incorrect behaviour,
so just prevent CTE of calls with named parameters for now.
We can choose to support it later, but introducing support for this in
a stable branch seems too dangerous.

This patch does not change the removal of SEND_* opcodes in remove_call
because the crash bug can't be triggered anymore with this patch as
there are no named parameters anymore and no variadic CTE functions
exist.

Closes GH-10811.
2023-03-10 19:22:44 +01:00
NathanFreeman
49b2ff5dbb
Fix GH-10519: Array Data Address Reference Issue
We need to carry around a reference to the underlying Bucket to be able to modify it by reference.

Closes GH-10749

Signed-off-by: George Peter Banyard <girgias@php.net>
2023-03-10 14:23:30 +00:00
Niels Dossche
85df512a12 Fix GH-8065: opcache.consistency_checks > 0 causes segfaults in PHP >= 8.1.5 in fpm context
Disable opcache.consistency_checks.

This feature does not work right now and leads to memory leaks and other
problems. For analysis and discussion see GH-8065. In GH-10624 it was
decided to disable the feature to prevent problems for end users.
If end users which to get some consistency guarantees, they can rely on
opcache.protect_memory.

Closes GH-10798.
2023-03-07 20:17:55 +01:00
Niels Dossche
ff62d117a3 Fix GH-8646: Memory leak PHP FPM 8.1
Fixes GH-8646
See https://github.com/php/php-src/issues/8646 for thorough discussion.

Interned strings that hold class entries can get a corresponding slot in map_ptr for the CE cache.
map_ptr works like a bump allocator: there is a counter which increases to allocate the next slot in the map.

For class name strings in non-opcache we have:
  - on startup: permanent + interned
  - on request: interned
For class name strings in opcache we have:
  - on startup: permanent + interned
  - on request: either not interned at all, which we can ignore because they won't get a CE cache entry
                or they were already permanent + interned
                or we get a new permanent + interned string in the opcache persistence code

Notice that the map_ptr layout always has the permanent strings first, and the request strings after.
In non-opcache, a request string may get a slot in map_ptr, and that interned request string
gets destroyed at the end of the request. The corresponding map_ptr slot can thereafter never be used again.
This causes map_ptr to keep reallocating to larger and larger sizes.

We solve it as follows:
We can check whether we had any interned request strings, which only happens in non-opcache.
If we have any, we reset map_ptr to the last permanent string.
We can't lose any permanent strings because of map_ptr's layout.

Closes GH-10783.
2023-03-07 20:16:17 +01:00
Ilija Tovilo
7202fe16b7
Fix GH-10709: UAF in recursive AST evaluation
Fixes https://oss-fuzz.com/testcase-detail/6445949468934144
Closes GH-10718
2023-03-06 14:55:34 +01:00
Niels Dossche
22c9e7e27e Add missing error check on PEM_write_bio_PKCS7()
Closes GH-10752.
2023-03-05 22:07:43 +01:00
Niels Dossche
30ebecb1d4 Add missing error checks on EVP_MD_CTX_create() and EVP_VerifyInit()
The first one returns NULL on error, and the second one returns 0 on
error. These weren't checked.

Closes GH-10762.
2023-03-05 22:05:11 +01:00
Niels Dossche
e633be3e87 Fix GH-10766: PharData archive created with Phar::Zip format does not keep files metadata (datetime)
Due to an incorrect check, the datetime was never actually set.
To test this we need to write the file using phar, but read the file
using a different method to not get a cached, or a value that's been
transformed twice and is therefore accidentally correct.

Closes GH-10769
2023-03-04 23:43:48 +01:00
nielsdos
abc6fe8f2e
Propagate success status of ftp_close() to userland
The docs say that this function returns true on success, and false on
error. This function always returns true in the current implementation
because the success return value from ftp_close() is never propagated to
userland. This affects one test: since the test server exits after an
invalid login, the ftp close correctly fails (because the server has
gone away).
2023-03-03 15:26:11 +01:00
David Carlier
ffc2a53a9b Fix GH-10728: opcache capstone header's inclusion.
Remove capstone include folder.
For most of the supported systems it worked fine somehow despite
 the pkg-config --cflags, but is always include it even on Linux.

Closes GH-10732.
2023-03-03 12:43:12 +00:00
Arnaud Le Blanc
8f92a07852 [ci skip] NEWS 2023-03-03 11:44:33 +01:00
Ilija Tovilo
df93146a15
Fix missing readonly modification error with inc/dec in JIT
Closes GH-10746
2023-03-02 11:29:53 +01:00
Patrick Allaert
729f006de8
PHP-8.1 is now for PHP 8.1.18-dev 2023-02-28 21:37:52 +01:00
Niels Dossche
0f21cbc57c Fix GH-10715: phpdbg heap buffer overflow -- by misuse of the option "--run"
Fixes GH-10715

When a string starting with a NUL character is passed to
phpdbg_vprint(), the vasprintf() will return that 0 characters have been
printed. This causes msglen == 0. When phpdbg_process_print() is called
with a message of length 0, the -1 to check for '\n' will perform an out
of bounds read. Since nothing is printed anyway for msglen == 0, it
seems best to just skip the printing routine for this case.

Closes GH-10720.
2023-02-27 19:47:55 +00:00
Niels Dossche
b09be29ac1 Fix incorrect error checking in php_openssl_set_server_dh_param()
SSL_CTX_set_tmp_dh() and SSL_CTX_set0_tmp_dh_pkey() return 1 on success
and 0 on error. But only < 0 was checked which means that errors were
never caught.

Closes GH-10705.
2023-02-26 08:17:14 +00:00
Niels Dossche
df579adac7 Fix GH-10692: PHP crashes on Windows when an inexistent filename is executed
Fixes GH-10692

php_fopen_primary_script() does not initialize all fields of
zend_file_handle. So when it fails and when fastcgi is true, the
zend_destroy_file_handle() function will try to free uninitialized
pointers, causing a segmentation fault. Fix it by zero-initializing file
handles just like the zend_stream_init_fp() counterpart does.

Closes GH-10697.
2023-02-25 14:32:55 +00:00
Ilija Tovilo
7d2b01eea7
[skip ci] Add github reference to bug fix in NEWS 2023-02-24 20:44:47 +01:00
Niels Dossche
b9a5bfc355
Fix GH-10570: Assertion `(key)->h != 0 && "Hash must be known"' failed.
Fixes GH-10570, see GH-10570 for analysis.

Closes GH-10572
2023-02-24 20:40:29 +01:00
nielsdos
8959ff39d8 Fix incorrect type for return value of zend_update_static_property_ex()
zend_update_static_property_ex() returns a zend_result, but the return
value is stored here in a bool. A bool is unsigned on my system, so in
case zend_update_static_property_ex() returns FAILURE (== -1) this gets
converted to 1 instead. This is not a valid zend_result value. This
means that (transitive) callers could mistakingly think the function
succeeded while it did in fact not succeed. Fix it by changing the type
to zend_result.

Closes GH-10691.
2023-02-24 17:02:32 +00:00