Commit ab596ad8 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Lachlan McIlroy

xfs: fix dentry aliasing issues in open_by_handle

Open by handle just grabs an inode by handle and then creates itself
a dentry for it.  While this works for regular files it is horribly
broken for directories, where the VFS locking relies on the fact that
there is only just one single dentry for a given inode, and that
these are always connected to the root of the filesystem so that
it's locking algorithms work (see Documentations/filesystems/Locking)

Remove all the existing open by handle code and replace it with a small
wrapper around the exportfs code which deals with all these issues.
At the same time we also make the checks for a valid handle strict
enough to reject all not perfectly well formed handles - given that
we never hand out others that's okay and simplifies the code.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarDave Chinner <david@fromorbit.com>
parent f3b8436a
config XFS_FS config XFS_FS
tristate "XFS filesystem support" tristate "XFS filesystem support"
depends on BLOCK depends on BLOCK
select EXPORTFS
help help
XFS is a high performance journaling filesystem which originated XFS is a high performance journaling filesystem which originated
on the SGI IRIX platform. It is completely multi-threaded, can on the SGI IRIX platform. It is completely multi-threaded, can
......
This diff is collapsed.
...@@ -34,16 +34,13 @@ xfs_find_handle( ...@@ -34,16 +34,13 @@ xfs_find_handle(
extern int extern int
xfs_open_by_handle( xfs_open_by_handle(
xfs_mount_t *mp,
xfs_fsop_handlereq_t *hreq,
struct file *parfilp, struct file *parfilp,
struct inode *parinode); xfs_fsop_handlereq_t *hreq);
extern int extern int
xfs_readlink_by_handle( xfs_readlink_by_handle(
xfs_mount_t *mp, struct file *parfilp,
xfs_fsop_handlereq_t *hreq, xfs_fsop_handlereq_t *hreq);
struct inode *parinode);
extern int extern int
xfs_attrmulti_attr_get( xfs_attrmulti_attr_get(
...@@ -67,6 +64,12 @@ xfs_attrmulti_attr_remove( ...@@ -67,6 +64,12 @@ xfs_attrmulti_attr_remove(
char *name, char *name,
__uint32_t flags); __uint32_t flags);
extern struct dentry *
xfs_handle_to_dentry(
struct file *parfilp,
void __user *uhandle,
u32 hlen);
extern long extern long
xfs_file_ioctl( xfs_file_ioctl(
struct file *filp, struct file *filp,
......
...@@ -340,96 +340,24 @@ xfs_compat_handlereq_copyin( ...@@ -340,96 +340,24 @@ xfs_compat_handlereq_copyin(
return 0; return 0;
} }
/* STATIC struct dentry *
* Convert userspace handle data into inode. xfs_compat_handlereq_to_dentry(
* struct file *parfilp,
* We use the fact that all the fsop_handlereq ioctl calls have a data compat_xfs_fsop_handlereq_t *hreq)
* structure argument whose first component is always a xfs_fsop_handlereq_t,
* so we can pass that sub structure into this handy, shared routine.
*
* If no error, caller must always iput the returned inode.
*/
STATIC int
xfs_vget_fsop_handlereq_compat(
xfs_mount_t *mp,
struct inode *parinode, /* parent inode pointer */
compat_xfs_fsop_handlereq_t *hreq,
struct inode **inode)
{ {
void __user *hanp; return xfs_handle_to_dentry(parfilp,
size_t hlen; compat_ptr(hreq->ihandle), hreq->ihandlen);
xfs_fid_t *xfid;
xfs_handle_t *handlep;
xfs_handle_t handle;
xfs_inode_t *ip;
xfs_ino_t ino;
__u32 igen;
int error;
/*
* Only allow handle opens under a directory.
*/
if (!S_ISDIR(parinode->i_mode))
return XFS_ERROR(ENOTDIR);
hanp = compat_ptr(hreq->ihandle);
hlen = hreq->ihandlen;
handlep = &handle;
if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
return XFS_ERROR(EINVAL);
if (copy_from_user(handlep, hanp, hlen))
return XFS_ERROR(EFAULT);
if (hlen < sizeof(*handlep))
memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
if (hlen > sizeof(handlep->ha_fsid)) {
if (handlep->ha_fid.fid_len !=
(hlen - sizeof(handlep->ha_fsid) -
sizeof(handlep->ha_fid.fid_len)) ||
handlep->ha_fid.fid_pad)
return XFS_ERROR(EINVAL);
}
/*
* Crack the handle, obtain the inode # & generation #
*/
xfid = (struct xfs_fid *)&handlep->ha_fid;
if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
ino = xfid->fid_ino;
igen = xfid->fid_gen;
} else {
return XFS_ERROR(EINVAL);
}
/*
* Get the XFS inode, building a Linux inode to go with it.
*/
error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
if (error)
return error;
if (ip == NULL)
return XFS_ERROR(EIO);
if (ip->i_d.di_gen != igen) {
xfs_iput_new(ip, XFS_ILOCK_SHARED);
return XFS_ERROR(ENOENT);
}
xfs_iunlock(ip, XFS_ILOCK_SHARED);
*inode = VFS_I(ip);
return 0;
} }
STATIC int STATIC int
xfs_compat_attrlist_by_handle( xfs_compat_attrlist_by_handle(
xfs_mount_t *mp, struct file *parfilp,
void __user *arg, void __user *arg)
struct inode *parinode)
{ {
int error; int error;
attrlist_cursor_kern_t *cursor; attrlist_cursor_kern_t *cursor;
compat_xfs_fsop_attrlist_handlereq_t al_hreq; compat_xfs_fsop_attrlist_handlereq_t al_hreq;
struct inode *inode; struct dentry *dentry;
char *kbuf; char *kbuf;
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
...@@ -446,17 +374,17 @@ xfs_compat_attrlist_by_handle( ...@@ -446,17 +374,17 @@ xfs_compat_attrlist_by_handle(
if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE)) if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
return -XFS_ERROR(EINVAL); return -XFS_ERROR(EINVAL);
error = xfs_vget_fsop_handlereq_compat(mp, parinode, &al_hreq.hreq, dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
&inode); if (IS_ERR(dentry))
if (error) return PTR_ERR(dentry);
goto out;
error = -ENOMEM;
kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL); kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
if (!kbuf) if (!kbuf)
goto out_vn_rele; goto out_dput;
cursor = (attrlist_cursor_kern_t *)&al_hreq.pos; cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen, error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
al_hreq.flags, cursor); al_hreq.flags, cursor);
if (error) if (error)
goto out_kfree; goto out_kfree;
...@@ -466,22 +394,20 @@ xfs_compat_attrlist_by_handle( ...@@ -466,22 +394,20 @@ xfs_compat_attrlist_by_handle(
out_kfree: out_kfree:
kfree(kbuf); kfree(kbuf);
out_vn_rele: out_dput:
iput(inode); dput(dentry);
out: return error;
return -error;
} }
STATIC int STATIC int
xfs_compat_attrmulti_by_handle( xfs_compat_attrmulti_by_handle(
xfs_mount_t *mp, struct file *parfilp,
void __user *arg, void __user *arg)
struct inode *parinode)
{ {
int error; int error;
compat_xfs_attr_multiop_t *ops; compat_xfs_attr_multiop_t *ops;
compat_xfs_fsop_attrmulti_handlereq_t am_hreq; compat_xfs_fsop_attrmulti_handlereq_t am_hreq;
struct inode *inode; struct dentry *dentry;
unsigned int i, size; unsigned int i, size;
char *attr_name; char *attr_name;
...@@ -491,20 +417,19 @@ xfs_compat_attrmulti_by_handle( ...@@ -491,20 +417,19 @@ xfs_compat_attrmulti_by_handle(
sizeof(compat_xfs_fsop_attrmulti_handlereq_t))) sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
error = xfs_vget_fsop_handlereq_compat(mp, parinode, &am_hreq.hreq, dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
&inode); if (IS_ERR(dentry))
if (error) return PTR_ERR(dentry);
goto out;
error = E2BIG; error = E2BIG;
size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t); size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
if (!size || size > 16 * PAGE_SIZE) if (!size || size > 16 * PAGE_SIZE)
goto out_vn_rele; goto out_dput;
error = ENOMEM; error = ENOMEM;
ops = kmalloc(size, GFP_KERNEL); ops = kmalloc(size, GFP_KERNEL);
if (!ops) if (!ops)
goto out_vn_rele; goto out_dput;
error = EFAULT; error = EFAULT;
if (copy_from_user(ops, compat_ptr(am_hreq.ops), size)) if (copy_from_user(ops, compat_ptr(am_hreq.ops), size))
...@@ -527,20 +452,21 @@ xfs_compat_attrmulti_by_handle( ...@@ -527,20 +452,21 @@ xfs_compat_attrmulti_by_handle(
switch (ops[i].am_opcode) { switch (ops[i].am_opcode) {
case ATTR_OP_GET: case ATTR_OP_GET:
ops[i].am_error = xfs_attrmulti_attr_get(inode, ops[i].am_error = xfs_attrmulti_attr_get(
attr_name, dentry->d_inode, attr_name,
compat_ptr(ops[i].am_attrvalue), compat_ptr(ops[i].am_attrvalue),
&ops[i].am_length, ops[i].am_flags); &ops[i].am_length, ops[i].am_flags);
break; break;
case ATTR_OP_SET: case ATTR_OP_SET:
ops[i].am_error = xfs_attrmulti_attr_set(inode, ops[i].am_error = xfs_attrmulti_attr_set(
attr_name, dentry->d_inode, attr_name,
compat_ptr(ops[i].am_attrvalue), compat_ptr(ops[i].am_attrvalue),
ops[i].am_length, ops[i].am_flags); ops[i].am_length, ops[i].am_flags);
break; break;
case ATTR_OP_REMOVE: case ATTR_OP_REMOVE:
ops[i].am_error = xfs_attrmulti_attr_remove(inode, ops[i].am_error = xfs_attrmulti_attr_remove(
attr_name, ops[i].am_flags); dentry->d_inode, attr_name,
ops[i].am_flags);
break; break;
default: default:
ops[i].am_error = EINVAL; ops[i].am_error = EINVAL;
...@@ -553,22 +479,20 @@ xfs_compat_attrmulti_by_handle( ...@@ -553,22 +479,20 @@ xfs_compat_attrmulti_by_handle(
kfree(attr_name); kfree(attr_name);
out_kfree_ops: out_kfree_ops:
kfree(ops); kfree(ops);
out_vn_rele: out_dput:
iput(inode); dput(dentry);
out:
return -error; return -error;
} }
STATIC int STATIC int
xfs_compat_fssetdm_by_handle( xfs_compat_fssetdm_by_handle(
xfs_mount_t *mp, struct file *parfilp,
void __user *arg, void __user *arg)
struct inode *parinode)
{ {
int error; int error;
struct fsdmidata fsd; struct fsdmidata fsd;
compat_xfs_fsop_setdm_handlereq_t dmhreq; compat_xfs_fsop_setdm_handlereq_t dmhreq;
struct inode *inode; struct dentry *dentry;
if (!capable(CAP_MKNOD)) if (!capable(CAP_MKNOD))
return -XFS_ERROR(EPERM); return -XFS_ERROR(EPERM);
...@@ -576,12 +500,11 @@ xfs_compat_fssetdm_by_handle( ...@@ -576,12 +500,11 @@ xfs_compat_fssetdm_by_handle(
sizeof(compat_xfs_fsop_setdm_handlereq_t))) sizeof(compat_xfs_fsop_setdm_handlereq_t)))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
error = xfs_vget_fsop_handlereq_compat(mp, parinode, &dmhreq.hreq, dentry = xfs_compat_handlereq_to_dentry(parfilp, &dmhreq.hreq);
&inode); if (IS_ERR(dentry))
if (error) return PTR_ERR(dentry);
return -error;
if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) { if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
error = -XFS_ERROR(EPERM); error = -XFS_ERROR(EPERM);
goto out; goto out;
} }
...@@ -591,11 +514,11 @@ xfs_compat_fssetdm_by_handle( ...@@ -591,11 +514,11 @@ xfs_compat_fssetdm_by_handle(
goto out; goto out;
} }
error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask, error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
fsd.fsd_dmstate); fsd.fsd_dmstate);
out: out:
iput(inode); dput(dentry);
return error; return error;
} }
...@@ -722,21 +645,21 @@ xfs_file_compat_ioctl( ...@@ -722,21 +645,21 @@ xfs_file_compat_ioctl(
if (xfs_compat_handlereq_copyin(&hreq, arg)) if (xfs_compat_handlereq_copyin(&hreq, arg))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
return xfs_open_by_handle(mp, &hreq, filp, inode); return xfs_open_by_handle(filp, &hreq);
} }
case XFS_IOC_READLINK_BY_HANDLE_32: { case XFS_IOC_READLINK_BY_HANDLE_32: {
struct xfs_fsop_handlereq hreq; struct xfs_fsop_handlereq hreq;
if (xfs_compat_handlereq_copyin(&hreq, arg)) if (xfs_compat_handlereq_copyin(&hreq, arg))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
return xfs_readlink_by_handle(mp, &hreq, inode); return xfs_readlink_by_handle(filp, &hreq);
} }
case XFS_IOC_ATTRLIST_BY_HANDLE_32: case XFS_IOC_ATTRLIST_BY_HANDLE_32:
return xfs_compat_attrlist_by_handle(mp, arg, inode); return xfs_compat_attrlist_by_handle(filp, arg);
case XFS_IOC_ATTRMULTI_BY_HANDLE_32: case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
return xfs_compat_attrmulti_by_handle(mp, arg, inode); return xfs_compat_attrmulti_by_handle(filp, arg);
case XFS_IOC_FSSETDM_BY_HANDLE_32: case XFS_IOC_FSSETDM_BY_HANDLE_32:
return xfs_compat_fssetdm_by_handle(mp, arg, inode); return xfs_compat_fssetdm_by_handle(filp, arg);
default: default:
return -XFS_ERROR(ENOIOCTLCMD); return -XFS_ERROR(ENOIOCTLCMD);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment