JKRhb / dtls2

A DTLS library for Dart based on OpenSSL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash client side in void _incoming(Uint8List input) when calling _libCrypto.BIO_write(_rbio, buffer.cast(), input.length)

Ifilehk opened this issue · comments

Hello there!

Back again with a new crash report. By the way, the client server long tests are stable for some hours since the last fixes.

Here the new situation:

I/flutter (22358): void _startListening() {
I/flutter (22358): start -> void _connectToPeer() {
I/flutter (22358): before -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (22358): after -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (22358): start --> int _maintainOutgoing() {
I/RawSoundPlayer(22358): Create audio track w/ bufferSize: 2048, sampleRate: 24000, encoding: 2, nChannels: 1
I/flutter (22358): int _maintainOutgoing() { ---> after --> final ret = _libCrypto.BIO_read(_wbio, buffer.cast(), bufferSize); ret=129
I/flutter (22358): void _connectToPeer() { --> before --> _handleError(ret, _connectCompleter.completeError);
I/flutter (22358): start --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (22358): _handleError(int ret, void Function(Exception) errorHandler) { --> after --> final code = _libSsl.SSL_get_error(_ssl, ret); code=2
I/RawSoundPlayer(22358): sessionId: 206505, bufferCapacityInFrames: 1928, bufferSizeInFrames: 1928
I/flutter (22358): end --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (22358): void _connectToPeer() { --> after --> _handleError(ret, _connectCompleter.completeError);
I/flutter (22358): start --> Future<void> close() async { _closed=false
I/flutter (22358): Future<void> close() async { --> before --> _libSsl.SSL_shutdown(_ssl);
I/flutter (22358): Future<void> close() async { --> after --> _libSsl.SSL_shutdown(_ssl);
I/flutter (22358): start --> void _maintainState() {
I/flutter (22358): _maintainState _connectToPeer()
I/flutter (22358): start -> void _connectToPeer() {
I/flutter (22358): before -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (22358): after -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (22358): start --> int _maintainOutgoing() {
I/flutter (22358): int _maintainOutgoing() { ---> after --> final ret = _libCrypto.BIO_read(_wbio, buffer.cast(), bufferSize); ret=-1
I/flutter (22358): void _connectToPeer() { --> before --> _handleError(ret, _connectCompleter.completeError);
I/flutter (22358): start --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (22358): _handleError(int ret, void Function(Exception) errorHandler) { --> after --> final code = _libSsl.SSL_get_error(_ssl, ret); code=2
I/flutter (22358): end --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (22358): void _connectToPeer() { --> after --> _handleError(ret, _connectCompleter.completeError);
I/flutter (22358): start --> Future<void> close() async { _closed=true
I/flutter (22358): end --> _maintainState _connectToPeer()
I/flutter (22358): start -> void _freeResources() {
I/flutter (22358): before -> _libSsl.SSL_free(_ssl);
I/flutter (22358): after -> _libSsl.SSL_free(_ssl);
I/flutter (22358): start --> void _incoming(Uint8List input) {
I/flutter (22358): void _incoming(Uint8List input) { ---> before --> _libCrypto.BIO_write(_rbio, buffer.cast(), input.length);
F/libc    (22358): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x3005911e in tid 30232 (1.ui), pid 22358

The exception occurs when calling _libCrypto.BIO_write(_rbio, buffer.cast(), input.length); in void _incoming(Uint8List input)

Obviously _ssl has been freed before. I don't know at the moment why the handshake is not successful. Just seeing that the returned code from _libSsl.SSL_get_error(_ssl, ret) returned code is 2 (SSL3_AL_FATAL). But event with this handshake error the crash should not happen.

Any clue ?

Reversed to 0.13.2 and it is working there. Your refactoring to 0.13.3 has introduced a bug somewhere.

Thank you for the report! (And sorry for the newly introduced bug :/) I will to fix it as soon as possible – the error occurs on the client side, right?

Ah, I know why: It is because of the call to _freeResources where the ssl object is being freed. Apparently, that should not actually happen at that point of time since the connection is still in the process of being shut down.

If I see it correctly, the peer sends a fatal alert (AL_FATAL), the client tries to shutdown the connection (and send a close notification to the peer) but due to the bug the ssl object is already freed, which was an oversight. Sorry for that, but atleast the problem should be easy to fix :) I am currently on my home and will open a PR with a potential fix once I'm there.

Thanks again for testing and reporting errors and bugs!

I think I now found the actual cause of the problem. Could you have a look at #64 if that resolves the issue?

Guten Morgen !
Thank you for your fast response.
Unfortunately your fix #64 is not doing the trick.

Here the new trace:

I/flutter (15946): void _startListening() {
I/flutter (15946): start -> void _connectToPeer() {
I/flutter (15946): before -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (15946): after -> final ret = _libSsl.SSL_connect(_ssl);
I/flutter (15946): start --> int _maintainOutgoing() {
I/flutter (15946): int _maintainOutgoing() { ---> after --> final ret = _libCrypto.BIO_read(_wbio, buffer.cast(), bufferSize); ret=129
I/flutter (15946): void _connectToPeer() { --> before --> _handleError(ret, _connectCompleter.completeError);
I/flutter (15946): start --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (15946): _handleError(int ret, void Function(Exception) errorHandler) { --> after --> final code = _libSsl.SSL_get_error(_ssl, ret); code=2
I/flutter (15946): end --> void _handleError(int ret, void Function(Exception) errorHandler) {
I/flutter (15946): void _connectToPeer() { --> after --> _handleError(ret, _connectCompleter.completeError);
I/flutter (15946): start --> Future<void> close() async { _closed=false
I/flutter (15946): start -> void _freeResources() {
I/flutter (15946): before -> _libSsl.SSL_free(_ssl);
I/flutter (15946): after -> _libSsl.SSL_free(_ssl);
I/flutter (15946): RS:<--- initialize
I/flutter (15946): start -> void _freeResources() {
I/flutter (15946): before -> _libSsl.SSL_free(_ssl);
F/libc    (15946): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x40200000 in tid 24076 (1.ui), pid 15946

In my opinion would make sense de free the resources before calling again _maintainState that will restart a fresh connection. Like this ?

    if (_connected) {
      _connected = false;
      print('Future<void> close() async { --> before --> _libSsl.SSL_shutdown(_ssl);');
      _libSsl.SSL_shutdown(_ssl);
      print('Future<void> close() async { --> after --> _libSsl.SSL_shutdown(_ssl);');
      await _received.close();
      _freeResources();
      _maintainState();
    }

Hallo! ;)

Thank you for the additional information, I updated #64 now with additional bug fixes, I am not sure if that already addresses your bug, though. Could you maybe test it once more?

In my opinion would make sense de free the resources before calling again _maintainState that will restart a fresh connection.

Hmm, I think that would imply an additional behavior/feature, where the client would try to reconnect automatically, wouldn't it? We can certainly add that, but I would only enable this behavior if, for example, a reconnect parameter is set to true, otherwise I think you would end up in a reconnect loop.

Oh yes true regarding the reconnection story.

Anyways the problem is solved here if I believe my last test