mlx5vf_resume_write() caches migf->buf[0] and migf->buf_header[0] and validates filp->f_pos before acquiring state_mutex and migf->lock. Because the migration file is opened with stream_open(), FMODE_ATOMIC_POS is cleared and concurrent writes on duplicated descriptors are not serialized by f_pos_lock. A racing writer inside migf->lock can free and reallocate migf->buf[0] or migf->buf_header[0] in PREP_HEADER_DATA or PREP_IMAGE, leaving the unlocked caller with a stale or freed pointer. Move the f_pos overflow check and buffer pointer reads under migf->lock after the MLX5_MIGF_STATE_ERROR check, matching the pattern used in commit 4ec5e932e636 ("vfio/qat: fix f_pos race in qat_vf_resume_write()"). Set done = -EINVAL rather than ret = -EINVAL on invalid f_pos so the migration file is not transitioned into MLX5_MIGF_STATE_ERROR, preserving existing behavior. Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (b6a9fa34ce4). Build-tested only due to lack of mlx5 VF live-migration hardware. Fixes: 6fadb021266d ("vfio/mlx5: Implement vfio_pci driver for mlx5 devices") Cc: stable@vger.kernel.org Signed-off-by: Qican Ma --- v2: resend via direct SMTP without line-wrapping corruption in the diff. drivers/vfio/pci/mlx5/main.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index 18a6d224b1f..ebcc22bcb75 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -884,8 +884,8 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf, size_t len, loff_t *pos) { struct mlx5_vf_migration_file *migf = filp->private_data; - struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0]; - struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0]; + struct mlx5_vhca_data_buffer *vhca_buf; + struct mlx5_vhca_data_buffer *vhca_buf_header; loff_t requested_length; bool has_work = false; ssize_t done = 0; @@ -895,10 +895,6 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf, return -ESPIPE; pos = &filp->f_pos; - if (*pos < 0 || - check_add_overflow((loff_t)len, *pos, &requested_length)) - return -EINVAL; - mutex_lock(&migf->mvdev->state_mutex); mutex_lock(&migf->lock); if (migf->state == MLX5_MIGF_STATE_ERROR) { @@ -906,6 +902,15 @@ static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf, goto out_unlock; } + if (*pos < 0 || + check_add_overflow((loff_t)len, *pos, &requested_length)) { + done = -EINVAL; + goto out_unlock; + } + + vhca_buf = migf->buf[0]; + vhca_buf_header = migf->buf_header[0]; + while (len || has_work) { has_work = false; switch (migf->load_state) { -- 2.50.1 (Apple Git-155)