summaryrefslogtreecommitdiff
path: root/xen/common/guestcopy.c
blob: 6d38eefedd3ab894d5e511fa6a8ccb6f3842e2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <xen/lib.h>
#include <xen/guest_access.h>
#include <xen/err.h>

/*
 * The function copies a string from the guest and adds a NUL to
 * make sure the string is correctly terminated.
 */
char *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
                                  size_t size, size_t max_size)
{
    char *tmp;

    if ( size > max_size )
        return ERR_PTR(-ENOBUFS);

    /* Add an extra +1 to append \0 */
    tmp = xmalloc_array(char, size + 1);
    if ( !tmp )
        return ERR_PTR(-ENOMEM);

    if ( copy_from_guest(tmp, u_buf, size) )
    {
        xfree(tmp);
        return ERR_PTR(-EFAULT);
    }
    tmp[size] = '\0';

    return tmp;
}