Skip to contentSkip to conversation

vfio/qat: fix overflow check in qat_vf_resume_write()

PATCH Applied Giovanni Cabiddu 2 participants 2 messages Oct 21, 2024 – Nov 1, 2024

Subsystems (5)
lore
Share & tools
lore

Giovanni Cabiddu

author maintainer
#0 lore raw

PATCH 1/1 vfio/qat: fix overflow check in qat_vf_resume_write()

The unsigned variable size_t len is cast to the signed type loff_t when passed to the function check_add_overflow(). This function considers the type of the destination, which is of type loff_t (signed), potentially leading to an overflow. This issue is similar to the one described in the link below.

Remove the cast.

Note that even if check_add_overflow() is bypassed, by setting len to a value that is greater than LONG_MAX (which is considered as a negative value after the cast), the function copy_from_user(), invoked a few lines later, will not perform any copy and return len as (len > INT_MAX) causing qat_vf_resume_write() to fail with -EFAULT.

Fixes bb208810b1ab ("vfio/qat: Add vfio_pci driver for Intel QAT SR-IOV VF devices")
---
 drivers/vfio/pci/qat/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/qat/main.c +1 -1
index e36740a282e7..1e3563fe7cab 100644
@@ -305,7 +305,7 @@ static ssize_t qat_vf_resume_write(struct file *filp, const char __user *buf,
 	offs = &filp->f_pos;
 
 	if (*offs < 0 ||
-	    check_add_overflow((loff_t)len, *offs, &end))
+	    check_add_overflow(len, *offs, &end))
 		return -EOVERFLOW;
 
 	if (end > mig_dev->state_size)
Signature · 1 line
2.47.0
#1 lore raw
Quoted 29 lines · On Mon, 21 Oct 2024 13:37:53 +0100 Giovanni Cabiddu…

The unsigned variable size_t len is cast to the signed type loff_t when passed to the function check_add_overflow(). This function considers the type of the destination, which is of type loff_t (signed), potentially leading to an overflow. This issue is similar to the one described in the link below.

Remove the cast.

Note that even if check_add_overflow() is bypassed, by setting len to a value that is greater than LONG_MAX (which is considered as a negative value after the cast), the function copy_from_user(), invoked a few lines later, will not perform any copy and return len as (len > INT_MAX) causing qat_vf_resume_write() to fail with -EFAULT.

Fixes bb208810b1ab ("vfio/qat: Add vfio_pci driver for Intel QAT SR-IOV VF devices")
---
 drivers/vfio/pci/qat/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/pci/qat/main.c +0 -0
index e36740a282e7..1e3563fe7cab 100644

drivers/vfio/pci/qat/main.c static ssize_t qat_vf_resume_write(struct file *filp, const char __user *buf, · line 311

Quoted 8 lines
drivers/vfio/pci/qat/main.c +1 -1
@@ -305,7 +305,7 @@ static ssize_t qat_vf_resume_write(struct file *filp, const char __user *buf,
 	offs = &filp->f_pos;
 
 	if (*offs < 0 ||
-	    check_add_overflow((loff_t)len, *offs, &end))
+	    check_add_overflow(len, *offs, &end))
 		return -EOVERFLOW;
 
 	if (end > mig_dev->state_size)

Applied to vfio next branch for v6.13. Thanks,

Alex