Skip to content

Fix GH-16131: Prevent mixing PDO sub-classes with different DSN #16167

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

Closed
wants to merge 1 commit into from

Conversation

kocsismate
Copy link
Member

No description provided.

@TimWolla
Copy link
Member

TimWolla commented Oct 2, 2024

Should this PR target PHP-8.4?

@kocsismate
Copy link
Member Author

Should this PR target PHP-8.4?

Yes, of course, I just always forget to set the target on GitHub :) Thanks for the reminder though!

@cmb69
Copy link
Member

cmb69 commented Oct 2, 2024

There appears to be no ABI break.

@cmb69 cmb69 removed the ABI break label Oct 2, 2024
Copy link
Member

@NattyNarwhal NattyNarwhal left a comment

Choose a reason for hiding this comment

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

It's a little annoying it has to be in each driver subclass, but my internals-fu is weak enough I can't offer any alternatives.

@kocsismate
Copy link
Member Author

It's a little annoying it has to be in each driver subclass, but my internals-fu is weak enough I can't offer any alternatives.

Yeah, agreed... Neither I have any better idea which would work... But maybe Ilija will have one if he gets a chance to review the PR.

@kocsismate kocsismate marked this pull request as ready for review October 2, 2024 20:45
@kocsismate kocsismate requested a review from iluuu1994 October 3, 2024 09:55
@NattyNarwhal
Copy link
Member

FWIW, I'm also realizing this approach is going to be annoying for third-party PDO drivers; it'll involve a last-minute addition to UPGRADING.INTERNALS and some ifdef in the stub and a short stub function for the class implementation.

@cmb69 cmb69 linked an issue Oct 9, 2024 that may be closed by this pull request
@kocsismate
Copy link
Member Author

FWIW, I'm also realizing this approach is going to be annoying for third-party PDO drivers; it'll involve a last-minute addition to UPGRADING.INTERNALS and some ifdef in the stub and a short stub function for the class implementation.

Yes, agreed... @iluuu1994 if you have capacity... do you by chance have any better approach to fix the issue than what I found?

@iluuu1994
Copy link
Member

@kocsismate I can have a look, but please know that I have 0 experience with pdo, both as a user and developer. Maybe there's somebody more qualified, @SakiTakamachi?

@kocsismate
Copy link
Member Author

kocsismate commented Oct 11, 2024

I can have a look, but please know that I have 0 experience with pdo, both as a user and developer

@iluuu1994 I think this problem is more about general OOP behavior than about PDO specific stuff: when instantiating a PDO children, we have to check whether the DSN is appropriate for the called internal PDO subclass (i.e. Pdo\Mysql, Pdo\Pgsql etc.). The problem is that these subclasses can also have children in userland, so we cannot simply use the currently called object, but we have to retrieve the internal driver-specific PDO subclass which is extended. That's why I added a separate constructor for each internal PDO subclass.

@cmb69
Copy link
Member

cmb69 commented Oct 11, 2024

The problem is that these subclasses can also have children in userland, so we cannot simply use the currently called object, but we have to retrieve the name first PDO subclass.

Can't we "just" look up the first internal class in the ancestor chain? Might not work if the class is not linked, though.

@nielsdos
Copy link
Member

Can't we "just" look up the first internal class in the ancestor chain? Might not work if the class is not linked, though.

In what scenario would it not be linked?
e.g. DOM walks the ancestor chain when dealing with overriden child classes.

@cmb69
Copy link
Member

cmb69 commented Oct 17, 2024

In what scenario would it not be linked?
e.g. DOM walks the ancestor chain when dealing with overriden child classes.

Yeah, you're right. I had just looked up zend_class_entry, and didn't think about runtime (where linking should have been happened).

@kocsismate
Copy link
Member Author

I'm going to rewrite my solution with the one suggested by Christoph ASAP

Copy link
Member Author

@kocsismate kocsismate left a comment

Choose a reason for hiding this comment

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

It turned out that neither adding a dedicated constructor for each driver-specific class, nor iterating over the class hierarchy is needed to fix the issue.

}
/* }}} */

/* {{{ */
PHP_METHOD(PDO, connect)
{
internal_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, Z_OBJ(EX(This)), EX(This).value.ce, return_value);
php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, Z_CE_P(ZEND_THIS), return_value);
Copy link
Member Author

Choose a reason for hiding this comment

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

EX(This).value.ce is Z_OBJCE_P(ZEND_THIS) which is different than Z_CE_P(ZEND_THIS). For a static method, the latter is needed to properly support the static return type

}
ZEND_ASSERT((driver->driver_name != NULL) && "PDO driver name is null");

if (!create_driver_specific_pdo_object(driver, called_scope, new_zval_object)) {
Copy link
Member Author

@kocsismate kocsismate Oct 20, 2024

Choose a reason for hiding this comment

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

create_driver_specific_pdo_object() has to be called for the constructor as well. Previously, only connect() calls eere validated...

return false;
}
}

/* A non-driver specific PDO subclass is instantiated via the constructor. This results in the legacy behavior. */
Copy link
Member Author

@kocsismate kocsismate Oct 20, 2024

Choose a reason for hiding this comment

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

Without this, older tests using legacy "quasi classes" would fail, now that constructor calls are also validated.

@@ -14,4 +14,4 @@ try {

?>
--EXPECT--
Pdo\Pgsql::connect() cannot be called when connecting to the "sqlite" driver, either Pdo\Sqlite::connect() or PDO::connect() must be called instead
Pdo\Pgsql::connect() cannot be used for connecting to the "sqlite" driver, either call Pdo\Sqlite::connect() or PDO::connect() instead
Copy link
Member Author

Choose a reason for hiding this comment

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

The new wording is more clear IMO.

@@ -694,6 +694,8 @@ PDO_API void php_pdo_dbh_delref(pdo_dbh_t *dbh);
PDO_API void php_pdo_free_statement(pdo_stmt_t *stmt);
PDO_API void php_pdo_stmt_set_column_count(pdo_stmt_t *stmt, int new_count);

PDO_API void php_pdo_internal_construct_driver(INTERNAL_FUNCTION_PARAMETERS, zend_object *current_object, zend_class_entry *called_scope, zval *new_zval_object);
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess it makes sense to expose this function for extensions which override the PDO constructor (?)

Copy link
Member

@NattyNarwhal NattyNarwhal left a comment

Choose a reason for hiding this comment

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

This seems to be a much better solution that minimize impact elsewhere. The only weird part is the stack trace does mention the PDO base class, even if I use a driver subclass instead. i.e.

Fatal error: Uncaught PDOException: Pdo\Mysql::__construct() cannot be used for connecting to the "sqlite" driver, either call Pdo\Sqlite::__construct() or PDO::__construct() instead in /Users/calvin/src/evil.php:2
Stack trace:
#0 /Users/calvin/src/evil.php(2): PDO->__construct('sqlite:test.db')
#1 {main}
  thrown in /Users/calvin/src/evil.php on line 2

(where line 2 is $db = new Pdo\Mysql('sqlite:test.db');)

@cmb69
Copy link
Member

cmb69 commented Oct 22, 2024

The only weird part is the stack trace does mention the PDO base class, even if I use a driver subclass instead.

I think we need to fix that somehow (thinking about debug_backtrace() and friends, too).

@kocsismate
Copy link
Member Author

kocsismate commented Oct 22, 2024

The only weird part is the stack trace does mention the PDO base class, even if I use a driver subclass instead.

I wasn't 100% sure at first, but It seems like this is a general behavior: https://3v4l.org/aLUDr#v8.3.12

Anyway, I agree that this should be improved.

@kocsismate
Copy link
Member Author

Since I'm leaving for vacation, I'm merging this. I can do followup changes/fixes if someone reviews it later.

@kocsismate
Copy link
Member Author

Closing via 5892991

@kocsismate kocsismate closed this Oct 22, 2024
@kocsismate kocsismate deleted the fix-gh-16131 branch October 22, 2024 20:56
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.

It is possible to mix PDO sub-classes with different dsn and crash PHP
6 participants