Skip to contentSkip to conversation

tty: vt: use memdup_user to reuse the code

PATCH In review Rahul Pathak 3 participants 4 messages Dec 19, 2015 – Feb 7, 2016 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: vt: use memdup_user to reuse the code

Fixing coccicheck warning which recommends to use memdup_user instead to reimplement its code, using memdup_user simplifies the code

./drivers/tty/vt/keyboard.c:1709:9-16: WARNING opportunity for memdup_user ./drivers/tty/vt/keyboard.c:1752:9-16: WARNING opportunity for memdup_user

---
 drivers/tty/vt/keyboard.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
drivers/tty/vt/keyboard.c +8 -20
index 6f0336f..ec05360 100644
@@ -1706,16 +1706,10 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;
 
 		if (ct) {
-			dia = kmalloc(sizeof(struct kbdiacr) * ct,
-								GFP_KERNEL);
-			if (!dia)
-				return -ENOMEM;
-
-			if (copy_from_user(dia, a->kbdiacr,
-					sizeof(struct kbdiacr) * ct)) {
-				kfree(dia);
-				return -EFAULT;
-			}
+			dia = memdup_user(a->kbdiacr,
+					sizeof(struct kbdiacr) * ct);
+			if (IS_ERR(dia))
+				return PTR_ERR(dia);
 		}
 
 		spin_lock_irqsave(&kbd_event_lock, flags);
@@ -1749,16 +1743,10 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;
 
 		if (ct) {
-			buf = kmalloc(ct * sizeof(struct kbdiacruc),
-								GFP_KERNEL);
-			if (buf == NULL)
-				return -ENOMEM;
-
-			if (copy_from_user(buf, a->kbdiacruc,
-					ct * sizeof(struct kbdiacruc))) {
-				kfree(buf);
-				return -EFAULT;
-			}
+			buf = memdup_user(a->kbdiacruc,
+					sizeof(struct kbdiacruc) * ct);
+			if (IS_ERR(buf))
+				return PTR_ERR(buf);
 		} 
 		spin_lock_irqsave(&kbd_event_lock, flags);
 		if (ct)
Signature · 1 line
2.1.4
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Rahul Pathak

Fixing coccicheck warning which recommends to use memdup_user instead to reimplement its code, using memdup_user simplifies the code

./drivers/tty/vt/keyboard.c:1709:9-16: WARNING opportunity for memdup_user ./drivers/tty/vt/keyboard.c:1752:9-16: WARNING opportunity for memdup_user

(assuming Greg will pick it up, otherwise I can take it through my tree).

Thanks.

Quoted 53 lines
---
 drivers/tty/vt/keyboard.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
drivers/tty/vt/keyboard.c +8 -20
index 6f0336f..ec05360 100644
@@ -1706,16 +1706,10 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;
 
 		if (ct) {
-			dia = kmalloc(sizeof(struct kbdiacr) * ct,
-								GFP_KERNEL);
-			if (!dia)
-				return -ENOMEM;
-
-			if (copy_from_user(dia, a->kbdiacr,
-					sizeof(struct kbdiacr) * ct)) {
-				kfree(dia);
-				return -EFAULT;
-			}
+			dia = memdup_user(a->kbdiacr,
+					sizeof(struct kbdiacr) * ct);
+			if (IS_ERR(dia))
+				return PTR_ERR(dia);
 		}
 
 		spin_lock_irqsave(&kbd_event_lock, flags);
@@ -1749,16 +1743,10 @@ int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm)
 			return -EINVAL;
 
 		if (ct) {
-			buf = kmalloc(ct * sizeof(struct kbdiacruc),
-								GFP_KERNEL);
-			if (buf == NULL)
-				return -ENOMEM;
-
-			if (copy_from_user(buf, a->kbdiacruc,
-					ct * sizeof(struct kbdiacruc))) {
-				kfree(buf);
-				return -EFAULT;
-			}
+			buf = memdup_user(a->kbdiacruc,
+					sizeof(struct kbdiacruc) * ct);
+			if (IS_ERR(buf))
+				return PTR_ERR(buf);
 		} 
 		spin_lock_irqsave(&kbd_event_lock, flags);
 		if (ct)
Signature · 1 line
2.1.4
Signature · 1 line
Dmitry
maintainer
#2 lore raw
In reply to Rahul Pathak
Quoted 11 lines · Rahul Pathak

Fixing coccicheck warning which recommends to use memdup_user instead to reimplement its code, using memdup_user simplifies the code

./drivers/tty/vt/keyboard.c:1709:9-16: WARNING opportunity for memdup_user ./drivers/tty/vt/keyboard.c:1752:9-16: WARNING opportunity for memdup_user

---
 drivers/tty/vt/keyboard.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

Someone else sent half of this patch already, can you redo it against my tty-testing branch of the tty.git tree and resend it?

thanks,

greg k-h

#3 lore raw

Hello Greg,

Yes I will do this.

Thanks
Rahul

Quoted 19 lines · Greg KH
Quoted 11 lines · Rahul Pathak

Fixing coccicheck warning which recommends to use memdup_user instead to reimplement its code, using memdup_user simplifies the code

./drivers/tty/vt/keyboard.c:1709:9-16: WARNING opportunity for memdup_user ./drivers/tty/vt/keyboard.c:1752:9-16: WARNING opportunity for memdup_user

---
 drivers/tty/vt/keyboard.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)

Someone else sent half of this patch already, can you redo it against my tty-testing branch of the tty.git tree and resend it?

thanks,

greg k-h