Skip to content

Commit 37e3a26

Browse files
Gality369gality369
andauthored
dmu_direct: avoid UAF in dmu_write_direct_done()
dmu_write_direct_done() passes dmu_sync_arg_t to dmu_sync_done(), which updates the override state and frees the completion context. The Direct I/O error path then still dereferences dsa->dsa_tx while rolling the dirty record back with dbuf_undirty(), resulting in a use-after-free. Save dsa->dsa_tx in a local variable before calling dmu_sync_done() and use that saved tx for the error rollback. This preserves the existing ownership model for dsa and does not change the Direct I/O write semantics. Reviewed-by: Brian Atkinson <batkinson@lanl.gov> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Co-authored-by: gality369 <gality369@example.com> Signed-off-by: ZhengYuan Huang <gality369@gmail.com> Closes #18440
1 parent ddf19dc commit 37e3a26

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

module/zfs/dmu_direct.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ dmu_write_direct_done(zio_t *zio)
9191
dmu_sync_arg_t *dsa = zio->io_private;
9292
dbuf_dirty_record_t *dr = dsa->dsa_dr;
9393
dmu_buf_impl_t *db = dr->dr_dbuf;
94+
dmu_tx_t *tx = dsa->dsa_tx;
9495

9596
abd_free(zio->io_abd);
9697

@@ -101,6 +102,11 @@ dmu_write_direct_done(zio_t *zio)
101102
db->db_state = DB_UNCACHED;
102103
mutex_exit(&db->db_mtx);
103104

105+
/*
106+
* dmu_sync_done() owns dsa and frees it after publishing the final
107+
* override state. The direct-I/O error path still needs the original
108+
* open-context tx to roll the dirty record back with dbuf_undirty().
109+
*/
104110
dmu_sync_done(zio, NULL, zio->io_private);
105111

106112
if (zio->io_error != 0) {
@@ -120,7 +126,7 @@ dmu_write_direct_done(zio_t *zio)
120126
* calling dbuf_undirty().
121127
*/
122128
mutex_enter(&db->db_mtx);
123-
VERIFY3B(dbuf_undirty(db, dsa->dsa_tx), ==, B_FALSE);
129+
VERIFY3B(dbuf_undirty(db, tx), ==, B_FALSE);
124130
mutex_exit(&db->db_mtx);
125131
}
126132

0 commit comments

Comments
 (0)