1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2007-2011 Robert N. M. Watson
5 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/capsicum.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 #include <sys/un.h>
35 #include <sys/user.h>
36
37 #include <netinet/in.h>
38
39 #include <arpa/inet.h>
40
41 #include <err.h>
42 #include <libprocstat.h>
43 #include <inttypes.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "procstat.h"
49
50 static const char *
protocol_to_string(int domain,int type,int protocol)51 protocol_to_string(int domain, int type, int protocol)
52 {
53
54 switch (domain) {
55 case AF_INET:
56 case AF_INET6:
57 switch (protocol) {
58 case IPPROTO_TCP:
59 return ("TCP");
60 case IPPROTO_UDP:
61 return ("UDP");
62 case IPPROTO_ICMP:
63 return ("ICM");
64 case IPPROTO_RAW:
65 return ("RAW");
66 case IPPROTO_SCTP:
67 return ("SCT");
68 default:
69 return ("IP?");
70 }
71
72 case AF_LOCAL:
73 switch (type) {
74 case SOCK_STREAM:
75 return ("UDS");
76 case SOCK_DGRAM:
77 return ("UDD");
78 case SOCK_SEQPACKET:
79 return ("UDQ");
80 default:
81 return ("UD?");
82 }
83 case AF_DIVERT:
84 return ("IPD");
85 break;
86 default:
87 return ("?");
88 }
89 }
90
91 static void
addr_to_string(struct sockaddr_storage * ss,char * buffer,int buflen)92 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
93 {
94 char buffer2[INET6_ADDRSTRLEN];
95 struct sockaddr_in6 *sin6;
96 struct sockaddr_in *sin;
97 struct sockaddr_un *sun;
98
99 switch (ss->ss_family) {
100 case AF_LOCAL:
101 sun = (struct sockaddr_un *)ss;
102 if (strlen(sun->sun_path) == 0)
103 strlcpy(buffer, "-", buflen);
104 else
105 strlcpy(buffer, sun->sun_path, buflen);
106 break;
107
108 case AF_INET:
109 sin = (struct sockaddr_in *)ss;
110 if (sin->sin_addr.s_addr == INADDR_ANY)
111 snprintf(buffer, buflen, "%s:%d", "*",
112 ntohs(sin->sin_port));
113 else if (inet_ntop(AF_INET, &sin->sin_addr, buffer2,
114 sizeof(buffer2)) != NULL)
115 snprintf(buffer, buflen, "%s:%d", buffer2,
116 ntohs(sin->sin_port));
117 break;
118
119 case AF_INET6:
120 sin6 = (struct sockaddr_in6 *)ss;
121 if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
122 sizeof(buffer2)) != NULL)
123 snprintf(buffer, buflen, "%s.%d", buffer2,
124 ntohs(sin6->sin6_port));
125 else
126 strlcpy(buffer, "-", buflen);
127 break;
128
129 default:
130 strlcpy(buffer, "", buflen);
131 break;
132 }
133 }
134
135 static struct cap_desc {
136 uint64_t cd_right;
137 const char *cd_desc;
138 } cap_desc[] = {
139 /* General file I/O. */
140 { CAP_READ, "rd" },
141 { CAP_WRITE, "wr" },
142 { CAP_SEEK, "se" },
143 { CAP_MMAP, "mm" },
144 { CAP_CREATE, "cr" },
145 { CAP_FEXECVE, "fe" },
146 { CAP_FSYNC, "fy" },
147 { CAP_FTRUNCATE, "ft" },
148
149 /* VFS methods. */
150 { CAP_FCHDIR, "cd" },
151 { CAP_FCHFLAGS, "cf" },
152 { CAP_FCHMOD, "cm" },
153 { CAP_FCHOWN, "cn" },
154 { CAP_FCHROOT, "ct" },
155 { CAP_FCNTL, "fc" },
156 { CAP_FLOCK, "fl" },
157 { CAP_FPATHCONF, "fp" },
158 { CAP_FSCK, "fk" },
159 { CAP_FSTAT, "fs" },
160 { CAP_FSTATFS, "sf" },
161 { CAP_FUTIMES, "fu" },
162 { CAP_LINKAT_SOURCE, "ls" },
163 { CAP_LINKAT_TARGET, "lt" },
164 { CAP_MKDIRAT, "md" },
165 { CAP_MKFIFOAT, "mf" },
166 { CAP_MKNODAT, "mn" },
167 { CAP_RENAMEAT_SOURCE, "rs" },
168 { CAP_RENAMEAT_TARGET, "rt" },
169 { CAP_SYMLINKAT, "sl" },
170 { CAP_UNLINKAT, "un" },
171
172 /* Lookups - used to constrain *at() calls. */
173 { CAP_LOOKUP, "lo" },
174
175 /* Extended attributes. */
176 { CAP_EXTATTR_GET, "eg" },
177 { CAP_EXTATTR_SET, "es" },
178 { CAP_EXTATTR_DELETE, "ed" },
179 { CAP_EXTATTR_LIST, "el" },
180
181 /* Access Control Lists. */
182 { CAP_ACL_GET, "ag" },
183 { CAP_ACL_SET, "as" },
184 { CAP_ACL_DELETE, "ad" },
185 { CAP_ACL_CHECK, "ac" },
186
187 /* Socket operations. */
188 { CAP_ACCEPT, "at" },
189 { CAP_BIND, "bd" },
190 { CAP_CONNECT, "co" },
191 { CAP_GETPEERNAME, "pn" },
192 { CAP_GETSOCKNAME, "sn" },
193 { CAP_GETSOCKOPT, "gs" },
194 { CAP_LISTEN, "ln" },
195 { CAP_PEELOFF, "pf" },
196 { CAP_SETSOCKOPT, "ss" },
197 { CAP_SHUTDOWN, "sh" },
198
199 /* Mandatory Access Control. */
200 { CAP_MAC_GET, "mg" },
201 { CAP_MAC_SET, "ms" },
202
203 /* Methods on semaphores. */
204 { CAP_SEM_GETVALUE, "sg" },
205 { CAP_SEM_POST, "sp" },
206 { CAP_SEM_WAIT, "sw" },
207
208 /* Event monitoring and posting. */
209 { CAP_EVENT, "ev" },
210 { CAP_KQUEUE_EVENT, "ke" },
211 { CAP_KQUEUE_CHANGE, "kc" },
212
213 /* Strange and powerful rights that should not be given lightly. */
214 { CAP_IOCTL, "io" },
215 { CAP_TTYHOOK, "ty" },
216
217 /* Process management via process descriptors. */
218 { CAP_PDGETPID, "pg" },
219 { CAP_PDWAIT, "pw" },
220 { CAP_PDKILL, "pk" },
221
222 /*
223 * Rights that allow to use bindat(2) and connectat(2) syscalls on a
224 * directory descriptor.
225 */
226 { CAP_BINDAT, "ba" },
227 { CAP_CONNECTAT, "ca" },
228
229 /* Aliases and defines that combine multiple rights. */
230 { CAP_PREAD, "prd" },
231 { CAP_PWRITE, "pwr" },
232
233 { CAP_MMAP_R, "mmr" },
234 { CAP_MMAP_W, "mmw" },
235 { CAP_MMAP_X, "mmx" },
236 { CAP_MMAP_RW, "mrw" },
237 { CAP_MMAP_RX, "mrx" },
238 { CAP_MMAP_WX, "mwx" },
239 { CAP_MMAP_RWX, "mma" },
240
241 { CAP_RECV, "re" },
242 { CAP_SEND, "sd" },
243
244 { CAP_SOCK_CLIENT, "scl" },
245 { CAP_SOCK_SERVER, "ssr" },
246 };
247 static const u_int cap_desc_count = nitems(cap_desc);
248
249 static u_int
width_capability(cap_rights_t * rightsp)250 width_capability(cap_rights_t *rightsp)
251 {
252 u_int count, i, width;
253
254 count = 0;
255 width = 0;
256 for (i = 0; i < cap_desc_count; i++) {
257 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
258 width += strlen(cap_desc[i].cd_desc);
259 if (count)
260 width++;
261 count++;
262 }
263 }
264 return (width);
265 }
266
267 static void
print_capability(cap_rights_t * rightsp,u_int capwidth)268 print_capability(cap_rights_t *rightsp, u_int capwidth)
269 {
270 u_int count, i;
271
272 count = 0;
273 for (i = width_capability(rightsp); i < capwidth; i++) {
274 if (i != 0)
275 xo_emit(" ");
276 else
277 xo_emit("-");
278 }
279 xo_open_list("capabilities");
280 for (i = 0; i < cap_desc_count; i++) {
281 if (cap_rights_is_set(rightsp, cap_desc[i].cd_right)) {
282 xo_emit("{D:/%s}{l:capabilities/%s}", count ? "," : "",
283 cap_desc[i].cd_desc);
284 count++;
285 }
286 }
287 xo_close_list("capabilities");
288 }
289
290 void
procstat_files(struct procstat * procstat,struct kinfo_proc * kipp)291 procstat_files(struct procstat *procstat, struct kinfo_proc *kipp)
292 {
293 struct sockstat sock;
294 struct filestat_list *head;
295 struct filestat *fst;
296 const char *str;
297 struct vnstat vn;
298 u_int capwidth, width;
299 int error;
300 char src_addr[PATH_MAX];
301 char dst_addr[PATH_MAX];
302
303 /*
304 * To print the header in capability mode, we need to know the width
305 * of the widest capability string. Even if we get no processes
306 * back, we will print the header, so we defer aborting due to a lack
307 * of processes until after the header logic.
308 */
309 capwidth = 0;
310 head = procstat_getfiles(procstat, kipp, 0);
311 if (head != NULL &&
312 (procstat_opts & PS_OPT_CAPABILITIES) != 0) {
313 STAILQ_FOREACH(fst, head, next) {
314 width = width_capability(&fst->fs_cap_rights);
315 if (width > capwidth)
316 capwidth = width;
317 }
318 if (capwidth < strlen("CAPABILITIES"))
319 capwidth = strlen("CAPABILITIES");
320 }
321
322 if ((procstat_opts & PS_OPT_NOHEADER) == 0) {
323 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0)
324 xo_emit("{T:/%5s %-16s %5s %1s %-8s %-*s "
325 "%-3s %-12s}\n", "PID", "COMM", "FD", "T",
326 "FLAGS", capwidth, "CAPABILITIES", "PRO",
327 "NAME");
328 else
329 xo_emit("{T:/%5s %-16s %5s %1s %1s %-8s "
330 "%3s %7s %-3s %-12s}\n", "PID", "COMM", "FD", "T",
331 "V", "FLAGS", "REF", "OFFSET", "PRO", "NAME");
332 }
333
334 if (head == NULL)
335 return;
336 xo_emit("{ek:process_id/%5d/%d}", kipp->ki_pid);
337 xo_emit("{e:command/%-16s/%s}", kipp->ki_comm);
338 xo_open_list("files");
339 STAILQ_FOREACH(fst, head, next) {
340 xo_open_instance("files");
341 xo_emit("{dk:process_id/%5d/%d} ", kipp->ki_pid);
342 xo_emit("{d:command/%-16s/%s} ", kipp->ki_comm);
343 if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
344 xo_emit("{P: }{:fd/%s} ", "ctty");
345 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
346 xo_emit("{P: }{:fd/%s} ", "cwd");
347 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
348 xo_emit("{P: }{:fd/%s} ", "jail");
349 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
350 xo_emit("{P: }{:fd/%s} ", "root");
351 else if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
352 xo_emit("{P: }{:fd/%s} ", "text");
353 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
354 xo_emit("{:fd/%s} ", "trace");
355 else
356 xo_emit("{:fd/%5d} ", fst->fs_fd);
357
358 switch (fst->fs_type) {
359 case PS_FST_TYPE_VNODE:
360 str = "v";
361 xo_emit("{eq:fd_type/vnode}");
362 break;
363
364 case PS_FST_TYPE_SOCKET:
365 str = "s";
366 xo_emit("{eq:fd_type/socket}");
367 break;
368
369 case PS_FST_TYPE_PIPE:
370 str = "p";
371 xo_emit("{eq:fd_type/pipe}");
372 break;
373
374 case PS_FST_TYPE_FIFO:
375 str = "f";
376 xo_emit("{eq:fd_type/fifo}");
377 break;
378
379 case PS_FST_TYPE_KQUEUE:
380 str = "k";
381 xo_emit("{eq:fd_type/kqueue}");
382 break;
383
384 case PS_FST_TYPE_MQUEUE:
385 str = "m";
386 xo_emit("{eq:fd_type/mqueue}");
387 break;
388
389 case PS_FST_TYPE_SHM:
390 str = "h";
391 xo_emit("{eq:fd_type/shm}");
392 break;
393
394 case PS_FST_TYPE_PTS:
395 str = "t";
396 xo_emit("{eq:fd_type/pts}");
397 break;
398
399 case PS_FST_TYPE_SEM:
400 str = "e";
401 xo_emit("{eq:fd_type/sem}");
402 break;
403
404 case PS_FST_TYPE_PROCDESC:
405 str = "P";
406 xo_emit("{eq:fd_type/procdesc}");
407 break;
408
409 case PS_FST_TYPE_DEV:
410 str = "D";
411 xo_emit("{eq:fd_type/dev}");
412 break;
413
414 case PS_FST_TYPE_EVENTFD:
415 str = "E";
416 xo_emit("{eq:fd_type/eventfd}");
417 break;
418
419 case PS_FST_TYPE_NONE:
420 str = "?";
421 xo_emit("{eq:fd_type/none}");
422 break;
423
424 case PS_FST_TYPE_UNKNOWN:
425 default:
426 str = "?";
427 xo_emit("{eq:fd_type/unknown}");
428 break;
429 }
430 xo_emit("{d:fd_type/%1s/%s} ", str);
431 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) {
432 str = "-";
433 if (fst->fs_type == PS_FST_TYPE_VNODE) {
434 error = procstat_get_vnode_info(procstat, fst,
435 &vn, NULL);
436 switch (vn.vn_type) {
437 case PS_FST_VTYPE_VREG:
438 str = "r";
439 xo_emit("{eq:vode_type/regular}");
440 break;
441
442 case PS_FST_VTYPE_VDIR:
443 str = "d";
444 xo_emit("{eq:vode_type/directory}");
445 break;
446
447 case PS_FST_VTYPE_VBLK:
448 str = "b";
449 xo_emit("{eq:vode_type/block}");
450 break;
451
452 case PS_FST_VTYPE_VCHR:
453 str = "c";
454 xo_emit("{eq:vode_type/character}");
455 break;
456
457 case PS_FST_VTYPE_VLNK:
458 str = "l";
459 xo_emit("{eq:vode_type/link}");
460 break;
461
462 case PS_FST_VTYPE_VSOCK:
463 str = "s";
464 xo_emit("{eq:vode_type/socket}");
465 break;
466
467 case PS_FST_VTYPE_VFIFO:
468 str = "f";
469 xo_emit("{eq:vode_type/fifo}");
470 break;
471
472 case PS_FST_VTYPE_VBAD:
473 str = "x";
474 xo_emit("{eq:vode_type/revoked_device}");
475 break;
476
477 case PS_FST_VTYPE_VNON:
478 str = "?";
479 xo_emit("{eq:vode_type/non}");
480 break;
481
482 case PS_FST_VTYPE_UNKNOWN:
483 default:
484 str = "?";
485 xo_emit("{eq:vode_type/unknown}");
486 break;
487 }
488 }
489 xo_emit("{d:vnode_type/%1s/%s} ", str);
490 }
491
492 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_READ ?
493 "r" : "-");
494 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_WRITE ?
495 "w" : "-");
496 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_APPEND ?
497 "a" : "-");
498 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_ASYNC ?
499 "s" : "-");
500 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_SYNC ?
501 "f" : "-");
502 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_NONBLOCK ?
503 "n" : "-");
504 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_DIRECT ?
505 "d" : "-");
506 xo_emit("{d:/%s}", fst->fs_fflags & PS_FST_FFLAG_HASLOCK ?
507 "l" : "-");
508 xo_emit(" ");
509 xo_open_list("fd_flags");
510 if (fst->fs_fflags & PS_FST_FFLAG_READ)
511 xo_emit("{elq:fd_flags/read}");
512 if (fst->fs_fflags & PS_FST_FFLAG_WRITE)
513 xo_emit("{elq:fd_flags/write}");
514 if (fst->fs_fflags & PS_FST_FFLAG_APPEND)
515 xo_emit("{elq:fd_flags/append}");
516 if (fst->fs_fflags & PS_FST_FFLAG_ASYNC)
517 xo_emit("{elq:fd_flags/async}");
518 if (fst->fs_fflags & PS_FST_FFLAG_SYNC)
519 xo_emit("{elq:fd_flags/fsync}");
520 if (fst->fs_fflags & PS_FST_FFLAG_NONBLOCK)
521 xo_emit("{elq:fd_flags/nonblocking}");
522 if (fst->fs_fflags & PS_FST_FFLAG_DIRECT)
523 xo_emit("{elq:fd_flags/direct_io}");
524 if (fst->fs_fflags & PS_FST_FFLAG_HASLOCK)
525 xo_emit("{elq:fd_flags/lock_held}");
526 xo_close_list("fd_flags");
527
528 if ((procstat_opts & PS_OPT_CAPABILITIES) == 0) {
529 if (fst->fs_ref_count > -1)
530 xo_emit("{:ref_count/%3d/%d} ",
531 fst->fs_ref_count);
532 else
533 xo_emit("{q:ref_count/%3c/%c} ", '-');
534 if (fst->fs_offset > -1)
535 xo_emit("{:offset/%7jd/%jd} ",
536 (intmax_t)fst->fs_offset);
537 else
538 xo_emit("{q:offset/%7c/%c} ", '-');
539 }
540 if ((procstat_opts & PS_OPT_CAPABILITIES) != 0) {
541 print_capability(&fst->fs_cap_rights, capwidth);
542 xo_emit(" ");
543 }
544 switch (fst->fs_type) {
545 case PS_FST_TYPE_SOCKET:
546 error = procstat_get_socket_info(procstat, fst, &sock,
547 NULL);
548 if (error != 0)
549 break;
550 xo_emit("{:protocol/%-3s/%s} ",
551 protocol_to_string(sock.dom_family,
552 sock.type, sock.proto));
553 if (sock.proto == IPPROTO_TCP ||
554 sock.proto == IPPROTO_SCTP ||
555 sock.type == SOCK_STREAM) {
556 xo_emit("{:sendq/%u} ", sock.sendq);
557 xo_emit("{:recvq/%u} ", sock.recvq);
558 }
559 /*
560 * While generally we like to print two addresses,
561 * local and peer, for sockets, it turns out to be
562 * more useful to print the first non-nul address for
563 * local sockets, as typically they aren't bound and
564 * connected, and the path strings can get long.
565 */
566 if (sock.dom_family == AF_LOCAL) {
567 struct sockaddr_un *sun =
568 (struct sockaddr_un *)&sock.sa_local;
569
570 if (sun->sun_path[0] != 0)
571 addr_to_string(&sock.sa_local,
572 src_addr, sizeof(src_addr));
573 else
574 addr_to_string(&sock.sa_peer,
575 src_addr, sizeof(src_addr));
576 xo_emit("{:path/%s}", src_addr);
577 } else {
578 addr_to_string(&sock.sa_local, src_addr,
579 sizeof(src_addr));
580 addr_to_string(&sock.sa_peer, dst_addr,
581 sizeof(dst_addr));
582 xo_emit("{:path/%s %s}", src_addr, dst_addr);
583 }
584 break;
585
586 default:
587 xo_emit("{:protocol/%-3s/%s} ", "-");
588 xo_emit("{:path/%-18s/%s}", fst->fs_path != NULL ?
589 fst->fs_path : "-");
590 }
591
592 xo_emit("\n");
593 xo_close_instance("files");
594 }
595 xo_close_list("files");
596 procstat_freefiles(procstat, head);
597 }
598