netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin LaHaise <bcrl@kvack.org>
To: Jason Baron <jbaron@akamai.com>
Cc: Eric Dumazet <edumazet@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Hannes Frederic Sowa <hannes@stressinduktion.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	David Howells <dhowells@redhat.com>,
	Ying Xue <ying.xue@windriver.com>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Rainer Weikusat <rweikusat@mobileactivedefense.com>,
	netdev <netdev@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	syzkaller <syzkaller@googlegroups.com>,
	Kostya Serebryany <kcc@google.com>,
	Alexander Potapenko <glider@google.com>,
	Sasha Levin <sasha.levin@oracle.com>
Subject: Re: use-after-free in sock_wake_async
Date: Tue, 24 Nov 2015 16:45:12 -0500	[thread overview]
Message-ID: <20151124214512.GM27046@kvack.org> (raw)
In-Reply-To: <5654D6D9.1050108@akamai.com>

On Tue, Nov 24, 2015 at 04:30:01PM -0500, Jason Baron wrote:
> So looking at this trace I think its the other->sk_socket that gets
> freed and then we call sk_wake_async() on it.
> 
> We could I think grab the socket reference there with unix_state_lock(),
> since that is held by unix_release_sock() before the final iput() is called.
> 
> So something like below might work (compile tested only):

That just adds the performance regression back in.  It should be possible 
to protect the other socket dereference using RCU.  I haven't had time to 
look at this yet today, but will try to find some time this evening to come 
up with a suggested patch.

		-ben

> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index aaa0b58..2b014f1 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -196,6 +196,19 @@ static inline int unix_recvq_full(struct sock const
> *sk)
>  	return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog;
>  }
> 
> +struct socket *unix_peer_get_socket(struct sock *s)
> +{
> +	struct socket *peer;
> +
> +	unix_state_lock(s);
> +	peer = s->sk_socket;
> +	if (peer)
> +		__iget(SOCK_INODE(s->sk_socket));
> +	unix_state_unlock(s);
> +
> +	return peer;
> +}
> +
>  struct sock *unix_peer_get(struct sock *s)
>  {
>  	struct sock *peer;
> @@ -1639,6 +1652,7 @@ static int unix_stream_sendmsg(struct socket
> *sock, struct msghdr *msg,
>  {
>  	struct sock *sk = sock->sk;
>  	struct sock *other = NULL;
> +	struct socket *other_socket = NULL;
>  	int err, size;
>  	struct sk_buff *skb;
>  	int sent = 0;
> @@ -1662,7 +1676,10 @@ static int unix_stream_sendmsg(struct socket
> *sock, struct msghdr *msg,
>  	} else {
>  		err = -ENOTCONN;
>  		other = unix_peer(sk);
> -		if (!other)
> +		if (other)
> +			other_socket = unix_peer_get_socket(other);
> +
> +		if (!other_socket)
>  			goto out_err;
>  	}
> 
> @@ -1721,6 +1738,9 @@ static int unix_stream_sendmsg(struct socket
> *sock, struct msghdr *msg,
>  		sent += size;
>  	}
> 
> +	if (other_socket)
> +		iput(SOCK_INODE(other_socket));
> +
>  	scm_destroy(&scm);
> 
>  	return sent;
> @@ -1733,6 +1753,8 @@ pipe_err:
>  		send_sig(SIGPIPE, current, 0);
>  	err = -EPIPE;
>  out_err:
> +	if (other_socket)
> +		iput(SOCK_INODE(other_socket));
>  	scm_destroy(&scm);
>  	return sent ? : err;
>  }

-- 
"Thought is the essence of where you are now."

  parent reply	other threads:[~2015-11-24 21:45 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 14:18 use-after-free in sock_wake_async Dmitry Vyukov
2015-11-24 15:21 ` Eric Dumazet
2015-11-24 15:39   ` Eric Dumazet
2015-11-24 21:30   ` Jason Baron
2015-11-24 21:40     ` Al Viro
2015-11-24 21:45     ` Benjamin LaHaise [this message]
2015-11-24 22:03       ` Eric Dumazet
2015-11-24 22:12         ` Eric Dumazet
2015-11-24 23:34   ` Rainer Weikusat
2015-11-24 23:43     ` Eric Dumazet
2015-11-25  1:10       ` Rainer Weikusat
2015-11-25  1:16         ` Rainer Weikusat
2015-11-25  1:18         ` Eric Dumazet
2015-11-25  2:28           ` Eric Dumazet
2015-11-25  5:43             ` Eric Dumazet
2015-11-25 14:18               ` Eric Dumazet
2015-11-25 16:43           ` Rainer Weikusat
2015-11-25 17:11             ` Eric Dumazet
2015-11-25 17:30               ` Rainer Weikusat
2015-11-25 17:51                 ` Eric Dumazet
2015-11-25 18:24                   ` Rainer Weikusat
2015-11-25 18:39                     ` Eric Dumazet
2015-11-25 19:38                       ` Rainer Weikusat
2015-11-25 19:50                         ` Eric Dumazet
2015-11-25 20:23                           ` Eric Dumazet
2015-11-25 20:57                             ` Rainer Weikusat
2015-11-25 22:09                               ` Eric Dumazet
2015-11-25 22:32                                 ` Hannes Frederic Sowa
2015-11-25 22:43                                   ` Eric Dumazet
2015-11-25 22:52                                     ` Hannes Frederic Sowa
2015-11-26 13:32                                       ` Hannes Frederic Sowa
2015-11-26 14:31                                         ` Hannes Frederic Sowa
2015-11-26 15:51                                         ` Eric Dumazet
2015-11-26 17:03                                           ` Hannes Frederic Sowa
2015-11-26 17:09                                             ` Eric Dumazet
2015-11-26 17:15                                               ` Hannes Frederic Sowa
2015-11-26 17:29                                             ` Eric Dumazet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151124214512.GM27046@kvack.org \
    --to=bcrl@kvack.org \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=dvyukov@google.com \
    --cc=ebiederm@xmission.com \
    --cc=edumazet@google.com \
    --cc=glider@google.com \
    --cc=hannes@stressinduktion.org \
    --cc=jbaron@akamai.com \
    --cc=kcc@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=rweikusat@mobileactivedefense.com \
    --cc=sasha.levin@oracle.com \
    --cc=syzkaller@googlegroups.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=ying.xue@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).