Skip to contentSkip to conversation

ks390/keyboard: use memdup_user().

PATCH Superseded Muhammad Falak R Wani 2 participants 3 messages May 20, 2016 s390

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ks390/keyboard: use memdup_user().

Use memdup_user to duplicate a memory region from user-space to kernel-space, instead of open coding using kmalloc & copy_from_user.

---
 drivers/s390/char/keyboard.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/s390/char/keyboard.c +3 -7
index ef04a9f..2955983 100644
@@ -438,13 +438,9 @@ do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
 			return -EFAULT;
 		if (len > sizeof(u_kbs->kb_string))
 			return -EINVAL;
-		p = kmalloc(len, GFP_KERNEL);
-		if (!p)
-			return -ENOMEM;
-		if (copy_from_user(p, u_kbs->kb_string, len)) {
-			kfree(p);
-			return -EFAULT;
-		}
+		p = memdup_user(u_kbs->kb_string, len);
+		if (IS_ERR(p))
+			return PTR_ERR(p);
 		/*
 		 * Make sure the string is terminated by 0. User could have
 		 * modified it between us running strnlen_user() and copying it.
Signature · 1 line
1.9.1
#1 lore raw
Quoted 19 lines · Muhammad Falak R Wani

Use memdup_user to duplicate a memory region from user-space to kernel-space, instead of open coding using kmalloc & copy_from_user.

---
 drivers/s390/char/keyboard.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/s390/char/keyboard.c +0 -3
index ef04a9f..2955983 100644
@@ -438,13 +438,9 @@ do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
 			return -EFAULT;
 		if (len > sizeof(u_kbs->kb_string))
 			return -EINVAL;
-		p = kmalloc(len, GFP_KERNEL);
-		if (!p)
-			return -ENOMEM;

drivers/s390/char/keyboard.c do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs, · line 446

Quoted 10 lines
drivers/s390/char/keyboard.c +3 -4
-		if (copy_from_user(p, u_kbs->kb_string, len)) {
-			kfree(p);
-			return -EFAULT;
-		}
+		p = memdup_user(u_kbs->kb_string, len);
+		if (IS_ERR(p))
+			return PTR_ERR(p);
 		/*
 		 * Make sure the string is terminated by 0. User could have
 		 * modified it between us running strnlen_user() and copying it.

Hi Muhammad, thanks for this patch. However could you change this to use memdup_user_nul() instead, so we can get rid of even more code?

#2 lore raw
Quoted 4 lines · Heiko Carstens
Quoted 3 lines · Muhammad Falak R Wani

Use memdup_user to duplicate a memory region from user-space to kernel-space, instead of open coding using kmalloc & copy_from_user.

Quoted 11 lines

drivers/s390/char/keyboard.c do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs, · line 446

Quoted 7 lines
drivers/s390/char/keyboard.c +3 -0
+		p = memdup_user(u_kbs->kb_string, len);
+		if (IS_ERR(p))
+			return PTR_ERR(p);
 		/*
 		 * Make sure the string is terminated by 0. User could have
 		 * modified it between us running strnlen_user() and copying it.

Hi Muhammad, thanks for this patch. However could you change this to use memdup_user_nul() instead, so we can get rid of even more code?

Should I send V2 with the suggested changes ?