From e40f75b25322c11065dbade34e32ca177b10768c Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sat, 16 Jan 2021 21:00:34 +0000 Subject: [PATCH] malware: fix ClamAV file send corking --- src/src/malware.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/src/malware.c b/src/src/malware.c index b581a8752..abe627ded 100644 --- a/src/src/malware.c +++ b/src/src/malware.c @@ -1453,7 +1453,6 @@ badseek: err = errno; uschar av_buffer[1024]; uschar *hostname = US""; host_item connhost; - uschar *clamav_fbuf; int clam_fd, result; unsigned int fsize_uint; BOOL use_scan_command = FALSE; @@ -1631,6 +1630,9 @@ badseek: err = errno; if (!use_scan_command) { struct stat st; +#ifdef EXIM_TCP_CORK + BOOL corked = TRUE; +#endif /* New protocol: "zINSTREAM\n" followed by a sequence of chunks, a 4-byte number (network order), terminated by a zero-length chunk. */ @@ -1675,7 +1677,8 @@ badseek: err = errno; /* send file size */ #ifdef EXIM_TCP_CORK - (void) setsockopt(clam_fd, IPPROTO_TCP, EXIM_TCP_CORK, US &on, sizeof(on)); + (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK, + US &on, sizeof(on)); #endif send_size = htonl(fsize_uint); if (send(malware_daemon_ctx.sock, &send_size, sizeof(send_size), 0) < 0) @@ -1684,6 +1687,7 @@ badseek: err = errno; malware_daemon_ctx.sock); /* send file body */ + /*XXX sendfile? */ while (fsize_uint) { unsigned n = MIN(fsize_uint, big_buffer_size); @@ -1692,11 +1696,19 @@ badseek: err = errno; string_sprintf("can't read spool file %s: %s", eml_filename, strerror(errno)), malware_daemon_ctx.sock); - if ((n = send(malware_daemon_ctx.sock, clamav_fbuf, n, 0)) < 0) + if (send(malware_daemon_ctx.sock, big_buffer, (size_t)n, 0) < 0) return m_panic_defer_3(scanent, NULL, - string_sprintf("unable to send file body to socket (%s)", hostname), + string_sprintf("unable to send file body to socket (%s): %s", hostname, strerror(errno)), malware_daemon_ctx.sock); fsize_uint -= n; +#ifdef EXIM_TCP_CORK + if (corked) + { + corked = FALSE; + (void) setsockopt(malware_daemon_ctx.sock, IPPROTO_TCP, EXIM_TCP_CORK, + US &off, sizeof(off)); + } +#endif } send_final_zeroblock = 0; @@ -1704,9 +1716,6 @@ badseek: err = errno; return m_panic_defer_3(scanent, NULL, string_sprintf("unable to send file terminator to socket (%s)", hostname), malware_daemon_ctx.sock); -#ifdef EXIM_TCP_CORK - (void) setsockopt(clam_fd, IPPROTO_TCP, EXIM_TCP_CORK, US &off, sizeof(off)); -#endif } else { /* use scan command */ -- 2.30.2