Skip to content

Commit

Permalink
unix: fix a race condition in unix_release()
Browse files Browse the repository at this point in the history
As reported by Jan, and others over the past few years, there is a
race condition caused by unix_release setting the sock->sk pointer
to NULL before properly marking the socket as dead/orphaned.  This
can cause a problem with the LSM hook security_unix_may_send() if
there is another socket attempting to write to this partially
released socket in between when sock->sk is set to NULL and it is
marked as dead/orphaned.  This patch fixes this by only setting
sock->sk to NULL after the socket has been marked as dead; I also
take the opportunity to make unix_release_sock() a void function
as it only ever returned 0/success.

Dave, I think this one should go on the -stable pile.

Special thanks to Jan for coming up with a reproducer for this
problem.

Reported-by: Jan Stancek <jan.stancek@gmail.com>
Signed-off-by: Paul Moore <pmoore@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Paul Moore authored and David S. Miller committed Mar 25, 2013
1 parent 7ebe183 commit ded34e0
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions net/unix/af_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static void unix_sock_destructor(struct sock *sk)
#endif
}

static int unix_release_sock(struct sock *sk, int embrion)
static void unix_release_sock(struct sock *sk, int embrion)
{
struct unix_sock *u = unix_sk(sk);
struct path path;
Expand Down Expand Up @@ -451,8 +451,6 @@ static int unix_release_sock(struct sock *sk, int embrion)

if (unix_tot_inflight)
unix_gc(); /* Garbage collect fds */

return 0;
}

static void init_peercred(struct sock *sk)
Expand Down Expand Up @@ -699,9 +697,10 @@ static int unix_release(struct socket *sock)
if (!sk)
return 0;

unix_release_sock(sk, 0);
sock->sk = NULL;

return unix_release_sock(sk, 0);
return 0;
}

static int unix_autobind(struct socket *sock)
Expand Down

0 comments on commit ded34e0

Please sign in to comment.