Skip to content
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

SSL problems after locally scoped disconnect #431

Closed
rlauer6 opened this issue Jun 13, 2024 · 8 comments · Fixed by #432
Closed

SSL problems after locally scoped disconnect #431

rlauer6 opened this issue Jun 13, 2024 · 8 comments · Fixed by #432
Assignees
Labels

Comments

@rlauer6
Copy link

rlauer6 commented Jun 13, 2024

See the note at the end of this report first...

DBD::mysql version

5.003, 5.004, 5.005

MySQL client version

8.0.37

Server version

8.4.0

Operating system version

Linux (amazonlinux:2), libssl.so.1.0.2k

What happened?

Creating a connection using DBD::mysql versions 5.003 - 5.005 in a locally scoped block and then disconnecting in the same locally scope block puts SSL in a state where downstream SSL connections fail with a 500 (internal response).

The script below will demonstrate the problem:

use strict;
use warnings;

use LWP::UserAgent;
use Net::SSLeay;
use HTTP::Request;
use Data::Dumper;
use DBI;

$Net::SSLeay::trace = 2;

my $dsn = sprintf 'dbi:mysql:%s:%s', $ENV{DBI_DBNAME}, $ENV{DBI_HOST};

my $dbi = DBI->connect( $dsn, $ENV{DBI_USER}, $ENV{DBI_PASS} );

my $req = HTTP::Request->new( GET => 'https://google.com' );

my $ua = LWP::UserAgent->new;

my $rsp = $ua->request($req);

print {*STDERR} Dumper( [ status => $rsp->status_line ] );

connect_and_do_something($dsn);

$rsp = $ua->request($req);

print {*STDERR} Dumper( [ status => $rsp->status_line ] );

########################################################################
sub connect_and_do_something {
########################################################################
    my ($dsn) = shift;

    my $dbi = DBI->connect( $dsn, $ENV{DBI_USER}, $ENV{DBI_PASS} );
    $dbi->disconnect;
}

1;

The problem persists regardless of the 8.x MySQL client library I use to build DBD::mysql (I've tried several), so I believe the problem is related to what has been changed from version 5.002 to version 5.003. The problem does not manifest itself in 5.002 or when using version 4.050.

The output of the program above is attached:

bug-report.txt

Other information

In version 5.003 mysql_library_end() was added. Removing this line "fixes" the issue, although there may be other side effects of removing this line (memory leaks?). At the very least this sub is probably being called prematurely as it appears to remove plugins (like openssl plugin that apparently resets the TLS context):

static void deinit(mysql_harness::PluginFuncEnv *) {
  // let the TlsLibraryContext destructor do the SSL cleanup
  tls_library_context.reset();
}

Incidentally, it appears that dbd_db_destroy() is being invoked when a database handle goes out scope in either a closure or a sub. This implies you probably should not be calling mysql_library_end() in that subroutine?

dbd_discon_all might be the appropriate place to call mysql_library_end() but it appears to have been disabled back in 2003. You should also know that mysql_server_end() is called in dbd_discon_all() already. (mysql_server_end is an alias for mysql_library_end()).

@dveeden
Copy link
Collaborator

dveeden commented Jun 13, 2024

Are both Perl and MySQL using libssl.so.1.0.2k ? Or is MySQL using a bundled OpenSSL?

@dveeden
Copy link
Collaborator

dveeden commented Jun 13, 2024

I assume this also fails with v5.006 or that you didn't test this version?

dveeden added a commit to dveeden/DBD-mysql that referenced this issue Jun 13, 2024
- Stop calling `mysql_library_end()` in `dbd_db_destroy()` as this leads
  to issues with OpenSSL in specific circumstances.
- Call `myql_library_end()` instead of `mysql_server_end()` as the
  latter has been deprecated.

See also:
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-server-end.html
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-library-end.html

Closes perl5-dbi#431
dveeden added a commit to dveeden/DBD-mysql that referenced this issue Jun 13, 2024
- Stop calling `mysql_library_end()` in `dbd_db_destroy()` as this leads
  to issues with OpenSSL in specific circumstances.
- Call `myql_library_end()` instead of `mysql_server_end()` as the
  latter has been deprecated.

See also:
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-server-end.html
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-library-end.html

Closes perl5-dbi#431
@dveeden
Copy link
Collaborator

dveeden commented Jun 13, 2024

@rlauer6 could you review #432 ?

@dveeden
Copy link
Collaborator

dveeden commented Jun 13, 2024

I also filed this: https://bugs.mysql.com/bug.php?id=115326

@rlauer6
Copy link
Author

rlauer6 commented Jun 13, 2024

I assume this also fails with v5.006 or that you didn't test this version?

yes

@rlauer6
Copy link
Author

rlauer6 commented Jun 13, 2024

Are both Perl and MySQL using libssl.so.1.0.2k ? Or is MySQL using a bundled OpenSSL?

I'm sorry I don't quite understand the question. If you are asking if the MySQL library is using the same SSL library I can't answer. I would assume so - but I don't think this is germane to the problem. I think the problem is essentially that DBD::mysql is calling mysql_library_end() prematurely when closures and subs cause the dbd_db_destroy() to be called.

dveeden added a commit to dveeden/DBD-mysql that referenced this issue Jun 18, 2024
- Stop calling `mysql_library_end()` in `dbd_db_destroy()` as this leads
  to issues with OpenSSL in specific circumstances.
- Call `myql_library_end()` instead of `mysql_server_end()` as the
  latter has been deprecated.
- Add missing define for `dbd_discon_all`

See also:
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-server-end.html
- https://dev.mysql.com/doc/c-api/8.4/en/mysql-library-end.html

Closes perl5-dbi#431
@rlauer6
Copy link
Author

rlauer6 commented Jun 24, 2024

Will you be releasing this version to CPAN soon?

@dveeden
Copy link
Collaborator

dveeden commented Jun 24, 2024

Will you be releasing this version to CPAN soon?

This is scheduled for next week https://github.com/perl5-dbi/DBD-mysql/milestone/11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants