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.

nlattr.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. * SPDX-License-Identifier: LGPL-2.1-or-later
  8. */
  9. #include "defs.h"
  10. #include <endian.h>
  11. #include "netlink.h"
  12. #include "nlattr.h"
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <linux/sock_diag.h>
  16. #include "static_assert.h"
  17. #include "xlat/netlink_sk_meminfo_indices.h"
  18. static bool
  19. fetch_nlattr(struct tcb *const tcp, struct nlattr *const nlattr,
  20. const kernel_ulong_t addr, const unsigned int len,
  21. const bool in_array)
  22. {
  23. if (len < sizeof(struct nlattr)) {
  24. printstr_ex(tcp, addr, len, QUOTE_FORCE_HEX);
  25. return false;
  26. }
  27. if (tfetch_obj(tcp, addr, nlattr))
  28. return true;
  29. if (in_array) {
  30. tprints("...");
  31. printaddr_comment(addr);
  32. } else {
  33. printaddr(addr);
  34. }
  35. return false;
  36. }
  37. static void
  38. print_nlattr(const struct nlattr *const nla,
  39. const struct xlat *const table,
  40. const char *const dflt)
  41. {
  42. static_assert(NLA_TYPE_MASK == ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER),
  43. "wrong NLA_TYPE_MASK");
  44. tprintf("{nla_len=%u, nla_type=", nla->nla_len);
  45. if (nla->nla_type & NLA_F_NESTED) {
  46. print_xlat(NLA_F_NESTED);
  47. tprints("|");
  48. }
  49. if (nla->nla_type & NLA_F_NET_BYTEORDER) {
  50. print_xlat(NLA_F_NET_BYTEORDER);
  51. tprints("|");
  52. }
  53. printxval(table, nla->nla_type & NLA_TYPE_MASK, dflt);
  54. tprints("}");
  55. }
  56. static void
  57. decode_nlattr_with_data(struct tcb *const tcp,
  58. const struct nlattr *const nla,
  59. const kernel_ulong_t addr,
  60. const unsigned int len,
  61. const struct xlat *const table,
  62. const char *const dflt,
  63. const nla_decoder_t *const decoders,
  64. const unsigned int size,
  65. const void *const opaque_data)
  66. {
  67. const unsigned int nla_len = MIN(nla->nla_len, len);
  68. if (nla_len > NLA_HDRLEN)
  69. tprints("{");
  70. print_nlattr(nla, table, dflt);
  71. if (nla_len > NLA_HDRLEN) {
  72. const unsigned int idx =
  73. size ? nla->nla_type & NLA_TYPE_MASK : 0;
  74. tprints(", ");
  75. if (!decoders
  76. || (size && idx >= size)
  77. || !decoders[idx]
  78. || !decoders[idx](
  79. tcp, addr + NLA_HDRLEN,
  80. nla_len - NLA_HDRLEN,
  81. size ? opaque_data
  82. : (const void *) (uintptr_t) nla->nla_type)
  83. )
  84. printstr_ex(tcp, addr + NLA_HDRLEN,
  85. nla_len - NLA_HDRLEN, QUOTE_FORCE_HEX);
  86. tprints("}");
  87. }
  88. }
  89. void
  90. decode_nlattr(struct tcb *const tcp,
  91. kernel_ulong_t addr,
  92. unsigned int len,
  93. const struct xlat *const table,
  94. const char *const dflt,
  95. const nla_decoder_t *const decoders,
  96. const unsigned int size,
  97. const void *const opaque_data)
  98. {
  99. struct nlattr nla;
  100. bool is_array = false;
  101. unsigned int elt;
  102. if (decoders && !size && opaque_data)
  103. error_func_msg("[xlat %p, dflt \"%s\", decoders %p] "
  104. "size is zero (going to pass nla_type as "
  105. "decoder argument), but opaque data (%p) is not "
  106. "- will be ignored",
  107. table, dflt, decoders, opaque_data);
  108. for (elt = 0; fetch_nlattr(tcp, &nla, addr, len, is_array); elt++) {
  109. if (abbrev(tcp) && elt == max_strlen) {
  110. tprints("...");
  111. break;
  112. }
  113. const unsigned int nla_len = NLA_ALIGN(nla.nla_len);
  114. kernel_ulong_t next_addr = 0;
  115. unsigned int next_len = 0;
  116. if (nla.nla_len >= NLA_HDRLEN) {
  117. next_len = (len >= nla_len) ? len - nla_len : 0;
  118. if (next_len && addr + nla_len > addr)
  119. next_addr = addr + nla_len;
  120. }
  121. if (!is_array && next_addr) {
  122. tprints("[");
  123. is_array = true;
  124. }
  125. decode_nlattr_with_data(tcp, &nla, addr, len, table, dflt,
  126. decoders, size, opaque_data);
  127. if (!next_addr)
  128. break;
  129. tprints(", ");
  130. addr = next_addr;
  131. len = next_len;
  132. }
  133. if (is_array) {
  134. tprints("]");
  135. }
  136. }
  137. bool
  138. decode_nla_str(struct tcb *const tcp,
  139. const kernel_ulong_t addr,
  140. const unsigned int len,
  141. const void *const opaque_data)
  142. {
  143. printstr_ex(tcp, addr, len, QUOTE_0_TERMINATED);
  144. return true;
  145. }
  146. bool
  147. decode_nla_strn(struct tcb *const tcp,
  148. const kernel_ulong_t addr,
  149. const unsigned int len,
  150. const void *const opaque_data)
  151. {
  152. printstrn(tcp, addr, len);
  153. return true;
  154. }
  155. bool
  156. decode_nla_meminfo(struct tcb *const tcp,
  157. const kernel_ulong_t addr,
  158. const unsigned int len,
  159. const void *const opaque_data)
  160. {
  161. uint32_t mem;
  162. const size_t nmemb = len / sizeof(mem);
  163. if (!nmemb)
  164. return false;
  165. unsigned int count = 0;
  166. print_array_ex(tcp, addr, nmemb, &mem, sizeof(mem),
  167. tfetch_mem, print_uint32_array_member, &count,
  168. PAF_PRINT_INDICES | PAF_INDEX_XLAT_VALUE_INDEXED
  169. | XLAT_STYLE_FMT_U,
  170. ARRSZ_PAIR(netlink_sk_meminfo_indices) - 1,
  171. "SK_MEMINFO_???");
  172. return true;
  173. }
  174. bool
  175. decode_nla_fd(struct tcb *const tcp,
  176. const kernel_ulong_t addr,
  177. const unsigned int len,
  178. const void *const opaque_data)
  179. {
  180. int fd;
  181. if (len < sizeof(fd))
  182. return false;
  183. else if (!umove_or_printaddr(tcp, addr, &fd))
  184. printfd(tcp, fd);
  185. return true;
  186. }
  187. bool
  188. decode_nla_uid(struct tcb *const tcp,
  189. const kernel_ulong_t addr,
  190. const unsigned int len,
  191. const void *const opaque_data)
  192. {
  193. uint32_t uid;
  194. if (len < sizeof(uid))
  195. return false;
  196. else if (!umove_or_printaddr(tcp, addr, &uid))
  197. printuid("", uid);
  198. return true;
  199. }
  200. bool
  201. decode_nla_gid(struct tcb *const tcp,
  202. const kernel_ulong_t addr,
  203. const unsigned int len,
  204. const void *const opaque_data)
  205. {
  206. return decode_nla_uid(tcp, addr, len, opaque_data);
  207. }
  208. bool
  209. decode_nla_ifindex(struct tcb *const tcp,
  210. const kernel_ulong_t addr,
  211. const unsigned int len,
  212. const void *const opaque_data)
  213. {
  214. uint32_t ifindex;
  215. if (len < sizeof(ifindex))
  216. return false;
  217. else if (!umove_or_printaddr(tcp, addr, &ifindex))
  218. print_ifindex(ifindex);
  219. return true;
  220. }
  221. bool
  222. decode_nla_xval(struct tcb *const tcp,
  223. const kernel_ulong_t addr,
  224. unsigned int len,
  225. const void *const opaque_data)
  226. {
  227. const struct decode_nla_xlat_opts * const opts = opaque_data;
  228. union {
  229. uint64_t val;
  230. uint8_t bytes[sizeof(uint64_t)];
  231. } data = { .val = 0 };
  232. if (len > sizeof(data) || len < opts->size)
  233. return false;
  234. if (opts->size)
  235. len = MIN(len, opts->size);
  236. const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
  237. if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs)) {
  238. if (opts->process_fn)
  239. data.val = opts->process_fn(data.val);
  240. if (opts->prefix)
  241. tprints(opts->prefix);
  242. printxval_dispatch_ex(opts->xlat, opts->xlat_size, data.val,
  243. opts->dflt, opts->xt, opts->style);
  244. if (opts->suffix)
  245. tprints(opts->suffix);
  246. }
  247. return true;
  248. }
  249. static uint64_t
  250. process_host_order(uint64_t val)
  251. {
  252. return ntohs(val);
  253. }
  254. bool
  255. decode_nla_ether_proto(struct tcb *const tcp,
  256. const kernel_ulong_t addr,
  257. const unsigned int len,
  258. const void *const opaque_data)
  259. {
  260. const struct decode_nla_xlat_opts opts = {
  261. .xlat = ethernet_protocols,
  262. .xlat_size = ethernet_protocols_size,
  263. .dflt = "ETHER_P_???",
  264. .xt = XT_SORTED,
  265. .prefix = "htons(",
  266. .suffix = ")",
  267. .size = 2,
  268. .process_fn = process_host_order,
  269. };
  270. return decode_nla_xval(tcp, addr, len, &opts);
  271. }
  272. bool
  273. decode_nla_ip_proto(struct tcb *const tcp,
  274. const kernel_ulong_t addr,
  275. const unsigned int len,
  276. const void *const opaque_data)
  277. {
  278. const struct decode_nla_xlat_opts opts = {
  279. .xlat = inet_protocols,
  280. .xlat_size = inet_protocols_size,
  281. .xt = XT_SORTED,
  282. .dflt = "IPPROTO_???",
  283. .size = 1,
  284. };
  285. return decode_nla_xval(tcp, addr, len, &opts);
  286. }
  287. bool
  288. decode_nla_in_addr(struct tcb *const tcp,
  289. const kernel_ulong_t addr,
  290. const unsigned int len,
  291. const void *const opaque_data)
  292. {
  293. struct in_addr in;
  294. if (len < sizeof(in))
  295. return false;
  296. else if (!umove_or_printaddr(tcp, addr, &in))
  297. print_inet_addr(AF_INET, &in, sizeof(in), NULL);
  298. return true;
  299. }
  300. bool
  301. decode_nla_in6_addr(struct tcb *const tcp,
  302. const kernel_ulong_t addr,
  303. const unsigned int len,
  304. const void *const opaque_data)
  305. {
  306. struct in6_addr in6;
  307. if (len < sizeof(in6))
  308. return false;
  309. else if (!umove_or_printaddr(tcp, addr, &in6))
  310. print_inet_addr(AF_INET6, &in6, sizeof(in6), NULL);
  311. return true;
  312. }
  313. bool
  314. decode_nla_flags(struct tcb *const tcp,
  315. const kernel_ulong_t addr,
  316. unsigned int len,
  317. const void *const opaque_data)
  318. {
  319. const struct decode_nla_xlat_opts * const opts = opaque_data;
  320. union {
  321. uint64_t flags;
  322. uint8_t bytes[sizeof(uint64_t)];
  323. } data = { .flags = 0 };
  324. if (len > sizeof(data) || len < opts->size)
  325. return false;
  326. if (opts->size)
  327. len = MIN(len, opts->size);
  328. const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
  329. if (opts->xt == XT_INDEXED)
  330. error_func_msg("indexed xlats are currently incompatible with "
  331. "printflags");
  332. if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs)) {
  333. if (opts->process_fn)
  334. data.flags = opts->process_fn(data.flags);
  335. if (opts->prefix)
  336. tprints(opts->prefix);
  337. printflags_ex(data.flags, opts->dflt, opts->style, opts->xlat,
  338. NULL);
  339. if (opts->suffix)
  340. tprints(opts->suffix);
  341. }
  342. return true;
  343. }
  344. bool
  345. decode_nla_be16(struct tcb *const tcp,
  346. const kernel_ulong_t addr,
  347. const unsigned int len,
  348. const void *const opaque_data)
  349. {
  350. uint16_t num;
  351. if (len < sizeof(num))
  352. return false;
  353. else if (!umove_or_printaddr(tcp, addr, &num))
  354. tprintf("htons(%u)", ntohs(num));
  355. return true;
  356. }
  357. bool
  358. decode_nla_be64(struct tcb *const tcp,
  359. const kernel_ulong_t addr,
  360. const unsigned int len,
  361. const void *const opaque_data)
  362. {
  363. #if defined HAVE_BE64TOH || defined be64toh
  364. uint64_t num;
  365. if (len < sizeof(num))
  366. return false;
  367. else if (!umove_or_printaddr(tcp, addr, &num))
  368. tprintf("htobe64(%" PRIu64 ")", be64toh(num));
  369. return true;
  370. #else
  371. return false;
  372. #endif
  373. }
  374. #define DECODE_NLA_INTEGER(name, type, fmt) \
  375. bool \
  376. decode_nla_ ## name(struct tcb *const tcp, \
  377. const kernel_ulong_t addr, \
  378. const unsigned int len, \
  379. const void *const opaque_data) \
  380. { \
  381. type num; \
  382. \
  383. if (len < sizeof(num)) \
  384. return false; \
  385. if (!umove_or_printaddr(tcp, addr, &num)) \
  386. tprintf(fmt, num); \
  387. return true; \
  388. }
  389. DECODE_NLA_INTEGER(x8, uint8_t, "%#" PRIx8)
  390. DECODE_NLA_INTEGER(x16, uint16_t, "%#" PRIx16)
  391. DECODE_NLA_INTEGER(x32, uint32_t, "%#" PRIx32)
  392. DECODE_NLA_INTEGER(x64, uint64_t, "%#" PRIx64)
  393. DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
  394. DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
  395. DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
  396. DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
  397. DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
  398. DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
  399. DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
  400. DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)