Skip to contentSkip to conversation

udp: Fix __ip_append_data()'s handling of MSG_SPLICE_PAGES

PATCH Applied David Howells 3 participants 7 messages Aug 1, 2023 – Aug 3, 2023

Subsystems (5)
Networking Network drivers VFS BPF +1 more subsystems

v1 of 2 Applied 1/1 Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udp: Fix __ip_append_data()'s handling of MSG_SPLICE_PAGES

__ip_append_data() can get into an infinite loop when asked to splice into a partially-built UDP message that has more than the frag-limit data and up to the MTU limit. Something like:

        pipe(pfd);
        sfd = socket(AF_INET, SOCK_DGRAM, 0);
        connect(sfd, ...);
        send(sfd, buffer, 8161, MSG_CONFIRM|MSG_MORE);
        write(pfd[1], buffer, 8);
        splice(pfd[0], 0, sfd, 0, 0x4ffe0ul, 0);

where the amount of data given to send() is dependent on the MTU size (in this instance an interface with an MTU of 8192).

The problem is that the calculation of the amount to copy in __ip_append_data() goes negative in two places, and, in the second place, this gets subtracted from the length remaining, thereby increasing it.

This happens when pagedlen > 0 (which happens for MSG_ZEROCOPY and MSG_SPLICE_PAGES), because the terms in:

        copy = datalen - transhdrlen - fraggap - pagedlen;

then mostly cancel when pagedlen is substituted for, leaving just -fraggap. This causes:

        length -= copy + transhdrlen;

to increase the length to more than the amount of data in msg->msg_iter, which causes skb_splice_from_iter() to be unable to fill the request and it returns less than 'copied' - which means that length never gets to 0 and we never exit the loop.

Fix this by:

(1) Insert a note about the dodgy calculation of 'copy'.

(2) If MSG_SPLICE_PAGES, clear copy if it is negative from the above equation, so that 'offset' isn't regressed and 'length' isn't increased, which will mean that length and thus copy should match the amount left in the iterator.

(3) When handling MSG_SPLICE_PAGES, give a warning and return -EIO if we're asked to splice more than is in the iterator. It might be better to not give the warning or even just give a 'short' write.

[!] Note that this ought to also affect MSG_ZEROCOPY, but MSG_ZEROCOPY avoids the problem by simply assuming that everything asked for got copied, not just the amount that was in the iterator. This is a potential bug for the future.

Fixes 7ac7c987850c ("udp: Convert udp_sendpage() to use MSG_SPLICE_PAGES")
Reported-by [email protected]
cc Willem de Bruijn <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
cc Paolo Abeni <[email protected]>
cc David Ahern <[email protected]>
cc Jens Axboe <[email protected]>
---
 net/ipv4/ip_output.c |    9 +++++++++
 1 file changed, 9 insertions(+)
net/ipv4/ip_output.c +9 -0
index 6e70839257f7..91715603cf6e 100644
@@ -1158,10 +1158,15 @@ static int __ip_append_data(struct sock *sk,
 			}
 
 			copy = datalen - transhdrlen - fraggap - pagedlen;
+			/* [!] NOTE: copy will be negative if pagedlen>0
+			 * because then the equation reduces to -fraggap.
+			 */
 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
 				err = -EFAULT;
 				kfree_skb(skb);
 				goto error;
+			} else if (flags & MSG_SPLICE_PAGES) {
+				copy = 0;
 			}
 
 			offset += copy;
@@ -1209,6 +1214,10 @@ static int __ip_append_data(struct sock *sk,
 		} else if (flags & MSG_SPLICE_PAGES) {
 			struct msghdr *msg = from;
 
+			err = -EIO;
+			if (WARN_ON_ONCE(copy > msg->msg_iter.count))
+				goto error;
+
 			err = skb_splice_from_iter(skb, &msg->msg_iter, copy,
 						   sk->sk_allocation);
 			if (err < 0)
#1 lore raw
Quoted 52 lines · David Howells

__ip_append_data() can get into an infinite loop when asked to splice into a partially-built UDP message that has more than the frag-limit data and up to the MTU limit. Something like:

        pipe(pfd);
        sfd = socket(AF_INET, SOCK_DGRAM, 0);
        connect(sfd, ...);
        send(sfd, buffer, 8161, MSG_CONFIRM|MSG_MORE);
        write(pfd[1], buffer, 8);
        splice(pfd[0], 0, sfd, 0, 0x4ffe0ul, 0);

where the amount of data given to send() is dependent on the MTU size (in this instance an interface with an MTU of 8192).

The problem is that the calculation of the amount to copy in __ip_append_data() goes negative in two places, and, in the second place, this gets subtracted from the length remaining, thereby increasing it.

This happens when pagedlen > 0 (which happens for MSG_ZEROCOPY and MSG_SPLICE_PAGES), because the terms in:

        copy = datalen - transhdrlen - fraggap - pagedlen;

then mostly cancel when pagedlen is substituted for, leaving just -fraggap. This causes:

        length -= copy + transhdrlen;

to increase the length to more than the amount of data in msg->msg_iter, which causes skb_splice_from_iter() to be unable to fill the request and it returns less than 'copied' - which means that length never gets to 0 and we never exit the loop.

Fix this by:

(1) Insert a note about the dodgy calculation of 'copy'.

(2) If MSG_SPLICE_PAGES, clear copy if it is negative from the above equation, so that 'offset' isn't regressed and 'length' isn't increased, which will mean that length and thus copy should match the amount left in the iterator.

(3) When handling MSG_SPLICE_PAGES, give a warning and return -EIO if we're asked to splice more than is in the iterator. It might be better to not give the warning or even just give a 'short' write.

[!] Note that this ought to also affect MSG_ZEROCOPY, but MSG_ZEROCOPY avoids the problem by simply assuming that everything asked for got copied, not just the amount that was in the iterator. This is a potential bug for the future.

Quoted 12 lines
Fixes 7ac7c987850c ("udp: Convert udp_sendpage() to use MSG_SPLICE_PAGES")
Reported-by [email protected]
cc Willem de Bruijn <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
cc Paolo Abeni <[email protected]>
cc David Ahern <[email protected]>
cc Jens Axboe <[email protected]>

Thanks for limiting this to MSG_SPLICE_PAGES.

__ip6_append_data probably needs the same.

I see your point that the

  if (copy > 0) {
  } else {
    copy = 0;
  }

might apply to MSG_ZEROCOPY too. I'll take a look at that. For now this is a clear fix to a specific MSG_SPLICE_PAGES commit.

copy is recomputed on each iteration in the loop. The only fields it directly affects below this new line are offset and length. offset is only used in copy paths: "offset into linear skb".

So this changes length, the number of bytes still to be written.

copy -= -fraggap definitely seems off. You point out that it even can turn length negative?

The WARN_ON_ONCE, if it can be reached, will be user triggerable. Usually for those cases and when there is a viable return with error path, that is preferable. But if you prefer to taunt syzbot, ok. We can always remove this later.

Quoted 36 lines
---
 net/ipv4/ip_output.c |    9 +++++++++
 1 file changed, 9 insertions(+)
net/ipv4/ip_output.c +9 -0
index 6e70839257f7..91715603cf6e 100644
@@ -1158,10 +1158,15 @@ static int __ip_append_data(struct sock *sk,
 			}
 
 			copy = datalen - transhdrlen - fraggap - pagedlen;
+			/* [!] NOTE: copy will be negative if pagedlen>0
+			 * because then the equation reduces to -fraggap.
+			 */
 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
 				err = -EFAULT;
 				kfree_skb(skb);
 				goto error;
+			} else if (flags & MSG_SPLICE_PAGES) {
+				copy = 0;
 			}
 
 			offset += copy;
@@ -1209,6 +1214,10 @@ static int __ip_append_data(struct sock *sk,
 		} else if (flags & MSG_SPLICE_PAGES) {
 			struct msghdr *msg = from;
 
+			err = -EIO;
+			if (WARN_ON_ONCE(copy > msg->msg_iter.count))
+				goto error;
+
 			err = skb_splice_from_iter(skb, &msg->msg_iter, copy,
 						   sk->sk_allocation);
 			if (err < 0)
#2 lore raw
Quoted 2 lines · Willem de Bruijn

copy -= -fraggap definitely seems off. You point out that it even can turn length negative?

Yes. See the logging I posted:

	==>splice_to_socket() 6630
	udp_sendmsg(8,8)
	__ip_append_data(copy=-1,len=8, mtu=8192 skblen=8189 maxfl=8188)
	pagedlen 9 = 9 - 0
	copy -1 = 9 - 0 - 1 - 9
	length 8 -= -1 + 0

Since datalen and transhdrlen cancel, and fraggap is unsigned, if fraggap is non-zero, copy will be negative.

Quoted 4 lines

The WARN_ON_ONCE, if it can be reached, will be user triggerable. Usually for those cases and when there is a viable return with error path, that is preferable. But if you prefer to taunt syzbot, ok. We can always remove this later.

It shouldn't be possible for length to exceed msg->msg_iter.count (assuming there is a msg) coming from userspace; further, userspace can't directly specify MSG_SPLICE_PAGES.

Quoted 1 line

__ip6_append_data probably needs the same.

Good point. The arrangement of the code is a bit different, but I think it's substantially the same in this regard.

David

#3 lore raw
In reply to Willem de Bruijn
Quoted 1 line · Willem de Bruijn

__ip6_append_data probably needs the same.

Now that's interesting. __ip6_append_data() has a check for this and returns -EINVAL in this case:

		copy = datalen - transhdrlen - fraggap - pagedlen;
		if (copy < 0) {
			err = -EINVAL;
			goto error;
		}

but should I bypass that check for MSG_SPLICE_PAGES? It hits the check when it should be able to get past it. The code seems to go back to prehistoric times, so I'm not sure why it's there.

For an 8192 MTU, the breaking point is at a send of 8137 bytes. The attached test program iterates through different send sizes until it hits the point.

David
---
#define _GNU_SOURCE

#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/ip6.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/uio.h>

#define OSERROR(R, S) do { if ((long)(R) == -1L) { perror((S)); exit(1); } } while(0)

int main()

{
	struct sockaddr_storage ss;
	struct sockaddr_in6 sin6;
	void *buffer;
	unsigned int tmp;
	int pfd[2], sfd;
	int res, i;

	OSERROR(pipe(pfd), "pipe");

	sfd = socket(AF_INET6, SOCK_DGRAM, 0);
	OSERROR(sfd, "socket/2");

	memset(&sin6, 0, sizeof(sin6));
	sin6.sin6_family = AF_INET6;
	sin6.sin6_port = htons(7);
#warning set dest IPv6 address below
	sin6.sin6_addr.s6_addr32[0] = htonl(0x01020304);
	sin6.sin6_addr.s6_addr32[1] = htonl(0x05060708);
	sin6.sin6_addr.s6_addr32[2] = htonl(0x00000000);
	sin6.sin6_addr.s6_addr32[3] = htonl(0x00000001);
	OSERROR(connect(sfd, (struct sockaddr *)&sin6, sizeof(sin6)), "connect");

	buffer = mmap(NULL, 1024*1024, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
	OSERROR(buffer, "mmap");

	for (i = 1000; i < 65535; i++) {
		printf("%d\n", i);
		OSERROR(send(sfd, buffer, i, MSG_MORE), "send");

		OSERROR(write(pfd[1], buffer, 8), "write");

		OSERROR(splice(pfd[0], 0, sfd, 0, 0x4ffe0ul, 0), "splice");
	}
	return 0;
}
#4 lore raw
Quoted 13 lines · David Howells
Quoted 1 line · Willem de Bruijn

__ip6_append_data probably needs the same.

Now that's interesting. __ip6_append_data() has a check for this and returns -EINVAL in this case:

		copy = datalen - transhdrlen - fraggap - pagedlen;
		if (copy < 0) {
			err = -EINVAL;
			goto error;
		}
Quoted 3 lines

but should I bypass that check for MSG_SPLICE_PAGES? It hits the check when it should be able to get past it. The code seems to go back to prehistoric times, so I'm not sure why it's there.

Argh, saved by inconsistency between the two stacks.

I don't immediately understand the race that caused this code to move, in commit 232cd35d0804 ("ipv6: fix out of bound writes in __ip6_append_data()"). Maybe a race with a mtu update?

Technically there is no Fixes tag to apply, so this would not be a fix for net.

If we want equivalent behavior, a patch removing this branch is probably best sent to net-next, in a way that works from the start.

Reviewed-by on v1 1/1
#5 lore raw
In reply to David Howells
Quoted 52 lines · David Howells

__ip_append_data() can get into an infinite loop when asked to splice into a partially-built UDP message that has more than the frag-limit data and up to the MTU limit. Something like:

        pipe(pfd);
        sfd = socket(AF_INET, SOCK_DGRAM, 0);
        connect(sfd, ...);
        send(sfd, buffer, 8161, MSG_CONFIRM|MSG_MORE);
        write(pfd[1], buffer, 8);
        splice(pfd[0], 0, sfd, 0, 0x4ffe0ul, 0);

where the amount of data given to send() is dependent on the MTU size (in this instance an interface with an MTU of 8192).

The problem is that the calculation of the amount to copy in __ip_append_data() goes negative in two places, and, in the second place, this gets subtracted from the length remaining, thereby increasing it.

This happens when pagedlen > 0 (which happens for MSG_ZEROCOPY and MSG_SPLICE_PAGES), because the terms in:

        copy = datalen - transhdrlen - fraggap - pagedlen;

then mostly cancel when pagedlen is substituted for, leaving just -fraggap. This causes:

        length -= copy + transhdrlen;

to increase the length to more than the amount of data in msg->msg_iter, which causes skb_splice_from_iter() to be unable to fill the request and it returns less than 'copied' - which means that length never gets to 0 and we never exit the loop.

Fix this by:

(1) Insert a note about the dodgy calculation of 'copy'.

(2) If MSG_SPLICE_PAGES, clear copy if it is negative from the above equation, so that 'offset' isn't regressed and 'length' isn't increased, which will mean that length and thus copy should match the amount left in the iterator.

(3) When handling MSG_SPLICE_PAGES, give a warning and return -EIO if we're asked to splice more than is in the iterator. It might be better to not give the warning or even just give a 'short' write.

[!] Note that this ought to also affect MSG_ZEROCOPY, but MSG_ZEROCOPY avoids the problem by simply assuming that everything asked for got copied, not just the amount that was in the iterator. This is a potential bug for the future.

Quoted 12 lines
Fixes 7ac7c987850c ("udp: Convert udp_sendpage() to use MSG_SPLICE_PAGES")
Reported-by [email protected]
cc Willem de Bruijn <[email protected]>
cc "David S. Miller" <[email protected]>
cc Eric Dumazet <[email protected]>
cc Jakub Kicinski <[email protected]>
cc Paolo Abeni <[email protected]>
cc David Ahern <[email protected]>
cc Jens Axboe <[email protected]>

I noticed that this is still open in patchwork, no need to resend.

#6 lore raw
In reply to David Howells
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 4 lines · On Tue, 01 Aug 2023 16:48:53 +0100 you wrote:

__ip_append_data() can get into an infinite loop when asked to splice into a partially-built UDP message that has more than the frag-limit data and up to the MTU limit. Something like:

Quoted 8 lines
        pipe(pfd);
        sfd = socket(AF_INET, SOCK_DGRAM, 0);
        connect(sfd, ...);
        send(sfd, buffer, 8161, MSG_CONFIRM|MSG_MORE);
        write(pfd[1], buffer, 8);
        splice(pfd[0], 0, sfd, 0, 0x4ffe0ul, 0);

[...]

Here is the summary with links:
- [net] udp: Fix __ip_append_data()'s handling of MSG_SPLICE_PAGES

    https://git.kernel.org/netdev/net/c/0f71c9caf267

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html