| 1 |
|
| #ifndef HM_PROXY_H |
| 2 |
|
| #define HM_PROXY_H |
| 3 |
|
| |
| 4 |
|
| /* The PROXY protocol (HAProxy's, v1 and v2), parsed off the first bytes of a |
| 5 |
|
| * connection on a listener that asked for it. Perl-free, like hm_parse.h, so |
| 6 |
|
| * it can be fuzzed standalone (tools/fuzz/fuzz_proxy.c) and driven from C by |
| 7 |
|
| * the selftest below. |
| 8 |
|
| * |
| 9 |
|
| * The contract with the caller (hm_proxy_readable in hm_core.h): the caller |
| 10 |
|
| * PEEKS at the socket, hands the prefix here, and only when this returns a |
| 11 |
|
| * complete length does it read exactly that many bytes. So the parser never |
| 12 |
|
| * sees more than one header, and the bytes behind the header - a ClientHello, |
| 13 |
|
| * an h2 preface, a request line - are never taken off the socket by it. */ |
| 14 |
|
| |
| 15 |
|
| #include <stddef.h> |
| 16 |
|
| #include <stdint.h> |
| 17 |
|
| #include <string.h> |
| 18 |
|
| #ifdef _WIN32 |
| 19 |
|
| #include <ws2tcpip.h> |
| 20 |
|
| #else |
| 21 |
|
| #include <arpa/inet.h> |
| 22 |
|
| #include <sys/socket.h> |
| 23 |
|
| #endif |
| 24 |
|
| |
| 25 |
|
| /* v1 is one text line of at most 107 bytes including its CRLF (the spec's |
| 26 |
|
| * figure); v2 is a 16-byte fixed header plus an address block and TLVs, and |
| 27 |
|
| * anything over this ceiling is refused rather than buffered. */ |
| 28 |
|
| #define HM_PP_V1_MAX 107 |
| 29 |
|
| #define HM_PP_MAX 4096 |
| 30 |
|
| |
| 31 |
|
| typedef struct { |
| 32 |
|
| int family; /* AF_INET, AF_INET6, or 0 = keep the accepted peer */ |
| 33 |
|
| unsigned char src[16]; /* the client's address, in network order */ |
| 34 |
|
| int src_port; |
| 35 |
|
| } hm_pp_result; |
| 36 |
|
| |
| 37 |
|
| static const unsigned char hm_pp_v2_sig[12] = |
| 38 |
|
| { 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x51, 0x55, 0x49, 0x54, 0x0a }; |
| 39 |
|
| |
| 40 |
|
| /* digits only, 0..65535 */ |
| 41 |
24 |
| static int hm_pp_port(const char *s, size_t n, int *out) { |
| 42 |
|
| size_t i; |
| 43 |
24 |
| long v = 0; |
| 44 |
|
| /* |
| 45 |
|
| # uncoverable branch true class:ignore_covered_err |
| 46 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 47 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 48 |
24 |
▶ | if (n == 0 || n > 5) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 0 || n > 5) return -1; |
24 |
0 |
24 |
| if (n == 0 || n > 5) return -1; |
0 |
24 |
24 |
|
| 49 |
93 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
69 |
24 |
93 |
|
| 50 |
|
| /* |
| 51 |
|
| # uncoverable branch true class:ignore_covered_err |
| 52 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 53 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 54 |
69 |
▶ | if (s[i] < '0' || s[i] > '9') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] < '0' || s[i] > '9') return -1; |
69 |
0 |
69 |
| if (s[i] < '0' || s[i] > '9') return -1; |
0 |
69 |
69 |
|
| 55 |
69 |
| v = v * 10 + (s[i] - '0'); |
| 56 |
|
| } |
| 57 |
24 |
▶ | if (v > 65535) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v > 65535) return -1; |
2 |
22 |
24 |
|
| 58 |
22 |
| *out = (int)v; |
| 59 |
22 |
| return 0; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| /* One token up to the next space (or the end), into a NUL-terminated buffer. */ |
| 63 |
78 |
| static size_t hm_pp_tok(const char *p, size_t n, char *out, size_t cap) { |
| 64 |
78 |
| size_t i = 0; |
| 65 |
532 |
▶ | while (i < n && p[i] != ' ') i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < n && p[i] != ' ') i++; |
516 |
16 |
532 |
| while (i < n && p[i] != ' ') i++; |
454 |
62 |
516 |
|
| 66 |
|
| /* |
| 67 |
|
| # uncoverable branch true class:ignore_covered_err |
| 68 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 69 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 70 |
78 |
▶ | if (i == 0 || i >= cap) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (i == 0 || i >= cap) return 0; |
78 |
0 |
78 |
| if (i == 0 || i >= cap) return 0; |
0 |
78 |
78 |
|
| 71 |
78 |
| memcpy(out, p, i); |
| 72 |
78 |
| out[i] = '\0'; |
| 73 |
78 |
| return i; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| /* Returns the header's length when it is complete and valid, 0 when more |
| 77 |
|
| * bytes are needed, -1 when the bytes cannot be a PROXY header. */ |
| 78 |
78 |
| static long hm_pp_parse(const unsigned char *p, size_t n, hm_pp_result *out) { |
| 79 |
78 |
| out->family = 0; |
| 80 |
78 |
| out->src_port = 0; |
| 81 |
|
| /* |
| 82 |
|
| # uncoverable branch true |
| 83 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 84 |
78 |
▶ | if (n == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 0; |
0 |
78 |
78 |
|
| 85 |
|
| |
| 86 |
|
| /* ---- v2: the binary signature ------------------------------------- */ |
| 87 |
78 |
▶ | if (n >= 1 && p[0] == 0x0d) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (n >= 1 && p[0] == 0x0d) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
78 |
0 |
78 |
| if (n >= 1 && p[0] == 0x0d) { /* # uncoverable condition right count:1 class:ignore_covered_err */ |
15 |
63 |
78 |
|
| 88 |
15 |
| size_t cmp = n < 12 ? n : 12; |
| 89 |
15 |
▶ | if (memcmp(p, hm_pp_v2_sig, cmp) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, hm_pp_v2_sig, cmp) != 0) { |
2 |
13 |
15 |
|
| 90 |
|
| /* a bare CR starts no v1 line either */ |
| 91 |
2 |
| return -1; |
| 92 |
|
| } |
| 93 |
|
| /* |
| 94 |
|
| # uncoverable branch true |
| 95 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 96 |
13 |
▶ | if (n < 16) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 16) return 0; |
0 |
13 |
13 |
|
| 97 |
|
| { |
| 98 |
13 |
| unsigned ver_cmd = p[12], fam_proto = p[13]; |
| 99 |
13 |
| size_t len = ((size_t)p[14] << 8) | p[15]; |
| 100 |
13 |
| size_t total = 16 + len; |
| 101 |
13 |
▶ | if ((ver_cmd >> 4) != 2) return -1; /* not version 2 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ver_cmd >> 4) != 2) return -1; /* not version 2 */ |
2 |
11 |
13 |
|
| 102 |
|
| /* |
| 103 |
|
| # uncoverable branch true |
| 104 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 105 |
11 |
▶ | if ((ver_cmd & 0x0f) > 1) return -1; /* LOCAL or PROXY */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ver_cmd & 0x0f) > 1) return -1; /* LOCAL or PROXY */ |
0 |
11 |
11 |
|
| 106 |
11 |
▶ | if (total > HM_PP_MAX) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total > HM_PP_MAX) return -1; |
2 |
9 |
11 |
|
| 107 |
9 |
▶ | if (n < total) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < total) return 0; |
2 |
7 |
9 |
|
| 108 |
7 |
▶ | if ((ver_cmd & 0x0f) == 0) return (long)total; /* LOCAL: keep peer */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((ver_cmd & 0x0f) == 0) return (long)total; /* LOCAL: keep peer */ |
3 |
4 |
7 |
|
| 109 |
4 |
▶ | if (fam_proto == 0x11) { /* TCP over IPv4 */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam_proto == 0x11) { /* TCP over IPv4 */ |
3 |
1 |
4 |
|
| 110 |
|
| /* |
| 111 |
|
| # uncoverable branch true |
| 112 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 113 |
3 |
▶ | if (len < 12) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 12) return -1; |
0 |
3 |
3 |
|
| 114 |
3 |
| out->family = AF_INET; |
| 115 |
3 |
| memcpy(out->src, p + 16, 4); |
| 116 |
3 |
| out->src_port = (p[24] << 8) | p[25]; |
| 117 |
|
| /* |
| 118 |
|
| # uncoverable branch false class:ignore_covered_err |
| 119 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 120 |
1 |
▶ | } else if (fam_proto == 0x21) { /* TCP over IPv6 */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (fam_proto == 0x21) { /* TCP over IPv6 */ |
1 |
0 |
1 |
|
| 121 |
|
| /* |
| 122 |
|
| # uncoverable branch true |
| 123 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 124 |
1 |
▶ | if (len < 36) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < 36) return -1; |
0 |
1 |
1 |
|
| 125 |
1 |
| out->family = AF_INET6; |
| 126 |
1 |
| memcpy(out->src, p + 16, 16); |
| 127 |
1 |
| out->src_port = (p[48] << 8) | p[49]; |
| 128 |
|
| } |
| 129 |
|
| /* UNSPEC, UDP, UNIX: the peer stays what accept said; TLVs are |
| 130 |
|
| * skipped by the length */ |
| 131 |
4 |
| return (long)total; |
| 132 |
|
| } |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| /* ---- v1: "PROXY TCP4|TCP6|UNKNOWN src dst sport dport\r\n" ----------- */ |
| 136 |
|
| { |
| 137 |
|
| static const char pfx[] = "PROXY "; |
| 138 |
63 |
| size_t cmp = n < 6 ? n : 6; |
| 139 |
|
| const unsigned char *eol; |
| 140 |
|
| size_t linelen, i; |
| 141 |
|
| char fam[8], a1[48], a2[48], p1[8], p2[8]; |
| 142 |
|
| size_t l; |
| 143 |
63 |
▶ | if (memcmp(p, pfx, cmp) != 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, pfx, cmp) != 0) return -1; |
4 |
59 |
63 |
|
| 144 |
|
| /* |
| 145 |
|
| # uncoverable branch true |
| 146 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 147 |
59 |
▶ | if (n < 6) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 6) return 0; |
0 |
59 |
59 |
|
| 148 |
59 |
| eol = NULL; |
| 149 |
1466 |
▶ | for (i = 0; i + 1 < n && i + 1 < HM_PP_V1_MAX; i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i + 1 < n && i + 1 < HM_PP_V1_MAX; i++) |
1427 |
39 |
1466 |
| for (i = 0; i + 1 < n && i + 1 < HM_PP_V1_MAX; i++) |
1425 |
2 |
1427 |
|
| 150 |
1425 |
▶ | if (p[i] == '\r' && p[i + 1] == '\n') { eol = p + i; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (p[i] == '\r' && p[i + 1] == '\n') { eol = p + i; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
18 |
1407 |
1425 |
| if (p[i] == '\r' && p[i + 1] == '\n') { eol = p + i; break; } /* # uncoverable condition right count:2 class:ignore_covered_err */ |
18 |
0 |
18 |
|
| 151 |
59 |
▶ | if (!eol) return n >= HM_PP_V1_MAX ? -1 : 0; /* no CRLF within 107 */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!eol) return n >= HM_PP_V1_MAX ? -1 : 0; /* no CRLF within 107 */ |
41 |
18 |
59 |
| if (!eol) return n >= HM_PP_V1_MAX ? -1 : 0; /* no CRLF within 107 */ |
2 |
39 |
41 |
|
| 152 |
18 |
| linelen = (size_t)(eol - p) + 2; |
| 153 |
|
| /* the five tokens after "PROXY " */ |
| 154 |
|
| { |
| 155 |
18 |
| const char *q = (const char *)p + 6, *end = (const char *)eol; |
| 156 |
|
| /* |
| 157 |
|
| # uncoverable branch true |
| 158 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 159 |
18 |
▶ | if (!(l = hm_pp_tok(q, (size_t)(end - q), fam, sizeof fam))) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(l = hm_pp_tok(q, (size_t)(end - q), fam, sizeof fam))) return -1; |
0 |
18 |
18 |
|
| 160 |
18 |
| q += l; |
| 161 |
18 |
▶ | if (strcmp(fam, "UNKNOWN") == 0) return (long)linelen; /* keep peer */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(fam, "UNKNOWN") == 0) return (long)linelen; /* keep peer */ |
3 |
15 |
18 |
|
| 162 |
|
| /* |
| 163 |
|
| # uncoverable branch true class:ignore_covered_err |
| 164 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 165 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 166 |
15 |
▶ | if (strcmp(fam, "TCP4") != 0 && strcmp(fam, "TCP6") != 0) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (strcmp(fam, "TCP4") != 0 && strcmp(fam, "TCP6") != 0) return -1; |
4 |
11 |
15 |
| if (strcmp(fam, "TCP4") != 0 && strcmp(fam, "TCP6") != 0) return -1; |
0 |
4 |
4 |
|
| 167 |
|
| /* |
| 168 |
|
| # uncoverable branch true |
| 169 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 170 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 171 |
15 |
▶ | if (q >= end || *q != ' ') return -1; q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= end || *q != ' ') return -1; q++; |
15 |
0 |
15 |
| if (q >= end || *q != ' ') return -1; q++; |
0 |
15 |
15 |
|
| 172 |
|
| /* |
| 173 |
|
| # uncoverable branch true class:ignore_covered_err |
| 174 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 175 |
15 |
▶ | if (!(l = hm_pp_tok(q, (size_t)(end - q), a1, sizeof a1))) return -1; q += l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(l = hm_pp_tok(q, (size_t)(end - q), a1, sizeof a1))) return -1; q += l; |
0 |
15 |
15 |
|
| 176 |
|
| /* |
| 177 |
|
| # uncoverable branch true |
| 178 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 179 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 180 |
15 |
▶ | if (q >= end || *q != ' ') return -1; q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= end || *q != ' ') return -1; q++; |
15 |
0 |
15 |
| if (q >= end || *q != ' ') return -1; q++; |
0 |
15 |
15 |
|
| 181 |
|
| /* |
| 182 |
|
| # uncoverable branch true class:ignore_covered_err |
| 183 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 184 |
15 |
▶ | if (!(l = hm_pp_tok(q, (size_t)(end - q), a2, sizeof a2))) return -1; q += l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(l = hm_pp_tok(q, (size_t)(end - q), a2, sizeof a2))) return -1; q += l; |
0 |
15 |
15 |
|
| 185 |
|
| /* |
| 186 |
|
| # uncoverable branch true |
| 187 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 188 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 189 |
15 |
▶ | if (q >= end || *q != ' ') return -1; q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= end || *q != ' ') return -1; q++; |
15 |
0 |
15 |
| if (q >= end || *q != ' ') return -1; q++; |
0 |
15 |
15 |
|
| 190 |
|
| /* |
| 191 |
|
| # uncoverable branch true class:ignore_covered_err |
| 192 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 193 |
15 |
▶ | if (!(l = hm_pp_tok(q, (size_t)(end - q), p1, sizeof p1))) return -1; q += l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(l = hm_pp_tok(q, (size_t)(end - q), p1, sizeof p1))) return -1; q += l; |
0 |
15 |
15 |
|
| 194 |
|
| /* |
| 195 |
|
| # uncoverable branch true |
| 196 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 197 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 198 |
15 |
▶ | if (q >= end || *q != ' ') return -1; q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= end || *q != ' ') return -1; q++; |
15 |
0 |
15 |
| if (q >= end || *q != ' ') return -1; q++; |
0 |
15 |
15 |
|
| 199 |
|
| /* |
| 200 |
|
| # uncoverable branch true count:1 |
| 201 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 202 |
15 |
▶ | if (!(l = hm_pp_tok(q, (size_t)(end - q), p2, sizeof p2))) return -1; q += l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(l = hm_pp_tok(q, (size_t)(end - q), p2, sizeof p2))) return -1; q += l; |
0 |
15 |
15 |
|
| 203 |
15 |
▶ | if (q != end) return -1; /* trailing junk */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (q != end) return -1; /* trailing junk */ |
2 |
13 |
15 |
|
| 204 |
13 |
▶ | if (hm_pp_port(p1, strlen(p1), &out->src_port) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_port(p1, strlen(p1), &out->src_port) < 0) return -1; |
2 |
11 |
13 |
|
| 205 |
|
| /* |
| 206 |
|
| # uncoverable branch true |
| 207 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 208 |
11 |
▶ | { int dp; if (hm_pp_port(p2, strlen(p2), &dp) < 0) return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| { int dp; if (hm_pp_port(p2, strlen(p2), &dp) < 0) return -1; } |
0 |
11 |
11 |
|
| 209 |
11 |
▶ | if (fam[3] == '4') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam[3] == '4') { |
7 |
4 |
11 |
|
| 210 |
|
| unsigned char tmp[4]; |
| 211 |
|
| /* |
| 212 |
|
| # uncoverable branch true |
| 213 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 214 |
7 |
▶ | if (inet_pton(AF_INET, a1, out->src) != 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (inet_pton(AF_INET, a1, out->src) != 1) return -1; |
0 |
7 |
7 |
|
| 215 |
|
| /* |
| 216 |
|
| # uncoverable branch true |
| 217 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 218 |
7 |
▶ | if (inet_pton(AF_INET, a2, tmp) != 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (inet_pton(AF_INET, a2, tmp) != 1) return -1; |
0 |
7 |
7 |
|
| 219 |
7 |
| out->family = AF_INET; |
| 220 |
|
| } else { |
| 221 |
|
| unsigned char tmp[16]; |
| 222 |
|
| /* |
| 223 |
|
| # uncoverable branch true |
| 224 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 225 |
4 |
▶ | if (inet_pton(AF_INET6, a1, out->src) != 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (inet_pton(AF_INET6, a1, out->src) != 1) return -1; |
0 |
4 |
4 |
|
| 226 |
|
| /* |
| 227 |
|
| # uncoverable branch true |
| 228 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 229 |
4 |
▶ | if (inet_pton(AF_INET6, a2, tmp) != 1) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (inet_pton(AF_INET6, a2, tmp) != 1) return -1; |
0 |
4 |
4 |
|
| 230 |
4 |
| out->family = AF_INET6; |
| 231 |
|
| } |
| 232 |
|
| } |
| 233 |
11 |
| return (long)linelen; |
| 234 |
|
| } |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
|
| /* Driven from C by Hyperman::_proxy_selftest (t/61). Returns the number of |
| 238 |
|
| * the first failing step, or 0. Hostile inputs on purpose: these are the |
| 239 |
|
| * bytes a parser gets from a port open to the internet. */ |
| 240 |
2 |
| static int hm_proxy_selftest(void) { |
| 241 |
|
| hm_pp_result r; |
| 242 |
|
| unsigned char v2[64]; |
| 243 |
|
| /* 1: v1 TCP4 */ |
| 244 |
2 |
| { const char *s = "PROXY TCP4 192.0.2.1 198.51.100.2 54321 443\r\nGET"; |
| 245 |
2 |
| long n = hm_pp_parse((const unsigned char *)s, strlen(s), &r); |
| 246 |
|
| /* |
| 247 |
|
| # uncoverable branch true class:ignore_covered_err |
| 248 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 249 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 250 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 251 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 252 |
|
| # uncoverable condition left count:5 class:ignore_covered_err */ |
| 253 |
2 |
▶ | if (n != 45 || r.family != AF_INET || r.src_port != 54321 |
Branch: 3 branches
| Branch | True | False | Total |
| if (n != 45 || r.family != AF_INET || r.src_port != 54321 |
2 |
0 |
2 |
| if (n != 45 || r.family != AF_INET || r.src_port != 54321 |
2 |
0 |
2 |
| if (n != 45 || r.family != AF_INET || r.src_port != 54321 |
2 |
0 |
2 |
|
| 254 |
2 |
▶ | || r.src[0] != 192 || r.src[3] != 1) return 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| || r.src[0] != 192 || r.src[3] != 1) return 1; } |
2 |
0 |
2 |
| || r.src[0] != 192 || r.src[3] != 1) return 1; } |
0 |
2 |
2 |
|
| 255 |
|
| /* 2: v1 TCP6 */ |
| 256 |
2 |
| { const char *s = "PROXY TCP6 2001:db8::1 2001:db8::2 1 2\r\n"; |
| 257 |
2 |
| long n = hm_pp_parse((const unsigned char *)s, strlen(s), &r); |
| 258 |
|
| /* |
| 259 |
|
| # uncoverable branch true class:ignore_covered_err |
| 260 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 261 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 262 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 263 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 264 |
|
| # uncoverable condition left count:5 class:ignore_covered_err */ |
| 265 |
2 |
▶ | if (n != (long)strlen(s) || r.family != AF_INET6 || r.src_port != 1 |
Branch: 3 branches
| Branch | True | False | Total |
| if (n != (long)strlen(s) || r.family != AF_INET6 || r.src_port != 1 |
2 |
0 |
2 |
| if (n != (long)strlen(s) || r.family != AF_INET6 || r.src_port != 1 |
2 |
0 |
2 |
| if (n != (long)strlen(s) || r.family != AF_INET6 || r.src_port != 1 |
2 |
0 |
2 |
|
| 266 |
2 |
▶ | || r.src[0] != 0x20 || r.src[15] != 1) return 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| || r.src[0] != 0x20 || r.src[15] != 1) return 2; } |
2 |
0 |
2 |
| || r.src[0] != 0x20 || r.src[15] != 1) return 2; } |
0 |
2 |
2 |
|
| 267 |
|
| /* 3: v1 UNKNOWN keeps the peer */ |
| 268 |
2 |
| { const char *s = "PROXY UNKNOWN\r\n"; |
| 269 |
|
| /* |
| 270 |
|
| # uncoverable branch true class:ignore_covered_err |
| 271 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 272 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 273 |
2 |
▶ | if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != 15 || r.family != 0) return 3; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != 15 || r.family != 0) return 3; } |
2 |
0 |
2 |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != 15 || r.family != 0) return 3; } |
0 |
2 |
2 |
|
| 274 |
|
| /* 4: a partial v1 line needs more */ |
| 275 |
2 |
| { const char *s = "PROXY TCP4 192.0.2.1 198.51."; |
| 276 |
|
| /* |
| 277 |
|
| # uncoverable branch true |
| 278 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 279 |
2 |
▶ | if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != 0) return 4; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != 0) return 4; } |
0 |
2 |
2 |
|
| 280 |
|
| /* 5: 107 bytes with no CRLF is invalid, not "more" */ |
| 281 |
2 |
| { unsigned char big[120]; memset(big, 'x', sizeof big); memcpy(big, "PROXY ", 6); |
| 282 |
|
| /* |
| 283 |
|
| # uncoverable branch true |
| 284 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 285 |
2 |
▶ | if (hm_pp_parse(big, sizeof big, &r) != -1) return 5; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse(big, sizeof big, &r) != -1) return 5; } |
0 |
2 |
2 |
|
| 286 |
|
| /* 6: not a PROXY line at all */ |
| 287 |
2 |
| { const char *s = "GET / HTTP/1.1\r\n"; |
| 288 |
|
| /* |
| 289 |
|
| # uncoverable branch true |
| 290 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 291 |
2 |
▶ | if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 6; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 6; } |
0 |
2 |
2 |
|
| 292 |
|
| /* 7: bad port */ |
| 293 |
2 |
| { const char *s = "PROXY TCP4 192.0.2.1 198.51.100.2 99999 443\r\n"; |
| 294 |
|
| /* |
| 295 |
|
| # uncoverable branch true |
| 296 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 297 |
2 |
▶ | if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 7; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 7; } |
0 |
2 |
2 |
|
| 298 |
|
| /* 8: trailing junk */ |
| 299 |
2 |
| { const char *s = "PROXY TCP4 192.0.2.1 198.51.100.2 1 2 3\r\n"; |
| 300 |
|
| /* |
| 301 |
|
| # uncoverable branch true |
| 302 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 303 |
2 |
▶ | if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 8; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse((const unsigned char *)s, strlen(s), &r) != -1) return 8; } |
0 |
2 |
2 |
|
| 304 |
|
| /* 9: v2 PROXY TCP4 with a TLV */ |
| 305 |
2 |
| memcpy(v2, hm_pp_v2_sig, 12); |
| 306 |
2 |
| v2[12] = 0x21; v2[13] = 0x11; v2[14] = 0; v2[15] = 12 + 6; |
| 307 |
2 |
| v2[16] = 10; v2[17] = 0; v2[18] = 0; v2[19] = 7; /* src 10.0.0.7 */ |
| 308 |
2 |
| v2[20] = 10; v2[21] = 0; v2[22] = 0; v2[23] = 1; /* dst */ |
| 309 |
2 |
| v2[24] = 0x1f; v2[25] = 0x90; /* 8080 */ |
| 310 |
2 |
| v2[26] = 0x00; v2[27] = 0x50; /* 80 */ |
| 311 |
2 |
| v2[28] = 0xee; v2[29] = 0; v2[30] = 3; v2[31] = 'a'; v2[32] = 'b'; v2[33] = 'c'; |
| 312 |
2 |
| { long n = hm_pp_parse(v2, 34, &r); |
| 313 |
|
| /* |
| 314 |
|
| # uncoverable branch true class:ignore_covered_err |
| 315 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 316 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 317 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 318 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 319 |
2 |
▶ | if (n != 34 || r.family != AF_INET || r.src_port != 8080 || r.src[3] != 7) return 9; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (n != 34 || r.family != AF_INET || r.src_port != 8080 || r.src[3] != 7) return 9; } |
2 |
0 |
2 |
| if (n != 34 || r.family != AF_INET || r.src_port != 8080 || r.src[3] != 7) return 9; } |
2 |
0 |
2 |
| if (n != 34 || r.family != AF_INET || r.src_port != 8080 || r.src[3] != 7) return 9; } |
2 |
0 |
2 |
| if (n != 34 || r.family != AF_INET || r.src_port != 8080 || r.src[3] != 7) return 9; } |
0 |
2 |
2 |
|
| 320 |
|
| /* 10: the same, truncated, needs more */ |
| 321 |
|
| /* |
| 322 |
|
| # uncoverable branch true |
| 323 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 324 |
2 |
▶ | if (hm_pp_parse(v2, 30, &r) != 0) return 10; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse(v2, 30, &r) != 0) return 10; |
0 |
2 |
2 |
|
| 325 |
|
| /* 11: v2 LOCAL keeps the peer */ |
| 326 |
2 |
| v2[12] = 0x20; v2[14] = 0; v2[15] = 0; |
| 327 |
|
| /* |
| 328 |
|
| # uncoverable branch true class:ignore_covered_err |
| 329 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 330 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 331 |
2 |
▶ | if (hm_pp_parse(v2, 16, &r) != 16 || r.family != 0) return 11; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hm_pp_parse(v2, 16, &r) != 16 || r.family != 0) return 11; |
2 |
0 |
2 |
| if (hm_pp_parse(v2, 16, &r) != 16 || r.family != 0) return 11; |
0 |
2 |
2 |
|
| 332 |
|
| /* 12: a version nibble other than 2 is refused */ |
| 333 |
2 |
| v2[12] = 0x31; |
| 334 |
|
| /* |
| 335 |
|
| # uncoverable branch true |
| 336 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 337 |
2 |
▶ | if (hm_pp_parse(v2, 16, &r) != -1) return 12; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse(v2, 16, &r) != -1) return 12; |
0 |
2 |
2 |
|
| 338 |
|
| /* 13: a length past the ceiling is refused */ |
| 339 |
2 |
| v2[12] = 0x21; v2[14] = 0xff; v2[15] = 0xff; |
| 340 |
|
| /* |
| 341 |
|
| # uncoverable branch true |
| 342 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 343 |
2 |
▶ | if (hm_pp_parse(v2, 16, &r) != -1) return 13; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse(v2, 16, &r) != -1) return 13; |
0 |
2 |
2 |
|
| 344 |
|
| /* 14: a CR that is not the signature is invalid */ |
| 345 |
2 |
| { const unsigned char s[] = { 0x0d, 0x0a, 0x0d, 0x0a, 0x01 }; |
| 346 |
|
| /* |
| 347 |
|
| # uncoverable branch true |
| 348 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 349 |
2 |
▶ | if (hm_pp_parse(s, sizeof s, &r) != -1) return 14; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_pp_parse(s, sizeof s, &r) != -1) return 14; } |
0 |
2 |
2 |
|
| 350 |
2 |
| return 0; |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| #endif /* HM_PROXY_H */ |