Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757431AbbEVOv0 (ORCPT ); Fri, 22 May 2015 10:51:26 -0400 Received: from mail-ig0-f173.google.com ([209.85.213.173]:35743 "EHLO mail-ig0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757161AbbEVOvV (ORCPT ); Fri, 22 May 2015 10:51:21 -0400 Message-ID: <555F4267.30704@android.com> Date: Fri, 22 May 2015 07:51:19 -0700 From: Mark Salyzyn User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: Hannes Frederic Sowa CC: linux-kernel@vger.kernel.org, "David S. Miller" , Al Viro , David Howells , Ying Xue , Christoph Hellwig , netdev@vger.kernel.org Subject: Re: net/unix: sk_socket can disappear when state is unlocked References: <1432225541-28498-1-git-send-email-salyzyn@android.com> <1432288230.3364.23.camel@redhat.com> In-Reply-To: <1432288230.3364.23.camel@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2141 Lines: 53 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 >> --- >> 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! Sincerely -- Mark Salyzyn -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/