Home | History | Annotate | Download | only in strace
      1 /*
      2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk (at) cs.few.eur.nl>
      3  * Copyright (c) 1993 Branko Lankester <branko (at) hacktic.nl>
      4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs (at) world.std.com>
      5  * Copyright (c) 1996-2000 Wichert Akkerman <wichert (at) cistron.nl>
      6  * Copyright (c) 2005-2016 Dmitry V. Levin <ldv (at) altlinux.org>
      7  * Copyright (c) 2016-2018 The strace developers.
      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"
     34 #include "print_fields.h"
     35 #include "msghdr.h"
     36 #include <limits.h>
     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 
     44 #ifndef current_wordsize
     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;
     55 #ifndef current_wordsize
     56 	struct cmsghdr32 *cmsg32;
     57 #endif
     58 } union_cmsghdr;
     59 
     60 static void
     61 print_scm_rights(struct tcb *tcp, const void *cmsg_data,
     62 		 const unsigned int data_len)
     63 {
     64 	const int *fds = cmsg_data;
     65 	const unsigned int nfds = data_len / sizeof(*fds);
     66 	unsigned int i;
     67 
     68 	tprints("[");
     69 
     70 	for (i = 0; i < nfds; ++i) {
     71 		if (i)
     72 			tprints(", ");
     73 		if (abbrev(tcp) && i >= max_strlen) {
     74 			tprints("...");
     75 			break;
     76 		}
     77 		printfd(tcp, fds[i]);
     78 	}
     79 
     80 	tprints("]");
     81 }
     82 
     83 static void
     84 print_scm_creds(struct tcb *tcp, const void *cmsg_data,
     85 		const unsigned int data_len)
     86 {
     87 	const struct ucred *uc = cmsg_data;
     88 
     89 	PRINT_FIELD_U("{", *uc, pid);
     90 	PRINT_FIELD_UID(", ", *uc, uid);
     91 	PRINT_FIELD_UID(", ", *uc, gid);
     92 	tprints("}");
     93 }
     94 
     95 static void
     96 print_scm_security(struct tcb *tcp, const void *cmsg_data,
     97 		   const unsigned int data_len)
     98 {
     99 	print_quoted_string(cmsg_data, data_len, 0);
    100 }
    101 
    102 static void
    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
    124 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
    125 		      const unsigned int data_len)
    126 {
    127 	const struct in_pktinfo *info = cmsg_data;
    128 
    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("}");
    133 }
    134 
    135 static void
    136 print_cmsg_uint(struct tcb *tcp, const void *cmsg_data,
    137 		const unsigned int data_len)
    138 {
    139 	const unsigned int *p = cmsg_data;
    140 
    141 	tprintf("[%u]", *p);
    142 }
    143 
    144 static void
    145 print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
    146 		   const unsigned int data_len)
    147 {
    148 	const uint8_t *p = cmsg_data;
    149 
    150 	tprintf("[%#x]", *p);
    151 }
    152 
    153 static void
    154 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
    155 		   const unsigned int data_len)
    156 {
    157 	const unsigned char *opts = cmsg_data;
    158 	unsigned int i;
    159 
    160 	tprints("[");
    161 	for (i = 0; i < data_len; ++i) {
    162 		if (i)
    163 			tprints(", ");
    164 		if (abbrev(tcp) && i >= max_strlen) {
    165 			tprints("...");
    166 			break;
    167 		}
    168 		tprintf("0x%02x", opts[i]);
    169 	}
    170 	tprints("]");
    171 }
    172 
    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 
    184 static void
    185 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
    186 		      const unsigned int data_len)
    187 {
    188 	const struct sock_ee *const err = cmsg_data;
    189 
    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);
    197 	tprints("}");
    198 }
    199 
    200 static void
    201 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
    202 			  const unsigned int data_len)
    203 {
    204 	const unsigned int addr_len =
    205 		data_len > sizeof(struct sockaddr_storage)
    206 		? sizeof(struct sockaddr_storage) : data_len;
    207 
    208 	print_sockaddr(cmsg_data, addr_len);
    209 }
    210 
    211 typedef void (* const cmsg_printer)(struct tcb *, const void *, unsigned int);
    212 
    213 static const struct {
    214 	const cmsg_printer printer;
    215 	const unsigned int min_len;
    216 } cmsg_socket_printers[] = {
    217 	[SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
    218 	[SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
    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 }
    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 
    235 static void
    236 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
    237 		     const void *cmsg_data, const unsigned int data_len)
    238 {
    239 	const unsigned int utype = cmsg_type;
    240 	switch (cmsg_level) {
    241 	case SOL_SOCKET:
    242 		printxval(scmvals, cmsg_type, "SCM_???");
    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);
    248 		}
    249 		break;
    250 	case SOL_IP:
    251 		printxval(ip_cmsg_types, cmsg_type, "IP_???");
    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);
    257 		}
    258 		break;
    259 	default:
    260 		tprintf("%#x", cmsg_type);
    261 	}
    262 }
    263 
    264 static unsigned int
    265 get_optmem_max(struct tcb *tcp)
    266 {
    267 	static int optmem_max;
    268 
    269 	if (!optmem_max) {
    270 		if (read_int_from_file(tcp, "/proc/sys/net/core/optmem_max",
    271 				       &optmem_max) || optmem_max <= 0) {
    272 			optmem_max = sizeof(long long) * (2 * IOV_MAX + 512);
    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
    283 decode_msg_control(struct tcb *const tcp, const kernel_ulong_t addr,
    284 		   const kernel_ulong_t in_control_len)
    285 {
    286 	if (!in_control_len)
    287 		return;
    288 	tprints(", msg_control=");
    289 
    290 	const unsigned int cmsg_size =
    291 #ifndef current_wordsize
    292 		(current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
    293 #endif
    294 			sizeof(struct cmsghdr);
    295 
    296 	unsigned int control_len = in_control_len > get_optmem_max(tcp)
    297 				   ? get_optmem_max(tcp) : in_control_len;
    298 	unsigned int buf_len = control_len;
    299 	char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
    300 	if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
    301 		printaddr(addr);
    302 		free(buf);
    303 		return;
    304 	}
    305 
    306 	union_cmsghdr u = { .ptr = buf };
    307 
    308 	tprints("[");
    309 	while (buf_len >= cmsg_size) {
    310 		const kernel_ulong_t cmsg_len =
    311 #ifndef current_wordsize
    312 			(current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
    313 #endif
    314 				u.cmsg->cmsg_len;
    315 		const int cmsg_level =
    316 #ifndef current_wordsize
    317 			(current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
    318 #endif
    319 				u.cmsg->cmsg_level;
    320 		const int cmsg_type =
    321 #ifndef current_wordsize
    322 			(current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
    323 #endif
    324 				u.cmsg->cmsg_type;
    325 
    326 		if (u.ptr != buf)
    327 			tprints(", ");
    328 		tprintf("{cmsg_len=%" PRI_klu ", cmsg_level=", cmsg_len);
    329 		printxval(socketlayers, cmsg_level, "SOL_???");
    330 		tprints(", cmsg_type=");
    331 
    332 		kernel_ulong_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
    333 
    334 		print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
    335 				     (const void *) (u.ptr + cmsg_size),
    336 				     len > cmsg_size ? len - cmsg_size : 0);
    337 		tprints("}");
    338 
    339 		if (len < cmsg_size) {
    340 			buf_len -= cmsg_size;
    341 			break;
    342 		}
    343 		len = (cmsg_len + current_wordsize - 1) &
    344 			~((kernel_ulong_t) current_wordsize - 1);
    345 		if (len >= buf_len) {
    346 			buf_len = 0;
    347 			break;
    348 		}
    349 		u.ptr += len;
    350 		buf_len -= len;
    351 	}
    352 	if (buf_len) {
    353 		tprints(", ...");
    354 		printaddr_comment(addr + (control_len - buf_len));
    355 	} else if (control_len < in_control_len) {
    356 		tprints(", ...");
    357 	}
    358 	tprints("]");
    359 	free(buf);
    360 }
    361 
    362 void
    363 print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg,
    364 		    const int *const p_user_msg_namelen,
    365 		    const kernel_ulong_t data_size)
    366 {
    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;
    370 
    371 	tprints("{msg_name=");
    372 	const int family =
    373 		decode_sockaddr(tcp, ptr_to_kulong(msg->msg_name), msg_namelen);
    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);
    381 
    382 	tprints(", msg_iov=");
    383 	tprint_iov_upto(tcp, msg->msg_iovlen,
    384 			ptr_to_kulong(msg->msg_iov), decode, data_size);
    385 	PRINT_FIELD_U(", ", *msg, msg_iovlen);
    386 
    387 	decode_msg_control(tcp, ptr_to_kulong(msg->msg_control),
    388 			   msg->msg_controllen);
    389 	PRINT_FIELD_U(", ", *msg, msg_controllen);
    390 
    391 	PRINT_FIELD_FLAGS(", ", *msg, msg_flags, msg_flags, "MSG_???");
    392 	tprints("}");
    393 }
    394 
    395 static bool
    396 fetch_msghdr_namelen(struct tcb *const tcp, const kernel_ulong_t addr,
    397 		     int *const p_msg_namelen)
    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 
    409 static void
    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)
    412 {
    413 	struct msghdr msg;
    414 
    415 	if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
    416 		print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size);
    417 	else
    418 		printaddr(addr);
    419 }
    420 
    421 void
    422 dumpiov_in_msghdr(struct tcb *const tcp, const kernel_ulong_t addr,
    423 		  const kernel_ulong_t data_size)
    424 {
    425 	struct msghdr msg;
    426 
    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 	}
    431 }
    432 
    433 SYS_FUNC(sendmsg)
    434 {
    435 	printfd(tcp, tcp->u_arg[0]);
    436 	tprints(", ");
    437 	decode_msghdr(tcp, 0, tcp->u_arg[1], -1);
    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)) {
    453 			set_tcb_priv_ulong(tcp, msg_namelen);
    454 			return 0;
    455 		}
    456 		printaddr(tcp->u_arg[1]);
    457 	} else {
    458 		msg_namelen = get_tcb_priv_ulong(tcp);
    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 }
    473