Skip to contentSkip to conversation

drm/amdkfd: fix overflow for the function criu_restore_bos

PATCH In review Jesse Zhang 2 participants 2 messages May 30, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 3/8 drm/amdkfd: fix overflow for the function criu_restore_bos

When copying the information from the user fails, it will goto exit. But the variable i remains at 0, and do i-- will overflow.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -1
index fdf171ad4a3c..dac8fdc49e3b 100644
@@ -2480,10 +2480,11 @@ static int criu_restore_bos(struct kfd_process *p,
 		ret = -EFAULT;
 
 exit:
-	while (ret && i--) {
+	while (ret && i) {
 		if (bo_buckets[i].alloc_flags
 		   & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT))
 			close_fd(bo_buckets[i].dmabuf_fd);
+		i--;
 	}
 	kvfree(bo_buckets);
 	kvfree(bo_privs);
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 2 lines · Jesse Zhang

When copying the information from the user fails, it will goto exit. But the variable i remains at 0, and do i-- will overflow.

i-- may underflow, but the loop will still exit. Why is the underflow a problem?

Quoted 11 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index fdf171ad4a3c..dac8fdc49e3b 100644

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int criu_restore_bos(struct kfd_process *p, · line 2487

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -1
@@ -2480,10 +2480,11 @@ static int criu_restore_bos(struct kfd_process *p,
  		ret = -EFAULT;
 
  exit:
-	while (ret && i--) {
+	while (ret && i) {
  		if (bo_buckets[i].alloc_flags
  		   & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT))
  			close_fd(bo_buckets[i].dmabuf_fd);
+		i--;

This changes the value of i in the loop body. To get the same behaviour you'd need to decrement i at the start of the loop body.

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
  	}
  	kvfree(bo_buckets);
  	kvfree(bo_privs);