Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace zend_object object with its header to prevent the "using flexible array in the middle of another struct" problem in its inheritted classes (static analyzer report) #17598

Open
2 of 16 tasks
Snape3058 opened this issue Jan 27, 2025 · 15 comments

Comments

@Snape3058
Copy link

Snape3058 commented Jan 27, 2025

Class zend_object is defined as a flexible array of length 1. The flexible array defined with size 1 and 0 is not the standard behavior. It is suggested to use the unsized definition (https://people.kernel.org/kees/bounded-flexible-arrays-in-c). Besides, not all its subclasses will use the array field properties_table of the zend_object class. If I understand the code correctly, when the properties_table[0] field is not used, it will store a ZVAL_UNDEF zval indicating the end of the iteration. Whereas when the properties_table[0] field is used, the flags and array length are checked first before accessing the data in the array.

If the properties_table[0] field is not used in these sub-classes, will it be better to replace the zend_object in these classes with only the header part of zend_object?

i.e. (as suggested in case 2 of https://lpc.events/event/18/contributions/1722/attachments/1591/3303/Wfamnae_lpceu2024.pdf)
We can define another struct with only the header part (let's name it zend_object_header_part), but leave the zend_object struct with both the header and the flexible array part.
When only the header is needed, we can use the zend_object_header_part (e.g. in the class inheritance), whereas for those requiring the array part, or using the object through a zend_object pointer, we can still use the full definition.


Usages of zend_object in the middle of other structs whose array field is potentially never used through the composite struct:

report ids: 250106-1639:1-6,8-17 (16 reports in total)

@cmb69
Copy link
Member

cmb69 commented Jan 27, 2025

Hmm, as of PHP 7.0.0, zend_object is supposed to be the last member (see https://www.npopov.com/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html#objects-in-php-7). I thought only ext/com hasn't been changed in this regard, but apparently there are several other classes. I'll have a look at ext/com.

@cmb69
Copy link
Member

cmb69 commented Jan 27, 2025

/* offset of real object header (usually zero) */

Appears to be somewhat outdated.

@Snape3058
Copy link
Author

Appears to be somewhat outdated.

Should we make changes to them? Or are they left for compatibility with the old code?

@cmb69
Copy link
Member

cmb69 commented Jan 27, 2025

Well, the comment should probably be updated, and the struct layouts should be changed.

@Snape3058
Copy link
Author

I am not very familiar with the code base. I can draft patches for them but I still need other developers to help me to prevent possible mistakes.

I will try to resolve one of them in recent days and open a PR.

Thank you for your replies.

@cmb69
Copy link
Member

cmb69 commented Jan 27, 2025

See the following for a yet incomplete patch for com_saproxy`(ignore the changes to build.ninja):

 ext/com_dotnet/com_saproxy.c | 19 ++++++++++++-------
 win32/build/build.ninja      |  2 +-
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/ext/com_dotnet/com_saproxy.c b/ext/com_dotnet/com_saproxy.c
index ea0e9e47a1..3e12d695fe 100644
--- a/ext/com_dotnet/com_saproxy.c
+++ b/ext/com_dotnet/com_saproxy.c
@@ -33,7 +33,6 @@
 #include "Zend/zend_exceptions.h"
 
 typedef struct {
-	zend_object std;
 	/* the object we a proxying for; we hold a refcount to it */
 	php_com_dotnet_object *obj;
 
@@ -42,7 +41,7 @@ typedef struct {
 
 	/* this is an array whose size_is(dimensions) */
 	zval *indices;
-
+	zend_object std;
 } php_com_saproxy;
 
 typedef struct {
@@ -55,13 +54,19 @@ typedef struct {
 	LONG *indices;
 } php_com_saproxy_iter;
 
-#define SA_FETCH(zv)			(php_com_saproxy*)Z_OBJ_P(zv)
+static inline php_com_saproxy *php_com_saproxy_from_obj(zend_object *obj) {
+	return (php_com_saproxy *) ((char *) obj - XtOffsetOf(php_com_saproxy, std));
+}
+
+#define SA_FETCH(zv) php_com_saproxy_from_obj(Z_OBJ_P(zv))
 
 zend_object *php_com_saproxy_create_object(zend_class_entry *class_type)
 {
-	php_com_saproxy *intern = emalloc(sizeof(*intern));
-	memset(intern, 0, sizeof(*intern));
+	size_t block_len = sizeof(php_com_saproxy) + zend_object_properties_size(class_type);
+	php_com_saproxy *intern = emalloc(block_len);
+	memset(intern, 0, block_len);
 	zend_object_std_init(&intern->std, class_type);
+	object_properties_init(&intern->std, class_type);
 	return &intern->std;
 }
 
@@ -364,7 +369,7 @@ static zend_result saproxy_count_elements(zend_object *object, zend_long *count)
 
 static void saproxy_free_storage(zend_object *object)
 {
-	php_com_saproxy *proxy = (php_com_saproxy *)object;
+	php_com_saproxy *proxy = php_com_saproxy_from_obj(object);
 //???	int i;
 //???
 //???	for (i = 0; i < proxy->dimensions; i++) {
@@ -398,7 +403,7 @@ static zend_object* saproxy_clone(zend_object *object)
 }
 
 zend_object_handlers php_com_saproxy_handlers = {
-	0,
+	XtOffsetOf(php_com_saproxy, std),
 	saproxy_free_storage,
 	zend_objects_destroy_object,
 	saproxy_clone,
diff --git a/win32/build/build.ninja b/win32/build/build.ninja
index 71db89e375..45580ce028 100644
--- a/win32/build/build.ninja
+++ b/win32/build/build.ninja
@@ -23,7 +23,7 @@ PGOMGR="${PGOMGR}"
 PHP_BUILD=${PHP_BUILD}
 
 # See https://ninja-build.org/manual.html#_deps
-# msvc_deps_prefix = Hinweis: Einlesen der Datei:
+msvc_deps_prefix = Hinweis: Einlesen der Datei:
 
 MCFILE=${BUILD_DIR}\wsyslog.rc

Girgias added a commit to Girgias/php-src that referenced this issue Jan 28, 2025
@Girgias
Copy link
Member

Girgias commented Jan 28, 2025

I've submitted #17606 for the pdo_row_t struct.

@Snape3058
Copy link
Author

To make it clear, I added checkboxes to each case to be fixed.

@Snape3058
Copy link
Author

I am working on _zend_generator now. I will check the box when I submit the PR.

Girgias added a commit that referenced this issue Jan 29, 2025
As of PHP 7 [1] the `std` should be at the end of the struct instead of at the beginning.

See GH-17598 for more UB related details.

[1] https://www.npopov.com/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html#objects-in-php-7
@Snape3058
Copy link
Author

@cmb69 I tried to make a change to zend_generator (Snape3058@ebe9ffc). However. it seems that the generators returned from a function (related to execute_data) cannot be addressed correctly after the change. Could you please help me with this patch? Where can I find the places creating and assigning the return values?

@cmb69
Copy link
Member

cmb69 commented Jan 29, 2025

@Snape3058, EX(return_value) is of type zval*, so I would assume that a zend_generator* is just cast to zval* somewhere. If I'm right, just sticking with the cast back to zend_generator* should work. Anyway, consider to file a draft PR, where such details can be better discussed.

@Snape3058
Copy link
Author

@cmb69 The current version (Snape3058@ebe9ffc) contains the changes to all cases that I can find. But it cannot pass the test cases. Is it OK to draft a PR now?

@cmb69
Copy link
Member

cmb69 commented Jan 29, 2025

Is it OK to draft a PR now?

Yes, sure. :)

@Girgias
Copy link
Member

Girgias commented Jan 29, 2025

I don't think tackling the Zend or Opcache ones is the easiest first step. Fixing the ext/intl are probably the easiest.

@bwoebi
Copy link
Member

bwoebi commented Jan 30, 2025

I thought we had the zend_object on purpose as the first member on those classes which don't use properties.
The purpose being avoiding useless offset computations and better locality for the trivial refcounting which is always in the first place when zend_object is the first item.

IIRC it was worth the extra 8 bytes allocated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants