Skip to content

Prevent direct instantiation of com_safearray_proxy #10278

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

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Jan 10, 2023

The com_safearray_proxy class is meant for internal usage, but so far it was possible to instantiate it from userland, although that made no sense. However, a while ago there was a relevant change[1], namely that its default_object_handlers are now assigned when the class is registered, while previously they only have been assigned when an instance had been created internally. So now when freeing a manually created object, free_obj() is called, although the object never has been properly initialized (causing segfaults).

We fix this by introducing a create_object() handler which properly initializes the object with dummy values. Since a manually created com_safearray_proxy still does not make sense, we disallow its instantiation.

[1] 94ee4f9

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me

@cmb69
Copy link
Member Author

cmb69 commented Jan 10, 2023

Needs more work, though.

cmb69 added 2 commits October 6, 2024 19:09
The `com_safearray_proxy` class is meant for internal usage, but so far
it was possible to instantiate it from userland, although that made no
sense.  However, a while ago there was a relevant change[1], namely
that its `default_object_handlers` are now assigned when the class is
registered, while previously they only have been assigned when an
instance had been created internally.  So now when freeing a manually
created object, `free_obj()` is called, although the object never has
been properly initialized (causing segfaults).

We fix this by introducing a `create_object()` handler which properly
initializes the object with dummy values.  Since a manually created
`com_safearray_proxy` still does not make sense, we disallow its
instantiation.

[1] <php@94ee4f9>
com_dotnet objects are special, since they still have the `zend_object`
as first member, so we need to use a somewhat different initialization.
@cmb69
Copy link
Member Author

cmb69 commented Oct 6, 2024

Needs more work, though.

That was because ASan complained about the new test. To get the latest goodies (such as Windows ASan running in CI ;), I've rebased onto master, and added a commit that fixes the problem.

@cmb69 cmb69 marked this pull request as ready for review October 6, 2024 20:21
@@ -57,6 +57,14 @@ typedef struct {

#define SA_FETCH(zv) (php_com_saproxy*)Z_OBJ_P(zv)

zend_object *php_com_saproxy_create_object(zend_class_entry *class_type)
{
php_com_saproxy *intern = emalloc(sizeof(*intern));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use zend_object_alloc which will do the allocation and memset

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would't zero the last sizeof(zend_object) bytes of the structure, due to com_dotnet objects having the zend_object as first member, not as last, as usual. I'm planning to change this soonish.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's weird...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the standard struct layout in PHP 5; all bundled extension had been changed for PHP 7, but com_dotnet had been overlooked.

Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
@cmb69 cmb69 merged commit 2f52dbd into php:master Oct 7, 2024
10 checks passed
@cmb69 cmb69 deleted the cmb/saproxy branch October 7, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants