]> granicus.if.org Git - strace/blob - mmsghdr.c
tests: improve setugid error diagnostics a bit
[strace] / mmsghdr.c
1 /*
2  * Copyright (c) 2010 Andreas Schwab <schwab@linux-m68k.org>
3  * Copyright (c) 2012-2013 Denys Vlasenko <vda.linux@googlemail.com>
4  * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com>
5  * Copyright (c) 2010-2016 Dmitry V. Levin <ldv@altlinux.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32 #include "msghdr.h"
33 #include "xstring.h"
34 #include <limits.h>
35
36 static int
37 fetch_struct_mmsghdr_or_printaddr(struct tcb *const tcp,
38                                   const kernel_ulong_t addr,
39                                   const unsigned int len, void *const mh)
40 {
41         if ((entering(tcp) || !syserror(tcp))
42             && fetch_struct_mmsghdr(tcp, addr, mh)) {
43                 return 0;
44         } else {
45                 printaddr(addr);
46                 return -1;
47         }
48 }
49
50 struct print_struct_mmsghdr_config {
51         const int *p_user_msg_namelen;
52         unsigned int msg_len_vlen;
53         unsigned int count;
54         bool use_msg_len;
55 };
56
57 static bool
58 print_struct_mmsghdr(struct tcb *tcp, void *elem_buf,
59                      size_t elem_size, void *data)
60 {
61         const struct mmsghdr *const mmsg = elem_buf;
62         struct print_struct_mmsghdr_config *const c = data;
63
64         if (!c->count) {
65                 tprints("...");
66                 return false;
67         }
68         --c->count;
69
70         tprints("{msg_hdr=");
71         print_struct_msghdr(tcp, &mmsg->msg_hdr, c->p_user_msg_namelen,
72                             c->use_msg_len ? mmsg->msg_len : (kernel_ulong_t) -1);
73         if (c->msg_len_vlen) {
74                 tprintf(", msg_len=%u", mmsg->msg_len);
75                 --c->msg_len_vlen;
76         }
77         tprints("}");
78
79         if (c->p_user_msg_namelen)
80                 ++c->p_user_msg_namelen;
81
82         return true;
83 }
84
85 static void
86 free_mmsgvec_data(void *ptr)
87 {
88         char **pstr = ptr;
89         free(*pstr);
90         *pstr = 0;
91
92         free(ptr);
93 }
94
95 struct mmsgvec_data {
96         char *timeout;
97         unsigned int count;
98         int namelen[IOV_MAX];
99 };
100
101 static void
102 save_mmsgvec_namelen(struct tcb *const tcp, kernel_ulong_t addr,
103                      unsigned int len, const char *const timeout)
104 {
105         if (len > IOV_MAX)
106                 len = IOV_MAX;
107
108         const size_t data_size = offsetof(struct mmsgvec_data, namelen)
109                                  + sizeof(int) * len;
110         struct mmsgvec_data *const data = xmalloc(data_size);
111         data->timeout = xstrdup(timeout);
112
113         unsigned int i, fetched;
114
115         for (i = 0; i < len; ++i, addr += fetched) {
116                 struct mmsghdr mh;
117
118                 fetched = fetch_struct_mmsghdr(tcp, addr, &mh);
119                 if (!fetched)
120                         break;
121                 data->namelen[i] = mh.msg_hdr.msg_namelen;
122         }
123         data->count = i;
124
125         set_tcb_priv_data(tcp, data, free_mmsgvec_data);
126 }
127
128 static void
129 decode_mmsgvec(struct tcb *const tcp, const kernel_ulong_t addr,
130                const unsigned int vlen, const unsigned int msg_len_vlen,
131                const bool use_msg_len)
132 {
133         struct mmsghdr mmsg;
134         struct print_struct_mmsghdr_config c = {
135                 .msg_len_vlen = msg_len_vlen,
136                 .count = IOV_MAX,
137                 .use_msg_len = use_msg_len
138         };
139         const struct mmsgvec_data *const data = get_tcb_priv_data(tcp);
140
141         if (data) {
142                 if (data->count < c.count)
143                         c.count = data->count;
144                 c.p_user_msg_namelen = data->namelen;
145         }
146
147         print_array(tcp, addr, vlen, &mmsg, sizeof_struct_mmsghdr(),
148                     fetch_struct_mmsghdr_or_printaddr,
149                     print_struct_mmsghdr, &c);
150 }
151
152 void
153 dumpiov_in_mmsghdr(struct tcb *const tcp, kernel_ulong_t addr)
154 {
155         unsigned int len = tcp->u_rval;
156         unsigned int i, fetched;
157         struct mmsghdr mmsg;
158
159         for (i = 0; i < len; ++i, addr += fetched) {
160                 fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg);
161                 if (!fetched)
162                         break;
163                 tprintf(" = %" PRI_klu " buffers in vector %u\n",
164                         (kernel_ulong_t) mmsg.msg_hdr.msg_iovlen, i);
165                 dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen,
166                              ptr_to_kulong(mmsg.msg_hdr.msg_iov),
167                              mmsg.msg_len);
168         }
169 }
170
171 SYS_FUNC(sendmmsg)
172 {
173         if (entering(tcp)) {
174                 /* sockfd */
175                 printfd(tcp, tcp->u_arg[0]);
176                 tprints(", ");
177                 if (!verbose(tcp)) {
178                         /* msgvec */
179                         printaddr(tcp->u_arg[1]);
180                         /* vlen */
181                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
182                         /* flags */
183                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
184                         return RVAL_DECODED;
185                 }
186         } else {
187                 const unsigned int msg_len_vlen =
188                         syserror(tcp) ? 0 : tcp->u_rval;
189                 /* msgvec */
190                 temporarily_clear_syserror(tcp);
191                 decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_arg[2],
192                                msg_len_vlen, false);
193                 restore_cleared_syserror(tcp);
194                 /* vlen */
195                 tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
196                 /* flags */
197                 printflags(msg_flags, tcp->u_arg[3], "MSG_???");
198         }
199         return 0;
200 }
201
202 SYS_FUNC(recvmmsg)
203 {
204         if (entering(tcp)) {
205                 printfd(tcp, tcp->u_arg[0]);
206                 tprints(", ");
207                 if (verbose(tcp)) {
208                         save_mmsgvec_namelen(tcp, tcp->u_arg[1], tcp->u_arg[2],
209                                              sprint_timespec(tcp, tcp->u_arg[4]));
210                 } else {
211                         /* msgvec */
212                         printaddr(tcp->u_arg[1]);
213                         /* vlen */
214                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
215                         /* flags */
216                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
217                         tprints(", ");
218                         print_timespec(tcp, tcp->u_arg[4]);
219                 }
220                 return 0;
221         } else {
222                 if (verbose(tcp)) {
223                         /* msgvec */
224                         decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval,
225                                        tcp->u_rval, true);
226                         /* vlen */
227                         tprintf(", %u, ", (unsigned int) tcp->u_arg[2]);
228                         /* flags */
229                         printflags(msg_flags, tcp->u_arg[3], "MSG_???");
230                         tprints(", ");
231                         /* timeout on entrance */
232                         tprints(*(const char **) get_tcb_priv_data(tcp));
233                 }
234                 if (syserror(tcp))
235                         return 0;
236                 if (tcp->u_rval == 0) {
237                         tcp->auxstr = "Timeout";
238                         return RVAL_STR;
239                 }
240                 if (!verbose(tcp) || !tcp->u_arg[4])
241                         return 0;
242                 /* timeout on exit */
243                 static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE];
244                 xsprintf(str, "left %s", sprint_timespec(tcp, tcp->u_arg[4]));
245                 tcp->auxstr = str;
246                 return RVAL_STR;
247         }
248 }