| 1 |
|
| /* clamd_verdict.h - turning a reply into an answer that cannot be |
| 2 |
|
| * misread by accident. |
| 3 |
|
| * |
| 4 |
|
| * Phase 4 of plan_clamav_clamd. |
| 5 |
|
| * |
| 6 |
|
| * FOUR states, because clamd answers OK for files it declined to scan. |
| 7 |
|
| * A boolean API here reports "clean" for precisely the inputs an |
| 8 |
|
| * attacker constructs - a 2 KB nested zip, a password-protected archive, |
| 9 |
|
| * anything over MaxFileSize - and passes every test anybody writes, |
| 10 |
|
| * because nobody writes the test where the answer is "I did not look". |
| 11 |
|
| */ |
| 12 |
|
| #ifndef CLAMD_VERDICT_H |
| 13 |
|
| #define CLAMD_VERDICT_H |
| 14 |
|
| |
| 15 |
|
| /* stream.h, not conn.h: the local-ceiling and stream-cut codes live |
| 16 |
|
| * there, and both of them are verdicts rather than transport failures. */ |
| 17 |
|
| #include "clamav/clamd_stream.h" |
| 18 |
|
| |
| 19 |
|
| #define CC_CLEAN 0 |
| 20 |
|
| #define CC_INFECTED 1 |
| 21 |
|
| #define CC_UNSCANNABLE 2 |
| 22 |
|
| #define CC_ERROR 3 |
| 23 |
|
| |
| 24 |
|
| /* A signature name is REMOTE INPUT. A file crafted to match a chosen |
| 25 |
|
| * signature chooses the string that comes back, and that string ends up |
| 26 |
|
| * in logs and, in a careless consumer, in an HTTP response. It is |
| 27 |
|
| * length-bounded here and handed onward with its length. */ |
| 28 |
|
| #define CC_SIGLEN 256 |
| 29 |
|
| #define CC_REASONLEN 64 |
| 30 |
|
| |
| 31 |
|
| typedef struct { |
| 32 |
|
| int state; |
| 33 |
|
| char signature[CC_SIGLEN]; /* "" when there is none */ |
| 34 |
|
| char reason[CC_REASONLEN]; /* why unscannable; "" otherwise */ |
| 35 |
|
| } cc_verdict; |
| 36 |
|
| |
| 37 |
|
| /* The two families that mean "clamd did not see the content". |
| 38 |
|
| * |
| 39 |
|
| * NOT every Heuristics.* name: Heuristics.Phishing.* and |
| 40 |
|
| * Heuristics.OLE2.ContainsMacros are clamd saying it LOOKED and thinks |
| 41 |
|
| * the thing is bad, which is a detection. These two are clamd saying it |
| 42 |
|
| * could not look, which is the opposite and must never read as clean. */ |
| 43 |
|
| #define CC_PFX_LIMITS "Heuristics.Limits.Exceeded." |
| 44 |
|
| #define CC_PFX_ENCRYPTED "Heuristics.Encrypted." |
| 45 |
|
| |
| 46 |
58 |
| static int cc_starts_with(const char *s, size_t len, const char *pfx) { |
| 47 |
58 |
| size_t n = strlen(pfx); |
| 48 |
58 |
▶ | return len >= n && memcmp(s, pfx, n) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return len >= n && memcmp(s, pfx, n) == 0; |
18 |
40 |
58 |
| return len >= n && memcmp(s, pfx, n) == 0; |
12 |
6 |
18 |
|
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
147 |
| static int cc_ends_with(const char *s, size_t len, const char *suf) { |
| 52 |
147 |
| size_t n = strlen(suf); |
| 53 |
147 |
▶ | return len >= n && memcmp(s + len - n, suf, n) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return len >= n && memcmp(s + len - n, suf, n) == 0; |
134 |
13 |
147 |
| return len >= n && memcmp(s + len - n, suf, n) == 0; |
74 |
60 |
134 |
|
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
61 |
| static void cc_copy_bounded(char *dst, size_t cap, const char *src, size_t len) { |
| 57 |
61 |
▶ | if (len >= cap) len = cap - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= cap) len = cap - 1; |
1 |
60 |
61 |
|
| 58 |
61 |
▶ | if (len) memcpy(dst, src, len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) memcpy(dst, src, len); |
59 |
2 |
61 |
|
| 59 |
61 |
| dst[len] = '\0'; |
| 60 |
61 |
| } |
| 61 |
|
| |
| 62 |
|
| /* Parse one reply. |
| 63 |
|
| * |
| 64 |
|
| * Shapes, all measured rather than assumed: |
| 65 |
|
| * |
| 66 |
|
| * fd[11]: OK clean |
| 67 |
|
| * stream: OK clean |
| 68 |
|
| * fd[11]: Eicar-Test-Signature FOUND infected |
| 69 |
|
| * fd[11]: Heuristics.Limits.Exceeded.MaxFileSize FOUND unscannable |
| 70 |
|
| * fd[11]: Heuristics.Encrypted.Zip FOUND unscannable |
| 71 |
|
| * INSTREAM size limit exceeded. ERROR unscannable |
| 72 |
|
| * /x: File path check failure: ... ERROR error |
| 73 |
|
| * UNKNOWN COMMAND error (no ERROR suffix!) |
| 74 |
|
| * anything unrecognised error, NEVER clean |
| 75 |
|
| */ |
| 76 |
85 |
| static void cc_parse_reply(const char *reply, size_t len, cc_verdict *v) { |
| 77 |
85 |
| memset(v, 0, sizeof *v); |
| 78 |
85 |
| v->state = CC_ERROR; |
| 79 |
|
| |
| 80 |
85 |
▶ | if (len == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) return; |
1 |
84 |
85 |
|
| 81 |
|
| |
| 82 |
|
| /* Trim a trailing newline, which the 'n' framing leaves behind. */ |
| 83 |
87 |
▶ | while (len && (reply[len - 1] == '\n' || reply[len - 1] == '\r')) len--; |
Branch: 3 branches
| Branch | True | False | Total |
| while (len && (reply[len - 1] == '\n' || reply[len - 1] == '\r')) len--; |
86 |
1 |
87 |
| while (len && (reply[len - 1] == '\n' || reply[len - 1] == '\r')) len--; |
2 |
84 |
86 |
| while (len && (reply[len - 1] == '\n' || reply[len - 1] == '\r')) len--; |
1 |
83 |
84 |
|
| 84 |
|
| |
| 85 |
84 |
▶ | if (cc_ends_with(reply, len, " FOUND")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_ends_with(reply, len, " FOUND")) { |
31 |
53 |
84 |
|
| 86 |
|
| const char *sig; |
| 87 |
31 |
| size_t siglen, cut = len - 6; /* strip " FOUND" */ |
| 88 |
|
| size_t i; |
| 89 |
|
| |
| 90 |
|
| /* The description before the signature is "fd[N]: ", "stream: " |
| 91 |
|
| * or a path - and a path may itself contain ": ". Take the LAST |
| 92 |
|
| * ": " so a colon in a filename cannot eat the signature. */ |
| 93 |
31 |
| sig = reply; siglen = cut; |
| 94 |
4703 |
▶ | for (i = cut; i >= 2; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cut; i >= 2; i--) { |
4701 |
2 |
4703 |
|
| 95 |
4701 |
▶ | if (reply[i - 2] == ':' && reply[i - 1] == ' ') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (reply[i - 2] == ':' && reply[i - 1] == ' ') { |
30 |
4671 |
4701 |
| if (reply[i - 2] == ':' && reply[i - 1] == ' ') { |
29 |
1 |
30 |
|
| 96 |
29 |
| sig = reply + i; |
| 97 |
29 |
| siglen = cut - i; |
| 98 |
29 |
| break; |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
31 |
| cc_copy_bounded(v->signature, sizeof v->signature, sig, siglen); |
| 103 |
|
| |
| 104 |
31 |
▶ | if (cc_starts_with(sig, siglen, CC_PFX_LIMITS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_starts_with(sig, siglen, CC_PFX_LIMITS)) { |
4 |
27 |
31 |
|
| 105 |
4 |
| size_t n = strlen(CC_PFX_LIMITS); |
| 106 |
4 |
| v->state = CC_UNSCANNABLE; |
| 107 |
4 |
| cc_copy_bounded(v->reason, sizeof v->reason, sig + n, siglen - n); |
| 108 |
27 |
▶ | } else if (cc_starts_with(sig, siglen, CC_PFX_ENCRYPTED)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cc_starts_with(sig, siglen, CC_PFX_ENCRYPTED)) { |
8 |
19 |
27 |
|
| 109 |
8 |
| v->state = CC_UNSCANNABLE; |
| 110 |
8 |
| cc_copy_bounded(v->reason, sizeof v->reason, "Encrypted", 9); |
| 111 |
|
| } else { |
| 112 |
19 |
| v->state = CC_INFECTED; |
| 113 |
|
| } |
| 114 |
31 |
| return; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
53 |
▶ | if (cc_ends_with(reply, len, " OK") || (len == 2 && memcmp(reply, "OK", 2) == 0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cc_ends_with(reply, len, " OK") || (len == 2 && memcmp(reply, "OK", 2) == 0)) { |
11 |
42 |
53 |
| if (cc_ends_with(reply, len, " OK") || (len == 2 && memcmp(reply, "OK", 2) == 0)) { |
2 |
9 |
11 |
| if (cc_ends_with(reply, len, " OK") || (len == 2 && memcmp(reply, "OK", 2) == 0)) { |
1 |
1 |
2 |
|
| 118 |
43 |
| v->state = CC_CLEAN; |
| 119 |
43 |
| return; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* The stream ceiling. clamd DOES answer here - phase 0 recorded a |
| 123 |
|
| * silent close, which was its probe's bug, not clamd's behaviour. |
| 124 |
|
| * It is unscannable, not a transport error: nothing was scanned. */ |
| 125 |
10 |
▶ | if (cc_ends_with(reply, len, "size limit exceeded. ERROR")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_ends_with(reply, len, "size limit exceeded. ERROR")) { |
1 |
9 |
10 |
|
| 126 |
1 |
| v->state = CC_UNSCANNABLE; |
| 127 |
1 |
| cc_copy_bounded(v->reason, sizeof v->reason, "StreamMaxLength", 15); |
| 128 |
1 |
| return; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| /* Everything else - including "UNKNOWN COMMAND", which carries no |
| 132 |
|
| * ERROR suffix - is an error. The default is error and never clean, |
| 133 |
|
| * which is the single most important line in this file. */ |
| 134 |
9 |
| v->state = CC_ERROR; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| /* A failure that never reached a reply. Still a verdict, so that |
| 138 |
|
| * is_clean() is safe to call on whatever a scan returned. */ |
| 139 |
50 |
| static void cc_verdict_from_error(int code, cc_verdict *v) { |
| 140 |
50 |
| memset(v, 0, sizeof *v); |
| 141 |
|
| |
| 142 |
|
| /* Refused locally against max_size, or cut off past clamd's stream |
| 143 |
|
| * ceiling: in both cases nothing was scanned, which is exactly what |
| 144 |
|
| * unscannable means. Reporting these as transport errors would |
| 145 |
|
| * invite a caller to retry rather than to decide. */ |
| 146 |
50 |
▶ | if (code == CC_ERR_TOOBIGLOC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code == CC_ERR_TOOBIGLOC) { |
6 |
44 |
50 |
|
| 147 |
6 |
| v->state = CC_UNSCANNABLE; |
| 148 |
6 |
| cc_copy_bounded(v->reason, sizeof v->reason, "max_size", 8); |
| 149 |
6 |
| return; |
| 150 |
|
| } |
| 151 |
44 |
▶ | if (code == CC_ERR_STREAMCUT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code == CC_ERR_STREAMCUT) { |
11 |
33 |
44 |
|
| 152 |
11 |
| v->state = CC_UNSCANNABLE; |
| 153 |
11 |
| cc_copy_bounded(v->reason, sizeof v->reason, "StreamMaxLength", 15); |
| 154 |
11 |
| return; |
| 155 |
|
| } |
| 156 |
33 |
| v->state = CC_ERROR; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
104 |
| static const char *cc_state_name(int state) { |
| 160 |
104 |
| switch (state) { |
| 161 |
31 |
| case CC_CLEAN: return "clean"; |
| 162 |
14 |
| case CC_INFECTED: return "infected"; |
| 163 |
24 |
| case CC_UNSCANNABLE: return "unscannable"; |
| 164 |
35 |
| default: return "error"; |
| 165 |
|
| } |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| #endif /* CLAMD_VERDICT_H */ |