]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
xfs: fix an incore inode UAF in xfs_bui_recover
authorDarrick J. Wong <darrick.wong@oracle.com>
Sat, 26 Sep 2020 00:39:51 +0000 (17:39 -0700)
committerDarrick J. Wong <darrick.wong@oracle.com>
Wed, 7 Oct 2020 15:40:28 +0000 (08:40 -0700)
In xfs_bui_item_recover, there exists a use-after-free bug with regards
to the inode that is involved in the bmap replay operation.  If the
mapping operation does not complete, we call xfs_bmap_unmap_extent to
create a deferred op to finish the unmapping work, and we retain a
pointer to the incore inode.

Unfortunately, the very next thing we do is commit the transaction and
drop the inode.  If reclaim tears down the inode before we try to finish
the defer ops, we dereference garbage and blow up.  Therefore, create a
way to join inodes to the defer ops freezer so that we can maintain the
xfs_inode reference until we're done with the inode.

Note: This imposes the requirement that there be enough memory to keep
every incore inode in memory throughout recovery.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Brian Foster <bfoster@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/libxfs/xfs_defer.c
fs/xfs/libxfs/xfs_defer.h
fs/xfs/xfs_bmap_item.c
fs/xfs/xfs_extfree_item.c
fs/xfs/xfs_log_recover.c
fs/xfs/xfs_refcount_item.c
fs/xfs/xfs_rmap_item.c

index e19dc1ced7e6e43225051f2bc2d43ae4dd1baff4..4d2583758f3debf3dacfed01a536975f752485fc 100644 (file)
@@ -16,6 +16,7 @@
 #include "xfs_inode.h"
 #include "xfs_inode_item.h"
 #include "xfs_trace.h"
+#include "xfs_icache.h"
 
 /*
  * Deferred Operations in XFS
@@ -553,10 +554,14 @@ xfs_defer_move(
  * deferred ops state is transferred to the capture structure and the
  * transaction is then ready for the caller to commit it.  If there are no
  * intent items to capture, this function returns NULL.
+ *
+ * If capture_ip is not NULL, the capture structure will obtain an extra
+ * reference to the inode.
  */
 static struct xfs_defer_capture *
 xfs_defer_ops_capture(
-       struct xfs_trans                *tp)
+       struct xfs_trans                *tp,
+       struct xfs_inode                *capture_ip)
 {
        struct xfs_defer_capture        *dfc;
 
@@ -582,6 +587,15 @@ xfs_defer_ops_capture(
        /* Preserve the log reservation size. */
        dfc->dfc_logres = tp->t_log_res;
 
+       /*
+        * Grab an extra reference to this inode and attach it to the capture
+        * structure.
+        */
+       if (capture_ip) {
+               ihold(VFS_I(capture_ip));
+               dfc->dfc_capture_ip = capture_ip;
+       }
+
        return dfc;
 }
 
@@ -592,24 +606,33 @@ xfs_defer_ops_release(
        struct xfs_defer_capture        *dfc)
 {
        xfs_defer_cancel_list(mp, &dfc->dfc_dfops);
+       if (dfc->dfc_capture_ip)
+               xfs_irele(dfc->dfc_capture_ip);
        kmem_free(dfc);
 }
 
 /*
  * Capture any deferred ops and commit the transaction.  This is the last step
- * needed to finish a log intent item that we recovered from the log.
+ * needed to finish a log intent item that we recovered from the log.  If any
+ * of the deferred ops operate on an inode, the caller must pass in that inode
+ * so that the reference can be transferred to the capture structure.  The
+ * caller must hold ILOCK_EXCL on the inode, and must unlock it before calling
+ * xfs_defer_ops_continue.
  */
 int
 xfs_defer_ops_capture_and_commit(
        struct xfs_trans                *tp,
+       struct xfs_inode                *capture_ip,
        struct list_head                *capture_list)
 {
        struct xfs_mount                *mp = tp->t_mountp;
        struct xfs_defer_capture        *dfc;
        int                             error;
 
+       ASSERT(!capture_ip || xfs_isilocked(capture_ip, XFS_ILOCK_EXCL));
+
        /* If we don't capture anything, commit transaction and exit. */
-       dfc = xfs_defer_ops_capture(tp);
+       dfc = xfs_defer_ops_capture(tp, capture_ip);
        if (!dfc)
                return xfs_trans_commit(tp);
 
@@ -626,16 +649,26 @@ xfs_defer_ops_capture_and_commit(
 
 /*
  * Attach a chain of captured deferred ops to a new transaction and free the
- * capture structure.
+ * capture structure.  If an inode was captured, it will be passed back to the
+ * caller with ILOCK_EXCL held and joined to the transaction with lockflags==0.
+ * The caller now owns the inode reference.
  */
 void
 xfs_defer_ops_continue(
        struct xfs_defer_capture        *dfc,
-       struct xfs_trans                *tp)
+       struct xfs_trans                *tp,
+       struct xfs_inode                **captured_ipp)
 {
        ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
        ASSERT(!(tp->t_flags & XFS_TRANS_DIRTY));
 
+       /* Lock and join the captured inode to the new transaction. */
+       if (dfc->dfc_capture_ip) {
+               xfs_ilock(dfc->dfc_capture_ip, XFS_ILOCK_EXCL);
+               xfs_trans_ijoin(tp, dfc->dfc_capture_ip, 0);
+       }
+       *captured_ipp = dfc->dfc_capture_ip;
+
        /* Move captured dfops chain and state to the transaction. */
        list_splice_init(&dfc->dfc_dfops, &tp->t_dfops);
        tp->t_flags |= dfc->dfc_tpflags;
index 6cde6f0713f738ed70df3231be742f29b3161b8a..05472f71fffe432b20613854bf7fc6e91abacc39 100644 (file)
@@ -82,6 +82,12 @@ struct xfs_defer_capture {
 
        /* Log reservation saved from the transaction. */
        unsigned int            dfc_logres;
+
+       /*
+        * An inode reference that must be maintained to complete the deferred
+        * work.
+        */
+       struct xfs_inode        *dfc_capture_ip;
 };
 
 /*
@@ -89,8 +95,9 @@ struct xfs_defer_capture {
  * This doesn't normally happen except log recovery.
  */
 int xfs_defer_ops_capture_and_commit(struct xfs_trans *tp,
-               struct list_head *capture_list);
-void xfs_defer_ops_continue(struct xfs_defer_capture *d, struct xfs_trans *tp);
+               struct xfs_inode *capture_ip, struct list_head *capture_list);
+void xfs_defer_ops_continue(struct xfs_defer_capture *d, struct xfs_trans *tp,
+               struct xfs_inode **captured_ipp);
 void xfs_defer_ops_release(struct xfs_mount *mp, struct xfs_defer_capture *d);
 
 #endif /* __XFS_DEFER_H__ */
index 852411568d1437e90d3f79d684b26629e2218a2f..4570da07eb06aab3aba68d41ce67c71726570aab 100644 (file)
@@ -513,8 +513,11 @@ xfs_bui_item_recover(
                xfs_bmap_unmap_extent(tp, ip, &irec);
        }
 
-       /* Commit transaction, which frees the transaction. */
-       error = xfs_defer_ops_capture_and_commit(tp, capture_list);
+       /*
+        * Commit transaction, which frees the transaction and saves the inode
+        * for later replay activities.
+        */
+       error = xfs_defer_ops_capture_and_commit(tp, ip, capture_list);
        if (error)
                goto err_unlock;
 
index 17d36fe5cfd0e680a657fe4c40b07bf882363f2c..3920542f5736ef912521a0bc999d4554eff73d21 100644 (file)
@@ -627,7 +627,7 @@ xfs_efi_item_recover(
 
        }
 
-       return xfs_defer_ops_capture_and_commit(tp, capture_list);
+       return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
 
 abort_error:
        xfs_trans_cancel(tp);
index 001e1585ddc6f6114d48ef0c153295e497709635..a8289adc1b29ec13739016e71d1d7ad1375a0e12 100644 (file)
@@ -2439,6 +2439,7 @@ xlog_finish_defer_ops(
 {
        struct xfs_defer_capture *dfc, *next;
        struct xfs_trans        *tp;
+       struct xfs_inode        *ip;
        int                     error = 0;
 
        list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
@@ -2464,9 +2465,13 @@ xlog_finish_defer_ops(
                 * from recovering a single intent item.
                 */
                list_del_init(&dfc->dfc_list);
-               xfs_defer_ops_continue(dfc, tp);
+               xfs_defer_ops_continue(dfc, tp, &ip);
 
                error = xfs_trans_commit(tp);
+               if (ip) {
+                       xfs_iunlock(ip, XFS_ILOCK_EXCL);
+                       xfs_irele(ip);
+               }
                if (error)
                        return error;
        }
index 0478374add64aec73dfd9b2e198c8fc477e3b442..ad895b48f365a410236e127dec68b3a4732f2579 100644 (file)
@@ -544,7 +544,7 @@ xfs_cui_item_recover(
        }
 
        xfs_refcount_finish_one_cleanup(tp, rcur, error);
-       return xfs_defer_ops_capture_and_commit(tp, capture_list);
+       return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
 
 abort_error:
        xfs_refcount_finish_one_cleanup(tp, rcur, error);
index 0d8fa707f07995c2aa23725f2af17ee6ec239ea8..1163f32c3e629d7b49b8b150c38956b900d34e56 100644 (file)
@@ -567,7 +567,7 @@ xfs_rui_item_recover(
        }
 
        xfs_rmap_finish_one_cleanup(tp, rcur, error);
-       return xfs_defer_ops_capture_and_commit(tp, capture_list);
+       return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
 
 abort_error:
        xfs_rmap_finish_one_cleanup(tp, rcur, error);