]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: enforce metadata inode flag
authorDarrick J. Wong <djwong@kernel.org>
Mon, 25 Nov 2024 21:14:17 +0000 (13:14 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 24 Dec 2024 02:01:25 +0000 (18:01 -0800)
Source kernel commit: 7297fd0bebbd70efd12f72632a0f3ac49a8f59fe

Add checks for the metadata inode flag so that we don't ever leak
metadata inodes out to userspace, and we don't ever try to read a
regular inode as metadata.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
libxfs/xfs_inode_buf.c
libxfs/xfs_inode_buf.h
libxfs/xfs_metafile.h

index 981113f6acd37aacd22c5d1fce48e20d3fa24e90..98482cb4948284ae345396666af965c2e48b4834 100644 (file)
@@ -17,6 +17,7 @@
 #include "xfs_ialloc.h"
 #include "xfs_dir2.h"
 #include "xfs_health.h"
+#include "xfs_metafile.h"
 
 
 /*
@@ -486,6 +487,69 @@ xfs_dinode_verify_nrext64(
        return NULL;
 }
 
+/*
+ * Validate all the picky requirements we have for a file that claims to be
+ * filesystem metadata.
+ */
+xfs_failaddr_t
+xfs_dinode_verify_metadir(
+       struct xfs_mount        *mp,
+       struct xfs_dinode       *dip,
+       uint16_t                mode,
+       uint16_t                flags,
+       uint64_t                flags2)
+{
+       if (!xfs_has_metadir(mp))
+               return __this_address;
+
+       /* V5 filesystem only */
+       if (dip->di_version < 3)
+               return __this_address;
+
+       if (be16_to_cpu(dip->di_metatype) >= XFS_METAFILE_MAX)
+               return __this_address;
+
+       /* V3 inode fields that are always zero */
+       if ((flags2 & XFS_DIFLAG2_NREXT64) && dip->di_nrext64_pad)
+               return __this_address;
+       if (!(flags2 & XFS_DIFLAG2_NREXT64) && dip->di_flushiter)
+               return __this_address;
+
+       /* Metadata files can only be directories or regular files */
+       if (!S_ISDIR(mode) && !S_ISREG(mode))
+               return __this_address;
+
+       /* They must have zero access permissions */
+       if (mode & 0777)
+               return __this_address;
+
+       /* DMAPI event and state masks are zero */
+       if (dip->di_dmevmask || dip->di_dmstate)
+               return __this_address;
+
+       /*
+        * User and group IDs must be zero.  The project ID is used for
+        * grouping inodes.  Metadata inodes are never accounted to quotas.
+        */
+       if (dip->di_uid || dip->di_gid)
+               return __this_address;
+
+       /* Mandatory inode flags must be set */
+       if (S_ISDIR(mode)) {
+               if ((flags & XFS_METADIR_DIFLAGS) != XFS_METADIR_DIFLAGS)
+                       return __this_address;
+       } else {
+               if ((flags & XFS_METAFILE_DIFLAGS) != XFS_METAFILE_DIFLAGS)
+                       return __this_address;
+       }
+
+       /* dax flags2 must not be set */
+       if (flags2 & XFS_DIFLAG2_DAX)
+               return __this_address;
+
+       return NULL;
+}
+
 xfs_failaddr_t
 xfs_dinode_verify(
        struct xfs_mount        *mp,
@@ -670,6 +734,12 @@ xfs_dinode_verify(
            !xfs_has_bigtime(mp))
                return __this_address;
 
+       if (flags2 & XFS_DIFLAG2_METADATA) {
+               fa = xfs_dinode_verify_metadir(mp, dip, mode, flags, flags2);
+               if (fa)
+                       return fa;
+       }
+
        return NULL;
 }
 
index 585ed5a110af4e72a65783adf91639cc09686ea7..8d43d2641c73280301a6827a076f9363eafe2c01 100644 (file)
@@ -28,6 +28,9 @@ int   xfs_inode_from_disk(struct xfs_inode *ip, struct xfs_dinode *from);
 
 xfs_failaddr_t xfs_dinode_verify(struct xfs_mount *mp, xfs_ino_t ino,
                           struct xfs_dinode *dip);
+xfs_failaddr_t xfs_dinode_verify_metadir(struct xfs_mount *mp,
+               struct xfs_dinode *dip, uint16_t mode, uint16_t flags,
+               uint64_t flags2);
 xfs_failaddr_t xfs_inode_validate_extsize(struct xfs_mount *mp,
                uint32_t extsize, uint16_t mode, uint16_t flags);
 xfs_failaddr_t xfs_inode_validate_cowextsize(struct xfs_mount *mp,
index 60fe1890611277bdc79098688f594a305d56ee7c..c66b0c51b461a8f0ed1988fa837e5ec63924e019 100644 (file)
@@ -6,6 +6,17 @@
 #ifndef __XFS_METAFILE_H__
 #define __XFS_METAFILE_H__
 
+/* All metadata files must have these flags set. */
+#define XFS_METAFILE_DIFLAGS   (XFS_DIFLAG_IMMUTABLE | \
+                                XFS_DIFLAG_SYNC | \
+                                XFS_DIFLAG_NOATIME | \
+                                XFS_DIFLAG_NODUMP | \
+                                XFS_DIFLAG_NODEFRAG)
+
+/* All metadata directories must have these flags set. */
+#define XFS_METADIR_DIFLAGS    (XFS_METAFILE_DIFLAGS | \
+                                XFS_DIFLAG_NOSYMLINKS)
+
 /* Code specific to kernel/userspace; must be provided externally. */
 
 int xfs_trans_metafile_iget(struct xfs_trans *tp, xfs_ino_t ino,