git: 35f498434322 - main - sockstat(1): tolerate situation where file info cannot be fetched

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sun, 21 Jul 2024 08:51:56 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=35f4984343229545881a324a00cdbb3980d675ce

commit 35f4984343229545881a324a00cdbb3980d675ce
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-07-20 00:30:55 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-07-21 08:51:42 +0000

    sockstat(1): tolerate situation where file info cannot be fetched
    
    Either due to a race, or to the privilege restrictions, it is not
    guaranteed that kern.files returned file information for all pcbs
    read from net.inet.<proto>.pcblist.  In this case the file rbtree does
    not return the matching file by data address, and code must avoid
    dereferencing NULL.
    
    PR:     279875
    Reviewed by:    asomers
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D46050
---
 usr.bin/sockstat/sockstat.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/usr.bin/sockstat/sockstat.c b/usr.bin/sockstat/sockstat.c
index 73b1f00a4481..5eac327ca184 100644
--- a/usr.bin/sockstat/sockstat.c
+++ b/usr.bin/sockstat/sockstat.c
@@ -1164,8 +1164,11 @@ displaysock(struct sock *s, int pos)
 					f = RB_FIND(files_t, &ftree,
 					    &(struct file){ .xf_data =
 					    p->socket });
-					pos += xprintf("[%lu %d]",
-					    (u_long)f->xf_pid, f->xf_fd);
+					if (f != NULL) {
+						pos += xprintf("[%lu %d]",
+						    (u_long)f->xf_pid,
+						    f->xf_fd);
+					}
 				} else
 					pos += printaddr(&p->laddr->address);
 			}
@@ -1183,9 +1186,12 @@ displaysock(struct sock *s, int pos)
 					f = RB_FIND(files_t, &ftree,
 					    &(struct file){ .xf_data =
 					    p->socket });
-					pos += xprintf("%s[%lu %d]",
-					    fref ? "" : ",",
-					    (u_long)f->xf_pid, f->xf_fd);
+					if (f != NULL) {
+						pos += xprintf("%s[%lu %d]",
+						    fref ? "" : ",",
+						    (u_long)f->xf_pid,
+						    f->xf_fd);
+					}
 					ref = p->faddr->nextref;
 					fref = false;
 				}