]> granicus.if.org Git - strace/blob - msghdr.c
msghdr.c: limit output when printing excessively large messages
[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 static void
336 print_msghdr(struct tcb *tcp, struct msghdr *msg, unsigned long data_size)
337 {
338         tprints("{msg_name=");
339         decode_sockaddr(tcp, (long)msg->msg_name, msg->msg_namelen);
340         tprintf(", msg_namelen=%d", msg->msg_namelen);
341
342         tprints(", msg_iov=");
343         tprint_iov_upto(tcp, (unsigned long) msg->msg_iovlen,
344                         (unsigned long) msg->msg_iov, IOV_DECODE_STR, data_size);
345         tprintf(", msg_iovlen=%lu", (unsigned long) msg->msg_iovlen);
346
347         decode_msg_control(tcp, (unsigned long) msg->msg_control,
348                            msg->msg_controllen);
349         tprintf(", msg_controllen=%lu", (unsigned long) msg->msg_controllen);
350
351         tprints(", msg_flags=");
352         printflags(msg_flags, msg->msg_flags, "MSG_???");
353         tprints("}");
354 }
355
356 void
357 decode_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
358 {
359         struct msghdr msg;
360
361         if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
362                 print_msghdr(tcp, &msg, data_size);
363         else
364                 printaddr(addr);
365 }
366
367 void
368 dumpiov_in_msghdr(struct tcb *tcp, long addr, unsigned long data_size)
369 {
370         struct msghdr msg;
371
372         if (fetch_struct_msghdr(tcp, addr, &msg))
373                 dumpiov_upto(tcp, msg.msg_iovlen, (long)msg.msg_iov, data_size);
374 }
375
376 static int
377 decode_mmsghdr(struct tcb *tcp, long addr, bool use_msg_len)
378 {
379         struct mmsghdr mmsg;
380         int fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
381
382         if (fetched) {
383                 tprints("{msg_hdr=");
384                 print_msghdr(tcp, &mmsg.msg_hdr, use_msg_len ? mmsg.msg_len : -1UL);
385                 tprintf(", msg_len=%u}", mmsg.msg_len);
386         } else {
387                 printaddr(addr);
388         }
389
390         return fetched;
391 }
392
393 void
394 decode_mmsgvec(struct tcb *tcp, unsigned long addr, unsigned int len,
395                bool use_msg_len)
396 {
397         if (syserror(tcp)) {
398                 printaddr(addr);
399         } else {
400                 unsigned int i, fetched;
401
402                 tprints("[");
403                 for (i = 0; i < len; ++i, addr += fetched) {
404                         if (i)
405                                 tprints(", ");
406                         fetched = decode_mmsghdr(tcp, addr, use_msg_len);
407                         if (!fetched)
408                                 break;
409                 }
410                 tprints("]");
411         }
412 }
413
414 void
415 dumpiov_in_mmsghdr(struct tcb *tcp, long addr)
416 {
417         unsigned int len = tcp->u_rval;
418         unsigned int i, fetched;
419         struct mmsghdr mmsg;
420
421         for (i = 0; i < len; ++i, addr += fetched) {
422                 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
423                 if (!fetched)
424                         break;
425                 tprintf(" = %lu buffers in vector %u\n",
426                         (unsigned long)mmsg.msg_hdr.msg_iovlen, i);
427                 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
428                         (long)mmsg.msg_hdr.msg_iov, mmsg.msg_len);
429         }
430 }