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> |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. The name of the author may not be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "defs.h" |
| 33 | #include "msghdr.h" |
| 34 | #include <arpa/inet.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
| 37 | #include "xlat/msg_flags.h" |
| 38 | #include "xlat/scmvals.h" |
| 39 | #include "xlat/ip_cmsg_types.h" |
| 40 | |
| 41 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 42 | struct cmsghdr32 { |
| 43 | uint32_t cmsg_len; |
| 44 | int cmsg_level; |
| 45 | int cmsg_type; |
| 46 | }; |
| 47 | #endif |
| 48 | |
| 49 | typedef union { |
| 50 | char *ptr; |
| 51 | struct cmsghdr *cmsg; |
| 52 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 53 | struct cmsghdr32 *cmsg32; |
| 54 | #endif |
| 55 | } union_cmsghdr; |
| 56 | |
| 57 | static void |
| 58 | print_scm_rights(struct tcb *tcp, const void *cmsg_data, |
| 59 | const size_t data_len) |
| 60 | { |
| 61 | const int *fds = cmsg_data; |
| 62 | const char *end = (const char *) cmsg_data + data_len; |
| 63 | bool seen = false; |
| 64 | |
| 65 | if (sizeof(*fds) > data_len) |
| 66 | return; |
| 67 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 68 | tprints(", cmsg_data=["); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 69 | while ((const char *) fds < end) { |
| 70 | if (seen) |
| 71 | tprints(", "); |
| 72 | else |
| 73 | seen = true; |
| 74 | printfd(tcp, *fds++); |
| 75 | } |
| 76 | tprints("]"); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | print_scm_creds(struct tcb *tcp, const void *cmsg_data, |
| 81 | const size_t data_len) |
| 82 | { |
| 83 | const struct ucred *uc = cmsg_data; |
| 84 | |
| 85 | if (sizeof(*uc) > data_len) |
| 86 | return; |
| 87 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 88 | tprintf(", cmsg_data={pid=%u, uid=%u, gid=%u}", |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 89 | (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | print_scm_security(struct tcb *tcp, const void *cmsg_data, |
| 94 | const size_t data_len) |
| 95 | { |
| 96 | if (!data_len) |
| 97 | return; |
| 98 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 99 | tprints(", cmsg_data="); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 100 | print_quoted_string(cmsg_data, data_len, 0); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data, |
| 105 | const size_t data_len) |
| 106 | { |
| 107 | const struct in_pktinfo *info = cmsg_data; |
| 108 | |
| 109 | if (sizeof(*info) > data_len) |
| 110 | return; |
| 111 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 112 | tprints(", cmsg_data={ipi_ifindex="); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 113 | print_ifindex(info->ipi_ifindex); |
| 114 | tprintf(", ipi_spec_dst=inet_addr(\"%s\"), ipi_addr=inet_addr(\"%s\")}", |
| 115 | inet_ntoa(info->ipi_spec_dst), inet_ntoa(info->ipi_addr)); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data, |
| 120 | const size_t data_len) |
| 121 | { |
| 122 | const unsigned int *ttl = cmsg_data; |
| 123 | |
| 124 | if (sizeof(*ttl) > data_len) |
| 125 | return; |
| 126 | |
Dmitry V. Levin | 17e624c | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 127 | tprintf(", cmsg_data=[%u]", *ttl); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void |
| 131 | print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data, |
| 132 | const size_t data_len) |
| 133 | { |
| 134 | const uint8_t *tos = cmsg_data; |
| 135 | |
| 136 | if (sizeof(*tos) > data_len) |
| 137 | return; |
| 138 | |
Dmitry V. Levin | 17e624c | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 139 | tprintf(", cmsg_data=[%#x]", *tos); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static void |
| 143 | print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data, |
| 144 | const size_t data_len) |
| 145 | { |
| 146 | const uint32_t *csum = cmsg_data; |
| 147 | |
| 148 | if (sizeof(*csum) > data_len) |
| 149 | return; |
| 150 | |
Dmitry V. Levin | 17e624c | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 151 | tprintf(", cmsg_data=[%u]", *csum); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void |
| 155 | print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data, |
| 156 | const size_t data_len) |
| 157 | { |
| 158 | const unsigned char *opts = cmsg_data; |
| 159 | size_t i; |
| 160 | |
| 161 | if (!data_len) |
| 162 | return; |
| 163 | |
Dmitry V. Levin | 2a89737 | 2016-06-28 22:25:10 +0000 | [diff] [blame^] | 164 | tprints(", cmsg_data=["); |
| 165 | for (i = 0; i < data_len; ++i) { |
| 166 | if (i) |
| 167 | tprints(", "); |
| 168 | tprintf("0x%02x", opts[i]); |
| 169 | } |
| 170 | tprints("]"); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void |
| 174 | print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data, |
| 175 | const size_t data_len) |
| 176 | { |
| 177 | const struct { |
| 178 | uint32_t ee_errno; |
| 179 | uint8_t ee_origin; |
| 180 | uint8_t ee_type; |
| 181 | uint8_t ee_code; |
| 182 | uint8_t ee_pad; |
| 183 | uint32_t ee_info; |
| 184 | uint32_t ee_data; |
| 185 | struct sockaddr_in offender; |
| 186 | } *err = cmsg_data; |
| 187 | |
| 188 | if (sizeof(*err) > data_len) |
| 189 | return; |
| 190 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 191 | tprintf(", cmsg_data={ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u" |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 192 | ", ee_info=%u, ee_data=%u, offender=", |
| 193 | err->ee_errno, err->ee_origin, err->ee_type, |
| 194 | err->ee_code, err->ee_info, err->ee_data); |
| 195 | print_sockaddr(tcp, &err->offender, sizeof(err->offender)); |
| 196 | tprints("}"); |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data, |
| 201 | const size_t data_len) |
| 202 | { |
| 203 | if (sizeof(struct sockaddr_in) > data_len) |
| 204 | return; |
| 205 | |
Dmitry V. Levin | 08c0960 | 2016-06-28 22:25:10 +0000 | [diff] [blame] | 206 | tprints(", cmsg_data="); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 207 | print_sockaddr(tcp, cmsg_data, data_len); |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type, |
| 212 | const void *cmsg_data, const size_t data_len) |
| 213 | { |
| 214 | switch (cmsg_level) { |
| 215 | case SOL_SOCKET: |
| 216 | printxval(scmvals, cmsg_type, "SCM_???"); |
| 217 | switch (cmsg_type) { |
| 218 | case SCM_RIGHTS: |
| 219 | print_scm_rights(tcp, cmsg_data, data_len); |
| 220 | break; |
| 221 | case SCM_CREDENTIALS: |
| 222 | print_scm_creds(tcp, cmsg_data, data_len); |
| 223 | break; |
| 224 | case SCM_SECURITY: |
| 225 | print_scm_security(tcp, cmsg_data, data_len); |
| 226 | break; |
| 227 | } |
| 228 | break; |
| 229 | case SOL_IP: |
| 230 | printxval(ip_cmsg_types, cmsg_type, "IP_???"); |
| 231 | switch (cmsg_type) { |
| 232 | case IP_PKTINFO: |
| 233 | print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len); |
| 234 | break; |
| 235 | case IP_TTL: |
| 236 | print_cmsg_ip_ttl(tcp, cmsg_data, data_len); |
| 237 | break; |
| 238 | case IP_TOS: |
| 239 | print_cmsg_ip_tos(tcp, cmsg_data, data_len); |
| 240 | break; |
| 241 | case IP_RECVOPTS: |
| 242 | case IP_RETOPTS: |
| 243 | print_cmsg_ip_opts(tcp, cmsg_data, data_len); |
| 244 | break; |
| 245 | case IP_RECVERR: |
| 246 | print_cmsg_ip_recverr(tcp, cmsg_data, data_len); |
| 247 | break; |
| 248 | case IP_ORIGDSTADDR: |
| 249 | print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len); |
| 250 | break; |
| 251 | case IP_CHECKSUM: |
| 252 | print_cmsg_ip_checksum(tcp, cmsg_data, data_len); |
| 253 | break; |
| 254 | case SCM_SECURITY: |
| 255 | print_scm_security(tcp, cmsg_data, data_len); |
| 256 | break; |
| 257 | } |
| 258 | break; |
| 259 | default: |
| 260 | tprintf("%u", cmsg_type); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | decode_msg_control(struct tcb *tcp, unsigned long addr, size_t len) |
| 266 | { |
| 267 | const size_t cmsg_size = |
| 268 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 269 | (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) : |
| 270 | #endif |
| 271 | sizeof(struct cmsghdr); |
| 272 | |
| 273 | if (!len) |
| 274 | return; |
| 275 | tprints(", msg_control="); |
| 276 | |
| 277 | char *buf = len < cmsg_size ? NULL : malloc(len); |
| 278 | if (!buf || umoven(tcp, addr, len, buf) < 0) { |
| 279 | printaddr(addr); |
| 280 | free(buf); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | union_cmsghdr u = { .ptr = buf }; |
| 285 | |
| 286 | tprints("["); |
| 287 | while (len >= cmsg_size) { |
| 288 | size_t cmsg_len = |
| 289 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 290 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len : |
| 291 | #endif |
| 292 | u.cmsg->cmsg_len; |
| 293 | int cmsg_level = |
| 294 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 295 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level : |
| 296 | #endif |
| 297 | u.cmsg->cmsg_level; |
| 298 | int cmsg_type = |
| 299 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
| 300 | (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type : |
| 301 | #endif |
| 302 | u.cmsg->cmsg_type; |
| 303 | |
| 304 | if (u.ptr != buf) |
| 305 | tprints(", "); |
| 306 | tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len); |
| 307 | printxval(socketlayers, cmsg_level, "SOL_???"); |
| 308 | tprints(", cmsg_type="); |
| 309 | |
| 310 | if (cmsg_len > len) |
| 311 | cmsg_len = len; |
| 312 | |
| 313 | print_cmsg_type_data(tcp, cmsg_level, cmsg_type, |
| 314 | (const void *) (u.ptr + cmsg_size), |
| 315 | cmsg_len > cmsg_size ? cmsg_len - cmsg_size: 0); |
| 316 | tprints("}"); |
| 317 | |
| 318 | if (cmsg_len < cmsg_size) { |
| 319 | len -= cmsg_size; |
| 320 | break; |
| 321 | } |
| 322 | cmsg_len = (cmsg_len + current_wordsize - 1) & |
| 323 | (size_t) ~(current_wordsize - 1); |
| 324 | if (cmsg_len >= len) { |
| 325 | len = 0; |
| 326 | break; |
| 327 | } |
| 328 | u.ptr += cmsg_len; |
| 329 | len -= cmsg_len; |
| 330 | } |
| 331 | if (len) |
| 332 | tprints(", ..."); |
| 333 | tprints("]"); |
| 334 | free(buf); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size) |
| 339 | { |
| 340 | tprints("{msg_name="); |
| 341 | decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen); |
| 342 | tprintf(", msg_namelen=%d", msg->msg_namelen); |
| 343 | |
| 344 | tprints(", msg_iov="); |
| 345 | tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen, |
| 346 | (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size); |
| 347 | tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen); |
| 348 | |
| 349 | decode_msg_control(tcp, (unsigned long) msg->msg_control, |
| 350 | msg->msg_controllen); |
| 351 | tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen); |
| 352 | |
| 353 | tprints(", msg_flags="); |
| 354 | printflags(msg_flags, msg->msg_flags, "MSG_???"); |
| 355 | tprints("}"); |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size) |
| 360 | { |
| 361 | struct msghdr msg; |
| 362 | |
| 363 | if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) |
| 364 | print_msghdr(tcp, &msg, data_size); |
| 365 | else |
| 366 | printaddr(addr); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size) |
| 371 | { |
| 372 | struct msghdr msg; |
| 373 | |
| 374 | if (fetch_struct_msghdr(tcp, addr, &msg)) |
| 375 | dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size); |
| 376 | } |
| 377 | |
| 378 | static int |
| 379 | decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len) |
| 380 | { |
| 381 | struct mmsghdr mmsg; |
| 382 | int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg); |
| 383 | |
| 384 | if (fetched) { |
Dmitry V. Levin | a50ec34 | 2016-06-27 00:14:34 +0000 | [diff] [blame] | 385 | tprints("{msg_hdr="); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 386 | print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL); |
Dmitry V. Levin | a50ec34 | 2016-06-27 00:14:34 +0000 | [diff] [blame] | 387 | tprintf(", msg_len=%u}", mmsg.msg_len); |
Dmitry V. Levin | 95cce4f | 2016-06-27 00:02:55 +0000 | [diff] [blame] | 388 | } else { |
| 389 | printaddr(addr); |
| 390 | } |
| 391 | |
| 392 | return fetched; |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len, |
| 397 | bool use_msg_len) |
| 398 | { |
| 399 | if (syserror(tcp)) { |
| 400 | printaddr(addr); |
| 401 | } else { |
| 402 | unsigned int i, fetched; |
| 403 | |
| 404 | tprints("["); |
| 405 | for (i = 0; i < len; ++i, addr += fetched) { |
| 406 | if (i) |
| 407 | tprints(", "); |
| 408 | fetched = decode_mmsghdr(tcp, addr, use_msg_len); |
| 409 | if (!fetched) |
| 410 | break; |
| 411 | } |
| 412 | tprints("]"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void |
| 417 | dumpiov_in_mmsghdr(struct tcb *tcp, long addr) |
| 418 | { |
| 419 | unsigned int len = tcp->u_rval; |
| 420 | unsigned int i, fetched; |
| 421 | struct mmsghdr mmsg; |
| 422 | |
| 423 | for (i = 0; i < len; ++i, addr += fetched) { |
| 424 | fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg); |
| 425 | if (!fetched) |
| 426 | break; |
| 427 | tprintf(" = %lu buffers in vector %u\n", |
| 428 | (unsigned long)mmsg.msg_hdr.msg_iovlen, i); |
| 429 | dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen, |
| 430 | (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len); |
| 431 | } |
| 432 | } |