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
......
...@@ -50,12 +50,14 @@ ...@@ -50,12 +50,14 @@
#include "xfs_vnodeops.h" #include "xfs_vnodeops.h"
#include "xfs_quota.h" #include "xfs_quota.h"
#include "xfs_inode_item.h" #include "xfs_inode_item.h"
#include "xfs_export.h"
#include <linux/capability.h> #include <linux/capability.h>
#include <linux/dcache.h> #include <linux/dcache.h>
#include <linux/mount.h> #include <linux/mount.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/exportfs.h>
/* /*
* xfs_find_handle maps from userspace xfs_fsop_handlereq structure to * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
...@@ -164,97 +166,69 @@ xfs_find_handle( ...@@ -164,97 +166,69 @@ xfs_find_handle(
return 0; return 0;
} }
/* /*
* Convert userspace handle data into inode. * No need to do permission checks on the various pathname components
* * as the handle operations are privileged.
* We use the fact that all the fsop_handlereq ioctl calls have a data
* 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 STATIC int
xfs_vget_fsop_handlereq( xfs_handle_acceptable(
xfs_mount_t *mp, void *context,
struct inode *parinode, /* parent inode pointer */ struct dentry *dentry)
xfs_fsop_handlereq_t *hreq, {
struct inode **inode) return 1;
}
/*
* Convert userspace handle data into a dentry.
*/
struct dentry *
xfs_handle_to_dentry(
struct file *parfilp,
void __user *uhandle,
u32 hlen)
{ {
void __user *hanp;
size_t hlen;
xfs_fid_t *xfid;
xfs_handle_t *handlep;
xfs_handle_t handle; xfs_handle_t handle;
xfs_inode_t *ip; struct xfs_fid64 fid;
xfs_ino_t ino;
__u32 igen;
int error;
/* /*
* Only allow handle opens under a directory. * Only allow handle opens under a directory.
*/ */
if (!S_ISDIR(parinode->i_mode)) if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
return XFS_ERROR(ENOTDIR); return ERR_PTR(-ENOTDIR);
hanp = hreq->ihandle; if (hlen != sizeof(xfs_handle_t))
hlen = hreq->ihandlen; return ERR_PTR(-EINVAL);
handlep = &handle; if (copy_from_user(&handle, uhandle, hlen))
return ERR_PTR(-EFAULT);
if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep)) if (handle.ha_fid.fid_len !=
return XFS_ERROR(EINVAL); sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
if (copy_from_user(handlep, hanp, hlen)) return ERR_PTR(-EINVAL);
return XFS_ERROR(EFAULT);
if (hlen < sizeof(*handlep)) memset(&fid, 0, sizeof(struct fid));
memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen); fid.ino = handle.ha_fid.fid_ino;
if (hlen > sizeof(handlep->ha_fsid)) { fid.gen = handle.ha_fid.fid_gen;
if (handlep->ha_fid.fid_len !=
(hlen - sizeof(handlep->ha_fsid) - return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
sizeof(handlep->ha_fid.fid_len)) || FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
handlep->ha_fid.fid_pad) xfs_handle_acceptable, NULL);
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); STATIC struct dentry *
return 0; xfs_handlereq_to_dentry(
struct file *parfilp,
xfs_fsop_handlereq_t *hreq)
{
return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
} }
int 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)
{ {
const struct cred *cred = current_cred(); const struct cred *cred = current_cred();
int error; int error;
int new_fd; int fd;
int permflag; int permflag;
struct file *filp; struct file *filp;
struct inode *inode; struct inode *inode;
...@@ -263,19 +237,21 @@ xfs_open_by_handle( ...@@ -263,19 +237,21 @@ xfs_open_by_handle(
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -XFS_ERROR(EPERM); return -XFS_ERROR(EPERM);
error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode); dentry = xfs_handlereq_to_dentry(parfilp, hreq);
if (error) if (IS_ERR(dentry))
return -error; return PTR_ERR(dentry);
inode = dentry->d_inode;
/* Restrict xfs_open_by_handle to directories & regular files. */ /* Restrict xfs_open_by_handle to directories & regular files. */
if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) { if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
iput(inode); error = -XFS_ERROR(EPERM);
return -XFS_ERROR(EINVAL); goto out_dput;
} }
#if BITS_PER_LONG != 32 #if BITS_PER_LONG != 32
hreq->oflags |= O_LARGEFILE; hreq->oflags |= O_LARGEFILE;
#endif #endif
/* Put open permission in namei format. */ /* Put open permission in namei format. */
permflag = hreq->oflags; permflag = hreq->oflags;
if ((permflag+1) & O_ACCMODE) if ((permflag+1) & O_ACCMODE)
...@@ -285,50 +261,45 @@ xfs_open_by_handle( ...@@ -285,50 +261,45 @@ xfs_open_by_handle(
if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) && if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
(permflag & FMODE_WRITE) && IS_APPEND(inode)) { (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
iput(inode); error = -XFS_ERROR(EPERM);
return -XFS_ERROR(EPERM); goto out_dput;
} }
if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) { if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
iput(inode); error = -XFS_ERROR(EACCES);
return -XFS_ERROR(EACCES); goto out_dput;
} }
/* Can't write directories. */ /* Can't write directories. */
if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) { if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
iput(inode); error = -XFS_ERROR(EISDIR);
return -XFS_ERROR(EISDIR); goto out_dput;
} }
if ((new_fd = get_unused_fd()) < 0) { fd = get_unused_fd();
iput(inode); if (fd < 0) {
return new_fd; error = fd;
goto out_dput;
} }
dentry = d_obtain_alias(inode); filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
if (IS_ERR(dentry)) { hreq->oflags, cred);
put_unused_fd(new_fd);
return PTR_ERR(dentry);
}
/* Ensure umount returns EBUSY on umounts while this file is open. */
mntget(parfilp->f_path.mnt);
/* Create file pointer. */
filp = dentry_open(dentry, parfilp->f_path.mnt, hreq->oflags, cred);
if (IS_ERR(filp)) { if (IS_ERR(filp)) {
put_unused_fd(new_fd); put_unused_fd(fd);
return -XFS_ERROR(-PTR_ERR(filp)); return PTR_ERR(filp);
} }
if (inode->i_mode & S_IFREG) { if (inode->i_mode & S_IFREG) {
/* invisible operation should not change atime */
filp->f_flags |= O_NOATIME; filp->f_flags |= O_NOATIME;
filp->f_mode |= FMODE_NOCMTIME; filp->f_mode |= FMODE_NOCMTIME;
} }
fd_install(new_fd, filp); fd_install(fd, filp);
return new_fd; return fd;
out_dput:
dput(dentry);
return error;
} }
/* /*
...@@ -359,11 +330,10 @@ do_readlink( ...@@ -359,11 +330,10 @@ do_readlink(
int 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)
{ {
struct inode *inode; struct dentry *dentry;
__u32 olen; __u32 olen;
void *link; void *link;
int error; int error;
...@@ -371,26 +341,28 @@ xfs_readlink_by_handle( ...@@ -371,26 +341,28 @@ xfs_readlink_by_handle(
if (!capable(CAP_SYS_ADMIN)) if (!capable(CAP_SYS_ADMIN))
return -XFS_ERROR(EPERM); return -XFS_ERROR(EPERM);
error = xfs_vget_fsop_handlereq(mp, parinode, hreq, &inode); dentry = xfs_handlereq_to_dentry(parfilp, hreq);
if (error) if (IS_ERR(dentry))
return -error; return PTR_ERR(dentry);
/* Restrict this handle operation to symlinks only. */ /* Restrict this handle operation to symlinks only. */
if (!S_ISLNK(inode->i_mode)) { if (!S_ISLNK(dentry->d_inode->i_mode)) {
error = -XFS_ERROR(EINVAL); error = -XFS_ERROR(EINVAL);
goto out_iput; goto out_dput;
} }
if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) { if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
error = -XFS_ERROR(EFAULT); error = -XFS_ERROR(EFAULT);
goto out_iput; goto out_dput;
} }
link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
if (!link) if (!link) {
goto out_iput; error = -XFS_ERROR(ENOMEM);
goto out_dput;
}
error = -xfs_readlink(XFS_I(inode), link); error = -xfs_readlink(XFS_I(dentry->d_inode), link);
if (error) if (error)
goto out_kfree; goto out_kfree;
error = do_readlink(hreq->ohandle, olen, link); error = do_readlink(hreq->ohandle, olen, link);
...@@ -399,32 +371,31 @@ xfs_readlink_by_handle( ...@@ -399,32 +371,31 @@ xfs_readlink_by_handle(
out_kfree: out_kfree:
kfree(link); kfree(link);
out_iput: out_dput:
iput(inode); dput(dentry);
return error; return error;
} }
STATIC int STATIC int
xfs_fssetdm_by_handle( xfs_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;
xfs_fsop_setdm_handlereq_t dmhreq; 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);
if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t))) if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode); dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
if (error) if (IS_ERR(dentry))
return -error; return PTR_ERR(dentry);
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;
} }
...@@ -434,24 +405,23 @@ xfs_fssetdm_by_handle( ...@@ -434,24 +405,23 @@ xfs_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;
} }
STATIC int STATIC int
xfs_attrlist_by_handle( xfs_attrlist_by_handle(
xfs_mount_t *mp, struct file *parfilp,
void __user *arg, void __user *arg)
struct inode *parinode)
{ {
int error; int error = -ENOMEM;
attrlist_cursor_kern_t *cursor; attrlist_cursor_kern_t *cursor;
xfs_fsop_attrlist_handlereq_t al_hreq; 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))
...@@ -467,16 +437,16 @@ xfs_attrlist_by_handle( ...@@ -467,16 +437,16 @@ xfs_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(mp, parinode, &al_hreq.hreq, &inode); dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
if (error) if (IS_ERR(dentry))
goto out; return PTR_ERR(dentry);
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;
...@@ -486,10 +456,9 @@ xfs_attrlist_by_handle( ...@@ -486,10 +456,9 @@ xfs_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;
} }
int int
...@@ -564,15 +533,13 @@ xfs_attrmulti_attr_remove( ...@@ -564,15 +533,13 @@ xfs_attrmulti_attr_remove(
STATIC int STATIC int
xfs_attrmulti_by_handle( xfs_attrmulti_by_handle(
xfs_mount_t *mp,
void __user *arg,
struct file *parfilp, struct file *parfilp,
struct inode *parinode) void __user *arg)
{ {
int error; int error;
xfs_attr_multiop_t *ops; xfs_attr_multiop_t *ops;
xfs_fsop_attrmulti_handlereq_t am_hreq; 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;
...@@ -581,19 +548,19 @@ xfs_attrmulti_by_handle( ...@@ -581,19 +548,19 @@ xfs_attrmulti_by_handle(
if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t))) if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
return -XFS_ERROR(EFAULT); return -XFS_ERROR(EFAULT);
error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode); dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
if (error) if (IS_ERR(dentry))
goto out; return PTR_ERR(dentry);
error = E2BIG; error = E2BIG;
size = am_hreq.opcount * sizeof(xfs_attr_multiop_t); size = am_hreq.opcount * sizeof(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, am_hreq.ops, size)) if (copy_from_user(ops, am_hreq.ops, size))
...@@ -615,25 +582,28 @@ xfs_attrmulti_by_handle( ...@@ -615,25 +582,28 @@ xfs_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, ops[i].am_attrvalue, dentry->d_inode, attr_name,
&ops[i].am_length, ops[i].am_flags); ops[i].am_attrvalue, &ops[i].am_length,
ops[i].am_flags);
break; break;
case ATTR_OP_SET: case ATTR_OP_SET:
ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
if (ops[i].am_error) if (ops[i].am_error)
break; break;
ops[i].am_error = xfs_attrmulti_attr_set(inode, ops[i].am_error = xfs_attrmulti_attr_set(
attr_name, ops[i].am_attrvalue, dentry->d_inode, attr_name,
ops[i].am_length, ops[i].am_flags); ops[i].am_attrvalue, ops[i].am_length,
ops[i].am_flags);
mnt_drop_write(parfilp->f_path.mnt); mnt_drop_write(parfilp->f_path.mnt);
break; break;
case ATTR_OP_REMOVE: case ATTR_OP_REMOVE:
ops[i].am_error = mnt_want_write(parfilp->f_path.mnt); ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
if (ops[i].am_error) if (ops[i].am_error)
break; break;
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);
mnt_drop_write(parfilp->f_path.mnt); mnt_drop_write(parfilp->f_path.mnt);
break; break;
default: default:
...@@ -647,9 +617,8 @@ xfs_attrmulti_by_handle( ...@@ -647,9 +617,8 @@ xfs_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;
} }
...@@ -1440,23 +1409,23 @@ xfs_file_ioctl( ...@@ -1440,23 +1409,23 @@ xfs_file_ioctl(
if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
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_FSSETDM_BY_HANDLE: case XFS_IOC_FSSETDM_BY_HANDLE:
return xfs_fssetdm_by_handle(mp, arg, inode); return xfs_fssetdm_by_handle(filp, arg);
case XFS_IOC_READLINK_BY_HANDLE: { case XFS_IOC_READLINK_BY_HANDLE: {
xfs_fsop_handlereq_t hreq; xfs_fsop_handlereq_t hreq;
if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t))) if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
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: case XFS_IOC_ATTRLIST_BY_HANDLE:
return xfs_attrlist_by_handle(mp, arg, inode); return xfs_attrlist_by_handle(filp, arg);
case XFS_IOC_ATTRMULTI_BY_HANDLE: case XFS_IOC_ATTRMULTI_BY_HANDLE:
return xfs_attrmulti_by_handle(mp, arg, filp, inode); return xfs_attrmulti_by_handle(filp, arg);
case XFS_IOC_SWAPEXT: { case XFS_IOC_SWAPEXT: {
struct xfs_swapext sxp; struct xfs_swapext sxp;
......
...@@ -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