OSDN Git Service

fs: Move notify_change permission checks into may_setattr
authorAndreas Gruenbacher <agruenba@redhat.com>
Wed, 28 Jul 2021 12:47:33 +0000 (07:47 -0500)
committerAl Viro <viro@zeniv.linux.org.uk>
Fri, 13 Aug 2021 04:41:05 +0000 (00:41 -0400)
Move the permission checks in notify_change into a separate function to
make them available to filesystems.

When notify_change is called, the vfs performs those checks before
calling into iop->setattr.  However, a filesystem like gfs2 can only
lock and revalidate the inode inside ->setattr, and it must then repeat
those checks to err on the safe side.

It would be nice to get rid of the double checking, but moving the
permission check into iop->setattr altogether isn't really an option.

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Bob Peterson <rpeterso@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
fs/attr.c
include/linux/fs.h

index 87ef39d..473d21b 100644 (file)
--- a/fs/attr.c
+++ b/fs/attr.c
@@ -249,6 +249,34 @@ void setattr_copy(struct user_namespace *mnt_userns, struct inode *inode,
 }
 EXPORT_SYMBOL(setattr_copy);
 
+int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
+               unsigned int ia_valid)
+{
+       int error;
+
+       if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
+               if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
+                       return -EPERM;
+       }
+
+       /*
+        * If utimes(2) and friends are called with times == NULL (or both
+        * times are UTIME_NOW), then we need to check for write permission
+        */
+       if (ia_valid & ATTR_TOUCH) {
+               if (IS_IMMUTABLE(inode))
+                       return -EPERM;
+
+               if (!inode_owner_or_capable(mnt_userns, inode)) {
+                       error = inode_permission(mnt_userns, inode, MAY_WRITE);
+                       if (error)
+                               return error;
+               }
+       }
+       return 0;
+}
+EXPORT_SYMBOL(may_setattr);
+
 /**
  * notify_change - modify attributes of a filesytem object
  * @mnt_userns:        user namespace of the mount the inode was found from
@@ -290,25 +318,9 @@ int notify_change(struct user_namespace *mnt_userns, struct dentry *dentry,
 
        WARN_ON_ONCE(!inode_is_locked(inode));
 
-       if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
-               if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-                       return -EPERM;
-       }
-
-       /*
-        * If utimes(2) and friends are called with times == NULL (or both
-        * times are UTIME_NOW), then we need to check for write permission
-        */
-       if (ia_valid & ATTR_TOUCH) {
-               if (IS_IMMUTABLE(inode))
-                       return -EPERM;
-
-               if (!inode_owner_or_capable(mnt_userns, inode)) {
-                       error = inode_permission(mnt_userns, inode, MAY_WRITE);
-                       if (error)
-                               return error;
-               }
-       }
+       error = may_setattr(mnt_userns, inode, ia_valid);
+       if (error)
+               return error;
 
        if ((ia_valid & ATTR_MODE)) {
                umode_t amode = attr->ia_mode;
index 6405742..5019296 100644 (file)
@@ -3469,6 +3469,8 @@ extern int buffer_migrate_page_norefs(struct address_space *,
 #define buffer_migrate_page_norefs NULL
 #endif
 
+int may_setattr(struct user_namespace *mnt_userns, struct inode *inode,
+               unsigned int ia_valid);
 int setattr_prepare(struct user_namespace *, struct dentry *, struct iattr *);
 extern int inode_newsize_ok(const struct inode *, loff_t offset);
 void setattr_copy(struct user_namespace *, struct inode *inode,