]> granicus.if.org Git - strace/blob - ucopy.c
ucopy: move legacy fallbacks of umoven and umovestr to separate functions
[strace] / ucopy.c
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-2017 The strace developers.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "defs.h"
36 #include <sys/uio.h>
37 #include <asm/unistd.h>
38
39 #include "scno.h"
40 #include "ptrace.h"
41
42 static bool process_vm_readv_not_supported;
43
44 #ifndef HAVE_PROCESS_VM_READV
45 /*
46  * Need to do this since process_vm_readv() is not yet available in libc.
47  * When libc is updated, only "static bool process_vm_readv_not_supported"
48  * line remains.
49  * The name is different to avoid potential collision with OS headers.
50  */
51 static ssize_t strace_process_vm_readv(pid_t pid,
52                  const struct iovec *lvec,
53                  unsigned long liovcnt,
54                  const struct iovec *rvec,
55                  unsigned long riovcnt,
56                  unsigned long flags)
57 {
58         return syscall(__NR_process_vm_readv,
59                        (long) pid, lvec, liovcnt, rvec, riovcnt, flags);
60 }
61 # define process_vm_readv strace_process_vm_readv
62 #endif /* !HAVE_PROCESS_VM_READV */
63
64 static ssize_t
65 vm_read_mem(const pid_t pid, void *const laddr,
66             const kernel_ulong_t raddr, const size_t len)
67 {
68         const unsigned long truncated_raddr = raddr;
69
70 #if SIZEOF_LONG < SIZEOF_KERNEL_LONG_T
71         if (raddr != (kernel_ulong_t) truncated_raddr) {
72                 errno = EIO;
73                 return -1;
74         }
75 #endif
76
77         const struct iovec local = {
78                 .iov_base = laddr,
79                 .iov_len = len
80         };
81         const struct iovec remote = {
82                 .iov_base = (void *) truncated_raddr,
83                 .iov_len = len
84         };
85
86         const ssize_t rc = process_vm_readv(pid, &local, 1, &remote, 1, 0);
87         if (rc < 0 && errno == ENOSYS)
88                 process_vm_readv_not_supported = true;
89
90         return rc;
91 }
92
93 static bool
94 tracee_addr_is_invalid(kernel_ulong_t addr)
95 {
96         return
97 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
98                 current_wordsize < sizeof(addr) && addr & ~(kernel_ulong_t) -1U;
99 #else
100                 false;
101 #endif
102 }
103
104 /* legacy method of copying from tracee */
105 static int
106 umoven_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
107                 void *laddr)
108 {
109         unsigned int n, m, nread;
110         union {
111                 long val;
112                 char x[sizeof(long)];
113         } u;
114
115         nread = 0;
116         if (addr & (sizeof(long) - 1)) {
117                 /* addr not a multiple of sizeof(long) */
118                 n = addr & (sizeof(long) - 1);  /* residue */
119                 addr &= -sizeof(long);          /* aligned address */
120                 errno = 0;
121                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
122                 switch (errno) {
123                         case 0:
124                                 break;
125                         case ESRCH: case EINVAL:
126                                 /* these could be seen if the process is gone */
127                                 return -1;
128                         case EFAULT: case EIO: case EPERM:
129                                 /* address space is inaccessible */
130                                 return -1;
131                         default:
132                                 /* all the rest is strange and should be reported */
133                                 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
134                                             pid, addr);
135                                 return -1;
136                 }
137                 m = MIN(sizeof(long) - n, len);
138                 memcpy(laddr, &u.x[n], m);
139                 addr += sizeof(long);
140                 laddr += m;
141                 nread += m;
142                 len -= m;
143         }
144         while (len) {
145                 errno = 0;
146                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
147                 switch (errno) {
148                         case 0:
149                                 break;
150                         case ESRCH: case EINVAL:
151                                 /* these could be seen if the process is gone */
152                                 return -1;
153                         case EFAULT: case EIO: case EPERM:
154                                 /* address space is inaccessible */
155                                 if (nread) {
156                                         perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
157                                                    nread, nread + len, addr - nread);
158                                 }
159                                 return -1;
160                         default:
161                                 /* all the rest is strange and should be reported */
162                                 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
163                                             pid, addr);
164                                 return -1;
165                 }
166                 m = MIN(sizeof(long), len);
167                 memcpy(laddr, u.x, m);
168                 addr += sizeof(long);
169                 laddr += m;
170                 nread += m;
171                 len -= m;
172         }
173
174         return 0;
175 }
176
177 /*
178  * Copy `len' bytes of data from process `pid'
179  * at address `addr' to our space at `our_addr'.
180  */
181 int
182 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
183        void *const our_addr)
184 {
185         if (tracee_addr_is_invalid(addr))
186                 return -1;
187
188         const int pid = tcp->pid;
189
190         if (process_vm_readv_not_supported)
191                 return umoven_peekdata(pid, addr, len, our_addr);
192
193         int r = vm_read_mem(pid, our_addr, addr, len);
194         if ((unsigned int) r == len)
195                 return 0;
196         if (r >= 0) {
197                 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
198                           (unsigned int) r, len, addr);
199                 return -1;
200         }
201         switch (errno) {
202                 case ENOSYS:
203                 case EPERM:
204                         /* try PTRACE_PEEKDATA */
205                         return umoven_peekdata(pid, addr, len, our_addr);
206                 case ESRCH:
207                         /* the process is gone */
208                         return -1;
209                 case EFAULT: case EIO:
210                         /* address space is inaccessible */
211                         return -1;
212                 default:
213                         /* all the rest is strange and should be reported */
214                         perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
215                                     pid, addr);
216                         return -1;
217         }
218 }
219
220 /*
221  * Like umoven_peekdata but make the additional effort of looking
222  * for a terminating zero byte.
223  */
224 static int
225 umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
226                   void *laddr)
227 {
228         const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
229         const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
230
231         unsigned int n, m, nread;
232         union {
233                 unsigned long val;
234                 char x[sizeof(long)];
235         } u;
236
237         nread = 0;
238         if (addr & (sizeof(long) - 1)) {
239                 /* addr not a multiple of sizeof(long) */
240                 n = addr & (sizeof(long) - 1);  /* residue */
241                 addr &= -sizeof(long);          /* aligned address */
242                 errno = 0;
243                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
244                 switch (errno) {
245                         case 0:
246                                 break;
247                         case ESRCH: case EINVAL:
248                                 /* these could be seen if the process is gone */
249                                 return -1;
250                         case EFAULT: case EIO: case EPERM:
251                                 /* address space is inaccessible */
252                                 return -1;
253                         default:
254                                 /* all the rest is strange and should be reported */
255                                 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
256                                             pid, addr);
257                                 return -1;
258                 }
259                 m = MIN(sizeof(long) - n, len);
260                 memcpy(laddr, &u.x[n], m);
261                 while (n & (sizeof(long) - 1))
262                         if (u.x[n++] == '\0')
263                                 return 1;
264                 addr += sizeof(long);
265                 laddr += m;
266                 nread += m;
267                 len -= m;
268         }
269
270         while (len) {
271                 errno = 0;
272                 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
273                 switch (errno) {
274                         case 0:
275                                 break;
276                         case ESRCH: case EINVAL:
277                                 /* these could be seen if the process is gone */
278                                 return -1;
279                         case EFAULT: case EIO: case EPERM:
280                                 /* address space is inaccessible */
281                                 if (nread) {
282                                         perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
283                                                    nread, nread + len, addr - nread);
284                                 }
285                                 return -1;
286                         default:
287                                 /* all the rest is strange and should be reported */
288                                 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
289                                            pid, addr);
290                                 return -1;
291                 }
292                 m = MIN(sizeof(long), len);
293                 memcpy(laddr, u.x, m);
294                 /* "If a NUL char exists in this word" */
295                 if ((u.val - x01010101) & ~u.val & x80808080)
296                         return 1;
297                 addr += sizeof(long);
298                 laddr += m;
299                 nread += m;
300                 len -= m;
301         }
302
303         return 0;
304 }
305
306 /*
307  * Like `umove' but make the additional effort of looking
308  * for a terminating zero byte.
309  *
310  * Returns < 0 on error, > 0 if NUL was seen,
311  * (TODO if useful: return count of bytes including NUL),
312  * else 0 if len bytes were read but no NUL byte seen.
313  *
314  * Note: there is no guarantee we won't overwrite some bytes
315  * in laddr[] _after_ terminating NUL (but, of course,
316  * we never write past laddr[len-1]).
317  */
318 int
319 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
320          char *laddr)
321 {
322         if (tracee_addr_is_invalid(addr))
323                 return -1;
324
325         const int pid = tcp->pid;
326
327         if (process_vm_readv_not_supported)
328                 return umovestr_peekdata(pid, addr, len, laddr);
329
330         const size_t page_size = get_pagesize();
331         const size_t page_mask = page_size - 1;
332         unsigned int nread = 0;
333
334         while (len) {
335                 /*
336                  * Don't cross pages, otherwise we can get EFAULT
337                  * and fail to notice that terminating NUL lies
338                  * in the existing (first) page.
339                  */
340                 unsigned int chunk_len = len > page_size ? page_size : len;
341                 unsigned int end_in_page = (addr + chunk_len) & page_mask;
342                 if (chunk_len > end_in_page) /* crosses to the next page */
343                         chunk_len -= end_in_page;
344
345                 int r = vm_read_mem(pid, laddr, addr, chunk_len);
346                 if (r > 0) {
347                         if (memchr(laddr, '\0', r))
348                                 return 1;
349                         addr += r;
350                         laddr += r;
351                         nread += r;
352                         len -= r;
353                         continue;
354                 }
355                 switch (errno) {
356                         case ENOSYS:
357                         case EPERM:
358                                 /* try PTRACE_PEEKDATA */
359                                 if (!nread)
360                                         return umovestr_peekdata(pid, addr,
361                                                                  len, laddr);
362                                 /* fall through */
363                         case EFAULT: case EIO:
364                                 /* address space is inaccessible */
365                                 if (nread)
366                                         perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
367                                                    nread, nread + len, addr - nread);
368                                 return -1;
369                         case ESRCH:
370                                 /* the process is gone */
371                                 return -1;
372                         default:
373                                 /* all the rest is strange and should be reported */
374                                 perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
375                                             pid, addr);
376                                 return -1;
377                 }
378         }
379
380         return 0;
381 }