From 01f31baeb5364ac3635a20c3dbe2d5a51625c8a2 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 24 Nov 2026 20:58:16 +0000 Subject: [PATCH] ext/mysqlnd: Fix GH-19222 - Handle ok packet with auth_socket --- NEWS | 4 + ext/mysqli/tests/gh19222.phpt | 55 +++++++++++++ ext/mysqlnd/mysqlnd_auth.c | 120 ++++++++++++++++++++++++----- ext/mysqlnd/mysqlnd_wireprotocol.c | 5 ++ ext/mysqlnd/mysqlnd_wireprotocol.h | 2 + 5 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 ext/mysqli/tests/gh19222.phpt diff --git a/NEWS b/NEWS index 3ab26f0768f2..954a0c831c9e 100644 --- a/NEWS +++ b/NEWS @@ -95,6 +95,10 @@ PHP NEWS . Fixed the JIT freeing the string returned by mb_encode_mimeheader() or mb_output_handler() while it is still in use. (Ilia Alshanetsky) +- MySQLnd: + . Fixed bug GH-19222 (php hangs when auth_socket is set for the user). + (Kamil Tekiela) + - Opcache: . Fixed bug GH-24801 (JIT: SIGFPE in ir_fold MUL_OV constant folding). (waroad) diff --git a/ext/mysqli/tests/gh19222.phpt b/ext/mysqli/tests/gh19222.phpt new file mode 100644 index 000000000000..72fa07d1de31 --- /dev/null +++ b/ext/mysqli/tests/gh19222.phpt @@ -0,0 +1,55 @@ +--TEST-- +GH-19222 (php hangs when auth_socket is set for the user) +--EXTENSIONS-- +mysqli +--INI-- +mysqlnd.net_read_timeout=5 +--SKIPIF-- +server_info, 'MariaDB') || $link->server_version < 80000) { + die('skip MySQL 8.0+ is required for caching_sha2_password and auth_socket'); +} +$status = $link->query("SELECT PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME = 'auth_socket'")->fetch_row(); +if ((!$status || $status[0] !== 'ACTIVE') && !$link->query("INSTALL PLUGIN auth_socket SONAME 'auth_socket.so'")) { + die('skip Cannot install the auth_socket plugin: ' . $link->error); +} +$link->query("DROP USER IF EXISTS 'gh19222'@'localhost'"); +if (!$link->query(sprintf("CREATE USER 'gh19222'@'localhost' IDENTIFIED WITH auth_socket AS '%s'", $link->real_escape_string(get_current_user())))) { + die('skip Cannot create an auth_socket user: ' . $link->error); +} +// The server accepts an auth_socket login only over the Unix socket from the matching OS user +$conn = @mysqli_connect('localhost', 'gh19222', '', '', 0, $socket); +$link->query("DROP USER 'gh19222'@'localhost'"); +if (!$conn) { + die('skip Cannot log in as the auth_socket user over the Unix socket: ' . mysqli_connect_error()); +} +?> +--FILE-- +query(sprintf("CREATE USER 'gh19222'@'localhost' IDENTIFIED WITH auth_socket AS '%s'", $link->real_escape_string(get_current_user()))); + +// auth_socket ignores the password, so the server sends OK right after the handshake response +$conn = mysqli_connect('localhost', 'gh19222', 'password', '', 0, $socket); +var_dump($conn->query('SELECT CURRENT_USER()')->fetch_row()[0]); + +var_dump($conn->change_user('gh19222', 'password', null)); +var_dump($conn->query('SELECT CURRENT_USER()')->fetch_row()[0]); +?> +--CLEAN-- +query("DROP USER IF EXISTS 'gh19222'@'localhost'"); +?> +--EXPECT-- +string(17) "gh19222@localhost" +bool(true) +string(17) "gh19222@localhost" diff --git a/ext/mysqlnd/mysqlnd_auth.c b/ext/mysqlnd/mysqlnd_auth.c index 69bf2abbe065..c985ae13074e 100644 --- a/ext/mysqlnd/mysqlnd_auth.c +++ b/ext/mysqlnd/mysqlnd_auth.c @@ -30,6 +30,15 @@ static const char * const mysqlnd_old_passwd = "mysqlnd cannot connect to MySQL "store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords " "flag from your my.cnf file"; +static enum_func_status +mysqlnd_auth_handle_server_response(struct st_mysqlnd_authentication_plugin * auth_plugin, + MYSQLND_CONN_DATA * conn, + const zend_uchar * const auth_plugin_data, const size_t auth_plugin_data_len, + const char * const passwd, const size_t passwd_len, + char ** switch_to_auth_protocol, size_t * const switch_to_auth_protocol_len, + zend_uchar ** switch_to_auth_protocol_data, size_t * const switch_to_auth_protocol_data_len, + bool * const auth_done); + /* {{{ mysqlnd_run_authentication */ enum_func_status @@ -255,6 +264,7 @@ mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn, enum_func_status ret = FAIL; const MYSQLND_CHARSET * charset = NULL; MYSQLND_PACKET_AUTH_RESPONSE auth_resp_packet; + bool auth_done; DBG_ENTER("mysqlnd_auth_handshake"); @@ -313,13 +323,18 @@ mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn, PACKET_FREE(&auth_packet); } - if (auth_plugin && auth_plugin->methods.handle_server_response) { - if (FAIL == auth_plugin->methods.handle_server_response(auth_plugin, conn, - orig_auth_plugin_data, orig_auth_plugin_data_len, passwd, passwd_len, - switch_to_auth_protocol, switch_to_auth_protocol_len, - switch_to_auth_protocol_data, switch_to_auth_protocol_data_len)) { - goto end; - } + if (FAIL == mysqlnd_auth_handle_server_response(auth_plugin, conn, + orig_auth_plugin_data, orig_auth_plugin_data_len, passwd, passwd_len, + switch_to_auth_protocol, switch_to_auth_protocol_len, + switch_to_auth_protocol_data, switch_to_auth_protocol_data_len, &auth_done)) { + goto end; + } + + if (auth_done) { + /* The plugin already read the OK packet, a message from it is not available */ + mysqlnd_set_string(&conn->last_message, NULL, 0); + ret = PASS; + goto end; } if (FAIL == PACKET_READ(conn, &auth_resp_packet) || auth_resp_packet.response_code >= 0xFE) { @@ -385,6 +400,7 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn, enum_func_status ret = FAIL; const MYSQLND_CHARSET * old_cs = conn->charset; MYSQLND_PACKET_CHG_USER_RESPONSE chg_user_resp; + bool auth_done; DBG_ENTER("mysqlnd_auth_change_user"); @@ -439,17 +455,20 @@ mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn, PACKET_FREE(&auth_packet); } - if (auth_plugin && auth_plugin->methods.handle_server_response) { - if (FAIL == auth_plugin->methods.handle_server_response(auth_plugin, conn, - orig_auth_plugin_data, orig_auth_plugin_data_len, passwd, passwd_len, - switch_to_auth_protocol, switch_to_auth_protocol_len, - switch_to_auth_protocol_data, switch_to_auth_protocol_data_len)) { - goto end; - } + if (FAIL == mysqlnd_auth_handle_server_response(auth_plugin, conn, + orig_auth_plugin_data, orig_auth_plugin_data_len, passwd, passwd_len, + switch_to_auth_protocol, switch_to_auth_protocol_len, + switch_to_auth_protocol_data, switch_to_auth_protocol_data_len, &auth_done)) { + goto end; } - ret = PACKET_READ(conn, &chg_user_resp); - COPY_CLIENT_ERROR(conn->error_info, chg_user_resp.error_info); + if (auth_done) { + /* The plugin already read the OK packet */ + ret = PASS; + } else { + ret = PACKET_READ(conn, &chg_user_resp); + COPY_CLIENT_ERROR(conn->error_info, chg_user_resp.error_info); + } if (0xFE == chg_user_resp.response_code) { ret = FAIL; @@ -1221,20 +1240,22 @@ static int is_secure_transport(MYSQLND_CONN_DATA *conn) { return strcmp(conn->vio->data->stream->ops->label, "unix_socket") == 0; } -/* {{{ mysqlnd_caching_sha2_handle_server_response */ +/* {{{ mysqlnd_caching_sha2_read_server_response */ static enum_func_status -mysqlnd_caching_sha2_handle_server_response(struct st_mysqlnd_authentication_plugin *self, - MYSQLND_CONN_DATA * conn, +mysqlnd_caching_sha2_read_server_response(MYSQLND_CONN_DATA * conn, const zend_uchar * auth_plugin_data, const size_t auth_plugin_data_len, const char * const passwd, const size_t passwd_len, char **new_auth_protocol, size_t *new_auth_protocol_len, - zend_uchar **new_auth_protocol_data, size_t *new_auth_protocol_data_len + zend_uchar **new_auth_protocol_data, size_t *new_auth_protocol_data_len, + bool * const auth_done ) { - DBG_ENTER("mysqlnd_caching_sha2_handle_server_response"); + DBG_ENTER("mysqlnd_caching_sha2_read_server_response"); MYSQLND_PACKET_CACHED_SHA2_RESULT result_packet; + *auth_done = false; + if (passwd_len == 0) { DBG_INF("empty password fast path"); DBG_RETURN(PASS); @@ -1245,6 +1266,12 @@ mysqlnd_caching_sha2_handle_server_response(struct st_mysqlnd_authentication_plu DBG_RETURN(PASS); } + if (result_packet.is_ok_packet) { + /* The server sent the final OK packet, the caller must not wait for another one */ + *auth_done = true; + DBG_RETURN(PASS); + } + switch (result_packet.response_code) { case 0xFF: if (result_packet.sqlstate[0]) { @@ -1284,6 +1311,7 @@ mysqlnd_caching_sha2_handle_server_response(struct st_mysqlnd_authentication_plu mnd_sprintf(&msg, 0, "Unexpected server response while doing caching_sha2 auth: %i", result_packet.response_code); SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, msg); mnd_sprintf_free(msg); + DBG_RETURN(FAIL); } } @@ -1291,6 +1319,25 @@ mysqlnd_caching_sha2_handle_server_response(struct st_mysqlnd_authentication_plu } /* }}} */ + +/* {{{ mysqlnd_caching_sha2_handle_server_response */ +static enum_func_status +mysqlnd_caching_sha2_handle_server_response(struct st_mysqlnd_authentication_plugin *self, + MYSQLND_CONN_DATA * conn, + const zend_uchar * auth_plugin_data, const size_t auth_plugin_data_len, + const char * const passwd, + const size_t passwd_len, + char **new_auth_protocol, size_t *new_auth_protocol_len, + zend_uchar **new_auth_protocol_data, size_t *new_auth_protocol_data_len + ) +{ + bool auth_done; + + return mysqlnd_caching_sha2_read_server_response(conn, auth_plugin_data, auth_plugin_data_len, passwd, passwd_len, + new_auth_protocol, new_auth_protocol_len, new_auth_protocol_data, new_auth_protocol_data_len, &auth_done); +} +/* }}} */ + static struct st_mysqlnd_authentication_plugin mysqlnd_caching_sha2_auth_plugin = { { @@ -1316,6 +1363,37 @@ static struct st_mysqlnd_authentication_plugin mysqlnd_caching_sha2_auth_plugin #endif +/* {{{ mysqlnd_auth_handle_server_response */ +static enum_func_status +mysqlnd_auth_handle_server_response(struct st_mysqlnd_authentication_plugin * auth_plugin, + MYSQLND_CONN_DATA * conn, + const zend_uchar * const auth_plugin_data, const size_t auth_plugin_data_len, + const char * const passwd, const size_t passwd_len, + char ** switch_to_auth_protocol, size_t * const switch_to_auth_protocol_len, + zend_uchar ** switch_to_auth_protocol_data, size_t * const switch_to_auth_protocol_data_len, + bool * const auth_done) +{ + *auth_done = false; + + if (!auth_plugin || !auth_plugin->methods.handle_server_response) { + return PASS; + } +#ifdef MYSQLND_HAVE_SSL + /* The plugin method cannot report that it read the final OK packet, so call the built-in plugin directly */ + if (auth_plugin->methods.handle_server_response == mysqlnd_caching_sha2_handle_server_response) { + return mysqlnd_caching_sha2_read_server_response(conn, auth_plugin_data, auth_plugin_data_len, passwd, passwd_len, + switch_to_auth_protocol, switch_to_auth_protocol_len, + switch_to_auth_protocol_data, switch_to_auth_protocol_data_len, auth_done); + } +#endif + return auth_plugin->methods.handle_server_response(auth_plugin, conn, + auth_plugin_data, auth_plugin_data_len, passwd, passwd_len, + switch_to_auth_protocol, switch_to_auth_protocol_len, + switch_to_auth_protocol_data, switch_to_auth_protocol_data_len); +} +/* }}} */ + + /* {{{ mysqlnd_register_builtin_authentication_plugins */ void mysqlnd_register_builtin_authentication_plugins(void) diff --git a/ext/mysqlnd/mysqlnd_wireprotocol.c b/ext/mysqlnd/mysqlnd_wireprotocol.c index 740d13031e88..f27829607d89 100644 --- a/ext/mysqlnd/mysqlnd_wireprotocol.c +++ b/ext/mysqlnd/mysqlnd_wireprotocol.c @@ -2230,6 +2230,11 @@ php_mysqlnd_cached_sha2_result_read(MYSQLND_CONN_DATA * conn, void * _packet) } DBG_RETURN(PASS); } + if (0x00 == packet->response_code) { + /* OK packet: the server accepted the login without fast or full auth, e.g. for an auth_socket account */ + packet->is_ok_packet = true; + DBG_RETURN(PASS); + } if (0x1 != packet->response_code) { DBG_ERR_FMT("Unexpected response code %d", packet->response_code); diff --git a/ext/mysqlnd/mysqlnd_wireprotocol.h b/ext/mysqlnd/mysqlnd_wireprotocol.h index 33c90f315a50..edc5cd575980 100644 --- a/ext/mysqlnd/mysqlnd_wireprotocol.h +++ b/ext/mysqlnd/mysqlnd_wireprotocol.h @@ -293,6 +293,8 @@ typedef struct st_mysqlnd_packet_cached_sha2_result { char error[MYSQLND_ERRMSG_SIZE+1]; char sqlstate[MYSQLND_SQLSTATE_LENGTH + 1]; unsigned int error_no; + /* The server sent the final OK packet instead of an auth status */ + bool is_ok_packet; } MYSQLND_PACKET_CACHED_SHA2_RESULT; /* The following structure implements an optional value,