| 1 |
|
| /* clamd_async.h - a scan that proceeds on somebody else's loop. |
| 2 |
|
| * |
| 3 |
|
| * Phase 5 of plan_clamav_clamd. |
| 4 |
|
| * |
| 5 |
|
| * WHY NOT AN ADAPTER. DBIx::Loop, the house precedent, takes a loop |
| 6 |
|
| * adapter - add_reader/add_writer/remove/timer/new_future - plus a C |
| 7 |
|
| * vtable so a C-side loop dispatches without a Perl frame. That is right |
| 8 |
|
| * for DBIx::Loop: it has long-lived connections, a worker pool, |
| 9 |
|
| * transactions and futures, and it genuinely needs timers. |
| 10 |
|
| * |
| 11 |
|
| * This has one short-lived connection and one request/response. An |
| 12 |
|
| * adapter interface plus a conformance suite plus a dist per loop would |
| 13 |
|
| * be more machinery than the protocol. So the surface is smaller: |
| 14 |
|
| * |
| 15 |
|
| * fd - register it with whatever loop you have |
| 16 |
|
| * want - readable, writable, or neither |
| 17 |
|
| * step - advance as far as possible without blocking |
| 18 |
|
| * verdict - once finished |
| 19 |
|
| * |
| 20 |
|
| * Twenty lines wraps that in a Punk::Future, an IO::Async one, or a bare |
| 21 |
|
| * select loop, and none of those wrappers are this file's problem. |
| 22 |
|
| * |
| 23 |
|
| * THE BLOCKING PATH IS THIS MACHINE UNDER poll(). There is deliberately |
| 24 |
|
| * no second implementation of the protocol - one being a re-run of the |
| 25 |
|
| * other is how the rarely-used path rots. That goes for PING, VERSION and |
| 26 |
|
| * the rest as well: a command is a scan with nothing to stream, so the |
| 27 |
|
| * reply reader and its ceiling are the same code whichever way clamd is |
| 28 |
|
| * asked. |
| 29 |
|
| * |
| 30 |
|
| * WHAT THE CALLER OWNS. Without a timer this cannot wake anybody, so the |
| 31 |
|
| * deadline lives in here instead: it is checked on every step, and a |
| 32 |
|
| * step taken after it expires fails. A driver that stops stepping is a |
| 33 |
|
| * driver that never learns it timed out - which is the honest division, |
| 34 |
|
| * and it is documented rather than papered over. |
| 35 |
|
| */ |
| 36 |
|
| #ifndef CLAMD_ASYNC_H |
| 37 |
|
| #define CLAMD_ASYNC_H |
| 38 |
|
| |
| 39 |
|
| #include "clamav/clamd_verdict.h" |
| 40 |
|
| |
| 41 |
|
| #define CC_WANT_NONE 0 |
| 42 |
|
| #define CC_WANT_READ 1 |
| 43 |
|
| #define CC_WANT_WRITE 2 |
| 44 |
|
| |
| 45 |
|
| #define CC_STEP_MORE 0 /* not finished; wait for cc_want() then step */ |
| 46 |
|
| #define CC_STEP_DONE 1 /* finished; the verdict is ready */ |
| 47 |
|
| |
| 48 |
|
| typedef enum { |
| 49 |
|
| CC_PH_SEND_CMD, |
| 50 |
|
| CC_PH_SEND_FD, |
| 51 |
|
| CC_PH_STREAM, |
| 52 |
|
| CC_PH_READ, |
| 53 |
|
| CC_PH_DONE |
| 54 |
|
| } cc_phase; |
| 55 |
|
| |
| 56 |
|
| typedef struct { |
| 57 |
|
| cc_phase phase; |
| 58 |
|
| int sock; |
| 59 |
|
| int want; |
| 60 |
|
| double deadline; |
| 61 |
|
| |
| 62 |
|
| /* An async scan outlives the call that started it, so the address |
| 63 |
|
| * cannot stay a borrowed pointer into a Perl scalar. */ |
| 64 |
|
| cc_target t; |
| 65 |
|
| char *path_copy; |
| 66 |
|
| char *host_copy; |
| 67 |
|
| |
| 68 |
|
| char cmd[32]; |
| 69 |
|
| size_t cmdlen, cmdoff; |
| 70 |
|
| |
| 71 |
|
| int scan_fd; /* FILDES: borrowed unless own_fd */ |
| 72 |
|
| int own_fd; |
| 73 |
|
| |
| 74 |
|
| cc_source src; |
| 75 |
|
| unsigned char hdr[4]; |
| 76 |
|
| size_t hdroff; |
| 77 |
|
| const char *chunk; |
| 78 |
|
| size_t chunklen, chunkoff; |
| 79 |
|
| int have_chunk; |
| 80 |
|
| |
| 81 |
|
| char *reply; |
| 82 |
|
| size_t replylen, replycap; |
| 83 |
|
| |
| 84 |
|
| int transport; |
| 85 |
|
| int rc; |
| 86 |
|
| cc_err err; |
| 87 |
|
| |
| 88 |
|
| /* Parsed once, on demand. Both the XS layer and the C ABI read the |
| 89 |
|
| * verdict from here, so there is one parse and one answer - an |
| 90 |
|
| * accessor that reparses can disagree with itself, which phase 5 |
| 91 |
|
| * already got caught doing. */ |
| 92 |
|
| cc_verdict verdict; |
| 93 |
|
| int verdict_ready; |
| 94 |
|
| |
| 95 |
|
| /* A step that neither consumes nor produces anything, repeatedly, is |
| 96 |
|
| * a busy loop wearing the shape of progress. On a level-triggered |
| 97 |
|
| * loop the caller will call straight back, so the machine counts its |
| 98 |
|
| * own idle steps and gives up rather than pinning a core. */ |
| 99 |
|
| int idle_steps; |
| 100 |
|
| } cc_scan; |
| 101 |
|
| |
| 102 |
|
| #define CC_MAX_IDLE_STEPS 10000 |
| 103 |
|
| |
| 104 |
181 |
| static void cc_scan_free(cc_scan *s) { |
| 105 |
181 |
▶ | if (s->sock != CC_INVALID_SOCK) { cc_close_sock(s->sock); s->sock = CC_INVALID_SOCK; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->sock != CC_INVALID_SOCK) { cc_close_sock(s->sock); s->sock = CC_INVALID_SOCK; } |
7 |
174 |
181 |
|
| 106 |
181 |
▶ | if (s->own_fd) cc_close_fd(s->scan_fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->own_fd) cc_close_fd(s->scan_fd); |
3 |
178 |
181 |
|
| 107 |
181 |
| s->scan_fd = -1; s->own_fd = 0; |
| 108 |
181 |
▶ | if (s->src.iobuf) { free(s->src.iobuf); s->src.iobuf = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->src.iobuf) { free(s->src.iobuf); s->src.iobuf = NULL; } |
7 |
174 |
181 |
|
| 109 |
181 |
▶ | if (s->reply) { free(s->reply); s->reply = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->reply) { free(s->reply); s->reply = NULL; } |
34 |
147 |
181 |
|
| 110 |
181 |
▶ | if (s->path_copy) { free(s->path_copy); s->path_copy = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->path_copy) { free(s->path_copy); s->path_copy = NULL; } |
148 |
33 |
181 |
|
| 111 |
181 |
▶ | if (s->host_copy) { free(s->host_copy); s->host_copy = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->host_copy) { free(s->host_copy); s->host_copy = NULL; } |
22 |
159 |
181 |
|
| 112 |
181 |
| } |
| 113 |
|
| |
| 114 |
|
| /* Finish with a failure. A NULL msg keeps the error already recorded. */ |
| 115 |
60 |
| static void cc_fail(cc_scan *s, int code, const char *msg, const char *detail) { |
| 116 |
60 |
▶ | if (msg) cc_err_set(&s->err, code, msg, detail); |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg) cc_err_set(&s->err, code, msg, detail); |
33 |
27 |
60 |
|
| 117 |
60 |
| s->rc = code; |
| 118 |
60 |
| s->phase = CC_PH_DONE; |
| 119 |
60 |
| s->want = CC_WANT_NONE; |
| 120 |
60 |
▶ | if (s->sock != CC_INVALID_SOCK) { cc_close_sock(s->sock); s->sock = CC_INVALID_SOCK; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->sock != CC_INVALID_SOCK) { cc_close_sock(s->sock); s->sock = CC_INVALID_SOCK; } |
33 |
27 |
60 |
|
| 121 |
60 |
| } |
| 122 |
|
| |
| 123 |
|
| /* Finish with clamd's reply in hand. */ |
| 124 |
105 |
| static void cc_finish(cc_scan *s) { |
| 125 |
105 |
| s->reply[s->replylen] = '\0'; |
| 126 |
105 |
| s->rc = CC_OK; |
| 127 |
105 |
| s->phase = CC_PH_DONE; |
| 128 |
105 |
| s->want = CC_WANT_NONE; |
| 129 |
105 |
| cc_close_sock(s->sock); s->sock = CC_INVALID_SOCK; |
| 130 |
105 |
| } |
| 131 |
|
| |
| 132 |
373 |
| static char *cc_strdup(const char *p) { |
| 133 |
|
| size_t n; |
| 134 |
|
| char *d; |
| 135 |
373 |
▶ | if (!p) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return NULL; |
172 |
201 |
373 |
|
| 136 |
201 |
| n = strlen(p) + 1; |
| 137 |
201 |
| d = (char *)malloc(n); |
| 138 |
|
| /* |
| 139 |
|
| # uncoverable branch false |
| 140 |
|
| # uncoverable condition right count:1 */ |
| 141 |
201 |
▶ | if (d) memcpy(d, p, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (d) memcpy(d, p, n); |
201 |
0 |
201 |
|
| 142 |
201 |
| return d; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
171 |
| static void cc_scan_reset(cc_scan *s, const cc_target *t, int transport) { |
| 146 |
171 |
| memset(s, 0, sizeof *s); |
| 147 |
171 |
| s->sock = CC_INVALID_SOCK; |
| 148 |
171 |
| s->scan_fd = -1; |
| 149 |
171 |
| s->rc = CC_OK; |
| 150 |
|
| |
| 151 |
171 |
| s->t = *t; |
| 152 |
171 |
| s->path_copy = cc_strdup(t->path); |
| 153 |
171 |
| s->host_copy = cc_strdup(t->host); |
| 154 |
171 |
| s->t.path = s->path_copy; |
| 155 |
171 |
| s->t.host = s->host_copy; |
| 156 |
|
| |
| 157 |
171 |
▶ | s->deadline = cc_now() + (t->reply_timeout > 0 ? t->reply_timeout : 30.0); |
Branch: 1 branch
| Branch | True | False | Total |
| s->deadline = cc_now() + (t->reply_timeout > 0 ? t->reply_timeout : 30.0); |
169 |
2 |
171 |
|
| 158 |
171 |
| s->transport = transport; |
| 159 |
171 |
| } |
| 160 |
|
| |
| 161 |
|
| /* Frame the command and begin the connect. Does not block: cc_connect |
| 162 |
|
| * leaves the socket non-blocking, and has already waited for |
| 163 |
|
| * writability, which is the one thing here that is not yet fully |
| 164 |
|
| * incremental - a UNIX connect completes immediately and a TCP one is |
| 165 |
|
| * bounded by connect_timeout. */ |
| 166 |
171 |
| static int cc_scan_open(cc_scan *s, const char *cmd) { |
| 167 |
171 |
| size_t n = strlen(cmd); |
| 168 |
|
| |
| 169 |
|
| /* |
| 170 |
|
| # uncoverable branch true |
| 171 |
|
| # uncoverable condition left count:1 */ |
| 172 |
171 |
▶ | if (n + 2 > sizeof s->cmd) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n + 2 > sizeof s->cmd) { |
0 |
171 |
171 |
|
| 173 |
0 |
| cc_fail(s, CC_ERR_CONFIG, "command too long", cmd); /* # uncoverable statement */ |
| 174 |
0 |
| return s->rc; /* # uncoverable statement */ |
| 175 |
|
| } |
| 176 |
171 |
▶ | s->cmd[0] = (s->t.frame == CC_FRAME_N) ? 'n' : 'z'; |
Branch: 1 branch
| Branch | True | False | Total |
| s->cmd[0] = (s->t.frame == CC_FRAME_N) ? 'n' : 'z'; |
3 |
168 |
171 |
|
| 177 |
171 |
| memcpy(s->cmd + 1, cmd, n); |
| 178 |
171 |
▶ | s->cmd[n + 1] = (s->t.frame == CC_FRAME_N) ? '\n' : '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| s->cmd[n + 1] = (s->t.frame == CC_FRAME_N) ? '\n' : '\0'; |
3 |
168 |
171 |
|
| 179 |
171 |
| s->cmdlen = n + 2; |
| 180 |
171 |
| s->cmdoff = 0; |
| 181 |
|
| |
| 182 |
171 |
| s->sock = cc_connect(&s->t, &s->err); |
| 183 |
171 |
▶ | if (s->sock == CC_INVALID_SOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->sock == CC_INVALID_SOCK) { |
26 |
145 |
171 |
|
| 184 |
26 |
| cc_fail(s, s->err.code, NULL, NULL); |
| 185 |
26 |
| return s->rc; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
145 |
| s->phase = CC_PH_SEND_CMD; |
| 189 |
145 |
| s->want = CC_WANT_WRITE; |
| 190 |
145 |
| return CC_OK; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
|
| /* Start a scan. |
| 194 |
|
| * |
| 195 |
|
| * mode is CC_TRANSPORT_FILDES or CC_TRANSPORT_INSTREAM. For FILDES the |
| 196 |
|
| * descriptor is borrowed (own_fd 0) unless the caller opened it here. |
| 197 |
|
| * For INSTREAM either mem/memlen or a source fd is used. |
| 198 |
|
| */ |
| 199 |
131 |
| static int cc_scan_start(cc_scan *s, const cc_target *t, int mode, |
| 200 |
|
| int fd, int own_fd, |
| 201 |
|
| const char *mem, size_t memlen, int src_fd) { |
| 202 |
131 |
| cc_scan_reset(s, t, mode); |
| 203 |
|
| |
| 204 |
131 |
▶ | if (mode == CC_TRANSPORT_FILDES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mode == CC_TRANSPORT_FILDES) { |
13 |
118 |
131 |
|
| 205 |
13 |
| s->scan_fd = fd; |
| 206 |
13 |
| s->own_fd = own_fd; |
| 207 |
|
| } else { |
| 208 |
118 |
▶ | s->src.chunk = t->chunk ? t->chunk : CC_DEF_CHUNK; |
Branch: 1 branch
| Branch | True | False | Total |
| s->src.chunk = t->chunk ? t->chunk : CC_DEF_CHUNK; |
17 |
101 |
118 |
|
| 209 |
118 |
| s->src.fd = -1; |
| 210 |
118 |
▶ | if (mem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mem) { |
111 |
7 |
118 |
|
| 211 |
111 |
| s->src.mem = mem; |
| 212 |
111 |
| s->src.memlen = memlen; |
| 213 |
|
| } else { |
| 214 |
7 |
| s->src.fd = src_fd; |
| 215 |
7 |
| s->scan_fd = src_fd; |
| 216 |
7 |
| s->own_fd = own_fd; |
| 217 |
7 |
| s->src.iobuf = (char *)malloc(s->src.chunk); |
| 218 |
|
| /* |
| 219 |
|
| # uncoverable branch true |
| 220 |
|
| # uncoverable condition left count:1 */ |
| 221 |
7 |
▶ | if (!s->src.iobuf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->src.iobuf) { |
0 |
7 |
7 |
|
| 222 |
0 |
| cc_fail(s, CC_ERR_IO, "out of memory", NULL); /* # uncoverable statement */ |
| 223 |
0 |
| return s->rc; /* # uncoverable statement */ |
| 224 |
|
| } |
| 225 |
|
| } |
| 226 |
|
| } |
| 227 |
131 |
▶ | return cc_scan_open(s, mode == CC_TRANSPORT_FILDES ? "FILDES" : "INSTREAM"); |
Branch: 1 branch
| Branch | True | False | Total |
| return cc_scan_open(s, mode == CC_TRANSPORT_FILDES ? "FILDES" : "INSTREAM"); |
13 |
118 |
131 |
|
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| /* A command with nothing to stream: PING, VERSION, STATS, RELOAD. */ |
| 231 |
40 |
| static int cc_scan_command(cc_scan *s, const cc_target *t, const char *cmd) { |
| 232 |
40 |
| cc_scan_reset(s, t, CC_TRANSPORT_NONE); |
| 233 |
40 |
| return cc_scan_open(s, cmd); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
133 |
| static cc_phase cc_after_command(int transport) { |
| 237 |
133 |
▶ | if (transport == CC_TRANSPORT_FILDES) return CC_PH_SEND_FD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (transport == CC_TRANSPORT_FILDES) return CC_PH_SEND_FD; |
12 |
121 |
133 |
|
| 238 |
121 |
▶ | if (transport == CC_TRANSPORT_INSTREAM) return CC_PH_STREAM; |
Branch: 1 branch
| Branch | True | False | Total |
| if (transport == CC_TRANSPORT_INSTREAM) return CC_PH_STREAM; |
91 |
30 |
121 |
|
| 239 |
30 |
| return CC_PH_READ; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| #if CC_HAVE_FD_PASSING |
| 243 |
|
| /* The descriptor rides its own one-byte message, after the command - |
| 244 |
|
| * see clamd_scan.h for why. It is a single sendmsg, so there is no |
| 245 |
|
| * partial-progress state to keep. */ |
| 246 |
12 |
| static ssize_t cc_send_fd(int sock, int fd) { |
| 247 |
|
| struct msghdr msg; |
| 248 |
|
| struct iovec iov; |
| 249 |
|
| union { struct cmsghdr align; char buf[CMSG_SPACE(sizeof(int))]; } ctl; |
| 250 |
|
| struct cmsghdr *cm; |
| 251 |
12 |
| char pad = '\0'; |
| 252 |
|
| ssize_t n; |
| 253 |
|
| |
| 254 |
12 |
| memset(&msg, 0, sizeof msg); |
| 255 |
12 |
| memset(&ctl, 0, sizeof ctl); |
| 256 |
12 |
| iov.iov_base = &pad; iov.iov_len = 1; |
| 257 |
12 |
| msg.msg_iov = &iov; msg.msg_iovlen = 1; |
| 258 |
12 |
| msg.msg_control = ctl.buf; |
| 259 |
12 |
| msg.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 260 |
12 |
▶ | cm = CMSG_FIRSTHDR(&msg); |
Branch: 1 branch
| Branch | True | False | Total |
| cm = CMSG_FIRSTHDR(&msg); |
12 |
0 |
12 |
|
| 261 |
12 |
| cm->cmsg_level = SOL_SOCKET; |
| 262 |
12 |
| cm->cmsg_type = SCM_RIGHTS; |
| 263 |
12 |
| cm->cmsg_len = CMSG_LEN(sizeof(int)); |
| 264 |
12 |
| memcpy(CMSG_DATA(cm), &fd, sizeof(int)); |
| 265 |
|
| |
| 266 |
|
| do { |
| 267 |
12 |
| n = sendmsg(sock, &msg, CC_MSG_NOSIGNAL); |
| 268 |
|
| /* |
| 269 |
|
| # uncoverable branch true |
| 270 |
|
| # uncoverable condition left count:1 |
| 271 |
|
| # uncoverable condition left count:2 |
| 272 |
|
| # uncoverable condition right count:2 */ |
| 273 |
12 |
▶ | } while (n < 0 && errno == EINTR); |
Branch: 2 branches
| Branch | True | False | Total |
| } while (n < 0 && errno == EINTR); |
0 |
12 |
12 |
| } while (n < 0 && errno == EINTR); |
0 |
0 |
0 |
|
| 274 |
12 |
| return n; |
| 275 |
|
| } |
| 276 |
|
| #endif |
| 277 |
|
| |
| 278 |
|
| /* Send what is left of a buffer. 1 when all of it has gone, 0 when the |
| 279 |
|
| * socket is full and the caller should wait for it, -1 when the peer has |
| 280 |
|
| * gone, -2 when the scan has failed. A partial send is the normal case |
| 281 |
|
| * here rather than an exception, and an offset that does not survive |
| 282 |
|
| * between steps corrupts the stream silently. |
| 283 |
|
| * |
| 284 |
|
| * The peer going mid-send is real - clamd hangs up at StreamMaxLength - |
| 285 |
|
| * but a hangup is also readable, and the check for a pending reply |
| 286 |
|
| * before every chunk sees it first unless clamd closes in the few |
| 287 |
|
| * microseconds between that check and the send. No test can arrange |
| 288 |
|
| * that on purpose, so the arm is marked as one a run may or may not |
| 289 |
|
| * meet. */ |
| 290 |
49579 |
| static int cc_send_some(cc_scan *s, const char *buf, size_t len, size_t *off, int *progressed) { |
| 291 |
99066 |
▶ | while (*off < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*off < len) { |
49502 |
49564 |
99066 |
|
| 292 |
49502 |
| ssize_t n = cc_send(s->sock, buf + *off, len - *off, CC_MSG_NOSIGNAL); |
| 293 |
49502 |
▶ | if (n > 0) { *off += (size_t)n; *progressed = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { *off += (size_t)n; *progressed = 1; continue; } |
49487 |
15 |
49502 |
|
| 294 |
15 |
▶ | if (cc_would_block(errno)) return 0; /* # uncoverable branch false class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_would_block(errno)) return 0; /* # uncoverable branch false class:ignore_covered_err */ |
13 |
2 |
15 |
|
| 295 |
|
| /* |
| 296 |
|
| # uncoverable statement class:ignore_covered_err |
| 297 |
|
| # uncoverable branch true class:ignore_covered_err |
| 298 |
|
| # uncoverable branch false |
| 299 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 300 |
|
| # uncoverable condition right count:1 */ |
| 301 |
2 |
▶ | if (cc_errno_is_closed(errno)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_errno_is_closed(errno)) return -1; |
2 |
0 |
2 |
|
| 302 |
0 |
| cc_fail(s, CC_ERR_IO, "send", strerror(errno)); /* # uncoverable statement */ |
| 303 |
0 |
| return -2; /* # uncoverable statement */ |
| 304 |
|
| } |
| 305 |
49564 |
| return 1; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| /* Advance as far as possible without blocking. |
| 309 |
|
| * |
| 310 |
|
| * Returns CC_STEP_DONE when finished (check s->rc and s->reply), or |
| 311 |
|
| * CC_STEP_MORE with s->want naming the readiness to wait for. |
| 312 |
|
| */ |
| 313 |
10263 |
| static int cc_scan_step(cc_scan *s) { |
| 314 |
10263 |
| int progressed = 0; |
| 315 |
|
| |
| 316 |
10263 |
▶ | if (s->phase == CC_PH_DONE) return CC_STEP_DONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->phase == CC_PH_DONE) return CC_STEP_DONE; |
4 |
10259 |
10263 |
|
| 317 |
|
| |
| 318 |
10259 |
▶ | if (cc_now() > s->deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_now() > s->deadline) { |
2 |
10257 |
10259 |
|
| 319 |
2 |
| cc_fail(s, CC_ERR_TIMEOUT, "deadline exceeded", NULL); |
| 320 |
2 |
| return CC_STEP_DONE; |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
|
| for (;;) { |
| 324 |
35212 |
| switch (s->phase) { /* # uncoverable branch true count:4 */ |
| 325 |
|
| |
| 326 |
|
| /* The command, then for FILDES the descriptor's own message. |
| 327 |
|
| * Neither can meet a full socket: both go out first on a fresh |
| 328 |
|
| * connection, and the second is one byte. */ |
| 329 |
146 |
| case CC_PH_SEND_CMD: |
| 330 |
|
| case CC_PH_SEND_FD: { |
| 331 |
|
| ssize_t n; |
| 332 |
146 |
▶ | if (s->phase == CC_PH_SEND_CMD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->phase == CC_PH_SEND_CMD) { |
134 |
12 |
146 |
|
| 333 |
134 |
| n = cc_send(s->sock, s->cmd + s->cmdoff, s->cmdlen - s->cmdoff, CC_MSG_NOSIGNAL); |
| 334 |
134 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
133 |
1 |
134 |
|
| 335 |
133 |
| s->cmdoff += (size_t)n; |
| 336 |
133 |
| progressed = 1; |
| 337 |
|
| /* |
| 338 |
|
| # uncoverable branch true |
| 339 |
|
| # uncoverable condition left count:1 */ |
| 340 |
133 |
▶ | if (s->cmdoff < s->cmdlen) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->cmdoff < s->cmdlen) continue; |
0 |
133 |
133 |
|
| 341 |
133 |
| s->phase = cc_after_command(s->transport); |
| 342 |
133 |
| continue; |
| 343 |
|
| } |
| 344 |
|
| } else { |
| 345 |
|
| #if CC_HAVE_FD_PASSING |
| 346 |
12 |
| n = cc_send_fd(s->sock, s->scan_fd); |
| 347 |
|
| /* |
| 348 |
|
| # uncoverable branch false |
| 349 |
|
| # uncoverable condition right count:1 */ |
| 350 |
12 |
▶ | if (n >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= 0) { |
12 |
0 |
12 |
|
| 351 |
12 |
| progressed = 1; |
| 352 |
12 |
| s->phase = CC_PH_READ; |
| 353 |
12 |
| continue; |
| 354 |
|
| } |
| 355 |
|
| #else |
| 356 |
|
| cc_fail(s, CC_ERR_NOFDPASS, |
| 357 |
|
| "this platform cannot pass file descriptors", NULL); |
| 358 |
|
| return CC_STEP_DONE; |
| 359 |
|
| #endif |
| 360 |
|
| } |
| 361 |
|
| /* |
| 362 |
|
| # uncoverable branch true |
| 363 |
|
| # uncoverable condition left count:1 */ |
| 364 |
1 |
▶ | if (cc_would_block(errno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_would_block(errno)) { |
0 |
1 |
1 |
|
| 365 |
0 |
| s->want = CC_WANT_WRITE; /* # uncoverable statement */ |
| 366 |
0 |
| goto more; /* # uncoverable statement */ |
| 367 |
|
| } |
| 368 |
1 |
▶ | cc_fail(s, cc_errno_is_closed(errno) ? CC_ERR_CLOSED : CC_ERR_IO, /* # uncoverable condition right count:1 */ |
Branch: 1 branch
| Branch | True | False | Total |
| cc_fail(s, cc_errno_is_closed(errno) ? CC_ERR_CLOSED : CC_ERR_IO, /* # uncoverable condition right count:1 */ |
1 |
0 |
1 |
|
| 369 |
1 |
| "send", strerror(errno)); |
| 370 |
1 |
| return CC_STEP_DONE; |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
49582 |
| case CC_PH_STREAM: { |
| 374 |
|
| int r; |
| 375 |
|
| |
| 376 |
|
| /* A reply arriving mid-write is a VERDICT. Checked before |
| 377 |
|
| * every chunk, exactly as the blocking path did. */ |
| 378 |
24804 |
▶ | if (cc_reply_pending(s->sock)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_reply_pending(s->sock)) { |
10 |
24794 |
24804 |
|
| 379 |
10 |
| progressed = 1; |
| 380 |
10 |
| s->phase = CC_PH_READ; |
| 381 |
10 |
| continue; |
| 382 |
|
| } |
| 383 |
|
| |
| 384 |
|
| /* The end of the input is a chunk of length zero, which is |
| 385 |
|
| * exactly how the protocol spells the end of the stream. */ |
| 386 |
24794 |
▶ | if (!s->have_chunk) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->have_chunk) { |
24792 |
2 |
24794 |
|
| 387 |
24792 |
| const char *p = NULL; |
| 388 |
24792 |
| ssize_t n = cc_source_next(&s->src, &p, &s->err); |
| 389 |
24792 |
▶ | if (n < 0) { cc_fail(s, CC_ERR_IO, NULL, NULL); return CC_STEP_DONE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { cc_fail(s, CC_ERR_IO, NULL, NULL); return CC_STEP_DONE; } |
1 |
24791 |
24792 |
|
| 390 |
24791 |
| s->chunk = p; |
| 391 |
24791 |
| s->chunklen = (size_t)n; |
| 392 |
24791 |
| s->chunkoff = 0; |
| 393 |
24791 |
| s->hdr[0] = (unsigned char)((s->chunklen >> 24) & 0xff); |
| 394 |
24791 |
| s->hdr[1] = (unsigned char)((s->chunklen >> 16) & 0xff); |
| 395 |
24791 |
| s->hdr[2] = (unsigned char)((s->chunklen >> 8) & 0xff); |
| 396 |
24791 |
| s->hdr[3] = (unsigned char)( s->chunklen & 0xff); |
| 397 |
24791 |
| s->hdroff = 0; |
| 398 |
24791 |
| s->have_chunk = 1; |
| 399 |
24791 |
| progressed = 1; |
| 400 |
|
| } |
| 401 |
|
| |
| 402 |
24793 |
| r = cc_send_some(s, (const char *)s->hdr, 4, &s->hdroff, &progressed); |
| 403 |
24793 |
▶ | if (r == 1) r = cc_send_some(s, s->chunk, s->chunklen, &s->chunkoff, &progressed); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 1) r = cc_send_some(s, s->chunk, s->chunklen, &s->chunkoff, &progressed); |
24786 |
7 |
24793 |
|
| 404 |
24793 |
▶ | if (r == 0) { s->want = CC_WANT_WRITE; goto more; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) { s->want = CC_WANT_WRITE; goto more; } |
13 |
24780 |
24793 |
|
| 405 |
|
| /* |
| 406 |
|
| # uncoverable branch true class:ignore_covered_err |
| 407 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 408 |
24780 |
▶ | if (r == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == -1) { |
2 |
24778 |
24780 |
|
| 409 |
|
| /* clamd hung up mid-stream. Whatever it had to say is |
| 410 |
|
| * still there to be read, and that is the verdict. */ |
| 411 |
2 |
| s->phase = CC_PH_READ; /* # uncoverable statement class:ignore_covered_err */ |
| 412 |
2 |
| progressed = 1; /* # uncoverable statement class:ignore_covered_err */ |
| 413 |
2 |
| continue; /* # uncoverable statement class:ignore_covered_err */ |
| 414 |
|
| } |
| 415 |
|
| /* |
| 416 |
|
| # uncoverable branch true |
| 417 |
|
| # uncoverable condition left count:1 */ |
| 418 |
24778 |
▶ | if (r < 0) return CC_STEP_DONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r < 0) return CC_STEP_DONE; |
0 |
24778 |
24778 |
|
| 419 |
|
| |
| 420 |
24778 |
| s->have_chunk = 0; |
| 421 |
24778 |
▶ | if (s->chunklen == 0) s->phase = CC_PH_READ; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->chunklen == 0) s->phase = CC_PH_READ; |
77 |
24701 |
24778 |
|
| 422 |
24778 |
| continue; |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
10262 |
| case CC_PH_READ: { |
| 426 |
10262 |
▶ | char term = (s->t.frame == CC_FRAME_N) ? '\n' : '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| char term = (s->t.frame == CC_FRAME_N) ? '\n' : '\0'; |
6 |
10256 |
10262 |
|
| 427 |
10262 |
▶ | size_t hard = s->t.reply_max ? s->t.reply_max : (1024 * 1024); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t hard = s->t.reply_max ? s->t.reply_max : (1024 * 1024); |
10258 |
4 |
10262 |
|
| 428 |
|
| ssize_t n; |
| 429 |
|
| size_t room; |
| 430 |
|
| |
| 431 |
|
| /* INVARIANT: replycap never exceeds reply_max + 1, so the |
| 432 |
|
| * ceiling binds on every read rather than only on the reads |
| 433 |
|
| * that happen to grow the buffer. Getting this wrong makes |
| 434 |
|
| * reply_max unenforced for any reply smaller than the initial |
| 435 |
|
| * allocation, which is most of them. */ |
| 436 |
10262 |
▶ | if (!s->reply) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->reply) { |
131 |
10131 |
10262 |
|
| 437 |
131 |
| s->replycap = 512; |
| 438 |
131 |
▶ | if (s->replycap > hard + 1) s->replycap = hard + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->replycap > hard + 1) s->replycap = hard + 1; |
2 |
129 |
131 |
|
| 439 |
131 |
| s->reply = (char *)malloc(s->replycap); |
| 440 |
|
| /* |
| 441 |
|
| # uncoverable branch true |
| 442 |
|
| # uncoverable condition left count:1 */ |
| 443 |
131 |
▶ | if (!s->reply) { cc_fail(s, CC_ERR_IO, "out of memory", NULL); return CC_STEP_DONE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->reply) { cc_fail(s, CC_ERR_IO, "out of memory", NULL); return CC_STEP_DONE; } |
0 |
131 |
131 |
|
| 444 |
131 |
| s->replylen = 0; |
| 445 |
|
| } |
| 446 |
|
| |
| 447 |
10262 |
▶ | if (s->replylen >= hard) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->replylen >= hard) { |
4 |
10258 |
10262 |
|
| 448 |
4 |
| cc_fail(s, CC_ERR_TOOBIG, "reply exceeded the configured ceiling", NULL); |
| 449 |
4 |
| return CC_STEP_DONE; |
| 450 |
|
| } |
| 451 |
10258 |
▶ | if (s->replylen + 1 >= s->replycap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->replylen + 1 >= s->replycap) { |
15 |
10243 |
10258 |
|
| 452 |
15 |
| size_t ncap = s->replycap * 2; |
| 453 |
|
| char *nb; |
| 454 |
15 |
▶ | if (ncap > hard + 1) ncap = hard + 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ncap > hard + 1) ncap = hard + 1; |
2 |
13 |
15 |
|
| 455 |
15 |
| nb = (char *)realloc(s->reply, ncap); |
| 456 |
|
| /* |
| 457 |
|
| # uncoverable branch true |
| 458 |
|
| # uncoverable condition left count:1 */ |
| 459 |
15 |
▶ | if (!nb) { cc_fail(s, CC_ERR_IO, "out of memory", NULL); return CC_STEP_DONE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nb) { cc_fail(s, CC_ERR_IO, "out of memory", NULL); return CC_STEP_DONE; } |
0 |
15 |
15 |
|
| 460 |
15 |
| s->reply = nb; s->replycap = ncap; |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
10258 |
| room = s->replycap - s->replylen - 1; |
| 464 |
|
| |
| 465 |
10258 |
| n = cc_recv(s->sock, s->reply + s->replylen, room); |
| 466 |
10258 |
▶ | if (n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) { |
124 |
10134 |
10258 |
|
| 467 |
124 |
| size_t start = s->replylen, i; |
| 468 |
124 |
| s->replylen += (size_t)n; |
| 469 |
124 |
| progressed = 1; |
| 470 |
|
| /* the terminator can land anywhere in what just arrived */ |
| 471 |
75686 |
▶ | for (i = start; i < s->replylen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < s->replylen; i++) { |
75666 |
20 |
75686 |
|
| 472 |
75666 |
▶ | if (s->reply[i] == term) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->reply[i] == term) { |
104 |
75562 |
75666 |
|
| 473 |
104 |
| s->replylen = i; |
| 474 |
104 |
| cc_finish(s); |
| 475 |
104 |
| return CC_STEP_DONE; |
| 476 |
|
| } |
| 477 |
|
| } |
| 478 |
20 |
| continue; |
| 479 |
|
| } |
| 480 |
10134 |
▶ | if (n == 0 || cc_errno_is_closed(errno)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 0 || cc_errno_is_closed(errno)) { |
10131 |
3 |
10134 |
| if (n == 0 || cc_errno_is_closed(errno)) { |
11 |
10120 |
10131 |
|
| 481 |
|
| /* A reset IS the close being looked for: when the peer |
| 482 |
|
| * hangs up with our bytes still unread - which is |
| 483 |
|
| * exactly what clamd does at StreamMaxLength - Linux |
| 484 |
|
| * reports ECONNRESET here, not EOF. Same event, same |
| 485 |
|
| * handling, or the cut shows up as a generic IO error |
| 486 |
|
| * on every Linux box. |
| 487 |
|
| * |
| 488 |
|
| * clamd does close after most replies, so a |
| 489 |
|
| * complete-looking reply that simply ended is still a |
| 490 |
|
| * reply; an empty one is not. */ |
| 491 |
14 |
▶ | if (s->replylen > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->replylen > 0) { |
1 |
13 |
14 |
|
| 492 |
1 |
| cc_finish(s); |
| 493 |
1 |
| return CC_STEP_DONE; |
| 494 |
|
| } |
| 495 |
13 |
▶ | if (s->transport == CC_TRANSPORT_NONE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->transport == CC_TRANSPORT_NONE) |
2 |
11 |
13 |
|
| 496 |
2 |
| cc_fail(s, CC_ERR_CLOSED, "peer closed without a reply", NULL); |
| 497 |
|
| else |
| 498 |
11 |
| cc_fail(s, CC_ERR_STREAMCUT, |
| 499 |
|
| "clamd closed the stream without a verdict " |
| 500 |
|
| "(usually StreamMaxLength exceeded)", NULL); |
| 501 |
13 |
| return CC_STEP_DONE; |
| 502 |
|
| } |
| 503 |
|
| /* |
| 504 |
|
| # uncoverable branch false |
| 505 |
|
| # uncoverable condition right count:1 */ |
| 506 |
10120 |
▶ | if (cc_would_block(errno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_would_block(errno)) { |
10120 |
0 |
10120 |
|
| 507 |
10120 |
| s->want = CC_WANT_READ; |
| 508 |
10120 |
| goto more; |
| 509 |
|
| } |
| 510 |
0 |
| cc_fail(s, CC_ERR_IO, "recv", strerror(errno)); /* # uncoverable statement */ |
| 511 |
0 |
| return CC_STEP_DONE; /* # uncoverable statement */ |
| 512 |
|
| } |
| 513 |
|
| |
| 514 |
0 |
| default: /* # uncoverable statement */ |
| 515 |
0 |
| s->want = CC_WANT_NONE; /* # uncoverable statement */ |
| 516 |
0 |
| return CC_STEP_DONE; /* # uncoverable statement */ |
| 517 |
|
| } |
| 518 |
|
| } |
| 519 |
|
| |
| 520 |
10133 |
| more: |
| 521 |
10133 |
▶ | if (progressed) s->idle_steps = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (progressed) s->idle_steps = 0; |
130 |
10003 |
10133 |
|
| 522 |
10003 |
▶ | else if (++s->idle_steps > CC_MAX_IDLE_STEPS) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (++s->idle_steps > CC_MAX_IDLE_STEPS) { |
1 |
10002 |
10003 |
|
| 523 |
1 |
| cc_fail(s, CC_ERR_IO, |
| 524 |
|
| "scan made no progress over many steps; the driver is spinning", |
| 525 |
|
| NULL); |
| 526 |
1 |
| return CC_STEP_DONE; |
| 527 |
|
| } |
| 528 |
10132 |
| return CC_STEP_MORE; |
| 529 |
|
| } |
| 530 |
|
| |
| 531 |
|
| /* The verdict, parsed once and kept. */ |
| 532 |
89 |
| static const cc_verdict *cc_scan_verdict(cc_scan *s) { |
| 533 |
89 |
▶ | if (!s->verdict_ready) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->verdict_ready) { |
31 |
58 |
89 |
|
| 534 |
31 |
▶ | if (s->rc == CC_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->rc == CC_OK) |
21 |
10 |
31 |
|
| 535 |
21 |
| cc_parse_reply(s->reply, s->replylen, &s->verdict); |
| 536 |
|
| else |
| 537 |
10 |
| cc_verdict_from_error(s->rc, &s->verdict); |
| 538 |
31 |
| s->verdict_ready = 1; |
| 539 |
|
| } |
| 540 |
89 |
| return &s->verdict; |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
|
| /* Cancellation. |
| 544 |
|
| * |
| 545 |
|
| * The connection is CLOSED, never returned to anything. clamd is still |
| 546 |
|
| * going to answer, and a connection carrying an unread verdict handed to |
| 547 |
|
| * an unrelated scan would give that scan this one's answer - a |
| 548 |
|
| * cross-request result confusion bug, and the reason phase 0 chose a |
| 549 |
|
| * connection per scan in the first place. |
| 550 |
|
| */ |
| 551 |
5 |
| static void cc_scan_cancel(cc_scan *s) { |
| 552 |
5 |
| cc_fail(s, CC_ERR_CLOSED, "scan cancelled", NULL); |
| 553 |
5 |
| } |
| 554 |
|
| |
| 555 |
|
| /* The blocking driver: this same machine, under poll. |
| 556 |
|
| * |
| 557 |
|
| * There is no second copy of the protocol anywhere in this dist. Any bug |
| 558 |
|
| * fixed here is fixed on both paths, and the async path cannot rot from |
| 559 |
|
| * disuse because the blocking one exercises it on every call. |
| 560 |
|
| */ |
| 561 |
110 |
| static int cc_scan_run(cc_scan *s, char **out, size_t *outlen, cc_err *err) { |
| 562 |
110 |
| *out = NULL; *outlen = 0; |
| 563 |
|
| |
| 564 |
208 |
▶ | while (cc_scan_step(s) == CC_STEP_MORE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (cc_scan_step(s) == CC_STEP_MORE) { |
105 |
103 |
208 |
|
| 565 |
105 |
| int w = cc_wait(s->sock, s->want == CC_WANT_WRITE, s->deadline); |
| 566 |
105 |
▶ | if (w == 0) { cc_fail(s, CC_ERR_TIMEOUT, "timed out waiting for clamd", NULL); break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w == 0) { cc_fail(s, CC_ERR_TIMEOUT, "timed out waiting for clamd", NULL); break; } |
7 |
98 |
105 |
|
| 567 |
|
| /* |
| 568 |
|
| # uncoverable branch true |
| 569 |
|
| # uncoverable condition left count:1 */ |
| 570 |
98 |
▶ | if (w < 0) { cc_fail(s, CC_ERR_IO, "poll", strerror(errno)); break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w < 0) { cc_fail(s, CC_ERR_IO, "poll", strerror(errno)); break; } |
0 |
98 |
98 |
|
| 571 |
|
| } |
| 572 |
|
| |
| 573 |
110 |
| *err = s->err; |
| 574 |
110 |
▶ | if (s->rc == CC_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->rc == CC_OK) { |
93 |
17 |
110 |
|
| 575 |
93 |
| *out = s->reply; *outlen = s->replylen; |
| 576 |
93 |
| s->reply = NULL; /* ownership moves to the caller */ |
| 577 |
|
| } |
| 578 |
110 |
| return s->rc; |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
|
| /* connect, send one command, read one reply, close. Phase 0 decided on |
| 582 |
|
| * a connection per command: IDSESSION dies whole when any one command in |
| 583 |
|
| * it fails, taking every outstanding reply with it. */ |
| 584 |
40 |
| static int cc_roundtrip(const cc_target *t, const char *cmd, |
| 585 |
|
| char **out, size_t *outlen, cc_err *err) { |
| 586 |
|
| cc_scan sc; |
| 587 |
40 |
| int rc = cc_scan_command(&sc, t, cmd); |
| 588 |
40 |
▶ | if (rc == CC_OK) rc = cc_scan_run(&sc, out, outlen, err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == CC_OK) rc = cc_scan_run(&sc, out, outlen, err); |
31 |
9 |
40 |
|
| 589 |
9 |
| else *err = sc.err; |
| 590 |
40 |
| cc_scan_free(&sc); |
| 591 |
40 |
| return rc; |
| 592 |
|
| } |
| 593 |
|
| |
| 594 |
|
| #endif /* CLAMD_ASYNC_H */ |