Discussion:
`iptables -m tcp --syn` doesn't do what the man says
Artyom Gavrichenkov
2012-03-30 20:58:54 UTC
Permalink
Hi all,

The iptables(8) manpage says:

--- [cut here] ---
tcp
These extensions can be used if `--protocol tcp' is specified. It provides the following options:
[!] --syn
Only match TCP packets with the SYN bit set and the ACK,RST and FIN bits cleared. Such packets are used to request TCP connection initia‐
tion; for example, blocking such packets coming in an interface will prevent incoming TCP connections, but outgoing TCP connections will be
unaffected. It is equivalent to --tcp-flags SYN,RST,ACK,FIN SYN. If the "!" flag precedes the "--syn", the sense of the option is
inverted.
--- [cut here] ---

Unfortunately, with current stable Linux kernel release (as well as
with most of the previous versions) blocking TCP packets with the SYN
bit set and the ACK,RST and FIN bits cleared won't prevent incoming
TCP connections.

Currently Linux TCP stack considers an incoming TCP segment to be a
connection initiation request if the segment only has SYN flag set and
ACK and RST flags cleared. You can easily check it yourself with your
nearest Linux box, as well as on the netfilter.org (213.95.27.115):

# hping3 -c 2 -n -FS -p 80 netfilter.org
HPING netfilter.org (wlan0 213.95.27.115): SF set, 40 headers + 0 data bytes
len=44 ip=213.95.27.115 ttl=52 DF id=0 sport=80 flags=SA seq=0 win=5840 rtt=58.8 ms
len=44 ip=213.95.27.115 ttl=52 DF id=0 sport=80 flags=SA seq=1 win=5840 rtt=51.1 ms

--- netfilter.org hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 51.1/55.0/58.8 ms
#

As you see, the netfilter.org server sends SYN/ACK in response to an
incoming SYN/FIN, indicating that a connection is being established.
It is only a matter of a few checks to make sure that the indication
is correct and the connection is indeed initialized.

This might be a Linux bug as well to accept SYN/FIN as a connection
initiation attempt. However, there could as well be a reason for kernel
developers to do this, because such thing as T/TCP (RFC 1644) allows a
TCP server to act like this, and though this RFC is experimental and
obsolete, as far as I know, it is still implemented somewhere, for
example, in FreeBSD.

I guess that most iptables setups probably are not affected by this
behaviour, because `iptables -m tcp --syn' is often used for something
in lines of this:

iptables -A INPUT -p tcp -m tcp --dport 22 --syn -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j DROP

In this case, SYN/FIN segments will be dropped, because they are not
considered plain SYN and they are not associated with an established
connection. My guess is that, for example, kernel.org is set up like that:

# hping3 -c 2 -n -FS -p 80 kernel.org
HPING kernel.org (wlan0 149.20.4.69): SF set, 40 headers + 0 data bytes

--- kernel.org hping statistic ---
2 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms
#

However, there are cases when this behaviour produces clear security
breach, for example, when one is trying to prevent incoming TCP
connections from a certain IP (as manpage suggests) or when one is
trying to limit the rate of connection establishment attempts. In
this case attacker can send SYN/FIN packets which would pass all the
rules containing --syn and would establish a connection.

The patch to fix this is trivial (it is attached below), however, it
might be a good idea to discuss reasons first and find the best
solution possible.

P. S. Sorry for being so verbose, I'm new to this mailing list and I'm
trying to explain everything as clear as I can.
--
| Artyom Gavrichenkov
| gpg: fa1c670e
| mailto: ***@gmail.com
| xmpp: ***@gmail.com
| skype: xima_era
| tel. no: +7 916 515 49 58

-- Signed-off-by: Artyom Gavrichenkov <***@gmail.com>

diff --git a/extensions/libxt_tcp.c b/extensions/libxt_tcp.c
index e849fa2..3af03c4 100644
--- a/extensions/libxt_tcp.c
+++ b/extensions/libxt_tcp.c
@@ -169,7 +169,7 @@ tcp_parse(int c, char **argv, int invert, unsigned int *flags,
xtables_error(PARAMETER_PROBLEM,
"Only one of `--syn' or `--tcp-flags' "
" allowed");
- parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", invert);
+ parse_tcp_flags(tcpinfo, "SYN,RST,ACK", "SYN", invert);
*flags |= TCP_FLAGS;
break;

diff --git a/extensions/libxt_tcp.man b/extensions/libxt_tcp.man
index 7a16118..e1698df 100644
--- a/extensions/libxt_tcp.man
+++ b/extensions/libxt_tcp.man
@@ -31,12 +31,12 @@ will only match packets with the SYN flag set, and the ACK, FIN and
RST flags unset.
.TP
[\fB!\fP] \fB\-\-syn\fP
-Only match TCP packets with the SYN bit set and the ACK,RST and FIN bits
+Only match TCP packets with the SYN bit set and the ACK and RST bits
cleared. Such packets are used to request TCP connection initiation;
for example, blocking such packets coming in an interface will prevent
incoming TCP connections, but outgoing TCP connections will be
unaffected.
-It is equivalent to \fB\-\-tcp\-flags SYN,RST,ACK,FIN SYN\fP.
+It is equivalent to \fB\-\-tcp\-flags SYN,RST,ACK SYN\fP.
If the "!" flag precedes the "\-\-syn", the sense of the
option is inverted.
.TP
Artyom Gavrichenkov
2012-03-30 21:40:41 UTC
Permalink
FYI current linux kernel just drops this kind of message (SYN+FIN)
OK, thanks for clarification, I should have missed this commit.
--
| Artyom Gavrichenkov
| gpg: fa1c670e
| mailto: ***@gmail.com
| xmpp: ***@gmail.com
| skype: xima_era
| tel. no: +7 916 515 49 58
Eric Dumazet
2012-03-30 21:50:13 UTC
Permalink
31.03.2012 01:19, Eric Dumazet =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=
FYI current linux kernel just drops this kind of message (SYN+FIN)
=20
OK, thanks for clarification, I should have missed this commit.
=20
You're welcome.

I just realize I made a typo in the Changelog...

Dont call conn_request() if the TCP flags includes SYN flag

->

Dont call conn_request() if the TCP flags includes FIN flag


--
To unsubscribe from this list: send the line "unsubscribe netfilter-dev=
el" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Eric Dumazet
2012-03-30 21:19:25 UTC
Permalink
Post by Artyom Gavrichenkov
Hi all,
=20
=20
--- [cut here] ---
tcp
These extensions can be used if `--protocol tcp' is specified.=
[!] --syn
Only match TCP packets with the SYN bit set and the A=
CK,RST and FIN bits cleared. Such packets are used to request TCP conn=
ection initia=E2=80=90
Post by Artyom Gavrichenkov
tion; for example, blocking such packets coming in an i=
nterface will prevent incoming TCP connections, but outgoing TCP connec=
tions will be
Post by Artyom Gavrichenkov
unaffected. It is equivalent to --tcp-flags SYN,=
RST,ACK,FIN SYN. If the "!" flag precedes the "--syn", the sense of=
the option is
Post by Artyom Gavrichenkov
inverted.
--- [cut here] ---
=20
Unfortunately, with current stable Linux kernel release (as well as
with most of the previous versions) blocking TCP packets with the SYN
bit set and the ACK,RST and FIN bits cleared won't prevent incoming
TCP connections.
=20
Currently Linux TCP stack considers an incoming TCP segment to be a
connection initiation request if the segment only has SYN flag set an=
d
Post by Artyom Gavrichenkov
ACK and RST flags cleared. You can easily check it yourself with your
=20
# hping3 -c 2 -n -FS -p 80 netfilter.org
HPING netfilter.org (wlan0 213.95.27.115): SF set, 40 headers + 0 dat=
a bytes
Post by Artyom Gavrichenkov
len=3D44 ip=3D213.95.27.115 ttl=3D52 DF id=3D0 sport=3D80 flags=3DSA =
seq=3D0 win=3D5840 rtt=3D58.8 ms
Post by Artyom Gavrichenkov
len=3D44 ip=3D213.95.27.115 ttl=3D52 DF id=3D0 sport=3D80 flags=3DSA =
seq=3D1 win=3D5840 rtt=3D51.1 ms
Post by Artyom Gavrichenkov
=20
--- netfilter.org hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max =3D 51.1/55.0/58.8 ms
#=20
=20
As you see, the netfilter.org server sends SYN/ACK in response to an
incoming SYN/FIN, indicating that a connection is being established.
It is only a matter of a few checks to make sure that the indication
is correct and the connection is indeed initialized.
=20
This might be a Linux bug as well to accept SYN/FIN as a connection
initiation attempt. However, there could as well be a reason for kern=
el
Post by Artyom Gavrichenkov
developers to do this, because such thing as T/TCP (RFC 1644) allows =
a
Post by Artyom Gavrichenkov
TCP server to act like this, and though this RFC is experimental and
obsolete, as far as I know, it is still implemented somewhere, for
example, in FreeBSD.
=20
=46YI current linux kernel just drops this kind of message (SYN+FIN)

git describe --contains fdf5af0daf8019cec2396cdef8fb042d80fe71fa
v3.3-rc1~182^2~365

commit fdf5af0daf8019cec2396cdef8fb042d80fe71fa
Author: Eric Dumazet <***@gmail.com>
Date: Fri Dec 2 23:41:42 2011 +0000

tcp: drop SYN+FIN messages
=20
Denys Fedoryshchenko reported that SYN+FIN attacks were bringing hi=
s
linux machines to their limits.
=20
Dont call conn_request() if the TCP flags includes SYN flag
=20
Reported-by: Denys Fedoryshchenko <***@visp.net.lb>
Signed-off-by: Eric Dumazet <***@gmail.com>
Signed-off-by: David S. Miller <***@davemloft.net>

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 78dd38c..0cbb440 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -5811,6 +5811,8 @@ int tcp_rcv_state_process(struct sock *sk, struct=
sk_buff *skb,
goto discard;
=20
if (th->syn) {
+ if (th->fin)
+ goto discard;
if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
return 1;
=20
Post by Artyom Gavrichenkov
I guess that most iptables setups probably are not affected by this
behaviour, because `iptables -m tcp --syn' is often used for somethin=
g
Post by Artyom Gavrichenkov
=20
iptables -A INPUT -p tcp -m tcp --dport 22 --syn -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCE=
PT
Post by Artyom Gavrichenkov
iptables -A INPUT -j DROP
=20
In this case, SYN/FIN segments will be dropped, because they are not
considered plain SYN and they are not associated with an established
connection. My guess is that, for example, kernel.org is set up like =
=20
Or it runs linux 3.3
Post by Artyom Gavrichenkov
# hping3 -c 2 -n -FS -p 80 kernel.org
HPING kernel.org (wlan0 149.20.4.69): SF set, 40 headers + 0 data byt=
es
Post by Artyom Gavrichenkov
=20
--- kernel.org hping statistic ---
2 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max =3D 0.0/0.0/0.0 ms
#
=20
However, there are cases when this behaviour produces clear security
breach, for example, when one is trying to prevent incoming TCP
connections from a certain IP (as manpage suggests) or when one is
trying to limit the rate of connection establishment attempts. In
this case attacker can send SYN/FIN packets which would pass all the
rules containing --syn and would establish a connection.
=20
The patch to fix this is trivial (it is attached below), however, it
might be a good idea to discuss reasons first and find the best
solution possible.
=20
P. S. Sorry for being so verbose, I'm new to this mailing list and I'=
m
Post by Artyom Gavrichenkov
trying to explain everything as clear as I can.
=20
Thats perfect, thanks !
Post by Artyom Gavrichenkov
--=20
| Artyom Gavrichenkov
| gpg: fa1c670e
| skype: xima_era
| tel. no: +7 916 515 49 58
=20
=20
diff --git a/extensions/libxt_tcp.c b/extensions/libxt_tcp.c
index e849fa2..3af03c4 100644
--- a/extensions/libxt_tcp.c
+++ b/extensions/libxt_tcp.c
@@ -169,7 +169,7 @@ tcp_parse(int c, char **argv, int invert, unsigne=
d int *flags,
Post by Artyom Gavrichenkov
xtables_error(PARAMETER_PROBLEM,
"Only one of `--syn' or `--tcp-fla=
gs' "
Post by Artyom Gavrichenkov
" allowed");
- parse_tcp_flags(tcpinfo, "SYN,RST,ACK,FIN", "SYN", in=
vert);
Post by Artyom Gavrichenkov
+ parse_tcp_flags(tcpinfo, "SYN,RST,ACK", "SYN", invert=
);
Post by Artyom Gavrichenkov
*flags |=3D TCP_FLAGS;
break;
=20
diff --git a/extensions/libxt_tcp.man b/extensions/libxt_tcp.man
index 7a16118..e1698df 100644
--- a/extensions/libxt_tcp.man
+++ b/extensions/libxt_tcp.man
@@ -31,12 +31,12 @@ will only match packets with the SYN flag set, an=
d the ACK, FIN and
Post by Artyom Gavrichenkov
RST flags unset.
.TP
[\fB!\fP] \fB\-\-syn\fP
-Only match TCP packets with the SYN bit set and the ACK,RST and FIN =
bits
Post by Artyom Gavrichenkov
+Only match TCP packets with the SYN bit set and the ACK and RST bits
cleared. Such packets are used to request TCP connection initiation=
;
Post by Artyom Gavrichenkov
for example, blocking such packets coming in an interface will preve=
nt
Post by Artyom Gavrichenkov
incoming TCP connections, but outgoing TCP connections will be
unaffected.
-It is equivalent to \fB\-\-tcp\-flags SYN,RST,ACK,FIN SYN\fP.
+It is equivalent to \fB\-\-tcp\-flags SYN,RST,ACK SYN\fP.
If the "!" flag precedes the "\-\-syn", the sense of the
option is inverted.
.TP
=20
--
To unsubscribe from this list: send the line "unsubscribe netfilter-dev=
el" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
2012-04-01 17:42:40 UTC
Permalink
Post by Artyom Gavrichenkov
Hi all,
=20
=20
--- [cut here] ---
tcp
These extensions can be used if `--protocol tcp' is specified.=
[!] --syn
Only match TCP packets with the SYN bit set and the A=
CK,RST and FIN bits cleared. Such packets are used to request TCP conn=
ection initia=E2=80=90
Post by Artyom Gavrichenkov
tion; for example, blocking such packets coming in an i=
nterface will prevent incoming TCP connections, but outgoing TCP connec=
tions will be
Post by Artyom Gavrichenkov
unaffected. It is equivalent to --tcp-flags SYN,=
RST,ACK,FIN SYN. If the "!" flag precedes the "--syn", the sense of=
the option is
Post by Artyom Gavrichenkov
inverted.
--- [cut here] ---
=20
Unfortunately, with current stable Linux kernel release (as well as
with most of the previous versions) blocking TCP packets with the SYN
bit set and the ACK,RST and FIN bits cleared won't prevent incoming
TCP connections.
=20
Currently Linux TCP stack considers an incoming TCP segment to be a
connection initiation request if the segment only has SYN flag set an=
d
Post by Artyom Gavrichenkov
ACK and RST flags cleared. You can easily check it yourself with your
=20
# hping3 -c 2 -n -FS -p 80 netfilter.org
HPING netfilter.org (wlan0 213.95.27.115): SF set, 40 headers + 0 dat=
a bytes
Post by Artyom Gavrichenkov
len=3D44 ip=3D213.95.27.115 ttl=3D52 DF id=3D0 sport=3D80 flags=3DSA =
seq=3D0 win=3D5840 rtt=3D58.8 ms
Post by Artyom Gavrichenkov
len=3D44 ip=3D213.95.27.115 ttl=3D52 DF id=3D0 sport=3D80 flags=3DSA =
seq=3D1 win=3D5840 rtt=3D51.1 ms
Post by Artyom Gavrichenkov
=20
--- netfilter.org hping statistic ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max =3D 51.1/55.0/58.8 ms
#=20
=20
As you see, the netfilter.org server sends SYN/ACK in response to an
incoming SYN/FIN, indicating that a connection is being established.
It is only a matter of a few checks to make sure that the indication
is correct and the connection is indeed initialized.
=20
This might be a Linux bug as well to accept SYN/FIN as a connection
initiation attempt. However, there could as well be a reason for kern=
el
Post by Artyom Gavrichenkov
developers to do this, because such thing as T/TCP (RFC 1644) allows =
a
Post by Artyom Gavrichenkov
TCP server to act like this, and though this RFC is experimental and
obsolete, as far as I know, it is still implemented somewhere, for
example, in FreeBSD.
=20
I guess that most iptables setups probably are not affected by this
behaviour, because `iptables -m tcp --syn' is often used for somethin=
g
Post by Artyom Gavrichenkov
=20
iptables -A INPUT -p tcp -m tcp --dport 22 --syn -j ACCEPT
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCE=
PT
Post by Artyom Gavrichenkov
iptables -A INPUT -j DROP
=20
In this case, SYN/FIN segments will be dropped, because they are not
considered plain SYN and they are not associated with an established
connection. My guess is that, for example, kernel.org is set up like =
=20
# hping3 -c 2 -n -FS -p 80 kernel.org
HPING kernel.org (wlan0 149.20.4.69): SF set, 40 headers + 0 data byt=
es
Post by Artyom Gavrichenkov
=20
--- kernel.org hping statistic ---
2 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max =3D 0.0/0.0/0.0 ms
#
=20
However, there are cases when this behaviour produces clear security
breach, for example, when one is trying to prevent incoming TCP
connections from a certain IP (as manpage suggests) or when one is
trying to limit the rate of connection establishment attempts. In
this case attacker can send SYN/FIN packets which would pass all the
rules containing --syn and would establish a connection.
I understand your concern, but the info in the manpage is correct:
basically, it can be extracted from it that --syn will not match
SYN+FIN packets.

As you point in your patch, you have to use:

--tcp-flags SYN,RST,ACK SYN

in your rule-set for the situation that you describe.

Changing the default behaviour of --syn to catch this case is
delicate, I don't want to break backward compatibility.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-dev=
el" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Eric Dumazet
2012-04-01 17:53:31 UTC
Permalink
Post by Pablo Neira Ayuso
basically, it can be extracted from it that --syn will not match
SYN+FIN packets.
--tcp-flags SYN,RST,ACK SYN
in your rule-set for the situation that you describe.
Changing the default behaviour of --syn to catch this case is
delicate, I don't want to break backward compatibility.
Agreed.

With TCP Fast Open, it might be possible to send a SYN+FIN+cookies+DATA
in a single frame.



--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to ***@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Loading...