Skip to contentSkip to conversation

xfs: fix a use after free in xfs_defer_finish_recovery

PATCH New Christoph Hellwig 1 participant 2 messages Dec 28, 2023 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 xfs: fix a use after free in xfs_defer_finish_recovery

dfp will be freed by ->recover_work and thus the tracepoint in case of an error can lead to a use after free.

Store the defer ops in a local variable to avoid that.

Fixes 7f2f7531e0d4 ("xfs: store an ops pointer in struct xfs_defer_pending")

---

- add a comment

 fs/xfs/libxfs/xfs_defer.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_defer.c +4 -2
index ca7f0ac0489604..75c5b3a2c2cba4 100644
@@ -915,12 +915,14 @@ xfs_defer_finish_recovery(
 	struct xfs_defer_pending	*dfp,
 	struct list_head		*capture_list)
 {
+	const struct xfs_defer_op_type	*ops = dfp->dfp_ops;
 	int				error;
 
-	error = dfp->dfp_ops->recover_work(dfp, capture_list);
+	/* dfp is freed by recover_work and must not be accessed afterwards */
+	error = ops->recover_work(dfp, capture_list);
 	if (error)
 		trace_xlog_intent_recovery_failed(mp, error,
-				dfp->dfp_ops->recover_work);
+				ops->recover_work);
 	return error;
 }
 
Signature · 1 line
2.39.2
#1 lore raw

PATCH v2 2/2 xfs: use the op name in trace_xlog_intent_recovery_failed

Instead of tracing the address of the recovery handler, use the name in the defer op, similar to other defer ops related tracepoints.

---
 fs/xfs/libxfs/xfs_defer.c |  3 +--
 fs/xfs/xfs_trace.h        | 15 +++++++++------
 2 files changed, 10 insertions(+), 8 deletions(-)
fs/xfs/libxfs/xfs_defer.c +1 -2
index 75c5b3a2c2cba4..66a17910d02194 100644
@@ -921,8 +921,7 @@ xfs_defer_finish_recovery(
 	/* dfp is freed by recover_work and must not be accessed afterwards */
 	error = ops->recover_work(dfp, capture_list);
 	if (error)
-		trace_xlog_intent_recovery_failed(mp, error,
-				ops->recover_work);
+		trace_xlog_intent_recovery_failed(mp, ops, error);
 	return error;
 }
 
fs/xfs/xfs_trace.h +9 -6
index 0efcdb79d10e51..0984a1c884c742 100644
@@ -67,6 +67,7 @@ struct xfs_buf_log_format;
 struct xfs_inode_log_format;
 struct xfs_bmbt_irec;
 struct xfs_btree_cur;
+struct xfs_defer_op_type;
 struct xfs_refcount_irec;
 struct xfs_fsmap;
 struct xfs_rmap_irec;
@@ -145,21 +146,23 @@ DEFINE_ATTR_LIST_EVENT(xfs_attr_leaf_list);
 DEFINE_ATTR_LIST_EVENT(xfs_attr_node_list);
 
 TRACE_EVENT(xlog_intent_recovery_failed,
-	TP_PROTO(struct xfs_mount *mp, int error, void *function),
-	TP_ARGS(mp, error, function),
+	TP_PROTO(struct xfs_mount *mp, const struct xfs_defer_op_type *ops,
+		 int error),
+	TP_ARGS(mp, ops, error),
 	TP_STRUCT__entry(
 		__field(dev_t, dev)
+		__string(name, ops->name)
 		__field(int, error)
-		__field(void *, function)
 	),
 	TP_fast_assign(
 		__entry->dev = mp->m_super->s_dev;
+		__assign_str(name, ops->name);
 		__entry->error = error;
-		__entry->function = function;
 	),
-	TP_printk("dev %d:%d error %d function %pS",
+	TP_printk("dev %d:%d optype %s error %d",
 		  MAJOR(__entry->dev), MINOR(__entry->dev),
-		  __entry->error, __entry->function)
+		  __get_str(name),
+		  __entry->error)
 );
 
 DECLARE_EVENT_CLASS(xfs_perag_class,
Signature · 1 line
2.39.2