Skip to content

Commit

Permalink
Added filestream flushing on error in EncryptCopyRegularFileNet
Browse files Browse the repository at this point in the history
By flushing the filestream, we make sure that there is no junk data
after error.

Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Oct 29, 2024
1 parent 34def7c commit 3a14d76
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions libcfnet/client_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,18 @@ bool CompareHashNet(const char *file1, const char *file2, bool encrypt, AgentCon

/*********************************************************************/

static void FlushFileStream(int sd, int toget)
{
int i;
char buffer[2];

Log(LOG_LEVEL_VERBOSE, "Flushing rest of file...%d bytes", toget);

for (i = 0; i < toget; i++)
{
recv(sd, buffer, 1, 0); /* flush to end of current file */
}
}

static bool EncryptCopyRegularFileNet(const char *source, const char *dest, off_t size, AgentConnection *conn)
{
Expand Down Expand Up @@ -675,13 +687,15 @@ static bool EncryptCopyRegularFileNet(const char *source, const char *dest, off_

if (!EVP_DecryptUpdate(crypto_ctx, (unsigned char *) workbuf, &plainlen, (unsigned char *) buf, cipherlen))
{
FlushFileStream(conn->conn_info->sd, size - n_wrote_total - n_read);
close(dd);
EVP_CIPHER_CTX_free(crypto_ctx);
return false;
}

if (!EVP_DecryptFinal_ex(crypto_ctx, (unsigned char *) workbuf + plainlen, &finlen))
{
FlushFileStream(conn->conn_info->sd, size - n_wrote_total - n_read);
close(dd);
EVP_CIPHER_CTX_free(crypto_ctx);
return false;
Expand All @@ -705,6 +719,7 @@ static bool EncryptCopyRegularFileNet(const char *source, const char *dest, off_
Log(LOG_LEVEL_INFO, "Source '%s:%s' changed while copying",
conn->this_server, source);
}
FlushFileStream(conn->conn_info->sd, size - n_wrote_total);
unlink(dest);
close(dd);
conn->error = true;
Expand All @@ -728,19 +743,6 @@ static bool EncryptCopyRegularFileNet(const char *source, const char *dest, off_
return true;
}

static void FlushFileStream(int sd, int toget)
{
int i;
char buffer[2];

Log(LOG_LEVEL_VERBOSE, "Flushing rest of file...%d bytes", toget);

for (i = 0; i < toget; i++)
{
recv(sd, buffer, 1, 0); /* flush to end of current file */
}
}

/* TODO finalize socket or TLS session in all cases that this function fails
* and the transaction protocol is out of sync. */
bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
Expand Down

0 comments on commit 3a14d76

Please sign in to comment.