]> granicus.if.org Git - strace/blobdiff - msghdr.c
io: handle data_size of -1 as unlimited data in print_iovec
[strace] / msghdr.c
index bd15da00101a7c49374755b52183a4a33905b32a..63f5171457e50d744f66b470ab65af029e357139 100644 (file)
--- a/msghdr.c
+++ b/msghdr.c
@@ -31,6 +31,7 @@
 
 #include "defs.h"
 #include "msghdr.h"
+#include <limits.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 
@@ -55,21 +56,21 @@ typedef union {
 } union_cmsghdr;
 
 static void
-print_scm_rights(struct tcb *tcp, const void *cmsg_data,
-                const size_t data_len)
+print_scm_rights(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
 {
        const int *fds = cmsg_data;
        const size_t nfds = data_len / sizeof(*fds);
        size_t i;
 
-       if (!nfds)
-               return;
-
-       tprints(", cmsg_data=[");
+       tprints("[");
 
        for (i = 0; i < nfds; ++i) {
                if (i)
                        tprints(", ");
+               if (abbrev(tcp) && i >= max_strlen) {
+                       tprints("...");
+                       break;
+               }
                printfd(tcp, fds[i]);
        }
 
@@ -77,15 +78,11 @@ print_scm_rights(struct tcb *tcp, const void *cmsg_data,
 }
 
 static void
-print_scm_creds(struct tcb *tcp, const void *cmsg_data,
-               const size_t data_len)
+print_scm_creds(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
 {
        const struct ucred *uc = cmsg_data;
 
-       if (sizeof(*uc) > data_len)
-               return;
-
-       tprintf(", cmsg_data={pid=%u, uid=%u, gid=%u}",
+       tprintf("{pid=%u, uid=%u, gid=%u}",
                (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
 }
 
@@ -93,10 +90,6 @@ static void
 print_scm_security(struct tcb *tcp, const void *cmsg_data,
                   const size_t data_len)
 {
-       if (!data_len)
-               return;
-
-       tprints(", cmsg_data=");
        print_quoted_string(cmsg_data, data_len, 0);
 }
 
@@ -106,10 +99,7 @@ print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
 {
        const struct in_pktinfo *info = cmsg_data;
 
-       if (sizeof(*info) > data_len)
-               return;
-
-       tprints(", cmsg_data={ipi_ifindex=");
+       tprints("{ipi_ifindex=");
        print_ifindex(info->ipi_ifindex);
        tprintf(", ipi_spec_dst=inet_addr(\"%s\")",
                inet_ntoa(info->ipi_spec_dst));
@@ -118,39 +108,20 @@ print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
 }
 
 static void
-print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
-                 const size_t data_len)
+print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
 {
-       const unsigned int *ttl = cmsg_data;
+       const unsigned int *p = cmsg_data;
 
-       if (sizeof(*ttl) > data_len)
-               return;
-
-       tprintf(", cmsg_data=[%u]", *ttl);
+       tprintf("[%u]", *p);
 }
 
 static void
-print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
-                 const size_t data_len)
+print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
+                  const size_t data_len)
 {
-       const uint8_t *tos = cmsg_data;
-
-       if (sizeof(*tos) > data_len)
-               return;
+       const uint8_t *p = cmsg_data;
 
-       tprintf(", cmsg_data=[%#x]", *tos);
-}
-
-static void
-print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
-                      const size_t data_len)
-{
-       const uint32_t *csum = cmsg_data;
-
-       if (sizeof(*csum) > data_len)
-               return;
-
-       tprintf(", cmsg_data=[%u]", *csum);
+       tprintf("[%#x]", *p);
 }
 
 static void
@@ -160,37 +131,37 @@ print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
        const unsigned char *opts = cmsg_data;
        size_t i;
 
-       if (!data_len)
-               return;
-
-       tprints(", cmsg_data=[");
+       tprints("[");
        for (i = 0; i < data_len; ++i) {
                if (i)
                        tprints(", ");
+               if (abbrev(tcp) && i >= max_strlen) {
+                       tprints("...");
+                       break;
+               }
                tprintf("0x%02x", opts[i]);
        }
        tprints("]");
 }
 
+struct sock_ee {
+       uint32_t ee_errno;
+       uint8_t  ee_origin;
+       uint8_t  ee_type;
+       uint8_t  ee_code;
+       uint8_t  ee_pad;
+       uint32_t ee_info;
+       uint32_t ee_data;
+       struct sockaddr_in offender;
+};
+
 static void
 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
                      const size_t data_len)
 {
-       const struct {
-               uint32_t ee_errno;
-               uint8_t  ee_origin;
-               uint8_t  ee_type;
-               uint8_t  ee_code;
-               uint8_t  ee_pad;
-               uint32_t ee_info;
-               uint32_t ee_data;
-               struct sockaddr_in offender;
-       } *err = cmsg_data;
-
-       if (sizeof(*err) > data_len)
-               return;
+       const struct sock_ee *const err = cmsg_data;
 
-       tprintf(", cmsg_data={ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
+       tprintf("{ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
                ", ee_info=%u, ee_data=%u, offender=",
                err->ee_errno, err->ee_origin, err->ee_type,
                err->ee_code, err->ee_info, err->ee_data);
@@ -206,57 +177,52 @@ print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
                data_len > sizeof(struct sockaddr_storage)
                ? sizeof(struct sockaddr_storage) : data_len;
 
-       tprints(", cmsg_data=");
        print_sockaddr(tcp, cmsg_data, addr_len);
 }
 
+typedef void (* const cmsg_printer)(struct tcb *, const void *, size_t);
+
+static const struct {
+       const cmsg_printer printer;
+       const size_t min_len;
+} cmsg_socket_printers[] = {
+       [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
+       [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
+       [SCM_SECURITY] = { print_scm_security, 1 }
+}, cmsg_ip_printers[] = {
+       [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
+       [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
+       [IP_TOS] = { print_cmsg_uint8_t, 1 },
+       [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
+       [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
+       [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
+       [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
+       [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
+       [SCM_SECURITY] = { print_scm_security, 1 }
+};
+
 static void
 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
                     const void *cmsg_data, const size_t data_len)
 {
+       const unsigned int utype = cmsg_type;
        switch (cmsg_level) {
        case SOL_SOCKET:
                printxval(scmvals, cmsg_type, "SCM_???");
-               switch (cmsg_type) {
-               case SCM_RIGHTS:
-                       print_scm_rights(tcp, cmsg_data, data_len);
-                       break;
-               case SCM_CREDENTIALS:
-                       print_scm_creds(tcp, cmsg_data, data_len);
-                       break;
-               case SCM_SECURITY:
-                       print_scm_security(tcp, cmsg_data, data_len);
-                       break;
+               if (utype < ARRAY_SIZE(cmsg_socket_printers)
+                   && cmsg_socket_printers[utype].printer
+                   && data_len >= cmsg_socket_printers[utype].min_len) {
+                       tprints(", cmsg_data=");
+                       cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
                }
                break;
        case SOL_IP:
                printxval(ip_cmsg_types, cmsg_type, "IP_???");
-               switch (cmsg_type) {
-               case IP_PKTINFO:
-                       print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
-                       break;
-               case IP_TTL:
-                       print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
-                       break;
-               case IP_TOS:
-                       print_cmsg_ip_tos(tcp, cmsg_data, data_len);
-                       break;
-               case IP_RECVOPTS:
-               case IP_RETOPTS:
-                       print_cmsg_ip_opts(tcp, cmsg_data, data_len);
-                       break;
-               case IP_RECVERR:
-                       print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
-                       break;
-               case IP_ORIGDSTADDR:
-                       print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
-                       break;
-               case IP_CHECKSUM:
-                       print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
-                       break;
-               case SCM_SECURITY:
-                       print_scm_security(tcp, cmsg_data, data_len);
-                       break;
+               if (utype < ARRAY_SIZE(cmsg_ip_printers)
+                   && cmsg_ip_printers[utype].printer
+                   && data_len >= cmsg_ip_printers[utype].min_len) {
+                       tprints(", cmsg_data=");
+                       cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
                }
                break;
        default:
@@ -264,10 +230,29 @@ print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
        }
 }
 
+static unsigned int
+get_optmem_max(void)
+{
+       static int optmem_max;
+
+       if (!optmem_max) {
+               if (read_int_from_file("/proc/sys/net/core/optmem_max",
+                                      &optmem_max) || optmem_max <= 0) {
+                       optmem_max = sizeof(long long) * (2 * IOV_MAX + 512);
+               } else {
+                       optmem_max = (optmem_max + sizeof(long long) - 1)
+                                    & ~(sizeof(long long) - 1);
+               }
+       }
+
+       return optmem_max;
+}
+
 static void
-decode_msg_control(struct tcb *tcp, unsigned long addr, const size_t control_len)
+decode_msg_control(struct tcb *tcp, unsigned long addr,
+                  const size_t in_control_len)
 {
-       if (!control_len)
+       if (!in_control_len)
                return;
        tprints(", msg_control=");
 
@@ -277,6 +262,9 @@ decode_msg_control(struct tcb *tcp, unsigned long addr, const size_t control_len
 #endif
                        sizeof(struct cmsghdr);
 
+       size_t control_len =
+               in_control_len > get_optmem_max()
+               ? get_optmem_max() : in_control_len;
        size_t buf_len = control_len;
        char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
        if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
@@ -334,21 +322,37 @@ decode_msg_control(struct tcb *tcp, unsigned long addr, const size_t control_len
        if (buf_len) {
                tprints(", ");
                printaddr(addr + (control_len - buf_len));
+       } else if (control_len < in_control_len) {
+               tprints(", ...");
        }
        tprints("]");
        free(buf);
 }
 
-static void
-print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
+void
+print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg,
+                   const int *const p_user_msg_namelen,
+                   const unsigned long data_size)
 {
+       const int msg_namelen =
+               p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen
+               ? *p_user_msg_namelen : (int) msg->msg_namelen;
+
        tprints("{msg_name=");
-       decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
-       tprintf(", msg_namelen=%d", msg->msg_namelen);
+       const int family =
+               decode_sockaddr(tcp, (long) msg->msg_name, msg_namelen);
+       const enum iov_decode decode =
+               (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR;
+
+       tprints(", msg_namelen=");
+       if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen)
+               tprintf("%d->", *p_user_msg_namelen);
+       tprintf("%d", msg->msg_namelen);
 
        tprints(", msg_iov=");
+
        tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
-                       (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
+                       (unsigned long) msg->msg_iov, decode, data_size);
        tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
 
        decode_msg_control(tcp, (unsigned long) msg->msg_control,
@@ -360,13 +364,27 @@ print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
        tprints("}");
 }
 
-void
-decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
+static bool
+fetch_msghdr_namelen(struct tcb *tcp, const long addr, int *const p_msg_namelen)
+{
+       struct msghdr msg;
+
+       if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) {
+               *p_msg_namelen = msg.msg_namelen;
+               return true;
+       } else {
+               return false;
+       }
+}
+
+static void
+decode_msghdr(struct tcb *tcp, const int *const p_user_msg_namelen,
+             const long addr, const unsigned long data_size)
 {
        struct msghdr msg;
 
        if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
-               print_msghdr(tcp, &msg, data_size);
+               print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size);
        else
                printaddr(addr);
 }
@@ -380,58 +398,43 @@ dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
                dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
 }
 
-static int
-decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
+SYS_FUNC(sendmsg)
 {
-       struct mmsghdr mmsg;
-       int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
-
-       if (fetched) {
-               tprints("{msg_hdr=");
-               print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
-               tprintf(", msg_len=%u}", mmsg.msg_len);
-       } else {
-               printaddr(addr);
-       }
-
-       return fetched;
+       printfd(tcp, tcp->u_arg[0]);
+       tprints(", ");
+       decode_msghdr(tcp, 0, tcp->u_arg[1], (unsigned long) -1L);
+       /* flags */
+       tprints(", ");
+       printflags(msg_flags, tcp->u_arg[2], "MSG_???");
+
+       return RVAL_DECODED;
 }
 
-void
-decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
-              bool use_msg_len)
+SYS_FUNC(recvmsg)
 {
-       if (syserror(tcp)) {
-               printaddr(addr);
-       } else {
-               unsigned int i, fetched;
-
-               tprints("[");
-               for (i = 0; i < len; ++i, addr += fetched) {
-                       if (i)
-                               tprints(", ");
-                       fetched = decode_mmsghdr(tcp, addr, use_msg_len);
-                       if (!fetched)
-                               break;
+       int msg_namelen;
+
+       if (entering(tcp)) {
+               printfd(tcp, tcp->u_arg[0]);
+               tprints(", ");
+               if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) {
+                       set_tcb_priv_ulong(tcp, msg_namelen);
+                       return 0;
                }
-               tprints("]");
+               printaddr(tcp->u_arg[1]);
+       } else {
+               msg_namelen = get_tcb_priv_ulong(tcp);
+
+               if (syserror(tcp))
+                       tprintf("{msg_namelen=%d}", msg_namelen);
+               else
+                       decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1],
+                                     tcp->u_rval);
        }
-}
 
-void
-dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
-{
-       unsigned int len = tcp->u_rval;
-       unsigned int i, fetched;
-       struct mmsghdr mmsg;
+       /* flags */
+       tprints(", ");
+       printflags(msg_flags, tcp->u_arg[2], "MSG_???");
 
-       for (i = 0; i < len; ++i, addr += fetched) {
-               fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
-               if (!fetched)
-                       break;
-               tprintf(" = %lu buffers in vector %u\n",
-                       (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
-               dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
-                       (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
-       }
+       return RVAL_DECODED;
 }