include/clamav/clamd_stream.h

filter: Statementstmt 100% 17 / 17 Branchbran 80.0% 9 / 10 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.5% 26 / 27 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/ClamAV/Clamd/Install/Files.pm ↑ Summary include/clamav/clamd_async.h »
1 /* clamd_stream.h - the INSTREAM pieces.
2 *
3 * Phase 3 of plan_clamav_clamd, trimmed by phase 5. The write loop that
4 * used to live here is now the state machine in clamd_async.h, which
5 * both the blocking and the non-blocking paths run - there is only one
6 * implementation of the protocol in this distribution, deliberately.
7 *
8 * INSTREAM exists for three cases FILDES cannot serve: bytes that were
9 * never a file, a clamd reached over TCP, and a platform with no
10 * SCM_RIGHTS.
11 *
12 * It is harder than FILDES for one reason, and the shape of the write
13 * loop comes entirely from it: clamd may answer WHILE THE CLIENT IS
14 * STILL WRITING. It found a signature in chunk 3 of 400, or the stream
15 * hit StreamMaxLength - and it may then close. A client that writes the
16 * whole stream before it reads gets EPIPE on a scan that actually
17 * succeeded, and reports an error for a file clamd correctly identified
18 * as infected.
19 *
20 * Phase 0 recorded that exceeding StreamMaxLength produced a silent
21 * close. That was wrong, and wrong because of this exact bug: the probe
22 * wrote the whole stream before reading, so it hit EPIPE and never read
23 * the reply already waiting for it. clamd does send
24 *
25 * INSTREAM size limit exceeded. ERROR
26 *
27 * and reading before writing the next chunk is what makes it visible.
28 */
29 #ifndef CLAMD_STREAM_H
30 #define CLAMD_STREAM_H
31  
32 #include "clamav/clamd_conn.h"
33  
34 #ifndef _WIN32
35 # include <sys/uio.h>
36 #endif
37  
38 #define CC_ERR_STREAMCUT -9 /* peer closed mid-stream with no verdict */
39 #define CC_ERR_TOOBIGLOC -10 /* refused locally against max_size */
40  
41 /* Measured, not picked - see the plan's phase 3 results. Large enough
42 * that a big upload is not a syscall storm, small enough that an early
43 * reply is noticed promptly, and small enough that one buffer per
44 * concurrent scan is not real memory in a prefork pool. */
45 #define CC_DEF_CHUNK (64 * 1024)
46  
47 /* Has clamd said something already? A reply arriving mid-write is a
48 * VERDICT, not a failure, and this is the check that keeps it from being
49 * mistaken for one. Non-blocking: never waits. */
50 24804 static int cc_reply_pending(int sock) {
51 24804 return cc_readable_now(sock);
52 }
53  
54 /* Source abstraction so memory and a descriptor share one write loop -
55 * two copies of that loop would mean the rarely used one rots. */
56 typedef struct {
57 const char *mem; /* memory source, or NULL */
58 size_t memlen;
59 size_t memoff;
60 int fd; /* descriptor source, or -1 */
61 char *iobuf;
62 size_t chunk;
63 } cc_source;
64  
65 /* Returns bytes available at *p, 0 at end of input, -1 on error.
66 *
67 * Note this read is blocking even on the async path. The source is a
68 * regular file or memory - never a socket - so it does not wait on a
69 * peer, and making local file reads incremental would buy nothing but
70 * state to get wrong. */
71 24792 static ssize_t cc_source_next(cc_source *s, const char **p, cc_err *err) {
72 24792 ▶ if (s->mem) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->mem) { 24727 65 24792
73 24727 size_t left = s->memlen - s->memoff;
74 24727 size_t take = left < s->chunk ? left : s->chunk;
75 24727 ▶ if (take == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (take == 0) return 0; 71 24656 24727
76 24656 *p = s->mem + s->memoff;
77 24656 s->memoff += take;
78 24656 return (ssize_t)take;
79 }
80 {
81 ssize_t n;
82 do {
83 65 n = cc_read_fd(s->fd, s->iobuf, s->chunk);
84 /*
85 # uncoverable branch true
86 # uncoverable condition left count:1
87 # uncoverable condition left count:2 */
88 65 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 1 64 65
} while (n < 0 && errno == EINTR); 0 1 1
89 65 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 1 64 65
90 1 cc_err_set(err, CC_ERR_IO, "read", strerror(errno));
91 1 return -1;
92 }
93 64 *p = s->iobuf;
94 64 return n;
95 }
96 }
97  
98 #endif /* CLAMD_STREAM_H */
« blib/arch/ClamAV/Clamd/Install/Files.pm ↑ Summary include/clamav/clamd_async.h »