]> granicus.if.org Git - strace/blob - dm.c
mem: decode hugetlb page size in mmap flags
[strace] / dm.c
1 /*
2  * Support for decoding of DM_* ioctl commands.
3  *
4  * Copyright (c) 2016 Mikulas Patocka <mpatocka@redhat.com>
5  * Copyright (c) 2016 Masatake Yamato <yamato@redhat.com>
6  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
7  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
8  * Copyright (c) 2016-2017 The strace developers.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35
36 #ifdef HAVE_LINUX_DM_IOCTL_H
37
38 # include "print_fields.h"
39 # include <linux/dm-ioctl.h>
40 # include <linux/ioctl.h>
41
42 # if DM_VERSION_MAJOR == 4
43
44 /* Definitions for command which have been added later */
45
46 #  ifndef DM_LIST_VERSIONS
47 #   define DM_LIST_VERSIONS    _IOWR(DM_IOCTL, 0x0d, struct dm_ioctl)
48 #  endif
49 #  ifndef DM_TARGET_MSG
50 #   define DM_TARGET_MSG       _IOWR(DM_IOCTL, 0x0e, struct dm_ioctl)
51 #  endif
52 #  ifndef DM_DEV_SET_GEOMETRY
53 #   define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, 0x0f, struct dm_ioctl)
54 #  endif
55 #  ifndef DM_DEV_ARM_POLL
56 #   define DM_DEV_ARM_POLL     _IOWR(DM_IOCTL, 0x10, struct dm_ioctl)
57 #  endif
58
59
60 static void
61 dm_decode_device(const unsigned int code, const struct dm_ioctl *ioc)
62 {
63         switch (code) {
64         case DM_REMOVE_ALL:
65         case DM_LIST_DEVICES:
66         case DM_LIST_VERSIONS:
67                 break;
68         default:
69                 if (ioc->dev)
70                         PRINT_FIELD_DEV(", ", *ioc, dev);
71
72                 if (ioc->name[0])
73                         PRINT_FIELD_CSTRING(", ", *ioc, name);
74
75                 if (ioc->uuid[0])
76                         PRINT_FIELD_CSTRING(", ", *ioc, uuid);
77
78                 break;
79         }
80 }
81
82 static void
83 dm_decode_values(struct tcb *tcp, const unsigned int code,
84                  const struct dm_ioctl *ioc)
85 {
86         if (entering(tcp)) {
87                 switch (code) {
88                 case DM_TABLE_LOAD:
89                         PRINT_FIELD_U(", ", *ioc, target_count);
90                         break;
91                 case DM_DEV_SUSPEND:
92                         if (ioc->flags & DM_SUSPEND_FLAG)
93                                 break;
94                         /* Fall through */
95                 case DM_DEV_RENAME:
96                 case DM_DEV_REMOVE:
97                 case DM_DEV_WAIT:
98                         PRINT_FIELD_U(", ", *ioc, event_nr);
99                         break;
100                 }
101         } else if (!syserror(tcp)) {
102                 switch (code) {
103                 case DM_DEV_CREATE:
104                 case DM_DEV_RENAME:
105                 case DM_DEV_SUSPEND:
106                 case DM_DEV_STATUS:
107                 case DM_DEV_WAIT:
108                 case DM_TABLE_LOAD:
109                 case DM_TABLE_CLEAR:
110                 case DM_TABLE_DEPS:
111                 case DM_TABLE_STATUS:
112                 case DM_TARGET_MSG:
113                         PRINT_FIELD_U(", ", *ioc, target_count);
114                         PRINT_FIELD_U(", ", *ioc, open_count);
115                         PRINT_FIELD_U(", ", *ioc, event_nr);
116                         break;
117                 }
118         }
119 }
120
121 #include "xlat/dm_flags.h"
122
123 static void
124 dm_decode_flags(const struct dm_ioctl *ioc)
125 {
126         PRINT_FIELD_FLAGS(", ", *ioc, flags, dm_flags, "DM_???");
127 }
128
129 static void
130 dm_decode_dm_target_spec(struct tcb *const tcp, const kernel_ulong_t addr,
131                          const struct dm_ioctl *const ioc)
132 {
133         static const uint32_t target_spec_size =
134                 sizeof(struct dm_target_spec);
135         uint32_t i;
136         uint32_t offset = ioc->data_start;
137         uint32_t offset_end = 0;
138
139         if (abbrev(tcp)) {
140                 if (ioc->target_count)
141                         tprints(", ...");
142
143                 return;
144         }
145
146         for (i = 0; i < ioc->target_count; i++) {
147                 tprints(", ");
148
149                 if (i && offset <= offset_end)
150                         goto misplaced;
151
152                 offset_end = offset + target_spec_size;
153
154                 if (offset_end <= offset || offset_end > ioc->data_size)
155                         goto misplaced;
156
157                 if (i >= max_strlen) {
158                         tprints("...");
159                         break;
160                 }
161
162                 struct dm_target_spec s;
163
164                 if (umove_or_printaddr(tcp, addr + offset, &s))
165                         break;
166
167                 PRINT_FIELD_U("{", s, sector_start);
168                 PRINT_FIELD_U(", ", s, length);
169
170                 if (exiting(tcp))
171                         PRINT_FIELD_D(", ", s, status);
172
173                 PRINT_FIELD_CSTRING(", ", s, target_type);
174
175                 tprints(", string=");
176                 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
177                              QUOTE_0_TERMINATED);
178                 tprints("}");
179
180                 if (entering(tcp))
181                         offset += s.next;
182                 else
183                         offset = ioc->data_start + s.next;
184         }
185
186         return;
187
188 misplaced:
189         tprints("???");
190         tprints_comment("misplaced struct dm_target_spec");
191 }
192
193 bool
194 dm_print_dev(struct tcb *tcp, void *dev_ptr, size_t dev_size, void *dummy)
195 {
196         uint64_t *dev = (uint64_t *) dev_ptr;
197
198         print_dev_t(*dev);
199
200         return 1;
201 }
202
203 static void
204 dm_decode_dm_target_deps(struct tcb *const tcp, const kernel_ulong_t addr,
205                          const struct dm_ioctl *const ioc)
206 {
207         if (ioc->data_start == ioc->data_size)
208                 return;
209
210         tprints(", ");
211
212         if (abbrev(tcp)) {
213                 tprints("...");
214                 return;
215         }
216
217         static const uint32_t target_deps_dev_offs =
218                 offsetof(struct dm_target_deps, dev);
219         uint64_t dev_buf;
220         struct dm_target_deps s;
221         uint32_t offset = ioc->data_start;
222         uint32_t offset_end = offset + target_deps_dev_offs;
223         uint32_t space;
224
225         if (offset_end <= offset || offset_end > ioc->data_size)
226                 goto misplaced;
227
228         if (umove_or_printaddr(tcp, addr + offset, &s))
229                 return;
230
231         space = (ioc->data_size - offset_end) / sizeof(dev_buf);
232
233         if (s.count > space)
234                 goto misplaced;
235
236         PRINT_FIELD_U("{", s, count);
237
238         tprints(", deps=");
239         print_array(tcp, addr + offset_end, s.count, &dev_buf, sizeof(dev_buf),
240                     umoven_or_printaddr, dm_print_dev, NULL);
241
242         tprints("}");
243
244         return;
245
246 misplaced:
247         tprints("???");
248         tprints_comment("misplaced struct dm_target_deps");
249 }
250
251 static void
252 dm_decode_dm_name_list(struct tcb *const tcp, const kernel_ulong_t addr,
253                        const struct dm_ioctl *const ioc)
254 {
255         static const uint32_t name_list_name_offs =
256                 offsetof(struct dm_name_list, name);
257         struct dm_name_list s;
258         uint32_t offset = ioc->data_start;
259         uint32_t offset_end = 0;
260         uint32_t count;
261
262         if (ioc->data_start == ioc->data_size)
263                 return;
264
265         if (abbrev(tcp)) {
266                 tprints(", ...");
267                 return;
268         }
269
270         for (count = 0;; count++) {
271                 tprints(", ");
272
273                 if (count && offset <= offset_end)
274                         goto misplaced;
275
276                 offset_end = offset + name_list_name_offs;
277
278                 if (offset_end <= offset || offset_end > ioc->data_size)
279                         goto misplaced;
280
281                 if (count >= max_strlen) {
282                         tprints("...");
283                         break;
284                 }
285
286                 if (umove_or_printaddr(tcp, addr + offset, &s))
287                         break;
288
289                 PRINT_FIELD_DEV("{", s, dev);
290                 tprints(", name=");
291                 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
292                             QUOTE_0_TERMINATED);
293                 tprints("}");
294
295                 if (!s.next)
296                         break;
297
298                 offset += s.next;
299         }
300
301         return;
302
303 misplaced:
304         tprints("???");
305         tprints_comment("misplaced struct dm_name_list");
306 }
307
308 static void
309 dm_decode_dm_target_versions(struct tcb *const tcp, const kernel_ulong_t addr,
310                              const struct dm_ioctl *const ioc)
311 {
312         static const uint32_t target_vers_name_offs =
313                 offsetof(struct dm_target_versions, name);
314         struct dm_target_versions s;
315         uint32_t offset = ioc->data_start;
316         uint32_t offset_end = 0;
317         uint32_t count;
318
319         if (ioc->data_start == ioc->data_size)
320                 return;
321
322         if (abbrev(tcp)) {
323                 tprints(", ...");
324                 return;
325         }
326
327         for (count = 0;; count++) {
328                 tprints(", ");
329
330                 if (count && offset <= offset_end)
331                         goto misplaced;
332
333                 offset_end = offset + target_vers_name_offs;
334
335                 if (offset_end <= offset || offset_end > ioc->data_size)
336                         goto misplaced;
337
338                 if (count >= max_strlen) {
339                         tprints("...");
340                         break;
341                 }
342
343                 if (umove_or_printaddr(tcp, addr + offset, &s))
344                         break;
345
346                 tprints("{name=");
347                 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
348                             QUOTE_0_TERMINATED);
349                 tprintf(", version=%" PRIu32 ".%" PRIu32 ".%" PRIu32 "}",
350                         s.version[0], s.version[1], s.version[2]);
351
352                 if (!s.next)
353                         break;
354
355                 offset += s.next;
356         }
357
358         return;
359
360 misplaced:
361         tprints("???");
362         tprints_comment("misplaced struct dm_target_versions");
363 }
364
365 static void
366 dm_decode_dm_target_msg(struct tcb *const tcp, const kernel_ulong_t addr,
367                         const struct dm_ioctl *const ioc)
368 {
369         if (ioc->data_start == ioc->data_size)
370                 return;
371
372         tprints(", ");
373
374         if (abbrev(tcp)) {
375                 tprints("...");
376                 return;
377         }
378
379         static const uint32_t target_msg_message_offs =
380                 offsetof(struct dm_target_msg, message);
381         uint32_t offset = ioc->data_start;
382         uint32_t offset_end = offset + target_msg_message_offs;
383
384         if (offset_end > offset && offset_end <= ioc->data_size) {
385                 struct dm_target_msg s;
386
387                 if (umove_or_printaddr(tcp, addr + offset, &s))
388                         return;
389
390                 PRINT_FIELD_U("{", s, sector);
391                 tprints(", message=");
392                 printstr_ex(tcp, addr + offset_end, ioc->data_size - offset_end,
393                             QUOTE_0_TERMINATED);
394                 tprints("}");
395         } else {
396                 tprints("???");
397                 tprints_comment("misplaced struct dm_target_msg");
398         }
399 }
400
401 static void
402 dm_decode_string(struct tcb *const tcp, const kernel_ulong_t addr,
403                  const struct dm_ioctl *const ioc)
404 {
405         tprints(", ");
406
407         if (abbrev(tcp)) {
408                 tprints("...");
409                 return;
410         }
411
412         uint32_t offset = ioc->data_start;
413
414         if (offset <= ioc->data_size) {
415                 tprints("string=");
416                 printstr_ex(tcp, addr + offset, ioc->data_size - offset,
417                             QUOTE_0_TERMINATED);
418         } else {
419                 tprints("???");
420                 tprints_comment("misplaced string");
421         }
422 }
423
424 static inline bool
425 dm_ioctl_has_params(const unsigned int code)
426 {
427         switch (code) {
428         case DM_VERSION:
429         case DM_REMOVE_ALL:
430         case DM_DEV_CREATE:
431         case DM_DEV_REMOVE:
432         case DM_DEV_SUSPEND:
433         case DM_DEV_STATUS:
434         case DM_TABLE_CLEAR:
435         case DM_DEV_ARM_POLL:
436                 return false;
437         }
438
439         return true;
440 }
441
442 static int
443 dm_known_ioctl(struct tcb *const tcp, const unsigned int code,
444                const kernel_ulong_t arg)
445 {
446         struct dm_ioctl *ioc = NULL;
447         struct dm_ioctl *entering_ioc = NULL;
448         bool ioc_changed = false;
449
450         if (entering(tcp)) {
451                 ioc = malloc(sizeof(*ioc));
452                 if (!ioc)
453                         return 0;
454         } else {
455                 ioc = alloca(sizeof(*ioc));
456         }
457
458         if ((umoven(tcp, arg, offsetof(struct dm_ioctl, data), ioc) < 0) ||
459             (ioc->data_size < offsetof(struct dm_ioctl, data_size))) {
460                 if (entering(tcp))
461                         free(ioc);
462                 return 0;
463         }
464         if (entering(tcp))
465                 set_tcb_priv_data(tcp, ioc, free);
466         else {
467                 entering_ioc = get_tcb_priv_data(tcp);
468
469                 /*
470                  * retrieve_status, __dev_status called only in case of success,
471                  * so it looks like there's no need to check open_count,
472                  * event_nr, target_count, dev fields for change (they are
473                  * printed only in case of absence of errors).
474                  */
475                 if (!entering_ioc ||
476                     (ioc->version[0] != entering_ioc->version[0]) ||
477                     (ioc->version[1] != entering_ioc->version[1]) ||
478                     (ioc->version[2] != entering_ioc->version[2]) ||
479                     (ioc->data_size != entering_ioc->data_size) ||
480                     (ioc->data_start != entering_ioc->data_start) ||
481                     (ioc->flags != entering_ioc->flags))
482                         ioc_changed = true;
483         }
484
485         if (exiting(tcp) && syserror(tcp) && !ioc_changed)
486                 return RVAL_IOCTL_DECODED;
487
488         /*
489          * device mapper code uses %d in some places and %u in another, but
490          * fields themselves are declared as __u32.
491          */
492         tprintf("%s{version=%u.%u.%u",  entering(tcp) ? ", " : " => ",
493                 ioc->version[0], ioc->version[1], ioc->version[2]);
494         /*
495          * if we use a different version of ABI, do not attempt to decode
496          * ioctl fields
497          */
498         if (ioc->version[0] != DM_VERSION_MAJOR) {
499                 tprints_comment("unsupported device mapper ABI version");
500                 goto skip;
501         }
502
503         PRINT_FIELD_U(", ", *ioc, data_size);
504
505         if (ioc->data_size < offsetof(struct dm_ioctl, data)) {
506                 tprints_comment("data_size too small");
507                 goto skip;
508         }
509
510         if (dm_ioctl_has_params(code))
511                 PRINT_FIELD_U(", ", *ioc, data_start);
512
513         dm_decode_device(code, ioc);
514         dm_decode_values(tcp, code, ioc);
515         dm_decode_flags(ioc);
516
517         switch (code) {
518         case DM_DEV_WAIT:
519         case DM_TABLE_STATUS:
520                 if (entering(tcp) || syserror(tcp))
521                         break;
522                 dm_decode_dm_target_spec(tcp, arg, ioc);
523                 break;
524         case DM_TABLE_LOAD:
525                 if (exiting(tcp))
526                         break;
527                 dm_decode_dm_target_spec(tcp, arg, ioc);
528                 break;
529         case DM_TABLE_DEPS:
530                 if (entering(tcp) || syserror(tcp))
531                         break;
532                 dm_decode_dm_target_deps(tcp, arg, ioc);
533                 break;
534         case DM_LIST_DEVICES:
535                 if (entering(tcp) || syserror(tcp))
536                         break;
537                 dm_decode_dm_name_list(tcp, arg, ioc);
538                 break;
539         case DM_LIST_VERSIONS:
540                 if (entering(tcp) || syserror(tcp))
541                         break;
542                 dm_decode_dm_target_versions(tcp, arg, ioc);
543                 break;
544         case DM_TARGET_MSG:
545                 if (entering(tcp))
546                         dm_decode_dm_target_msg(tcp, arg, ioc);
547                 else if (!syserror(tcp) && ioc->flags & DM_DATA_OUT_FLAG)
548                         dm_decode_string(tcp, arg, ioc);
549                 break;
550         case DM_DEV_RENAME:
551         case DM_DEV_SET_GEOMETRY:
552                 if (exiting(tcp))
553                         break;
554                 dm_decode_string(tcp, arg, ioc);
555                 break;
556         }
557
558  skip:
559         tprints("}");
560         return entering(tcp) ? 0 : RVAL_IOCTL_DECODED;
561 }
562
563 int
564 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
565 {
566         switch (code) {
567         case DM_VERSION:
568         case DM_REMOVE_ALL:
569         case DM_LIST_DEVICES:
570         case DM_DEV_CREATE:
571         case DM_DEV_REMOVE:
572         case DM_DEV_RENAME:
573         case DM_DEV_SUSPEND:
574         case DM_DEV_STATUS:
575         case DM_DEV_WAIT:
576         case DM_TABLE_LOAD:
577         case DM_TABLE_CLEAR:
578         case DM_TABLE_DEPS:
579         case DM_TABLE_STATUS:
580         case DM_LIST_VERSIONS:
581         case DM_TARGET_MSG:
582         case DM_DEV_SET_GEOMETRY:
583         case DM_DEV_ARM_POLL:
584                 return dm_known_ioctl(tcp, code, arg);
585         default:
586                 return RVAL_DECODED;
587         }
588 }
589
590 # else /* !(DM_VERSION_MAJOR == 4) */
591
592 int
593 dm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
594 {
595         return RVAL_DECODED;
596 }
597
598 # endif /* DM_VERSION_MAJOR == 4 */
599 #endif /* HAVE_LINUX_DM_IOCTL_H */