xfs: validate explicit directory data buffer owners
authorDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:54:40 +0000 (14:54 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Mon, 15 Apr 2024 21:58:52 +0000 (14:58 -0700)
Port the existing directory data header checking function to accept an
owner number instead of an xfs_inode, then update the callsites to use
xfs_da_args.owner when possible.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/libxfs/xfs_dir2.h
fs/xfs/libxfs/xfs_dir2_block.c
fs/xfs/libxfs/xfs_dir2_data.c
fs/xfs/libxfs/xfs_dir2_leaf.c
fs/xfs/libxfs/xfs_dir2_node.c
fs/xfs/libxfs/xfs_dir2_priv.h
fs/xfs/scrub/dir.c
fs/xfs/scrub/readdir.c
fs/xfs/xfs_dir2_readdir.c

index 2f728c26a4162493ee90004afe66f47a23919f9b..d623bfdcd421850600e38c1dfc7e68a82670e8d8 100644 (file)
@@ -102,6 +102,7 @@ extern struct xfs_dir2_data_free *xfs_dir2_data_freefind(
 extern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino);
 
 xfs_failaddr_t xfs_dir3_leaf_header_check(struct xfs_buf *bp, xfs_ino_t owner);
+xfs_failaddr_t xfs_dir3_data_header_check(struct xfs_buf *bp, xfs_ino_t owner);
 
 extern const struct xfs_buf_ops xfs_dir3_block_buf_ops;
 extern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops;
index 61cbc668f228a590a9de4881db6387c0d589b517..b20b08394aa06d49975bbe82be9550d6439a5d49 100644 (file)
@@ -982,7 +982,8 @@ xfs_dir2_leaf_to_block(
         * Read the data block if we don't already have it, give up if it fails.
         */
        if (!dbp) {
-               error = xfs_dir3_data_read(tp, dp, args->geo->datablk, 0, &dbp);
+               error = xfs_dir3_data_read(tp, dp, args->owner,
+                               args->geo->datablk, 0, &dbp);
                if (error)
                        return error;
        }
index c3ef720b5ff6e83cc552e220afc745bc21022035..ea0b9628df18b09cbc9d2bd57ca06aec52a4e94b 100644 (file)
@@ -395,17 +395,20 @@ static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
        .verify_write = xfs_dir3_data_write_verify,
 };
 
-static xfs_failaddr_t
+xfs_failaddr_t
 xfs_dir3_data_header_check(
-       struct xfs_inode        *dp,
-       struct xfs_buf          *bp)
+       struct xfs_buf          *bp,
+       xfs_ino_t               owner)
 {
-       struct xfs_mount        *mp = dp->i_mount;
+       struct xfs_mount        *mp = bp->b_mount;
 
        if (xfs_has_crc(mp)) {
                struct xfs_dir3_data_hdr *hdr3 = bp->b_addr;
 
-               if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
+               if (hdr3->hdr.magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
+                       return __this_address;
+
+               if (be64_to_cpu(hdr3->hdr.owner) != owner)
                        return __this_address;
        }
 
@@ -416,6 +419,7 @@ int
 xfs_dir3_data_read(
        struct xfs_trans        *tp,
        struct xfs_inode        *dp,
+       xfs_ino_t               owner,
        xfs_dablk_t             bno,
        unsigned int            flags,
        struct xfs_buf          **bpp)
@@ -429,7 +433,7 @@ xfs_dir3_data_read(
                return err;
 
        /* Check things that we can't do in the verifier. */
-       fa = xfs_dir3_data_header_check(dp, *bpp);
+       fa = xfs_dir3_data_header_check(*bpp, owner);
        if (fa) {
                __xfs_buf_mark_corrupt(*bpp, fa);
                xfs_trans_brelse(tp, *bpp);
index 53b808e2a5f07b26a734e0dcbe514d857e5e5110..0b1b852f6178bdad837ff9e4d7f27ad34ba81fa6 100644 (file)
@@ -885,9 +885,9 @@ xfs_dir2_leaf_addname(
                 * Already had space in some data block.
                 * Just read that one in.
                 */
-               error = xfs_dir3_data_read(tp, dp,
-                                  xfs_dir2_db_to_da(args->geo, use_block),
-                                  0, &dbp);
+               error = xfs_dir3_data_read(tp, dp, args->owner,
+                               xfs_dir2_db_to_da(args->geo, use_block), 0,
+                               &dbp);
                if (error) {
                        xfs_trans_brelse(tp, lbp);
                        return error;
@@ -1328,9 +1328,9 @@ xfs_dir2_leaf_lookup_int(
                if (newdb != curdb) {
                        if (dbp)
                                xfs_trans_brelse(tp, dbp);
-                       error = xfs_dir3_data_read(tp, dp,
-                                          xfs_dir2_db_to_da(args->geo, newdb),
-                                          0, &dbp);
+                       error = xfs_dir3_data_read(tp, dp, args->owner,
+                                       xfs_dir2_db_to_da(args->geo, newdb), 0,
+                                       &dbp);
                        if (error) {
                                xfs_trans_brelse(tp, lbp);
                                return error;
@@ -1370,9 +1370,9 @@ xfs_dir2_leaf_lookup_int(
                ASSERT(cidb != -1);
                if (cidb != curdb) {
                        xfs_trans_brelse(tp, dbp);
-                       error = xfs_dir3_data_read(tp, dp,
-                                          xfs_dir2_db_to_da(args->geo, cidb),
-                                          0, &dbp);
+                       error = xfs_dir3_data_read(tp, dp, args->owner,
+                                       xfs_dir2_db_to_da(args->geo, cidb), 0,
+                                       &dbp);
                        if (error) {
                                xfs_trans_brelse(tp, lbp);
                                return error;
@@ -1666,7 +1666,8 @@ xfs_dir2_leaf_trim_data(
        /*
         * Read the offending data block.  We need its buffer.
         */
-       error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(geo, db), 0, &dbp);
+       error = xfs_dir3_data_read(tp, dp, args->owner,
+                       xfs_dir2_db_to_da(geo, db), 0, &dbp);
        if (error)
                return error;
 
index e21965788188b41b5404a5637715da47fe4102aa..dc85197b8448ecf7e7ac4b4963d084b7ec497aa7 100644 (file)
@@ -863,7 +863,7 @@ xfs_dir2_leafn_lookup_for_entry(
                                ASSERT(state->extravalid);
                                curbp = state->extrablk.bp;
                        } else {
-                               error = xfs_dir3_data_read(tp, dp,
+                               error = xfs_dir3_data_read(tp, dp, args->owner,
                                                xfs_dir2_db_to_da(args->geo,
                                                                  newdb),
                                                0, &curbp);
@@ -1949,9 +1949,8 @@ xfs_dir2_node_addname_int(
                                                  &freehdr, &findex);
        } else {
                /* Read the data block in. */
-               error = xfs_dir3_data_read(tp, dp,
-                                          xfs_dir2_db_to_da(args->geo, dbno),
-                                          0, &dbp);
+               error = xfs_dir3_data_read(tp, dp, args->owner,
+                               xfs_dir2_db_to_da(args->geo, dbno), 0, &dbp);
        }
        if (error)
                return error;
index 2f0e3ad47b3713e825db6a6dc61151652f25f2d2..879aa2e9fd73041e13f7c41e513529fa882e4c83 100644 (file)
@@ -78,7 +78,8 @@ extern void xfs_dir3_data_check(struct xfs_inode *dp, struct xfs_buf *bp);
 extern xfs_failaddr_t __xfs_dir3_data_check(struct xfs_inode *dp,
                struct xfs_buf *bp);
 int xfs_dir3_data_read(struct xfs_trans *tp, struct xfs_inode *dp,
-               xfs_dablk_t bno, unsigned int flags, struct xfs_buf **bpp);
+               xfs_ino_t owner, xfs_dablk_t bno, unsigned int flags,
+               struct xfs_buf **bpp);
 int xfs_dir3_data_readahead(struct xfs_inode *dp, xfs_dablk_t bno,
                unsigned int flags);
 
index d94e265a8e1f2f97314457ef0c1023369a723398..6b572196bb43dffa7a39451ba0aa1bd187a62e80 100644 (file)
@@ -196,8 +196,8 @@ xchk_dir_rec(
                xchk_da_set_corrupt(ds, level);
                goto out;
        }
-       error = xfs_dir3_data_read(ds->dargs.trans, dp, rec_bno,
-                       XFS_DABUF_MAP_HOLE_OK, &bp);
+       error = xfs_dir3_data_read(ds->dargs.trans, dp, ds->dargs.owner,
+                       rec_bno, XFS_DABUF_MAP_HOLE_OK, &bp);
        if (!xchk_fblock_process_error(ds->sc, XFS_DATA_FORK, rec_bno,
                        &error))
                goto out;
@@ -318,7 +318,8 @@ xchk_directory_data_bestfree(
                error = xfs_dir3_block_read(sc->tp, sc->ip, &bp);
        } else {
                /* dir data format */
-               error = xfs_dir3_data_read(sc->tp, sc->ip, lblk, 0, &bp);
+               error = xfs_dir3_data_read(sc->tp, sc->ip, sc->ip->i_ino, lblk,
+                               0, &bp);
        }
        if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk, &error))
                goto out;
@@ -531,10 +532,9 @@ xchk_directory_leaf1_bestfree(
        /* Check all the bestfree entries. */
        for (i = 0; i < bestcount; i++, bestp++) {
                best = be16_to_cpu(*bestp);
-               error = xfs_dir3_data_read(sc->tp, sc->ip,
+               error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
                                xfs_dir2_db_to_da(args->geo, i),
-                               XFS_DABUF_MAP_HOLE_OK,
-                               &dbp);
+                               XFS_DABUF_MAP_HOLE_OK, &dbp);
                if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
                                &error))
                        break;
@@ -597,7 +597,7 @@ xchk_directory_free_bestfree(
                        stale++;
                        continue;
                }
-               error = xfs_dir3_data_read(sc->tp, sc->ip,
+               error = xfs_dir3_data_read(sc->tp, sc->ip, args->owner,
                                (freehdr.firstdb + i) * args->geo->fsbcount,
                                0, &dbp);
                if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, lblk,
index fb98b7624994a34ce21c531371cbcd9080cbceb3..bed15a9524a2f31dada785babcdc7afc7bcf8aa3 100644 (file)
@@ -175,7 +175,7 @@ xchk_read_leaf_dir_buf(
        if (new_off > *curoff)
                *curoff = new_off;
 
-       return xfs_dir3_data_read(tp, dp, map.br_startoff, 0, bpp);
+       return xfs_dir3_data_read(tp, dp, dp->i_ino, map.br_startoff, 0, bpp);
 }
 
 /* Call a function for every entry in a leaf directory. */
index 4e811fa393adda9764aa864c1d4bfe55459fed62..2c03371b542aedab712a84f1f6ea02f4e44ec988 100644 (file)
@@ -282,7 +282,8 @@ xfs_dir2_leaf_readbuf(
        new_off = xfs_dir2_da_to_byte(geo, map.br_startoff);
        if (new_off > *cur_off)
                *cur_off = new_off;
-       error = xfs_dir3_data_read(args->trans, dp, map.br_startoff, 0, &bp);
+       error = xfs_dir3_data_read(args->trans, dp, args->owner,
+                       map.br_startoff, 0, &bp);
        if (error)
                goto out;