Skip to content

Commit

Permalink
mm: use memdup_user
Browse files Browse the repository at this point in the history
Use memdup_user when user data is immediately copied into the
allocated region.

The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@@
expression from,to,size,flag;
position p;
identifier l1,l2;
@@

-  to = \(kmalloc@p\|kzalloc@p\)(size,flag);
+  to = memdup_user(from,size);
   if (
-      to==NULL
+      IS_ERR(to)
                 || ...) {
   <+... when != goto l1;
-  -ENOMEM
+  PTR_ERR(to)
   ...+>
   }
-  if (copy_from_user(to, from, size) != 0) {
-    <+... when != goto l2;
-    -EFAULT
-    ...+>
-  }
// </smpl>

Signed-off-by: Julia Lawall <[email protected]>
Cc: KOSAKI Motohiro <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Julia Lawall authored and torvalds committed Aug 10, 2010
1 parent 9e58143 commit 90d7404
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions mm/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,10 @@ char *strndup_user(const char __user *s, long n)
if (length > n)
return ERR_PTR(-EINVAL);

p = kmalloc(length, GFP_KERNEL);
p = memdup_user(s, length);

if (!p)
return ERR_PTR(-ENOMEM);

if (copy_from_user(p, s, length)) {
kfree(p);
return ERR_PTR(-EFAULT);
}
if (IS_ERR(p))
return p;

p[length - 1] = '\0';

Expand Down

0 comments on commit 90d7404

Please sign in to comment.