Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 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-2000 Wichert Akkerman <wichert@cistron.nl> |
| 6 | * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org> |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 7 | * Copyright (c) 2016-2018 The strace developers. |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. The name of the author may not be used to endorse or promote products |
| 19 | * derived from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include "defs.h" |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 34 | #include "print_fields.h" |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 35 | #include "msghdr.h" |
Dmitry V. Levin | 8da9668 | 2016-07-15 22:58:48 +0000 | [diff] [blame] | 36 | #include <limits.h> |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 37 | #include <arpa/inet.h> |
| 38 | #include <netinet/in.h> |
| 39 | |
| 40 | #include "xlat/msg_flags.h" |
| 41 | #include "xlat/scmvals.h" |
| 42 | #include "xlat/ip_cmsg_types.h" |
| 43 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 44 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 45 | struct cmsghdr32 { |
| 46 | uint32_t cmsg_len; |
| 47 | int cmsg_level; |
| 48 | int cmsg_type; |
| 49 | }; |
| 50 | #endif |
| 51 | |
| 52 | typedef union { |
| 53 | char *ptr; |
| 54 | struct cmsghdr *cmsg; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 55 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 56 | struct cmsghdr32 *cmsg32; |
| 57 | #endif |
| 58 | } union_cmsghdr; |
| 59 | |
| 60 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 61 | print_scm_rights(struct tcb *tcp, const void *cmsg_data, |
| 62 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 63 | { |
| 64 | const int *fds = cmsg_data; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 65 | const unsigned int nfds = data_len / sizeof(*fds); |
| 66 | unsigned int i; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 67 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 68 | tprints("["); |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 69 | |
| 70 | for (i = 0; i < nfds; ++i) { |
| 71 | if (i) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 72 | tprints(", "); |
Dmitry V. Levin | d35d5ec | 2016-07-02 21:14:26 +0000 | [diff] [blame] | 73 | if (abbrev(tcp) && i >= max_strlen) { |
| 74 | tprints("..."); |
| 75 | break; |
| 76 | } |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 77 | printfd(tcp, fds[i]); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 78 | } |
Dmitry V. Levin | 029d979 | 2016-06-30 22:20:56 +0000 | [diff] [blame] | 79 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 80 | tprints("]"); |
| 81 | } |
| 82 | |
| 83 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 84 | print_scm_creds(struct tcb *tcp, const void *cmsg_data, |
| 85 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 86 | { |
| 87 | const struct ucred *uc = cmsg_data; |
| 88 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 89 | PRINT_FIELD_U("{", *uc, pid); |
| 90 | PRINT_FIELD_UID(", ", *uc, uid); |
| 91 | PRINT_FIELD_UID(", ", *uc, gid); |
| 92 | tprints("}"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void |
| 96 | print_scm_security(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 97 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 98 | { |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 99 | print_quoted_string(cmsg_data, data_len, 0); |
| 100 | } |
| 101 | |
| 102 | static void |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 103 | print_scm_timestamp(struct tcb *tcp, const void *cmsg_data, |
| 104 | const unsigned int data_len) |
| 105 | { |
| 106 | print_struct_timeval_data_size(cmsg_data, data_len); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | print_scm_timestampns(struct tcb *tcp, const void *cmsg_data, |
| 111 | const unsigned int data_len) |
| 112 | { |
| 113 | print_struct_timespec_data_size(cmsg_data, data_len); |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | print_scm_timestamping(struct tcb *tcp, const void *cmsg_data, |
| 118 | const unsigned int data_len) |
| 119 | { |
| 120 | print_struct_timespec_array_data_size(cmsg_data, 3, data_len); |
| 121 | } |
| 122 | |
| 123 | static void |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 124 | print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 125 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 126 | { |
| 127 | const struct in_pktinfo *info = cmsg_data; |
| 128 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 129 | PRINT_FIELD_IFINDEX("{", *info, ipi_ifindex); |
| 130 | PRINT_FIELD_INET4_ADDR(", ", *info, ipi_spec_dst); |
| 131 | PRINT_FIELD_INET4_ADDR(", ", *info, ipi_addr); |
| 132 | tprints("}"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 136 | print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, |
| 137 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 138 | { |
Dmitry V. Levin | b7937bd | 2016-06-30 22:39:02 +0000 | [diff] [blame] | 139 | const unsigned int *p = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 140 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 141 | tprintf("[%u]", *p); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 145 | print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 146 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 147 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 148 | const uint8_t *p = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 149 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 150 | tprintf("[%#x]", *p); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 154 | print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 155 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 156 | { |
| 157 | const unsigned char *opts = cmsg_data; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 158 | unsigned int i; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 159 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 160 | tprints("["); |
Dmitry V. Levin | 2a89737 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 161 | for (i = 0; i < data_len; ++i) { |
| 162 | if (i) |
| 163 | tprints(", "); |
Dmitry V. Levin | 6713582 | 2016-07-02 21:14:48 +0000 | [diff] [blame] | 164 | if (abbrev(tcp) && i >= max_strlen) { |
| 165 | tprints("..."); |
| 166 | break; |
| 167 | } |
Dmitry V. Levin | 2a89737 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 168 | tprintf("0x%02x", opts[i]); |
| 169 | } |
| 170 | tprints("]"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 173 | struct sock_ee { |
| 174 | uint32_t ee_errno; |
| 175 | uint8_t ee_origin; |
| 176 | uint8_t ee_type; |
| 177 | uint8_t ee_code; |
| 178 | uint8_t ee_pad; |
| 179 | uint32_t ee_info; |
| 180 | uint32_t ee_data; |
| 181 | struct sockaddr_in offender; |
| 182 | }; |
| 183 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 184 | static void |
| 185 | print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 186 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 187 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 188 | const struct sock_ee *const err = cmsg_data; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 189 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 190 | PRINT_FIELD_U("{", *err, ee_errno); |
| 191 | PRINT_FIELD_U(", ", *err, ee_origin); |
| 192 | PRINT_FIELD_U(", ", *err, ee_type); |
| 193 | PRINT_FIELD_U(", ", *err, ee_code); |
| 194 | PRINT_FIELD_U(", ", *err, ee_info); |
| 195 | PRINT_FIELD_U(", ", *err, ee_data); |
| 196 | PRINT_FIELD_SOCKADDR(", ", *err, offender); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 197 | tprints("}"); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 202 | const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 203 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 204 | const unsigned int addr_len = |
Dmitry V. Levin | dfeea69 | 2016-06-30 22:26:35 +0000 | [diff] [blame] | 205 | data_len > sizeof(struct sockaddr_storage) |
| 206 | ? sizeof(struct sockaddr_storage) : data_len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 207 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 208 | print_sockaddr(cmsg_data, addr_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 211 | typedef void (* const cmsg_printer)(struct tcb *, const void *, unsigned int); |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 212 | |
| 213 | static const struct { |
| 214 | const cmsg_printer printer; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 215 | const unsigned int min_len; |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 216 | } cmsg_socket_printers[] = { |
| 217 | [SCM_RIGHTS] = { print_scm_rights, sizeof(int) }, |
| 218 | [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) }, |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 219 | [SCM_SECURITY] = { print_scm_security, 1 }, |
| 220 | [SCM_TIMESTAMP] = { print_scm_timestamp, 1 }, |
| 221 | [SCM_TIMESTAMPNS] = { print_scm_timestampns, 1 }, |
| 222 | [SCM_TIMESTAMPING] = { print_scm_timestamping, 1 } |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 223 | }, cmsg_ip_printers[] = { |
| 224 | [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) }, |
| 225 | [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) }, |
| 226 | [IP_TOS] = { print_cmsg_uint8_t, 1 }, |
| 227 | [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 }, |
| 228 | [IP_RETOPTS] = { print_cmsg_ip_opts, 1 }, |
| 229 | [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) }, |
| 230 | [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) }, |
| 231 | [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) }, |
| 232 | [SCM_SECURITY] = { print_scm_security, 1 } |
| 233 | }; |
| 234 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 235 | static void |
| 236 | print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 237 | const void *cmsg_data, const unsigned int data_len) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 238 | { |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 239 | const unsigned int utype = cmsg_type; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 240 | switch (cmsg_level) { |
| 241 | case SOL_SOCKET: |
| 242 | printxval(scmvals, cmsg_type, "SCM_???"); |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 243 | if (utype < ARRAY_SIZE(cmsg_socket_printers) |
| 244 | && cmsg_socket_printers[utype].printer |
| 245 | && data_len >= cmsg_socket_printers[utype].min_len) { |
| 246 | tprints(", cmsg_data="); |
| 247 | cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 248 | } |
| 249 | break; |
| 250 | case SOL_IP: |
| 251 | printxval(ip_cmsg_types, cmsg_type, "IP_???"); |
Dmitry V. Levin | df57a9b | 2016-06-30 22:49:36 +0000 | [diff] [blame] | 252 | if (utype < ARRAY_SIZE(cmsg_ip_printers) |
| 253 | && cmsg_ip_printers[utype].printer |
| 254 | && data_len >= cmsg_ip_printers[utype].min_len) { |
| 255 | tprints(", cmsg_data="); |
| 256 | cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
| 259 | default: |
Dmitry V. Levin | 6c30aec | 2016-06-30 22:14:51 +0000 | [diff] [blame] | 260 | tprintf("%#x", cmsg_type); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 264 | static unsigned int |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 265 | get_optmem_max(struct tcb *tcp) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 266 | { |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 267 | static int optmem_max; |
| 268 | |
| 269 | if (!optmem_max) { |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 270 | if (read_int_from_file(tcp, "/proc/sys/net/core/optmem_max", |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 271 | &optmem_max) || optmem_max <= 0) { |
Dmitry V. Levin | 8da9668 | 2016-07-15 22:58:48 +0000 | [diff] [blame] | 272 | optmem_max = sizeof(long long) * (2 * IOV_MAX + 512); |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 273 | } else { |
| 274 | optmem_max = (optmem_max + sizeof(long long) - 1) |
| 275 | & ~(sizeof(long long) - 1); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return optmem_max; |
| 280 | } |
| 281 | |
| 282 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 283 | decode_msg_control(struct tcb *const tcp, const kernel_ulong_t addr, |
| 284 | const kernel_ulong_t in_control_len) |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 285 | { |
| 286 | if (!in_control_len) |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 287 | return; |
| 288 | tprints(", msg_control="); |
| 289 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 290 | const unsigned int cmsg_size = |
| 291 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 292 | (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) : |
| 293 | #endif |
| 294 | sizeof(struct cmsghdr); |
| 295 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 296 | unsigned int control_len = in_control_len > get_optmem_max(tcp) |
| 297 | ? get_optmem_max(tcp) : in_control_len; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 298 | unsigned int buf_len = control_len; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 299 | char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len); |
| 300 | if (!buf || umoven(tcp, addr, buf_len, buf) < 0) { |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 301 | printaddr(addr); |
| 302 | free(buf); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | union_cmsghdr u = { .ptr = buf }; |
| 307 | |
| 308 | tprints("["); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 309 | while (buf_len >= cmsg_size) { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 310 | const kernel_ulong_t cmsg_len = |
| 311 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 312 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len : |
| 313 | #endif |
| 314 | u.cmsg->cmsg_len; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 315 | const int cmsg_level = |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 316 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 317 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level : |
| 318 | #endif |
| 319 | u.cmsg->cmsg_level; |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 320 | const int cmsg_type = |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 321 | #ifndef current_wordsize |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 322 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type : |
| 323 | #endif |
| 324 | u.cmsg->cmsg_type; |
| 325 | |
| 326 | if (u.ptr != buf) |
| 327 | tprints(", "); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 328 | tprintf("{cmsg_len=%" PRI_klu ", cmsg_level=", cmsg_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 329 | printxval(socketlayers, cmsg_level, "SOL_???"); |
| 330 | tprints(", cmsg_type="); |
| 331 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 332 | kernel_ulong_t len = cmsg_len > buf_len ? buf_len : cmsg_len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 333 | |
| 334 | print_cmsg_type_data(tcp, cmsg_level, cmsg_type, |
| 335 | (const void *) (u.ptr + cmsg_size), |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 336 | len > cmsg_size ? len - cmsg_size : 0); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 337 | tprints("}"); |
| 338 | |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 339 | if (len < cmsg_size) { |
| 340 | buf_len -= cmsg_size; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 341 | break; |
| 342 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 343 | len = (cmsg_len + current_wordsize - 1) & |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 344 | ~((kernel_ulong_t) current_wordsize - 1); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 345 | if (len >= buf_len) { |
| 346 | buf_len = 0; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 349 | u.ptr += len; |
| 350 | buf_len -= len; |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 351 | } |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 352 | if (buf_len) { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 353 | tprints(", ..."); |
| 354 | printaddr_comment(addr + (control_len - buf_len)); |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 355 | } else if (control_len < in_control_len) { |
| 356 | tprints(", ..."); |
Dmitry V. Levin | 1d53210 | 2016-06-30 22:34:27 +0000 | [diff] [blame] | 357 | } |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 358 | tprints("]"); |
| 359 | free(buf); |
| 360 | } |
| 361 | |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 362 | void |
| 363 | print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg, |
| 364 | const int *const p_user_msg_namelen, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 365 | const kernel_ulong_t data_size) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 366 | { |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 367 | const int msg_namelen = |
| 368 | p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen |
| 369 | ? *p_user_msg_namelen : (int) msg->msg_namelen; |
Fabien Siron | 2850f74 | 2016-07-06 15:49:22 +0000 | [diff] [blame] | 370 | |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 371 | tprints("{msg_name="); |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 372 | const int family = |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 373 | decode_sockaddr(tcp, ptr_to_kulong(msg->msg_name), msg_namelen); |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 374 | const enum iov_decode decode = |
| 375 | (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR; |
| 376 | |
| 377 | tprints(", msg_namelen="); |
| 378 | if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen) |
| 379 | tprintf("%d->", *p_user_msg_namelen); |
| 380 | tprintf("%d", msg->msg_namelen); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 381 | |
| 382 | tprints(", msg_iov="); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 383 | tprint_iov_upto(tcp, msg->msg_iovlen, |
| 384 | ptr_to_kulong(msg->msg_iov), decode, data_size); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 385 | PRINT_FIELD_U(", ", *msg, msg_iovlen); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 386 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 387 | decode_msg_control(tcp, ptr_to_kulong(msg->msg_control), |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 388 | msg->msg_controllen); |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 389 | PRINT_FIELD_U(", ", *msg, msg_controllen); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 390 | |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 391 | PRINT_FIELD_FLAGS(", ", *msg, msg_flags, msg_flags, "MSG_???"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 392 | tprints("}"); |
| 393 | } |
| 394 | |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 395 | static bool |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 396 | fetch_msghdr_namelen(struct tcb *const tcp, const kernel_ulong_t addr, |
| 397 | int *const p_msg_namelen) |
Dmitry V. Levin | d8f77cd | 2016-07-13 21:56:16 +0000 | [diff] [blame] | 398 | { |
| 399 | struct msghdr msg; |
| 400 | |
| 401 | if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) { |
| 402 | *p_msg_namelen = msg.msg_namelen; |
| 403 | return true; |
| 404 | } else { |
| 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 409 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 410 | decode_msghdr(struct tcb *const tcp, const int *const p_user_msg_namelen, |
| 411 | const kernel_ulong_t addr, const kernel_ulong_t data_size) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 412 | { |
| 413 | struct msghdr msg; |
| 414 | |
| 415 | if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 416 | print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 417 | else |
| 418 | printaddr(addr); |
| 419 | } |
| 420 | |
| 421 | void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 422 | dumpiov_in_msghdr(struct tcb *const tcp, const kernel_ulong_t addr, |
| 423 | const kernel_ulong_t data_size) |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 424 | { |
| 425 | struct msghdr msg; |
| 426 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 427 | if (fetch_struct_msghdr(tcp, addr, &msg)) { |
| 428 | dumpiov_upto(tcp, msg.msg_iovlen, |
| 429 | ptr_to_kulong(msg.msg_iov), data_size); |
| 430 | } |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 431 | } |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 432 | |
| 433 | SYS_FUNC(sendmsg) |
| 434 | { |
| 435 | printfd(tcp, tcp->u_arg[0]); |
| 436 | tprints(", "); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 437 | decode_msghdr(tcp, 0, tcp->u_arg[1], -1); |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 438 | /* flags */ |
| 439 | tprints(", "); |
| 440 | printflags(msg_flags, tcp->u_arg[2], "MSG_???"); |
| 441 | |
| 442 | return RVAL_DECODED; |
| 443 | } |
| 444 | |
| 445 | SYS_FUNC(recvmsg) |
| 446 | { |
| 447 | int msg_namelen; |
| 448 | |
| 449 | if (entering(tcp)) { |
| 450 | printfd(tcp, tcp->u_arg[0]); |
| 451 | tprints(", "); |
| 452 | if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) { |
Dmitry V. Levin | b759d27 | 2016-07-15 16:08:19 +0000 | [diff] [blame] | 453 | set_tcb_priv_ulong(tcp, msg_namelen); |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 454 | return 0; |
| 455 | } |
| 456 | printaddr(tcp->u_arg[1]); |
| 457 | } else { |
Dmitry V. Levin | b759d27 | 2016-07-15 16:08:19 +0000 | [diff] [blame] | 458 | msg_namelen = get_tcb_priv_ulong(tcp); |
Dmitry V. Levin | dc84fa3 | 2016-07-14 22:26:28 +0000 | [diff] [blame] | 459 | |
| 460 | if (syserror(tcp)) |
| 461 | tprintf("{msg_namelen=%d}", msg_namelen); |
| 462 | else |
| 463 | decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1], |
| 464 | tcp->u_rval); |
| 465 | } |
| 466 | |
| 467 | /* flags */ |
| 468 | tprints(", "); |
| 469 | printflags(msg_flags, tcp->u_arg[2], "MSG_???"); |
| 470 | |
| 471 | return RVAL_DECODED; |
| 472 | } |