Skip to contentSkip to conversation

vfio/mlx5: read migration buffers and f_pos under migf->lock

PATCH Superseded Qican Ma 2 participants 3 messages Nov 24

Subsystems (4)
lore
Share & tools
lore

Qican Ma

author
#0 lore raw

PATCH v2 1/1 vfio/mlx5: read migration buffers and f_pos under migf->lock

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")

---
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
#1 lore raw
Quoted 8 lines · Qican Ma

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.

Right, the cached buffer pointers can go stale.

Quoted 1 line

Move the f_pos overflow check and buffer pointer reads under migf->lock

Moving the f_pos check under the lock is fine, but it doesn't fix a real issue. All other accesses to *pos are already under the lock. The early check can only see an older value, which is never negative and can't get close to an overflow, so its result is the same.

Quoted 11 lines

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")

In that commit, resume_write() had no cached buffer pointers at all. They can go stale only once resume_write() frees and reallocates the buffers, which was added by commit 34e2f27143d1, so it should be:

Fixes 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")

I would suggest something like the below for the commit message in V3:

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.

Fixes 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads")

Yishai

Quoted 50 lines

---
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) {
author
#2 lore raw

Hi Yishai,

Thanks for the review. You are right on both counts - I have sent v3 with the Fixes: tag corrected to 34e2f27143d1 ("vfio/mlx5: Introduce multiple loads") and the commit message reworked per your suggestion. The code change itself is unchanged.

Thanks,
Qican Ma