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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 | XLAT_STYLE_FMT_U,
  169. netlink_sk_meminfo_indices, "SK_MEMINFO_???");
  170. return true;
  171. }
  172. bool
  173. decode_nla_fd(struct tcb *const tcp,
  174. const kernel_ulong_t addr,
  175. const unsigned int len,
  176. const void *const opaque_data)
  177. {
  178. int fd;
  179. if (len < sizeof(fd))
  180. return false;
  181. else if (!umove_or_printaddr(tcp, addr, &fd))
  182. printfd(tcp, fd);
  183. return true;
  184. }
  185. bool
  186. decode_nla_uid(struct tcb *const tcp,
  187. const kernel_ulong_t addr,
  188. const unsigned int len,
  189. const void *const opaque_data)
  190. {
  191. uint32_t uid;
  192. if (len < sizeof(uid))
  193. return false;
  194. else if (!umove_or_printaddr(tcp, addr, &uid))
  195. printuid("", uid);
  196. return true;
  197. }
  198. bool
  199. decode_nla_gid(struct tcb *const tcp,
  200. const kernel_ulong_t addr,
  201. const unsigned int len,
  202. const void *const opaque_data)
  203. {
  204. return decode_nla_uid(tcp, addr, len, opaque_data);
  205. }
  206. bool
  207. decode_nla_ifindex(struct tcb *const tcp,
  208. const kernel_ulong_t addr,
  209. const unsigned int len,
  210. const void *const opaque_data)
  211. {
  212. uint32_t ifindex;
  213. if (len < sizeof(ifindex))
  214. return false;
  215. else if (!umove_or_printaddr(tcp, addr, &ifindex))
  216. print_ifindex(ifindex);
  217. return true;
  218. }
  219. bool
  220. decode_nla_xval(struct tcb *const tcp,
  221. const kernel_ulong_t addr,
  222. unsigned int len,
  223. const void *const opaque_data)
  224. {
  225. const struct decode_nla_xlat_opts * const opts = opaque_data;
  226. union {
  227. uint64_t val;
  228. uint8_t bytes[sizeof(uint64_t)];
  229. } data = { .val = 0 };
  230. if (len > sizeof(data) || len < opts->size)
  231. return false;
  232. if (opts->size)
  233. len = MIN(len, opts->size);
  234. const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
  235. if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs)) {
  236. if (opts->process_fn)
  237. data.val = opts->process_fn(data.val);
  238. if (opts->prefix)
  239. tprints(opts->prefix);
  240. printxval_ex(opts->xlat, data.val, opts->dflt, opts->style);
  241. if (opts->suffix)
  242. tprints(opts->suffix);
  243. }
  244. return true;
  245. }
  246. static uint64_t
  247. process_host_order(uint64_t val)
  248. {
  249. return ntohs(val);
  250. }
  251. bool
  252. decode_nla_ether_proto(struct tcb *const tcp,
  253. const kernel_ulong_t addr,
  254. const unsigned int len,
  255. const void *const opaque_data)
  256. {
  257. static const struct decode_nla_xlat_opts opts = {
  258. .xlat = ethernet_protocols,
  259. .dflt = "ETHER_P_???",
  260. .prefix = "htons(",
  261. .suffix = ")",
  262. .size = 2,
  263. .process_fn = process_host_order,
  264. };
  265. return decode_nla_xval(tcp, addr, len, &opts);
  266. }
  267. bool
  268. decode_nla_ip_proto(struct tcb *const tcp,
  269. const kernel_ulong_t addr,
  270. const unsigned int len,
  271. const void *const opaque_data)
  272. {
  273. static const struct decode_nla_xlat_opts opts = {
  274. .xlat = inet_protocols,
  275. .dflt = "IPPROTO_???",
  276. .size = 1,
  277. };
  278. return decode_nla_xval(tcp, addr, len, &opts);
  279. }
  280. bool
  281. decode_nla_in_addr(struct tcb *const tcp,
  282. const kernel_ulong_t addr,
  283. const unsigned int len,
  284. const void *const opaque_data)
  285. {
  286. struct in_addr in;
  287. if (len < sizeof(in))
  288. return false;
  289. else if (!umove_or_printaddr(tcp, addr, &in))
  290. print_inet_addr(AF_INET, &in, sizeof(in), NULL);
  291. return true;
  292. }
  293. bool
  294. decode_nla_in6_addr(struct tcb *const tcp,
  295. const kernel_ulong_t addr,
  296. const unsigned int len,
  297. const void *const opaque_data)
  298. {
  299. struct in6_addr in6;
  300. if (len < sizeof(in6))
  301. return false;
  302. else if (!umove_or_printaddr(tcp, addr, &in6))
  303. print_inet_addr(AF_INET6, &in6, sizeof(in6), NULL);
  304. return true;
  305. }
  306. bool
  307. decode_nla_flags(struct tcb *const tcp,
  308. const kernel_ulong_t addr,
  309. 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. if (len > sizeof(data) || len < opts->size)
  318. return false;
  319. if (opts->size)
  320. len = MIN(len, opts->size);
  321. const size_t bytes_offs = is_bigendian ? sizeof(data) - len : 0;
  322. if (!umoven_or_printaddr(tcp, addr, len, data.bytes + bytes_offs)) {
  323. if (opts->process_fn)
  324. data.flags = opts->process_fn(data.flags);
  325. if (opts->prefix)
  326. tprints(opts->prefix);
  327. printflags_ex(data.flags, opts->dflt, opts->style, opts->xlat,
  328. NULL);
  329. if (opts->suffix)
  330. tprints(opts->suffix);
  331. }
  332. return true;
  333. }
  334. bool
  335. decode_nla_be16(struct tcb *const tcp,
  336. const kernel_ulong_t addr,
  337. const unsigned int len,
  338. const void *const opaque_data)
  339. {
  340. uint16_t num;
  341. if (len < sizeof(num))
  342. return false;
  343. else if (!umove_or_printaddr(tcp, addr, &num))
  344. tprintf("htons(%u)", ntohs(num));
  345. return true;
  346. }
  347. bool
  348. decode_nla_be64(struct tcb *const tcp,
  349. const kernel_ulong_t addr,
  350. const unsigned int len,
  351. const void *const opaque_data)
  352. {
  353. #if defined HAVE_BE64TOH || defined be64toh
  354. uint64_t num;
  355. if (len < sizeof(num))
  356. return false;
  357. else if (!umove_or_printaddr(tcp, addr, &num))
  358. tprintf("htobe64(%" PRIu64 ")", be64toh(num));
  359. return true;
  360. #else
  361. return false;
  362. #endif
  363. }
  364. #define DECODE_NLA_INTEGER(name, type, fmt) \
  365. bool \
  366. decode_nla_ ## name(struct tcb *const tcp, \
  367. const kernel_ulong_t addr, \
  368. const unsigned int len, \
  369. const void *const opaque_data) \
  370. { \
  371. type num; \
  372. \
  373. if (len < sizeof(num)) \
  374. return false; \
  375. if (!umove_or_printaddr(tcp, addr, &num)) \
  376. tprintf(fmt, num); \
  377. return true; \
  378. }
  379. DECODE_NLA_INTEGER(x8, uint8_t, "%#" PRIx8)
  380. DECODE_NLA_INTEGER(x16, uint16_t, "%#" PRIx16)
  381. DECODE_NLA_INTEGER(x32, uint32_t, "%#" PRIx32)
  382. DECODE_NLA_INTEGER(x64, uint64_t, "%#" PRIx64)
  383. DECODE_NLA_INTEGER(u8, uint8_t, "%" PRIu8)
  384. DECODE_NLA_INTEGER(u16, uint16_t, "%" PRIu16)
  385. DECODE_NLA_INTEGER(u32, uint32_t, "%" PRIu32)
  386. DECODE_NLA_INTEGER(u64, uint64_t, "%" PRIu64)
  387. DECODE_NLA_INTEGER(s8, int8_t, "%" PRId8)
  388. DECODE_NLA_INTEGER(s16, int16_t, "%" PRId16)
  389. DECODE_NLA_INTEGER(s32, int32_t, "%" PRId32)
  390. DECODE_NLA_INTEGER(s64, int64_t, "%" PRId64)