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.

msghdr.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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-2000 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org>
  7. * Copyright (c) 2016-2018 The strace developers.
  8. * All rights reserved.
  9. *
  10. * SPDX-License-Identifier: LGPL-2.1-or-later
  11. */
  12. #include "defs.h"
  13. #include "print_fields.h"
  14. #include "msghdr.h"
  15. #include <limits.h>
  16. #include <arpa/inet.h>
  17. #include <netinet/in.h>
  18. #include "xlat/msg_flags.h"
  19. #include "xlat/scmvals.h"
  20. #include "xlat/ip_cmsg_types.h"
  21. #ifndef current_wordsize
  22. struct cmsghdr32 {
  23. uint32_t cmsg_len;
  24. int cmsg_level;
  25. int cmsg_type;
  26. };
  27. #endif
  28. typedef union {
  29. char *ptr;
  30. struct cmsghdr *cmsg;
  31. #ifndef current_wordsize
  32. struct cmsghdr32 *cmsg32;
  33. #endif
  34. } union_cmsghdr;
  35. static void
  36. print_scm_rights(struct tcb *tcp, const void *cmsg_data,
  37. const unsigned int data_len)
  38. {
  39. const int *fds = cmsg_data;
  40. const unsigned int nfds = data_len / sizeof(*fds);
  41. unsigned int i;
  42. tprints("[");
  43. for (i = 0; i < nfds; ++i) {
  44. if (i)
  45. tprints(", ");
  46. if (abbrev(tcp) && i >= max_strlen) {
  47. tprints("...");
  48. break;
  49. }
  50. printfd(tcp, fds[i]);
  51. }
  52. tprints("]");
  53. }
  54. static void
  55. print_scm_creds(struct tcb *tcp, const void *cmsg_data,
  56. const unsigned int data_len)
  57. {
  58. const struct ucred *uc = cmsg_data;
  59. PRINT_FIELD_U("{", *uc, pid);
  60. PRINT_FIELD_UID(", ", *uc, uid);
  61. PRINT_FIELD_UID(", ", *uc, gid);
  62. tprints("}");
  63. }
  64. static void
  65. print_scm_security(struct tcb *tcp, const void *cmsg_data,
  66. const unsigned int data_len)
  67. {
  68. print_quoted_string(cmsg_data, data_len, 0);
  69. }
  70. static void
  71. print_scm_timestamp(struct tcb *tcp, const void *cmsg_data,
  72. const unsigned int data_len)
  73. {
  74. print_struct_timeval_data_size(cmsg_data, data_len);
  75. }
  76. static void
  77. print_scm_timestampns(struct tcb *tcp, const void *cmsg_data,
  78. const unsigned int data_len)
  79. {
  80. print_struct_timespec_data_size(cmsg_data, data_len);
  81. }
  82. static void
  83. print_scm_timestamping(struct tcb *tcp, const void *cmsg_data,
  84. const unsigned int data_len)
  85. {
  86. print_struct_timespec_array_data_size(cmsg_data, 3, data_len);
  87. }
  88. static void
  89. print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
  90. const unsigned int data_len)
  91. {
  92. const struct in_pktinfo *info = cmsg_data;
  93. PRINT_FIELD_IFINDEX("{", *info, ipi_ifindex);
  94. PRINT_FIELD_INET_ADDR(", ", *info, ipi_spec_dst, AF_INET);
  95. PRINT_FIELD_INET_ADDR(", ", *info, ipi_addr, AF_INET);
  96. tprints("}");
  97. }
  98. static void
  99. print_cmsg_uint(struct tcb *tcp, const void *cmsg_data,
  100. const unsigned int data_len)
  101. {
  102. const unsigned int *p = cmsg_data;
  103. tprintf("[%u]", *p);
  104. }
  105. static void
  106. print_cmsg_uint8_t(struct tcb *tcp, const void *cmsg_data,
  107. const unsigned int data_len)
  108. {
  109. const uint8_t *p = cmsg_data;
  110. tprintf("[%#x]", *p);
  111. }
  112. static void
  113. print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
  114. const unsigned int data_len)
  115. {
  116. const unsigned char *opts = cmsg_data;
  117. unsigned int i;
  118. tprints("[");
  119. for (i = 0; i < data_len; ++i) {
  120. if (i)
  121. tprints(", ");
  122. if (abbrev(tcp) && i >= max_strlen) {
  123. tprints("...");
  124. break;
  125. }
  126. tprintf("0x%02x", opts[i]);
  127. }
  128. tprints("]");
  129. }
  130. struct sock_ee {
  131. uint32_t ee_errno;
  132. uint8_t ee_origin;
  133. uint8_t ee_type;
  134. uint8_t ee_code;
  135. uint8_t ee_pad;
  136. uint32_t ee_info;
  137. uint32_t ee_data;
  138. struct sockaddr_in offender;
  139. };
  140. static void
  141. print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
  142. const unsigned int data_len)
  143. {
  144. const struct sock_ee *const err = cmsg_data;
  145. PRINT_FIELD_U("{", *err, ee_errno);
  146. PRINT_FIELD_U(", ", *err, ee_origin);
  147. PRINT_FIELD_U(", ", *err, ee_type);
  148. PRINT_FIELD_U(", ", *err, ee_code);
  149. PRINT_FIELD_U(", ", *err, ee_info);
  150. PRINT_FIELD_U(", ", *err, ee_data);
  151. PRINT_FIELD_SOCKADDR(", ", *err, offender);
  152. tprints("}");
  153. }
  154. static void
  155. print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
  156. const unsigned int data_len)
  157. {
  158. const unsigned int addr_len =
  159. data_len > sizeof(struct sockaddr_storage)
  160. ? sizeof(struct sockaddr_storage) : data_len;
  161. print_sockaddr(cmsg_data, addr_len);
  162. }
  163. typedef void (* const cmsg_printer)(struct tcb *, const void *, unsigned int);
  164. static const struct {
  165. const cmsg_printer printer;
  166. const unsigned int min_len;
  167. } cmsg_socket_printers[] = {
  168. [SCM_RIGHTS] = { print_scm_rights, sizeof(int) },
  169. [SCM_CREDENTIALS] = { print_scm_creds, sizeof(struct ucred) },
  170. [SCM_SECURITY] = { print_scm_security, 1 },
  171. [SCM_TIMESTAMP] = { print_scm_timestamp, 1 },
  172. [SCM_TIMESTAMPNS] = { print_scm_timestampns, 1 },
  173. [SCM_TIMESTAMPING] = { print_scm_timestamping, 1 }
  174. }, cmsg_ip_printers[] = {
  175. [IP_PKTINFO] = { print_cmsg_ip_pktinfo, sizeof(struct in_pktinfo) },
  176. [IP_TTL] = { print_cmsg_uint, sizeof(unsigned int) },
  177. [IP_TOS] = { print_cmsg_uint8_t, 1 },
  178. [IP_RECVOPTS] = { print_cmsg_ip_opts, 1 },
  179. [IP_RETOPTS] = { print_cmsg_ip_opts, 1 },
  180. [IP_RECVERR] = { print_cmsg_ip_recverr, sizeof(struct sock_ee) },
  181. [IP_ORIGDSTADDR] = { print_cmsg_ip_origdstaddr, sizeof(struct sockaddr_in) },
  182. [IP_CHECKSUM] = { print_cmsg_uint, sizeof(unsigned int) },
  183. [SCM_SECURITY] = { print_scm_security, 1 }
  184. };
  185. static void
  186. print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
  187. const void *cmsg_data, const unsigned int data_len)
  188. {
  189. const unsigned int utype = cmsg_type;
  190. switch (cmsg_level) {
  191. case SOL_SOCKET:
  192. printxval(scmvals, cmsg_type, "SCM_???");
  193. if (utype < ARRAY_SIZE(cmsg_socket_printers)
  194. && cmsg_socket_printers[utype].printer
  195. && data_len >= cmsg_socket_printers[utype].min_len) {
  196. tprints(", cmsg_data=");
  197. cmsg_socket_printers[utype].printer(tcp, cmsg_data, data_len);
  198. }
  199. break;
  200. case SOL_IP:
  201. printxval(ip_cmsg_types, cmsg_type, "IP_???");
  202. if (utype < ARRAY_SIZE(cmsg_ip_printers)
  203. && cmsg_ip_printers[utype].printer
  204. && data_len >= cmsg_ip_printers[utype].min_len) {
  205. tprints(", cmsg_data=");
  206. cmsg_ip_printers[utype].printer(tcp, cmsg_data, data_len);
  207. }
  208. break;
  209. default:
  210. tprintf("%#x", cmsg_type);
  211. }
  212. }
  213. static unsigned int
  214. get_optmem_max(struct tcb *tcp)
  215. {
  216. static int optmem_max;
  217. if (!optmem_max) {
  218. if (read_int_from_file(tcp, "/proc/sys/net/core/optmem_max",
  219. &optmem_max) || optmem_max <= 0) {
  220. optmem_max = sizeof(long long) * (2 * IOV_MAX + 512);
  221. } else {
  222. optmem_max = (optmem_max + sizeof(long long) - 1)
  223. & ~(sizeof(long long) - 1);
  224. }
  225. }
  226. return optmem_max;
  227. }
  228. static void
  229. decode_msg_control(struct tcb *const tcp, const kernel_ulong_t addr,
  230. const kernel_ulong_t in_control_len)
  231. {
  232. if (!in_control_len)
  233. return;
  234. tprints(", msg_control=");
  235. const unsigned int cmsg_size =
  236. #ifndef current_wordsize
  237. (current_wordsize < sizeof(long)) ? sizeof(struct cmsghdr32) :
  238. #endif
  239. sizeof(struct cmsghdr);
  240. unsigned int control_len = in_control_len > get_optmem_max(tcp)
  241. ? get_optmem_max(tcp) : in_control_len;
  242. unsigned int buf_len = control_len;
  243. char *buf = buf_len < cmsg_size ? NULL : malloc(buf_len);
  244. if (!buf || umoven(tcp, addr, buf_len, buf) < 0) {
  245. printaddr(addr);
  246. free(buf);
  247. return;
  248. }
  249. union_cmsghdr u = { .ptr = buf };
  250. tprints("[");
  251. while (buf_len >= cmsg_size) {
  252. const kernel_ulong_t cmsg_len =
  253. #ifndef current_wordsize
  254. (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_len :
  255. #endif
  256. u.cmsg->cmsg_len;
  257. const int cmsg_level =
  258. #ifndef current_wordsize
  259. (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_level :
  260. #endif
  261. u.cmsg->cmsg_level;
  262. const int cmsg_type =
  263. #ifndef current_wordsize
  264. (current_wordsize < sizeof(long)) ? u.cmsg32->cmsg_type :
  265. #endif
  266. u.cmsg->cmsg_type;
  267. if (u.ptr != buf)
  268. tprints(", ");
  269. tprintf("{cmsg_len=%" PRI_klu ", cmsg_level=", cmsg_len);
  270. printxval(socketlayers, cmsg_level, "SOL_???");
  271. tprints(", cmsg_type=");
  272. kernel_ulong_t len = cmsg_len > buf_len ? buf_len : cmsg_len;
  273. print_cmsg_type_data(tcp, cmsg_level, cmsg_type,
  274. (const void *) (u.ptr + cmsg_size),
  275. len > cmsg_size ? len - cmsg_size : 0);
  276. tprints("}");
  277. if (len < cmsg_size) {
  278. buf_len -= cmsg_size;
  279. break;
  280. }
  281. len = (cmsg_len + current_wordsize - 1) &
  282. ~((kernel_ulong_t) current_wordsize - 1);
  283. if (len >= buf_len) {
  284. buf_len = 0;
  285. break;
  286. }
  287. u.ptr += len;
  288. buf_len -= len;
  289. }
  290. if (buf_len) {
  291. tprints(", ...");
  292. printaddr_comment(addr + (control_len - buf_len));
  293. } else if (control_len < in_control_len) {
  294. tprints(", ...");
  295. }
  296. tprints("]");
  297. free(buf);
  298. }
  299. void
  300. print_struct_msghdr(struct tcb *tcp, const struct msghdr *msg,
  301. const int *const p_user_msg_namelen,
  302. const kernel_ulong_t data_size)
  303. {
  304. const int msg_namelen =
  305. p_user_msg_namelen && (int) msg->msg_namelen > *p_user_msg_namelen
  306. ? *p_user_msg_namelen : (int) msg->msg_namelen;
  307. tprints("{msg_name=");
  308. const int family =
  309. decode_sockaddr(tcp, ptr_to_kulong(msg->msg_name), msg_namelen);
  310. const enum iov_decode decode =
  311. (family == AF_NETLINK) ? IOV_DECODE_NETLINK : IOV_DECODE_STR;
  312. tprints(", msg_namelen=");
  313. if (p_user_msg_namelen && *p_user_msg_namelen != (int) msg->msg_namelen)
  314. tprintf("%d->", *p_user_msg_namelen);
  315. tprintf("%d", msg->msg_namelen);
  316. tprints(", msg_iov=");
  317. tprint_iov_upto(tcp, msg->msg_iovlen,
  318. ptr_to_kulong(msg->msg_iov), decode, data_size);
  319. PRINT_FIELD_U(", ", *msg, msg_iovlen);
  320. decode_msg_control(tcp, ptr_to_kulong(msg->msg_control),
  321. msg->msg_controllen);
  322. PRINT_FIELD_U(", ", *msg, msg_controllen);
  323. PRINT_FIELD_FLAGS(", ", *msg, msg_flags, msg_flags, "MSG_???");
  324. tprints("}");
  325. }
  326. static bool
  327. fetch_msghdr_namelen(struct tcb *const tcp, const kernel_ulong_t addr,
  328. int *const p_msg_namelen)
  329. {
  330. struct msghdr msg;
  331. if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg)) {
  332. *p_msg_namelen = msg.msg_namelen;
  333. return true;
  334. } else {
  335. return false;
  336. }
  337. }
  338. static void
  339. decode_msghdr(struct tcb *const tcp, const int *const p_user_msg_namelen,
  340. const kernel_ulong_t addr, const kernel_ulong_t data_size)
  341. {
  342. struct msghdr msg;
  343. if (addr && verbose(tcp) && fetch_struct_msghdr(tcp, addr, &msg))
  344. print_struct_msghdr(tcp, &msg, p_user_msg_namelen, data_size);
  345. else
  346. printaddr(addr);
  347. }
  348. void
  349. dumpiov_in_msghdr(struct tcb *const tcp, const kernel_ulong_t addr,
  350. const kernel_ulong_t data_size)
  351. {
  352. struct msghdr msg;
  353. if (fetch_struct_msghdr(tcp, addr, &msg)) {
  354. dumpiov_upto(tcp, msg.msg_iovlen,
  355. ptr_to_kulong(msg.msg_iov), data_size);
  356. }
  357. }
  358. SYS_FUNC(sendmsg)
  359. {
  360. printfd(tcp, tcp->u_arg[0]);
  361. tprints(", ");
  362. decode_msghdr(tcp, 0, tcp->u_arg[1], -1);
  363. /* flags */
  364. tprints(", ");
  365. printflags(msg_flags, tcp->u_arg[2], "MSG_???");
  366. return RVAL_DECODED;
  367. }
  368. SYS_FUNC(recvmsg)
  369. {
  370. int msg_namelen;
  371. if (entering(tcp)) {
  372. printfd(tcp, tcp->u_arg[0]);
  373. tprints(", ");
  374. if (fetch_msghdr_namelen(tcp, tcp->u_arg[1], &msg_namelen)) {
  375. set_tcb_priv_ulong(tcp, msg_namelen);
  376. return 0;
  377. }
  378. printaddr(tcp->u_arg[1]);
  379. } else {
  380. msg_namelen = get_tcb_priv_ulong(tcp);
  381. if (syserror(tcp))
  382. tprintf("{msg_namelen=%d}", msg_namelen);
  383. else
  384. decode_msghdr(tcp, &msg_namelen, tcp->u_arg[1],
  385. tcp->u_rval);
  386. }
  387. /* flags */
  388. tprints(", ");
  389. printflags(msg_flags, tcp->u_arg[2], "MSG_???");
  390. return RVAL_DECODED;
  391. }