1 #include <xen/lib.h>
2 #include <xen/guest_access.h>
3 #include <xen/err.h>
4 
5 /*
6  * The function copies a string from the guest and adds a NUL to
7  * make sure the string is correctly terminated.
8  */
safe_copy_string_from_guest(XEN_GUEST_HANDLE (char)u_buf,size_t size,size_t max_size)9 char *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
10                                   size_t size, size_t max_size)
11 {
12     char *tmp;
13 
14     if ( size > max_size )
15         return ERR_PTR(-ENOBUFS);
16 
17     /* Add an extra +1 to append \0 */
18     tmp = xmalloc_array(char, size + 1);
19     if ( !tmp )
20         return ERR_PTR(-ENOMEM);
21 
22     if ( copy_from_guest(tmp, u_buf, size) )
23     {
24         xfree(tmp);
25         return ERR_PTR(-EFAULT);
26     }
27     tmp[size] = '\0';
28 
29     return tmp;
30 }
31