[MDEV-24998] Clients linked with libmysqlclient misinterpret connection errors from MariaDB server Created: 2021-02-26  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Protocol
Affects Version/s: 10.2, 10.3, 10.4, 10.5, 10.6
Fix Version/s: 10.4, 10.5

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Basic example (to be run with debug server, as it uses a debug injection):

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>
 
int main(int argc, char **argv)
{  
  MYSQL *con = mysql_init(NULL);
  MYSQL *con2 = mysql_init(NULL);
  mysql_real_connect(con, "127.0.0.1", "root", "", NULL, 0, NULL, 0);
  mysql_query(con, "set global debug_dbug= '+d,vio_peer_addr_fake_ipv4,getnameinfo_error_again'");
 
  if (mysql_real_connect(con2, "127.0.0.1", "root", "", 
          NULL, 0, NULL, 0) == NULL) 
      fprintf(stderr, "%i: %s\n", mysql_errno(con2), mysql_error(con2));
  
  mysql_query(con, "set global debug_dbug= ''");
  mysql_close(con);
  mysql_close(con2);
  exit(0);
}

If it's linked with libmariadb, it gets and shows ER_HOST_NOT_PRIVILEGED, as expected:

connects to 10.2 5ecaf52d

$ ldd test-libmariadb | grep -E 'libmariadb|libmysql'
	libmariadb.so.3 => /lib/x86_64-linux-gnu/libmariadb.so.3 (0x00007fe29d3a5000)
$ ./test-libmariadb
1130: Host '192.0.2.4' is not allowed to connect to this MariaDB server

But if it's linked with libmysqlclient (18, 20, 21), it shows the generic 2013 with slightly varying "system errors":

connects to 10.2 5ecaf52d

$ ldd test-libmysqlclient21 | grep -E 'libmariadb|libmysql'
	libmysqlclient.so.21 => /lib/x86_64-linux-gnu/libmysqlclient.so.21 (0x00007f46671af000)
$ ./test-libmysqlclient21
2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 2

connects to 10.2 5ecaf52d

$ LD_LIBRARY_PATH=/data/bld/mysql-5.7/lib ldd ./test-libmysqlclient20 | grep -E 'libmariadb|libmysql'
	libmysqlclient.so.20 => /data/bld/mysql-5.7/lib/libmysqlclient.so.20 (0x00007f98d5036000)
$ LD_LIBRARY_PATH=/data/bld/mysql-5.7/lib ./test-libmysqlclient20
2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 0

connects to 10.2 5ecaf52d

$ LD_LIBRARY_PATH=/data/bld/10.1/lib ldd ./test-libmysqlclient18 | grep -E 'libmariadb|libmysql'
	libmysqlclient.so.18 => /data/bld/10.1/lib/libmysqlclient.so.18 (0x00007fa35d039000)
$ LD_LIBRARY_PATH=/data/bld/10.1/lib ./test-libmysqlclient18
2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 2 "No such file or directory"

Reproducible with 10.2-10.6 servers.
While connecting to 10.1 server, all of them (either linked with libmysqlclient or libmariadb) return the expected error ER_HOST_NOT_PRIVILEGED.

As a practical example, libdbd-mysql-perl linked with libmysqlclient returns the wrong error. For example, Ubuntu 20.4 Focal (latest LTS) offers such libdbd-mysql-perl:

Ubuntu Focal

$ apt-cache depends libdbd-mysql-perl
libdbd-mysql-perl
  Depends: perl
  Depends: <perl-dbdabi-94>
    libdbi-perl
  Depends: <perlapi-5.30.0>
    perl-base
  Depends: libc6
  Depends: libmysqlclient21
  Depends: libdbi-perl

And that's how it works:

MTR test using perl DBD

call mtr.add_suppression("Temporary failure in name resolution");
 
set global debug_dbug= "+d,vio_peer_addr_fake_ipv4,getnameinfo_error_again";
 
--echo
--echo # "Native" MTR connection:
--echo
--error ER_HOST_NOT_PRIVILEGED
--connect (con1,127.0.0.1,root,,$MASTER_MYPORT)
--echo
--echo # Perl connection:
--echo
--perl
use DBI;
$dbh= DBI->connect("dbi:mysql:user=root:host=127.0.0.1:port=$ENV{MASTER_MYPORT}");
if ($dbh) {
  print "Connected\n";
  $dbh->disconnect();
} else {
  print DBI->err, ": ", DBI->errstr, "\n";
}
EOF
--echo
 
set global debug_dbug= "";

10.2 5ecaf52d

# "Native" MTR connection:
 
connect(127.0.0.1,root,,16000,16000,/data/bld/10.2-debug/mysql-test/var/tmp/mysqld.1.sock);
connect  con1,127.0.0.1,root,,$MASTER_MYPORT;
ERROR HY000: Host '192.0.2.4' is not allowed to connect to this MariaDB server
 
# Perl connection:
 
2013: Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Debian Sid (unstable) offers libdbd-mysql-perl linked with libmariadb:

Debian Sid

$ apt-cache depends libdbd-mysql-perl
libdbd-mysql-perl
  Depends: perl
  Depends: <perl-dbdabi-94>
    libdbi-perl
  Depends: <perlapi-5.32.0>
    perl-base
  Depends: libc6
  Depends: libmariadb3
  Depends: libdbi-perl

And there the error is correct (same MTR test on 10.2):

# "Native" MTR connection:
 
connect(127.0.0.1,root,,16000,16000,/home/buildbot/10.2/mysql-test/var/tmp/mysqld.1.sock);
connect  con1,127.0.0.1,root,,$MASTER_MYPORT;
ERROR HY000: Host '192.0.2.4' is not allowed to connect to this MariaDB server
 
# Perl connection:
 
1130: Host '192.0.2.4' is not allowed to connect to this MariaDB server


Generated at Thu Feb 08 09:34:19 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.