| 1 |
/* -*- linux-c -*- |
| 2 |
* Copy from user space functions |
| 3 |
* Copyright (C) 2012 Red Hat Inc. |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _STAPDYN_COPY_C_ |
| 12 |
#define _STAPDYN_COPY_C_ |
| 13 |
|
| 14 |
#include <unistd.h> |
| 15 |
#include <sys/syscall.h> |
| 16 |
#include <sys/uio.h> |
| 17 |
|
| 18 |
#include "stp_string.c" |
| 19 |
|
| 20 |
|
| 21 |
static int _stp_mem_fd = -1; |
| 22 |
|
| 23 |
|
| 24 |
static inline __must_check ssize_t __self_readv(const struct iovec *lvec, |
| 25 |
unsigned long liovcnt, |
| 26 |
const struct iovec *rvec, |
| 27 |
unsigned long riovcnt) |
| 28 |
{ |
| 29 |
#if !__GLIBC_PREREQ(2, 15) |
| 30 |
#ifdef __NR_process_vm_readv |
| 31 |
#define process_vm_readv(...) \ |
| 32 |
syscall(__NR_process_vm_readv, __VA_ARGS__) |
| 33 |
#else |
| 34 |
#define process_vm_readv(...) \ |
| 35 |
({ (void)lvec; (void)liovcnt; \ |
| 36 |
(void)rvec; (void)riovcnt; \ |
| 37 |
errno = ENOSYS; -1 }) |
| 38 |
#endif |
| 39 |
#endif |
| 40 |
return process_vm_readv(getpid(), lvec, liovcnt, rvec, riovcnt, 0UL); |
| 41 |
} |
| 42 |
|
| 43 |
static inline __must_check ssize_t __self_writev(const struct iovec *lvec, |
| 44 |
unsigned long liovcnt, |
| 45 |
const struct iovec *rvec, |
| 46 |
unsigned long riovcnt) |
| 47 |
{ |
| 48 |
#if !__GLIBC_PREREQ(2, 15) |
| 49 |
#ifdef __NR_process_vm_writev |
| 50 |
#define process_vm_writev(...) \ |
| 51 |
syscall(__NR_process_vm_writev, __VA_ARGS__) |
| 52 |
#else |
| 53 |
#define process_vm_writev(...) \ |
| 54 |
({ (void)lvec; (void)liovcnt; \ |
| 55 |
(void)rvec; (void)riovcnt; \ |
| 56 |
errno = ENOSYS; -1 }) |
| 57 |
#endif |
| 58 |
#endif |
| 59 |
return process_vm_writev(getpid(), lvec, liovcnt, rvec, riovcnt, 0UL); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
static int _stp_copy_init(void) |
| 64 |
{ |
| 65 |
/* Try a no-op process_vm_readv/writev to make sure they're available, |
| 66 |
* esp. not ENOSYS, then we don't need to bother /proc/self/mem. */ |
| 67 |
if ((__self_readv(NULL, 0, NULL, 0) == 0) && |
| 68 |
(__self_writev(NULL, 0, NULL, 0) == 0)) |
| 69 |
return 0; |
| 70 |
|
| 71 |
_stp_mem_fd = open("/proc/self/mem", O_RDWR /*| O_LARGEFILE*/); |
| 72 |
if (_stp_mem_fd < 0) |
| 73 |
return -errno; |
| 74 |
fcntl(_stp_mem_fd, F_SETFD, FD_CLOEXEC); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static void _stp_copy_destroy(void) |
| 79 |
{ |
| 80 |
if (_stp_mem_fd >= 0) { |
| 81 |
close (_stp_mem_fd); |
| 82 |
_stp_mem_fd = -1; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
static inline __must_check long __copy_from_user(void *to, |
| 88 |
const void __user * from, unsigned long n) |
| 89 |
{ |
| 90 |
int rc = 0; |
| 91 |
|
| 92 |
if (_stp_mem_fd >= 0) { |
| 93 |
/* pread is like lseek+read, without racing other threads. */ |
| 94 |
if (pread(_stp_mem_fd, to, n, (off_t)(uintptr_t)from) != n) |
| 95 |
rc = -EFAULT; |
| 96 |
} else { |
| 97 |
struct iovec lvec = { .iov_base = to, .iov_len = n }; |
| 98 |
struct iovec rvec = { .iov_base = (void *)from, .iov_len = n }; |
| 99 |
if (__self_readv(&lvec, 1, &rvec, 1) != n) |
| 100 |
rc = -EFAULT; |
| 101 |
} |
| 102 |
|
| 103 |
return rc; |
| 104 |
} |
| 105 |
|
| 106 |
static inline __must_check long __copy_to_user(void *to, const void *from, |
| 107 |
unsigned long n) |
| 108 |
{ |
| 109 |
int rc = 0; |
| 110 |
|
| 111 |
if (_stp_mem_fd >= 0) { |
| 112 |
/* pwrite is like lseek+write, without racing other threads. */ |
| 113 |
/* NB: some kernels will refuse to write /proc/self/mem */ |
| 114 |
if (pwrite(_stp_mem_fd, from, n, (off_t)(uintptr_t)to) != n) |
| 115 |
rc = -EFAULT; |
| 116 |
} else { |
| 117 |
struct iovec lvec = { .iov_base = (void *)from, .iov_len = n }; |
| 118 |
struct iovec rvec = { .iov_base = to, .iov_len = n }; |
| 119 |
if (__self_writev(&lvec, 1, &rvec, 1) != n) |
| 120 |
rc = -EFAULT; |
| 121 |
} |
| 122 |
|
| 123 |
return rc; |
| 124 |
} |
| 125 |
|
| 126 |
static long |
| 127 |
_stp_strncpy_from_user(char *dst, const char *src, long count) |
| 128 |
{ |
| 129 |
if (count <= 0) |
| 130 |
return -EINVAL; |
| 131 |
|
| 132 |
/* Reads are batched on aligned 4k boundaries, approximately |
| 133 |
* page size, to reduce the number of pread syscalls. It will |
| 134 |
* likely read past the terminating '\0', but shouldn't fault. |
| 135 |
* NB: We shouldn't try to read the entire 'count' at once, in |
| 136 |
* case some small string is already near the end of its page. |
| 137 |
*/ |
| 138 |
long i = 0; |
| 139 |
while (i < count) { |
| 140 |
long n = 0x1000 - ((long)(src + i) & 0xFFF); |
| 141 |
n = min(n, count - i); |
| 142 |
if (__copy_from_user(dst + i, src + i, n)) |
| 143 |
return -EFAULT; |
| 144 |
|
| 145 |
char *dst0 = memchr(dst + i, 0, n); |
| 146 |
if (dst0 != NULL) |
| 147 |
return (dst0 - dst); |
| 148 |
|
| 149 |
i += n; |
| 150 |
} |
| 151 |
|
| 152 |
dst[i - 1] = 0; |
| 153 |
return i - 1; |
| 154 |
} |
| 155 |
|
| 156 |
static unsigned long _stp_copy_from_user(char *dst, const char *src, unsigned long count) |
| 157 |
{ |
| 158 |
if (count && __copy_from_user(dst, src, count) == 0) |
| 159 |
return 0; |
| 160 |
return count; |
| 161 |
} |
| 162 |
|
| 163 |
#endif /* _STAPDYN_COPY_C_ */ |
| 164 |
|