Skip to content

Commit ef3fe19

Browse files
hlefdavidchisnall
authored andcommitted
Better handle TCP/IP stack crashes in the TLS compartment.
When the TCP/IP stack crashes, API calls to the compartment return `-ECOMPARTMENTFAIL`. These should be treated similarly to `-ENOTCONN`. Currently `-ECOMPARTMENTFAIL` failures are not considered by the TLS compartment and are handled in various (incorrect) ways across the code-base. Address this. Signed-off-by: Hugo Lefeuvre <[email protected]>
1 parent 7142e42 commit ef3fe19

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

include/NetAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ NetworkReceiveResult __cheri_compartment("TCPIP")
163163
*
164164
* The negative values will be errno values:
165165
*
166+
* - `-EPERM`: `buffer` and/or `length` are invalid.
166167
* - `-EINVAL`: The socket is not valid.
167168
* - `-ETIMEDOUT`: The timeout was reached before data could be received.
168169
* - `-ENOTCONN`: The socket is not connected.

lib/tls/tls.cc

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,21 @@ namespace
353353
if ((state & BR_SSL_RECVREC) == BR_SSL_RECVREC)
354354
{
355355
int received = receive_records(t, connection);
356-
if (received == 0 || received == -ENOTCONN)
357-
{
358-
// The link died. After
359-
// getting -ENOTCONN, the
360-
// caller should close the
361-
// TLS socket.
362-
return -ENOTCONN;
363-
}
364356
if (received == -ETIMEDOUT)
365357
{
366358
return -ETIMEDOUT;
367359
}
360+
if (received <= 0)
361+
{
362+
// The receive failed. This
363+
// can happen for a number of
364+
// reasons, but most likely
365+
// if the link died. After
366+
// getting -ENOTCONN, the
367+
// caller of this API should
368+
// close the TLS socket.
369+
return -ENOTCONN;
370+
}
368371
// Next loop iteration, we'll try pulling the
369372
// data out of the TLS engine.
370373
}
@@ -552,6 +555,12 @@ ssize_t tls_connection_send(Timeout *t,
552555
// If there's data ready to send over the network, prioritise
553556
// sending it
554557
auto [sent, unfinished] = send_records(t, connection);
558+
if (sent == -ECOMPARTMENTFAIL)
559+
{
560+
// The TCP/IP stack crashed; tell the
561+
// caller that the link is dead.
562+
return -ENOTCONN;
563+
}
555564
if (sent <= 0)
556565
{
557566
return sent;
@@ -742,6 +751,12 @@ int tls_connection_close(Timeout *t, SObj sealed)
742751
{
743752
return -ETIMEDOUT;
744753
}
754+
if (received == -ECOMPARTMENTFAIL)
755+
{
756+
// The TCP/IP stack crashed; give up and don't
757+
// gracefully terminate.
758+
break;
759+
}
745760
if (received <= 0)
746761
{
747762
// If we failed for any reason other than

0 commit comments

Comments
 (0)