1#include <linux/slab.h> 2#include <linux/string.h> 3#include <linux/module.h> 4#include <linux/err.h> 5#include <asm/uaccess.h> 6 7/* 8 * strndup_user - duplicate an existing string from user space 9 * 10 * @s: The string to duplicate 11 * @n: Maximum number of bytes to copy, including the trailing NUL. 12 */ 13char *strndup_user(const char __user *s, long n) 14{ 15 char *p; 16 long length; 17 18 length = strnlen_user(s, n); 19 20 if (!length) 21 return ERR_PTR(-EFAULT); 22 23 if (length > n) 24 return ERR_PTR(-EINVAL); 25 26 p = kmalloc(length, GFP_KERNEL); 27 28 if (!p) 29 return ERR_PTR(-ENOMEM); 30 31 if (copy_from_user(p, s, length)) { 32 kfree(p); 33 return ERR_PTR(-EFAULT); 34 } 35 36 p[length - 1] = '\0'; 37 38 return p; 39} 40EXPORT_SYMBOL(strndup_user); 41

