]> granicus.if.org Git - strace/blob - socketutils.c
socketutils: do not request NDIAG_SHOW_MEMINFO
[strace] / socketutils.c
1 /*
2  * Copyright (c) 2014 Zubin Mithra <zubin.mithra@gmail.com>
3  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2014-2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "defs.h"
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include "netlink.h"
35 #include <linux/sock_diag.h>
36 #include <linux/inet_diag.h>
37 #include <linux/unix_diag.h>
38 #include <linux/netlink_diag.h>
39 #include <linux/rtnetlink.h>
40 #if HAVE_LINUX_GENETLINK_H
41 #include <linux/genetlink.h>
42 #endif
43
44 #include <sys/un.h>
45 #ifndef UNIX_PATH_MAX
46 # define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) 0)->sun_path)
47 #endif
48
49 #include "xstring.h"
50
51 typedef struct {
52         unsigned long inode;
53         char *details;
54 } cache_entry;
55
56 #define CACHE_SIZE 1024U
57 static cache_entry cache[CACHE_SIZE];
58 #define CACHE_MASK (CACHE_SIZE - 1)
59
60 static int
61 cache_inode_details(const unsigned long inode, char *const details)
62 {
63         cache_entry *e = &cache[inode & CACHE_MASK];
64         free(e->details);
65         e->inode = inode;
66         e->details = details;
67
68         return 1;
69 }
70
71 static const char *
72 get_sockaddr_by_inode_cached(const unsigned long inode)
73 {
74         const cache_entry *const e = &cache[inode & CACHE_MASK];
75         return (e && inode == e->inode) ? e->details : NULL;
76 }
77
78 static bool
79 print_sockaddr_by_inode_cached(const unsigned long inode)
80 {
81         const char *const details = get_sockaddr_by_inode_cached(inode);
82         if (details) {
83                 tprints(details);
84                 return true;
85         }
86         return false;
87 }
88
89 static bool
90 send_query(struct tcb *tcp, const int fd, void *req, size_t req_size)
91 {
92         struct sockaddr_nl nladdr = {
93                 .nl_family = AF_NETLINK
94         };
95         struct iovec iov = {
96                 .iov_base = req,
97                 .iov_len = req_size
98         };
99         const struct msghdr msg = {
100                 .msg_name = &nladdr,
101                 .msg_namelen = sizeof(nladdr),
102                 .msg_iov = &iov,
103                 .msg_iovlen = 1
104         };
105
106         for (;;) {
107                 if (sendmsg(fd, &msg, 0) < 0) {
108                         if (errno == EINTR)
109                                 continue;
110                         return false;
111                 }
112                 return true;
113         }
114 }
115
116 static bool
117 inet_send_query(struct tcb *tcp, const int fd, const int family,
118                 const int proto)
119 {
120         struct {
121                 const struct nlmsghdr nlh;
122                 const struct inet_diag_req_v2 idr;
123         } req = {
124                 .nlh = {
125                         .nlmsg_len = sizeof(req),
126                         .nlmsg_type = SOCK_DIAG_BY_FAMILY,
127                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
128                 },
129                 .idr = {
130                         .sdiag_family = family,
131                         .sdiag_protocol = proto,
132                         .idiag_states = -1
133                 }
134         };
135         return send_query(tcp, fd, &req, sizeof(req));
136 }
137
138 static int
139 inet_parse_response(const void *const data, const int data_len,
140                     const unsigned long inode, void *opaque_data)
141 {
142         const char *const proto_name = opaque_data;
143         const struct inet_diag_msg *const diag_msg = data;
144         static const char zero_addr[sizeof(struct in6_addr)];
145         socklen_t addr_size, text_size;
146
147         if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
148                 return -1;
149         if (diag_msg->idiag_inode != inode)
150                 return 0;
151
152         switch (diag_msg->idiag_family) {
153                 case AF_INET:
154                         addr_size = sizeof(struct in_addr);
155                         text_size = INET_ADDRSTRLEN;
156                         break;
157                 case AF_INET6:
158                         addr_size = sizeof(struct in6_addr);
159                         text_size = INET6_ADDRSTRLEN;
160                         break;
161                 default:
162                         return -1;
163         }
164
165         char src_buf[text_size];
166         char *details;
167
168         /* open/closing brackets for IPv6 addresses */
169         const char *ob = diag_msg->idiag_family == AF_INET6 ? "[" : "";
170         const char *cb = diag_msg->idiag_family == AF_INET6 ? "]" : "";
171
172         if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_src,
173                        src_buf, text_size))
174                 return -1;
175
176         if (diag_msg->id.idiag_dport ||
177             memcmp(zero_addr, diag_msg->id.idiag_dst, addr_size)) {
178                 char dst_buf[text_size];
179
180                 if (!inet_ntop(diag_msg->idiag_family, diag_msg->id.idiag_dst,
181                                dst_buf, text_size))
182                         return -1;
183
184                 if (asprintf(&details, "%s:[%s%s%s:%u->%s%s%s:%u]", proto_name,
185                              ob, src_buf, cb, ntohs(diag_msg->id.idiag_sport),
186                              ob, dst_buf, cb, ntohs(diag_msg->id.idiag_dport))
187                     < 0)
188                         return false;
189         } else {
190                 if (asprintf(&details, "%s:[%s%s%s:%u]",
191                              proto_name, ob, src_buf, cb,
192                              ntohs(diag_msg->id.idiag_sport)) < 0)
193                         return false;
194         }
195
196         return cache_inode_details(inode, details);
197 }
198
199 static bool
200 receive_responses(struct tcb *tcp, const int fd, const unsigned long inode,
201                   const unsigned long expected_msg_type,
202                   int (*parser)(const void *, int,
203                                 unsigned long, void *),
204                   void *opaque_data)
205 {
206         static union {
207                 struct nlmsghdr hdr;
208                 long buf[8192 / sizeof(long)];
209         } hdr_buf;
210
211         struct sockaddr_nl nladdr = {
212                 .nl_family = AF_NETLINK
213         };
214         struct iovec iov = {
215                 .iov_base = hdr_buf.buf,
216                 .iov_len = sizeof(hdr_buf.buf)
217         };
218         int flags = 0;
219
220         for (;;) {
221                 struct msghdr msg = {
222                         .msg_name = &nladdr,
223                         .msg_namelen = sizeof(nladdr),
224                         .msg_iov = &iov,
225                         .msg_iovlen = 1
226                 };
227
228                 ssize_t ret = recvmsg(fd, &msg, flags);
229                 if (ret < 0) {
230                         if (errno == EINTR)
231                                 continue;
232                         return false;
233                 }
234
235                 const struct nlmsghdr *h = &hdr_buf.hdr;
236                 if (!NLMSG_OK(h, ret))
237                         return false;
238                 for (; NLMSG_OK(h, ret); h = NLMSG_NEXT(h, ret)) {
239                         if (h->nlmsg_type != expected_msg_type)
240                                 return false;
241                         const int rc = parser(NLMSG_DATA(h),
242                                               h->nlmsg_len, inode, opaque_data);
243                         if (rc > 0)
244                                 return true;
245                         if (rc < 0)
246                                 return false;
247                 }
248                 flags = MSG_DONTWAIT;
249         }
250 }
251
252 static bool
253 unix_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
254 {
255         struct {
256                 const struct nlmsghdr nlh;
257                 const struct unix_diag_req udr;
258         } req = {
259                 .nlh = {
260                         .nlmsg_len = sizeof(req),
261                         .nlmsg_type = SOCK_DIAG_BY_FAMILY,
262                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
263                 },
264                 .udr = {
265                         .sdiag_family = AF_UNIX,
266                         .udiag_ino = inode,
267                         .udiag_states = -1,
268                         .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
269                 }
270         };
271         return send_query(tcp, fd, &req, sizeof(req));
272 }
273
274 static int
275 unix_parse_response(const void *data, const int data_len,
276                     const unsigned long inode, void *opaque_data)
277 {
278         const char *proto_name = opaque_data;
279         const struct unix_diag_msg *diag_msg = data;
280         struct rtattr *attr;
281         int rta_len = data_len - NLMSG_LENGTH(sizeof(*diag_msg));
282         uint32_t peer = 0;
283         size_t path_len = 0;
284         char path[UNIX_PATH_MAX + 1];
285
286         if (rta_len < 0)
287                 return -1;
288         if (diag_msg->udiag_ino != inode)
289                 return 0;
290         if (diag_msg->udiag_family != AF_UNIX)
291                 return -1;
292
293         for (attr = (struct rtattr *) (diag_msg + 1);
294              RTA_OK(attr, rta_len);
295              attr = RTA_NEXT(attr, rta_len)) {
296                 switch (attr->rta_type) {
297                 case UNIX_DIAG_NAME:
298                         if (!path_len) {
299                                 path_len = RTA_PAYLOAD(attr);
300                                 if (path_len > UNIX_PATH_MAX)
301                                         path_len = UNIX_PATH_MAX;
302                                 memcpy(path, RTA_DATA(attr), path_len);
303                                 path[path_len] = '\0';
304                         }
305                         break;
306                 case UNIX_DIAG_PEER:
307                         if (RTA_PAYLOAD(attr) >= 4)
308                                 peer = *(uint32_t *) RTA_DATA(attr);
309                         break;
310                 }
311         }
312
313         /*
314          * print obtained information in the following format:
315          * "UNIX:[" SELF_INODE [ "->" PEER_INODE ][ "," SOCKET_FILE ] "]"
316          */
317         if (!peer && !path_len)
318                 return -1;
319
320         char peer_str[3 + sizeof(peer) * 3];
321         if (peer)
322                 xsprintf(peer_str, "->%u", peer);
323         else
324                 peer_str[0] = '\0';
325
326         const char *path_str;
327         if (path_len) {
328                 char *outstr = alloca(4 * path_len + 4);
329
330                 outstr[0] = ',';
331                 if (path[0] == '\0') {
332                         outstr[1] = '@';
333                         string_quote(path + 1, outstr + 2,
334                                      path_len - 1, QUOTE_0_TERMINATED, NULL);
335                 } else {
336                         string_quote(path, outstr + 1,
337                                      path_len, QUOTE_0_TERMINATED, NULL);
338                 }
339                 path_str = outstr;
340         } else {
341                 path_str = "";
342         }
343
344         char *details;
345         if (asprintf(&details, "%s:[%lu%s%s]", proto_name, inode,
346                      peer_str, path_str) < 0)
347                 return -1;
348
349         return cache_inode_details(inode, details);
350 }
351
352 static bool
353 netlink_send_query(struct tcb *tcp, const int fd, const unsigned long inode)
354 {
355         struct {
356                 const struct nlmsghdr nlh;
357                 const struct netlink_diag_req ndr;
358         } req = {
359                 .nlh = {
360                         .nlmsg_len = sizeof(req),
361                         .nlmsg_type = SOCK_DIAG_BY_FAMILY,
362                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
363                 },
364                 .ndr = {
365                         .sdiag_family = AF_NETLINK,
366                         .sdiag_protocol = NDIAG_PROTO_ALL
367                 }
368         };
369         return send_query(tcp, fd, &req, sizeof(req));
370 }
371
372 static int
373 netlink_parse_response(const void *data, const int data_len,
374                        const unsigned long inode, void *opaque_data)
375 {
376         const char *proto_name = opaque_data;
377         const struct netlink_diag_msg *const diag_msg = data;
378         const char *netlink_proto;
379         char *details;
380
381         if (data_len < (int) NLMSG_LENGTH(sizeof(*diag_msg)))
382                 return -1;
383         if (diag_msg->ndiag_ino != inode)
384                 return 0;
385
386         if (diag_msg->ndiag_family != AF_NETLINK)
387                 return -1;
388
389         netlink_proto = xlookup(netlink_protocols,
390                                 diag_msg->ndiag_protocol);
391
392         if (netlink_proto) {
393                 netlink_proto = STR_STRIP_PREFIX(netlink_proto, "NETLINK_");
394                 if (asprintf(&details, "%s:[%s:%u]", proto_name,
395                              netlink_proto, diag_msg->ndiag_portid) < 0)
396                         return -1;
397         } else {
398                 if (asprintf(&details, "%s:[%u]", proto_name,
399                              (unsigned) diag_msg->ndiag_protocol) < 0)
400                         return -1;
401         }
402
403         return cache_inode_details(inode, details);
404 }
405
406 static const char *
407 unix_get(struct tcb *tcp, const int fd, const unsigned long inode)
408 {
409         return unix_send_query(tcp, fd, inode)
410                 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
411                                      unix_parse_response, (void *) "UNIX")
412                 ? get_sockaddr_by_inode_cached(inode) : NULL;
413 }
414
415 static const char *
416 inet_get(struct tcb *tcp, const int fd, const int family, const int protocol,
417          const unsigned long inode, const char *proto_name)
418 {
419         return inet_send_query(tcp, fd, family, protocol)
420                 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
421                                      inet_parse_response, (void *) proto_name)
422                 ? get_sockaddr_by_inode_cached(inode) : NULL;
423 }
424
425 static const char *
426 tcp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
427 {
428         return inet_get(tcp, fd, AF_INET, IPPROTO_TCP, inode, "TCP");
429 }
430
431 static const char *
432 udp_v4_get(struct tcb *tcp, const int fd, const unsigned long inode)
433 {
434         return inet_get(tcp, fd, AF_INET, IPPROTO_UDP, inode, "UDP");
435 }
436
437 static const char *
438 tcp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
439 {
440         return inet_get(tcp, fd, AF_INET6, IPPROTO_TCP, inode, "TCPv6");
441 }
442
443 static const char *
444 udp_v6_get(struct tcb *tcp, const int fd, const unsigned long inode)
445 {
446         return inet_get(tcp, fd, AF_INET6, IPPROTO_UDP, inode, "UDPv6");
447 }
448
449 static const char *
450 netlink_get(struct tcb *tcp, const int fd, const unsigned long inode)
451 {
452         return netlink_send_query(tcp, fd, inode)
453                 && receive_responses(tcp, fd, inode, SOCK_DIAG_BY_FAMILY,
454                                      netlink_parse_response, (void *) "NETLINK")
455                 ? get_sockaddr_by_inode_cached(inode) : NULL;
456 }
457
458 static const struct {
459         const char *const name;
460         const char * (*const get)(struct tcb *, int, unsigned long);
461 } protocols[] = {
462         [SOCK_PROTO_UNIX] = { "UNIX", unix_get },
463         [SOCK_PROTO_TCP] = { "TCP", tcp_v4_get },
464         [SOCK_PROTO_UDP] = { "UDP", udp_v4_get },
465         [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_get },
466         [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_get },
467         [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_get }
468 };
469
470 enum sock_proto
471 get_proto_by_name(const char *const name)
472 {
473         unsigned int i;
474         for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
475              i < ARRAY_SIZE(protocols); ++i) {
476                 if (protocols[i].name && !strcmp(name, protocols[i].name))
477                         return (enum sock_proto) i;
478         }
479         return SOCK_PROTO_UNKNOWN;
480 }
481
482 static const char *
483 get_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
484                                const enum sock_proto proto)
485 {
486         if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
487             (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].get))
488                 return NULL;
489
490         const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
491         if (fd < 0)
492                 return NULL;
493         const char *details = NULL;
494
495         if (proto != SOCK_PROTO_UNKNOWN) {
496                 details = protocols[proto].get(tcp, fd, inode);
497         } else {
498                 unsigned int i;
499                 for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
500                      i < ARRAY_SIZE(protocols); ++i) {
501                         if (!protocols[i].get)
502                                 continue;
503                         details = protocols[i].get(tcp, fd, inode);
504                         if (details)
505                                 break;
506                 }
507         }
508
509         close(fd);
510         return details;
511 }
512
513 static bool
514 print_sockaddr_by_inode_uncached(struct tcb *tcp, const unsigned long inode,
515                                  const enum sock_proto proto)
516 {
517         const char *details = get_sockaddr_by_inode_uncached(tcp, inode, proto);
518
519         if (details) {
520                 tprints(details);
521                 return true;
522         }
523
524         if ((unsigned int) proto < ARRAY_SIZE(protocols) &&
525             protocols[proto].name) {
526                 tprintf("%s:[%lu]", protocols[proto].name, inode);
527                 return true;
528         }
529
530         return false;
531 }
532
533 /* Given an inode number of a socket, return its protocol details.  */
534 const char *
535 get_sockaddr_by_inode(struct tcb *const tcp, const int fd,
536                       const unsigned long inode)
537 {
538         const char *details = get_sockaddr_by_inode_cached(inode);
539         return details ? details :
540                 get_sockaddr_by_inode_uncached(tcp, inode, getfdproto(tcp, fd));
541 }
542
543 /* Given an inode number of a socket, print out its protocol details.  */
544 bool
545 print_sockaddr_by_inode(struct tcb *const tcp, const int fd,
546                         const unsigned long inode)
547 {
548         return print_sockaddr_by_inode_cached(inode) ? true :
549                 print_sockaddr_by_inode_uncached(tcp, inode,
550                                                  getfdproto(tcp, fd));
551 }
552
553 #ifdef HAVE_LINUX_GENETLINK_H
554 /*
555  * Managing the cache for decoding communications of Netlink GENERIC protocol
556  *
557  * As name shown Netlink GENERIC protocol is generic protocol. The
558  * numbers of msg types used in the protocol are not defined
559  * statically. Kernel defines them on demand.  So the xlat converted
560  * from header files doesn't help for decoding the protocol. Following
561  * codes are building xlat(dyxlat) at runtime.
562  */
563 static bool
564 genl_send_dump_families(struct tcb *tcp, const int fd)
565 {
566         struct {
567                 const struct nlmsghdr nlh;
568                 struct genlmsghdr gnlh;
569         } req = {
570                 .nlh = {
571                         .nlmsg_len = sizeof(req),
572                         .nlmsg_type = GENL_ID_CTRL,
573                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
574                 },
575                 .gnlh = {
576                         .cmd = CTRL_CMD_GETFAMILY,
577                 }
578         };
579         return send_query(tcp, fd, &req, sizeof(req));
580 }
581
582 static int
583 genl_parse_families_response(const void *const data,
584                              const int data_len, const unsigned long inode,
585                              void *opaque_data)
586 {
587         struct dyxlat *const dyxlat = opaque_data;
588         const struct genlmsghdr *const gnlh = data;
589         struct rtattr *attr;
590         int rta_len = data_len - NLMSG_LENGTH(sizeof(*gnlh));
591
592         char *name = NULL;
593         unsigned int name_len = 0;
594         uint16_t *id = NULL;
595
596         if (rta_len < 0)
597                 return -1;
598         if (gnlh->cmd != CTRL_CMD_NEWFAMILY)
599                 return -1;
600         if (gnlh->version != 2)
601                 return -1;
602
603         for (attr = (struct rtattr *) (gnlh + 1);
604              RTA_OK(attr, rta_len);
605              attr = RTA_NEXT(attr, rta_len)) {
606                 switch (attr->rta_type) {
607                 case CTRL_ATTR_FAMILY_NAME:
608                         if (!name) {
609                                 name = RTA_DATA(attr);
610                                 name_len = RTA_PAYLOAD(attr);
611                         }
612                         break;
613                 case CTRL_ATTR_FAMILY_ID:
614                         if (!id && RTA_PAYLOAD(attr) == sizeof(*id))
615                                 id = RTA_DATA(attr);
616                         break;
617                 }
618
619                 if (name && id) {
620                         dyxlat_add_pair(dyxlat, *id, name, name_len);
621                         name = NULL;
622                         id = NULL;
623                 }
624         }
625
626         return 0;
627 }
628
629 const struct xlat *
630 genl_families_xlat(struct tcb *tcp)
631 {
632         static struct dyxlat *dyxlat;
633
634         if (!dyxlat) {
635                 dyxlat = dyxlat_alloc(32);
636
637                 int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
638                 if (fd < 0)
639                         goto out;
640
641                 if (genl_send_dump_families(tcp, fd))
642                         receive_responses(tcp, fd, 0, GENL_ID_CTRL,
643                                           genl_parse_families_response, dyxlat);
644                 close(fd);
645         }
646
647 out:
648         return dyxlat_get(dyxlat);
649 }
650
651 #else /* !HAVE_LINUX_GENETLINK_H */
652
653 const struct xlat *
654 genl_families_xlat(struct tcb *tcp)
655 {
656         return NULL;
657 }
658 #endif