]> granicus.if.org Git - strace/blob - msghdr.c
net.c: move parsers of sendmsg and recvmsg syscalls to msghdr.c
[strace] / msghdr.c
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, const size_t data_len)
59 {
60         const int *fds = cmsg_data;
61         const size_t nfds = data_len / sizeof(*fds);
62         size_t i;
63
64         tprints("[");
65
66         for (i = 0; i < nfds; ++i) {
67                 if (i)
68                         tprints(", ");
69                 if (abbrev(tcp) && i >= max_strlen) {
70                         tprints("...");
71                         break;
72                 }
73                 printfd(tcp, fds[i]);
74         }
75
76         tprints("]");
77 }
78
79 static void
80 print_scm_creds(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
81 {
82         const struct ucred *uc = cmsg_data;
83
84         tprintf("{pid=%u, uid=%u, gid=%u}",
85                 (unsigned) uc->pid, (unsigned) uc->uid, (unsigned) uc->gid);
86 }
87
88 static void
89 print_scm_security(struct tcb *tcp, const void *cmsg_data,
90                    const size_t data_len)
91 {
92         print_quoted_string(cmsg_data, data_len, 0);
93 }
94
95 static void
96 print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
97                       const size_t data_len)
98 {
99         const struct in_pktinfo *info = cmsg_data;
100
101         tprints("{ipi_ifindex=");
102         print_ifindex(info->ipi_ifindex);
103         tprintf(", ipi_spec_dst=inet_addr(\"%s\")",
104                 inet_ntoa(info->ipi_spec_dst));
105         tprintf(", ipi_addr=inet_addr(\"%s\")}",
106                 inet_ntoa(info->ipi_addr));
107 }
108
109 static void
110 print_cmsg_uint(struct tcb *tcp, const void *cmsg_data, const size_t data_len)
111 {
112         const unsigned int *p = cmsg_data;
113
114         tprintf("[%u]", *p);
115 }
116
117 static void
118 print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
119                    const size_t data_len)
120 {
121         const uint8_t *p = cmsg_data;
122
123         tprintf("[%#x]", *p);
124 }
125
126 static void
127 print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
128                    const size_t data_len)
129 {
130         const unsigned char *opts = cmsg_data;
131         size_t i;
132
133         tprints("[");
134         for (i = 0; i < data_len; ++i) {
135                 if (i)
136                         tprints(", ");
137                 if (abbrev(tcp) && i >= max_strlen) {
138                         tprints("...");
139                         break;
140                 }
141                 tprintf("0x%02x", opts[i]);
142         }
143         tprints("]");
144 }
145
146 struct sock_ee {
147         uint32_t ee_errno;
148         uint8_t  ee_origin;
149         uint8_t  ee_type;
150         uint8_t  ee_code;
151         uint8_t  ee_pad;
152         uint32_t ee_info;
153         uint32_t ee_data;
154         struct sockaddr_in offender;
155 };
156
157 static void
158 print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
159                       const size_t data_len)
160 {
161         const struct sock_ee *const err = cmsg_data;
162
163         tprintf("{ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
164                 ", ee_info=%u, ee_data=%u, offender=",
165                 err->ee_errno, err->ee_origin, err->ee_type,
166                 err->ee_code, err->ee_info, err->ee_data);
167         print_sockaddr(tcp, &err->offender, sizeof(err->offender));
168         tprints("}");
169 }
170
171 static void
172 print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
173                           const size_t data_len)
174 {
175         const int addr_len =
176                 data_len > sizeof(struct sockaddr_storage)
177                 ? sizeof(struct sockaddr_storage) : data_len;
178
179         print_sockaddr(tcp, cmsg_data, addr_len);
180 }
181
182 typedef void (* const cmsg_printer)(struct tcb *, const void *, size_t);
183
184 static const struct {
185         const cmsg_printer printer;
186         const size_t min_len;
187 } cmsg_socket_printers[] = {
188         [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
189         [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
190         [SCM_SECURITY] = { print_scm_security, 1 }
191 }, cmsg_ip_printers[] = {
192         [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
193         [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
194         [IP_TOS] = { print_cmsg_uint8_t, 1 },
195         [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
196         [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
197         [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
198         [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
199         [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
200         [SCM_SECURITY] = { print_scm_security, 1 }
201 };
202
203 static void
204 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
205                      const void *cmsg_data, const size_t data_len)
206 {
207         const unsigned int utype = cmsg_type;
208         switch (cmsg_level) {
209         case SOL_SOCKET:
210                 printxval(scmvals, cmsg_type, "SCM_???");
211                 if (utype < ARRAY_SIZE(cmsg_socket_printers)
212                     && cmsg_socket_printers[utype].printer
213                     && data_len >= cmsg_socket_printers[utype].min_len) {
214                         tprints(", cmsg_data=");
215                         cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
216                 }
217                 break;
218         case SOL_IP:
219                 printxval(ip_cmsg_types, cmsg_type, "IP_???");
220                 if (utype < ARRAY_SIZE(cmsg_ip_printers)
221                     && cmsg_ip_printers[utype].printer
222                     && data_len >= cmsg_ip_printers[utype].min_len) {
223                         tprints(", cmsg_data=");
224                         cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
225                 }
226                 break;
227         default:
228                 tprintf("%#x", cmsg_type);
229         }
230 }
231
232 #ifndef UIO_MAXIOV
233 # define UIO_MAXIOV 1024
234 #endif
235
236 static unsigned int
237 get_optmem_max(void)
238 {
239         static int optmem_max;
240
241         if (!optmem_max) {
242                 if (read_int_from_file("/proc/sys/net/core/optmem_max",
243                                        &optmem_max) || optmem_max <= 0) {
244                         optmem_max = sizeof(long long) * (2 * UIO_MAXIOV + 512);
245                 } else {
246                         optmem_max = (optmem_max + sizeof(long long) - 1)
247                                      & ~(sizeof(long long) - 1);
248                 }
249         }
250
251         return optmem_max;
252 }
253
254 static void
255 decode_msg_control(struct tcb *tcp, unsigned long addr,
256                    const size_t in_control_len)
257 {
258         if (!in_control_len)
259                 return;
260         tprints(", msg_control=");
261
262         const size_t cmsg_size =
263 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
264                 (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
265 #endif
266                         sizeof(struct cmsghdr);
267
268         size_t control_len =
269                 in_control_len > get_optmem_max()
270                 ? get_optmem_max() : in_control_len;
271         size_t buf_len = control_len;
272         char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
273         if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
274                 printaddr(addr);
275                 free(buf);
276                 return;
277         }
278
279         union_cmsghdr u = { .ptr = buf };
280
281         tprints("[");
282         while (buf_len >= cmsg_size) {
283                 const size_t cmsg_len =
284 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
285                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
286 #endif
287                                 u.cmsg->cmsg_len;
288                 const int cmsg_level =
289 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
290                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
291 #endif
292                                 u.cmsg->cmsg_level;
293                 const int cmsg_type =
294 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
295                         (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
296 #endif
297                                 u.cmsg->cmsg_type;
298
299                 if (u.ptr != buf)
300                         tprints(", ");
301                 tprintf("{cmsg_len=%lu, cmsg_level=", (unsigned long) cmsg_len);
302                 printxval(socketlayers, cmsg_level, "SOL_???");
303                 tprints(", cmsg_type=");
304
305                 size_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
306
307                 print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
308                                      (const void *) (u.ptr + cmsg_size),
309                                      len > cmsg_size ? len - cmsg_size: 0);
310                 tprints("}");
311
312                 if (len < cmsg_size) {
313                         buf_len -= cmsg_size;
314                         break;
315                 }
316                 len = (cmsg_len + current_wordsize - 1) &
317                         (size_t) ~(current_wordsize - 1);
318                 if (len >= buf_len) {
319                         buf_len = 0;
320                         break;
321                 }
322                 u.ptr += len;
323                 buf_len -= len;
324         }
325         if (buf_len) {
326                 tprints(", ");
327                 printaddr(addr + (control_len - buf_len));
328         } else if (control_len < in_control_len) {
329                 tprints(", ...");
330         }
331         tprints("]");
332         free(buf);
333 }
334
335 void
336 print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg,
337                     const int *const p_user_msg_namelen,
338                     const unsigned long data_size)
339 {
340         const int msg_namelen =
341                 p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen
342                 ? *p_user_msg_namelen : (int) msg->msg_namelen;
343
344         tprints("{msg_name=");
345         const int family =
346                 decode_sockaddr(tcp, (long) msg->msg_name, msg_namelen);
347         const enum iov_decode decode =
348                 (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR;
349
350         tprints(", msg_namelen=");
351         if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen)
352                 tprintf("%d->", *p_user_msg_namelen);
353         tprintf("%d", msg->msg_namelen);
354
355         tprints(", msg_iov=");
356
357         tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
358                         (unsigned long) msg->msg_iov, decode, data_size);
359         tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
360
361         decode_msg_control(tcp, (unsigned long) msg->msg_control,
362                            msg->msg_controllen);
363         tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
364
365         tprints(", msg_flags=");
366         printflags(msg_flags, msg->msg_flags, "MSG_???");
367         tprints("}");
368 }
369
370 static bool
371 fetch_msghdr_namelen(struct tcb *tcp, const long addr, int *const p_msg_namelen)
372 {
373         struct msghdr msg;
374
375         if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) {
376                 *p_msg_namelen = msg.msg_namelen;
377                 return true;
378         } else {
379                 return false;
380         }
381 }
382
383 static void
384 decode_msghdr(struct tcb *tcp, const int *const p_user_msg_namelen,
385               const long addr, const unsigned long data_size)
386 {
387         struct msghdr msg;
388
389         if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
390                 print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size);
391         else
392                 printaddr(addr);
393 }
394
395 void
396 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
397 {
398         struct msghdr msg;
399
400         if (fetch_struct_msghdr(tcp, addr, &msg))
401                 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
402 }
403
404 SYS_FUNC(sendmsg)
405 {
406         printfd(tcp, tcp->u_arg[0]);
407         tprints(", ");
408         decode_msghdr(tcp, 0, tcp->u_arg[1], (unsigned long) -1L);
409         /* flags */
410         tprints(", ");
411         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
412
413         return RVAL_DECODED;
414 }
415
416 SYS_FUNC(recvmsg)
417 {
418         int msg_namelen;
419
420         if (entering(tcp)) {
421                 printfd(tcp, tcp->u_arg[0]);
422                 tprints(", ");
423                 if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) {
424                         /* abuse of auxstr to retain state */
425                         tcp->auxstr = (void *) (long) msg_namelen;
426                         return 0;
427                 }
428                 printaddr(tcp->u_arg[1]);
429         } else {
430                 msg_namelen = (long) tcp->auxstr;
431                 tcp->auxstr = NULL;
432
433                 if (syserror(tcp))
434                         tprintf("{msg_namelen=%d}", msg_namelen);
435                 else
436                         decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1],
437                                       tcp->u_rval);
438         }
439
440         /* flags */
441         tprints(", ");
442         printflags(msg_flags, tcp->u_arg[2], "MSG_???");
443
444         return RVAL_DECODED;
445 }