]> granicus.if.org Git - strace/blobdiff - ucopy.c
nlattr: add UID/GID netlink attribute decoders
[strace] / ucopy.c
diff --git a/ucopy.c b/ucopy.c
index 9796756e53b74e623051c49ea8953ecdbb4de356..dafc1e4ed524061ddac568c6caa884e375bce3c5 100644 (file)
--- a/ucopy.c
+++ b/ucopy.c
@@ -6,7 +6,7 @@
  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  *                     Linux for s390 port by D.J. Barrow
  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
- * Copyright (c) 1999-2017 The strace developers.
+ * Copyright (c) 1999-2018 The strace developers.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -83,7 +83,11 @@ vm_read_mem(const pid_t pid, void *const laddr,
                .iov_len = len
        };
 
-       return process_vm_readv(pid, &local, 1, &remote, 1, 0);
+       const ssize_t rc = process_vm_readv(pid, &local, 1, &remote, 1, 0);
+       if (rc < 0 && errno == ENOSYS)
+               process_vm_readv_not_supported = true;
+
+       return rc;
 }
 
 static bool
@@ -97,63 +101,23 @@ tracee_addr_is_invalid(kernel_ulong_t addr)
 #endif
 }
 
-/*
- * Copy `len' bytes of data from process `pid'
- * at address `addr' to our space at `our_addr'.
- */
-int
-umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
-       void *const our_addr)
+/* legacy method of copying from tracee */
+static int
+umoven_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
+               void *laddr)
 {
-       char *laddr = our_addr;
-       int pid = tcp->pid;
-       unsigned int n, m, nread;
-       union {
-               long val;
-               char x[sizeof(long)];
-       } u;
-
-       if (tracee_addr_is_invalid(addr))
-               return -1;
-
-       if (!process_vm_readv_not_supported) {
-               int r = vm_read_mem(pid, laddr, addr, len);
-               if ((unsigned int) r == len)
-                       return 0;
-               if (r >= 0) {
-                       error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
-                                 (unsigned int) r, len, addr);
-                       return -1;
-               }
-               switch (errno) {
-                       case ENOSYS:
-                               /* never try it again */
-                               process_vm_readv_not_supported = 1;
-                               break;
-                       case EPERM:
-                               /* operation not permitted, try PTRACE_PEEKDATA */
-                               break;
-                       case ESRCH:
-                               /* the process is gone */
-                               return -1;
-                       case EFAULT: case EIO:
-                               /* address space is inaccessible */
-                               return -1;
-                       default:
-                               /* all the rest is strange and should be reported */
-                               perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
-                                           pid, addr);
-                               return -1;
-               }
-       }
+       unsigned int nread = 0;
+       unsigned int residue = addr & (sizeof(long) - 1);
 
-       nread = 0;
-       if (addr & (sizeof(long) - 1)) {
-               /* addr not a multiple of sizeof(long) */
-               n = addr & (sizeof(long) - 1);  /* residue */
+       while (len) {
                addr &= -sizeof(long);          /* aligned address */
+
                errno = 0;
-               u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
+               union {
+                       long val;
+                       char x[sizeof(long)];
+               } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
+
                switch (errno) {
                        case 0:
                                break;
@@ -162,6 +126,10 @@ umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
                                return -1;
                        case EFAULT: case EIO: case EPERM:
                                /* address space is inaccessible */
+                               if (nread) {
+                                       perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
+                                                  nread, nread + len, addr - nread);
+                               }
                                return -1;
                        default:
                                /* all the rest is strange and should be reported */
@@ -169,16 +137,83 @@ umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
                                            pid, addr);
                                return -1;
                }
-               m = MIN(sizeof(long) - n, len);
-               memcpy(laddr, &u.x[n], m);
+
+               unsigned int m = MIN(sizeof(long) - residue, len);
+               memcpy(laddr, &u.x[residue], m);
+               residue = 0;
                addr += sizeof(long);
                laddr += m;
                nread += m;
                len -= m;
        }
+
+       return 0;
+}
+
+/*
+ * Copy `len' bytes of data from process `pid'
+ * at address `addr' to our space at `our_addr'.
+ */
+int
+umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
+       void *const our_addr)
+{
+       if (tracee_addr_is_invalid(addr))
+               return -1;
+
+       const int pid = tcp->pid;
+
+       if (process_vm_readv_not_supported)
+               return umoven_peekdata(pid, addr, len, our_addr);
+
+       int r = vm_read_mem(pid, our_addr, addr, len);
+       if ((unsigned int) r == len)
+               return 0;
+       if (r >= 0) {
+               error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
+                         (unsigned int) r, len, addr);
+               return -1;
+       }
+       switch (errno) {
+               case ENOSYS:
+               case EPERM:
+                       /* try PTRACE_PEEKDATA */
+                       return umoven_peekdata(pid, addr, len, our_addr);
+               case ESRCH:
+                       /* the process is gone */
+                       return -1;
+               case EFAULT: case EIO:
+                       /* address space is inaccessible */
+                       return -1;
+               default:
+                       /* all the rest is strange and should be reported */
+                       perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
+                                   pid, addr);
+                       return -1;
+       }
+}
+
+/*
+ * Like umoven_peekdata but make the additional effort of looking
+ * for a terminating zero byte.
+ */
+static int
+umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len,
+                 void *laddr)
+{
+       unsigned int nread = 0;
+       unsigned int residue = addr & (sizeof(long) - 1);
+       void *const orig_addr = laddr;
+
        while (len) {
+               addr &= -sizeof(long);          /* aligned address */
+
                errno = 0;
-               u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
+               union {
+                       unsigned long val;
+                       char x[sizeof(long)];
+               } u = { .val = ptrace(PTRACE_PEEKDATA, pid, addr, 0) };
+
                switch (errno) {
                        case 0:
                                break;
@@ -188,18 +223,23 @@ umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
                        case EFAULT: case EIO: case EPERM:
                                /* address space is inaccessible */
                                if (nread) {
-                                       perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
+                                       perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
                                                   nread, nread + len, addr - nread);
                                }
                                return -1;
                        default:
                                /* all the rest is strange and should be reported */
-                               perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
-                                           pid, addr);
+                               perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
+                                          pid, addr);
                                return -1;
                }
-               m = MIN(sizeof(long), len);
-               memcpy(laddr, u.x, m);
+
+               unsigned int m = MIN(sizeof(long) - residue, len);
+               memcpy(laddr, &u.x[residue], m);
+               while (residue < sizeof(long))
+                       if (u.x[residue++] == '\0')
+                               return (laddr - orig_addr) + residue;
+               residue = 0;
                addr += sizeof(long);
                laddr += m;
                nread += m;
@@ -213,8 +253,7 @@ umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
  * Like `umove' but make the additional effort of looking
  * for a terminating zero byte.
  *
- * Returns < 0 on error, > 0 if NUL was seen,
- * (TODO if useful: return count of bytes including NUL),
+ * Returns < 0 on error, strlen + 1  if NUL was seen,
  * else 0 if len bytes were read but no NUL byte seen.
  *
  * Note: there is no guarantee we won't overwrite some bytes
@@ -222,144 +261,68 @@ umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
  * we never write past laddr[len-1]).
  */
 int
-umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
+umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
+        char *laddr)
 {
-       const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
-       const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
-
-       int pid = tcp->pid;
-       unsigned int n, m, nread;
-       union {
-               unsigned long val;
-               char x[sizeof(long)];
-       } u;
-
        if (tracee_addr_is_invalid(addr))
                return -1;
 
-       nread = 0;
-       if (!process_vm_readv_not_supported) {
-               const size_t page_size = get_pagesize();
-               const size_t page_mask = page_size - 1;
-
-               while (len > 0) {
-                       unsigned int chunk_len;
-                       unsigned int end_in_page;
-
-                       /*
-                        * Don't cross pages, otherwise we can get EFAULT
-                        * and fail to notice that terminating NUL lies
-                        * in the existing (first) page.
-                        */
-                       chunk_len = len > page_size ? page_size : len;
-                       end_in_page = (addr + chunk_len) & page_mask;
-                       if (chunk_len > end_in_page) /* crosses to the next page */
-                               chunk_len -= end_in_page;
-
-                       int r = vm_read_mem(pid, laddr, addr, chunk_len);
-                       if (r > 0) {
-                               if (memchr(laddr, '\0', r))
-                                       return 1;
-                               addr += r;
-                               laddr += r;
-                               nread += r;
-                               len -= r;
-                               continue;
-                       }
-                       switch (errno) {
-                               case ENOSYS:
-                                       /* never try it again */
-                                       process_vm_readv_not_supported = 1;
-                                       goto vm_readv_didnt_work;
-                               case ESRCH:
-                                       /* the process is gone */
-                                       return -1;
-                               case EPERM:
-                                       /* operation not permitted, try PTRACE_PEEKDATA */
-                                       if (!nread)
-                                               goto vm_readv_didnt_work;
-                                       /* fall through */
-                               case EFAULT: case EIO:
-                                       /* address space is inaccessible */
-                                       if (nread) {
-                                               perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
-                                                          nread, nread + len, addr - nread);
-                                       }
-                                       return -1;
-                               default:
-                                       /* all the rest is strange and should be reported */
-                                       perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
-                                                   pid, addr);
-                                       return -1;
-                       }
-               }
-               return 0;
-       }
- vm_readv_didnt_work:
+       const int pid = tcp->pid;
 
-       if (addr & (sizeof(long) - 1)) {
-               /* addr not a multiple of sizeof(long) */
-               n = addr & (sizeof(long) - 1);  /* residue */
-               addr &= -sizeof(long);          /* aligned address */
-               errno = 0;
-               u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
-               switch (errno) {
-                       case 0:
-                               break;
-                       case ESRCH: case EINVAL:
-                               /* these could be seen if the process is gone */
-                               return -1;
-                       case EFAULT: case EIO: case EPERM:
-                               /* address space is inaccessible */
-                               return -1;
-                       default:
-                               /* all the rest is strange and should be reported */
-                               perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
-                                           pid, addr);
-                               return -1;
-               }
-               m = MIN(sizeof(long) - n, len);
-               memcpy(laddr, &u.x[n], m);
-               while (n & (sizeof(long) - 1))
-                       if (u.x[n++] == '\0')
-                               return 1;
-               addr += sizeof(long);
-               laddr += m;
-               nread += m;
-               len -= m;
-       }
+       if (process_vm_readv_not_supported)
+               return umovestr_peekdata(pid, addr, len, laddr);
+
+       const size_t page_size = get_pagesize();
+       const size_t page_mask = page_size - 1;
+       unsigned int nread = 0;
 
        while (len) {
-               errno = 0;
-               u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
+               /*
+                * Don't cross pages, otherwise we can get EFAULT
+                * and fail to notice that terminating NUL lies
+                * in the existing (first) page.
+                */
+               unsigned int chunk_len = len > page_size ? page_size : len;
+               unsigned int end_in_page = (addr + chunk_len) & page_mask;
+               if (chunk_len > end_in_page) /* crosses to the next page */
+                       chunk_len -= end_in_page;
+
+               int r = vm_read_mem(pid, laddr, addr, chunk_len);
+               if (r > 0) {
+                       char *nul_addr = memchr(laddr, '\0', r);
+
+                       if (nul_addr)
+                               return (nul_addr - laddr) + 1;
+                       addr += r;
+                       laddr += r;
+                       nread += r;
+                       len -= r;
+                       continue;
+               }
                switch (errno) {
-                       case 0:
-                               break;
-                       case ESRCH: case EINVAL:
-                               /* these could be seen if the process is gone */
-                               return -1;
-                       case EFAULT: case EIO: case EPERM:
+                       case ENOSYS:
+                       case EPERM:
+                               /* try PTRACE_PEEKDATA */
+                               if (!nread)
+                                       return umovestr_peekdata(pid, addr,
+                                                                len, laddr);
+                               ATTRIBUTE_FALLTHROUGH;
+                       case EFAULT: case EIO:
                                /* address space is inaccessible */
-                               if (nread) {
+                               if (nread)
                                        perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
                                                   nread, nread + len, addr - nread);
-                               }
+                               return -1;
+                       case ESRCH:
+                               /* the process is gone */
                                return -1;
                        default:
                                /* all the rest is strange and should be reported */
-                               perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
-                                          pid, addr);
+                               perror_msg("process_vm_readv: pid:%d @0x%" PRI_klx,
+                                           pid, addr);
                                return -1;
                }
-               m = MIN(sizeof(long), len);
-               memcpy(laddr, u.x, m);
-               /* "If a NUL char exists in this word" */
-               if ((u.val - x01010101) & ~u.val & x80808080)
-                       return 1;
-               addr += sizeof(long);
-               laddr += m;
-               nread += m;
-               len -= m;
        }
+
        return 0;
 }