ecryptfs_lookup_interpose(): lower_dentry->d_parent is not stable either
authorAl Viro <viro@zeniv.linux.org.uk>
Sun, 3 Nov 2019 18:55:43 +0000 (13:55 -0500)
committerBen Hutchings <ben@decadent.org.uk>
Thu, 19 Dec 2019 15:58:59 +0000 (15:58 +0000)
commit 762c69685ff7ad5ad7fee0656671e20a0c9c864d upstream.

We need to get the underlying dentry of parent; sure, absent the races
it is the parent of underlying dentry, but there's nothing to prevent
losing a timeslice to preemtion in the middle of evaluation of
lower_dentry->d_parent->d_inode, having another process move lower_dentry
around and have its (ex)parent not pinned anymore and freed on memory
pressure.  Then we regain CPU and try to fetch ->d_inode from memory
that is freed by that point.

dentry->d_parent *is* stable here - it's an argument of ->lookup() and
we are guaranteed that it won't be moved anywhere until we feed it
to d_add/d_splice_alias.  So we safely go that way to get to its
underlying dentry.

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
[bwh: Backported to 3.16:
 - Open-code d_inode()
 - Adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
fs/ecryptfs/inode.c

index 7a04f8a3c6eea991fa2881144a7d6a01cc544d3c..2ce9d8a8c06d20fe3788fac212860c256df44b9d 100644 (file)
@@ -341,9 +341,9 @@ static int ecryptfs_lookup_interpose(struct dentry *dentry,
                                     struct dentry *lower_dentry,
                                     struct inode *dir_inode)
 {
+       struct path *path = ecryptfs_dentry_to_lower_path(dentry->d_parent);
        struct inode *inode, *lower_inode;
        struct ecryptfs_dentry_info *dentry_info;
-       struct vfsmount *lower_mnt;
        int rc = 0;
 
        dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
@@ -355,12 +355,11 @@ static int ecryptfs_lookup_interpose(struct dentry *dentry,
                return -ENOMEM;
        }
 
-       lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
-       fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
+       fsstack_copy_attr_atime(dir_inode, path->dentry->d_inode);
        BUG_ON(!d_count(lower_dentry));
 
        ecryptfs_set_dentry_private(dentry, dentry_info);
-       dentry_info->lower_path.mnt = lower_mnt;
+       dentry_info->lower_path.mnt = mntget(path->mnt);
        dentry_info->lower_path.dentry = lower_dentry;
 
        /*