Skip to content

Commit 8c0de6e

Browse files
congwangdavem330
authored andcommitted
ipv6: fix memory leaks on IPV6_ADDRFORM path
IPV6_ADDRFORM causes resource leaks when converting an IPv6 socket to IPv4, particularly struct ipv6_ac_socklist. Similar to struct ipv6_mc_socklist, we should just close it on this path. This bug can be easily reproduced with the following C program: #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> int main() { int s, value; struct sockaddr_in6 addr; struct ipv6_mreq m6; s = socket(AF_INET6, SOCK_DGRAM, 0); addr.sin6_family = AF_INET6; addr.sin6_port = htons(5000); inet_pton(AF_INET6, "::ffff:192.168.122.194", &addr.sin6_addr); connect(s, (struct sockaddr *)&addr, sizeof(addr)); inet_pton(AF_INET6, "fe80::AAAA", &m6.ipv6mr_multiaddr); m6.ipv6mr_interface = 5; setsockopt(s, SOL_IPV6, IPV6_JOIN_ANYCAST, &m6, sizeof(m6)); value = AF_INET; setsockopt(s, SOL_IPV6, IPV6_ADDRFORM, &value, sizeof(value)); close(s); return 0; } Reported-by: [email protected] Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Cong Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 27a2145 commit 8c0de6e

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

include/net/addrconf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex,
274274
const struct in6_addr *addr);
275275
int ipv6_sock_ac_drop(struct sock *sk, int ifindex,
276276
const struct in6_addr *addr);
277+
void __ipv6_sock_ac_close(struct sock *sk);
277278
void ipv6_sock_ac_close(struct sock *sk);
278279

279280
int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr);

net/ipv6/anycast.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,15 @@ int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
183183
return 0;
184184
}
185185

186-
void ipv6_sock_ac_close(struct sock *sk)
186+
void __ipv6_sock_ac_close(struct sock *sk)
187187
{
188188
struct ipv6_pinfo *np = inet6_sk(sk);
189189
struct net_device *dev = NULL;
190190
struct ipv6_ac_socklist *pac;
191191
struct net *net = sock_net(sk);
192192
int prev_index;
193193

194-
if (!np->ipv6_ac_list)
195-
return;
196-
197-
rtnl_lock();
194+
ASSERT_RTNL();
198195
pac = np->ipv6_ac_list;
199196
np->ipv6_ac_list = NULL;
200197

@@ -211,6 +208,16 @@ void ipv6_sock_ac_close(struct sock *sk)
211208
sock_kfree_s(sk, pac, sizeof(*pac));
212209
pac = next;
213210
}
211+
}
212+
213+
void ipv6_sock_ac_close(struct sock *sk)
214+
{
215+
struct ipv6_pinfo *np = inet6_sk(sk);
216+
217+
if (!np->ipv6_ac_list)
218+
return;
219+
rtnl_lock();
220+
__ipv6_sock_ac_close(sk);
214221
rtnl_unlock();
215222
}
216223

net/ipv6/ipv6_sockglue.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
240240

241241
fl6_free_socklist(sk);
242242
__ipv6_sock_mc_close(sk);
243+
__ipv6_sock_ac_close(sk);
243244

244245
/*
245246
* Sock is moving from IPv6 to IPv4 (sk_prot), so

0 commit comments

Comments
 (0)