All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options().
@ 2022-07-28  1:22 Kuniyuki Iwashima
  2022-07-28  3:11 ` Jakub Kicinski
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2022-07-28  1:22 UTC (permalink / raw)
  To: David S. Miller, David Ahern, Eric Dumazet, Jakub Kicinski, Paolo Abeni
  Cc: Lorenzo Colitti, Benjamin Herrenschmidt, Ayushman Dutta,
	Kuniyuki Iwashima, netdev, syzbot+a8430774139ec3ab7176

When we close ping6 sockets, some resources are left unfreed because
pingv6_prot is missing sk->sk_prot->destroy().  As reported by
syzbot [0], just three syscalls leak 96 bytes and easily cause OOM.

    struct ipv6_sr_hdr *hdr;
    char data[24] = {0};
    int fd;

    hdr = (struct ipv6_sr_hdr *)data;
    hdr->hdrlen = 2;
    hdr->type = IPV6_SRCRT_TYPE_4;

    fd = socket(AF_INET6, SOCK_DGRAM, NEXTHDR_ICMP);
    setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR, data, 24);
    close(fd);

To fix memory leaks, let's add a destroy function.

Note the socket() syscall checks if the GID is within the range of
net.ipv4.ping_group_range.  The default value is [1, 0] so that no
GID meets the condition (1 <= GID <= 0).  Thus, the local DoS does
not succeed until we change the default value.  However, at least
Ubuntu/Fedora/RHEL loosen it.

    $ cat /usr/lib/sysctl.d/50-default.conf
    ...
    -net.ipv4.ping_group_range = 0 2147483647

Also, there could be another path reported with these options, and
some of them require CAP_NET_RAW.

  setsockopt
      IPV6_ADDRFORM (inet6_sk(sk)->pktoptions)
      IPV6_RECVPATHMTU (inet6_sk(sk)->rxpmtu)
      IPV6_HOPOPTS (inet6_sk(sk)->opt)
      IPV6_RTHDRDSTOPTS (inet6_sk(sk)->opt)
      IPV6_RTHDR (inet6_sk(sk)->opt)
      IPV6_DSTOPTS (inet6_sk(sk)->opt)
      IPV6_2292PKTOPTIONS (inet6_sk(sk)->opt)

  getsockopt
      IPV6_FLOWLABEL_MGR (inet6_sk(sk)->ipv6_fl_list)

For the record, I left a different splat with syzbot's one.

  unreferenced object 0xffff888006270c60 (size 96):
    comm "repro2", pid 231, jiffies 4294696626 (age 13.118s)
    hex dump (first 32 bytes):
      01 00 00 00 44 00 00 00 00 00 00 00 00 00 00 00  ....D...........
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    backtrace:
      [<00000000f6bc7ea9>] sock_kmalloc (net/core/sock.c:2564 net/core/sock.c:2554)
      [<000000006d699550>] do_ipv6_setsockopt.constprop.0 (net/ipv6/ipv6_sockglue.c:715)
      [<00000000c3c3b1f5>] ipv6_setsockopt (net/ipv6/ipv6_sockglue.c:1024)
      [<000000007096a025>] __sys_setsockopt (net/socket.c:2254)
      [<000000003a8ff47b>] __x64_sys_setsockopt (net/socket.c:2265 net/socket.c:2262 net/socket.c:2262)
      [<000000007c409dcb>] do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
      [<00000000e939c4a9>] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120)

[0]: https://syzkaller.appspot.com/bug?extid=a8430774139ec3ab7176

Fixes: 6d0bfe226116 ("net: ipv6: Add IPv6 support to the ping socket.")
Reported-by: syzbot+a8430774139ec3ab7176@syzkaller.appspotmail.com
Reported-by: Ayushman Dutta <ayudutta@amazon.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
v2:
  - Remove ip6_flush_pending_frames() (Jakub Kicinski)

v1: offlist
---
 net/ipv6/ping.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index ecf3a553a0dc..8c6c2d82c1cd 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -22,6 +22,11 @@
 #include <linux/proc_fs.h>
 #include <net/ping.h>
 
+static void ping_v6_destroy(struct sock *sk)
+{
+	inet6_destroy_sock(sk);
+}
+
 /* Compatibility glue so we can support IPv6 when it's compiled as a module */
 static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
 				 int *addr_len)
@@ -181,6 +186,7 @@ struct proto pingv6_prot = {
 	.owner =	THIS_MODULE,
 	.init =		ping_init_sock,
 	.close =	ping_close,
+	.destroy =	ping_v6_destroy,
 	.connect =	ip6_datagram_connect_v6_only,
 	.disconnect =	__udp_disconnect,
 	.setsockopt =	ipv6_setsockopt,
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options().
  2022-07-28  1:22 [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options() Kuniyuki Iwashima
@ 2022-07-28  3:11 ` Jakub Kicinski
  2022-07-28 16:13 ` David Ahern
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2022-07-28  3:11 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, David Ahern, Eric Dumazet, Paolo Abeni,
	Lorenzo Colitti, Benjamin Herrenschmidt, Ayushman Dutta, netdev,
	syzbot+a8430774139ec3ab7176

On Wed, 27 Jul 2022 18:22:20 -0700 Kuniyuki Iwashima wrote:
>   - Remove ip6_flush_pending_frames() (Jakub Kicinski)

To be clear I was just asking if it's needed, I haven't checked the code
:)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options().
  2022-07-28  1:22 [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options() Kuniyuki Iwashima
  2022-07-28  3:11 ` Jakub Kicinski
@ 2022-07-28 16:13 ` David Ahern
  2022-07-28 16:50 ` Eric Dumazet
  2022-07-28 18:10 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: David Ahern @ 2022-07-28 16:13 UTC (permalink / raw)
  To: Kuniyuki Iwashima, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni
  Cc: Lorenzo Colitti, Benjamin Herrenschmidt, Ayushman Dutta, netdev,
	syzbot+a8430774139ec3ab7176

On 7/27/22 7:22 PM, Kuniyuki Iwashima wrote:
> When we close ping6 sockets, some resources are left unfreed because
> pingv6_prot is missing sk->sk_prot->destroy().  As reported by
> syzbot [0], just three syscalls leak 96 bytes and easily cause OOM.
> 
>     struct ipv6_sr_hdr *hdr;
>     char data[24] = {0};
>     int fd;
> 
>     hdr = (struct ipv6_sr_hdr *)data;
>     hdr->hdrlen = 2;
>     hdr->type = IPV6_SRCRT_TYPE_4;
> 
>     fd = socket(AF_INET6, SOCK_DGRAM, NEXTHDR_ICMP);
>     setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR, data, 24);
>     close(fd);
> 
> To fix memory leaks, let's add a destroy function.
> 
> Note the socket() syscall checks if the GID is within the range of
> net.ipv4.ping_group_range.  The default value is [1, 0] so that no
> GID meets the condition (1 <= GID <= 0).  Thus, the local DoS does
> not succeed until we change the default value.  However, at least
> Ubuntu/Fedora/RHEL loosen it.
> 
>     $ cat /usr/lib/sysctl.d/50-default.conf
>     ...
>     -net.ipv4.ping_group_range = 0 2147483647
> 
> Also, there could be another path reported with these options, and
> some of them require CAP_NET_RAW.
> 
>   setsockopt
>       IPV6_ADDRFORM (inet6_sk(sk)->pktoptions)
>       IPV6_RECVPATHMTU (inet6_sk(sk)->rxpmtu)
>       IPV6_HOPOPTS (inet6_sk(sk)->opt)
>       IPV6_RTHDRDSTOPTS (inet6_sk(sk)->opt)
>       IPV6_RTHDR (inet6_sk(sk)->opt)
>       IPV6_DSTOPTS (inet6_sk(sk)->opt)
>       IPV6_2292PKTOPTIONS (inet6_sk(sk)->opt)
> 
>   getsockopt
>       IPV6_FLOWLABEL_MGR (inet6_sk(sk)->ipv6_fl_list)
> 
> For the record, I left a different splat with syzbot's one.
> 
>   unreferenced object 0xffff888006270c60 (size 96):
>     comm "repro2", pid 231, jiffies 4294696626 (age 13.118s)
>     hex dump (first 32 bytes):
>       01 00 00 00 44 00 00 00 00 00 00 00 00 00 00 00  ....D...........
>       00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>     backtrace:
>       [<00000000f6bc7ea9>] sock_kmalloc (net/core/sock.c:2564 net/core/sock.c:2554)
>       [<000000006d699550>] do_ipv6_setsockopt.constprop.0 (net/ipv6/ipv6_sockglue.c:715)
>       [<00000000c3c3b1f5>] ipv6_setsockopt (net/ipv6/ipv6_sockglue.c:1024)
>       [<000000007096a025>] __sys_setsockopt (net/socket.c:2254)
>       [<000000003a8ff47b>] __x64_sys_setsockopt (net/socket.c:2265 net/socket.c:2262 net/socket.c:2262)
>       [<000000007c409dcb>] do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80)
>       [<00000000e939c4a9>] entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120)
> 
> [0]: https://syzkaller.appspot.com/bug?extid=a8430774139ec3ab7176
> 
> Fixes: 6d0bfe226116 ("net: ipv6: Add IPv6 support to the ping socket.")
> Reported-by: syzbot+a8430774139ec3ab7176@syzkaller.appspotmail.com
> Reported-by: Ayushman Dutta <ayudutta@amazon.com>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
> v2:
>   - Remove ip6_flush_pending_frames() (Jakub Kicinski)
> 
> v1: offlist
> ---
>  net/ipv6/ping.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 

Reviewed-by: David Ahern <dsahern@kernel.org>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options().
  2022-07-28  1:22 [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options() Kuniyuki Iwashima
  2022-07-28  3:11 ` Jakub Kicinski
  2022-07-28 16:13 ` David Ahern
@ 2022-07-28 16:50 ` Eric Dumazet
  2022-07-28 18:10 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2022-07-28 16:50 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: David S. Miller, David Ahern, Jakub Kicinski, Paolo Abeni,
	Lorenzo Colitti, Benjamin Herrenschmidt, Ayushman Dutta, netdev,
	syzbot+a8430774139ec3ab7176

On Thu, Jul 28, 2022 at 3:22 AM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> When we close ping6 sockets, some resources are left unfreed because
> pingv6_prot is missing sk->sk_prot->destroy().  As reported by
> syzbot [0], just three syscalls leak 96 bytes and easily cause OOM.
>

>
> Fixes: 6d0bfe226116 ("net: ipv6: Add IPv6 support to the ping socket.")
> Reported-by: syzbot+a8430774139ec3ab7176@syzkaller.appspotmail.com
> Reported-by: Ayushman Dutta <ayudutta@amazon.com>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
> v2:
>   - Remove ip6_flush_pending_frames() (Jakub Kicinski)

Nice catch, thanks.

Reviewed-by: Eric Dumazet <edumazet@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options().
  2022-07-28  1:22 [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options() Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  2022-07-28 16:50 ` Eric Dumazet
@ 2022-07-28 18:10 ` patchwork-bot+netdevbpf
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-28 18:10 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: davem, dsahern, edumazet, kuba, pabeni, lorenzo, benh, ayudutta,
	netdev, syzbot+a8430774139ec3ab7176

Hello:

This patch was applied to netdev/net.git (master)
by Jakub Kicinski <kuba@kernel.org>:

On Wed, 27 Jul 2022 18:22:20 -0700 you wrote:
> When we close ping6 sockets, some resources are left unfreed because
> pingv6_prot is missing sk->sk_prot->destroy().  As reported by
> syzbot [0], just three syscalls leak 96 bytes and easily cause OOM.
> 
>     struct ipv6_sr_hdr *hdr;
>     char data[24] = {0};
>     int fd;
> 
> [...]

Here is the summary with links:
  - [v2,net] net: ping6: Fix memleak in ipv6_renew_options().
    https://git.kernel.org/netdev/net/c/e27326009a3d

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-28 18:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-28  1:22 [PATCH v2 net] net: ping6: Fix memleak in ipv6_renew_options() Kuniyuki Iwashima
2022-07-28  3:11 ` Jakub Kicinski
2022-07-28 16:13 ` David Ahern
2022-07-28 16:50 ` Eric Dumazet
2022-07-28 18:10 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.