]> granicus.if.org Git - strace/blob - nlattr.c
nlattr: add value processing support for xlat/flags nlattr decoders
[strace] / nlattr.c
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4  * Copyright (c) 2016-2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31 #include <endian.h>
32 #include "netlink.h"
33 #include "nlattr.h"
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <linux/sock_diag.h>
37 #include "static_assert.h"
38
39 #include "xlat/netlink_sk_meminfo_indices.h"
40
41 static bool
42 fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
43              const kernel_ulong_t addr, const unsigned int len,
44              const bool in_array)
45 {
46         if (len < sizeof(struct nlattr)) {
47                 printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
48                 return false;
49         }
50
51         if (tfetch_obj(tcp, addr, nlattr))
52                 return true;
53
54         if (in_array) {
55                 tprints("...");
56                 printaddr_comment(addr);
57         } else {
58                 printaddr(addr);
59         }
60
61         return false;
62 }
63
64 static void
65 print_nlattr(const struct nlattr *const nla,
66              const struct xlat *const table,
67              const char *const dflt)
68 {
69         static_assert(NLA_TYPE_MASK == ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER),
70                       "wrong NLA_TYPE_MASK");
71
72         tprintf("{nla_len=%u, nla_type=", nla->nla_len);
73         if (nla->nla_type & NLA_F_NESTED) {
74                 print_xlat(NLA_F_NESTED);
75                 tprints("|");
76         }
77         if (nla->nla_type & NLA_F_NET_BYTEORDER) {
78                 print_xlat(NLA_F_NET_BYTEORDER);
79                 tprints("|");
80         }
81         printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
82         tprints("}");
83 }
84
85 static void
86 decode_nlattr_with_data(struct tcb *const tcp,
87                         const struct nlattr *const nla,
88                         const kernel_ulong_t addr,
89                         const unsigned int len,
90                         const struct xlat *const table,
91                         const char *const dflt,
92                         const nla_decoder_t *const decoders,
93                         const unsigned int size,
94                         const void *const opaque_data)
95 {
96         const unsigned int nla_len = MIN(nla->nla_len, len);
97
98         if (nla_len > NLA_HDRLEN)
99                 tprints("{");
100
101         print_nlattr(nla, table, dflt);
102
103         if (nla_len > NLA_HDRLEN) {
104                 const unsigned int idx = size ? nla->nla_type : 0;
105
106                 tprints(", ");
107                 if (!decoders
108                     || (size && idx >= size)
109                     || !decoders[idx]
110                     || !decoders[idx](
111                                 tcp, addr + NLA_HDRLEN,
112                                 nla_len - NLA_HDRLEN,
113                                 size ? opaque_data
114                                      : (const void *) (uintptr_t) nla->nla_type)
115                     )
116                         printstr_ex(tcp, addr + NLA_HDRLEN,
117                                     nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
118                 tprints("}");
119         }
120 }
121
122 void
123 decode_nlattr(struct tcb *const tcp,
124               kernel_ulong_t addr,
125               unsigned int len,
126               const struct xlat *const table,
127               const char *const dflt,
128               const nla_decoder_t *const decoders,
129               const unsigned int size,
130               const void *const opaque_data)
131 {
132         struct nlattr nla;
133         bool is_array = false;
134         unsigned int elt;
135
136         if (decoders && !size && opaque_data)
137                 error_func_msg("[xlat %p, dflt \"%s\", decoders %p] "
138                                "size is zero (going to pass nla_type as "
139                                "decoder argument), but opaque data (%p) is not "
140                                "- will be ignored",
141                                table, dflt, decoders, opaque_data);
142
143         for (elt = 0; fetch_nlattr(tcp, &nla, addr, len, is_array); elt++) {
144                 if (abbrev(tcp) && elt == max_strlen) {
145                         tprints("...");
146                         break;
147                 }
148
149                 const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
150                 kernel_ulong_t next_addr = 0;
151                 unsigned int next_len = 0;
152
153                 if (nla.nla_len >= NLA_HDRLEN) {
154                         next_len = (len >= nla_len) ? len - nla_len : 0;
155
156                         if (next_len && addr + nla_len > addr)
157                                 next_addr = addr + nla_len;
158                 }
159
160                 if (!is_array && next_addr) {
161                         tprints("[");
162                         is_array = true;
163                 }
164
165                 decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
166                                         decoders, size, opaque_data);
167
168                 if (!next_addr)
169                         break;
170
171                 tprints(", ");
172                 addr = next_addr;
173                 len = next_len;
174         }
175
176         if (is_array) {
177                 tprints("]");
178         }
179 }
180
181 bool
182 decode_nla_str(struct tcb *const tcp,
183                const kernel_ulong_t addr,
184                const unsigned int len,
185                const void *const opaque_data)
186 {
187         printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
188
189         return true;
190 }
191
192 bool
193 decode_nla_strn(struct tcb *const tcp,
194                 const kernel_ulong_t addr,
195                 const unsigned int len,
196                 const void *const opaque_data)
197 {
198         printstrn(tcp, addr, len);
199
200         return true;
201 }
202
203 bool
204 decode_nla_meminfo(struct tcb *const tcp,
205                    const kernel_ulong_t addr,
206                    const unsigned int len,
207                    const void *const opaque_data)
208 {
209         uint32_t mem;
210         const size_t nmemb = len / sizeof(mem);
211
212         if (!nmemb)
213                 return false;
214
215         unsigned int count = 0;
216         print_array_ex(tcp, addr, nmemb, &mem, sizeof(mem),
217                        tfetch_mem, print_uint32_array_member, &count,
218                        PAF_PRINT_INDICES | PAF_INDEX_XLAT_VALUE_INDEXED
219                         | XLAT_STYLE_FMT_U,
220                        ARRSZ_PAIR(netlink_sk_meminfo_indices),
221                        "SK_MEMINFO_???");
222
223         return true;
224 }
225
226 bool
227 decode_nla_fd(struct tcb *const tcp,
228               const kernel_ulong_t addr,
229               const unsigned int len,
230               const void *const opaque_data)
231 {
232         int fd;
233
234         if (len < sizeof(fd))
235                 return false;
236         else if (!umove_or_printaddr(tcp, addr, &fd))
237                 printfd(tcp, fd);
238
239         return true;
240 }
241
242 bool
243 decode_nla_ifindex(struct tcb *const tcp,
244                const kernel_ulong_t addr,
245                const unsigned int len,
246                const void *const opaque_data)
247 {
248         uint32_t ifindex;
249
250         if (len < sizeof(ifindex))
251                 return false;
252         else if (!umove_or_printaddr(tcp, addr, &ifindex))
253                 print_ifindex(ifindex);
254
255         return true;
256 }
257
258 bool
259 decode_nla_xval(struct tcb *const tcp,
260                 const kernel_ulong_t addr,
261                 const unsigned int len,
262                 const void *const opaque_data)
263 {
264         const struct decode_nla_xlat_opts * const opts = opaque_data;
265         union {
266                 uint64_t val;
267                 uint8_t  bytes[sizeof(uint64_t)];
268         } data;
269         const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
270
271         data.val = 0;
272
273         if (len > sizeof(data))
274                 return false;
275         else if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs))
276         {
277                 if (opts->process_fn)
278                         data.val = opts->process_fn(data.val);
279                 if (opts->prefix)
280                         tprints(opts->prefix);
281                 printxval_dispatch_ex(opts->xlat, opts->xlat_size, data.val,
282                                       opts->dflt, opts->xt, opts->style);
283                 if (opts->suffix)
284                         tprints(opts->suffix);
285         }
286
287         return true;
288 }
289
290 bool
291 decode_nla_ip_proto(struct tcb *const tcp,
292                     const kernel_ulong_t addr,
293                     const unsigned int len,
294                     const void *const opaque_data)
295 {
296         const struct decode_nla_xlat_opts opts = {
297                 .xlat = inet_protocols,
298                 .xlat_size = inet_protocols_size,
299                 .xt = XT_SORTED,
300                 .dflt = "IPPROTO_???",
301         };
302
303         return decode_nla_xval(tcp, addr, len, &opts);
304 }
305
306 bool
307 decode_nla_flags(struct tcb *const tcp,
308                  const kernel_ulong_t addr,
309                  const unsigned int len,
310                  const void *const opaque_data)
311 {
312         const struct decode_nla_xlat_opts * const opts = opaque_data;
313         union {
314                 uint64_t flags;
315                 uint8_t  bytes[sizeof(uint64_t)];
316         } data = { .flags = 0 };
317         const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
318
319         if (opts->xt == XT_INDEXED)
320                 error_func_msg("indexed xlats are currently incompatible with "
321                                "printflags");
322
323         if (len > sizeof(data))
324                 return false;
325         else if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs))
326         {
327                 if (opts->process_fn)
328                         data.flags = opts->process_fn(data.flags);
329                 if (opts->prefix)
330                         tprints(opts->prefix);
331                 printflags_ex(data.flags, opts->dflt, opts->style, opts->xlat,
332                               NULL);
333                 if (opts->suffix)
334                         tprints(opts->suffix);
335         }
336
337         return true;
338 }
339
340 bool
341 decode_nla_be16(struct tcb *const tcp,
342                 const kernel_ulong_t addr,
343                 const unsigned int len,
344                 const void *const opaque_data)
345 {
346         uint16_t num;
347
348         if (len < sizeof(num))
349                 return false;
350         else if (!umove_or_printaddr(tcp, addr, &num))
351                 tprintf("htons(%u)", ntohs(num));
352
353         return true;
354 }
355
356 bool
357 decode_nla_be64(struct tcb *const tcp,
358                 const kernel_ulong_t addr,
359                 const unsigned int len,
360                 const void *const opaque_data)
361 {
362 #if defined HAVE_BE64TOH || defined be64toh
363         uint64_t num;
364
365         if (len < sizeof(num))
366                 return false;
367         else if (!umove_or_printaddr(tcp, addr, &num))
368                 tprintf("htobe64(%" PRIu64 ")", be64toh(num));
369
370         return true;
371 #else
372         return false;
373 #endif
374 }
375
376 #define DECODE_NLA_INTEGER(name, type, fmt)             \
377 bool                                                    \
378 decode_nla_ ## name(struct tcb *const tcp,              \
379                     const kernel_ulong_t addr,          \
380                     const unsigned int len,             \
381                     const void *const opaque_data)      \
382 {                                                       \
383         type num;                                       \
384                                                         \
385         if (len < sizeof(num))                          \
386                 return false;                           \
387         if (!umove_or_printaddr(tcp, addr, &num))       \
388                 tprintf(fmt, num);                      \
389         return true;                                    \
390 }
391
392 DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
393 DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
394 DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
395 DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
396 DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
397 DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
398 DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
399 DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)