Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ioctl.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-2001 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 1999-2018 The strace developers.
  7. * All rights reserved.
  8. *
  9. * SPDX-License-Identifier: LGPL-2.1-or-later
  10. */
  11. #include "defs.h"
  12. #include <linux/ioctl.h>
  13. #include "xlat/ioctl_dirs.h"
  14. static int
  15. compare(const void *a, const void *b)
  16. {
  17. const unsigned int code1 = (const uintptr_t) a;
  18. const unsigned int code2 = ((struct_ioctlent *) b)->code;
  19. return (code1 > code2) ? 1 : (code1 < code2) ? -1 : 0;
  20. }
  21. static const struct_ioctlent *
  22. ioctl_lookup(const unsigned int code)
  23. {
  24. struct_ioctlent *iop;
  25. iop = bsearch((const void *) (const uintptr_t) code, ioctlent,
  26. nioctlents, sizeof(ioctlent[0]), compare);
  27. while (iop > ioctlent) {
  28. iop--;
  29. if (iop->code != code) {
  30. iop++;
  31. break;
  32. }
  33. }
  34. return iop;
  35. }
  36. static const struct_ioctlent *
  37. ioctl_next_match(const struct_ioctlent *iop)
  38. {
  39. const unsigned int code = iop->code;
  40. iop++;
  41. if (iop < ioctlent + nioctlents && iop->code == code)
  42. return iop;
  43. return NULL;
  44. }
  45. static void
  46. ioctl_print_code(const unsigned int code)
  47. {
  48. tprints("_IOC(");
  49. printflags(ioctl_dirs, _IOC_DIR(code), "_IOC_???");
  50. tprintf(", %#x, %#x, %#x)",
  51. _IOC_TYPE(code), _IOC_NR(code), _IOC_SIZE(code));
  52. }
  53. static int
  54. evdev_decode_number(const unsigned int code)
  55. {
  56. const unsigned int nr = _IOC_NR(code);
  57. if (_IOC_DIR(code) == _IOC_WRITE) {
  58. if (nr >= 0xc0 && nr <= 0xc0 + 0x3f) {
  59. tprints("EVIOCSABS(");
  60. printxval_indexn(evdev_abs, evdev_abs_size, nr - 0xc0,
  61. "ABS_???");
  62. tprints(")");
  63. return 1;
  64. }
  65. }
  66. if (_IOC_DIR(code) != _IOC_READ)
  67. return 0;
  68. if (nr >= 0x20 && nr <= 0x20 + 0x1f) {
  69. tprints("EVIOCGBIT(");
  70. if (nr == 0x20)
  71. tprintf("0");
  72. else
  73. printxval(evdev_ev, nr - 0x20, "EV_???");
  74. tprintf(", %u)", _IOC_SIZE(code));
  75. return 1;
  76. } else if (nr >= 0x40 && nr <= 0x40 + 0x3f) {
  77. tprints("EVIOCGABS(");
  78. printxval_indexn(evdev_abs, evdev_abs_size, nr - 0x40,
  79. "ABS_???");
  80. tprints(")");
  81. return 1;
  82. }
  83. switch (_IOC_NR(nr)) {
  84. case 0x06:
  85. tprintf("EVIOCGNAME(%u)", _IOC_SIZE(code));
  86. return 1;
  87. case 0x07:
  88. tprintf("EVIOCGPHYS(%u)", _IOC_SIZE(code));
  89. return 1;
  90. case 0x08:
  91. tprintf("EVIOCGUNIQ(%u)", _IOC_SIZE(code));
  92. return 1;
  93. case 0x09:
  94. tprintf("EVIOCGPROP(%u)", _IOC_SIZE(code));
  95. return 1;
  96. case 0x0a:
  97. tprintf("EVIOCGMTSLOTS(%u)", _IOC_SIZE(code));
  98. return 1;
  99. case 0x18:
  100. tprintf("EVIOCGKEY(%u)", _IOC_SIZE(code));
  101. return 1;
  102. case 0x19:
  103. tprintf("EVIOCGLED(%u)", _IOC_SIZE(code));
  104. return 1;
  105. case 0x1a:
  106. tprintf("EVIOCGSND(%u)", _IOC_SIZE(code));
  107. return 1;
  108. case 0x1b:
  109. tprintf("EVIOCGSW(%u)", _IOC_SIZE(code));
  110. return 1;
  111. default:
  112. return 0;
  113. }
  114. }
  115. static int
  116. hiddev_decode_number(const unsigned int code)
  117. {
  118. if (_IOC_DIR(code) == _IOC_READ) {
  119. switch (_IOC_NR(code)) {
  120. case 0x04:
  121. tprintf("HIDIOCGRAWNAME(%u)", _IOC_SIZE(code));
  122. return 1;
  123. case 0x05:
  124. tprintf("HIDIOCGRAWPHYS(%u)", _IOC_SIZE(code));
  125. return 1;
  126. case 0x06:
  127. tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
  128. return 1;
  129. case 0x12:
  130. tprintf("HIDIOCGPHYS(%u)", _IOC_SIZE(code));
  131. return 1;
  132. default:
  133. return 0;
  134. }
  135. } else if (_IOC_DIR(code) == (_IOC_READ | _IOC_WRITE)) {
  136. switch (_IOC_NR(code)) {
  137. case 0x06:
  138. tprintf("HIDIOCSFEATURE(%u)", _IOC_SIZE(code));
  139. return 1;
  140. case 0x07:
  141. tprintf("HIDIOCGFEATURE(%u)", _IOC_SIZE(code));
  142. return 1;
  143. default:
  144. return 0;
  145. }
  146. }
  147. return 0;
  148. }
  149. static int
  150. ioctl_decode_command_number(struct tcb *tcp)
  151. {
  152. const unsigned int code = tcp->u_arg[1];
  153. switch (_IOC_TYPE(code)) {
  154. case 'E':
  155. return evdev_decode_number(code);
  156. case 'H':
  157. return hiddev_decode_number(code);
  158. case 'M':
  159. if (_IOC_DIR(code) == _IOC_WRITE) {
  160. tprintf("MIXER_WRITE(%u)", _IOC_NR(code));
  161. return 1;
  162. } else if (_IOC_DIR(code) == _IOC_READ) {
  163. tprintf("MIXER_READ(%u)", _IOC_NR(code));
  164. return 1;
  165. }
  166. return 0;
  167. case 'U':
  168. if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x2c) {
  169. tprintf("UI_GET_SYSNAME(%u)", _IOC_SIZE(code));
  170. return 1;
  171. }
  172. return 0;
  173. case 'j':
  174. if (_IOC_DIR(code) == _IOC_READ && _IOC_NR(code) == 0x13) {
  175. tprintf("JSIOCGNAME(%u)", _IOC_SIZE(code));
  176. return 1;
  177. }
  178. return 0;
  179. case 'k':
  180. if (_IOC_DIR(code) == _IOC_WRITE && _IOC_NR(code) == 0) {
  181. tprintf("SPI_IOC_MESSAGE(%u)", _IOC_SIZE(code));
  182. return 1;
  183. }
  184. return 0;
  185. default:
  186. return 0;
  187. }
  188. }
  189. /**
  190. * Decode arg parameter of the ioctl call.
  191. *
  192. * @return There are two flags of the return value important for the purposes of
  193. * processing by SYS_FUNC(ioctl):
  194. * - RVAL_IOCTL_DECODED: indicates that ioctl decoder code
  195. * has printed arg parameter;
  196. * - RVAL_DECODED: indicates that decoding is done.
  197. * As a result, the following behaviour is expected:
  198. * - on entering:
  199. * - 0: decoding should be continued on exiting;
  200. * - RVAL_IOCTL_DECODED: decoding on exiting is not needed
  201. * and decoder has printed arg value;
  202. * - RVAL_DECODED: decoding on exiting is not needed
  203. * and generic handler should print arg value.
  204. * - on exiting:
  205. * - 0: generic handler should print arg value;
  206. * - RVAL_IOCTL_DECODED: decoder has printed arg value.
  207. *
  208. * Note that it makes no sense to return just RVAL_DECODED on exiting,
  209. * but, of course, it is not prohibited (for example, it may be useful
  210. * in cases where the return path is common on entering and on exiting
  211. * the syscall).
  212. *
  213. * SYS_FUNC(ioctl) converts RVAL_IOCTL_DECODED flag to RVAL_DECODED,
  214. * and passes all other bits of ioctl_decode return value unchanged.
  215. */
  216. static int
  217. ioctl_decode(struct tcb *tcp)
  218. {
  219. const unsigned int code = tcp->u_arg[1];
  220. const kernel_ulong_t arg = tcp->u_arg[2];
  221. switch (_IOC_TYPE(code)) {
  222. case '$':
  223. return perf_ioctl(tcp, code, arg);
  224. #if defined(ALPHA) || defined(POWERPC)
  225. case 'f': {
  226. int ret = file_ioctl(tcp, code, arg);
  227. if (ret != RVAL_DECODED)
  228. return ret;
  229. ATTRIBUTE_FALLTHROUGH;
  230. }
  231. case 't':
  232. case 'T':
  233. return term_ioctl(tcp, code, arg);
  234. #else /* !ALPHA */
  235. case 'f':
  236. return file_ioctl(tcp, code, arg);
  237. case 0x54:
  238. #endif /* !ALPHA */
  239. return term_ioctl(tcp, code, arg);
  240. case 0x89:
  241. return sock_ioctl(tcp, code, arg);
  242. case 'p':
  243. return rtc_ioctl(tcp, code, arg);
  244. case 0x03:
  245. return hdio_ioctl(tcp, code, arg);
  246. case 0x12:
  247. return block_ioctl(tcp, code, arg);
  248. case 'X':
  249. return fs_x_ioctl(tcp, code, arg);
  250. case 0x22:
  251. return scsi_ioctl(tcp, code, arg);
  252. case 'L':
  253. return loop_ioctl(tcp, code, arg);
  254. #ifdef HAVE_STRUCT_MTD_WRITE_REQ
  255. case 'M':
  256. return mtd_ioctl(tcp, code, arg);
  257. #endif
  258. #ifdef HAVE_STRUCT_UBI_ATTACH_REQ_MAX_BEB_PER1024
  259. case 'o':
  260. case 'O':
  261. return ubi_ioctl(tcp, code, arg);
  262. #endif
  263. case 'V':
  264. return v4l2_ioctl(tcp, code, arg);
  265. #ifdef HAVE_STRUCT_PTP_SYS_OFFSET
  266. case '=':
  267. return ptp_ioctl(tcp, code, arg);
  268. #endif
  269. #ifdef HAVE_LINUX_INPUT_H
  270. case 'E':
  271. return evdev_ioctl(tcp, code, arg);
  272. #endif
  273. #ifdef HAVE_LINUX_USERFAULTFD_H
  274. case 0xaa:
  275. return uffdio_ioctl(tcp, code, arg);
  276. #endif
  277. #ifdef HAVE_LINUX_BTRFS_H
  278. case 0x94:
  279. return btrfs_ioctl(tcp, code, arg);
  280. #endif
  281. case 0xb7:
  282. return nsfs_ioctl(tcp, code, arg);
  283. #ifdef HAVE_LINUX_DM_IOCTL_H
  284. case 0xfd:
  285. return dm_ioctl(tcp, code, arg);
  286. #endif
  287. #ifdef HAVE_LINUX_KVM_H
  288. case 0xae:
  289. return kvm_ioctl(tcp, code, arg);
  290. #endif
  291. case 'I':
  292. return inotify_ioctl(tcp, code, arg);
  293. case 0xab:
  294. return nbd_ioctl(tcp, code, arg);
  295. case 'R':
  296. return random_ioctl(tcp, code, arg);
  297. default:
  298. break;
  299. }
  300. return 0;
  301. }
  302. SYS_FUNC(ioctl)
  303. {
  304. const struct_ioctlent *iop;
  305. int ret;
  306. if (entering(tcp)) {
  307. printfd(tcp, tcp->u_arg[0]);
  308. tprints(", ");
  309. if (xlat_verbosity != XLAT_STYLE_ABBREV)
  310. tprintf("%#x", (unsigned int) tcp->u_arg[1]);
  311. if (xlat_verbosity == XLAT_STYLE_VERBOSE)
  312. tprints(" /* ");
  313. if (xlat_verbosity != XLAT_STYLE_RAW) {
  314. ret = ioctl_decode_command_number(tcp);
  315. if (!(ret & IOCTL_NUMBER_STOP_LOOKUP)) {
  316. iop = ioctl_lookup(tcp->u_arg[1]);
  317. if (iop) {
  318. if (ret)
  319. tprints(" or ");
  320. tprints(iop->symbol);
  321. while ((iop = ioctl_next_match(iop)))
  322. tprintf(" or %s", iop->symbol);
  323. } else if (!ret) {
  324. ioctl_print_code(tcp->u_arg[1]);
  325. }
  326. }
  327. }
  328. if (xlat_verbosity == XLAT_STYLE_VERBOSE)
  329. tprints(" */");
  330. ret = ioctl_decode(tcp);
  331. } else {
  332. ret = ioctl_decode(tcp) | RVAL_DECODED;
  333. }
  334. if (ret & RVAL_IOCTL_DECODED) {
  335. ret &= ~RVAL_IOCTL_DECODED;
  336. ret |= RVAL_DECODED;
  337. } else if (ret & RVAL_DECODED) {
  338. tprintf(", %#" PRI_klx, tcp->u_arg[2]);
  339. }
  340. return ret;
  341. }