udp: change MSG_TRUNC return behaviour for MSG_PEEK in recvmsg
Subsystems (3)
PATCH 1/1 udp: change MSG_TRUNC return behaviour for MSG_PEEK in recvmsg
Make UDP recvmsg only return the MSG_TRUNC flag if the read does not copy the tail end of the datagram. Specifically, this targets MSG_PEEK when we're using a positive peek offset.
The current behaviour means that if we have a positive peek offset off and we're reading r bytes from a datagram of ulen length, we respond with MSG_TRUNC if and only if r <= ulen - off. This is odd behaviour as we return MSG_TRUNC if the user requests exactly ulen - off which has no truncation.
The behaviour could be corrected in two ways:
This patch returns MSG_TRUNC only for tail-end truncation and not head truncation. This is more consistent with recv(2):
Quoted 3 lines
MSG_TRUNC
indicates that the trailing portion of a datagram was discarded because the datagram was larger than the buffer supplied.
although this isn't written with SO_PEEK_OFF in mind.
The second option is to always return MSG_TRUNC if off > 0 like the man-pages socket(7) page states:
Quoted 2 lines
For datagram sockets, if the "peek offset" points to the middle of a packet, the data returned will be marked with the
MSG_TRUNCflag.
--- net/ipv4/udp.c | 2 +- net/ipv6/udp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/udp.c +1 -1
@@ -1855,7 +1855,7 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, copied = len;if (copied > ulen - off) copied = ulen - off; -else if (copied < ulen) +else if (copied < ulen - off) msg->msg_flags |=MSG_TRUNC ;/*
net/ipv6/udp.c +1 -1
@@ -348,7 +348,7 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, copied = len;if (copied > ulen - off) copied = ulen - off; -else if (copied < ulen) +else if (copied < ulen - off) msg->msg_flags |=MSG_TRUNC ; is_udp4 = (skb->protocol ==htons (ETH_P_IP ));
--
2.34.1
From: Torin Carey
Quoted 37 lines
Sent: 22 March 2022 19:04
Make UDP recvmsg only return the
MSG_TRUNCflag if the read does not copy the tail end of the datagram. Specifically, this targetsMSG_PEEKwhen we're using a positive peek offset.The current behaviour means that if we have a positive peek offset
offand we're readingrbytes from a datagram ofulenlength, we respond withMSG_TRUNCif and only ifr <= ulen - off. This is odd behaviour as we returnMSG_TRUNCif the user requests exactlyulen - offwhich has no truncation.The behaviour could be corrected in two ways:
This patch returns
MSG_TRUNConly for tail-end truncation and not head truncation. This is more consistent with recv(2):Quoted 3 lines
MSG_TRUNC
indicates that the trailing portion of a datagram was discarded because the datagram was larger than the buffer supplied.although this isn't written with
SO_PEEK_OFFin mind.The second option is to always return
MSG_TRUNCifoff > 0like the man-pages socket(7) page states:Quoted 2 lines
For datagram sockets, if the "peek offset" points to the middle of a packet, the data returned will be marked with the
MSG_TRUNCflag.Signed-off-by Torin Carey <[email protected]>--- net/ipv4/udp.c | 2 +- net/ipv6/udp.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)net/ipv4/udp.c +0
-0
net/ipv4/udp.c int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, · line 1859
Quoted 6 lines
net/ipv4/udp.c +1
-1@@ -1855,7 +1855,7 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, copied = len;if (copied > ulen - off) copied = ulen - off; -else if (copied < ulen) +else if (copied < ulen - off) msg->msg_flags |=MSG_TRUNC ;
You can remove a test:
if (copied >= ulen - off)
copied = ulen - off;
else
msg->msg_flags |= MSG_TRUNC;
DavidQuoted 16 lines
net/ipv4/udp.c +0
-0/* net/ipv6/udp.c +1
-2@@ -348,7 +348,7 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, copied = len;if (copied > ulen - off) copied = ulen - off; -else if (copied < ulen) +else if (copied < ulen - off) msg->msg_flags |=MSG_TRUNC ; is_udp4 = (skb->protocol ==htons (ETH_P_IP )); --
Quoted 2 lines
2.34.1
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)