aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavi Arnaut <davi.arnaut@gmail.com>2006-03-24 06:18:42 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-03-24 10:33:31 -0500
commit96840aa00a031069a136ec4c55d0bdd09ac6d3a7 (patch)
treef93592ca47f2412aa0cce3ce4c116d1678a42239
parent6687a97d4041f996f725902d2990e5de6ef5cbe5 (diff)
[PATCH] strndup_user()
This patch series creates a strndup_user() function to easy copying C strings from userspace. Also we avoid common pitfalls like userspace modifying the final \0 after the strlen_user(). Signed-off-by: Davi Arnaut <davi.arnaut@gmail.com> Cc: David Howells <dhowells@redhat.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--include/linux/string.h2
-rw-r--r--mm/util.c37
2 files changed, 39 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h
index 369be3264a55..dee221429ad0 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -18,6 +18,8 @@ extern char * strsep(char **,const char *);
18extern __kernel_size_t strspn(const char *,const char *); 18extern __kernel_size_t strspn(const char *,const char *);
19extern __kernel_size_t strcspn(const char *,const char *); 19extern __kernel_size_t strcspn(const char *,const char *);
20 20
21extern char *strndup_user(const char __user *, long);
22
21/* 23/*
22 * Include machine specific inline routines 24 * Include machine specific inline routines
23 */ 25 */
diff --git a/mm/util.c b/mm/util.c
index 5f4bb59da63c..49e29f751b50 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -1,6 +1,8 @@
1#include <linux/slab.h> 1#include <linux/slab.h>
2#include <linux/string.h> 2#include <linux/string.h>
3#include <linux/module.h> 3#include <linux/module.h>
4#include <linux/err.h>
5#include <asm/uaccess.h>
4 6
5/** 7/**
6 * kzalloc - allocate memory. The memory is set to zero. 8 * kzalloc - allocate memory. The memory is set to zero.
@@ -37,3 +39,38 @@ char *kstrdup(const char *s, gfp_t gfp)
37 return buf; 39 return buf;
38} 40}
39EXPORT_SYMBOL(kstrdup); 41EXPORT_SYMBOL(kstrdup);
42
43/*
44 * strndup_user - duplicate an existing string from user space
45 *
46 * @s: The string to duplicate
47 * @n: Maximum number of bytes to copy, including the trailing NUL.
48 */
49char *strndup_user(const char __user *s, long n)
50{
51 char *p;
52 long length;
53
54 length = strnlen_user(s, n);
55
56 if (!length)
57 return ERR_PTR(-EFAULT);
58
59 if (length > n)
60 return ERR_PTR(-EINVAL);
61
62 p = kmalloc(length, GFP_KERNEL);
63
64 if (!p)
65 return ERR_PTR(-ENOMEM);
66
67 if (copy_from_user(p, s, length)) {
68 kfree(p);
69 return ERR_PTR(-EFAULT);
70 }
71
72 p[length - 1] = '\0';
73
74 return p;
75}
76EXPORT_SYMBOL(strndup_user);