All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Eric Dumazet <eric.dumazet@gmail.com>
Cc: netdev@vger.kernel.org, "David S . Miller" <davem@davemloft.net>,
	linux-kernel@vger.kernel.org, Mao Wenan <maowenan@huawei.com>,
	Cong Wang <xiyou.wangcong@gmail.com>,
	Lorenzo Colitti <lorenzo@google.com>,
	Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>,
	Al Viro <viro@zeniv.linux.org.uk>
Subject: Re: [PATCH net] net: socket: set sock->sk to NULL after calling proto_ops::release()
Date: Fri, 22 Feb 2019 09:57:44 -0800	[thread overview]
Message-ID: <20190222175743.GA163909@gmail.com> (raw)
In-Reply-To: <e4d0f1e8-68ff-512e-ad69-480523ff300f@gmail.com>

Hi Eric,

On Fri, Feb 22, 2019 at 09:45:35AM -0800, Eric Dumazet wrote:
> 
> 
> On 02/21/2019 02:13 PM, Eric Biggers wrote:
> > From: Eric Biggers <ebiggers@google.com>
> > 
> > Commit 9060cb719e61 ("net: crypto set sk to NULL when af_alg_release.")
> > fixed a use-after-free in sockfs_setattr() when an AF_ALG socket is
> > closed concurrently with fchownat().  However, it ignored that many
> > other proto_ops::release() methods don't set sock->sk to NULL and
> > therefore allow the same use-after-free:
> > 
> 
> I fail to see how setting a pointer to NULL can avoid races.
> 
> 
> We lack some kind of protection, rcu or something, if another thread can change sock->sk at anytime
> while sockfs_setattr() is used.
> 
> sockfs_setattr()
> ...
>      if (sock->sk)
> 
> // even if sock->sk was not NULL for the if (...).
> 
> // it can be NULL right now, compiler could read sock->sk a second time and catch a NULL.
> 
>         sock->sk->sk_uid = iattr->ia_uid;
> 
> 

->setattr() is called under inode_lock(), which __sock_release() also takes.  So
the uses of sock->sk are serialized.  See commit 6d8c50dcb029 ("socket: close
race condition between sock_close() and sockfs_setattr()").

The issue now is that if ->setattr() happens *after* __sock_release() (which is
possible if fchownat() gets the reference to the file's 'struct path', then the
file is close()d by another thread, then fchownat() continues), it will see
stale sock->sk because for many socket types it wasn't set to NULL earlier.

- Eric

  reply	other threads:[~2019-02-22 17:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-21 22:13 [PATCH net] net: socket: set sock->sk to NULL after calling proto_ops::release() Eric Biggers
2019-02-22  2:51 ` Al Viro
2019-02-22 17:45 ` Eric Dumazet
2019-02-22 17:57   ` Eric Biggers [this message]
2019-02-22 18:25     ` Eric Dumazet
2019-02-22 19:08       ` Al Viro
2019-02-22 18:05 ` Cong Wang
2019-02-25 18:41 ` David Miller

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=20190222175743.GA163909@gmail.com \
    --to=ebiggers@kernel.org \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorenzo@google.com \
    --cc=maowenan@huawei.com \
    --cc=netdev@vger.kernel.org \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=viro@zeniv.linux.org.uk \
    --cc=xiyou.wangcong@gmail.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 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.