| 1 |
|
| /* clamd_scan.h - scanning by descriptor. |
| 2 |
|
| * |
| 3 |
|
| * Phase 2 of plan_clamav_clamd. FILDES only; INSTREAM is phase 3. |
| 4 |
|
| * |
| 5 |
|
| * THE HANDSHAKE, captured from clamdscan --fdpass against a listener |
| 6 |
|
| * that dumps what it receives: |
| 7 |
|
| * |
| 8 |
|
| * write "zFILDES\0" the command, ALONE, with no ancillary data |
| 9 |
|
| * sendmsg "\0" + fd one NUL byte carrying SCM_RIGHTS |
| 10 |
|
| * |
| 11 |
|
| * Attaching the descriptor to the command itself - the obvious reading, |
| 12 |
|
| * and what every first attempt does - is rejected with |
| 13 |
|
| * |
| 14 |
|
| * PROTOCOL ERROR: ancillary data sent without FILDES. ERROR |
| 15 |
|
| * |
| 16 |
|
| * because clamd sees the control message on a read where FILDES has not |
| 17 |
|
| * been dispatched yet. Two messages, in this order. No document says so. |
| 18 |
|
| * |
| 19 |
|
| * Every control-message offset goes through the CMSG_* macros. The |
| 20 |
|
| * alignment rules are the sort of thing that works by accident on the |
| 21 |
|
| * platform you developed on and not on the next one. |
| 22 |
|
| */ |
| 23 |
|
| #ifndef CLAMD_SCAN_H |
| 24 |
|
| #define CLAMD_SCAN_H |
| 25 |
|
| |
| 26 |
|
| #include "clamav/clamd_conn.h" |
| 27 |
|
| #include "clamav/clamd_stream.h" |
| 28 |
|
| #include "clamav/clamd_async.h" |
| 29 |
|
| |
| 30 |
|
| /* CC_ERR_NOTREG and CC_ERR_NOFDPASS live in clamd_conn.h. They were |
| 31 |
|
| * defined here, below the includes, which meant clamd_async.h - included |
| 32 |
|
| * above - had already been parsed without them: the branch it takes on a |
| 33 |
|
| * platform that cannot pass descriptors raises NOFDPASS, and would not |
| 34 |
|
| * compile. */ |
| 35 |
|
| |
| 36 |
|
| /* Scan an open descriptor. |
| 37 |
|
| * |
| 38 |
|
| * The descriptor is BORROWED. This never closes it, and it never seeks |
| 39 |
|
| * it either: clamd scans the whole file regardless of where the |
| 40 |
|
| * descriptor happens to be positioned - measured at offsets 0, 10 and |
| 41 |
|
| * past EOF, all of which detected a signature at byte 0 - so there is |
| 42 |
|
| * nothing to correct, and correcting it would mutate something the |
| 43 |
|
| * caller owns. |
| 44 |
|
| * |
| 45 |
|
| * A descriptor that is not a regular file cannot mean what the caller |
| 46 |
|
| * thinks it means: a pipe has no length and can be read exactly once, a |
| 47 |
|
| * directory is not a document, and a socket is somebody else's stream. |
| 48 |
|
| * Refuse rather than hand clamd something it will answer about |
| 49 |
|
| * meaninglessly. |
| 50 |
|
| */ |
| 51 |
15 |
| static int cc_scan_fd(const cc_target *t, int fd, |
| 52 |
|
| char **out, size_t *outlen, int *transport, cc_err *err) { |
| 53 |
|
| cc_scan sc; |
| 54 |
|
| int rc, mode; |
| 55 |
|
| |
| 56 |
15 |
| err->code = CC_OK; err->msg[0] = '\0'; |
| 57 |
15 |
| *out = NULL; *outlen = 0; |
| 58 |
15 |
| *transport = CC_TRANSPORT_NONE; |
| 59 |
|
| |
| 60 |
|
| /* FILDES over TCP is not an error at clamd's end - it is a HANG. |
| 61 |
|
| * Phase 0: the command produces no reply, no diagnostic, and the |
| 62 |
|
| * connection stays open forever. clamd will never tell a client it |
| 63 |
|
| * asked for something impossible, so the client has to know. |
| 64 |
|
| * |
| 65 |
|
| * Same on any platform without SCM_RIGHTS. Both fall back to |
| 66 |
|
| * INSTREAM, which is correct but expensive: the file now crosses the |
| 67 |
|
| * socket in full. That is why the transport is reported. */ |
| 68 |
30 |
| mode = (CC_HAVE_FD_PASSING && t->path) ? CC_TRANSPORT_FILDES |
| 69 |
15 |
▶ | : CC_TRANSPORT_INSTREAM; |
Branch: 1 branch
| Branch | True | False | Total |
| : CC_TRANSPORT_INSTREAM; |
13 |
2 |
15 |
|
| 70 |
|
| |
| 71 |
|
| { |
| 72 |
|
| cc_stat_t st; |
| 73 |
15 |
▶ | if (cc_fstat(fd, &st) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cc_fstat(fd, &st) < 0) { |
1 |
14 |
15 |
|
| 74 |
1 |
| cc_err_set(err, CC_ERR_IO, "fstat", strerror(errno)); |
| 75 |
6 |
| return CC_ERR_IO; |
| 76 |
|
| } |
| 77 |
14 |
▶ | if (!CC_S_ISREG(st.st_mode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!CC_S_ISREG(st.st_mode)) { |
4 |
10 |
14 |
|
| 78 |
4 |
| cc_err_set(err, CC_ERR_NOTREG, "not a regular file", NULL); |
| 79 |
4 |
| return CC_ERR_NOTREG; |
| 80 |
|
| } |
| 81 |
10 |
▶ | if (t->max_size && (size_t)st.st_size > t->max_size) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->max_size && (size_t)st.st_size > t->max_size) { |
2 |
8 |
10 |
| if (t->max_size && (size_t)st.st_size > t->max_size) { |
1 |
1 |
2 |
|
| 82 |
1 |
| cc_err_set(err, CC_ERR_TOOBIGLOC, |
| 83 |
|
| "larger than max_size; refused without scanning", NULL); |
| 84 |
1 |
| return CC_ERR_TOOBIGLOC; |
| 85 |
|
| } |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
9 |
▶ | if (mode == CC_TRANSPORT_FILDES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mode == CC_TRANSPORT_FILDES) |
7 |
2 |
9 |
|
| 89 |
7 |
| rc = cc_scan_start(&sc, t, mode, fd, 0, NULL, 0, -1); |
| 90 |
|
| else |
| 91 |
2 |
| rc = cc_scan_start(&sc, t, mode, -1, 0, NULL, 0, fd); |
| 92 |
|
| |
| 93 |
9 |
▶ | 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); |
8 |
1 |
9 |
|
| 94 |
1 |
| else *err = sc.err; |
| 95 |
|
| |
| 96 |
9 |
| *transport = sc.transport; |
| 97 |
9 |
| cc_scan_free(&sc); |
| 98 |
9 |
| return rc; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* Scan a path. |
| 102 |
|
| * |
| 103 |
|
| * The path is opened HERE and the descriptor is what travels. This is |
| 104 |
|
| * NOT clamd's SCAN command, which hands clamd a path for clamd to |
| 105 |
|
| * resolve - and which therefore needs clamd to have permission on it. |
| 106 |
|
| * The whole point of FILDES is that it does not. |
| 107 |
|
| */ |
| 108 |
7 |
| static int cc_scan_path(const cc_target *t, const char *path, |
| 109 |
|
| char **out, size_t *outlen, int *transport, cc_err *err) { |
| 110 |
|
| int fd, rc; |
| 111 |
|
| |
| 112 |
7 |
| fd = cc_open_ro(path); |
| 113 |
7 |
▶ | if (fd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { |
1 |
6 |
7 |
|
| 114 |
1 |
| *out = NULL; *outlen = 0; |
| 115 |
1 |
| *transport = CC_TRANSPORT_NONE; |
| 116 |
1 |
| cc_err_set(err, CC_ERR_IO, "open", strerror(errno)); |
| 117 |
1 |
| return CC_ERR_IO; |
| 118 |
|
| } |
| 119 |
6 |
| rc = cc_scan_fd(t, fd, out, outlen, transport, err); |
| 120 |
6 |
| cc_close_fd(fd); /* ours, so ours to close - on every path */ |
| 121 |
6 |
| return rc; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| #endif /* CLAMD_SCAN_H */ |