Back to home page

LXR

 
 

    


0001 /*
0002   File: fs/xattr.c
0003 
0004   Extended attribute handling.
0005 
0006   Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
0007   Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
0008   Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
0009  */
0010 #include <linux/fs.h>
0011 #include <linux/slab.h>
0012 #include <linux/file.h>
0013 #include <linux/xattr.h>
0014 #include <linux/mount.h>
0015 #include <linux/namei.h>
0016 #include <linux/security.h>
0017 #include <linux/evm.h>
0018 #include <linux/syscalls.h>
0019 #include <linux/export.h>
0020 #include <linux/fsnotify.h>
0021 #include <linux/audit.h>
0022 #include <linux/vmalloc.h>
0023 #include <linux/posix_acl_xattr.h>
0024 
0025 #include <linux/uaccess.h>
0026 
0027 static const char *
0028 strcmp_prefix(const char *a, const char *a_prefix)
0029 {
0030     while (*a_prefix && *a == *a_prefix) {
0031         a++;
0032         a_prefix++;
0033     }
0034     return *a_prefix ? NULL : a;
0035 }
0036 
0037 /*
0038  * In order to implement different sets of xattr operations for each xattr
0039  * prefix, a filesystem should create a null-terminated array of struct
0040  * xattr_handler (one for each prefix) and hang a pointer to it off of the
0041  * s_xattr field of the superblock.
0042  */
0043 #define for_each_xattr_handler(handlers, handler)       \
0044     if (handlers)                       \
0045         for ((handler) = *(handlers)++;         \
0046             (handler) != NULL;          \
0047             (handler) = *(handlers)++)
0048 
0049 /*
0050  * Find the xattr_handler with the matching prefix.
0051  */
0052 static const struct xattr_handler *
0053 xattr_resolve_name(struct inode *inode, const char **name)
0054 {
0055     const struct xattr_handler **handlers = inode->i_sb->s_xattr;
0056     const struct xattr_handler *handler;
0057 
0058     if (!(inode->i_opflags & IOP_XATTR)) {
0059         if (unlikely(is_bad_inode(inode)))
0060             return ERR_PTR(-EIO);
0061         return ERR_PTR(-EOPNOTSUPP);
0062     }
0063     for_each_xattr_handler(handlers, handler) {
0064         const char *n;
0065 
0066         n = strcmp_prefix(*name, xattr_prefix(handler));
0067         if (n) {
0068             if (!handler->prefix ^ !*n) {
0069                 if (*n)
0070                     continue;
0071                 return ERR_PTR(-EINVAL);
0072             }
0073             *name = n;
0074             return handler;
0075         }
0076     }
0077     return ERR_PTR(-EOPNOTSUPP);
0078 }
0079 
0080 /*
0081  * Check permissions for extended attribute access.  This is a bit complicated
0082  * because different namespaces have very different rules.
0083  */
0084 static int
0085 xattr_permission(struct inode *inode, const char *name, int mask)
0086 {
0087     /*
0088      * We can never set or remove an extended attribute on a read-only
0089      * filesystem  or on an immutable / append-only inode.
0090      */
0091     if (mask & MAY_WRITE) {
0092         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
0093             return -EPERM;
0094         /*
0095          * Updating an xattr will likely cause i_uid and i_gid
0096          * to be writen back improperly if their true value is
0097          * unknown to the vfs.
0098          */
0099         if (HAS_UNMAPPED_ID(inode))
0100             return -EPERM;
0101     }
0102 
0103     /*
0104      * No restriction for security.* and system.* from the VFS.  Decision
0105      * on these is left to the underlying filesystem / security module.
0106      */
0107     if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
0108         !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
0109         return 0;
0110 
0111     /*
0112      * The trusted.* namespace can only be accessed by privileged users.
0113      */
0114     if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
0115         if (!capable(CAP_SYS_ADMIN))
0116             return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
0117         return 0;
0118     }
0119 
0120     /*
0121      * In the user.* namespace, only regular files and directories can have
0122      * extended attributes. For sticky directories, only the owner and
0123      * privileged users can write attributes.
0124      */
0125     if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
0126         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
0127             return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
0128         if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
0129             (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
0130             return -EPERM;
0131     }
0132 
0133     return inode_permission(inode, mask);
0134 }
0135 
0136 int
0137 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
0138            const void *value, size_t size, int flags)
0139 {
0140     const struct xattr_handler *handler;
0141 
0142     handler = xattr_resolve_name(inode, &name);
0143     if (IS_ERR(handler))
0144         return PTR_ERR(handler);
0145     if (!handler->set)
0146         return -EOPNOTSUPP;
0147     if (size == 0)
0148         value = "";  /* empty EA, do not remove */
0149     return handler->set(handler, dentry, inode, name, value, size, flags);
0150 }
0151 EXPORT_SYMBOL(__vfs_setxattr);
0152 
0153 /**
0154  *  __vfs_setxattr_noperm - perform setxattr operation without performing
0155  *  permission checks.
0156  *
0157  *  @dentry - object to perform setxattr on
0158  *  @name - xattr name to set
0159  *  @value - value to set @name to
0160  *  @size - size of @value
0161  *  @flags - flags to pass into filesystem operations
0162  *
0163  *  returns the result of the internal setxattr or setsecurity operations.
0164  *
0165  *  This function requires the caller to lock the inode's i_mutex before it
0166  *  is executed. It also assumes that the caller will make the appropriate
0167  *  permission checks.
0168  */
0169 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
0170         const void *value, size_t size, int flags)
0171 {
0172     struct inode *inode = dentry->d_inode;
0173     int error = -EAGAIN;
0174     int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
0175                    XATTR_SECURITY_PREFIX_LEN);
0176 
0177     if (issec)
0178         inode->i_flags &= ~S_NOSEC;
0179     if (inode->i_opflags & IOP_XATTR) {
0180         error = __vfs_setxattr(dentry, inode, name, value, size, flags);
0181         if (!error) {
0182             fsnotify_xattr(dentry);
0183             security_inode_post_setxattr(dentry, name, value,
0184                              size, flags);
0185         }
0186     } else {
0187         if (unlikely(is_bad_inode(inode)))
0188             return -EIO;
0189     }
0190     if (error == -EAGAIN) {
0191         error = -EOPNOTSUPP;
0192 
0193         if (issec) {
0194             const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
0195 
0196             error = security_inode_setsecurity(inode, suffix, value,
0197                                size, flags);
0198             if (!error)
0199                 fsnotify_xattr(dentry);
0200         }
0201     }
0202 
0203     return error;
0204 }
0205 
0206 
0207 int
0208 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
0209         size_t size, int flags)
0210 {
0211     struct inode *inode = dentry->d_inode;
0212     int error;
0213 
0214     error = xattr_permission(inode, name, MAY_WRITE);
0215     if (error)
0216         return error;
0217 
0218     inode_lock(inode);
0219     error = security_inode_setxattr(dentry, name, value, size, flags);
0220     if (error)
0221         goto out;
0222 
0223     error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
0224 
0225 out:
0226     inode_unlock(inode);
0227     return error;
0228 }
0229 EXPORT_SYMBOL_GPL(vfs_setxattr);
0230 
0231 ssize_t
0232 xattr_getsecurity(struct inode *inode, const char *name, void *value,
0233             size_t size)
0234 {
0235     void *buffer = NULL;
0236     ssize_t len;
0237 
0238     if (!value || !size) {
0239         len = security_inode_getsecurity(inode, name, &buffer, false);
0240         goto out_noalloc;
0241     }
0242 
0243     len = security_inode_getsecurity(inode, name, &buffer, true);
0244     if (len < 0)
0245         return len;
0246     if (size < len) {
0247         len = -ERANGE;
0248         goto out;
0249     }
0250     memcpy(value, buffer, len);
0251 out:
0252     security_release_secctx(buffer, len);
0253 out_noalloc:
0254     return len;
0255 }
0256 EXPORT_SYMBOL_GPL(xattr_getsecurity);
0257 
0258 /*
0259  * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
0260  *
0261  * Allocate memory, if not already allocated, or re-allocate correct size,
0262  * before retrieving the extended attribute.
0263  *
0264  * Returns the result of alloc, if failed, or the getxattr operation.
0265  */
0266 ssize_t
0267 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
0268            size_t xattr_size, gfp_t flags)
0269 {
0270     const struct xattr_handler *handler;
0271     struct inode *inode = dentry->d_inode;
0272     char *value = *xattr_value;
0273     int error;
0274 
0275     error = xattr_permission(inode, name, MAY_READ);
0276     if (error)
0277         return error;
0278 
0279     handler = xattr_resolve_name(inode, &name);
0280     if (IS_ERR(handler))
0281         return PTR_ERR(handler);
0282     if (!handler->get)
0283         return -EOPNOTSUPP;
0284     error = handler->get(handler, dentry, inode, name, NULL, 0);
0285     if (error < 0)
0286         return error;
0287 
0288     if (!value || (error > xattr_size)) {
0289         value = krealloc(*xattr_value, error + 1, flags);
0290         if (!value)
0291             return -ENOMEM;
0292         memset(value, 0, error + 1);
0293     }
0294 
0295     error = handler->get(handler, dentry, inode, name, value, error);
0296     *xattr_value = value;
0297     return error;
0298 }
0299 
0300 ssize_t
0301 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
0302            void *value, size_t size)
0303 {
0304     const struct xattr_handler *handler;
0305 
0306     handler = xattr_resolve_name(inode, &name);
0307     if (IS_ERR(handler))
0308         return PTR_ERR(handler);
0309     if (!handler->get)
0310         return -EOPNOTSUPP;
0311     return handler->get(handler, dentry, inode, name, value, size);
0312 }
0313 EXPORT_SYMBOL(__vfs_getxattr);
0314 
0315 ssize_t
0316 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
0317 {
0318     struct inode *inode = dentry->d_inode;
0319     int error;
0320 
0321     error = xattr_permission(inode, name, MAY_READ);
0322     if (error)
0323         return error;
0324 
0325     error = security_inode_getxattr(dentry, name);
0326     if (error)
0327         return error;
0328 
0329     if (!strncmp(name, XATTR_SECURITY_PREFIX,
0330                 XATTR_SECURITY_PREFIX_LEN)) {
0331         const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
0332         int ret = xattr_getsecurity(inode, suffix, value, size);
0333         /*
0334          * Only overwrite the return value if a security module
0335          * is actually active.
0336          */
0337         if (ret == -EOPNOTSUPP)
0338             goto nolsm;
0339         return ret;
0340     }
0341 nolsm:
0342     return __vfs_getxattr(dentry, inode, name, value, size);
0343 }
0344 EXPORT_SYMBOL_GPL(vfs_getxattr);
0345 
0346 ssize_t
0347 vfs_listxattr(struct dentry *dentry, char *list, size_t size)
0348 {
0349     struct inode *inode = d_inode(dentry);
0350     ssize_t error;
0351 
0352     error = security_inode_listxattr(dentry);
0353     if (error)
0354         return error;
0355     if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
0356         error = -EOPNOTSUPP;
0357         error = inode->i_op->listxattr(dentry, list, size);
0358     } else {
0359         error = security_inode_listsecurity(inode, list, size);
0360         if (size && error > size)
0361             error = -ERANGE;
0362     }
0363     return error;
0364 }
0365 EXPORT_SYMBOL_GPL(vfs_listxattr);
0366 
0367 int
0368 __vfs_removexattr(struct dentry *dentry, const char *name)
0369 {
0370     struct inode *inode = d_inode(dentry);
0371     const struct xattr_handler *handler;
0372 
0373     handler = xattr_resolve_name(inode, &name);
0374     if (IS_ERR(handler))
0375         return PTR_ERR(handler);
0376     if (!handler->set)
0377         return -EOPNOTSUPP;
0378     return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
0379 }
0380 EXPORT_SYMBOL(__vfs_removexattr);
0381 
0382 int
0383 vfs_removexattr(struct dentry *dentry, const char *name)
0384 {
0385     struct inode *inode = dentry->d_inode;
0386     int error;
0387 
0388     error = xattr_permission(inode, name, MAY_WRITE);
0389     if (error)
0390         return error;
0391 
0392     inode_lock(inode);
0393     error = security_inode_removexattr(dentry, name);
0394     if (error)
0395         goto out;
0396 
0397     error = __vfs_removexattr(dentry, name);
0398 
0399     if (!error) {
0400         fsnotify_xattr(dentry);
0401         evm_inode_post_removexattr(dentry, name);
0402     }
0403 
0404 out:
0405     inode_unlock(inode);
0406     return error;
0407 }
0408 EXPORT_SYMBOL_GPL(vfs_removexattr);
0409 
0410 
0411 /*
0412  * Extended attribute SET operations
0413  */
0414 static long
0415 setxattr(struct dentry *d, const char __user *name, const void __user *value,
0416      size_t size, int flags)
0417 {
0418     int error;
0419     void *kvalue = NULL;
0420     char kname[XATTR_NAME_MAX + 1];
0421 
0422     if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
0423         return -EINVAL;
0424 
0425     error = strncpy_from_user(kname, name, sizeof(kname));
0426     if (error == 0 || error == sizeof(kname))
0427         error = -ERANGE;
0428     if (error < 0)
0429         return error;
0430 
0431     if (size) {
0432         if (size > XATTR_SIZE_MAX)
0433             return -E2BIG;
0434         kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
0435         if (!kvalue) {
0436             kvalue = vmalloc(size);
0437             if (!kvalue)
0438                 return -ENOMEM;
0439         }
0440         if (copy_from_user(kvalue, value, size)) {
0441             error = -EFAULT;
0442             goto out;
0443         }
0444         if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
0445             (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
0446             posix_acl_fix_xattr_from_user(kvalue, size);
0447     }
0448 
0449     error = vfs_setxattr(d, kname, kvalue, size, flags);
0450 out:
0451     kvfree(kvalue);
0452 
0453     return error;
0454 }
0455 
0456 static int path_setxattr(const char __user *pathname,
0457              const char __user *name, const void __user *value,
0458              size_t size, int flags, unsigned int lookup_flags)
0459 {
0460     struct path path;
0461     int error;
0462 retry:
0463     error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
0464     if (error)
0465         return error;
0466     error = mnt_want_write(path.mnt);
0467     if (!error) {
0468         error = setxattr(path.dentry, name, value, size, flags);
0469         mnt_drop_write(path.mnt);
0470     }
0471     path_put(&path);
0472     if (retry_estale(error, lookup_flags)) {
0473         lookup_flags |= LOOKUP_REVAL;
0474         goto retry;
0475     }
0476     return error;
0477 }
0478 
0479 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
0480         const char __user *, name, const void __user *, value,
0481         size_t, size, int, flags)
0482 {
0483     return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
0484 }
0485 
0486 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
0487         const char __user *, name, const void __user *, value,
0488         size_t, size, int, flags)
0489 {
0490     return path_setxattr(pathname, name, value, size, flags, 0);
0491 }
0492 
0493 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
0494         const void __user *,value, size_t, size, int, flags)
0495 {
0496     struct fd f = fdget(fd);
0497     int error = -EBADF;
0498 
0499     if (!f.file)
0500         return error;
0501     audit_file(f.file);
0502     error = mnt_want_write_file(f.file);
0503     if (!error) {
0504         error = setxattr(f.file->f_path.dentry, name, value, size, flags);
0505         mnt_drop_write_file(f.file);
0506     }
0507     fdput(f);
0508     return error;
0509 }
0510 
0511 /*
0512  * Extended attribute GET operations
0513  */
0514 static ssize_t
0515 getxattr(struct dentry *d, const char __user *name, void __user *value,
0516      size_t size)
0517 {
0518     ssize_t error;
0519     void *kvalue = NULL;
0520     char kname[XATTR_NAME_MAX + 1];
0521 
0522     error = strncpy_from_user(kname, name, sizeof(kname));
0523     if (error == 0 || error == sizeof(kname))
0524         error = -ERANGE;
0525     if (error < 0)
0526         return error;
0527 
0528     if (size) {
0529         if (size > XATTR_SIZE_MAX)
0530             size = XATTR_SIZE_MAX;
0531         kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
0532         if (!kvalue) {
0533             kvalue = vmalloc(size);
0534             if (!kvalue)
0535                 return -ENOMEM;
0536         }
0537     }
0538 
0539     error = vfs_getxattr(d, kname, kvalue, size);
0540     if (error > 0) {
0541         if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
0542             (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
0543             posix_acl_fix_xattr_to_user(kvalue, size);
0544         if (size && copy_to_user(value, kvalue, error))
0545             error = -EFAULT;
0546     } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
0547         /* The file system tried to returned a value bigger
0548            than XATTR_SIZE_MAX bytes. Not possible. */
0549         error = -E2BIG;
0550     }
0551 
0552     kvfree(kvalue);
0553 
0554     return error;
0555 }
0556 
0557 static ssize_t path_getxattr(const char __user *pathname,
0558                  const char __user *name, void __user *value,
0559                  size_t size, unsigned int lookup_flags)
0560 {
0561     struct path path;
0562     ssize_t error;
0563 retry:
0564     error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
0565     if (error)
0566         return error;
0567     error = getxattr(path.dentry, name, value, size);
0568     path_put(&path);
0569     if (retry_estale(error, lookup_flags)) {
0570         lookup_flags |= LOOKUP_REVAL;
0571         goto retry;
0572     }
0573     return error;
0574 }
0575 
0576 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
0577         const char __user *, name, void __user *, value, size_t, size)
0578 {
0579     return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
0580 }
0581 
0582 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
0583         const char __user *, name, void __user *, value, size_t, size)
0584 {
0585     return path_getxattr(pathname, name, value, size, 0);
0586 }
0587 
0588 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
0589         void __user *, value, size_t, size)
0590 {
0591     struct fd f = fdget(fd);
0592     ssize_t error = -EBADF;
0593 
0594     if (!f.file)
0595         return error;
0596     audit_file(f.file);
0597     error = getxattr(f.file->f_path.dentry, name, value, size);
0598     fdput(f);
0599     return error;
0600 }
0601 
0602 /*
0603  * Extended attribute LIST operations
0604  */
0605 static ssize_t
0606 listxattr(struct dentry *d, char __user *list, size_t size)
0607 {
0608     ssize_t error;
0609     char *klist = NULL;
0610 
0611     if (size) {
0612         if (size > XATTR_LIST_MAX)
0613             size = XATTR_LIST_MAX;
0614         klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
0615         if (!klist) {
0616             klist = vmalloc(size);
0617             if (!klist)
0618                 return -ENOMEM;
0619         }
0620     }
0621 
0622     error = vfs_listxattr(d, klist, size);
0623     if (error > 0) {
0624         if (size && copy_to_user(list, klist, error))
0625             error = -EFAULT;
0626     } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
0627         /* The file system tried to returned a list bigger
0628            than XATTR_LIST_MAX bytes. Not possible. */
0629         error = -E2BIG;
0630     }
0631 
0632     kvfree(klist);
0633 
0634     return error;
0635 }
0636 
0637 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
0638                   size_t size, unsigned int lookup_flags)
0639 {
0640     struct path path;
0641     ssize_t error;
0642 retry:
0643     error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
0644     if (error)
0645         return error;
0646     error = listxattr(path.dentry, list, size);
0647     path_put(&path);
0648     if (retry_estale(error, lookup_flags)) {
0649         lookup_flags |= LOOKUP_REVAL;
0650         goto retry;
0651     }
0652     return error;
0653 }
0654 
0655 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
0656         size_t, size)
0657 {
0658     return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
0659 }
0660 
0661 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
0662         size_t, size)
0663 {
0664     return path_listxattr(pathname, list, size, 0);
0665 }
0666 
0667 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
0668 {
0669     struct fd f = fdget(fd);
0670     ssize_t error = -EBADF;
0671 
0672     if (!f.file)
0673         return error;
0674     audit_file(f.file);
0675     error = listxattr(f.file->f_path.dentry, list, size);
0676     fdput(f);
0677     return error;
0678 }
0679 
0680 /*
0681  * Extended attribute REMOVE operations
0682  */
0683 static long
0684 removexattr(struct dentry *d, const char __user *name)
0685 {
0686     int error;
0687     char kname[XATTR_NAME_MAX + 1];
0688 
0689     error = strncpy_from_user(kname, name, sizeof(kname));
0690     if (error == 0 || error == sizeof(kname))
0691         error = -ERANGE;
0692     if (error < 0)
0693         return error;
0694 
0695     return vfs_removexattr(d, kname);
0696 }
0697 
0698 static int path_removexattr(const char __user *pathname,
0699                 const char __user *name, unsigned int lookup_flags)
0700 {
0701     struct path path;
0702     int error;
0703 retry:
0704     error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
0705     if (error)
0706         return error;
0707     error = mnt_want_write(path.mnt);
0708     if (!error) {
0709         error = removexattr(path.dentry, name);
0710         mnt_drop_write(path.mnt);
0711     }
0712     path_put(&path);
0713     if (retry_estale(error, lookup_flags)) {
0714         lookup_flags |= LOOKUP_REVAL;
0715         goto retry;
0716     }
0717     return error;
0718 }
0719 
0720 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
0721         const char __user *, name)
0722 {
0723     return path_removexattr(pathname, name, LOOKUP_FOLLOW);
0724 }
0725 
0726 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
0727         const char __user *, name)
0728 {
0729     return path_removexattr(pathname, name, 0);
0730 }
0731 
0732 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
0733 {
0734     struct fd f = fdget(fd);
0735     int error = -EBADF;
0736 
0737     if (!f.file)
0738         return error;
0739     audit_file(f.file);
0740     error = mnt_want_write_file(f.file);
0741     if (!error) {
0742         error = removexattr(f.file->f_path.dentry, name);
0743         mnt_drop_write_file(f.file);
0744     }
0745     fdput(f);
0746     return error;
0747 }
0748 
0749 /*
0750  * Combine the results of the list() operation from every xattr_handler in the
0751  * list.
0752  */
0753 ssize_t
0754 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
0755 {
0756     const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
0757     unsigned int size = 0;
0758 
0759     if (!buffer) {
0760         for_each_xattr_handler(handlers, handler) {
0761             if (!handler->name ||
0762                 (handler->list && !handler->list(dentry)))
0763                 continue;
0764             size += strlen(handler->name) + 1;
0765         }
0766     } else {
0767         char *buf = buffer;
0768         size_t len;
0769 
0770         for_each_xattr_handler(handlers, handler) {
0771             if (!handler->name ||
0772                 (handler->list && !handler->list(dentry)))
0773                 continue;
0774             len = strlen(handler->name);
0775             if (len + 1 > buffer_size)
0776                 return -ERANGE;
0777             memcpy(buf, handler->name, len + 1);
0778             buf += len + 1;
0779             buffer_size -= len + 1;
0780         }
0781         size = buf - buffer;
0782     }
0783     return size;
0784 }
0785 EXPORT_SYMBOL(generic_listxattr);
0786 
0787 /**
0788  * xattr_full_name  -  Compute full attribute name from suffix
0789  *
0790  * @handler:    handler of the xattr_handler operation
0791  * @name:   name passed to the xattr_handler operation
0792  *
0793  * The get and set xattr handler operations are called with the remainder of
0794  * the attribute name after skipping the handler's prefix: for example, "foo"
0795  * is passed to the get operation of a handler with prefix "user." to get
0796  * attribute "user.foo".  The full name is still "there" in the name though.
0797  *
0798  * Note: the list xattr handler operation when called from the vfs is passed a
0799  * NULL name; some file systems use this operation internally, with varying
0800  * semantics.
0801  */
0802 const char *xattr_full_name(const struct xattr_handler *handler,
0803                 const char *name)
0804 {
0805     size_t prefix_len = strlen(xattr_prefix(handler));
0806 
0807     return name - prefix_len;
0808 }
0809 EXPORT_SYMBOL(xattr_full_name);
0810 
0811 /*
0812  * Allocate new xattr and copy in the value; but leave the name to callers.
0813  */
0814 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
0815 {
0816     struct simple_xattr *new_xattr;
0817     size_t len;
0818 
0819     /* wrap around? */
0820     len = sizeof(*new_xattr) + size;
0821     if (len < sizeof(*new_xattr))
0822         return NULL;
0823 
0824     new_xattr = kmalloc(len, GFP_KERNEL);
0825     if (!new_xattr)
0826         return NULL;
0827 
0828     new_xattr->size = size;
0829     memcpy(new_xattr->value, value, size);
0830     return new_xattr;
0831 }
0832 
0833 /*
0834  * xattr GET operation for in-memory/pseudo filesystems
0835  */
0836 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
0837              void *buffer, size_t size)
0838 {
0839     struct simple_xattr *xattr;
0840     int ret = -ENODATA;
0841 
0842     spin_lock(&xattrs->lock);
0843     list_for_each_entry(xattr, &xattrs->head, list) {
0844         if (strcmp(name, xattr->name))
0845             continue;
0846 
0847         ret = xattr->size;
0848         if (buffer) {
0849             if (size < xattr->size)
0850                 ret = -ERANGE;
0851             else
0852                 memcpy(buffer, xattr->value, xattr->size);
0853         }
0854         break;
0855     }
0856     spin_unlock(&xattrs->lock);
0857     return ret;
0858 }
0859 
0860 /**
0861  * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
0862  * @xattrs: target simple_xattr list
0863  * @name: name of the extended attribute
0864  * @value: value of the xattr. If %NULL, will remove the attribute.
0865  * @size: size of the new xattr
0866  * @flags: %XATTR_{CREATE|REPLACE}
0867  *
0868  * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
0869  * with -EEXIST.  If %XATTR_REPLACE is set, the xattr should exist;
0870  * otherwise, fails with -ENODATA.
0871  *
0872  * Returns 0 on success, -errno on failure.
0873  */
0874 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
0875              const void *value, size_t size, int flags)
0876 {
0877     struct simple_xattr *xattr;
0878     struct simple_xattr *new_xattr = NULL;
0879     int err = 0;
0880 
0881     /* value == NULL means remove */
0882     if (value) {
0883         new_xattr = simple_xattr_alloc(value, size);
0884         if (!new_xattr)
0885             return -ENOMEM;
0886 
0887         new_xattr->name = kstrdup(name, GFP_KERNEL);
0888         if (!new_xattr->name) {
0889             kfree(new_xattr);
0890             return -ENOMEM;
0891         }
0892     }
0893 
0894     spin_lock(&xattrs->lock);
0895     list_for_each_entry(xattr, &xattrs->head, list) {
0896         if (!strcmp(name, xattr->name)) {
0897             if (flags & XATTR_CREATE) {
0898                 xattr = new_xattr;
0899                 err = -EEXIST;
0900             } else if (new_xattr) {
0901                 list_replace(&xattr->list, &new_xattr->list);
0902             } else {
0903                 list_del(&xattr->list);
0904             }
0905             goto out;
0906         }
0907     }
0908     if (flags & XATTR_REPLACE) {
0909         xattr = new_xattr;
0910         err = -ENODATA;
0911     } else {
0912         list_add(&new_xattr->list, &xattrs->head);
0913         xattr = NULL;
0914     }
0915 out:
0916     spin_unlock(&xattrs->lock);
0917     if (xattr) {
0918         kfree(xattr->name);
0919         kfree(xattr);
0920     }
0921     return err;
0922 
0923 }
0924 
0925 static bool xattr_is_trusted(const char *name)
0926 {
0927     return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
0928 }
0929 
0930 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
0931               const char *name)
0932 {
0933     size_t len = strlen(name) + 1;
0934     if (*buffer) {
0935         if (*remaining_size < len)
0936             return -ERANGE;
0937         memcpy(*buffer, name, len);
0938         *buffer += len;
0939     }
0940     *remaining_size -= len;
0941     return 0;
0942 }
0943 
0944 /*
0945  * xattr LIST operation for in-memory/pseudo filesystems
0946  */
0947 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
0948               char *buffer, size_t size)
0949 {
0950     bool trusted = capable(CAP_SYS_ADMIN);
0951     struct simple_xattr *xattr;
0952     ssize_t remaining_size = size;
0953     int err = 0;
0954 
0955 #ifdef CONFIG_FS_POSIX_ACL
0956     if (inode->i_acl) {
0957         err = xattr_list_one(&buffer, &remaining_size,
0958                      XATTR_NAME_POSIX_ACL_ACCESS);
0959         if (err)
0960             return err;
0961     }
0962     if (inode->i_default_acl) {
0963         err = xattr_list_one(&buffer, &remaining_size,
0964                      XATTR_NAME_POSIX_ACL_DEFAULT);
0965         if (err)
0966             return err;
0967     }
0968 #endif
0969 
0970     spin_lock(&xattrs->lock);
0971     list_for_each_entry(xattr, &xattrs->head, list) {
0972         /* skip "trusted." attributes for unprivileged callers */
0973         if (!trusted && xattr_is_trusted(xattr->name))
0974             continue;
0975 
0976         err = xattr_list_one(&buffer, &remaining_size, xattr->name);
0977         if (err)
0978             break;
0979     }
0980     spin_unlock(&xattrs->lock);
0981 
0982     return err ? err : size - remaining_size;
0983 }
0984 
0985 /*
0986  * Adds an extended attribute to the list
0987  */
0988 void simple_xattr_list_add(struct simple_xattrs *xattrs,
0989                struct simple_xattr *new_xattr)
0990 {
0991     spin_lock(&xattrs->lock);
0992     list_add(&new_xattr->list, &xattrs->head);
0993     spin_unlock(&xattrs->lock);
0994 }