Skip to contentSkip to conversation

nouveau/u_memcpya: use vmemdup_user

PATCH In review Dave Airlie 2 participants 2 messages Aug 10, 2023

Subsystems (3)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

PATCH 1/1 nouveau/u_memcpya: use vmemdup_user

From: Dave Airlie <[email protected]>

I think there are limit checks in places for most things but the new api wants to not have them.

Add a limit check and use the vmemdup_user helper instead.

---
 drivers/gpu/drm/nouveau/nouveau_drv.h | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drv.h +5 -14
index 54063b094a69..8a7357688aff 100644
@@ -189,21 +189,12 @@ u_free(void *addr)
 static inline void *
 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
 {
-	void *mem;
-	void __user *userptr = (void __force __user *)(uintptr_t)user;
+	void __user *userptr = u64_to_user_ptr(user);
+	size_t bytes;
 
-	size *= nmemb;
-
-	mem = kvmalloc(size, GFP_KERNEL);
-	if (!mem)
-		return ERR_PTR(-ENOMEM);
-
-	if (copy_from_user(mem, userptr, size)) {
-		u_free(mem);
-		return ERR_PTR(-EFAULT);
-	}
-
-	return mem;
+	if (unlikely(check_mul_overflow(nmemb, size, &bytes)))
+		return NULL;
+	return vmemdup_user(userptr, bytes);
 }
 
 #include <nvif/object.h>
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Dave Airlie

From: Dave Airlie <[email protected]>

I think there are limit checks in places for most things but the new api wants to not have them.

Add a limit check and use the vmemdup_user helper instead.

Quoted 37 lines
---
  drivers/gpu/drm/nouveau/nouveau_drv.h | 19 +++++--------------
  1 file changed, 5 insertions(+), 14 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drv.h +5 -14
index 54063b094a69..8a7357688aff 100644
@@ -189,21 +189,12 @@ u_free(void *addr)
  static inline void *
  u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
  {
-	void *mem;
-	void __user *userptr = (void __force __user *)(uintptr_t)user;
+	void __user *userptr = u64_to_user_ptr(user);
+	size_t bytes;
 
-	size *= nmemb;
-
-	mem = kvmalloc(size, GFP_KERNEL);
-	if (!mem)
-		return ERR_PTR(-ENOMEM);
-
-	if (copy_from_user(mem, userptr, size)) {
-		u_free(mem);
-		return ERR_PTR(-EFAULT);
-	}
-
-	return mem;
+	if (unlikely(check_mul_overflow(nmemb, size, &bytes)))
+		return NULL;
+	return vmemdup_user(userptr, bytes);
  }
 
  #include <nvif/object.h>