Skip to content

Commit

Permalink
Fix potential bug where TLSRecv returns less than expected
Browse files Browse the repository at this point in the history
TLSRecv may return less than the requested amount of Bytes. We fix this
my repeatedly calling it until we get the expected number.

Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Oct 21, 2024
1 parent e1c4fc2 commit 5272708
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
18 changes: 14 additions & 4 deletions libcfnet/client_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
assert(toget > 0);

/* Stage C1 - receive */
int n_read;
int n_read = 0;

const ProtocolVersion version = conn->conn_info->protocol;

Expand All @@ -820,7 +820,19 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
}
else if (ProtocolIsTLS(version))
{
n_read = TLSRecv(conn->conn_info->ssl, buf, toget);
/* TLSRecv may return less than the requested number of Bytes, in
* which case we repeatedly call it until we get the expected
* number. */
while (n_read < toget)
{
int rc = TLSRecv(conn->conn_info->ssl, buf + n_read, toget - n_read);
if (rc <= 0)
{
n_read = rc;
break;
}
n_read += rc;
}
}
else
{
Expand All @@ -829,8 +841,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
n_read = -1;
}

/* TODO what if 0 < n_read < toget? Might happen with TLS. */

if (n_read <= 0)
{
/* This may happen on race conditions, where the file has shrunk
Expand Down
27 changes: 25 additions & 2 deletions libcfnet/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,19 @@ int ReceiveTransaction(ConnectionInfo *conn_info, char *buffer, int *more)
ret = RecvSocketStream(conn_info->sd, proto, CF_INBAND_OFFSET);
break;
case CF_PROTOCOL_TLS:
ret = TLSRecv(conn_info->ssl, proto, CF_INBAND_OFFSET);
/* TLSRecv may return less than the requested number of Bytes, in
* which case we repeatedly call it until we get the expected number.
*/
while (ret < CF_INBAND_OFFSET)
{
int rc = TLSRecv(conn_info->ssl, proto + ret, CF_INBAND_OFFSET - ret);
if (rc <= 0)
{
ret = rc;
break;
}
ret += rc;
}
break;
default:
UnexpectedError("ReceiveTransaction: ProtocolVersion %d!",
Expand Down Expand Up @@ -251,7 +263,18 @@ int ReceiveTransaction(ConnectionInfo *conn_info, char *buffer, int *more)
ret = RecvSocketStream(conn_info->sd, buffer, len);
break;
case CF_PROTOCOL_TLS:
ret = TLSRecv(conn_info->ssl, buffer, len);
while (ret < len)
{
/* TLSRecv may return less than the requested number of Bytes, in
* which case we repeatedly call it until we get the expected
* number. */
int rc = TLSRecv(conn_info->ssl, buffer + ret, len - ret);
if (rc <= 0)
{
ret = rc;
}
ret += rc;
}
break;
default:
UnexpectedError("ReceiveTransaction: ProtocolVersion %d!",
Expand Down

0 comments on commit 5272708

Please sign in to comment.