summaryrefslogtreecommitdiff
path: root/net/unix
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2021-06-19 06:50:32 +0300
committerDavid S. Miller <davem@davemloft.net>2021-06-21 22:28:49 +0300
commitc0c3b8d380a8f54c75786d41f6f9efbe761dac6c (patch)
tree4c1fd9679c28f8f8305af59caad691d6fe42bf86 /net/unix
parent56c1731b280dc71febf5df80fcac1923ea973ab8 (diff)
downloadlinux-c0c3b8d380a8f54c75786d41f6f9efbe761dac6c.tar.xz
unix_bind_bsd(): unlink if we fail after successful mknod
We can do that more or less safely, since the parent is held locked all along. Yes, somebody might observe the object via dcache, only to have it disappear afterwards, but there's really no good way to prevent that. It won't race with other bind(2) or attempts to move the sucker elsewhere, or put something else in its place - locked parent prevents that. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/unix')
-rw-r--r--net/unix/af_unix.c29
1 files changed, 16 insertions, 13 deletions
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 25dda9ca9d15..42a9e0730344 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1010,20 +1010,13 @@ static int unix_bind_bsd(struct sock *sk, struct unix_address *addr)
err = security_path_mknod(&parent, dentry, mode, 0);
if (!err)
err = vfs_mknod(ns, d_inode(parent.dentry), dentry, mode, 0);
- if (err) {
- done_path_create(&parent, dentry);
- return err;
- }
+ if (err)
+ goto out;
err = mutex_lock_interruptible(&u->bindlock);
- if (err) {
- done_path_create(&parent, dentry);
- return err;
- }
- if (u->addr) {
- mutex_unlock(&u->bindlock);
- done_path_create(&parent, dentry);
- return -EINVAL;
- }
+ if (err)
+ goto out_unlink;
+ if (u->addr)
+ goto out_unlock;
addr->hash = UNIX_HASH_SIZE;
hash = d_backing_inode(dentry)->i_ino & (UNIX_HASH_SIZE - 1);
@@ -1035,6 +1028,16 @@ static int unix_bind_bsd(struct sock *sk, struct unix_address *addr)
mutex_unlock(&u->bindlock);
done_path_create(&parent, dentry);
return 0;
+
+out_unlock:
+ mutex_unlock(&u->bindlock);
+ err = -EINVAL;
+out_unlink:
+ /* failed after successful mknod? unlink what we'd created... */
+ vfs_unlink(ns, d_inode(parent.dentry), dentry, NULL);
+out:
+ done_path_create(&parent, dentry);
+ return err;
}
static int unix_bind_abstract(struct sock *sk, unsigned hash,