vfio/virtio: read migration buffers and f_pos under migf->lock
Subsystems (5)
PATCH v2 1/1 vfio/virtio: read migration buffers and f_pos under migf->lock
virtiovf_resume_write() reads migf->buf and migf->buf_header and checks filp->f_pos against vhca_buf->start_pos before taking state_mutex and migf->lock. Because virtiovf_pci_resume_device_data() opens the file via stream_open(), FMODE_ATOMIC_POS is cleared and concurrent writes through duplicated file descriptors are not serialized by f_pos_lock. A racing writer in VIRTIOVF_LOAD_STATE_PREP_HEADER_DATA can free and reallocate migf->buf_header under migf->lock while another writer still holds the stale pointer captured before the lock.
Move the buffer pointer reads and start_pos check under migf->lock after checking VIRTIOVF_MIGF_STATE_ERROR.
Found with an LLM-assisted code review tool and verified by inspection on v7.4-rc4 (b6a9fa34ce4). Build-tested only due to lack of virtio VF live-migration hardware.
---
v2: resend via direct SMTP without line-wrapping corruption in the diff.
drivers/vfio/pci/virtio/migrate.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/vfio/pci/virtio/migrate.c +9 -4
@@ -918,8 +918,8 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf, size_t len, loff_t *pos) { struct virtiovf_migration_file *migf = filp->private_data; -struct virtiovf_data_buffer *vhca_buf = migf->buf; -struct virtiovf_data_buffer *vhca_buf_header = migf->buf_header; +struct virtiovf_data_buffer *vhca_buf; +struct virtiovf_data_buffer *vhca_buf_header;unsigned int orig_length;bool has_work = false;ssize_t done =0 ;
@@ -929,8 +929,6 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf,return -ESPIPE ; pos = &filp->f_pos; -if (*pos < vhca_buf->start_pos) -return -EINVAL ;mutex_lock (&migf->virtvdev->state_mutex);mutex_lock (&migf->lock);
@@ -939,6 +937,13 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf, goto out_unlock; } + vhca_buf = migf->buf; + vhca_buf_header = migf->buf_header; +if (*pos < vhca_buf->start_pos) { + done = -EINVAL ; + goto out_unlock; + } +while (len || has_work) { has_work = false;switch (migf->load_state) {
Signature · 1 line
2.50.1 (Apple Git-155)
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 12 lines
virtiovf_resume_write()readsmigf->bufandmigf->buf_headerand checksfilp->f_posagainstvhca_buf->start_posbefore takingstate_mutexandmigf->lock. Becausevirtiovf_pci_resume_device_data()opens the file viastream_open(),FMODE_ATOMIC_POSis cleared and concurrent writes through duplicated file descriptors are not serialized byf_pos_lock. A racing writer inVIRTIOVF_LOAD_STATE_PREP_HEADER_DATAcan free and reallocatemigf->buf_headerundermigf->lockwhile another writer still holds the stale pointer captured before the lock.Move the buffer pointer reads and
start_poscheck undermigf->lockafter checkingVIRTIOVF_MIGF_STATE_ERROR.
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 virtio VF live-migration hardware.Fixes 0bbc82e4ec79 ("vfio/virtio: Add support for the basic live migration functionality")Signed-off-by Qican Ma <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 12 lines · Qican Ma
virtiovf_resume_write()readsmigf->bufandmigf->buf_headerand checksfilp->f_posagainstvhca_buf->start_posbefore takingstate_mutexandmigf->lock. Becausevirtiovf_pci_resume_device_data()opens the file viastream_open(),FMODE_ATOMIC_POSis cleared and concurrent writes through duplicated file descriptors are not serialized byf_pos_lock. A racing writer inVIRTIOVF_LOAD_STATE_PREP_HEADER_DATAcan free and reallocatemigf->buf_headerundermigf->lockwhile another writer still holds the stale pointer captured before the lock.Move the buffer pointer reads and
start_poscheck undermigf->lockafter checkingVIRTIOVF_MIGF_STATE_ERROR.
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 virtio VF live-migration hardware.Fixes 0bbc82e4ec79 ("vfio/virtio: Add support for the basic live migration functionality")Signed-off-by Qican Ma <[email protected]>
Yishai
Quoted 44 lines
---
v2: resend via direct SMTP without line-wrapping corruption in the diff.drivers/vfio/pci/virtio/migrate.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)drivers/vfio/pci/virtio/migrate.c +9
-4@@ -918,8 +918,8 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf, size_t len, loff_t *pos) { struct virtiovf_migration_file *migf = filp->private_data; -struct virtiovf_data_buffer *vhca_buf = migf->buf; -struct virtiovf_data_buffer *vhca_buf_header = migf->buf_header; +struct virtiovf_data_buffer *vhca_buf; +struct virtiovf_data_buffer *vhca_buf_header;unsigned int orig_length;bool has_work = false;ssize_t done =0 ;@@ -929,8 +929,6 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf,return -ESPIPE ; pos = &filp->f_pos; -if (*pos < vhca_buf->start_pos) -return -EINVAL ;mutex_lock (&migf->virtvdev->state_mutex);mutex_lock (&migf->lock);@@ -939,6 +937,13 @@ static ssize_t virtiovf_resume_write(struct file *filp, const char __user *buf, goto out_unlock; } + vhca_buf = migf->buf; + vhca_buf_header = migf->buf_header; +if (*pos < vhca_buf->start_pos) { + done = -EINVAL ; + goto out_unlock; + } +while (len || has_work) { has_work = false;switch (migf->load_state) {