fc_pp_tlv() accepts the names SSL_VERSION, SSL_CN, SSL_CIPHER, SSL_SIG_ALG
and SSL_KEY_ALG since commit 2cc53ecc8 ("MINOR: sample: Add common TLV
types as constants for fc_pp_tlv") in 2.9-dev5, and the doc lists them,
but they never returned anything. These are second level TLVs, which the
spec places in the <sub_tlv> field of PP2_TYPE_SSL, and only the top-level
TLVs are stored in the connection's list where the fetch looks for them.
So behind a server using "send-proxy-v2-ssl", this is always empty:
http-request set-header x-tls-version %[fc_pp_tlv(SSL_VERSION)]
Let's search the value of PP2_TYPE_SSL as well when one of these five
types is requested. The receive path is unchanged and nothing is
allocated: the sample points into the stored value. The second level TLVs
are not checked on reception, so the search stops on the first one that
does not fit. Repeated ones are still returned one at a time, with
smp->ctx.a[1] keeping the last one returned.
The reg-test now looks these TLVs up by name and by number, and checks a
repeated one and a truncated one.
This should be backported as far as 3.0.
---
I found this while checking what a frontend can read from a header sent
with "send-proxy-v2-ssl". It is the same in master, 3.4, 3.2 and 3.0.
Only the five subtypes fc_pp_tlv() has names for are handled. 0x26 to
0x28 are in the spec but have no constant in the code yet, so I left them
out of a fix meant to be backported.
I went for a lookup in the fetch rather than storing the second level
TLVs on reception, so that the receive path is left alone. If you prefer
the other way, or would rather drop these names from the doc, I can send
a v2.
Tested on top of b0d3ef631, on macOS (clang 17) and Linux x86_64 (Rocky 9,
gcc 11.5): the reg-test fails without the fix and passes with it, also on
3.4, 3.2 and 3.0 where the patch applies as-is, and the full suite gives
the same results as without the patch. I also ran it under ASan with
truncated and otherwise malformed PP2_TYPE_SSL values.
Thanks,
Dongwon Jang
doc/configuration.txt | 2 +
reg-tests/sample_fetches/tlvs.vtc | 41 +++++++++++++++++-
src/connection.c | 71 ++++++++++++++++++++++++++++---
3 files changed, 106 insertions(+), 8 deletions(-)
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 265630f61..ba7be231d 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -26611,6 +26611,8 @@ fc_pp_tlv(<id>) : string
"SSL_CN": PP2_SUBTYPE_SSL_CN, "SSL_KEY_ALG": PP2_SUBTYPE_SSL_KEY_ALG,
"SSL_SIG_ALG": PP2_SUBTYPE_SSL_SIG_ALG,
"SSL_VERSION": PP2_SUBTYPE_SSL_VERSION, "UNIQUE_ID": PP2_TYPE_UNIQUE_ID.
+ The PP2_SUBTYPE_SSL_* ones are second level TLVs, they are looked up in the
+ value of the PP2_TYPE_SSL TLV.
The received value must be smaller or equal to 1024 bytes. This is done to
prevent potential DoS attacks. Values smaller or equal to 256 bytes will be
diff --git a/reg-tests/sample_fetches/tlvs.vtc
b/reg-tests/sample_fetches/tlvs.vtc
index d75473b6d..30eb5abd0 100644
--- a/reg-tests/sample_fetches/tlvs.vtc
+++ b/reg-tests/sample_fetches/tlvs.vtc
@@ -37,6 +37,28 @@ haproxy h1 -conf {
tcp-request content set-var(sess.third) str(yes) if { fc_pp_tlv(0xED)
-m str third }
http-after-response set-header echo6 %[var(sess.third)]
+ # The PP2_SUBTYPE_SSL_* types are second level TLVs found in the value
+ # of PP2_TYPE_SSL: they must be found by name and by number, and
+ # PP2_TYPE_SSL must still be returned as a whole.
+ http-after-response set-header echo7 %[fc_pp_tlv(SSL_VERSION)]
+ http-after-response set-header echo8 %[fc_pp_tlv(0x23)]
+ http-after-response set-header echo9 %[fc_pp_tlv(SSL),length]
+
+ # SSL_CN is sent twice below, for the same two checks as above on the
+ # occurrences of a second level TLV.
+ tcp-request content set-var(sess.subnomatch) str(no)
+ tcp-request content set-var(sess.subnomatch) str(yes) if {
fc_pp_tlv(SSL_CN) -m str does-not-match }
+ http-after-response set-header echo10 %[var(sess.subnomatch)]
+ tcp-request content set-var(sess.cn2) str(no)
+ tcp-request content set-var(sess.cn2) str(yes) if { fc_pp_tlv(SSL_CN)
-m str cn2 }
+ http-after-response set-header echo11 %[var(sess.cn2)]
+
+ # The last second level TLV, SSL_KEY_ALG, announces more data than what
+ # is left in PP2_TYPE_SSL: it must not be returned.
+ tcp-request content set-var(sess.keyalg) str(no)
+ tcp-request content set-var(sess.keyalg) str(yes) if {
fc_pp_tlv(SSL_KEY_ALG) -m found }
+ http-after-response set-header echo12 %[var(sess.keyalg)]
+
http-request return status 200
} -start
@@ -47,8 +69,8 @@ client c1 -connect ${h1_fe1_sock} {
sendhex "21"
# TCP4
sendhex "11"
- # length of the address (12) + length of the TLVs (14 + 10 + 9 + 131 + 3 *
8)
- sendhex "00 C8"
+ # length of the address (12) + length of the TLVs (14 + 10 + 9 + 131 + 60
+ 3 * 8)
+ sendhex "01 04"
# 127.0.0.1 42 127.0.0.1 1337
sendhex "7F 00 00 01 7F 00 00 01 00 2A 05 39"
@@ -67,6 +89,15 @@ client c1 -connect ${h1_fe1_sock} {
# custom type (0xEC) + length of the value (128, does not fit in pool) +
random data
sendhex "EC 00 80 3A D9 32 9B 11 A7 29 81 14 B2 33 F0 C2 0D 7A 53 D1 97 28
74 4B 78 8A D3 10 C4 B1 88 42 9C 63 8E 8B 8A A0 B4 B0 E7 9D 20 27 0F 1E 53 4D
33 F7 5A D0 91 3F B8 C9 E9 16 C4 61 C5 13 02 92 64 9D D4 22 5C 8E 4E 0B 2D 2D
7D 9F 5D 97 9B 25 C4 12 7D 21 75 C8 15 92 6B 64 F2 5F C0 A9 0F 9A 7D 0A 6D 68
79 F4 56 18 6F 23 45 2A 9B 36 34 3A 47 43 32 29 18 6F 23 45 2A 9B 36 34 3A 47
43 32 29 32 29"
+ # PP2_TYPE_SSL (0x20) + length of the value (57) + client (PP2_CLIENT_SSL)
+ verify (0),
+ # followed by its second level TLVs:
+ # PP2_SUBTYPE_SSL_VERSION (0x21) + length of the value + "TLSv1.3"
+ # PP2_SUBTYPE_SSL_CN (0x22) + length of the value + "cn1"
+ # PP2_SUBTYPE_SSL_CIPHER (0x23) + length of the value +
"TLS_AES_256_GCM_SHA384"
+ # PP2_SUBTYPE_SSL_CN (0x22) + length of the value + "cn2"
+ # PP2_SUBTYPE_SSL_KEY_ALG (0x25) + length of the value (16) + "RS",
truncated
+ sendhex "20 00 39 01 00 00 00 00 21 00 07 54 4C 53 76 31 2E 33 22 00 03
63 6E 31 23 00 16 54 4C 53 5F 41 45 53 5F 32 35 36 5F 47 43 4D 5F 53 48 41 33
38 34 22 00 03 63 6E 32 25 00 10 52 53"
+
# custom type (0xED) sent three times: "first", "secnd", "third"
sendhex "ED 00 05 66 69 72 73 74"
sendhex "ED 00 05 73 65 63 6E 64"
@@ -81,4 +112,10 @@ client c1 -connect ${h1_fe1_sock} {
expect resp.http.echo4 == 128
expect resp.http.echo5 == "no"
expect resp.http.echo6 == "yes"
+ expect resp.http.echo7 == "TLSv1.3"
+ expect resp.http.echo8 == "TLS_AES_256_GCM_SHA384"
+ expect resp.http.echo9 == 57
+ expect resp.http.echo10 == "no"
+ expect resp.http.echo11 == "yes"
+ expect resp.http.echo12 == "no"
} -run
diff --git a/src/connection.c b/src/connection.c
index 9500c00c4..7f60959ee 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -2726,12 +2726,48 @@ static int smp_check_tlv_type(struct arg *args, char
**err)
return 1;
}
-/* fetch an arbitrary TLV from a PROXY protocol v2 header */
+/* Looks for a second level TLV of type <type> in the value of the PP2_TYPE_SSL
+ * TLV <ssl>, starting after the one pointed to by <prev>, or from the first
+ * one when <prev> is NULL. Returns it, or NULL when none is found. The second
+ * level TLVs are not checked when the header is received, so the search stops
+ * on the first one which does not fit in the value.
+ */
+static struct tlv *conn_find_ssl_sub_tlv(struct conn_tlv_list *ssl,
+ const struct tlv *prev, int type)
+{
+ struct tlv *sub_tlv;
+ size_t ofs;
+
+ /* the second level TLVs are found in the <sub_tlv> field, after the
+ * <client> and <verify> ones
+ */
+ ofs = sizeof(struct tlv_ssl) - TLV_HEADER_SIZE;
+ if (prev)
+ ofs = (const char *)prev->value + get_tlv_length(prev) -
ssl->value;
+
+ while (ofs + TLV_HEADER_SIZE <= ssl->len) {
+ sub_tlv = (struct tlv *)(ssl->value + ofs);
+ ofs += TLV_HEADER_SIZE + get_tlv_length(sub_tlv);
+ if (ofs > ssl->len)
+ break;
+
+ if (sub_tlv->type == type)
+ return sub_tlv;
+ }
+
+ return NULL;
+}
+
+/* fetch an arbitrary TLV from a PROXY protocol v2 header. smp->ctx.a[0] holds
+ * the TLV returned by the previous call, or the PP2_TYPE_SSL TLV containing
the
+ * second level TLV it returned, which is then found in smp->ctx.a[1].
+ */
int smp_fetch_fc_pp_tlv(const struct arg *args, struct sample *smp, const char
*kw, void *private)
{
int idx;
struct connection *conn = NULL;
struct conn_tlv_list *conn_tlv = NULL;
+ struct tlv *sub_tlv = NULL;
conn = objt_conn(smp->sess->origin);
if (!conn)
@@ -2746,26 +2782,49 @@ int smp_fetch_fc_pp_tlv(const struct arg *args, struct
sample *smp, const char *
return 0;
idx = args[0].data.sint;
- conn_tlv = smp->ctx.p;
- if (conn_tlv) {
+ conn_tlv = smp->ctx.a[0];
+ sub_tlv = smp->ctx.a[1];
+ if (!conn_tlv)
+ conn_tlv = LIST_ELEM(conn->tlv_list.n, struct conn_tlv_list *,
list);
+ else if (!sub_tlv) {
/* resume *after* the TLV returned on the previous call:
* list_for_each_entry_from() starts on the element it is
given, so
* resuming on it would match it again and return it forever
*/
conn_tlv = LIST_NEXT(&conn_tlv->list, struct conn_tlv_list *,
list);
}
- else
- conn_tlv = LIST_ELEM(conn->tlv_list.n, struct conn_tlv_list *,
list);
+ /* otherwise resume in the same PP2_TYPE_SSL TLV, after the second level
+ * TLV returned on the previous call
+ */
list_for_each_entry_from(conn_tlv, &conn->tlv_list, list) {
if (conn_tlv->type == idx) {
smp->flags |= SMP_F_NOT_LAST;
smp->data.type = SMP_T_STR;
smp->data.u.str.area = conn_tlv->value;
smp->data.u.str.data = conn_tlv->len;
- smp->ctx.p = conn_tlv;
+ smp->ctx.a[0] = conn_tlv;
+ smp->ctx.a[1] = NULL;
return 1;
}
+
+ /* The PP2_SUBTYPE_SSL_* types are not in the list, they are
second
+ * level TLVs found in the value of PP2_TYPE_SSL.
+ */
+ if (conn_tlv->type == PP2_TYPE_SSL &&
+ idx >= PP2_SUBTYPE_SSL_VERSION && idx <=
PP2_SUBTYPE_SSL_KEY_ALG) {
+ sub_tlv = conn_find_ssl_sub_tlv(conn_tlv, sub_tlv, idx);
+ if (sub_tlv) {
+ smp->flags |= SMP_F_NOT_LAST;
+ smp->data.type = SMP_T_STR;
+ smp->data.u.str.area = (char *)sub_tlv->value;
+ smp->data.u.str.data = get_tlv_length(sub_tlv);
+ smp->ctx.a[0] = conn_tlv;
+ smp->ctx.a[1] = sub_tlv;
+
+ return 1;
+ }
+ }
}
smp->flags &= ~SMP_F_NOT_LAST;
--
2.50.1 (Apple Git-155)