ipvs: ftp: do not link expectations to unconfirmed conntracks
Subsystems (4)
PATCH v3 1/1 ipvs: ftp: do not link expectations to unconfirmed conntracks
ip_vs_ftp_out() can link an expectation to an unconfirmed conntrack. A later extension add on the same packet, such as seqadj in ip_vs_update_conntrack() or NAT, then reallocates ct->ext and leaves the expectation pointing into the freed area:
BUG: KASAN: slab-use-after-free in nf_ct_unlink_expect_report+0x490/0x4c0 Write of size 8 at addr ffff888004dd4e48 by task syz.2.17/649
Link the expectation only to a confirmed conntrack.
---
v3: skip unconfirmed conntracks, as suggested by Pablo; v2 did not cover NAT.
v2: https://lore.kernel.org/netdev/[email protected]/
net/netfilter/ipvs/ip_vs_ftp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/ipvs/ip_vs_ftp.c +4 -2
@@ -398,8 +398,10 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp, end - start, buf, buf_len);if (mangled) { -ip_vs_nfct_expect_related (skb, ct, n_cp, - ipvsh->protocol,0 ,0 ); +if (nf_ct_is_confirmed (ct)) +ip_vs_nfct_expect_related (skb, ct, n_cp, + ipvsh->protocol, +0 ,0 );if (skb->ip_summed ==CHECKSUM_COMPLETE ) skb->ip_summed =CHECKSUM_UNNECESSARY ;/* csum is updated */
Signature · 1 line
2.34.1
Pablo Neira Ayuso
maintainerQuoted 27 lines · Theodor Arsenij Larionov Trichkine
ip_vs_ftp_out()can link an expectation to an unconfirmed conntrack. A later extension add on the same packet, such as seqadj inip_vs_update_conntrack()or NAT, then reallocatesct->extand leaves the expectation pointing into the freed area:BUG: KASAN: slab-use-after-free in nf_ct_unlink_expect_report+0x490/0x4c0 Write of size 8 at addr ffff888004dd4e48 by task syz.2.17/649Link the expectation only to a confirmed conntrack.
Fixes b25adce16064 ("ipvs: correct usage/allocation of seqadj ext in ipvs")Suggested-by Pablo Neira Ayuso <[email protected]>Signed-off-by Theodor Arsenij Larionov Trichkine <[email protected]>---
v3: skip unconfirmed conntracks, as suggested by Pablo; v2 did not cover NAT.
v2: https://lore.kernel.org/netdev/[email protected]/net/netfilter/ipvs/ip_vs_ftp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/netfilter/ipvs/ip_vs_ftp.c +0
-0
net/netfilter/ipvs/ip_vs_ftp.c static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp, · line 401
Quoted 6 lines
net/netfilter/ipvs/ip_vs_ftp.c +1
-2@@ -398,8 +398,10 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp, end - start, buf, buf_len);if (mangled) { -ip_vs_nfct_expect_related (skb, ct, n_cp, - ipvsh->protocol,0 ,0 ); +if (nf_ct_is_confirmed (ct))
I think this is coming a bit late?
Maybe it is possible to check this earlier in this path?
Quoted 9 lines
net/netfilter/ipvs/ip_vs_ftp.c +3
-0+ip_vs_nfct_expect_related (skb, ct, n_cp, + ipvsh->protocol, +0 ,0 );if (skb->ip_summed ==CHECKSUM_COMPLETE ) skb->ip_summed =CHECKSUM_UNNECESSARY ;/* csum is updated */ Signature · 1 line
2.34.1
Quoted 3 lines · Pablo Neira Ayuso
I think this is coming a bit late?
Maybe it is possible to check this earlier in this path?
You mean returning at the top of ip_vs_ftp_out() if the ct is not confirmed, and letting the packet through untouched? Or even before ip_vs_ftp_out() gets called?
Pablo Neira Ayuso
maintainerQuoted 8 lines · Theodor Arsenij Larionov-Trichkine
Quoted 3 lines · Pablo Neira Ayuso
I think this is coming a bit late?
Maybe it is possible to check this earlier in this path?
You mean returning at the top of
ip_vs_ftp_out()if the ct is not confirmed, and letting the packet through untouched? Or even beforeip_vs_ftp_out()gets called?
net/netfilter/ipvs/ip_vs_ftp.c +1 -1
@@ -368,7 +368,7 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp, buf_len =strlen (buf); ct =nf_ct_get (skb, &ctinfo); -if (ct) { +if (ct && !nf_ct_unconfirmed (ct)) {bool mangled;/* If mangling fails this function will return 0
IIRC, there is a recent fix here to ignore template conntracks too here.
@Julian, is it worth all the pattern matching before getting here worth if the ct is unconfirmed? Otherwise, this can be short-circuit earlier.
Julian Anastasov
maintainerHi Theodor,
Quoted 8 lines · Theodor Arsenij Larionov-Trichkine
Quoted 3 lines · Pablo Neira Ayuso
I think this is coming a bit late?
Maybe it is possible to check this earlier in this path?
You mean returning at the top of
ip_vs_ftp_out()if the ct is not confirmed, and letting the packet through untouched? Or even beforeip_vs_ftp_out()gets called?
Early in ip_vs_ftp_out but you have to base this patch on
top of the template fix:
ipvs: ftp: skip conntrack templates in ip_vs_ftp_out
Or to wait it to be included in the nf tree. Then after the: if (cp->state != IP_VS_TCP_S_ESTABLISHED) return 1;
+ ct = nf_ct_get(skb, &ctinfo);
+ if (!ct || !nf_ct_is_confirmed(ct) || nf_ct_is_template(ct))
+ return 0;
+ /* seqadj extension not created by original packet? We usually
+ * add the extension for the first packet in ip_vs_update_conntrack().
+ */
+ if (!nfct_seqadj(ct))
+ return 0;
I.e. we want:
- ct to be present
- this should not be the first packet (nf_ct_is_confirmed)
- should not be template
- the first packet should create seqadj ext before confirmation, in ip_vs_update_conntrack()
By moving it early we drop the PASV response without
creating IPVS expectation (NO_CPORT connection).
I hope the above is correct :)
Regards
--
Julian Anastasov <[email protected]>