[PATCH] knfsd: Allow lockd to drop replies as appropriate
[pandora-kernel.git] / fs / xattr.c
index bcc2156..3956351 100644 (file)
 #include <linux/syscalls.h>
 #include <linux/module.h>
 #include <linux/fsnotify.h>
+#include <linux/audit.h>
 #include <asm/uaccess.h>
 
+
+/*
+ * Check permissions for extended attribute access.  This is a bit complicated
+ * because different namespaces have very different rules.
+ */
+static int
+xattr_permission(struct inode *inode, const char *name, int mask)
+{
+       /*
+        * We can never set or remove an extended attribute on a read-only
+        * filesystem  or on an immutable / append-only inode.
+        */
+       if (mask & MAY_WRITE) {
+               if (IS_RDONLY(inode))
+                       return -EROFS;
+               if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
+                       return -EPERM;
+       }
+
+       /*
+        * No restriction for security.* and system.* from the VFS.  Decision
+        * on these is left to the underlying filesystem / security module.
+        */
+       if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
+           !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+               return 0;
+
+       /*
+        * The trusted.* namespace can only accessed by a privilegued user.
+        */
+       if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
+               return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
+
+       if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
+               if (!S_ISREG(inode->i_mode) &&
+                   (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
+                       return -EPERM;
+       }
+
+       return permission(inode, mask, NULL);
+}
+
+int
+vfs_setxattr(struct dentry *dentry, char *name, void *value,
+               size_t size, int flags)
+{
+       struct inode *inode = dentry->d_inode;
+       int error;
+
+       error = xattr_permission(inode, name, MAY_WRITE);
+       if (error)
+               return error;
+
+       mutex_lock(&inode->i_mutex);
+       error = security_inode_setxattr(dentry, name, value, size, flags);
+       if (error)
+               goto out;
+       error = -EOPNOTSUPP;
+       if (inode->i_op->setxattr) {
+               error = inode->i_op->setxattr(dentry, name, value, size, flags);
+               if (!error) {
+                       fsnotify_xattr(dentry);
+                       security_inode_post_setxattr(dentry, name, value,
+                                                    size, flags);
+               }
+       } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
+                               XATTR_SECURITY_PREFIX_LEN)) {
+               const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
+               error = security_inode_setsecurity(inode, suffix, value,
+                                                  size, flags);
+               if (!error)
+                       fsnotify_xattr(dentry);
+       }
+out:
+       mutex_unlock(&inode->i_mutex);
+       return error;
+}
+EXPORT_SYMBOL_GPL(vfs_setxattr);
+
+ssize_t
+vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
+{
+       struct inode *inode = dentry->d_inode;
+       int error;
+
+       error = xattr_permission(inode, name, MAY_READ);
+       if (error)
+               return error;
+
+       error = security_inode_getxattr(dentry, name);
+       if (error)
+               return error;
+
+       if (inode->i_op->getxattr)
+               error = inode->i_op->getxattr(dentry, name, value, size);
+       else
+               error = -EOPNOTSUPP;
+
+       if (!strncmp(name, XATTR_SECURITY_PREFIX,
+                               XATTR_SECURITY_PREFIX_LEN)) {
+               const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
+               int ret = security_inode_getsecurity(inode, suffix, value,
+                                                    size, error);
+               /*
+                * Only overwrite the return value if a security module
+                * is actually active.
+                */
+               if (ret != -EOPNOTSUPP)
+                       error = ret;
+       }
+
+       return error;
+}
+EXPORT_SYMBOL_GPL(vfs_getxattr);
+
+ssize_t
+vfs_listxattr(struct dentry *d, char *list, size_t size)
+{
+       ssize_t error;
+
+       error = security_inode_listxattr(d);
+       if (error)
+               return error;
+       error = -EOPNOTSUPP;
+       if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
+               error = d->d_inode->i_op->listxattr(d, list, size);
+       } else {
+               error = security_inode_listsecurity(d->d_inode, list, size);
+               if (size && error > size)
+                       error = -ERANGE;
+       }
+       return error;
+}
+EXPORT_SYMBOL_GPL(vfs_listxattr);
+
+int
+vfs_removexattr(struct dentry *dentry, char *name)
+{
+       struct inode *inode = dentry->d_inode;
+       int error;
+
+       if (!inode->i_op->removexattr)
+               return -EOPNOTSUPP;
+
+       error = xattr_permission(inode, name, MAY_WRITE);
+       if (error)
+               return error;
+
+       error = security_inode_removexattr(dentry, name);
+       if (error)
+               return error;
+
+       mutex_lock(&inode->i_mutex);
+       error = inode->i_op->removexattr(dentry, name);
+       mutex_unlock(&inode->i_mutex);
+
+       if (!error)
+               fsnotify_xattr(dentry);
+       return error;
+}
+EXPORT_SYMBOL_GPL(vfs_removexattr);
+
+
 /*
  * Extended attribute SET operations
  */
@@ -51,29 +215,7 @@ setxattr(struct dentry *d, char __user *name, void __user *value,
                }
        }
 
-       down(&d->d_inode->i_sem);
-       error = security_inode_setxattr(d, kname, kvalue, size, flags);
-       if (error)
-               goto out;
-       error = -EOPNOTSUPP;
-       if (d->d_inode->i_op && d->d_inode->i_op->setxattr) {
-               error = d->d_inode->i_op->setxattr(d, kname, kvalue,
-                                                  size, flags);
-               if (!error) {
-                       fsnotify_xattr(d);
-                       security_inode_post_setxattr(d, kname, kvalue,
-                                                    size, flags);
-               }
-       } else if (!strncmp(kname, XATTR_SECURITY_PREFIX,
-                           sizeof XATTR_SECURITY_PREFIX - 1)) {
-               const char *suffix = kname + sizeof XATTR_SECURITY_PREFIX - 1;
-               error = security_inode_setsecurity(d->d_inode, suffix, kvalue,
-                                                  size, flags);
-               if (!error)
-                       fsnotify_xattr(d);
-       }
-out:
-       up(&d->d_inode->i_sem);
+       error = vfs_setxattr(d, kname, kvalue, size, flags);
        kfree(kvalue);
        return error;
 }
@@ -113,12 +255,15 @@ sys_fsetxattr(int fd, char __user *name, void __user *value,
              size_t size, int flags)
 {
        struct file *f;
+       struct dentry *dentry;
        int error = -EBADF;
 
        f = fget(fd);
        if (!f)
                return error;
-       error = setxattr(f->f_dentry, name, value, size, flags);
+       dentry = f->f_dentry;
+       audit_inode(NULL, dentry->d_inode);
+       error = setxattr(dentry, name, value, size, flags);
        fput(f);
        return error;
 }
@@ -147,22 +292,7 @@ getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
                        return -ENOMEM;
        }
 
-       error = security_inode_getxattr(d, kname);
-       if (error)
-               goto out;
-       error = -EOPNOTSUPP;
-       if (d->d_inode->i_op && d->d_inode->i_op->getxattr)
-               error = d->d_inode->i_op->getxattr(d, kname, kvalue, size);
-
-       if (!strncmp(kname, XATTR_SECURITY_PREFIX,
-                    sizeof XATTR_SECURITY_PREFIX - 1)) {
-               const char *suffix = kname + sizeof XATTR_SECURITY_PREFIX - 1;
-               int rv = security_inode_getsecurity(d->d_inode, suffix, kvalue,
-                                                   size, error);
-               /* Security module active: overwrite error value */
-               if (rv != -EOPNOTSUPP)
-                       error = rv;
-       }
+       error = vfs_getxattr(d, kname, kvalue, size);
        if (error > 0) {
                if (size && copy_to_user(value, kvalue, error))
                        error = -EFAULT;
@@ -171,7 +301,6 @@ getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
                   than XATTR_SIZE_MAX bytes. Not possible. */
                error = -E2BIG;
        }
-out:
        kfree(kvalue);
        return error;
 }
@@ -237,17 +366,7 @@ listxattr(struct dentry *d, char __user *list, size_t size)
                        return -ENOMEM;
        }
 
-       error = security_inode_listxattr(d);
-       if (error)
-               goto out;
-       error = -EOPNOTSUPP;
-       if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
-               error = d->d_inode->i_op->listxattr(d, klist, size);
-       } else {
-               error = security_inode_listsecurity(d->d_inode, klist, size);
-               if (size && error > size)
-                       error = -ERANGE;
-       }
+       error = vfs_listxattr(d, klist, size);
        if (error > 0) {
                if (size && copy_to_user(list, klist, error))
                        error = -EFAULT;
@@ -256,7 +375,6 @@ listxattr(struct dentry *d, char __user *list, size_t size)
                   than XATTR_LIST_MAX bytes. Not possible. */
                error = -E2BIG;
        }
-out:
        kfree(klist);
        return error;
 }
@@ -318,19 +436,7 @@ removexattr(struct dentry *d, char __user *name)
        if (error < 0)
                return error;
 
-       error = -EOPNOTSUPP;
-       if (d->d_inode->i_op && d->d_inode->i_op->removexattr) {
-               error = security_inode_removexattr(d, kname);
-               if (error)
-                       goto out;
-               down(&d->d_inode->i_sem);
-               error = d->d_inode->i_op->removexattr(d, kname);
-               up(&d->d_inode->i_sem);
-               if (!error)
-                       fsnotify_xattr(d);
-       }
-out:
-       return error;
+       return vfs_removexattr(d, kname);
 }
 
 asmlinkage long
@@ -365,12 +471,15 @@ asmlinkage long
 sys_fremovexattr(int fd, char __user *name)
 {
        struct file *f;
+       struct dentry *dentry;
        int error = -EBADF;
 
        f = fget(fd);
        if (!f)
                return error;
-       error = removexattr(f->f_dentry, name);
+       dentry = f->f_dentry;
+       audit_inode(NULL, dentry->d_inode);
+       error = removexattr(dentry, name);
        fput(f);
        return error;
 }