Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions ext/mysqli/tests/gh19222.phpt
Original file line number Diff line number Diff line change
@@ -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--
<?php
require_once 'skipifconnectfailure.inc';
if (!extension_loaded('openssl')) {
die('skip mysqlnd is built without SSL support, so the caching_sha2_password plugin is not available');
}
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
if (str_contains($link->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--
<?php
require_once 'connect.inc';

$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
$link->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--
<?php
require_once 'connect.inc';
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
$link->query("DROP USER IF EXISTS 'gh19222'@'localhost'");
?>
--EXPECT--
string(17) "gh19222@localhost"
bool(true)
string(17) "gh19222@localhost"
120 changes: 99 additions & 21 deletions ext/mysqlnd/mysqlnd_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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]) {
Expand Down Expand Up @@ -1284,13 +1311,33 @@ 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);
}
}

DBG_RETURN(PASS);
}
/* }}} */


/* {{{ 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 =
{
{
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions ext/mysqlnd/mysqlnd_wireprotocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ext/mysqlnd/mysqlnd_wireprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading