Skip to contentSkip to conversation

ipv6 UDP MSG_MORE oops fix

PATCH New David S. Miller 2 participants 7 messages Aug 8, 2003 – Oct 27, 2003

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

Yoshfuji-san, I found the MSG_MORE udp bug.

If "np->pending" is true, we call ip_dst_store() with uninitialized dst, it could be any kind of garbage. This is dereferenced and we crash.

If np->pending, we have some 'dst' stored into the socket already. So no need to relookup dst or anything like that.

This is the fix I am using, on top of Miyazawa-san's ah6_input and authentication error patches.

# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.1167 -> 1.1168
# net/ipv6/ip6_output.c 1.40 -> 1.41

#	      net/ipv6/raw.c	1.38    -> 1.39   
#	      net/ipv6/udp.c	1.45    -> 1.46   

#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 03/08/07 [email protected] 1.1168
# [IPV6]: Make sure errors propagate properly in {udp,raw} sendmsg. # --------------------------------------------
#

net/ipv6/ip6_output.c +0 -1
diff -Nru a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
@@ -209,7 +209,6 @@
 	int seg_len = skb->len;
 	int hlimit;
 	u32 mtu;
-	int err = 0;
 
 	if (opt) {
 		int head_room;
net/ipv6/raw.c +1 -1
diff -Nru a/net/ipv6/raw.c b/net/ipv6/raw.c
@@ -659,7 +659,7 @@
 		fl.oif = np->mcast_oif;
 
 	dst = ip6_dst_lookup(sk, &fl);
-	if (dst->error)
+	if ((err = dst->error))
 		goto out;
 
 	if (hlimit < 0) {
net/ipv6/udp.c +8 -5
diff -Nru a/net/ipv6/udp.c b/net/ipv6/udp.c
@@ -811,8 +811,10 @@
 		 * The socket lock must be held while it's corked.
 		 */
 		lock_sock(sk);
-		if (likely(up->pending))
+		if (likely(up->pending)) {
+			dst = NULL;
 			goto do_append_data;
+		}
 		release_sock(sk);
 	}
 	ulen += sizeof(struct udphdr);
@@ -929,7 +931,7 @@
 		fl.oif = np->mcast_oif;
 
 	dst = ip6_dst_lookup(sk, &fl);
-	if (dst->error)
+	if ((err = dst->error))
 		goto out;
 
 	if (hlimit < 0) {
@@ -968,9 +970,10 @@
 	else if (!corkreq)
 		err = udp_v6_push_pending_frames(sk, up);
 
-	ip6_dst_store(sk, dst,
-		      !ipv6_addr_cmp(&fl.fl6_dst, &np->daddr) ?
-		      &np->daddr : NULL);
+	if (dst)
+		ip6_dst_store(sk, dst,
+			      !ipv6_addr_cmp(&fl.fl6_dst, &np->daddr) ?
+			      &np->daddr : NULL);
 	if (err > 0)
 		err = np->recverr ? net_xmit_errno(err) : 0;
 	release_sock(sk);
#2 lore raw

Hello.

This thread would be too old but anyway...

In article <[email protected]> (at Fri, 08 Aug 2003 17:08:39 +0900 (JST)), YOSHIFUJI Hideaki / 吉藤英明 <[email protected]> says:

Quoted 6 lines

In article <[email protected]> (at Thu, 7 Aug 2003 22:34:21 -0700), "David S. Miller" <[email protected]> says:

Quoted 1 line

Yoshfuji-san, I found the MSG_MORE udp bug.

Yes, thanks. This is what I told you before. I'm going to test if problems go away with this patch.

This fixes oops, but there remains several issues. I'm about to send 3 patches to fix them.

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag [2/3] NET: store cork'ing flow information in common storage in inet_opt [3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

Thanks in advance.

Signature · 2 lines
Hideaki YOSHIFUJI @ USAGI Project <[email protected]>
GPG FP: 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
#3 lore raw

PATCH 1/3 IPv6: Fix odd IPv6 header in UDPv6 packets when sending MSG_MORE flag

Hello.

Quoted 5 lines

I'm about to send 3 patches to fix them.

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag [2/3] NET: store cork'ing flow information in common storage in inet_opt [3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag

D: Broken IPv6 header for UDPv6 was generated with MSG_MORE flag.
D: Remember cork'ed flow information in real storage.

===== include/linux/ipv6.h 1.12 vs edited =====

include/linux/ipv6.h +2 -1
@@ -174,6 +174,7 @@
 #include <net/if_inet6.h>       /* struct ipv6_mc_socklist */
 #include <linux/tcp.h>
 #include <linux/udp.h>
+#include <net/flow.h>
 
 /* 
    This structure contains results of exthdrs parsing
@@ -234,7 +235,7 @@
 	struct {
 		struct ipv6_txoptions *opt;
 		struct rt6_info	*rt;
-		struct flowi *fl;
+		struct flowi fl;
 		int hop_limit;
 	} cork;
 };

===== net/ipv6/ip6_output.c 1.44 vs edited =====

net/ipv6/ip6_output.c +4 -8
@@ -1239,7 +1239,7 @@
 		}
 		dst_hold(&rt->u.dst);
 		np->cork.rt = rt;
-		np->cork.fl = fl;
+		np->cork.fl = *fl;
 		np->cork.hop_limit = hlimit;
 		inet->cork.fragsize = mtu = dst_pmtu(&rt->u.dst);
 		inet->cork.length = 0;
@@ -1423,7 +1423,7 @@
 	struct ipv6hdr *hdr;
 	struct ipv6_txoptions *opt = np->cork.opt;
 	struct rt6_info *rt = np->cork.rt;
-	struct flowi *fl = np->cork.fl;
+	struct flowi *fl = &np->cork.fl;
 	unsigned char proto = fl->proto;
 	int err = 0;
 
@@ -1487,9 +1487,7 @@
 		dst_release(&np->cork.rt->u.dst);
 		np->cork.rt = NULL;
 	}
-	if (np->cork.fl) {
-		np->cork.fl = NULL;
-	}
+	memset(&np->cork.fl, 0, sizeof(np->cork.fl));
 	return err;
 error:
 	goto out;
@@ -1514,7 +1512,5 @@
 		dst_release(&np->cork.rt->u.dst);
 		np->cork.rt = NULL;
 	}
-	if (np->cork.fl) {
-		np->cork.fl = NULL;
-	}
+	memset(&np->cork.fl, 0, sizeof(np->cork.fl));
 }

===== net/ipv6/udp.c 1.50 vs edited =====

net/ipv6/udp.c +26 -26

132 lines not on this page · raw message

Signature · 2 lines
Hideaki YOSHIFUJI @ USAGI Project <[email protected]>
GPG FP: 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
#4 lore raw

PATCH 2/3 NET: store cork'ing flow information in common storage

In reply to YOSHIFUJI Hideaki / 吉藤英明

Hello.

Quoted 5 lines

I'm about to send 3 patches to fix them.

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag [2/3] NET: store cork'ing flow information in common storage in inet_opt [3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

[2/3] NET: store cork'ing flow information in common storage in inet_opt.

include/linux/ip.h +2 -0
@@ -83,6 +83,7 @@
 #include <linux/types.h>
 #include <net/sock.h>
 #include <linux/igmp.h>
+#include <net/flow.h>
 
 struct ip_options {
   __u32		faddr;				/* Saved first hop address */
@@ -141,6 +142,7 @@
 		struct rtable		*rt;
 		int			length; /* Total length of all frames */
 		u32			addr;
+		struct flowi		fl;
 	} cork;
 };
 
include/linux/ipv6.h +0 -2
@@ -174,7 +174,6 @@
 #include <net/if_inet6.h>       /* struct ipv6_mc_socklist */
 #include <linux/tcp.h>
 #include <linux/udp.h>
-#include <net/flow.h>
 
 /* 
    This structure contains results of exthdrs parsing
@@ -235,7 +234,6 @@
 	struct {
 		struct ipv6_txoptions *opt;
 		struct rt6_info	*rt;
-		struct flowi fl;
 		int hop_limit;
 	} cork;
 };
include/linux/udp.h +1 -5
@@ -44,13 +44,9 @@
 	unsigned int	corkflag;	/* Cork is required */
   	__u16		encap_type;	/* Is this an Encapsulation socket? */
 	/*
-	 * Following members retains the infomation to create a UDP header
+	 * Following member retains the infomation to create a UDP header
 	 * when the socket is uncorked.
 	 */
-	u32		saddr;		/* source address */
-	u32		daddr;		/* destination address */
-	__u16		sport;		/* source port */
-	__u16		dport;		/* destination port */
 	__u16		len;		/* total length of pending frames */
 };
 
net/ipv4/udp.c +11 -9
@@ -398,6 +398,8 @@
  */
 static int udp_push_pending_frames(struct sock *sk, struct udp_opt *up)
 {
+	struct inet_opt *inet = inet_sk(sk);
+	struct flowi *fl = &inet->cork.fl;
 	struct sk_buff *skb;
 	struct udphdr *uh;
 	int err = 0;
@@ -410,8 +412,8 @@
 	 * Create a UDP header
 	 */
 	uh = skb->h.uh;
-	uh->source = up->sport;
-	uh->dest = up->dport;
+	uh->source = fl->fl_ip_sport;
+	uh->dest = fl->fl_ip_dport;
 	uh->len = htons(up->len);
 	uh->check = 0;
 
@@ -426,12 +428,12 @@
 		 */
 		if (skb->ip_summed == CHECKSUM_HW) {
 			skb->csum = offsetof(struct udphdr, check);
-			uh->check = ~csum_tcpudp_magic(up->saddr, up->daddr,
+			uh->check = ~csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 					up->len, IPPROTO_UDP, 0);
 		} else {
 			skb->csum = csum_partial((char *)uh,
 					sizeof(struct udphdr), skb->csum);
-			uh->check = csum_tcpudp_magic(up->saddr, up->daddr,
+			uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 					up->len, IPPROTO_UDP, skb->csum);
 			if (uh->check == 0)
 				uh->check = -1;
@@ -456,7 +458,7 @@
 		skb_queue_walk(&sk->sk_write_queue, skb) {
 			csum = csum_add(csum, skb->csum);
 		}
-		uh->check = csum_tcpudp_magic(up->saddr, up->daddr,
+		uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst,
 				up->len, IPPROTO_UDP, csum);
 		if (uh->check == 0)
 			uh->check = -1;
@@ -636,10 +638,10 @@
 	/*
 	 *	Now cork the socket to pend data.
 	 */
-	up->daddr = daddr;
-	up->dport = dport;
-	up->saddr = saddr;
-	up->sport = inet->sport;
+	inet->cork.fl.fl4_dst = daddr;
+	inet->cork.fl.fl_ip_dport = dport;
+	inet->cork.fl.fl4_src = saddr;
+	inet->cork.fl.fl_ip_sport = inet->sport;
 	up->pending = 1;
 
 do_append_data:
net/ipv6/udp.c +6 -7
@@ -720,8 +720,8 @@
 {
 	struct sk_buff *skb;
 	struct udphdr *uh;
-	struct ipv6_pinfo *np = inet6_sk(sk);
-	struct flowi *fl = &np->cork.fl;
+	struct inet_opt *inet = inet_sk(sk);
+	struct flowi *fl = &inet->cork.fl;
 	int err = 0;
 
 	/* Grab the skbuff where UDP header space exists. */
@@ -783,7 +783,7 @@
 	struct in6_addr *daddr;
 	struct ipv6_txoptions *opt = NULL;
 	struct ip6_flowlabel *flowlabel = NULL;
-	struct flowi *fl = &np->cork.fl;
+	struct flowi *fl = &inet->cork.fl;
 	struct dst_entry *dst;
 	int addr_len = msg->msg_namelen;
 	int ulen = len;
@@ -830,7 +830,7 @@
 		if (sin6->sin6_port == 0)
 			return -EINVAL;
 
-		up->dport = sin6->sin6_port;
+		fl->fl_ip_dport = sin6->sin6_port;
 		daddr = &sin6->sin6_addr;
 
 		if (np->sndflow) {
@@ -859,7 +859,7 @@
 		if (sk->sk_state != TCP_ESTABLISHED)
 			return -EDESTADDRREQ;
 
-		up->dport = inet->dport;
+		fl->fl_ip_dport = inet->dport;
 		daddr = &np->daddr;
 		fl->fl6_flowlabel = np->flow_label;
 	}
@@ -874,7 +874,7 @@
 
 		sin.sin_family = AF_INET;
 		sin.sin_addr.s_addr = daddr->s6_addr32[3];
-		sin.sin_port = up->dport;
+		sin.sin_port = inet->cork.fl.fl_ip_dport;
 		msg->msg_name = (struct sockaddr *)(&sin);
 		msg->msg_namelen = sizeof(sin);
 		fl6_sock_release(flowlabel);
@@ -911,7 +911,6 @@
 	ipv6_addr_copy(&fl->fl6_dst, daddr);
 	if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
 		ipv6_addr_copy(&fl->fl6_src, &np->saddr);
-	fl->fl_ip_dport = up->dport;
 	fl->fl_ip_sport = inet->sport;
 	
 	/* merge ip6_build_xmit from ip6_output */
net/ipv6/ip6_output.c +5 -4
@@ -1239,7 +1239,7 @@
 		}
 		dst_hold(&rt->u.dst);
 		np->cork.rt = rt;
-		np->cork.fl = *fl;
+		inet->cork.fl = *fl;
 		np->cork.hop_limit = hlimit;
 		inet->cork.fragsize = mtu = dst_pmtu(&rt->u.dst);
 		inet->cork.length = 0;
@@ -1250,6 +1250,7 @@
 		transhdrlen += exthdrlen;
 	} else {
 		rt = np->cork.rt;
+		fl = &inet->cork.fl;
 		if (inet->cork.flags & IPCORK_OPT)
 			opt = np->cork.opt;
 		transhdrlen = 0;
@@ -1423,7 +1424,7 @@
 	struct ipv6hdr *hdr;
 	struct ipv6_txoptions *opt = np->cork.opt;
 	struct rt6_info *rt = np->cork.rt;
-	struct flowi *fl = &np->cork.fl;
+	struct flowi *fl = &inet->cork.fl;
 	unsigned char proto = fl->proto;
 	int err = 0;
 
@@ -1487,7 +1488,7 @@
 		dst_release(&np->cork.rt->u.dst);
 		np->cork.rt = NULL;
 	}
-	memset(&np->cork.fl, 0, sizeof(np->cork.fl));
+	memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
 	return err;
 error:
 	goto out;
@@ -1512,5 +1513,5 @@
 		dst_release(&np->cork.rt->u.dst);
 		np->cork.rt = NULL;
 	}
-	memset(&np->cork.fl, 0, sizeof(np->cork.fl));
+	memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
 }
Signature · 2 lines
Hideaki YOSHIFUJI @ USAGI Project <[email protected]>
GPG FP: 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
#5 lore raw

PATCH 3/3 IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

In reply to YOSHIFUJI Hideaki / 吉藤英明

Hello.

Quoted 5 lines

I'm about to send 3 patches to fix them.

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag [2/3] NET: store cork'ing flow information in common storage in inet_opt [3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

[3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

D: Fixing breakage of sendmsg to IPv4-mapped address via UDPv6 socket;
D: check destination address before checking cork flag to process
D: appropriately.

net/ipv4/udp.c +7 -2
@@ -522,8 +522,13 @@
 	 	 * The socket lock must be held while it's corked.
 		 */
 		lock_sock(sk);
-		if (likely(up->pending))
+		if (likely(up->pending)) {
+			if (unlikely(up->pending != AF_INET)) {
+				release_sock(sk);
+				return -EINVAL;
+			}
  			goto do_append_data;
+		}
 		release_sock(sk);
 	}
 	ulen += sizeof(struct udphdr);
@@ -642,7 +647,7 @@
 	inet->cork.fl.fl_ip_dport = dport;
 	inet->cork.fl.fl4_src = saddr;
 	inet->cork.fl.fl_ip_sport = inet->sport;
-	up->pending = 1;
+	up->pending = AF_INET;
 
 do_append_data:
 	up->len += ulen;
net/ipv6/udp.c +52 -33

118 lines not on this page · raw message

Signature · 2 lines
Hideaki YOSHIFUJI @ USAGI Project <[email protected]>
GPG FP: 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
#6 lore raw
In reply to YOSHIFUJI Hideaki / 吉藤英明
Quoted 3 lines · On Sun, 26 Oct 2003 18:50:23 +0900 (JST) YOSHIFUJI Hideaki / _$B5HF#1QL@…

[1/3] IPv6: Odd IPv6 header in UDPv6 packets when sending MSG_MORE flag [2/3] NET: store cork'ing flow information in common storage in inet_opt [3/3] IPv6: breakage of sendmsg to IPv4-mapped address via UDPv6 socket

All three patches applied, thanks Yoshfuji.