Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucopy.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
  3. * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
  4. * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
  5. * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Linux for s390 port by D.J. Barrow
  8. * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
  9. * Copyright (c) 1999-2019 The strace developers.
  10. * All rights reserved.
  11. *
  12. * SPDX-License-Identifier: LGPL-2.1-or-later
  13. */
  14. #include "defs.h"
  15. #include <sys/uio.h>
  16. #include "scno.h"
  17. #include "ptrace.h"
  18. static bool process_vm_readv_not_supported;
  19. #ifndef HAVE_PROCESS_VM_READV
  20. /*
  21. * Need to do this since process_vm_readv() is not yet available in libc.
  22. * When libc is updated, only "static bool process_vm_readv_not_supported"
  23. * line remains.
  24. * The name is different to avoid potential collision with OS headers.
  25. */
  26. static ssize_t strace_process_vm_readv(pid_t pid,
  27. const struct iovec *lvec,
  28. unsigned long liovcnt,
  29. const struct iovec *rvec,
  30. unsigned long riovcnt,
  31. unsigned long flags)
  32. {
  33. return syscall(__NR_process_vm_readv,
  34. (long) pid, lvec, liovcnt, rvec, riovcnt, flags);
  35. }
  36. # define process_vm_readv strace_process_vm_readv
  37. #endif /* !HAVE_PROCESS_VM_READV */
  38. static ssize_t
  39. process_read_mem(const pid_t pid, void *const laddr,
  40. void *const raddr, const size_t len)
  41. {
  42. const struct iovec local = {
  43. .iov_base = laddr,
  44. .iov_len = len
  45. };
  46. const struct iovec remote = {
  47. .iov_base = raddr,
  48. .iov_len = len
  49. };
  50. const ssize_t rc = process_vm_readv(pid, &local, 1, &remote, 1, 0);
  51. if (rc < 0 && errno == ENOSYS)
  52. process_vm_readv_not_supported = true;
  53. return rc;
  54. }
  55. static int cached_idx = -1;
  56. static unsigned long cached_raddr[2];
  57. void
  58. invalidate_umove_cache(void)
  59. {
  60. cached_idx = -1;
  61. }
  62. static ssize_t
  63. vm_read_mem(const pid_t pid, void *const laddr,
  64. const kernel_ulong_t raddr, const size_t len)
  65. {
  66. if (!len)
  67. return len;
  68. const unsigned long taddr = raddr;
  69. #if SIZEOF_LONG < SIZEOF_KERNEL_LONG_T
  70. if (raddr != (kernel_ulong_t) taddr) {
  71. errno = EIO;
  72. return -1;
  73. }
  74. #endif
  75. const size_t page_size = get_pagesize();
  76. const size_t page_mask = page_size - 1;
  77. const unsigned long raddr_page_start =
  78. taddr & ~page_mask;
  79. const unsigned long raddr_page_next =
  80. (taddr + len + page_mask) & ~page_mask;
  81. if (!raddr_page_start ||
  82. raddr_page_next < raddr_page_start ||
  83. raddr_page_next - raddr_page_start != page_size)
  84. return process_read_mem(pid, laddr, (void *) taddr, len);
  85. int idx = -1;
  86. if (cached_idx >= 0) {
  87. if (raddr_page_start == cached_raddr[cached_idx])
  88. idx = cached_idx;
  89. else if (raddr_page_start == cached_raddr[!cached_idx])
  90. idx = !cached_idx;
  91. }
  92. static char *buf[2];
  93. if (idx == -1) {
  94. idx = !cached_idx;
  95. if (!buf[idx])
  96. buf[idx] = xmalloc(page_size);
  97. const ssize_t rc =
  98. process_read_mem(pid, buf[idx],
  99. (void *) raddr_page_start, page_size);
  100. if (rc < 0)
  101. return rc;
  102. cached_raddr[idx] = raddr_page_start;
  103. if (cached_idx < 0)
  104. cached_raddr[!idx] = 0;
  105. cached_idx = idx;
  106. }
  107. memcpy(laddr, buf[idx] + (taddr - cached_raddr[idx]), len);
  108. return len;
  109. }
  110. static bool
  111. tracee_addr_is_invalid(kernel_ulong_t addr)
  112. {
  113. return
  114. #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
  115. current_wordsize < sizeof(addr) && addr & ~(kernel_ulong_t) -1U;
  116. #else
  117. false;
  118. #endif
  119. }
  120. /* legacy method of copying from tracee */
  121. static int
  122. umoven_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
  123. void *laddr)
  124. {
  125. unsigned int nread = 0;
  126. unsigned int residue = addr & (sizeof(long) - 1);
  127. while (len) {
  128. addr &= -sizeof(long); /* aligned address */
  129. errno = 0;
  130. union {
  131. long val;
  132. char x[sizeof(long)];
  133. } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
  134. switch (errno) {
  135. case 0:
  136. break;
  137. case ESRCH: case EINVAL:
  138. /* these could be seen if the process is gone */
  139. return -1;
  140. case EFAULT: case EIO: case EPERM:
  141. /* address space is inaccessible */
  142. if (nread) {
  143. perror_func_msg("short read (%u < %u)"
  144. " @0x%" PRI_klx,
  145. nread, nread + len,
  146. addr - nread);
  147. }
  148. return -1;
  149. default:
  150. /* all the rest is strange and should be reported */
  151. perror_func_msg("pid:%d @0x%" PRI_klx,
  152. pid, addr);
  153. return -1;
  154. }
  155. unsigned int m = MIN(sizeof(long) - residue, len);
  156. memcpy(laddr, &u.x[residue], m);
  157. residue = 0;
  158. addr += sizeof(long);
  159. laddr += m;
  160. nread += m;
  161. len -= m;
  162. }
  163. return 0;
  164. }
  165. /*
  166. * Copy `len' bytes of data from process `pid'
  167. * at address `addr' to our space at `our_addr'.
  168. */
  169. int
  170. umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
  171. void *const our_addr)
  172. {
  173. if (tracee_addr_is_invalid(addr))
  174. return -1;
  175. const int pid = tcp->pid;
  176. if (process_vm_readv_not_supported)
  177. return umoven_peekdata(pid, addr, len, our_addr);
  178. int r = vm_read_mem(pid, our_addr, addr, len);
  179. if ((unsigned int) r == len)
  180. return 0;
  181. if (r >= 0) {
  182. error_func_msg("short read (%u < %u) @0x%" PRI_klx,
  183. (unsigned int) r, len, addr);
  184. return -1;
  185. }
  186. switch (errno) {
  187. case ENOSYS:
  188. case EPERM:
  189. /* try PTRACE_PEEKDATA */
  190. return umoven_peekdata(pid, addr, len, our_addr);
  191. case ESRCH:
  192. /* the process is gone */
  193. return -1;
  194. case EFAULT: case EIO:
  195. /* address space is inaccessible */
  196. return -1;
  197. default:
  198. /* all the rest is strange and should be reported */
  199. perror_func_msg("pid:%d @0x%" PRI_klx, pid, addr);
  200. return -1;
  201. }
  202. }
  203. /*
  204. * Like umoven_peekdata but make the additional effort of looking
  205. * for a terminating zero byte.
  206. */
  207. static int
  208. umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
  209. void *laddr)
  210. {
  211. unsigned int nread = 0;
  212. unsigned int residue = addr & (sizeof(long) - 1);
  213. void *const orig_addr = laddr;
  214. while (len) {
  215. addr &= -sizeof(long); /* aligned address */
  216. errno = 0;
  217. union {
  218. unsigned long val;
  219. char x[sizeof(long)];
  220. } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
  221. switch (errno) {
  222. case 0:
  223. break;
  224. case ESRCH: case EINVAL:
  225. /* these could be seen if the process is gone */
  226. return -1;
  227. case EFAULT: case EIO: case EPERM:
  228. /* address space is inaccessible */
  229. if (nread) {
  230. perror_func_msg("short read (%d < %d)"
  231. " @0x%" PRI_klx,
  232. nread, nread + len,
  233. addr - nread);
  234. }
  235. return -1;
  236. default:
  237. /* all the rest is strange and should be reported */
  238. perror_func_msg("pid:%d @0x%" PRI_klx,
  239. pid, addr);
  240. return -1;
  241. }
  242. unsigned int m = MIN(sizeof(long) - residue, len);
  243. memcpy(laddr, &u.x[residue], m);
  244. while (residue < sizeof(long))
  245. if (u.x[residue++] == '\0')
  246. return (laddr - orig_addr) + residue;
  247. residue = 0;
  248. addr += sizeof(long);
  249. laddr += m;
  250. nread += m;
  251. len -= m;
  252. }
  253. return 0;
  254. }
  255. /*
  256. * Like `umove' but make the additional effort of looking
  257. * for a terminating zero byte.
  258. *
  259. * Returns < 0 on error, strlen + 1 if NUL was seen,
  260. * else 0 if len bytes were read but no NUL byte seen.
  261. *
  262. * Note: there is no guarantee we won't overwrite some bytes
  263. * in laddr[] _after_ terminating NUL (but, of course,
  264. * we never write past laddr[len-1]).
  265. */
  266. int
  267. umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
  268. char *laddr)
  269. {
  270. if (tracee_addr_is_invalid(addr))
  271. return -1;
  272. const int pid = tcp->pid;
  273. if (process_vm_readv_not_supported)
  274. return umovestr_peekdata(pid, addr, len, laddr);
  275. const size_t page_size = get_pagesize();
  276. const size_t page_mask = page_size - 1;
  277. unsigned int nread = 0;
  278. while (len) {
  279. /*
  280. * Don't cross pages, otherwise we can get EFAULT
  281. * and fail to notice that terminating NUL lies
  282. * in the existing (first) page.
  283. */
  284. unsigned int chunk_len = len > page_size ? page_size : len;
  285. unsigned int end_in_page = (addr + chunk_len) & page_mask;
  286. if (chunk_len > end_in_page) /* crosses to the next page */
  287. chunk_len -= end_in_page;
  288. int r = vm_read_mem(pid, laddr, addr, chunk_len);
  289. if (r > 0) {
  290. char *nul_addr = memchr(laddr, '\0', r);
  291. if (nul_addr)
  292. return (nul_addr - laddr) + 1;
  293. addr += r;
  294. laddr += r;
  295. nread += r;
  296. len -= r;
  297. continue;
  298. }
  299. switch (errno) {
  300. case ENOSYS:
  301. case EPERM:
  302. /* try PTRACE_PEEKDATA */
  303. if (!nread)
  304. return umovestr_peekdata(pid, addr,
  305. len, laddr);
  306. ATTRIBUTE_FALLTHROUGH;
  307. case EFAULT: case EIO:
  308. /* address space is inaccessible */
  309. if (nread)
  310. perror_func_msg("short read (%d < %d)"
  311. " @0x%" PRI_klx,
  312. nread, nread + len,
  313. addr - nread);
  314. return -1;
  315. case ESRCH:
  316. /* the process is gone */
  317. return -1;
  318. default:
  319. /* all the rest is strange and should be reported */
  320. perror_func_msg("pid:%d @0x%" PRI_klx, pid, addr);
  321. return -1;
  322. }
  323. }
  324. return 0;
  325. }