ip_mc_source() and ip6_mc_source() switch the mode of an empty socket filter before they look up or add the source, but do not restore it when the operation then fails. For example, after IP_ADD_MEMBERSHIP or IPV6_JOIN_GROUP the socket filter is EXCLUDE with no sources. IP_DROP_SOURCE_MEMBERSHIP or MCAST_LEAVE_SOURCE_GROUP for a source which was never added returns EADDRNOTAVAIL, but leaves the filter in INCLUDE mode with no sources, so the socket silently stops receiving the group. In a QEMU guest running an unpatched kernel, a UDP socket received 4 of 4 group datagrams before such a call and 0 of 4 afterwards, for both IPv4 and IPv6. No privilege is needed. The add paths leave partial state behind as well. They ignore errors returned while adding the source to the interface filter, so if the device-side source entry cannot be allocated, the socket-side filter is still committed and setsockopt() returns success. MCAST_JOIN_SOURCE_GROUP and IP_ADD_SOURCE_MEMBERSHIP create an INCLUDE membership before adding the source and keep that empty membership if the source update fails. Restore the previous mode when an operation on an empty filter fails. Return device-side allocation errors before publishing the socket filter. Drop a membership which the same setsockopt() call created. This was found while testing memcg accounting with slab fault injection; review of the same error paths then found the EADDRNOTAVAIL case above. Before the fix, injected IPv4 and IPv6 allocation failures either returned success or left the filter in INCLUDE mode with no source. After the fix, the EADDRNOTAVAIL case keeps the EXCLUDE filter and the group is still received, and injected allocation failures return ENOBUFS and preserve the previous filter and membership state. Kernel selftests were not run. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Yingjie Wang --- Tested on net/main e203d8a4751d with this series: x86_64 W=1 kernel build, affected-object builds for allmodconfig, i386 and CONFIG_IP_MULTICAST=n, and the QEMU reproducers and regression tests described in the cover letter. net/ipv4/igmp.c | 74 ++++++++++++++++++++++++++-------------- net/ipv4/ip_sockglue.c | 33 ++++++++++-------- net/ipv6/ipv6_sockglue.c | 11 +++++- net/ipv6/mcast.c | 16 +++++++-- 4 files changed, 92 insertions(+), 42 deletions(-) diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c index 52cde8782eeb..bfa448028f8a 100644 --- a/net/ipv4/igmp.c +++ b/net/ipv4/igmp.c @@ -2491,9 +2491,10 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct struct ip_mc_socklist *pmc; struct in_device *in_dev = NULL; struct inet_sock *inet = inet_sk(sk); - struct ip_sf_socklist *psl; + struct ip_sf_socklist *newpsl = NULL, *psl; struct net *net = sock_net(sk); int leavegroup = 0; + int oldmode = -1; int i, j, rv; if (!ipv4_is_multicast(addr)) @@ -2530,9 +2531,10 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct } } else if (pmc->sfmode != omode) { /* allow mode switches for empty-set filters */ + oldmode = pmc->sfmode; ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); - ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, - NULL, 0); + ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, oldmode, 0, + NULL, 0); pmc->sfmode = omode; } @@ -2572,8 +2574,17 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct err = -ENOBUFS; goto done; } + rv = 1; /* > 0 for insert logic below if sl_count is 0 */ + for (i = 0; psl && i < psl->sl_count; i++) { + rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, + sizeof(__be32)); + if (rv == 0) + break; + } + if (rv == 0) /* address already there is an error */ + goto done; + if (!psl || psl->sl_count == psl->sl_max) { - struct ip_sf_socklist *newpsl; int count = IP_SFBLOCK; if (psl) @@ -2585,10 +2596,30 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct goto done; } newpsl->sl_max = count; - newpsl->sl_count = count - IP_SFBLOCK; + newpsl->sl_count = psl ? psl->sl_count : 0; + if (psl) { + for (j = 0; j < psl->sl_count; j++) + newpsl->sl_addr[j] = psl->sl_addr[j]; + } + for (j = newpsl->sl_count - 1; j >= i; j--) + newpsl->sl_addr[j + 1] = newpsl->sl_addr[j]; + newpsl->sl_addr[i] = mreqs->imr_sourceaddr; + newpsl->sl_count++; + } + + /* update the interface list */ + err = ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, + &mreqs->imr_sourceaddr, 1); + if (err) { + if (newpsl) + sock_kfree_s(sk, newpsl, + struct_size(newpsl, sl_addr, + newpsl->sl_max)); + goto done; + } + + if (newpsl) { if (psl) { - for (i = 0; i < psl->sl_count; i++) - newpsl->sl_addr[i] = psl->sl_addr[i]; /* decrease mem now to avoid the memleak warning */ atomic_sub(struct_size(psl, sl_addr, psl->sl_max), &sk->sk_omem_alloc); @@ -2596,26 +2627,19 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct rcu_assign_pointer(pmc->sflist, newpsl); if (psl) kfree_rcu(psl, rcu); - psl = newpsl; - } - rv = 1; /* > 0 for insert logic below if sl_count is 0 */ - for (i = 0; i < psl->sl_count; i++) { - rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, - sizeof(__be32)); - if (rv == 0) - break; + } else { + for (j = psl->sl_count - 1; j >= i; j--) + psl->sl_addr[j + 1] = psl->sl_addr[j]; + psl->sl_addr[i] = mreqs->imr_sourceaddr; + psl->sl_count++; } - if (rv == 0) /* address already there is an error */ - goto done; - for (j = psl->sl_count-1; j >= i; j--) - psl->sl_addr[j+1] = psl->sl_addr[j]; - psl->sl_addr[i] = mreqs->imr_sourceaddr; - psl->sl_count++; - err = 0; - /* update the interface list */ - ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, - &mreqs->imr_sourceaddr, 1); done: + if (err && oldmode >= 0) { + ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, oldmode, 0, + NULL, 0); + ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); + pmc->sfmode = oldmode; + } if (leavegroup) err = ip_mc_leave_group(sk, &imr); return err; diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c index e06c1f48ecad..b4d466ddc80b 100644 --- a/net/ipv4/ip_sockglue.c +++ b/net/ipv4/ip_sockglue.c @@ -723,7 +723,9 @@ static int do_mcast_group_source(struct sock *sk, int optname, { struct group_source_req greqs; struct ip_mreq_source mreqs; + struct ip_mreqn joined = {}; struct sockaddr_in *psin; + bool drop_group = false; int omode, add, err; err = copy_group_source_from_sockptr(&greqs, optval, optlen); @@ -747,23 +749,24 @@ static int do_mcast_group_source(struct sock *sk, int optname, omode = MCAST_EXCLUDE; add = 0; } else if (optname == MCAST_JOIN_SOURCE_GROUP) { - struct ip_mreqn mreq; - psin = (struct sockaddr_in *)&greqs.gsr_group; - mreq.imr_multiaddr = psin->sin_addr; - mreq.imr_address.s_addr = 0; - mreq.imr_ifindex = greqs.gsr_interface; - err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); + joined.imr_multiaddr = psin->sin_addr; + joined.imr_ifindex = greqs.gsr_interface; + err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE); if (err && err != -EADDRINUSE) return err; - greqs.gsr_interface = mreq.imr_ifindex; + drop_group = !err; + greqs.gsr_interface = joined.imr_ifindex; omode = MCAST_INCLUDE; add = 1; } else /* MCAST_LEAVE_SOURCE_GROUP */ { omode = MCAST_INCLUDE; add = 0; } - return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface); + err = ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface); + if (err && drop_group) + ip_mc_leave_group(sk, &joined); + return err; } static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) @@ -1286,7 +1289,9 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, case IP_ADD_SOURCE_MEMBERSHIP: case IP_DROP_SOURCE_MEMBERSHIP: { + struct ip_mreqn joined = {}; struct ip_mreq_source mreqs; + bool drop_group = false; int omode, add; if (optlen != sizeof(struct ip_mreq_source)) @@ -1302,14 +1307,12 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, omode = MCAST_EXCLUDE; add = 0; } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { - struct ip_mreqn mreq; - - mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; - mreq.imr_address.s_addr = mreqs.imr_interface; - mreq.imr_ifindex = 0; - err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); + joined.imr_multiaddr.s_addr = mreqs.imr_multiaddr; + joined.imr_address.s_addr = mreqs.imr_interface; + err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE); if (err && err != -EADDRINUSE) break; + drop_group = !err; omode = MCAST_INCLUDE; add = 1; } else /* IP_DROP_SOURCE_MEMBERSHIP */ { @@ -1317,6 +1320,8 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, add = 0; } err = ip_mc_source(add, omode, sk, &mreqs, 0); + if (err && drop_group) + ip_mc_leave_group(sk, &joined); break; } case MCAST_JOIN_GROUP: diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c index 4b3536571c98..82573260b740 100644 --- a/net/ipv6/ipv6_sockglue.c +++ b/net/ipv6/ipv6_sockglue.c @@ -147,6 +147,7 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname, sockptr_t optval, int optlen) { struct group_source_req greqs; + bool drop_group = false; int omode, add; int ret; @@ -175,13 +176,21 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname, /* prior join w/ different source is ok */ if (retv && retv != -EADDRINUSE) return retv; + drop_group = !retv; omode = MCAST_INCLUDE; add = 1; } else /* MCAST_LEAVE_SOURCE_GROUP */ { omode = MCAST_INCLUDE; add = 0; } - return ip6_mc_source(add, omode, sk, &greqs); + ret = ip6_mc_source(add, omode, sk, &greqs); + if (ret && drop_group) { + struct sockaddr_in6 *psin6; + + psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; + ipv6_sock_mc_drop(sk, greqs.gsr_interface, &psin6->sin6_addr); + } + return ret; } static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c index ecef55f26189..0d982f7d29cd 100644 --- a/net/ipv6/mcast.c +++ b/net/ipv6/mcast.c @@ -363,6 +363,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk, struct ipv6_mc_socklist *pmc; struct inet6_dev *idev; int leavegroup = 0; + int oldmode = -1; int i, j; int err; @@ -403,8 +404,9 @@ int ip6_mc_source(int add, int omode, struct sock *sk, } } else if (pmc->sfmode != omode) { /* allow mode switches for empty-set filters */ + oldmode = pmc->sfmode; ip6_mc_add_src(idev, group, omode, 0, NULL, 0); - ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0); + ip6_mc_del_src(idev, group, oldmode, 0, NULL, 0); pmc->sfmode = omode; } @@ -485,7 +487,12 @@ int ip6_mc_source(int add, int omode, struct sock *sk, newpsl->sl_addr[i - 1] = *source; /* update the interface list */ - ip6_mc_add_src(idev, group, omode, 1, source, 1); + err = ip6_mc_add_src(idev, group, omode, 1, source, 1); + if (err) { + sock_kfree_s(sk, newpsl, + struct_size(newpsl, sl_addr, newpsl->sl_max)); + goto done; + } if (psl) atomic_sub(struct_size(psl, sl_addr, psl->sl_max), @@ -494,6 +501,11 @@ int ip6_mc_source(int add, int omode, struct sock *sk, kfree_rcu(psl, rcu); err = 0; done: + if (err && oldmode >= 0) { + ip6_mc_add_src(idev, group, oldmode, 0, NULL, 0); + ip6_mc_del_src(idev, group, omode, 0, NULL, 0); + pmc->sfmode = oldmode; + } mutex_unlock(&idev->mc_lock); in6_dev_put(idev); if (leavegroup) -- 2.43.0