Skip to contentSkip to conversation

vfio/mlx5: Read the resume buffer pointers under migf->lock

PATCH In review Qican Ma 2 participants 2 messages Nov 24

Subsystems (4)
lore
Share & tools
lore

Qican Ma

author
#0 lore raw

PATCH v3 1/1 vfio/mlx5: Read the resume buffer pointers under migf->lock

mlx5vf_resume_write() reads migf->buf[0] and migf->buf_header[0] before taking migf->lock. The migration file is opened with stream_open(), so concurrent writes are not serialized by f_pos_lock. A racing writer may free and reallocate these buffers in PREP_IMAGE or PREP_HEADER_DATA, leaving the other writer with a freed pointer.

Read the buffer pointers under migf->lock, after the MLX5_MIGF_STATE_ERROR check.

While at it, move the f_pos check under the lock as well, as f_pos is protected by it. This is not a functional change, as f_pos is never negative and can't get close to an overflow.

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 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")

---
v3: correct Fixes: tag to 34e2f27143d1 and rework the commit message per
Yishai's review; no code changes.
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(-)
drivers/vfio/pci/mlx5/main.c +11 -6
index 18a6d224b1f..ebcc22bcb75 100644
@@ -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) {
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 13 lines · Qican Ma

mlx5vf_resume_write() reads migf->buf[0] and migf->buf_header[0] before taking migf->lock. The migration file is opened with stream_open(), so concurrent writes are not serialized by f_pos_lock. A racing writer may free and reallocate these buffers in PREP_IMAGE or PREP_HEADER_DATA, leaving the other writer with a freed pointer.

Read the buffer pointers under migf->lock, after the MLX5_MIGF_STATE_ERROR check.

While at it, move the f_pos check under the lock as well, as f_pos is protected by it. This is not a functional change, as f_pos is never negative and can't get close to an overflow.

Quoted 7 lines

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 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")

Yishai

Quoted 50 lines

---
v3: correct Fixes: tag to 34e2f27143d1 and rework the commit message per
Yishai's review; no code changes.
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(-)
drivers/vfio/pci/mlx5/main.c +11 -6
index 18a6d224b1f..ebcc22bcb75 100644
@@ -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) {