Skip to content

Commit 467e9e5

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull assorted fixes - mostly vfs - from Al Viro: "Assorted fixes, with an unexpected detour into vfio refcounting logics (fell out when digging in an analog of eventpoll race in there)." * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: task_work: add a scheduling point in task_work_run() fs: fix fs/namei.c kernel-doc warnings eventpoll: use-after-possible-free in epoll_create1() vfio: grab vfio_device reference *before* exposing the sucker via fd_install() vfio: get rid of vfio_device_put()/vfio_group_get_device* races vfio: get rid of open-coding kref_put_mutex introduce kref_put_mutex() vfio: don't dereference after kfree... mqueue: lift mnt_want_write() outside ->i_mutex, clean up a bit
2 parents 23dcfa6 + 88ec278 commit 467e9e5

File tree

5 files changed

+56
-46
lines changed

5 files changed

+56
-46
lines changed

drivers/vfio/vfio.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
264264
return group;
265265
}
266266

267+
/* called with vfio.group_lock held */
267268
static void vfio_group_release(struct kref *kref)
268269
{
269270
struct vfio_group *group = container_of(kref, struct vfio_group, kref);
@@ -287,13 +288,7 @@ static void vfio_group_release(struct kref *kref)
287288

288289
static void vfio_group_put(struct vfio_group *group)
289290
{
290-
mutex_lock(&vfio.group_lock);
291-
/*
292-
* Release needs to unlock to unregister the notifier, so only
293-
* unlock if not released.
294-
*/
295-
if (!kref_put(&group->kref, vfio_group_release))
296-
mutex_unlock(&vfio.group_lock);
291+
kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
297292
}
298293

299294
/* Assume group_lock or group reference is held */
@@ -401,7 +396,6 @@ static void vfio_device_release(struct kref *kref)
401396
struct vfio_device, kref);
402397
struct vfio_group *group = device->group;
403398

404-
mutex_lock(&group->device_lock);
405399
list_del(&device->group_next);
406400
mutex_unlock(&group->device_lock);
407401

@@ -416,8 +410,9 @@ static void vfio_device_release(struct kref *kref)
416410
/* Device reference always implies a group reference */
417411
static void vfio_device_put(struct vfio_device *device)
418412
{
419-
kref_put(&device->kref, vfio_device_release);
420-
vfio_group_put(device->group);
413+
struct vfio_group *group = device->group;
414+
kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
415+
vfio_group_put(group);
421416
}
422417

423418
static void vfio_device_get(struct vfio_device *device)
@@ -1116,10 +1111,10 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
11161111
*/
11171112
filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
11181113

1119-
fd_install(ret, filep);
1120-
11211114
vfio_device_get(device);
11221115
atomic_inc(&group->container_users);
1116+
1117+
fd_install(ret, filep);
11231118
break;
11241119
}
11251120
mutex_unlock(&group->device_lock);

fs/eventpoll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,8 +1654,8 @@ SYSCALL_DEFINE1(epoll_create1, int, flags)
16541654
error = PTR_ERR(file);
16551655
goto out_free_fd;
16561656
}
1657-
fd_install(fd, file);
16581657
ep->file = file;
1658+
fd_install(fd, file);
16591659
return fd;
16601660

16611661
out_free_fd:

fs/namei.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ int __inode_permission(struct inode *inode, int mask)
352352
/**
353353
* sb_permission - Check superblock-level permissions
354354
* @sb: Superblock of inode to check permission on
355+
* @inode: Inode to check permission on
355356
* @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
356357
*
357358
* Separate out file-system wide checks from inode-specific permission checks.
@@ -656,6 +657,7 @@ int sysctl_protected_hardlinks __read_mostly = 1;
656657
/**
657658
* may_follow_link - Check symlink following for unsafe situations
658659
* @link: The path of the symlink
660+
* @nd: nameidata pathwalk data
659661
*
660662
* In the case of the sysctl_protected_symlinks sysctl being enabled,
661663
* CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is

include/linux/kref.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/bug.h>
1919
#include <linux/atomic.h>
2020
#include <linux/kernel.h>
21+
#include <linux/mutex.h>
2122

2223
struct kref {
2324
atomic_t refcount;
@@ -93,4 +94,21 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
9394
{
9495
return kref_sub(kref, 1, release);
9596
}
97+
98+
static inline int kref_put_mutex(struct kref *kref,
99+
void (*release)(struct kref *kref),
100+
struct mutex *lock)
101+
{
102+
WARN_ON(release == NULL);
103+
if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
104+
mutex_lock(lock);
105+
if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
106+
mutex_unlock(lock);
107+
return 0;
108+
}
109+
release(kref);
110+
return 1;
111+
}
112+
return 0;
113+
}
96114
#endif /* _KREF_H_ */

ipc/mqueue.c

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,6 @@ static struct file *do_create(struct ipc_namespace *ipc_ns, struct inode *dir,
726726
struct mq_attr *attr)
727727
{
728728
const struct cred *cred = current_cred();
729-
struct file *result;
730729
int ret;
731730

732731
if (attr) {
@@ -748,21 +747,11 @@ static struct file *do_create(struct ipc_namespace *ipc_ns, struct inode *dir,
748747
}
749748

750749
mode &= ~current_umask();
751-
ret = mnt_want_write(path->mnt);
752-
if (ret)
753-
return ERR_PTR(ret);
754750
ret = vfs_create(dir, path->dentry, mode, true);
755751
path->dentry->d_fsdata = NULL;
756-
if (!ret)
757-
result = dentry_open(path, oflag, cred);
758-
else
759-
result = ERR_PTR(ret);
760-
/*
761-
* dentry_open() took a persistent mnt_want_write(),
762-
* so we can now drop this one.
763-
*/
764-
mnt_drop_write(path->mnt);
765-
return result;
752+
if (ret)
753+
return ERR_PTR(ret);
754+
return dentry_open(path, oflag, cred);
766755
}
767756

768757
/* Opens existing queue */
@@ -788,7 +777,9 @@ SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
788777
struct mq_attr attr;
789778
int fd, error;
790779
struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
791-
struct dentry *root = ipc_ns->mq_mnt->mnt_root;
780+
struct vfsmount *mnt = ipc_ns->mq_mnt;
781+
struct dentry *root = mnt->mnt_root;
782+
int ro;
792783

793784
if (u_attr && copy_from_user(&attr, u_attr, sizeof(struct mq_attr)))
794785
return -EFAULT;
@@ -802,14 +793,15 @@ SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
802793
if (fd < 0)
803794
goto out_putname;
804795

796+
ro = mnt_want_write(mnt); /* we'll drop it in any case */
805797
error = 0;
806798
mutex_lock(&root->d_inode->i_mutex);
807799
path.dentry = lookup_one_len(name, root, strlen(name));
808800
if (IS_ERR(path.dentry)) {
809801
error = PTR_ERR(path.dentry);
810802
goto out_putfd;
811803
}
812-
path.mnt = mntget(ipc_ns->mq_mnt);
804+
path.mnt = mntget(mnt);
813805

814806
if (oflag & O_CREAT) {
815807
if (path.dentry->d_inode) { /* entry already exists */
@@ -820,6 +812,10 @@ SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
820812
}
821813
filp = do_open(&path, oflag);
822814
} else {
815+
if (ro) {
816+
error = ro;
817+
goto out;
818+
}
823819
filp = do_create(ipc_ns, root->d_inode,
824820
&path, oflag, mode,
825821
u_attr ? &attr : NULL);
@@ -845,6 +841,7 @@ SYSCALL_DEFINE4(mq_open, const char __user *, u_name, int, oflag, umode_t, mode,
845841
fd = error;
846842
}
847843
mutex_unlock(&root->d_inode->i_mutex);
844+
mnt_drop_write(mnt);
848845
out_putname:
849846
putname(name);
850847
return fd;
@@ -857,40 +854,38 @@ SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
857854
struct dentry *dentry;
858855
struct inode *inode = NULL;
859856
struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
857+
struct vfsmount *mnt = ipc_ns->mq_mnt;
860858

861859
name = getname(u_name);
862860
if (IS_ERR(name))
863861
return PTR_ERR(name);
864862

865-
mutex_lock_nested(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex,
866-
I_MUTEX_PARENT);
867-
dentry = lookup_one_len(name, ipc_ns->mq_mnt->mnt_root, strlen(name));
863+
err = mnt_want_write(mnt);
864+
if (err)
865+
goto out_name;
866+
mutex_lock_nested(&mnt->mnt_root->d_inode->i_mutex, I_MUTEX_PARENT);
867+
dentry = lookup_one_len(name, mnt->mnt_root, strlen(name));
868868
if (IS_ERR(dentry)) {
869869
err = PTR_ERR(dentry);
870870
goto out_unlock;
871871
}
872872

873-
if (!dentry->d_inode) {
874-
err = -ENOENT;
875-
goto out_err;
876-
}
877-
878873
inode = dentry->d_inode;
879-
if (inode)
874+
if (!inode) {
875+
err = -ENOENT;
876+
} else {
880877
ihold(inode);
881-
err = mnt_want_write(ipc_ns->mq_mnt);
882-
if (err)
883-
goto out_err;
884-
err = vfs_unlink(dentry->d_parent->d_inode, dentry);
885-
mnt_drop_write(ipc_ns->mq_mnt);
886-
out_err:
878+
err = vfs_unlink(dentry->d_parent->d_inode, dentry);
879+
}
887880
dput(dentry);
888881

889882
out_unlock:
890-
mutex_unlock(&ipc_ns->mq_mnt->mnt_root->d_inode->i_mutex);
891-
putname(name);
883+
mutex_unlock(&mnt->mnt_root->d_inode->i_mutex);
892884
if (inode)
893885
iput(inode);
886+
mnt_drop_write(mnt);
887+
out_name:
888+
putname(name);
894889

895890
return err;
896891
}

0 commit comments

Comments
 (0)