]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lsm: introduce new hooks for setting/getting inode fsxattr
authorAndrey Albershteyn <aalbersh@redhat.com>
Mon, 30 Jun 2025 16:20:12 +0000 (18:20 +0200)
committerChristian Brauner <brauner@kernel.org>
Tue, 1 Jul 2025 20:44:29 +0000 (22:44 +0200)
Introduce new hooks for setting and getting filesystem extended
attributes on inode (FS_IOC_FSGETXATTR).

Cc: selinux@vger.kernel.org
Cc: Paul Moore <paul@paul-moore.com>
Acked-by: Paul Moore <paul@paul-moore.com>
Signed-off-by: Andrey Albershteyn <aalbersh@kernel.org>
Link: https://lore.kernel.org/20250630-xattrat-syscall-v6-2-c4e3bc35227b@kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/file_attr.c
include/linux/lsm_hook_defs.h
include/linux/security.h
security/security.c

index a4dd1d9646c8375b40190b7b9e3ed8086a493c88..0aee402acde43b3bafd3045c573e6f5fa616eef4 100644 (file)
@@ -77,10 +77,15 @@ EXPORT_SYMBOL(fileattr_fill_flags);
 int vfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
 {
        struct inode *inode = d_inode(dentry);
+       int error;
 
        if (!inode->i_op->fileattr_get)
                return -ENOIOCTLCMD;
 
+       error = security_inode_file_getattr(dentry, fa);
+       if (error)
+               return error;
+
        return inode->i_op->fileattr_get(dentry, fa);
 }
 EXPORT_SYMBOL(vfs_fileattr_get);
@@ -243,12 +248,20 @@ int vfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry,
                } else {
                        fa->flags |= old_ma.flags & ~FS_COMMON_FL;
                }
+
                err = fileattr_set_prepare(inode, &old_ma, fa);
-               if (!err)
-                       err = inode->i_op->fileattr_set(idmap, dentry, fa);
+               if (err)
+                       goto out;
+               err = security_inode_file_setattr(dentry, fa);
+               if (err)
+                       goto out;
+               err = inode->i_op->fileattr_set(idmap, dentry, fa);
+               if (err)
+                       goto out;
        }
-       inode_unlock(inode);
 
+out:
+       inode_unlock(inode);
        return err;
 }
 EXPORT_SYMBOL(vfs_fileattr_set);
index bf3bbac4e02a59a67de4ace85b93b1a878131f60..9600a4350e791f47006720765c199e35d3abafd4 100644 (file)
@@ -157,6 +157,8 @@ LSM_HOOK(int, 0, inode_removexattr, struct mnt_idmap *idmap,
         struct dentry *dentry, const char *name)
 LSM_HOOK(void, LSM_RET_VOID, inode_post_removexattr, struct dentry *dentry,
         const char *name)
+LSM_HOOK(int, 0, inode_file_setattr, struct dentry *dentry, struct fileattr *fa)
+LSM_HOOK(int, 0, inode_file_getattr, struct dentry *dentry, struct fileattr *fa)
 LSM_HOOK(int, 0, inode_set_acl, struct mnt_idmap *idmap,
         struct dentry *dentry, const char *acl_name, struct posix_acl *kacl)
 LSM_HOOK(void, LSM_RET_VOID, inode_post_set_acl, struct dentry *dentry,
index dba349629229c1f7f58801a07a03d4d1ca4b8991..9ed0d0e0c81f115648fb1d2937255001a8411928 100644 (file)
@@ -451,6 +451,10 @@ int security_inode_listxattr(struct dentry *dentry);
 int security_inode_removexattr(struct mnt_idmap *idmap,
                               struct dentry *dentry, const char *name);
 void security_inode_post_removexattr(struct dentry *dentry, const char *name);
+int security_inode_file_setattr(struct dentry *dentry,
+                             struct fileattr *fa);
+int security_inode_file_getattr(struct dentry *dentry,
+                             struct fileattr *fa);
 int security_inode_need_killpriv(struct dentry *dentry);
 int security_inode_killpriv(struct mnt_idmap *idmap, struct dentry *dentry);
 int security_inode_getsecurity(struct mnt_idmap *idmap,
@@ -1052,6 +1056,18 @@ static inline void security_inode_post_removexattr(struct dentry *dentry,
                                                   const char *name)
 { }
 
+static inline int security_inode_file_setattr(struct dentry *dentry,
+                                             struct fileattr *fa)
+{
+       return 0;
+}
+
+static inline int security_inode_file_getattr(struct dentry *dentry,
+                                             struct fileattr *fa)
+{
+       return 0;
+}
+
 static inline int security_inode_need_killpriv(struct dentry *dentry)
 {
        return cap_inode_need_killpriv(dentry);
index 596d4181857735626eb0ec0f4c5693719c40dd9a..711b4de40b8d36497692041b33f6e8da87cf268f 100644 (file)
@@ -2622,6 +2622,36 @@ void security_inode_post_removexattr(struct dentry *dentry, const char *name)
        call_void_hook(inode_post_removexattr, dentry, name);
 }
 
+/**
+ * security_inode_file_setattr() - check if setting fsxattr is allowed
+ * @dentry: file to set filesystem extended attributes on
+ * @fa: extended attributes to set on the inode
+ *
+ * Called when file_setattr() syscall or FS_IOC_FSSETXATTR ioctl() is called on
+ * inode
+ *
+ * Return: Returns 0 if permission is granted.
+ */
+int security_inode_file_setattr(struct dentry *dentry, struct fileattr *fa)
+{
+       return call_int_hook(inode_file_setattr, dentry, fa);
+}
+
+/**
+ * security_inode_file_getattr() - check if retrieving fsxattr is allowed
+ * @dentry: file to retrieve filesystem extended attributes from
+ * @fa: extended attributes to get
+ *
+ * Called when file_getattr() syscall or FS_IOC_FSGETXATTR ioctl() is called on
+ * inode
+ *
+ * Return: Returns 0 if permission is granted.
+ */
+int security_inode_file_getattr(struct dentry *dentry, struct fileattr *fa)
+{
+       return call_int_hook(inode_file_getattr, dentry, fa);
+}
+
 /**
  * security_inode_need_killpriv() - Check if security_inode_killpriv() required
  * @dentry: associated dentry