ipv6: splicing a fragmented UDP datagram over an xfrm route warns in __ip6_append_data() and fails with EIO
Subsystems (2)
Hi David, Ido,
While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that sendfile() of a UDPv6 datagram that needs fragmentation, on a route through an xfrm tunnel state, hits the WARN_ON_ONCE() in the MSG_SPLICE_PAGES branch of __ip6_append_data(), and every such sendfile() fails with EIO:
WARNING: net/ipv6/ip6_output.c:1796 at __ip6_append_data+0x17ed/0x4b70, CPU#0: test/115 CPU: 0 UID: 65534 PID: 115 Comm: test Not tainted 7.4.0-rc3 #1 PREEMPT(lazy) RIP: 0010:__ip6_append_data+0x17ed/0x4b70 Call Trace: <TASK> ip6_make_skb+0x24d/0x420 udpv6_sendmsg+0x1f6f/0x2d70 sock_sendmsg+0x21f/0x360 splice_to_socket+0x8fa/0x1180 direct_splice_actor+0x164/0x6d0 splice_direct_to_actor+0x2b3/0x990 do_splice_direct+0x12b/0x1f0 do_sendfile+0x91b/0xf40 __x64_sys_sendfile64+0x195/0x1d0 do_syscall_64+0x116/0x630 entry_SYSCALL_64_after_hwframe+0x77/0x7f
When __ip6_append_data() starts a new skb for a fragment that is not the last one, it treats the dst's trailer space as data space:
if (datalen != length + fraggap) {
datalen += rt->dst.trailer_len;
}On the paged path, pagedlen was computed before that, so with an xfrm dst (trailer_len > 0)
copy = datalen - transhdrlen - fraggap - pagedlen;
is trailer_len rather than 0. getfrag() then copies those bytes from the message iterator into the linear part, but the branch right after it also runs when getfrag() succeeds:
if (copy > 0 &&
INDIRECT_CALL_1(getfrag, ip_generic_getfrag,
from, data + transhdrlen, offset,
copy, fraggap, skb) < 0) {
...
} else if (flags & MSG_SPLICE_PAGES) {
copy = 0;
}
offset += copy;
length -= copy + transhdrlen;so length stays trailer_len larger than what is left in msg->msg_iter. On the next pass the splice branch finds copy > msg->msg_iter.count, warns and returns -EIO. IPv4 is not affected: __ip_append_data() does not add trailer_len to datalen, so copy is 0 there.
The branch dates from ce650a166335 ("udp6: Fix __ip6_append_data()'s handling of MSG_SPLICE_PAGES"), when copy could be negative on this path. Since 736b380e28d0 ("ipv6: account for fraggap on the paged allocation path") a negative copy returns -EINVAL before this point, so dropping the branch should be enough. With it removed, the reproducer runs clean in 6 of 6 runs and all its sendfile() calls succeed, where they all fail with EIO before.
The reproducer creates a user and network namespace as an unprivileged user (uid 65534) and sets lo's MTU to 1280. It then adds an IPv6 ESP tunnel state ::1 -> ::1 with cbc(aes) and hmac(sha256), and an outbound policy for UDP to [::1]:9. Last, it calls sendfile() from a memfd into a connected UDPv6 socket for lengths of 1200 to 8000 bytes. The same happens with an IP-TFS state (mode 5); the source has a MODE define. On an x86_64 QEMU guest with 2 vCPUs and KASAN (gcc 13.3, config attached), the warning fires in every run (12 of 12), and each sendfile() longer than one fragment fails.
Checked against v7.4-rc3 (aebb72744764); the branch is the same in net-next (6ff4c5942a30) and ipsec (f0e68014f4ac). The earlier "WARNING in __ip6_append_data" (CVE-2023-52527, l2tp transhdrlen) is a different cause, and we found no report of this one.
The reproducer (repro.c) and the kernel config are attached.
Thanks,
Kyuwon Cho
// WARNING in __ip6_append_data: sendfile() of datagrams larger than the MTU into a UDPv6 socket whose traffic an // xfrm tunnel state carries. MODE is XFRM_MODE_TUNNEL (1) or IP-TFS (5).
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <fcntl.h>
#include <linux/netlink.h>
#include <linux/xfrm.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <unistd.h>
#ifndef MODE
#define MODE 5
#endif
#define ATTR_SA_DIR 33
#define SA_DIR_OUT 2
static int fd;
static void put_attr(char *buf, size_t *len, unsigned short type, const void *data, size_t dlen)
{
struct nlattr *a = (struct nlattr *)(buf + *len);
a->nla_type = type;
a->nla_len = NLA_HDRLEN + dlen;
memcpy((char *)a + NLA_HDRLEN, data, dlen);
*len += NLA_ALIGN(a->nla_len);
}static int req(unsigned short type, const void *body, size_t blen, const char *attrs, size_t alen)
{
char msg[4096] = {0}, rb[4096];
struct nlmsghdr *nh = (struct nlmsghdr *)msg;
memcpy(NLMSG_DATA(nh), body, blen);
memcpy((char *)NLMSG_DATA(nh) + NLMSG_ALIGN(blen), attrs, alen);
nh->nlmsg_len = NLMSG_LENGTH(NLMSG_ALIGN(blen) + alen);
nh->nlmsg_type = type;
nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
if (send(fd, msg, nh->nlmsg_len, 0) < 0 || recv(fd, rb, sizeof(rb), 0) < 0)
return -1;
return ((struct nlmsgerr *)NLMSG_DATA((struct nlmsghdr *)rb))->error;
}static void write_file(const char *path, const char *s)
{
int f = open(path, O_WRONLY);
if (f < 0 || write(f, s, strlen(s)) < 0)
exit(1);
close(f);
}int main(void)
{
struct { struct xfrm_algo a; char key[16]; } crypt = {0};
struct { struct xfrm_algo_auth a; char key[32]; } auth = {0};
struct xfrm_userpolicy_info pol = {0};
struct xfrm_usersa_info sa = {0};
struct xfrm_user_tmpl tmpl = {0};
struct sockaddr_in6 dst = {0};
struct ifreq ifr = {0};
static char buf[8192];
unsigned char dir = SA_DIR_OUT;
char attrs[512], map[32];
size_t al = 0;
int s, u, f, len, bad = 0;
snprintf(map, sizeof(map), "0 %d 1", getuid());
if (unshare(CLONE_NEWUSER | CLONE_NEWNET))
return 1;
write_file("/proc/self/setgroups", "deny");
write_file("/proc/self/uid_map", map);
snprintf(map, sizeof(map), "0 %d 1", getgid());
write_file("/proc/self/gid_map", map);
s = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, "lo");
ifr.ifr_mtu = 1280;
ioctl(s, SIOCSIFMTU, &ifr);
ifr.ifr_flags = IFF_UP;
ioctl(s, SIOCSIFFLAGS, &ifr);
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
/* Outbound ESP tunnel state ::1 -> ::1. */
inet_pton(AF_INET6, "::1", &sa.id.daddr);
inet_pton(AF_INET6, "::1", &sa.saddr);
sa.id.spi = htonl(0x100);
sa.id.proto = IPPROTO_ESP;
sa.family = AF_INET6;
sa.mode = MODE;
sa.reqid = 1;
sa.lft.soft_byte_limit = sa.lft.hard_byte_limit = XFRM_INF;
sa.lft.soft_packet_limit = sa.lft.hard_packet_limit = XFRM_INF;
strcpy(crypt.a.alg_name, "cbc(aes)");
crypt.a.alg_key_len = 128;
strcpy(auth.a.alg_name, "hmac(sha256)");
auth.a.alg_key_len = 256;
auth.a.alg_trunc_len = 128;
put_attr(attrs, &al, XFRMA_ALG_CRYPT, &crypt, sizeof(crypt));
put_attr(attrs, &al, XFRMA_ALG_AUTH_TRUNC, &auth, sizeof(auth));
put_attr(attrs, &al, ATTR_SA_DIR, &dir, 1);
printf("newsa: %d\n", req(XFRM_MSG_NEWSA, &sa, sizeof(sa), attrs, al));
/* Outbound policy for UDP to ::1 port 9 through that state. */
pol.sel.family = AF_INET6;
inet_pton(AF_INET6, "::1", &pol.sel.daddr);
pol.sel.prefixlen_d = 128;
pol.sel.proto = IPPROTO_UDP;
pol.sel.dport = htons(9);
pol.sel.dport_mask = 0xffff;
pol.dir = XFRM_POLICY_OUT;
pol.action = XFRM_POLICY_ALLOW;
pol.lft = sa.lft;
tmpl.id = sa.id;
tmpl.family = AF_INET6;
tmpl.saddr = sa.saddr;
tmpl.reqid = 1;
tmpl.mode = MODE;
tmpl.aalgos = tmpl.ealgos = tmpl.calgos = ~0;
al = 0;
put_attr(attrs, &al, XFRMA_TMPL, &tmpl, sizeof(tmpl));
printf("newpolicy: %d\n", req(XFRM_MSG_NEWPOLICY, &pol, sizeof(pol), attrs, al));
u = socket(AF_INET6, SOCK_DGRAM, 0);
dst.sin6_family = AF_INET6;
dst.sin6_port = htons(9);
inet_pton(AF_INET6, "::1", &dst.sin6_addr);
connect(u, (struct sockaddr *)&dst, sizeof(dst));
f = memfd_create("data", 0);
memset(buf, 0x33, sizeof(buf));
if (write(f, buf, sizeof(buf)) < 0)
return 1;
for (len = 1200; len <= 8000; len += 50) {
lseek(f, 0, SEEK_SET);
if (sendfile(u, f, NULL, len) < 0)
bad++;
}
printf("done, %d sendfile errors\n", bad);
return 0;
}Quoted 58 lines · Kyuwon Cho
Hi David, Ido,
While testing v7.4-rc3 with an LLM-guided test generator that we are developing as a research prototype, we found that
sendfile()of a UDPv6 datagram that needs fragmentation, on a route through an xfrm tunnel state, hits theWARN_ON_ONCE()in theMSG_SPLICE_PAGESbranch of__ip6_append_data(), and every suchsendfile()fails with EIO:WARNING: net/ipv6/ip6_output.c:1796 at __ip6_append_data+0x17ed/0x4b70, CPU#0: test/115 CPU: 0 UID: 65534 PID: 115 Comm: test Not tainted 7.4.0-rc3 #1 PREEMPT(lazy) RIP: 0010:__ip6_append_data+0x17ed/0x4b70 Call Trace: <TASK> ip6_make_skb+0x24d/0x420 udpv6_sendmsg+0x1f6f/0x2d70 sock_sendmsg+0x21f/0x360 splice_to_socket+0x8fa/0x1180 direct_splice_actor+0x164/0x6d0 splice_direct_to_actor+0x2b3/0x990 do_splice_direct+0x12b/0x1f0 do_sendfile+0x91b/0xf40 __x64_sys_sendfile64+0x195/0x1d0 do_syscall_64+0x116/0x630 entry_SYSCALL_64_after_hwframe+0x77/0x7fWhen
__ip6_append_data()starts a new skb for a fragment that is not the last one, it treats the dst's trailer space as data space:if (datalen != length + fraggap) { datalen += rt->dst.trailer_len; }On the paged path, pagedlen was computed before that, so with an xfrm dst (
trailer_len> 0)copy = datalen - transhdrlen - fraggap - pagedlen;is
trailer_lenrather than 0.getfrag()then copies those bytes from the message iterator into the linear part, but the branch right after it also runs whengetfrag()succeeds:if (copy > 0 && INDIRECT_CALL_1(getfrag, ip_generic_getfrag, from, data + transhdrlen, offset, copy, fraggap, skb) < 0) { ... } else if (flags & MSG_SPLICE_PAGES) { copy = 0; } offset += copy; length -= copy + transhdrlen;so length stays
trailer_lenlarger than what is left inmsg->msg_iter. On the next pass the splice branch finds copy >msg->msg_iter.count, warns and returns-EIO. IPv4 is not affected:__ip_append_data()does not addtrailer_lento datalen, so copy is 0 there.
Quoted 7 lines
The branch dates from
ce650a166335("udp6: Fix __ip6_append_data()'s handling ofMSG_SPLICE_PAGES"), when copy could be negative on this path. Since736b380e28d0("ipv6: account for fraggap on the paged allocation path") a negative copy returns-EINVALbefore this point, so dropping the branch should be enough. With it removed, the reproducer runs clean in 6 of 6 runs and all itssendfile()calls succeed, where they all fail with EIO before.
Please send this fix with the following tag:
Thanks