netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hannes Frederic Sowa <hannes@redhat.com>
To: Mark Salyzyn <salyzyn@android.com>
Cc: linux-kernel@vger.kernel.org,
	"David S. Miller" <davem@davemloft.net>,
	Al Viro <viro@zeniv.linux.org.uk>,
	David Howells <dhowells@redhat.com>,
	Ying Xue <ying.xue@windriver.com>, Christoph Hellwig <hch@lst.de>,
	netdev@vger.kernel.org
Subject: Re: net/unix: sk_socket can disappear when state is unlocked
Date: Fri, 22 May 2015 17:35:15 +0200	[thread overview]
Message-ID: <1432308915.28081.10.camel@redhat.com> (raw)
In-Reply-To: <555F4267.30704@android.com>

On Fr, 2015-05-22 at 07:51 -0700, Mark Salyzyn wrote:
> On 05/22/2015 02:50 AM, Hannes Frederic Sowa wrote:
> > On Do, 2015-05-21 at 09:25 -0700, Mark Salyzyn wrote:
> >> got a rare NULL pointer dereference in clear_bit
> >>
> >> Signed-off-by: Mark Salyzyn <salyzyn@android.com>
> >> ---
> >>   net/unix/af_unix.c | 5 +++++
> >>   1 file changed, 5 insertions(+)
> >>
> >> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> >> index 5266ea7..37a8925 100644
> >> --- a/net/unix/af_unix.c
> >> +++ b/net/unix/af_unix.c
> >> @@ -1880,6 +1880,11 @@ static long unix_stream_data_wait(struct sock *sk, long timeo,
> >>   		unix_state_unlock(sk);
> >>   		timeo = freezable_schedule_timeout(timeo);
> >>   		unix_state_lock(sk);
> >> +
> >> +		/* sk_socket may have been killed while unlocked */
> >> +		if (!sk->sk_socket)
> >> +			break;
> >> +
> >>   		clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
> >>   	}
> >>   
> > Canonical way is to test for sock_flag(sk, SOCK_DEAD). Also it does not
> > seem like we are returning an error to user space but are still looping
> > to try to dequeue skbs from sk_receive_queue, which is concurrently
> > emptied by unix_release (maybe, without holding unix_state_lock).
> >
> > Bye,
> > Hannes
> >
> I will send an updated patch shortly.
> 
> It may be acceptable given the expectation that sk_set_socket(sk, NULL) 
> occurs after SOCK_DEAD flag is set since we would not be here during the 
> socket initialization/connection phases. As such, for all phases (and I 
> re-iterate, we can only be here if in connected state), it is not a 
> generic guarantee of sk_socket != NULL. But I only saw one apparent 
> example (in net/decnet/dn_nsp_in.c) of using sock_flag(sk, SOCK_DEAD) as 
> protection against a possible deference NULL access with sk_socket, and 
> many KISS examples of checking sk_socket for NULL to protect against thus.
> 
> Thanks for making me look though, it appears that I missed the same 
> problem in net/caif/caif_socket.c and will add it!

Thank you for v2 of the patch.

I still wonder if we need to actually recheck the condition and not
simply break out of unix_stream_data_wait:

We return to the unix_stream_recvmsg loop and recheck the
sk_receive_queue. At this point sk_receive_queue is not really protected
with unix_state_lock against concurrent modification with unix_release,
as such we could end up concurrently dequeueing packets if socket is
DEAD.

Does that make sense?

Thanks,
Hannes

  reply	other threads:[~2015-05-22 15:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-21 16:25 net/unix: sk_socket can disappear when state is unlocked Mark Salyzyn
2015-05-22  9:50 ` Hannes Frederic Sowa
2015-05-22 14:51   ` Mark Salyzyn
2015-05-22 15:35     ` Hannes Frederic Sowa [this message]
2015-05-22 16:24       ` Mark Salyzyn
2015-05-22 18:16         ` Hannes Frederic Sowa
2015-05-22 19:59           ` Mark Salyzyn

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=1432308915.28081.10.camel@redhat.com \
    --to=hannes@redhat.com \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=hch@lst.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=salyzyn@android.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).