Skip to contentSkip to conversation

drm/amdkfd: validate SVM ioctl nattr against buffer size

PATCH Superseded Alysa Liu 1 participant 1 message Apr 21

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alysa Liu

author
#0 lore raw

PATCH 1/1 drm/amdkfd: validate SVM ioctl nattr against buffer size

Validate nattr field against the buffer size, preventing out-of-bounds stack access via user-controlled attribute count.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +16 -0
index 16fb39259911..3e575fdfa4c5 100644
@@ -3636,6 +3636,22 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 		memset(kdata, 0, usize);
 	}
 
+	if (nr == _IOC_NR(AMDKFD_IOC_SVM)) {
+		struct kfd_ioctl_svm_args *svm = (struct kfd_ioctl_svm_args *)kdata;
+		size_t expected_size;
+
+		// Guard against integer overflow in size calculation
+		if (svm->nattr > (SIZE_MAX - sizeof(*svm)) / sizeof(svm->attrs[0])) {
+			retcode = -EINVAL;
+			goto err_i1;
+		}
+		expected_size = sizeof(*svm) + (size_t)svm->nattr * sizeof(svm->attrs[0]);
+		if (usize < expected_size) {
+			retcode = -EINVAL;
+			goto err_i1;
+		}
+	}
+
 	retcode = func(filep, process, kdata);
 
 	if (cmd & IOC_OUT)
Signature · 1 line
2.34.1