Skip to contentSkip to conversation

netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack()

PATCH In review Rosen Penev 3 participants 5 messages May 25 – Jun 7

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack()

The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.

This matches the existing get_unaligned_be32() use later in the same function.

Assisted-by Claude:Opus-4.7
---
 net/netfilter/nf_conntrack_proto_tcp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +5 -5
index b67426c2189b..8993374c9df2 100644
@@ -405,11 +405,11 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
 		return;
 
 	/* Fast path for timestamp-only option */
-	if (length == TCPOLEN_TSTAMP_ALIGNED
-	    && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
-				       | (TCPOPT_NOP << 16)
-				       | (TCPOPT_TIMESTAMP << 8)
-				       | TCPOLEN_TIMESTAMP))
+	if (length == TCPOLEN_TSTAMP_ALIGNED &&
+	    get_unaligned_be32(ptr) == ((TCPOPT_NOP << 24) |
+					(TCPOPT_NOP << 16) |
+					(TCPOPT_TIMESTAMP << 8) |
+					TCPOLEN_TIMESTAMP))
 		return;
 
 	while (length > 0) {
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Rosen Penev

The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.

This matches the existing get_unaligned_be32() use later in the same function.

Assisted-by Claude:Opus-4.7

I already spotted this corner case when working on a SYNPROXY patch [1] but didn't send a patch yet. I think this is for correctness too.

Anyway, it is likely that there are more places where this tweak is needed.. I will look around.. meanwhile:

[1] lore.kernel.org/netfilter-devel/[email protected]/

#2 lore raw

Hi Fernando,

Quoted 10 lines · Fernando Fernandez Mancera
Quoted 9 lines · Rosen Penev

The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.

This matches the existing get_unaligned_be32() use later in the same function.

Quoted 6 lines
Quoted 2 lines
Assisted-by Claude:Opus-4.7

I already spotted this corner case when working on a SYNPROXY patch [1] but didn't send a patch yet. I think this is for correctness too.

Anyway, it is likely that there are more places where this tweak is needed..

I agree a more general audit to spot unaligned access, targetting nf-next would be good.

Thanks.

Quoted 5 lines

I will look around.. meanwhile:

[1] lore.kernel.org/netfilter-devel/[email protected]/

#3 lore raw
In reply to Rosen Penev
Quoted 23 lines · Rosen Penev

The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.

This matches the existing get_unaligned_be32() use later in the same function.

Assisted-by Claude:Opus-4.7
---
 net/netfilter/nf_conntrack_proto_tcp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
index b67426c2189b..8993374c9df2 100644
@@ -405,11 +405,11 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
 		return;
 
 	/* Fast path for timestamp-only option */

net/netfilter/nf_conntrack_proto_tcp.c static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, · line 412

Quoted 10 lines
net/netfilter/nf_conntrack_proto_tcp.c +5 -5
-	if (length == TCPOLEN_TSTAMP_ALIGNED
-	    && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
-				       | (TCPOPT_NOP << 16)
-				       | (TCPOPT_TIMESTAMP << 8)
-				       | TCPOLEN_TIMESTAMP))
+	if (length == TCPOLEN_TSTAMP_ALIGNED &&
+	    get_unaligned_be32(ptr) == ((TCPOPT_NOP << 24) |
+					(TCPOPT_NOP << 16) |
+					(TCPOPT_TIMESTAMP << 8) |
+					TCPOLEN_TIMESTAMP))

Missing put_unaligned_be32(), BTW.

Quoted 6 lines
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
 		return;
 
 	while (length > 0) {
Signature · 1 line
2.54.0
#4 lore raw
Quoted 23 lines · Pablo Neira Ayuso
Quoted 22 lines · Rosen Penev

The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.

This matches the existing get_unaligned_be32() use later in the same function.

Assisted-by Claude:Opus-4.7
---
 net/netfilter/nf_conntrack_proto_tcp.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
index b67426c2189b..8993374c9df2 100644
@@ -405,11 +405,11 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,
 		return;
 
Quoted 13 lines

net/netfilter/nf_conntrack_proto_tcp.c static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, · line 412

Quoted 11 lines
net/netfilter/nf_conntrack_proto_tcp.c +5 -5
 	/* Fast path for timestamp-only option */
-	if (length == TCPOLEN_TSTAMP_ALIGNED
-	    && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
-				       | (TCPOPT_NOP << 16)
-				       | (TCPOPT_TIMESTAMP << 8)
-				       | TCPOLEN_TIMESTAMP))
+	if (length == TCPOLEN_TSTAMP_ALIGNED &&
+	    get_unaligned_be32(ptr) == ((TCPOPT_NOP << 24) |
+					(TCPOPT_NOP << 16) |
+					(TCPOPT_TIMESTAMP << 8) |
+					TCPOLEN_TIMESTAMP))

Missing put_unaligned_be32(), BTW.

Sorry, no write in this case, only read, LGTM. Apologies.

Quoted 6 lines
Quoted 6 lines
net/netfilter/nf_conntrack_proto_tcp.c +0 -0
 		return;
 
 	while (length > 0) {
Signature · 1 line
2.54.0