| 1 |
/* -*- linux-c -*- |
| 2 |
* Copy from user space functions |
| 3 |
* Copyright (C) 2005-2017 Red Hat Inc. |
| 4 |
* Copyright (C) 2005 Intel Corporation. |
| 5 |
* |
| 6 |
* This file is part of systemtap, and is free software. You can |
| 7 |
* redistribute it and/or modify it under the terms of the GNU General |
| 8 |
* Public License (GPL); either version 2, or (at your option) any |
| 9 |
* later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef _STAPLINUX_COPY_C_ /* -*- linux-c -*- */ |
| 13 |
#define _STAPLINUX_COPY_C_ |
| 14 |
|
| 15 |
/** @file copy.c |
| 16 |
* @brief Functions to copy from user space. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** @addtogroup copy Functions to copy from user space. |
| 20 |
* Functions to copy from user space. |
| 21 |
* None of these functions will sleep (for example to allow pages |
| 22 |
* to be swapped in). It is possible (although rare) that the data |
| 23 |
* in user space will not present and these functions will return an error. |
| 24 |
* @{ |
| 25 |
*/ |
| 26 |
|
| 27 |
/** Copy a NULL-terminated string from userspace. |
| 28 |
* On success, returns the length of the string (not including the trailing |
| 29 |
* NULL). |
| 30 |
* |
| 31 |
* If access to userspace fails, returns -EFAULT (some data may have been |
| 32 |
* copied). |
| 33 |
* @param dst Destination address, in kernel space. This buffer must be at |
| 34 |
* least <i>count</i> bytes long. |
| 35 |
* @param src Source address, in user space. |
| 36 |
* @param count Maximum number of bytes to copy, including the trailing NULL. |
| 37 |
* |
| 38 |
* If <i>count</i> is smaller than the length of the string, copies |
| 39 |
* <i>count</i> bytes and returns <i>count</i>. |
| 40 |
*/ |
| 41 |
|
| 42 |
static long _stp_strncpy_from_user(char *dst, const char __user *src, |
| 43 |
long count) |
| 44 |
{ |
| 45 |
return _stp_deref_string_nofault(dst, src, count, STP_USER_DS); |
| 46 |
} |
| 47 |
|
| 48 |
/** Copy a block of data from user space. |
| 49 |
* |
| 50 |
* If data could not be copied, this function will not modify the |
| 51 |
* destination. |
| 52 |
* |
| 53 |
* @param dst Destination address, in kernel space. |
| 54 |
* @param src Source address, in user space. |
| 55 |
* @param count Number of bytes to copy. |
| 56 |
* @return number of bytes that could not be copied. On success, |
| 57 |
* this will be zero. |
| 58 |
* |
| 59 |
*/ |
| 60 |
|
| 61 |
/* XXX: see also kread/uread in loc2c-runtime.h */ |
| 62 |
static unsigned long _stp_copy_from_user(char *dst, const char __user *src, unsigned long count) |
| 63 |
{ |
| 64 |
if (count) { |
| 65 |
stp_mm_segment_t oldfs; |
| 66 |
pagefault_disable(); |
| 67 |
if (!stp_user_access_begin(VERIFY_READ, src, count, &oldfs, STP_USER_DS)) { |
| 68 |
pagefault_enable(); |
| 69 |
goto done; |
| 70 |
} |
| 71 |
if (!lookup_bad_addr(VERIFY_READ, (const unsigned long)src, count, STP_USER_DS)) |
| 72 |
count = __copy_from_user_inatomic(dst, src, count); |
| 73 |
else |
| 74 |
/* Notice that if we fail, we don't modify |
| 75 |
* the destination. In the failure case, we |
| 76 |
* can't trust 'count' to be reasonable. */ |
| 77 |
count = -EFAULT; |
| 78 |
pagefault_enable(); |
| 79 |
stp_user_access_end(oldfs); |
| 80 |
} |
| 81 |
done: |
| 82 |
return count; |
| 83 |
} |
| 84 |
|
| 85 |
/** @} */ |
| 86 |
#endif /* _STAPLINUX_COPY_C_ */ |