Skip to contentSkip to conversation

udp: validate checksum in udp_read_sock()

PATCH Superseded Cong Wang 2 participants 4 messages Oct 8, 2021 – Oct 13, 2021

Subsystems (4)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 udp: validate checksum in udp_read_sock()

From: Cong Wang <[email protected]>

It turns out the skb's in sock receive queue could have bad checksums, as both ->poll() and ->recvmsg() validate checksums. We have to do the same for ->read_sock() path too before they are redirected in sockmap.

Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")
Cc Lorenz Bauer <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
 net/ipv4/udp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/ipv4/udp.c +11 -0
index 8536b2a7210b..0ae8ab5e05b4 100644
@@ -1808,6 +1808,17 @@ int udp_read_sock(struct sock *sk, read_descriptor_t *desc,
 		skb = skb_recv_udp(sk, 0, 1, &err);
 		if (!skb)
 			return err;
+
+		if (udp_lib_checksum_complete(skb)) {
+			__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
+					IS_UDPLITE(sk));
+			__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
+					IS_UDPLITE(sk));
+			atomic_inc(&sk->sk_drops);
+			kfree_skb(skb);
+			continue;
+		}
+
 		used = recv_actor(desc, skb, 0, skb->len);
 		if (used <= 0) {
 			if (!copied)
Signature · 1 line
2.30.2
#1 lore raw
Quoted 23 lines · Cong Wang

From: Cong Wang <[email protected]>

It turns out the skb's in sock receive queue could have bad checksums, as both ->poll() and ->recvmsg() validate checksums. We have to do the same for ->read_sock() path too before they are redirected in sockmap.

Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")
Cc Lorenz Bauer <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
 net/ipv4/udp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/ipv4/udp.c +0 -0
index 8536b2a7210b..0ae8ab5e05b4 100644
@@ -1808,6 +1808,17 @@ int udp_read_sock(struct sock *sk, read_descriptor_t *desc,
 		skb = skb_recv_udp(sk, 0, 1, &err);

net/ipv4/udp.c int udp_read_sock(struct sock *sk, read_descriptor_t *desc, · line 1818

Quoted 10 lines
net/ipv4/udp.c +8 -0
 		if (!skb)
 			return err;
+
+		if (udp_lib_checksum_complete(skb)) {
+			__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
+					IS_UDPLITE(sk));
+			__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
+					IS_UDPLITE(sk));
+			atomic_inc(&sk->sk_drops);
+			kfree_skb(skb);

We could use sock_drop() here? Otherwise looks good thanks.

Quoted 9 lines
net/ipv4/udp.c +3 -0
+			continue;
+		}
+
 		used = recv_actor(desc, skb, 0, skb->len);
 		if (used <= 0) {
 			if (!copied)
Signature · 1 line
2.30.2
author
#2 lore raw
Quoted 29 lines · John Fastabend
Quoted 27 lines · Cong Wang

From: Cong Wang <[email protected]>

It turns out the skb's in sock receive queue could have bad checksums, as both ->poll() and ->recvmsg() validate checksums. We have to do the same for ->read_sock() path too before they are redirected in sockmap.

Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")
Cc Lorenz Bauer <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
 net/ipv4/udp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/ipv4/udp.c +2 -0
index 8536b2a7210b..0ae8ab5e05b4 100644
@@ -1808,6 +1808,17 @@ int udp_read_sock(struct sock *sk, read_descriptor_t *desc,
              skb = skb_recv_udp(sk, 0, 1, &err);
              if (!skb)
                      return err;
+
+             if (udp_lib_checksum_complete(skb)) {
Quoted 8 lines

net/ipv4/udp.c int udp_read_sock(struct sock *sk, read_descriptor_t *desc, · line 1818

Quoted 6 lines
net/ipv4/udp.c +6 -0
+                     __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
+                                     IS_UDPLITE(sk));
+                     __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
+                                     IS_UDPLITE(sk));
+                     atomic_inc(&sk->sk_drops);
+                     kfree_skb(skb);

We could use sock_drop() here? Otherwise looks good thanks.

sock_drop() is in include/linux/skmsg.h, I think we need to move it to sock.h before using it here in net/ipv4/udp.c, right?

And there are other similar patterns which can be replaced with sock_drop(), so we can do the replacement for all in a separate patch.

Thanks.

#3 lore raw
Quoted 40 lines · Cong Wang
Quoted 29 lines · John Fastabend
Quoted 27 lines · Cong Wang

From: Cong Wang <[email protected]>

It turns out the skb's in sock receive queue could have bad checksums, as both ->poll() and ->recvmsg() validate checksums. We have to do the same for ->read_sock() path too before they are redirected in sockmap.

Fixes d7f571188ecf ("udp: Implement ->read_sock() for sockmap")
Cc Lorenz Bauer <[email protected]>
Cc Jakub Sitnicki <[email protected]>
---
 net/ipv4/udp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/ipv4/udp.c +2 -0
index 8536b2a7210b..0ae8ab5e05b4 100644
@@ -1808,6 +1808,17 @@ int udp_read_sock(struct sock *sk, read_descriptor_t *desc,
              skb = skb_recv_udp(sk, 0, 1, &err);
              if (!skb)
                      return err;
+
+             if (udp_lib_checksum_complete(skb)) {
Quoted 8 lines

net/ipv4/udp.c int udp_read_sock(struct sock *sk, read_descriptor_t *desc, · line 1818

Quoted 6 lines
net/ipv4/udp.c +6 -0
+                     __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
+                                     IS_UDPLITE(sk));
+                     __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
+                                     IS_UDPLITE(sk));
+                     atomic_inc(&sk->sk_drops);
+                     kfree_skb(skb);

We could use sock_drop() here? Otherwise looks good thanks.

Quoted 2 lines

sock_drop() is in include/linux/skmsg.h, I think we need to move it to sock.h before using it here in net/ipv4/udp.c, right?

Yes it would be necessary. Lets not do it here otherwise backports will be ugly.

Quoted 6 lines

And there are other similar patterns which can be replaced with sock_drop(), so we can do the replacement for all in a separate patch.

Thanks.