You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Out of date/incorrect advice in SSL_get_error docs as of 1.1.0c #1903
This commit changed SSL_read to return -1 rather than 0 when BIO_read returns 0: 4880672, which landed in the 1.1.0c release.
When SSL_get_error is called, it'll return SSL_ERROR_SYSCALL, and the error stack will be empty. The man page for SSL_get_error says that at that point you should check SSL_read's return code; a -1 means that there was a read error and to consult the BIO-specific error (e.g. errno), and 0 means that there was an EOF in violation of the protocol, but that doesn't appear to be correct anymore.
Is this a bug in the implementation or the docs? If the new implementation is correct, I guess the new procedure should be to check the BIO error, and check if an error actually occurred to determine if it's an EOF?
changed the title [-]Possibly out of date/incorrect advice in SSL_get_error docs[/-][+]Out of date/incorrect advice in SSL_get_error docs as of 1.1.0c[/+]on Nov 11, 2016
In my opinion the new behavior is incorrect. This new behavior results in SSL_read returning -1 with a syscall error if the peer just closes the connection without proper shutdown. But for this case SSL_read is explicitly documented as returning 0, i.e. the commit breaks a documented API.
There are no guarantees that a 0 return from BIO_read() means EOF, e.g. by default reading from an empty mem bio gives you a return of -1 from BIO_read(), but calling BIO_eof() will return true. Some BIOs may work that way, but others may not.
Further, SSL_read() is not documented to return 0 on EOF:
=item Z<>0
The read operation was not successful. The reason may either be a clean
shutdown due to a "close notify" alert sent by the peer (in which case
the SSL_RECEIVED_SHUTDOWN flag in the ssl shutdown state is set
(see L<SSL_shutdown(3)>,
L<SSL_set_shutdown(3)>). It is also possible, that
the peer simply shut down the underlying transport and the shutdown is
incomplete. Call SSL_get_error() with the return value B<ret> to find out,
whether an error occurred or the connection was shut down cleanly
(SSL_ERROR_ZERO_RETURN).
=item E<lt>0
The read operation was not successful, because either an error occurred
or action must be taken by the calling process. Call SSL_get_error() with the
return value B<ret> to find out the reason.
The documentation for SSL_get_error() does says this:
=item SSL_ERROR_SYSCALL
Some I/O error occurred. The OpenSSL error queue may contain more
information on the error. If the error queue is empty
(i.e. ERR_get_error() returns 0), B<ret> can be used to find out more
about the error: If B<ret == 0>, an EOF was observed that violates
the protocol. If B<ret == -1>, the underlying B<BIO> reported an
I/O error (for socket I/O on Unix systems, consult B<errno> for details).
However, that is quite clearly wrong. Since libssl does not know the reason behind the return code from BIO_read() it cannot be said that a ret value of 0 from SSL_read() means EOF (and it is inconsistent with the documentation for SSL_read() in any case). This should probably be fixed in the documentation.
So can you please clarify, how to detect the EOF? I would expect the API to allow for that and not to require going down to the underlying transport to find out.
My reading of the documentation you cite is the following: Return value is 0 either on a clean shutdown or if the peer simply closed the connection, i.e incomplete shutdown. Return value is -1 either if some (unspecified error) occurs or if an action needs to be taken by the caller.
Based on this reading an EOF from the client instead of a clean shutdown perfectly fits the first case, i.e. return value of 0. And this is exactly what OpenSSL did for years. While it might be that of how OpenSSL internally works it never was true that 0 returned only in the documented cases it at least behaved this way in the documented cases. And applications relied on this documented behavior since it always worked as documented.
If the API stays at it is now it will cause unexpected errors in existing applications (it does already, i.e. breaks several tests in IO::Socket::SSL Perl module). And once the source of the error is figured out the applications or libraries will need to changed in that they need to do some new error handling to figure out if the problem happened due to EOF or something else. And of course backwards compatibility with the old behavior must be maintained somehow in many cases in case the application or library is compiled against an older version of OpenSSL.
In my opinion the API should not be simply changed like it is done currently, i.e. suddenly, silently, without updating the API documentation and even inside the stable branch where no such incompatible API changes would be expected. My expectation would be instead that OpenSSL does exactly the same what it now expects from all applications, i.e. figure out if this was EOF or not. This way the necessary change would only be needed in one place (OpenSSL) and the documented API would be kept stable.
So can you please clarify, how to detect the EOF? I would expect the API to allow for that and not to require going down to the underlying transport to find out.
There is no way without asking the underlying BIO (and there never has been no matter what the documentation may have implied), i.e. call BIO_eof().
Based on this reading an EOF from the client instead of a clean shutdown perfectly fits the first case, i.e. return value of 0. And this is exactly what OpenSSL did for years. While it might be that of how OpenSSL internally works it never was true that 0 returned only in the documented cases it at least behaved this way in the documented cases. And applications relied on this documented behavior since it always worked as documented.
The simple fact is that the documentation is inconsistent with itself. The SSL_read() documentation says nothing about EOF and makes no guarantees about it. SSL_get_error() implies something different, but that doesn't match the implementation. It might have been the case that in certain circumstances the behaviour happened to behave in a certain way, and that applications then started to rely on that behaviour. But it is equally true that in other situations it did not behave that way, and did not behave like the documented API. Either way something is broken. There is no simple way out of that...we're doomed whatever we do because someone won't be happy with it.
This change at least brings some consistency to the situation (although the SSL_get_error() page should be brought in line too). There is an argument to say we shouldn't have this "fix" backported to 1.1.0 and keep it in 1.1.1....although the two are supposed to be binary compatible with each other, so that in itself is difficult.
My expectation would be instead that OpenSSL does exactly the same what it now expects from all applications, i.e. figure out if this was EOF or not.
Hmmm, that could be possible, i.e. we could call BIO_eof() and return 0 if it is true...but I suspect even there...as I said in my mem BIO example, some things don't always behave quite as you expect. So I think we would still end up breaking applications.
The choice really comes down to keep it like it was (broken in some situations, but kind of did something sort of predictable in some situations, but that behaviour doesn't match the documentation (and the documentation can't be fixed because of the broken nature of the implementation)), OR fix it so that the behaviour is predictable in all situation and the documentation matches, BUT that might break some applications that rely on the old behaviour. ROCK meet HARD PLACE.
We might have to revert...although that leaves us in a very fragile position. We might have to just document the problems and the inconsistencies here, and provide some advice to not rely on this behaviour to try and stop more stuff from breaking in the future :-(
Yeah, I ran into the same issue with Python and MIT Kerberos.
@mattcaswell, have you considered smoke tests with open source projects? Python, PyCA/cryptography and PyCA/PyOpenSSL have extended unit and integration tests for OpenSSL's TLS and crypto libraries. The test suites are stable on major operating systems (Linux, OSX, Windows), too. Simple smoke or pre-release tests should be able to detect bugs before OpenSSL is released.
@tiran, OpenSSL 1.1.1 (i.e. current master) has the concept of external test suites. So far we've only integrated the BoringSSL suite, but it would be nice to integrate others.
@mattcaswell I can help with CPython. Maybe @alex, @reaperhulk and @hynek are interested to add external tests for PyCA/cryptography and PyCA/PyOpenSSL. Ping @frozencemetery, could you help with MIT KRB5?
For Boring it was a bit more complicated because we had to have an OpenSSL specific shim to interface between OpenSSL and the BoringSSL test runner. Probably with other external suites we wouldn't need that if they are built to run on OpenSSL in the first place.
I imagine hooking in a new external suite will be simply a case of extending 90-test_external.t to (somehow) detect whether a specific test is present, if so call some external executable/script, and then something to figure out whether the test succeeded or failed.
I'd love to see some PRs in this area. Once integrated we could then add it to our Travis CI. @ekasper has a WIP PR to do that for Boring (#1888)
It's rather simple to run Python's ssl tests. We even have a special test helper to just run OpenSSL-related tests. Python doesn't have a configure option for OpenSSL but you can use the usual compiler and linker flags.
cd Python build root
Set CFLAGS=-I${TOP}/include, LDFLAGS=-L${TOP} and LD_RUN_PATH=$TOP
remove build directory to force rebuilding of ssl and hashlib modules
I'm worried that if we suggest to look at that that people think it's a good idea to check that, while I think it's a bad idea. You're setting yourself up for a truncation attack. This is one of the things that was wrong with SSLv2 and was fixed in SSLv3, and you seem to want to reintroduce this at the application level.
The only case I can think of where this check might be useful is that you know the other side is broken, but you have another way to verify that the transfer was complete, for instance because at the application level you can signal that you're done.
@kroeckx: In this direction, the documentation of SSL_read() already contains:
Call SSL_get_error() with the return value ret to find out, whether an error occurred or the connection was shut down cleanly ( SSL_ERROR_ZERO_RETURN ).
It would in my view be well worth adding some of the information you mention above too!
Omitting information about detecting EOF should in my view not be justified by omitting other important information too.
@kroeckx, @mattcaswell : I have now tested whether using the API as suggested lets you detect the difference between a truncation attack and a clean shutdown. As far as I found out, it doesn't:
We get -1 from SSL_read() (or, depending on the OpenSSL version, 0) in both cases, and SSL_get_error() signals SSL_ERROR_SYSCALL in both cases. Further, SSL_get_error() will say 0, whether or not the connection was cleanly shut down.
As far as I can tell from these tests, neither using BIO_eof() nor the sequence suggested in this thread lets us distinguish between a truncation attack and a clean shutdown.
At least s_server seems to come in the SSL_ERROR_RETURN_ZERO case for me on a clean shutdown, and gives an error in case of an unclean shutdown. Can you show some example code?
At least s_server seems to come in the SSL_ERROR_RETURN_ZERO case for me on a clean shutdown, and gives an error in case of an unclean shutdown.
Quite. A clean shutdown is counted as one where a close_notify alert has been received from the peer. If that has happened then SSL_get_error() should return SSL_ERROR_RETURN_ZERO. This is different from SSL_get_error(), giving 0 which @triska says above. A 0 return from SSL_get_error() means SSL_ERROR_NONE (i.e. there has been no error).
As far as I can tell from these tests, neither using BIO_eof() nor the sequence suggested in this thread lets us distinguish between a truncation attack and a clean shutdown.
All BIO_eof() can tell you is whether the BIO has hit EOF. What EOF means is BIO dependant, but for a socket based BIO it would mean the connection has been closed. It tells you nothing about what happened before that closure, i.e. whether a close_notify was received or not.
I should add that you can also use SSL_get_shutdown() to explicitly test whether a connection has been cleanly shutdown or not. This actually tells you slightly different information to SSL_get_error(), i.e. it tells you whether a shutdown alert has been received (i.e. a close_notify or a fatal error alert), or whether we have sent a close_notify ourselves. A connection is only fully and cleanly closed if we have both sent and received a close_notify.
SSL_get_error() will tell you whether a close_notify has been received through SSL_ERROR_RETURN_ZERO. Receipt of a fatal alert will appear as SSL_ERROR_SSL from SSL_get_error() - although it could also mean some other kind of internal error has happened. A SSL_ERROR_SYSCALL return will tell you that some unknown error has occurred in a system call. This could be caused by an unclean shutdown.
So, in summary, if you get a 0 or -1 return from SSL_read()/SSL_write(), you should call SSL_get_error():
If you get back SSL_ERROR_RETURN_ZERO then you know the connection has been cleanly shutdown by the peer. To fully close the connection you may choose to call SSL_shutdown() to send a close_notify back.
If you get back SSL_ERROR_SSL then some kind of internal or protocol error has occurred. More details will be on the SSL error queue. You can also call SSL_get_shutdown(). If this indicates a state of SSL_RECEIVED_SHUTDOWN then you know a fatal alert has been received from the peer (if it had been a close_notify then SSL_get_error() would have returned SSL_ERROR_RETURN_ZERO). SSL_ERROR_SSL is considered fatal - you should not call SSL_shutdown() in this case.
If you get back SSL_ERROR_SYSCALL then some kind of fatal (i.e. non-retryable) error has occurred in a system call. You may be able to get more information from the SSL error queue or you might not. The fatal error could be because the underlying transport has been shutdown unexpectedly (no alert received) or just some other unknown system call error occurred. Calling BIO_eof() at this point will tell you whether the underlying transport has hit EOF (i.e. for a socket BIO the connection has been closed).
@mattcaswell: That's really an excellent overview, and I have used your explanation to make the application work with OpenSSL 1.1.0c as well as earlier versions. Thank you a lot, and I can only recommend to add the essential pieces of this guideline to the documentation of SSL_read().
Further, I have 2 specific questions about this:
Suppose that after detecting a fatal alert (i.e., getting SSL_ERROR_SSLandSSL_get_shutdown() signaling SSL_RECEIVED_SHUTDOWN), I still call SSL_shutdown(). How problematic is this? You say "you should not call SSL_shutdown() in this case", and I can see that it would not make sense, but does it warrant the extra complexity to prevent calling SSL_shutdown() regardless in this situation?
Second, when I try to negotiate a connection, and I get a return value ≤0 from SSL_accept() or SSL_connect(), and further, I get SSL_ERROR_SYSCALL from SSL_get_error(), andERR_get_error() returns 0, then the code I am maintaining assumes that EOF was unexpectedly encountered during negotiation. How safe is this assumption? Could this also be due to a different error? And conversely, could EOF also be reflected differently? In addition, what are the alternatives I have for distinguishing this case specifically (EOF during session negotiation). I suppose I could test bothwbio and rbio for EOF with BIO_eof(), but that likewise does not seem completely reliable to me, because if either of them is no longer needed, then it could have been closed without error.
Suppose that after detecting a fatal alert (i.e., getting SSL_ERROR_SSL and SSL_get_shutdown() signaling SSL_RECEIVED_SHUTDOWN), I still call SSL_shutdown(). How problematic is this? You say "you should not call SSL_shutdown() in this case", and I can see that it would not make sense, but does it warrant the extra complexity to prevent calling SSL_shutdown() regardless in this situation?
The problem here is that as soon as OpenSSL hits a fatal error (for whatever reason), it immediately returns. This might mean that the internal state of the SSL object is not left in a consistent state. If you then start to try and do IO with it then you will get undefined results. In the worst case scenario that could lead to a crash. In most cases it will probably work. In that case, if you call SSL_shutdown(), it will send a close_notify - which just doesn't make sense. Arguably, we should prevent that, so it's not inconceivable that we might change that behaviour at some point. As a general rule though, don't try and continue to do IO on an SSL object that has returned SSL_ERROR_SSL (or for that matter one which has returned SSL_ERROR_SYSCALL).
Second, when I try to negotiate a connection, and I get a return value ≤0 from SSL_accept() or SSL_connect(), and further, I get SSL_ERROR_SYSCALL from SSL_get_error(), and ERR_get_error() returns 0, then the code I am maintaining assumes that EOF was unexpectedly encountered during negotiation. How safe is this assumption? Could this also be due to a different error? And conversely, could EOF also be reflected differently? In addition, what are the alternatives I have for distinguishing this case specifically (EOF during session negotiation). I suppose I could test both wbio and rbio for EOF with BIO_eof(), but that likewise does not seem completely reliable to me, because if either of them is no longer needed, then it could have been closed without error.
If you get SSL_ERROR_SYSCALL from SSL_get_error() it tells you is that there was a fatal non-retryable error in a system call. 99.9% of the time that is going to be because the peer closed the connection down. But even if it isn't, its a fatal non-retryable error so - effectively - the connection is closed, so I think it should be ok to treat it the same way.
So just to clarify, in case of a fatal error (SSL_ERROR_SSL or SSL_ERROR_SYSCALL) you should not call SSL_shutdown(). We should probably at least document that. Possibly we should also make SSL_shutdown() and other functions check the state of the connection before they try to do something.
Could in theory also a fatal error happen in the bidirectional shutdown sequence ?
Is there a difference between a "fatal" SSL_ERROR_SYSCALL and a
non-fatal SSL_ERROR_SYSCALL from SSL_shutdown?
The manual on SSL_shutdown return value 0 says:
"The shutdown is not yet finished. Call SSL_shutdown() for a second time, if a bidirectional shutdown shall be performed. The output of SSL_get_error(3) may be misleading, as an erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred. "
Is a SSL_ERROR_SSL/SYSCALL with return value -1 more serious than with return value 0 ?
FYI: bio_ssl.c does not follow the rule to avoid SSL_shutdown if previously SSL_ERROR_SSL
or SSL_ERROR_SYSCALL were encountered, it is almost impossible to avoid the SSL_shutdown:
static int ssl_free(BIO *a)
{
BIO_SSL *bs;
if (a == NULL)
return (0);
bs = BIO_get_data(a);
if (bs->ssl != NULL)
SSL_shutdown(bs->ssl);
Activity
[-]Possibly out of date/incorrect advice in SSL_get_error docs[/-][+]Out of date/incorrect advice in SSL_get_error docs as of 1.1.0c[/+]Fix EOF detection
noxxi commentedon Nov 13, 2016
In my opinion the new behavior is incorrect. This new behavior results in SSL_read returning -1 with a syscall error if the peer just closes the connection without proper shutdown. But for this case SSL_read is explicitly documented as returning 0, i.e. the commit breaks a documented API.
kroeckx commentedon Nov 13, 2016
I'm wondering if we can find the places that return -1 (from the
BIO?) that should return 0 in SSL_read.
But this all seems like non-trivial to get right.
Kurt
mattcaswell commentedon Nov 14, 2016
There are no guarantees that a 0 return from BIO_read() means EOF, e.g. by default reading from an empty mem bio gives you a return of -1 from BIO_read(), but calling BIO_eof() will return true. Some BIOs may work that way, but others may not.
Further, SSL_read() is not documented to return 0 on EOF:
The documentation for SSL_get_error() does says this:
However, that is quite clearly wrong. Since libssl does not know the reason behind the return code from BIO_read() it cannot be said that a ret value of 0 from SSL_read() means EOF (and it is inconsistent with the documentation for SSL_read() in any case). This should probably be fixed in the documentation.
t8m commentedon Nov 14, 2016
So can you please clarify, how to detect the EOF? I would expect the API to allow for that and not to require going down to the underlying transport to find out.
noxxi commentedon Nov 14, 2016
My reading of the documentation you cite is the following: Return value is 0 either on a clean shutdown or if the peer simply closed the connection, i.e incomplete shutdown. Return value is -1 either if some (unspecified error) occurs or if an action needs to be taken by the caller.
Based on this reading an EOF from the client instead of a clean shutdown perfectly fits the first case, i.e. return value of 0. And this is exactly what OpenSSL did for years. While it might be that of how OpenSSL internally works it never was true that 0 returned only in the documented cases it at least behaved this way in the documented cases. And applications relied on this documented behavior since it always worked as documented.
If the API stays at it is now it will cause unexpected errors in existing applications (it does already, i.e. breaks several tests in IO::Socket::SSL Perl module). And once the source of the error is figured out the applications or libraries will need to changed in that they need to do some new error handling to figure out if the problem happened due to EOF or something else. And of course backwards compatibility with the old behavior must be maintained somehow in many cases in case the application or library is compiled against an older version of OpenSSL.
In my opinion the API should not be simply changed like it is done currently, i.e. suddenly, silently, without updating the API documentation and even inside the stable branch where no such incompatible API changes would be expected. My expectation would be instead that OpenSSL does exactly the same what it now expects from all applications, i.e. figure out if this was EOF or not. This way the necessary change would only be needed in one place (OpenSSL) and the documented API would be kept stable.
mattcaswell commentedon Nov 14, 2016
There is no way without asking the underlying BIO (and there never has been no matter what the documentation may have implied), i.e. call BIO_eof().
mattcaswell commentedon Nov 14, 2016
The simple fact is that the documentation is inconsistent with itself. The SSL_read() documentation says nothing about EOF and makes no guarantees about it. SSL_get_error() implies something different, but that doesn't match the implementation. It might have been the case that in certain circumstances the behaviour happened to behave in a certain way, and that applications then started to rely on that behaviour. But it is equally true that in other situations it did not behave that way, and did not behave like the documented API. Either way something is broken. There is no simple way out of that...we're doomed whatever we do because someone won't be happy with it.
This change at least brings some consistency to the situation (although the SSL_get_error() page should be brought in line too). There is an argument to say we shouldn't have this "fix" backported to 1.1.0 and keep it in 1.1.1....although the two are supposed to be binary compatible with each other, so that in itself is difficult.
Hmmm, that could be possible, i.e. we could call BIO_eof() and return 0 if it is true...but I suspect even there...as I said in my mem BIO example, some things don't always behave quite as you expect. So I think we would still end up breaking applications.
The choice really comes down to keep it like it was (broken in some situations, but kind of did something sort of predictable in some situations, but that behaviour doesn't match the documentation (and the documentation can't be fixed because of the broken nature of the implementation)), OR fix it so that the behaviour is predictable in all situation and the documentation matches, BUT that might break some applications that rely on the old behaviour. ROCK meet HARD PLACE.
mattcaswell commentedon Nov 14, 2016
Hmmm...more stuff broke. See #1919
We might have to revert...although that leaves us in a very fragile position. We might have to just document the problems and the inconsistencies here, and provide some advice to not rely on this behaviour to try and stop more stuff from breaking in the future :-(
tiran commentedon Nov 14, 2016
Yeah, I ran into the same issue with Python and MIT Kerberos.
@mattcaswell, have you considered smoke tests with open source projects? Python, PyCA/cryptography and PyCA/PyOpenSSL have extended unit and integration tests for OpenSSL's TLS and crypto libraries. The test suites are stable on major operating systems (Linux, OSX, Windows), too. Simple smoke or pre-release tests should be able to detect bugs before OpenSSL is released.
mattcaswell commentedon Nov 14, 2016
@tiran, OpenSSL 1.1.1 (i.e. current master) has the concept of external test suites. So far we've only integrated the BoringSSL suite, but it would be nice to integrate others.
tiran commentedon Nov 14, 2016
@mattcaswell I can help with CPython. Maybe @alex, @reaperhulk and @hynek are interested to add external tests for PyCA/cryptography and PyCA/PyOpenSSL. Ping @frozencemetery, could you help with MIT KRB5?
mattcaswell commentedon Nov 14, 2016
Take a look here for where we hooked in the Boring suite:
https://github.com/openssl/openssl/blob/master/test/recipes/90-test_external.t
For Boring it was a bit more complicated because we had to have an OpenSSL specific shim to interface between OpenSSL and the BoringSSL test runner. Probably with other external suites we wouldn't need that if they are built to run on OpenSSL in the first place.
See the following for instructions if you want to try out the BoringSSL runner:
https://github.com/openssl/openssl/blob/master/test/README.external
I imagine hooking in a new external suite will be simply a case of extending 90-test_external.t to (somehow) detect whether a specific test is present, if so call some external executable/script, and then something to figure out whether the test succeeded or failed.
I'd love to see some PRs in this area. Once integrated we could then add it to our Travis CI. @ekasper has a WIP PR to do that for Boring (#1888)
tiran commentedon Nov 14, 2016
It's Perl. I never learned to write Perl. :)
It's rather simple to run Python's ssl tests. We even have a special test helper to just run OpenSSL-related tests. Python doesn't have a configure option for OpenSSL but you can use the usual compiler and linker flags.
CFLAGS=-I${TOP}/include,LDFLAGS=-L${TOP}andLD_RUN_PATH=$TOPStep 3 and 4 can be optimized if needed, https://github.com/tiran/multissl/blob/master/multissl.py#L211
19 remaining items
PORT: EOF when writing is now correctly handled also in OpenSSL 1.1.0c.
kroeckx commentedon Dec 1, 2016
I'm worried that if we suggest to look at that that people think it's a good idea to check that, while I think it's a bad idea. You're setting yourself up for a truncation attack. This is one of the things that was wrong with SSLv2 and was fixed in SSLv3, and you seem to want to reintroduce this at the application level.
The only case I can think of where this check might be useful is that you know the other side is broken, but you have another way to verify that the transfer was complete, for instance because at the application level you can signal that you're done.
triska commentedon Dec 1, 2016
@kroeckx: In this direction, the documentation of
SSL_read()already contains:It would in my view be well worth adding some of the information you mention above too!
Omitting information about detecting
EOFshould in my view not be justified by omitting other important information too.triska commentedon Dec 2, 2016
@kroeckx, @mattcaswell : I have now tested whether using the API as suggested lets you detect the difference between a truncation attack and a clean shutdown. As far as I found out, it doesn't:
We get
-1fromSSL_read()(or, depending on the OpenSSL version,0) in both cases, andSSL_get_error()signalsSSL_ERROR_SYSCALLin both cases. Further,SSL_get_error()will say0, whether or not the connection was cleanly shut down.As far as I can tell from these tests, neither using
BIO_eof()nor the sequence suggested in this thread lets us distinguish between a truncation attack and a clean shutdown.kroeckx commentedon Dec 2, 2016
At least s_server seems to come in the SSL_ERROR_RETURN_ZERO case for me on a clean shutdown, and gives an error in case of an unclean shutdown. Can you show some example code?
mattcaswell commentedon Dec 2, 2016
@kroeckx said:
Quite. A clean shutdown is counted as one where a close_notify alert has been received from the peer. If that has happened then
SSL_get_error()should return SSL_ERROR_RETURN_ZERO. This is different fromSSL_get_error(), giving 0 which @triska says above. A 0 return fromSSL_get_error()means SSL_ERROR_NONE (i.e. there has been no error).@triska said:
All
BIO_eof()can tell you is whether the BIO has hit EOF. What EOF means is BIO dependant, but for a socket based BIO it would mean the connection has been closed. It tells you nothing about what happened before that closure, i.e. whether a close_notify was received or not.mattcaswell commentedon Dec 3, 2016
I should add that you can also use
SSL_get_shutdown()to explicitly test whether a connection has been cleanly shutdown or not. This actually tells you slightly different information toSSL_get_error(), i.e. it tells you whether a shutdown alert has been received (i.e. a close_notify or a fatal error alert), or whether we have sent a close_notify ourselves. A connection is only fully and cleanly closed if we have both sent and received a close_notify.SSL_get_error()will tell you whether a close_notify has been received through SSL_ERROR_RETURN_ZERO. Receipt of a fatal alert will appear as SSL_ERROR_SSL fromSSL_get_error()- although it could also mean some other kind of internal error has happened. A SSL_ERROR_SYSCALL return will tell you that some unknown error has occurred in a system call. This could be caused by an unclean shutdown.So, in summary, if you get a 0 or -1 return from
SSL_read()/SSL_write(), you should callSSL_get_error():SSL_shutdown()to send a close_notify back.SSL_get_shutdown(). If this indicates a state of SSL_RECEIVED_SHUTDOWN then you know a fatal alert has been received from the peer (if it had been a close_notify then SSL_get_error() would have returned SSL_ERROR_RETURN_ZERO). SSL_ERROR_SSL is considered fatal - you should not callSSL_shutdown()in this case.BIO_eof()at this point will tell you whether the underlying transport has hit EOF (i.e. for a socket BIO the connection has been closed).triska commentedon Dec 5, 2016
@mattcaswell: That's really an excellent overview, and I have used your explanation to make the application work with OpenSSL 1.1.0c as well as earlier versions. Thank you a lot, and I can only recommend to add the essential pieces of this guideline to the documentation of
SSL_read().Further, I have 2 specific questions about this:
SSL_ERROR_SSLandSSL_get_shutdown()signalingSSL_RECEIVED_SHUTDOWN), I still callSSL_shutdown(). How problematic is this? You say "you should not callSSL_shutdown()in this case", and I can see that it would not make sense, but does it warrant the extra complexity to prevent callingSSL_shutdown()regardless in this situation?SSL_accept()orSSL_connect(), and further, I getSSL_ERROR_SYSCALLfromSSL_get_error(), andERR_get_error()returns0, then the code I am maintaining assumes thatEOFwas unexpectedly encountered during negotiation. How safe is this assumption? Could this also be due to a different error? And conversely, couldEOFalso be reflected differently? In addition, what are the alternatives I have for distinguishing this case specifically (EOFduring session negotiation). I suppose I could test bothwbioandrbioforEOFwithBIO_eof(), but that likewise does not seem completely reliable to me, because if either of them is no longer needed, then it could have been closed without error.mattcaswell commentedon Dec 6, 2016
The problem here is that as soon as OpenSSL hits a fatal error (for whatever reason), it immediately returns. This might mean that the internal state of the SSL object is not left in a consistent state. If you then start to try and do IO with it then you will get undefined results. In the worst case scenario that could lead to a crash. In most cases it will probably work. In that case, if you call SSL_shutdown(), it will send a close_notify - which just doesn't make sense. Arguably, we should prevent that, so it's not inconceivable that we might change that behaviour at some point. As a general rule though, don't try and continue to do IO on an SSL object that has returned SSL_ERROR_SSL (or for that matter one which has returned SSL_ERROR_SYSCALL).
If you get SSL_ERROR_SYSCALL from SSL_get_error() it tells you is that there was a fatal non-retryable error in a system call. 99.9% of the time that is going to be because the peer closed the connection down. But even if it isn't, its a fatal non-retryable error so - effectively - the connection is closed, so I think it should be ok to treat it the same way.
kroeckx commentedon Dec 31, 2016
So just to clarify, in case of a fatal error (SSL_ERROR_SSL or SSL_ERROR_SYSCALL) you should not call SSL_shutdown(). We should probably at least document that. Possibly we should also make SSL_shutdown() and other functions check the state of the connection before they try to do something.
bernd-edlinger commentedon Dec 31, 2016
Could in theory also a fatal error happen in the bidirectional shutdown sequence ?
Is there a difference between a "fatal" SSL_ERROR_SYSCALL and a
non-fatal SSL_ERROR_SYSCALL from SSL_shutdown?
The manual on SSL_shutdown return value 0 says:
"The shutdown is not yet finished. Call SSL_shutdown() for a second time, if a bidirectional shutdown shall be performed. The output of SSL_get_error(3) may be misleading, as an erroneous SSL_ERROR_SYSCALL may be flagged even though no error occurred. "
Is a SSL_ERROR_SSL/SYSCALL with return value -1 more serious than with return value 0 ?
bernd-edlinger commentedon Jan 1, 2017
FYI: bio_ssl.c does not follow the rule to avoid SSL_shutdown if previously SSL_ERROR_SSL
or SSL_ERROR_SYSCALL were encountered, it is almost impossible to avoid the SSL_shutdown:
packages/libs/openssl: add patch for broken SSL_read