diff mbox series

net: explicitly clear the sk pointer, when pf->create fails

Message ID 20241003170151.69445-1-ignat@cloudflare.com (mailing list archive)
State Accepted
Commit 631083143315d1b192bd7d915b967b37819e88ea
Delegated to: Netdev Maintainers
Headers show
Series net: explicitly clear the sk pointer, when pf->create fails | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 24 this patch: 24
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-06--15-00 (tests: 775)

Commit Message

Ignat Korchagin Oct. 3, 2024, 5:01 p.m. UTC
We have recently noticed the exact same KASAN splat as in commit
6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
creation fails"). The problem is that commit did not fully address the
problem, as some pf->create implementations do not use sk_common_release
in their error paths.

For example, we can use the same reproducer as in the above commit, but
changing ping to arping. arping uses AF_PACKET socket and if packet_create
fails, it will just sk_free the allocated sk object.

While we could chase all the pf->create implementations and make sure they
NULL the freed sk object on error from the socket, we can't guarantee
future protocols will not make the same mistake.

So it is easier to just explicitly NULL the sk pointer upon return from
pf->create in __sock_create. We do know that pf->create always releases the
allocated sk object on error, so if the pointer is not NULL, it is
definitely dangling.

Fixes: 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket creation fails")
Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
Cc: stable@vger.kernel.org
---
 net/socket.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Kuniyuki Iwashima Oct. 3, 2024, 9:50 p.m. UTC | #1
From: Ignat Korchagin <ignat@cloudflare.com>
Date: Thu,  3 Oct 2024 18:01:51 +0100
> We have recently noticed the exact same KASAN splat as in commit
> 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> creation fails"). The problem is that commit did not fully address the
> problem, as some pf->create implementations do not use sk_common_release
> in their error paths.
> 
> For example, we can use the same reproducer as in the above commit, but
> changing ping to arping. arping uses AF_PACKET socket and if packet_create
> fails, it will just sk_free the allocated sk object.
> 
> While we could chase all the pf->create implementations and make sure they
> NULL the freed sk object on error from the socket, we can't guarantee
> future protocols will not make the same mistake.
> 
> So it is easier to just explicitly NULL the sk pointer upon return from
> pf->create in __sock_create. We do know that pf->create always releases the
> allocated sk object on error, so if the pointer is not NULL, it is
> definitely dangling.

Sounds good to me.

Let's remove the change by 6cd4a78d962b that should be unnecessary
with this patch.


> 
> Fixes: 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket creation fails")
> Signed-off-by: Ignat Korchagin <ignat@cloudflare.com>
> Cc: stable@vger.kernel.org
> ---
>  net/socket.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/net/socket.c b/net/socket.c
> index 7b046dd3e9a7..19afac3c2de9 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -1575,8 +1575,13 @@ int __sock_create(struct net *net, int family, int type, int protocol,
>  	rcu_read_unlock();
>  
>  	err = pf->create(net, sock, protocol, kern);
> -	if (err < 0)
> +	if (err < 0) {
> +		/* ->create should release the allocated sock->sk object on error
> +		 * but it may leave the dangling pointer
> +		 */
> +		sock->sk = NULL;
>  		goto out_module_put;
> +	}
>  
>  	/*
>  	 * Now to bump the refcnt of the [loadable] module that owns this
> -- 
> 2.39.5
>
Eric Dumazet Oct. 4, 2024, 8:55 a.m. UTC | #2
On Thu, Oct 3, 2024 at 11:50 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
>
> From: Ignat Korchagin <ignat@cloudflare.com>
> Date: Thu,  3 Oct 2024 18:01:51 +0100
> > We have recently noticed the exact same KASAN splat as in commit
> > 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> > creation fails"). The problem is that commit did not fully address the
> > problem, as some pf->create implementations do not use sk_common_release
> > in their error paths.
> >
> > For example, we can use the same reproducer as in the above commit, but
> > changing ping to arping. arping uses AF_PACKET socket and if packet_create
> > fails, it will just sk_free the allocated sk object.
> >
> > While we could chase all the pf->create implementations and make sure they
> > NULL the freed sk object on error from the socket, we can't guarantee
> > future protocols will not make the same mistake.
> >
> > So it is easier to just explicitly NULL the sk pointer upon return from
> > pf->create in __sock_create. We do know that pf->create always releases the
> > allocated sk object on error, so if the pointer is not NULL, it is
> > definitely dangling.
>
> Sounds good to me.
>
> Let's remove the change by 6cd4a78d962b that should be unnecessary
> with this patch.
>

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

Even if not strictly needed we also could fix af_packet to avoid a
dangling pointer.

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index a705ec21425409dc708cf61d619545ed342b1f01..db7d3482e732f236ec461b1ff52a264d1b93bf90
100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3421,16 +3421,18 @@ static int packet_create(struct net *net,
struct socket *sock, int protocol,
        if (sock->type == SOCK_PACKET)
                sock->ops = &packet_ops_spkt;

+       po = pkt_sk(sk);
+       err = packet_alloc_pending(po);
+       if (err)
+               goto out2;
+
+       /* No more error after this point. */
        sock_init_data(sock, sk);

-       po = pkt_sk(sk);
        init_completion(&po->skb_completion);
        sk->sk_family = PF_PACKET;
        po->num = proto;

-       err = packet_alloc_pending(po);
-       if (err)
-               goto out2;

        packet_cached_dev_reset(po);
Ignat Korchagin Oct. 4, 2024, 10:05 a.m. UTC | #3
On Fri, Oct 4, 2024 at 9:55 AM Eric Dumazet <edumazet@google.com> wrote:
>
> On Thu, Oct 3, 2024 at 11:50 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> >
> > From: Ignat Korchagin <ignat@cloudflare.com>
> > Date: Thu,  3 Oct 2024 18:01:51 +0100
> > > We have recently noticed the exact same KASAN splat as in commit
> > > 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> > > creation fails"). The problem is that commit did not fully address the
> > > problem, as some pf->create implementations do not use sk_common_release
> > > in their error paths.
> > >
> > > For example, we can use the same reproducer as in the above commit, but
> > > changing ping to arping. arping uses AF_PACKET socket and if packet_create
> > > fails, it will just sk_free the allocated sk object.
> > >
> > > While we could chase all the pf->create implementations and make sure they
> > > NULL the freed sk object on error from the socket, we can't guarantee
> > > future protocols will not make the same mistake.
> > >
> > > So it is easier to just explicitly NULL the sk pointer upon return from
> > > pf->create in __sock_create. We do know that pf->create always releases the
> > > allocated sk object on error, so if the pointer is not NULL, it is
> > > definitely dangling.
> >
> > Sounds good to me.
> >
> > Let's remove the change by 6cd4a78d962b that should be unnecessary
> > with this patch.
> >
>
> Reviewed-by: Eric Dumazet <edumazet@google.com>
>
> Even if not strictly needed we also could fix af_packet to avoid a
> dangling pointer.

af_packet was just one example - I reviewed every pf->create function
and there are others. It would not be fair to fix this, but not the
others, right?

> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index a705ec21425409dc708cf61d619545ed342b1f01..db7d3482e732f236ec461b1ff52a264d1b93bf90
> 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -3421,16 +3421,18 @@ static int packet_create(struct net *net,
> struct socket *sock, int protocol,
>         if (sock->type == SOCK_PACKET)
>                 sock->ops = &packet_ops_spkt;
>
> +       po = pkt_sk(sk);
> +       err = packet_alloc_pending(po);
> +       if (err)
> +               goto out2;
> +
> +       /* No more error after this point. */
>         sock_init_data(sock, sk);
>
> -       po = pkt_sk(sk);
>         init_completion(&po->skb_completion);
>         sk->sk_family = PF_PACKET;
>         po->num = proto;
>
> -       err = packet_alloc_pending(po);
> -       if (err)
> -               goto out2;
>
>         packet_cached_dev_reset(po);
Eric Dumazet Oct. 4, 2024, 10:19 a.m. UTC | #4
On Fri, Oct 4, 2024 at 12:05 PM Ignat Korchagin <ignat@cloudflare.com> wrote:
>
> On Fri, Oct 4, 2024 at 9:55 AM Eric Dumazet <edumazet@google.com> wrote:
> >
> > On Thu, Oct 3, 2024 at 11:50 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > >
> > > From: Ignat Korchagin <ignat@cloudflare.com>
> > > Date: Thu,  3 Oct 2024 18:01:51 +0100
> > > > We have recently noticed the exact same KASAN splat as in commit
> > > > 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> > > > creation fails"). The problem is that commit did not fully address the
> > > > problem, as some pf->create implementations do not use sk_common_release
> > > > in their error paths.
> > > >
> > > > For example, we can use the same reproducer as in the above commit, but
> > > > changing ping to arping. arping uses AF_PACKET socket and if packet_create
> > > > fails, it will just sk_free the allocated sk object.
> > > >
> > > > While we could chase all the pf->create implementations and make sure they
> > > > NULL the freed sk object on error from the socket, we can't guarantee
> > > > future protocols will not make the same mistake.
> > > >
> > > > So it is easier to just explicitly NULL the sk pointer upon return from
> > > > pf->create in __sock_create. We do know that pf->create always releases the
> > > > allocated sk object on error, so if the pointer is not NULL, it is
> > > > definitely dangling.
> > >
> > > Sounds good to me.
> > >
> > > Let's remove the change by 6cd4a78d962b that should be unnecessary
> > > with this patch.
> > >
> >
> > Reviewed-by: Eric Dumazet <edumazet@google.com>
> >
> > Even if not strictly needed we also could fix af_packet to avoid a
> > dangling pointer.
>
> af_packet was just one example - I reviewed every pf->create function
> and there are others. It would not be fair to fix this, but not the
> others, right?

I have not said your patch was not correct, I gave a +2 on it.

In general, leaving pointers to a freed piece of memory (and possibly reused)
can confuse things like kmemleak.

I have not said _you_ had to review all pf->create() functions.
Ignat Korchagin Oct. 4, 2024, 10:25 a.m. UTC | #5
On Fri, Oct 4, 2024 at 11:19 AM Eric Dumazet <edumazet@google.com> wrote:
>
> On Fri, Oct 4, 2024 at 12:05 PM Ignat Korchagin <ignat@cloudflare.com> wrote:
> >
> > On Fri, Oct 4, 2024 at 9:55 AM Eric Dumazet <edumazet@google.com> wrote:
> > >
> > > On Thu, Oct 3, 2024 at 11:50 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
> > > >
> > > > From: Ignat Korchagin <ignat@cloudflare.com>
> > > > Date: Thu,  3 Oct 2024 18:01:51 +0100
> > > > > We have recently noticed the exact same KASAN splat as in commit
> > > > > 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> > > > > creation fails"). The problem is that commit did not fully address the
> > > > > problem, as some pf->create implementations do not use sk_common_release
> > > > > in their error paths.
> > > > >
> > > > > For example, we can use the same reproducer as in the above commit, but
> > > > > changing ping to arping. arping uses AF_PACKET socket and if packet_create
> > > > > fails, it will just sk_free the allocated sk object.
> > > > >
> > > > > While we could chase all the pf->create implementations and make sure they
> > > > > NULL the freed sk object on error from the socket, we can't guarantee
> > > > > future protocols will not make the same mistake.
> > > > >
> > > > > So it is easier to just explicitly NULL the sk pointer upon return from
> > > > > pf->create in __sock_create. We do know that pf->create always releases the
> > > > > allocated sk object on error, so if the pointer is not NULL, it is
> > > > > definitely dangling.
> > > >
> > > > Sounds good to me.
> > > >
> > > > Let's remove the change by 6cd4a78d962b that should be unnecessary
> > > > with this patch.
> > > >
> > >
> > > Reviewed-by: Eric Dumazet <edumazet@google.com>
> > >
> > > Even if not strictly needed we also could fix af_packet to avoid a
> > > dangling pointer.
> >
> > af_packet was just one example - I reviewed every pf->create function
> > and there are others. It would not be fair to fix this, but not the
> > others, right?
>
> I have not said your patch was not correct, I gave a +2 on it.
>
> In general, leaving pointers to a freed piece of memory (and possibly reused)
> can confuse things like kmemleak.

That's a good point actually.

> I have not said _you_ had to review all pf->create() functions.

Ah, NP. I reviewed them before your comment, before submitting the
patch - basically to decide whether I should go with the current
approach or just go and fix them.
patchwork-bot+netdevbpf@kernel.org Oct. 7, 2024, 11:40 p.m. UTC | #6
Hello:

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

On Thu,  3 Oct 2024 18:01:51 +0100 you wrote:
> We have recently noticed the exact same KASAN splat as in commit
> 6cd4a78d962b ("net: do not leave a dangling sk pointer, when socket
> creation fails"). The problem is that commit did not fully address the
> problem, as some pf->create implementations do not use sk_common_release
> in their error paths.
> 
> For example, we can use the same reproducer as in the above commit, but
> changing ping to arping. arping uses AF_PACKET socket and if packet_create
> fails, it will just sk_free the allocated sk object.
> 
> [...]

Here is the summary with links:
  - net: explicitly clear the sk pointer, when pf->create fails
    https://git.kernel.org/netdev/net/c/631083143315

You are awesome, thank you!
diff mbox series

Patch

diff --git a/net/socket.c b/net/socket.c
index 7b046dd3e9a7..19afac3c2de9 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1575,8 +1575,13 @@  int __sock_create(struct net *net, int family, int type, int protocol,
 	rcu_read_unlock();
 
 	err = pf->create(net, sock, protocol, kern);
-	if (err < 0)
+	if (err < 0) {
+		/* ->create should release the allocated sock->sk object on error
+		 * but it may leave the dangling pointer
+		 */
+		sock->sk = NULL;
 		goto out_module_put;
+	}
 
 	/*
 	 * Now to bump the refcnt of the [loadable] module that owns this