ipvs: ftp: do not link expectations to unconfirmed conntracks
Subsystems (4)
PATCH v6 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
Check the conntrack at the start of ip_vs_ftp_out() and drop the packet unless the conntrack is confirmed, is not a template and already carries the seqadj extension. This makes the conntrack check before the mangling redundant, so remove it.
---
v6: remove the conntrack check before the mangling and the 'mangled'
variable, and unindent that block.
v5: drop the second nf_ct_get().
v4: check the conntrack at the start of ip_vs_ftp_out(). A server reply
that fails the check is now dropped even when it is not a PASV/EPSV
response. Without a conntrack, e.g. with a notrack rule, only PASV/EPSV
replies were dropped before.
Link to v5: https://lore.kernel.org/netfilter-devel/[email protected]/
Link to v4: https://lore.kernel.org/netfilter-devel/[email protected]/
net/netfilter/ipvs/ip_vs_ftp.c | 45 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-)
net/netfilter/ipvs/ip_vs_ftp.c +22 -23
@@ -29,6 +29,7 @@#include <linux/netfilter.h> #include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_expect.h> +#include <net/netfilter/nf_conntrack_seqadj.h> #include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_helper.h> #include <linux/gfp.h>
@@ -283,6 +284,12 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,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 ; +if (!nfct_seqadj (ct)) +return 0 ; +/* Linear packets are much easier to deal with. */ if (skb_ensure_writable (skb, skb->len))return 0 ;
@@ -382,29 +389,21 @@ 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 && !nf_ct_is_template (ct)) { -bool mangled; - -/* If mangling fails this function will return 0 -* which will cause the packet to be dropped. -* Mangling can only fail under memory pressure, -* hopefully it will succeed on the retransmitted -* packet. -*/ - mangled =nf_nat_mangle_tcp_packet (skb, ct, ctinfo, - ipvsh->len, - start - data, - end - start, - buf, buf_len); -if (mangled) { -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 */ - ret =1 ; - } +/* If mangling fails this function will return 0 +* which will cause the packet to be dropped. +* Mangling can only fail under memory pressure, +* hopefully it will succeed on the retransmitted +* packet. +*/ +if (nf_nat_mangle_tcp_packet (skb, ct, ctinfo, ipvsh->len, + start - data, end - start, + buf, buf_len)) { +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 */ + ret =1 ; }/* Not setting 'diff' is intentional, otherwise the sequence
Signature · 1 line
2.34.1
Hello,
Quoted 13 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/649Check the conntrack at the start of
ip_vs_ftp_out()and drop the packet unless the conntrack is confirmed, is not a template and already carries the seqadj extension. This makes the conntrack check before the mangling redundant, so remove it.
Quoted 5 lines
Fixes b25adce16064 ("ipvs: correct usage/allocation of seqadj ext in ipvs")Suggested-by Julian Anastasov <[email protected]>Suggested-by Pablo Neira Ayuso <[email protected]>Signed-off-by Theodor Arsenij Larionov Trichkine <[email protected]>
The patch looks good to me, thanks!
Quoted 86 lines
---
v6: remove the conntrack check before the mangling and the 'mangled'
variable, and unindent that block.
v5: drop the secondnf_ct_get().
v4: check the conntrack at the start ofip_vs_ftp_out(). A server reply
that fails the check is now dropped even when it is not a PASV/EPSV
response. Without a conntrack, e.g. with a notrack rule, only PASV/EPSV
replies were dropped before.
Link to v5: https://lore.kernel.org/netfilter-devel/[email protected]/
Link to v4: https://lore.kernel.org/netfilter-devel/[email protected]/net/netfilter/ipvs/ip_vs_ftp.c | 45 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-)net/netfilter/ipvs/ip_vs_ftp.c +22
-23@@ -29,6 +29,7 @@#include <linux/netfilter.h> #include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_expect.h> +#include <net/netfilter/nf_conntrack_seqadj.h> #include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_helper.h> #include <linux/gfp.h> @@ -283,6 +284,12 @@ static int ip_vs_ftp_out(struct ip_vs_app *app, struct ip_vs_conn *cp,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 ; +if (!nfct_seqadj (ct)) +return 0 ; +/* Linear packets are much easier to deal with. */ if (skb_ensure_writable (skb, skb->len))return 0 ;@@ -382,29 +389,21 @@ 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 && !nf_ct_is_template (ct)) { -bool mangled; - -/* If mangling fails this function will return 0 -* which will cause the packet to be dropped. -* Mangling can only fail under memory pressure, -* hopefully it will succeed on the retransmitted -* packet. -*/ - mangled =nf_nat_mangle_tcp_packet (skb, ct, ctinfo, - ipvsh->len, - start - data, - end - start, - buf, buf_len); -if (mangled) { -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 */ - ret =1 ; - } +/* If mangling fails this function will return 0 +* which will cause the packet to be dropped. +* Mangling can only fail under memory pressure, +* hopefully it will succeed on the retransmitted +* packet. +*/ +if (nf_nat_mangle_tcp_packet (skb, ct, ctinfo, ipvsh->len, + start - data, end - start, + buf, buf_len)) { +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 */ + ret =1 ; }/* Not setting 'diff' is intentional, otherwise the sequence Signature · 1 line
2.34.1
Regards
--
Julian Anastasov <[email protected]>