| 1 |
/* |
| 2 |
* netcap-advanced.c - Advanced capability analysis |
| 3 |
* Copyright (c) 2026 Steve Grubb |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* This software may be freely redistributed and/or modified under the |
| 7 |
* terms of the GNU General Public License as published by the Free |
| 8 |
* Software Foundation; either version 2, or (at your option) any |
| 9 |
* later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; see the file COPYING. If not, write to the |
| 18 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor |
| 19 |
* Boston, MA 02110-1335, USA. |
| 20 |
* |
| 21 |
* Authors: |
| 22 |
* Steve Grubb <[email protected]> |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
#include <arpa/inet.h> |
| 27 |
#include <dirent.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <ifaddrs.h> |
| 31 |
#include <linux/capability.h> |
| 32 |
#include <linux/inet_diag.h> |
| 33 |
#include <linux/netlink.h> |
| 34 |
#include <linux/sock_diag.h> |
| 35 |
#include <linux/vm_sockets.h> |
| 36 |
#include <limits.h> |
| 37 |
#ifdef HAVE_LINUX_VM_SOCKETS_DIAG_H |
| 38 |
#include <linux/vm_sockets_diag.h> |
| 39 |
#endif |
| 40 |
#include <net/if.h> |
| 41 |
#include <netinet/in.h> |
| 42 |
#include <netinet/tcp.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include <stdio_ext.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <stdint.h> |
| 47 |
#include <string.h> |
| 48 |
#include <sys/ioctl.h> |
| 49 |
#include <sys/socket.h> |
| 50 |
#include <sys/stat.h> |
| 51 |
#include <sys/syscall.h> |
| 52 |
#include <sys/types.h> |
| 53 |
#include <unistd.h> |
| 54 |
#include "cap-ng.h" |
| 55 |
#include "netcap-advanced.h" |
| 56 |
#include "proc-sanitize.h" |
| 57 |
|
| 58 |
/* |
| 59 |
* Overview: |
| 60 |
* netcap --advanced builds a process/socket ownership model from procfs and |
| 61 |
* sock_diag, then renders it as a tree or JSON without changing system state. |
| 62 |
* |
| 63 |
* The flow is: discover interface addresses, map socket inode->process |
| 64 |
* ownership from /proc/<pid>/fd, parse protocol-specific listener tables, |
| 65 |
* and project each endpoint onto interface/plane groupings for reporting. |
| 66 |
* |
| 67 |
* For internet sockets, wildcard binds are expanded onto concrete interface |
| 68 |
* addresses so the rendered tree/JSON can be consumed as an exposure map. |
| 69 |
* VSOCK listeners are collected via sock_diag when available and fall back to |
| 70 |
* /proc parsing when not, with ownership stitched back through socket inodes. |
| 71 |
* Tree output supports colorized capability/flag severity, with --no-color |
| 72 |
* forcing plain text. SO_REUSEPORT is detected per socket via pidfd_open and |
| 73 |
* pidfd_getfd while scanning /proc/<pid>/fd so the flag can be propagated into |
| 74 |
* endpoint rendering. |
| 75 |
* |
| 76 |
* Process metadata includes ambient capability enumeration with per-capability |
| 77 |
* detail and cgroup-unit extraction limited to system.slice services to keep |
| 78 |
* ownership context focused on service units. Line wrapping for tree output is |
| 79 |
* ANSI-escape-aware so colorized text wraps at display width without breaking |
| 80 |
* SGR sequences. |
| 81 |
* |
| 82 |
* Results depend on the current network namespace and procfs visibility; |
| 83 |
* restricted privileges can hide processes/sockets and yield partial output. |
| 84 |
*/ |
| 85 |
|
| 86 |
#ifdef NETCAP_DIAG_DEBUG |
| 87 |
#define diag_dbg(fmt, ...) \ |
| 88 |
fprintf(stderr, "netcap-diag: " fmt "\n", ##__VA_ARGS__) |
| 89 |
#else |
| 90 |
#define diag_dbg(fmt, ...) do { } while (0) |
| 91 |
#endif |
| 92 |
|
| 93 |
enum plane_kind { |
| 94 |
PLANE_INET_EXTERNAL, |
| 95 |
PLANE_INET_LOOPBACK, |
| 96 |
PLANE_PACKET, |
| 97 |
PLANE_VSOCK, |
| 98 |
PLANE_COUNT, |
| 99 |
}; |
| 100 |
|
| 101 |
#define PLANE_PACKET_NAME "LINK-LAYER" |
| 102 |
/* Keep user-facing key name centralized to avoid legacy regressions. */ |
| 103 |
#define DEFENSES_RUNS_AS_KEY "runs_as_nonroot" |
| 104 |
|
| 105 |
enum endpoint_flags { |
| 106 |
FLAG_WILDCARD_BIND = 1U << 0, |
| 107 |
FLAG_PRIVILEGED_CAPS = 1U << 2, |
| 108 |
FLAG_HYPERVISOR_PLANE = 1U << 4, |
| 109 |
FLAG_SSH_VSOCK_22 = 1U << 5, |
| 110 |
FLAG_REUSEPORT = 1U << 6, |
| 111 |
}; |
| 112 |
|
| 113 |
struct strset { |
| 114 |
const char **slots; |
| 115 |
size_t slots_cap; |
| 116 |
size_t used; |
| 117 |
}; |
| 118 |
|
| 119 |
struct iface_addr { |
| 120 |
int af; |
| 121 |
char *addr; |
| 122 |
}; |
| 123 |
|
| 124 |
struct iface_info { |
| 125 |
char *name; |
| 126 |
struct iface_addr *addrs; |
| 127 |
size_t addrs_n; |
| 128 |
size_t addrs_cap; |
| 129 |
}; |
| 130 |
|
| 131 |
struct defense_info { |
| 132 |
char *runs_as_nonroot; |
| 133 |
char *no_new_privs; |
| 134 |
char *seccomp; |
| 135 |
char *lsm_label; |
| 136 |
}; |
| 137 |
|
| 138 |
struct process_info { |
| 139 |
int pid; |
| 140 |
int uid; |
| 141 |
char *comm; |
| 142 |
char *exe; |
| 143 |
char *unit; |
| 144 |
char *caps; |
| 145 |
char *ambient_caps; |
| 146 |
int ambient_present; |
| 147 |
int open_ended_bounding; |
| 148 |
int has_privileged_caps; |
| 149 |
struct defense_info defenses; |
| 150 |
}; |
| 151 |
|
| 152 |
struct inode_proc { |
| 153 |
unsigned long inode; |
| 154 |
struct process_info **procs; |
| 155 |
size_t n; |
| 156 |
size_t cap; |
| 157 |
int reuseport; |
| 158 |
}; |
| 159 |
|
| 160 |
struct endpoint { |
| 161 |
char *proto; |
| 162 |
char *bind; |
| 163 |
char *label; |
| 164 |
unsigned int port; |
| 165 |
unsigned int vsock_cid; |
| 166 |
int has_vsock; |
| 167 |
enum plane_kind plane; |
| 168 |
char *ifname; |
| 169 |
char *ifaddr; |
| 170 |
struct process_info **procs; |
| 171 |
size_t procs_n; |
| 172 |
size_t procs_cap; |
| 173 |
int wildcard_bind; |
| 174 |
int reuseport; |
| 175 |
}; |
| 176 |
|
| 177 |
struct model { |
| 178 |
struct iface_info *ifaces; |
| 179 |
size_t ifaces_n; |
| 180 |
size_t ifaces_cap; |
| 181 |
struct process_info **procs; |
| 182 |
size_t procs_n; |
| 183 |
size_t procs_cap; |
| 184 |
struct inode_proc *inode_map; |
| 185 |
size_t inode_n; |
| 186 |
size_t inode_cap; |
| 187 |
size_t *inode_slots; |
| 188 |
size_t inode_slots_cap; |
| 189 |
struct endpoint *eps; |
| 190 |
size_t eps_n; |
| 191 |
size_t eps_cap; |
| 192 |
}; |
| 193 |
|
| 194 |
#define INODE_SLOT_EMPTY SIZE_MAX |
| 195 |
#define PIDSET_EMPTY INT_MIN |
| 196 |
|
| 197 |
struct pidset { |
| 198 |
int *slots; |
| 199 |
size_t cap; |
| 200 |
size_t used; |
| 201 |
}; |
| 202 |
|
| 203 |
struct status_fields { |
| 204 |
unsigned long no_new_privs; |
| 205 |
unsigned long seccomp; |
| 206 |
int seen_no_new_privs; |
| 207 |
int seen_seccomp; |
| 208 |
}; |
| 209 |
|
| 210 |
struct endpoint_attrs { |
| 211 |
int wildcard; |
| 212 |
int reuseport; |
| 213 |
}; |
| 214 |
|
| 215 |
static void free_process(struct process_info *p); |
| 216 |
static void free_endpoint(struct endpoint *e); |
| 217 |
|
| 218 |
static int use_color; |
| 219 |
|
| 220 |
#define COLOR_ORANGE "\033[38;5;208m" |
| 221 |
#define COLOR_YELLOW "\033[38;5;226m" |
| 222 |
#define COLOR_GREEN "\033[38;5;82m" |
| 223 |
#define COLOR_RESET "\033[0m" |
| 224 |
|
| 225 |
enum cap_severity { |
| 226 |
CAP_SEV_NEUTRAL, |
| 227 |
CAP_SEV_YELLOW, |
| 228 |
CAP_SEV_ORANGE, |
| 229 |
}; |
| 230 |
|
| 231 |
static const char *orange_caps[] = { |
| 232 |
"sys_ptrace", "sys_module", "sys_rawio", "setuid", "setgid", |
| 233 |
"setpcap", "audit_control", |
| 234 |
}; |
| 235 |
|
| 236 |
static const char *yellow_caps[] = { |
| 237 |
"sys_admin", "dac_override", "dac_read_search", "net_admin", |
| 238 |
"net_raw", "chown", "fowner", "mknod", "sys_chroot", |
| 239 |
}; |
| 240 |
|
| 241 |
/* |
| 242 |
* cap_name_severity - classify one capability name into severity tiers. |
| 243 |
* @name: capability token without "cap_" prefix. |
| 244 |
* |
| 245 |
* Returns severity bucket used for tree color selection. |
| 246 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 247 |
* procfs/netns state; it does not change kernel configuration. |
| 248 |
*/ |
| 249 |
static enum cap_severity cap_name_severity(const char *name) |
| 250 |
{ |
| 251 |
size_t i; |
| 252 |
|
| 253 |
for (i = 0; i < sizeof(orange_caps) / sizeof(orange_caps[0]); i++) { |
| 254 |
if (strcmp(name, orange_caps[i]) == 0) |
| 255 |
return CAP_SEV_ORANGE; |
| 256 |
} |
| 257 |
for (i = 0; i < sizeof(yellow_caps) / sizeof(yellow_caps[0]); i++) { |
| 258 |
if (strcmp(name, yellow_caps[i]) == 0) |
| 259 |
return CAP_SEV_YELLOW; |
| 260 |
} |
| 261 |
return CAP_SEV_NEUTRAL; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* caps_contains_token - test whether @token appears as a capability list item. |
| 266 |
* @caps: comma/space separated capability summary text. |
| 267 |
* @token: capability token to locate. |
| 268 |
* |
| 269 |
* Returns non-zero when @token is present as a whole list element, else 0. |
| 270 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 271 |
* procfs/netns state; it does not change kernel configuration. |
| 272 |
*/ |
| 273 |
static int caps_contains_token(const char *caps, const char *token) |
| 274 |
{ |
| 275 |
size_t len; |
| 276 |
const char *p; |
| 277 |
|
| 278 |
if (!caps || !token) |
| 279 |
return 0; |
| 280 |
len = strlen(token); |
| 281 |
for (p = caps; (p = strstr(p, token)) != NULL; p++) { |
| 282 |
int left_ok = (p == caps) || |
| 283 |
(p > caps + 1 && p[-1] == ' ' && p[-2] == ','); |
| 284 |
char right = p[len]; |
| 285 |
int right_ok = right == 0 || right == ',' || right == ' ' || right == '['; |
| 286 |
|
| 287 |
if (left_ok && right_ok) |
| 288 |
return 1; |
| 289 |
} |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* sev_color - map severity level to ANSI color sequence. |
| 295 |
* @sev: severity class to map. |
| 296 |
* |
| 297 |
* Returns static color code pointer, or NULL when uncolored. |
| 298 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 299 |
* procfs/netns state; it does not change kernel configuration. |
| 300 |
*/ |
| 301 |
static const char *sev_color(enum cap_severity sev) |
| 302 |
{ |
| 303 |
if (sev == CAP_SEV_ORANGE) |
| 304 |
return COLOR_ORANGE; |
| 305 |
if (sev == CAP_SEV_YELLOW) |
| 306 |
return COLOR_YELLOW; |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
* caps_worst_severity - find highest severity capability in @caps text. |
| 312 |
* @caps: capability list text from caps_summary_for_pid(). |
| 313 |
* |
| 314 |
* Returns highest matched severity, or CAP_SEV_NEUTRAL. |
| 315 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 316 |
* procfs/netns state; it does not change kernel configuration. |
| 317 |
*/ |
| 318 |
static enum cap_severity caps_worst_severity(const char *caps) |
| 319 |
{ |
| 320 |
size_t i; |
| 321 |
|
| 322 |
if (!caps) |
| 323 |
return CAP_SEV_NEUTRAL; |
| 324 |
|
| 325 |
if (strcmp(caps, "(full)") == 0) |
| 326 |
return CAP_SEV_ORANGE; |
| 327 |
|
| 328 |
for (i = 0; i < sizeof(orange_caps) / sizeof(orange_caps[0]); i++) { |
| 329 |
if (caps_contains_token(caps, orange_caps[i])) |
| 330 |
return CAP_SEV_ORANGE; |
| 331 |
} |
| 332 |
for (i = 0; i < sizeof(yellow_caps) / sizeof(yellow_caps[0]); i++) { |
| 333 |
if (caps_contains_token(caps, yellow_caps[i])) |
| 334 |
return CAP_SEV_YELLOW; |
| 335 |
} |
| 336 |
return CAP_SEV_NEUTRAL; |
| 337 |
} |
| 338 |
|
| 339 |
static void free_model(struct model *m); |
| 340 |
static void json_escape(const char *s); |
| 341 |
static void print_tree_node(const char *prefix, int is_last, |
| 342 |
const char *txt, int width); |
| 343 |
static int bind_sort_cmp(const char *a, const char *b); |
| 344 |
static struct inode_proc *lookup_inode(struct model *m, unsigned long inode); |
| 345 |
static void render_interfaces_text(struct model *m); |
| 346 |
static void render_interfaces_json(struct model *m); |
| 347 |
|
| 348 |
/* |
| 349 |
* iface_addr_cmp - order interface addresses by family then text. |
| 350 |
* @av/@bv: qsort pointers to struct iface_addr elements. |
| 351 |
* |
| 352 |
* Returns strcmp-style ordering suitable for qsort(). |
| 353 |
*/ |
| 354 |
static int iface_addr_cmp(const void *av, const void *bv) |
| 355 |
{ |
| 356 |
const struct iface_addr *a = av; |
| 357 |
const struct iface_addr *b = bv; |
| 358 |
|
| 359 |
if (a->af != b->af) |
| 360 |
return a->af - b->af; |
| 361 |
return strcmp(a->addr, b->addr); |
| 362 |
} |
| 363 |
|
| 364 |
/* |
| 365 |
* iface_info_cmp - order interfaces by name for stable output. |
| 366 |
* @av/@bv: qsort pointers to struct iface_info elements. |
| 367 |
* |
| 368 |
* Returns strcmp-style ordering suitable for qsort(). |
| 369 |
*/ |
| 370 |
static int iface_info_cmp(const void *av, const void *bv) |
| 371 |
{ |
| 372 |
const struct iface_info *a = av; |
| 373 |
const struct iface_info *b = bv; |
| 374 |
|
| 375 |
return strcmp(a->name, b->name); |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* sort_interfaces - sort interfaces and per-interface addresses in place. |
| 380 |
* @m: model containing interface inventory from getifaddrs(). |
| 381 |
* |
| 382 |
* Returns no value. |
| 383 |
*/ |
| 384 |
static void sort_interfaces(struct model *m) |
| 385 |
{ |
| 386 |
size_t i; |
| 387 |
|
| 388 |
if (m->ifaces_n > 1) |
| 389 |
qsort(m->ifaces, m->ifaces_n, sizeof(struct iface_info), |
| 390 |
iface_info_cmp); |
| 391 |
for (i = 0; i < m->ifaces_n; i++) { |
| 392 |
if (m->ifaces[i].addrs_n > 1) |
| 393 |
qsort(m->ifaces[i].addrs, m->ifaces[i].addrs_n, |
| 394 |
sizeof(struct iface_addr), iface_addr_cmp); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* str_hash - hash a string key for open-addressed set placement. |
| 400 |
* @s: NUL-terminated key string. |
| 401 |
* @slots_cap: destination hash table capacity. |
| 402 |
* |
| 403 |
* Returns index in [0, @slots_cap). |
| 404 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 405 |
* procfs/netns state; it does not change kernel configuration. |
| 406 |
*/ |
| 407 |
static size_t str_hash(const char *s, size_t slots_cap) |
| 408 |
{ |
| 409 |
uint64_t x = 1469598103934665603ULL; |
| 410 |
|
| 411 |
for (; *s; s++) { |
| 412 |
x ^= (unsigned char)*s; |
| 413 |
x *= 1099511628211ULL; |
| 414 |
} |
| 415 |
return (size_t)(x % slots_cap); |
| 416 |
} |
| 417 |
|
| 418 |
/* |
| 419 |
* strset_rebuild - resize and rehash string set storage. |
| 420 |
* @set: set object whose slots array is replaced. |
| 421 |
* @new_cap: requested slot capacity before minimum clamping. |
| 422 |
* |
| 423 |
* Returns 0 on success, -1 on allocation failure. |
| 424 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 425 |
* procfs/netns state; it does not change kernel configuration. |
| 426 |
*/ |
| 427 |
static int strset_rebuild(struct strset *set, size_t new_cap) |
| 428 |
{ |
| 429 |
const char **slots; |
| 430 |
size_t i; |
| 431 |
|
| 432 |
if (new_cap < 16) |
| 433 |
new_cap = 16; |
| 434 |
slots = calloc(new_cap, sizeof(*slots)); |
| 435 |
if (!slots) { |
| 436 |
fprintf(stderr, "Out of memory\n"); |
| 437 |
return -1; |
| 438 |
} |
| 439 |
for (i = 0; i < set->slots_cap; i++) { |
| 440 |
size_t pos; |
| 441 |
|
| 442 |
if (!set->slots[i]) |
| 443 |
continue; |
| 444 |
pos = str_hash(set->slots[i], new_cap); |
| 445 |
while (slots[pos]) |
| 446 |
pos = (pos + 1) % new_cap; |
| 447 |
slots[pos] = set->slots[i]; |
| 448 |
} |
| 449 |
free(set->slots); |
| 450 |
set->slots = slots; |
| 451 |
set->slots_cap = new_cap; |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
/* |
| 456 |
* strset_add - insert @s into the string set if absent. |
| 457 |
* @set: destination hash set. |
| 458 |
* @s: caller-owned string pointer stored by reference. |
| 459 |
* |
| 460 |
* Returns 1 when inserted, 0 when already present, -1 on failure. |
| 461 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 462 |
* procfs/netns state; it does not change kernel configuration. |
| 463 |
*/ |
| 464 |
static int strset_add(struct strset *set, const char *s) |
| 465 |
{ |
| 466 |
size_t pos; |
| 467 |
|
| 468 |
if (set->slots_cap == 0) { |
| 469 |
if (strset_rebuild(set, 16) != 0) |
| 470 |
return -1; |
| 471 |
} |
| 472 |
if ((set->used + 1) * 4 >= set->slots_cap * 3) { |
| 473 |
size_t new_cap = set->slots_cap * 2; |
| 474 |
|
| 475 |
if (new_cap < set->slots_cap) |
| 476 |
return -1; |
| 477 |
if (strset_rebuild(set, new_cap) != 0) |
| 478 |
return -1; |
| 479 |
} |
| 480 |
|
| 481 |
pos = str_hash(s, set->slots_cap); |
| 482 |
while (set->slots[pos]) { |
| 483 |
if (strcmp(set->slots[pos], s) == 0) |
| 484 |
return 0; |
| 485 |
pos = (pos + 1) % set->slots_cap; |
| 486 |
} |
| 487 |
set->slots[pos] = s; |
| 488 |
set->used++; |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
* strset_free - release dynamic storage owned by @set. |
| 494 |
* @set: hash set to reset. |
| 495 |
* |
| 496 |
* Returns no value. |
| 497 |
* Side effects/assumptions: Frees heap memory referenced by @set. |
| 498 |
*/ |
| 499 |
static void strset_free(struct strset *set) |
| 500 |
{ |
| 501 |
free(set->slots); |
| 502 |
set->slots = NULL; |
| 503 |
set->slots_cap = 0; |
| 504 |
set->used = 0; |
| 505 |
} |
| 506 |
|
| 507 |
/* |
| 508 |
* inode_hash - hash inode key for inode ownership map slot selection. |
| 509 |
* @inode: socket inode value. |
| 510 |
* @slots_cap: destination hash table capacity. |
| 511 |
* |
| 512 |
* Returns index in [0, @slots_cap). |
| 513 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 514 |
* procfs/netns state; it does not change kernel configuration. |
| 515 |
*/ |
| 516 |
static size_t inode_hash(unsigned long inode, size_t slots_cap) |
| 517 |
{ |
| 518 |
uint64_t x = inode; |
| 519 |
|
| 520 |
x ^= x >> 33; |
| 521 |
x *= 0xff51afd7ed558ccdULL; |
| 522 |
x ^= x >> 33; |
| 523 |
x *= 0xc4ceb9fe1a85ec53ULL; |
| 524 |
x ^= x >> 33; |
| 525 |
return (size_t)(x % slots_cap); |
| 526 |
} |
| 527 |
|
| 528 |
/* |
| 529 |
* inode_hash_rebuild - resize and repopulate inode hash slots. |
| 530 |
* @m: model containing inode_map entries and slot metadata. |
| 531 |
* @new_cap: requested new slot capacity. |
| 532 |
* |
| 533 |
* Returns 0 on success, -1 on allocation failure. |
| 534 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 535 |
* procfs/netns state; it does not change kernel configuration. |
| 536 |
*/ |
| 537 |
static int inode_hash_rebuild(struct model *m, size_t new_cap) |
| 538 |
{ |
| 539 |
size_t i; |
| 540 |
size_t *slots; |
| 541 |
|
| 542 |
if (new_cap < 16) |
| 543 |
new_cap = 16; |
| 544 |
slots = malloc(new_cap * sizeof(*slots)); |
| 545 |
if (!slots) { |
| 546 |
fprintf(stderr, "Out of memory\n"); |
| 547 |
return -1; |
| 548 |
} |
| 549 |
for (i = 0; i < new_cap; i++) |
| 550 |
slots[i] = INODE_SLOT_EMPTY; |
| 551 |
|
| 552 |
for (i = 0; i < m->inode_n; i++) { |
| 553 |
size_t pos = inode_hash(m->inode_map[i].inode, new_cap); |
| 554 |
|
| 555 |
while (slots[pos] != INODE_SLOT_EMPTY) |
| 556 |
pos = (pos + 1) % new_cap; |
| 557 |
slots[pos] = i; |
| 558 |
} |
| 559 |
|
| 560 |
free(m->inode_slots); |
| 561 |
m->inode_slots = slots; |
| 562 |
m->inode_slots_cap = new_cap; |
| 563 |
return 0; |
| 564 |
} |
| 565 |
|
| 566 |
/* |
| 567 |
* inode_hash_ensure_capacity - grow inode hash table when load is high. |
| 568 |
* @m: model whose inode slot table may be resized. |
| 569 |
* |
| 570 |
* Returns 0 when capacity is sufficient or grown, -1 on failure. |
| 571 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 572 |
* procfs/netns state; it does not change kernel configuration. |
| 573 |
*/ |
| 574 |
static int inode_hash_ensure_capacity(struct model *m) |
| 575 |
{ |
| 576 |
size_t new_cap; |
| 577 |
|
| 578 |
if (m->inode_slots_cap == 0) |
| 579 |
return inode_hash_rebuild(m, 16); |
| 580 |
if ((m->inode_n + 1) * 4 < m->inode_slots_cap * 3) |
| 581 |
return 0; |
| 582 |
|
| 583 |
new_cap = m->inode_slots_cap * 2; |
| 584 |
if (new_cap < m->inode_slots_cap) |
| 585 |
return -1; |
| 586 |
return inode_hash_rebuild(m, new_cap); |
| 587 |
} |
| 588 |
|
| 589 |
/* |
| 590 |
* inode_hash_find - find inode_map index for @inode. |
| 591 |
* @m: model containing inode hash slots. |
| 592 |
* @inode: inode key to search. |
| 593 |
* |
| 594 |
* Returns non-negative inode_map index on hit, -1 on miss. |
| 595 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 596 |
* procfs/netns state; it does not change kernel configuration. |
| 597 |
*/ |
| 598 |
static ssize_t inode_hash_find(struct model *m, unsigned long inode) |
| 599 |
{ |
| 600 |
size_t pos; |
| 601 |
size_t start; |
| 602 |
|
| 603 |
if (m->inode_slots_cap == 0) |
| 604 |
return -1; |
| 605 |
|
| 606 |
pos = inode_hash(inode, m->inode_slots_cap); |
| 607 |
start = pos; |
| 608 |
while (m->inode_slots[pos] != INODE_SLOT_EMPTY) { |
| 609 |
size_t idx = m->inode_slots[pos]; |
| 610 |
|
| 611 |
if (m->inode_map[idx].inode == inode) |
| 612 |
return idx; |
| 613 |
pos = (pos + 1) % m->inode_slots_cap; |
| 614 |
if (pos == start) |
| 615 |
break; |
| 616 |
} |
| 617 |
return -1; |
| 618 |
} |
| 619 |
|
| 620 |
/* |
| 621 |
* inode_hash_insert - place one inode_map index into hash slots. |
| 622 |
* @m: model containing destination hash slots. |
| 623 |
* @idx: inode_map entry index to insert. |
| 624 |
* |
| 625 |
* Returns no value. |
| 626 |
* Side effects/assumptions: Mutates @m->inode_slots insertion state. |
| 627 |
*/ |
| 628 |
static void inode_hash_insert(struct model *m, size_t idx) |
| 629 |
{ |
| 630 |
size_t pos = inode_hash(m->inode_map[idx].inode, m->inode_slots_cap); |
| 631 |
|
| 632 |
while (m->inode_slots[pos] != INODE_SLOT_EMPTY) |
| 633 |
pos = (pos + 1) % m->inode_slots_cap; |
| 634 |
m->inode_slots[pos] = idx; |
| 635 |
} |
| 636 |
|
| 637 |
/* |
| 638 |
* get_width - choose terminal width for wrapped tree output. |
| 639 |
* @none: function takes no parameters. |
| 640 |
* |
| 641 |
* Returns terminal columns from TIOCGWINSZ or $COLUMNS, else 80. |
| 642 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 643 |
* procfs/netns state; it does not change kernel configuration. |
| 644 |
*/ |
| 645 |
static int get_width(void) |
| 646 |
{ |
| 647 |
struct winsize ws; |
| 648 |
const int fds[] = { STDOUT_FILENO, STDERR_FILENO, STDIN_FILENO }; |
| 649 |
const char *env; |
| 650 |
char *end = NULL; |
| 651 |
unsigned long v; |
| 652 |
size_t i; |
| 653 |
|
| 654 |
for (i = 0; i < sizeof(fds) / sizeof(fds[0]); i++) { |
| 655 |
if (ioctl(fds[i], TIOCGWINSZ, &ws) == 0 && ws.ws_col) |
| 656 |
return ws.ws_col; |
| 657 |
} |
| 658 |
env = getenv("COLUMNS"); |
| 659 |
if (env) { |
| 660 |
v = strtoul(env, &end, 10); |
| 661 |
if (end != env && *end == '\0' && v > 0 && v < 400) |
| 662 |
return (int)v; |
| 663 |
} |
| 664 |
return 80; |
| 665 |
} |
| 666 |
|
| 667 |
/* |
| 668 |
* xstrdup - duplicate @s into a newly allocated string. |
| 669 |
* @s: source string, or NULL. |
| 670 |
* |
| 671 |
* Returns caller-owned copy, or NULL for NULL input or allocation failure. |
| 672 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 673 |
* procfs/netns state; it does not change kernel configuration. |
| 674 |
*/ |
| 675 |
static char *xstrdup(const char *s) |
| 676 |
{ |
| 677 |
char *p; |
| 678 |
|
| 679 |
if (!s) |
| 680 |
return NULL; |
| 681 |
p = strdup(s); |
| 682 |
if (!p) |
| 683 |
fprintf(stderr, "Out of memory\n"); |
| 684 |
return p; |
| 685 |
} |
| 686 |
|
| 687 |
/* |
| 688 |
* vec_grow - grow vector storage for dynamic arrays. |
| 689 |
* @v: pointer to heap buffer pointer updated on success. |
| 690 |
* @cap: current/new capacity element count. |
| 691 |
* @item: size in bytes of each element. |
| 692 |
* |
| 693 |
* Returns 0 on success, -1 on allocation failure. |
| 694 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 695 |
* procfs/netns state; it does not change kernel configuration. |
| 696 |
*/ |
| 697 |
static int vec_grow(void **v, size_t *cap, size_t item) |
| 698 |
{ |
| 699 |
void *p; |
| 700 |
size_t ncap; |
| 701 |
|
| 702 |
if (*cap) |
| 703 |
ncap = *cap * 2; |
| 704 |
else |
| 705 |
ncap = 8; |
| 706 |
p = realloc(*v, ncap * item); |
| 707 |
if (!p) { |
| 708 |
fprintf(stderr, "Out of memory\n"); |
| 709 |
return -1; |
| 710 |
} |
| 711 |
*v = p; |
| 712 |
*cap = ncap; |
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
/* |
| 717 |
* pid_hash - hash process ID for pidset probing. |
| 718 |
* @pid: process id key. |
| 719 |
* @cap: pidset slot capacity. |
| 720 |
* |
| 721 |
* Returns slot index in [0, @cap). |
| 722 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 723 |
* procfs/netns state; it does not change kernel configuration. |
| 724 |
*/ |
| 725 |
static size_t pid_hash(int pid, size_t cap) |
| 726 |
{ |
| 727 |
uint32_t v = (uint32_t)pid; |
| 728 |
|
| 729 |
v ^= v >> 16; |
| 730 |
v *= 0x7feb352dU; |
| 731 |
v ^= v >> 15; |
| 732 |
v *= 0x846ca68bU; |
| 733 |
v ^= v >> 16; |
| 734 |
return v % cap; |
| 735 |
} |
| 736 |
|
| 737 |
/* |
| 738 |
* pidset_rehash - resize/reinsert pidset contents. |
| 739 |
* @ps: pidset to grow. |
| 740 |
* @new_cap: requested slot capacity. |
| 741 |
* |
| 742 |
* Returns 0 on success, -1 on allocation failure. |
| 743 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 744 |
* procfs/netns state; it does not change kernel configuration. |
| 745 |
*/ |
| 746 |
static int pidset_rehash(struct pidset *ps, size_t new_cap) |
| 747 |
{ |
| 748 |
int *new_slots; |
| 749 |
size_t i; |
| 750 |
|
| 751 |
new_slots = malloc(new_cap * sizeof(*new_slots)); |
| 752 |
if (!new_slots) |
| 753 |
return -1; |
| 754 |
for (i = 0; i < new_cap; i++) |
| 755 |
new_slots[i] = PIDSET_EMPTY; |
| 756 |
for (i = 0; i < ps->cap; i++) { |
| 757 |
size_t pos; |
| 758 |
|
| 759 |
if (ps->slots[i] == PIDSET_EMPTY) |
| 760 |
continue; |
| 761 |
pos = pid_hash(ps->slots[i], new_cap); |
| 762 |
while (new_slots[pos] != PIDSET_EMPTY) |
| 763 |
pos = (pos + 1) % new_cap; |
| 764 |
new_slots[pos] = ps->slots[i]; |
| 765 |
} |
| 766 |
free(ps->slots); |
| 767 |
ps->slots = new_slots; |
| 768 |
ps->cap = new_cap; |
| 769 |
return 0; |
| 770 |
} |
| 771 |
|
| 772 |
/* |
| 773 |
* pidset_init - initialize pidset with empty hash table state. |
| 774 |
* @ps: pidset object to initialize. |
| 775 |
* |
| 776 |
* Returns 0 on success, -1 on allocation failure. |
| 777 |
* Side effects/assumptions: Allocates heap storage for @ps slots. |
| 778 |
*/ |
| 779 |
static int pidset_init(struct pidset *ps) |
| 780 |
{ |
| 781 |
ps->cap = 16; |
| 782 |
ps->used = 0; |
| 783 |
ps->slots = malloc(ps->cap * sizeof(*ps->slots)); |
| 784 |
if (!ps->slots) |
| 785 |
return -1; |
| 786 |
for (size_t i = 0; i < ps->cap; i++) |
| 787 |
ps->slots[i] = PIDSET_EMPTY; |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
/* |
| 792 |
* pidset_free - release pidset storage and reset fields. |
| 793 |
* @ps: pidset object to clear. |
| 794 |
* |
| 795 |
* Returns no value. |
| 796 |
* Side effects/assumptions: Frees heap memory referenced by @ps. |
| 797 |
*/ |
| 798 |
static void pidset_free(struct pidset *ps) |
| 799 |
{ |
| 800 |
free(ps->slots); |
| 801 |
ps->slots = NULL; |
| 802 |
ps->cap = 0; |
| 803 |
ps->used = 0; |
| 804 |
} |
| 805 |
|
| 806 |
/* |
| 807 |
* pidset_test_and_add - query/insert PID in dedup set. |
| 808 |
* @ps: pidset tracking seen process IDs. |
| 809 |
* @pid: process id to test and insert. |
| 810 |
* |
| 811 |
* Returns 1 if already present, 0 if newly inserted, -1 on failure. |
| 812 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 813 |
* procfs/netns state; it does not change kernel configuration. |
| 814 |
*/ |
| 815 |
static int pidset_test_and_add(struct pidset *ps, int pid) |
| 816 |
{ |
| 817 |
size_t pos; |
| 818 |
|
| 819 |
if ((ps->used + 1) * 10 >= ps->cap * 7) { |
| 820 |
if (pidset_rehash(ps, ps->cap * 2)) |
| 821 |
return -1; |
| 822 |
} |
| 823 |
pos = pid_hash(pid, ps->cap); |
| 824 |
while (ps->slots[pos] != PIDSET_EMPTY) { |
| 825 |
if (ps->slots[pos] == pid) |
| 826 |
return 1; |
| 827 |
pos = (pos + 1) % ps->cap; |
| 828 |
} |
| 829 |
ps->slots[pos] = pid; |
| 830 |
ps->used++; |
| 831 |
return 0; |
| 832 |
} |
| 833 |
|
| 834 |
/* |
| 835 |
* print_flag_nodes - render endpoint/process flag leaves under "flags" node. |
| 836 |
* @pfx_flags: tree prefix for flag child lines. |
| 837 |
* @width: wrap width for tree nodes. |
| 838 |
* @flags: bitmask of endpoint/process flags to print. |
| 839 |
* @priv_sev: privileged capability severity for colorizing that flag. |
| 840 |
* |
| 841 |
* Returns no value. |
| 842 |
* Side effects/assumptions: Writes formatted output to stdout. |
| 843 |
*/ |
| 844 |
static void print_flag_nodes(const char *pfx_flags, int width, |
| 845 |
unsigned int flags, enum cap_severity priv_sev) |
| 846 |
{ |
| 847 |
static const struct { |
| 848 |
unsigned int bit; |
| 849 |
const char *name; |
| 850 |
const char *color; |
| 851 |
} map[] = { |
| 852 |
{ FLAG_HYPERVISOR_PLANE, "hypervisor-plane", COLOR_YELLOW }, |
| 853 |
{ FLAG_SSH_VSOCK_22, "ssh-on-vsock-port-22", NULL }, |
| 854 |
{ FLAG_WILDCARD_BIND, "wildcard-bind", COLOR_YELLOW }, |
| 855 |
{ FLAG_REUSEPORT, "reuseport", COLOR_YELLOW }, |
| 856 |
{ FLAG_PRIVILEGED_CAPS, "privileged-caps", NULL }, |
| 857 |
}; |
| 858 |
size_t i; |
| 859 |
size_t n = 0; |
| 860 |
size_t printed = 0; |
| 861 |
char node[256]; |
| 862 |
|
| 863 |
for (i = 0; i < sizeof(map) / sizeof(map[0]); i++) |
| 864 |
if (flags & map[i].bit) |
| 865 |
n++; |
| 866 |
if (!n) { |
| 867 |
print_tree_node(pfx_flags, 1, "(none)", width); |
| 868 |
return; |
| 869 |
} |
| 870 |
for (i = 0; i < sizeof(map) / sizeof(map[0]); i++) { |
| 871 |
const char *color = map[i].color; |
| 872 |
|
| 873 |
if (!(flags & map[i].bit)) |
| 874 |
continue; |
| 875 |
if (map[i].bit == FLAG_PRIVILEGED_CAPS) |
| 876 |
color = sev_color(priv_sev); |
| 877 |
if (use_color && color) |
| 878 |
snprintf(node, sizeof(node), "%s%s%s", color, map[i].name, |
| 879 |
COLOR_RESET); |
| 880 |
else |
| 881 |
snprintf(node, sizeof(node), "%s", map[i].name); |
| 882 |
printed++; |
| 883 |
print_tree_node(pfx_flags, printed == n, node, width); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
/* |
| 888 |
* str_is_loopback - check whether textual @addr is loopback for @af. |
| 889 |
* @af: address family used to interpret @addr. |
| 890 |
* @addr: textual IPv4/IPv6 address to classify. |
| 891 |
* |
| 892 |
* Returns non-zero for loopback addresses, else 0. |
| 893 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 894 |
* procfs/netns state; it does not change kernel configuration. |
| 895 |
*/ |
| 896 |
static int str_is_loopback(int af, const char *addr) |
| 897 |
{ |
| 898 |
struct in_addr a4; |
| 899 |
struct in6_addr a6; |
| 900 |
|
| 901 |
if (af == AF_INET) { |
| 902 |
if (inet_pton(AF_INET, addr, &a4) != 1) |
| 903 |
return 0; |
| 904 |
return (ntohl(a4.s_addr) & 0xff000000U) == 0x7f000000U; |
| 905 |
} |
| 906 |
if (af == AF_INET6) { |
| 907 |
if (inet_pton(AF_INET6, addr, &a6) != 1) |
| 908 |
return 0; |
| 909 |
return IN6_IS_ADDR_LOOPBACK(&a6); |
| 910 |
} |
| 911 |
return 0; |
| 912 |
} |
| 913 |
|
| 914 |
/* |
| 915 |
* str_is_wildcard - check whether textual @addr is wildcard-any for @af. |
| 916 |
* @af: address family used to interpret @addr. |
| 917 |
* @addr: textual IPv4/IPv6 bind address to classify. |
| 918 |
* |
| 919 |
* Returns non-zero for 0.0.0.0/:: wildcard binds, else 0. |
| 920 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 921 |
* procfs/netns state; it does not change kernel configuration. |
| 922 |
*/ |
| 923 |
static int str_is_wildcard(int af, const char *addr) |
| 924 |
{ |
| 925 |
if (af == AF_INET) |
| 926 |
return strcmp(addr, "0.0.0.0") == 0; |
| 927 |
if (af == AF_INET6) |
| 928 |
return strcmp(addr, "::") == 0; |
| 929 |
return 0; |
| 930 |
} |
| 931 |
|
| 932 |
/* |
| 933 |
* str_is_multicast - check whether textual @addr is multicast for @af. |
| 934 |
* @af: address family used to interpret @addr. |
| 935 |
* @addr: textual IPv4/IPv6 bind address to classify. |
| 936 |
* |
| 937 |
* Returns non-zero for multicast addresses, else 0. |
| 938 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 939 |
* procfs/netns state; it does not change kernel configuration. |
| 940 |
*/ |
| 941 |
static int str_is_multicast(int af, const char *addr) |
| 942 |
{ |
| 943 |
struct in_addr a4; |
| 944 |
struct in6_addr a6; |
| 945 |
|
| 946 |
if (af == AF_INET) { |
| 947 |
if (inet_pton(AF_INET, addr, &a4) != 1) |
| 948 |
return 0; |
| 949 |
return IN_MULTICAST(ntohl(a4.s_addr)); |
| 950 |
} |
| 951 |
if (af == AF_INET6) { |
| 952 |
if (inet_pton(AF_INET6, addr, &a6) != 1) |
| 953 |
return 0; |
| 954 |
return IN6_IS_ADDR_MULTICAST(&a6); |
| 955 |
} |
| 956 |
return 0; |
| 957 |
} |
| 958 |
|
| 959 |
/* |
| 960 |
* find_iface - locate interface record by name in @m. |
| 961 |
* @m: model containing interface inventory. |
| 962 |
* @name: interface name key (borrowed, not owned). |
| 963 |
* |
| 964 |
* Returns a mutable iface pointer on match, or NULL if not found. |
| 965 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 966 |
* procfs/netns state; it does not change kernel configuration. |
| 967 |
*/ |
| 968 |
static struct iface_info *find_iface(struct model *m, const char *name) |
| 969 |
{ |
| 970 |
size_t i; |
| 971 |
|
| 972 |
for (i = 0; i < m->ifaces_n; i++) |
| 973 |
if (strcmp(m->ifaces[i].name, name) == 0) |
| 974 |
return &m->ifaces[i]; |
| 975 |
return NULL; |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* add_iface_addr - append one interface address if not already present. |
| 980 |
* @ifc: interface record to update (takes ownership of duplicated @addr). |
| 981 |
* @af: address family for @addr. |
| 982 |
* @addr: textual address to copy into @ifc. |
| 983 |
* |
| 984 |
* Returns 0 on success, -1 on allocation failure. |
| 985 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 986 |
* procfs/netns state; it does not change kernel configuration. |
| 987 |
*/ |
| 988 |
static int add_iface_addr(struct iface_info *ifc, int af, const char *addr) |
| 989 |
{ |
| 990 |
size_t i; |
| 991 |
|
| 992 |
for (i = 0; i < ifc->addrs_n; i++) { |
| 993 |
if (ifc->addrs[i].af == af && strcmp(ifc->addrs[i].addr, addr) == 0) |
| 994 |
return 0; |
| 995 |
} |
| 996 |
if (ifc->addrs_n == ifc->addrs_cap && vec_grow((void **)&ifc->addrs, |
| 997 |
&ifc->addrs_cap, sizeof(struct iface_addr))) |
| 998 |
return -1; |
| 999 |
ifc->addrs[ifc->addrs_n].af = af; |
| 1000 |
ifc->addrs[ifc->addrs_n].addr = xstrdup(addr); |
| 1001 |
if (!ifc->addrs[ifc->addrs_n].addr) |
| 1002 |
return -1; |
| 1003 |
ifc->addrs_n++; |
| 1004 |
return 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* collect_interfaces - snapshot AF_INET/AF_INET6 iface addresses in @m. |
| 1009 |
* @m: model populated from getifaddrs() in the current network namespace. |
| 1010 |
* |
| 1011 |
* Returns 0 on success, -1 on getifaddrs/allocation failure. |
| 1012 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1013 |
* procfs/netns state; it does not change kernel configuration. |
| 1014 |
*/ |
| 1015 |
static int collect_interfaces(struct model *m) |
| 1016 |
{ |
| 1017 |
struct ifaddrs *ifa, *cur; |
| 1018 |
char buf[INET6_ADDRSTRLEN]; |
| 1019 |
struct iface_info *ifc; |
| 1020 |
|
| 1021 |
if (getifaddrs(&ifa) != 0) |
| 1022 |
return -1; |
| 1023 |
for (cur = ifa; cur; cur = cur->ifa_next) { |
| 1024 |
if (!cur->ifa_name || !cur->ifa_addr) |
| 1025 |
continue; |
| 1026 |
if (cur->ifa_addr->sa_family != AF_INET && |
| 1027 |
cur->ifa_addr->sa_family != AF_INET6) |
| 1028 |
continue; |
| 1029 |
ifc = find_iface(m, cur->ifa_name); |
| 1030 |
if (!ifc) { |
| 1031 |
if (m->ifaces_n == m->ifaces_cap && |
| 1032 |
vec_grow((void **)&m->ifaces, &m->ifaces_cap, |
| 1033 |
sizeof(struct iface_info))) |
| 1034 |
goto fail; |
| 1035 |
ifc = &m->ifaces[m->ifaces_n++]; |
| 1036 |
memset(ifc, 0, sizeof(*ifc)); |
| 1037 |
ifc->name = xstrdup(cur->ifa_name); |
| 1038 |
if (!ifc->name) |
| 1039 |
goto fail; |
| 1040 |
} |
| 1041 |
if (cur->ifa_addr->sa_family == AF_INET) { |
| 1042 |
if (!inet_ntop(AF_INET, |
| 1043 |
&((struct sockaddr_in *)cur->ifa_addr)->sin_addr, |
| 1044 |
buf, sizeof(buf))) |
| 1045 |
continue; |
| 1046 |
} else { |
| 1047 |
if (!inet_ntop(AF_INET6, |
| 1048 |
&((struct sockaddr_in6 *)cur->ifa_addr)->sin6_addr, |
| 1049 |
buf, sizeof(buf))) |
| 1050 |
continue; |
| 1051 |
} |
| 1052 |
if (add_iface_addr(ifc, cur->ifa_addr->sa_family, buf)) |
| 1053 |
goto fail; |
| 1054 |
} |
| 1055 |
freeifaddrs(ifa); |
| 1056 |
return 0; |
| 1057 |
fail: |
| 1058 |
freeifaddrs(ifa); |
| 1059 |
return -1; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/* |
| 1063 |
* read_first_line - read and trim the first line from @path. |
| 1064 |
* @path: procfs/sysfs-style file path to read. |
| 1065 |
* |
| 1066 |
* Returns a caller-owned string, or NULL on open/read/allocation failure. |
| 1067 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1068 |
* procfs/netns state; it does not change kernel configuration. |
| 1069 |
*/ |
| 1070 |
static char *read_first_line(const char *path) |
| 1071 |
{ |
| 1072 |
int fd; |
| 1073 |
char *buf; |
| 1074 |
ssize_t len; |
| 1075 |
|
| 1076 |
fd = open(path, O_RDONLY | O_CLOEXEC); |
| 1077 |
if (fd < 0) |
| 1078 |
return NULL; |
| 1079 |
buf = malloc(1024); |
| 1080 |
if (!buf) { |
| 1081 |
close(fd); |
| 1082 |
return NULL; |
| 1083 |
} |
| 1084 |
len = read(fd, buf, 1023); |
| 1085 |
close(fd); |
| 1086 |
if (len <= 0) { |
| 1087 |
free(buf); |
| 1088 |
return NULL; |
| 1089 |
} |
| 1090 |
buf[len] = 0; |
| 1091 |
while (len > 0 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) |
| 1092 |
buf[--len] = 0; |
| 1093 |
return buf; |
| 1094 |
} |
| 1095 |
|
| 1096 |
/* |
| 1097 |
* extract_unit_from_cgroup - best-effort unit name lookup for @pid. |
| 1098 |
* @pid: process ID whose /proc/<pid>/cgroup is inspected. |
| 1099 |
* |
| 1100 |
* Returns a caller-owned unit/scope name, or NULL if not found/readable. |
| 1101 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1102 |
* procfs/netns state; it does not change kernel configuration. |
| 1103 |
*/ |
| 1104 |
static char *extract_unit_from_cgroup(int pid) |
| 1105 |
{ |
| 1106 |
char path[64], line[512]; |
| 1107 |
FILE *f; |
| 1108 |
|
| 1109 |
snprintf(path, sizeof(path), "/proc/%d/cgroup", pid); |
| 1110 |
f = fopen(path, "rte"); |
| 1111 |
if (!f) |
| 1112 |
return NULL; |
| 1113 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 1114 |
while (fgets(line, sizeof(line), f)) { |
| 1115 |
char *s; |
| 1116 |
s = strstr(line, ".service"); |
| 1117 |
if (!s) |
| 1118 |
s = strstr(line, ".scope"); |
| 1119 |
if (s && !strstr(line, "system.slice")) |
| 1120 |
continue; |
| 1121 |
if (s) { |
| 1122 |
while (s > line && *s != '/') |
| 1123 |
s--; |
| 1124 |
if (*s == '/') |
| 1125 |
s++; |
| 1126 |
char *e = s; |
| 1127 |
while (*e && *e != '\n' && *e != '/') |
| 1128 |
e++; |
| 1129 |
*e = 0; |
| 1130 |
fclose(f); |
| 1131 |
return xstrdup(s); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
fclose(f); |
| 1135 |
return NULL; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* |
| 1139 |
* caps_summary_for_pid - format capability summary for one process. |
| 1140 |
* @pid: process ID inspected through libcap-ng APIs. |
| 1141 |
* @privileged: out flag set when notable privileged effective caps exist. |
| 1142 |
* @has_amb: out flag set when ambient capabilities are present. |
| 1143 |
* @has_bnd: out flag set when bounding set entries are present. |
| 1144 |
* |
| 1145 |
* Returns caller-owned summary text; errors degrade to "(none)" style text. |
| 1146 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1147 |
* procfs/netns state; it does not change kernel configuration. |
| 1148 |
*/ |
| 1149 |
static char *caps_summary_for_pid(int pid, int *privileged, int *has_amb, |
| 1150 |
int *has_bnd, char **amb_list) |
| 1151 |
{ |
| 1152 |
char out[4096]; |
| 1153 |
char amb_out[1024]; |
| 1154 |
char *amb_dst = amb_out; |
| 1155 |
char *dst = out; |
| 1156 |
size_t amb_left = sizeof(amb_out); |
| 1157 |
size_t left = sizeof(out); |
| 1158 |
int i, first = 1, amb_first = 1; |
| 1159 |
capng_results_t c; |
| 1160 |
|
| 1161 |
*privileged = 0; |
| 1162 |
*has_amb = 0; |
| 1163 |
*has_bnd = 0; |
| 1164 |
*amb_list = NULL; |
| 1165 |
|
| 1166 |
capng_clear(CAPNG_SELECT_ALL); |
| 1167 |
capng_setpid(pid); |
| 1168 |
if (capng_get_caps_process()) |
| 1169 |
return xstrdup("(none)"); |
| 1170 |
|
| 1171 |
if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SYS_ADMIN) || |
| 1172 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_SYS_PTRACE) || |
| 1173 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_DAC_READ_SEARCH) || |
| 1174 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_ADMIN) || |
| 1175 |
capng_have_capability(CAPNG_EFFECTIVE, CAP_NET_RAW)) |
| 1176 |
*privileged = 1; |
| 1177 |
|
| 1178 |
c = capng_have_capabilities(CAPNG_SELECT_CAPS); |
| 1179 |
if (c == CAPNG_FULL) { |
| 1180 |
strncpy(out, "(full)", sizeof(out)); |
| 1181 |
out[sizeof(out) - 1] = 0; |
| 1182 |
} else if (c <= CAPNG_NONE) { |
| 1183 |
strncpy(out, "(none)", sizeof(out)); |
| 1184 |
out[sizeof(out) - 1] = 0; |
| 1185 |
} else { |
| 1186 |
*dst = 0; |
| 1187 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 1188 |
size_t n; |
| 1189 |
size_t sep = first ? 0 : 2; |
| 1190 |
|
| 1191 |
if (!capng_have_capability(CAPNG_PERMITTED, i)) |
| 1192 |
continue; |
| 1193 |
const char *name = capng_capability_to_name(i); |
| 1194 |
if (!name) |
| 1195 |
continue; |
| 1196 |
if (strncmp(name, "cap_", 4) == 0) |
| 1197 |
name += 4; |
| 1198 |
|
| 1199 |
n = strlen(name); |
| 1200 |
if (left <= sep + n) |
| 1201 |
break; |
| 1202 |
if (!first) { |
| 1203 |
*dst++ = ','; |
| 1204 |
*dst++ = ' '; |
| 1205 |
left -= 2; |
| 1206 |
} |
| 1207 |
memcpy(dst, name, n); |
| 1208 |
dst += n; |
| 1209 |
left -= n; |
| 1210 |
*dst = 0; |
| 1211 |
first = 0; |
| 1212 |
} |
| 1213 |
if (out[0] == 0) { |
| 1214 |
strncpy(out, "(none)", sizeof(out)); |
| 1215 |
out[sizeof(out) - 1] = 0; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
if (capng_have_capabilities(CAPNG_SELECT_AMBIENT) > CAPNG_NONE) |
| 1219 |
*has_amb = 1; |
| 1220 |
if (*has_amb) { |
| 1221 |
*amb_dst = 0; |
| 1222 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 1223 |
size_t n; |
| 1224 |
size_t sep = amb_first ? 0 : 2; |
| 1225 |
const char *name; |
| 1226 |
|
| 1227 |
if (!capng_have_capability(CAPNG_AMBIENT, i)) |
| 1228 |
continue; |
| 1229 |
name = capng_capability_to_name(i); |
| 1230 |
if (!name) |
| 1231 |
continue; |
| 1232 |
if (strncmp(name, "cap_", 4) == 0) |
| 1233 |
name += 4; |
| 1234 |
|
| 1235 |
n = strlen(name); |
| 1236 |
if (amb_left <= sep + n) |
| 1237 |
break; |
| 1238 |
if (!amb_first) { |
| 1239 |
*amb_dst++ = ','; |
| 1240 |
*amb_dst++ = ' '; |
| 1241 |
amb_left -= 2; |
| 1242 |
} |
| 1243 |
memcpy(amb_dst, name, n); |
| 1244 |
amb_dst += n; |
| 1245 |
amb_left -= n; |
| 1246 |
*amb_dst = 0; |
| 1247 |
amb_first = 0; |
| 1248 |
} |
| 1249 |
if (amb_out[0]) |
| 1250 |
*amb_list = xstrdup(amb_out); |
| 1251 |
} |
| 1252 |
if (capng_have_capabilities(CAPNG_SELECT_BOUNDS) > CAPNG_NONE) |
| 1253 |
*has_bnd = 1; |
| 1254 |
return xstrdup(out); |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* parse_status_defenses - read process hardening metadata into @d. |
| 1259 |
* @pid: process ID whose procfs status/attr files are parsed. |
| 1260 |
* @uid: process real UID, used for root/non-root interpretation. |
| 1261 |
* @d: destination struct receiving caller-freed string fields. |
| 1262 |
* @sf: parsed /proc/<pid>/status fields consumed for hardening decode. |
| 1263 |
* |
| 1264 |
* Missing fields are tolerated; function leaves best-effort defaults. |
| 1265 |
* Returns no value. |
| 1266 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1267 |
* procfs/netns state; it does not change kernel configuration. |
| 1268 |
*/ |
| 1269 |
static void parse_status_defenses(int pid, int uid, struct defense_info *d, |
| 1270 |
const struct status_fields *sf) |
| 1271 |
{ |
| 1272 |
char path[64]; |
| 1273 |
|
| 1274 |
d->runs_as_nonroot = xstrdup(uid != 0 ? "yes" : "no"); |
| 1275 |
d->no_new_privs = xstrdup("unknown"); |
| 1276 |
d->seccomp = xstrdup("disabled"); |
| 1277 |
d->lsm_label = NULL; |
| 1278 |
|
| 1279 |
if (sf->seen_no_new_privs) { |
| 1280 |
free(d->no_new_privs); |
| 1281 |
d->no_new_privs = xstrdup(sf->no_new_privs ? "yes" : "no"); |
| 1282 |
} |
| 1283 |
if (sf->seen_seccomp) { |
| 1284 |
free(d->seccomp); |
| 1285 |
if (sf->seccomp == 0) |
| 1286 |
d->seccomp = xstrdup("disabled"); |
| 1287 |
else if (sf->seccomp == 1) |
| 1288 |
d->seccomp = xstrdup("strict"); |
| 1289 |
else |
| 1290 |
d->seccomp = xstrdup("filter"); |
| 1291 |
} |
| 1292 |
|
| 1293 |
snprintf(path, sizeof(path), "/proc/%d/attr/current", pid); |
| 1294 |
d->lsm_label = read_first_line(path); |
| 1295 |
if (sanitize_untrusted_owned(&d->lsm_label) < 0) { |
| 1296 |
free(d->lsm_label); |
| 1297 |
d->lsm_label = NULL; |
| 1298 |
} |
| 1299 |
|
| 1300 |
} |
| 1301 |
|
| 1302 |
/* |
| 1303 |
* add_process - collect process metadata and append it to @m. |
| 1304 |
* @m: model taking ownership of the created process_info on success. |
| 1305 |
* @pid: numeric process ID to read from /proc. |
| 1306 |
* |
| 1307 |
* Returns stored process pointer on success, or NULL on parse/allocation error. |
| 1308 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1309 |
* procfs/netns state; it does not change kernel configuration. |
| 1310 |
*/ |
| 1311 |
static struct process_info *add_process(struct model *m, int pid) |
| 1312 |
{ |
| 1313 |
char path[64], line[256], comm[64] = ""; |
| 1314 |
char exepath[PATH_MAX]; |
| 1315 |
FILE *f; |
| 1316 |
ssize_t exelen; |
| 1317 |
int uid = -1; |
| 1318 |
int has_amb = 0, has_bnd = 0; |
| 1319 |
char *amb_list = NULL; |
| 1320 |
struct status_fields sf = { 0 }; |
| 1321 |
struct process_info *p; |
| 1322 |
|
| 1323 |
snprintf(path, sizeof(path), "/proc/%d/status", pid); |
| 1324 |
f = fopen(path, "rte"); |
| 1325 |
if (!f) |
| 1326 |
return NULL; |
| 1327 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 1328 |
while (fgets(line, sizeof(line), f)) { |
| 1329 |
if (sscanf(line, "Name:\t%63s", comm) == 1) |
| 1330 |
continue; |
| 1331 |
if (sscanf(line, "Uid:\t%d", &uid) == 1) |
| 1332 |
continue; |
| 1333 |
if (sscanf(line, "NoNewPrivs:\t%lu", &sf.no_new_privs) == 1) { |
| 1334 |
sf.seen_no_new_privs = 1; |
| 1335 |
continue; |
| 1336 |
} |
| 1337 |
if (sscanf(line, "Seccomp:\t%lu", &sf.seccomp) == 1) { |
| 1338 |
sf.seen_seccomp = 1; |
| 1339 |
continue; |
| 1340 |
} |
| 1341 |
if (uid >= 0 && comm[0] && sf.seen_no_new_privs && |
| 1342 |
sf.seen_seccomp) |
| 1343 |
break; |
| 1344 |
} |
| 1345 |
fclose(f); |
| 1346 |
if (uid < 0) |
| 1347 |
return NULL; |
| 1348 |
|
| 1349 |
p = calloc(1, sizeof(*p)); |
| 1350 |
if (!p) { |
| 1351 |
fprintf(stderr, "Out of memory\n"); |
| 1352 |
return NULL; |
| 1353 |
} |
| 1354 |
p->pid = pid; |
| 1355 |
p->uid = uid; |
| 1356 |
p->comm = xstrdup(comm[0] ? comm : "?"); |
| 1357 |
if (sanitize_untrusted_owned(&p->comm) < 0) |
| 1358 |
goto fail; |
| 1359 |
snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
| 1360 |
exelen = readlink(path, exepath, sizeof(exepath) - 1); |
| 1361 |
if (exelen >= 0) { |
| 1362 |
size_t deleted_len = strlen(" (deleted)"); |
| 1363 |
|
| 1364 |
exepath[exelen] = '\0'; |
| 1365 |
if ((size_t)exelen > deleted_len && |
| 1366 |
strcmp(exepath + exelen - deleted_len, |
| 1367 |
" (deleted)") == 0) |
| 1368 |
exepath[exelen - deleted_len] = '\0'; |
| 1369 |
p->exe = xstrdup(exepath); |
| 1370 |
if (sanitize_untrusted_owned(&p->exe) < 0) |
| 1371 |
goto fail; |
| 1372 |
} |
| 1373 |
p->unit = extract_unit_from_cgroup(pid); |
| 1374 |
if (sanitize_untrusted_owned(&p->unit) < 0) |
| 1375 |
goto fail; |
| 1376 |
p->caps = caps_summary_for_pid(pid, &p->has_privileged_caps, |
| 1377 |
&has_amb, &has_bnd, &amb_list); |
| 1378 |
p->ambient_caps = amb_list; |
| 1379 |
p->ambient_present = has_amb; |
| 1380 |
p->open_ended_bounding = has_bnd; |
| 1381 |
parse_status_defenses(pid, uid, &p->defenses, &sf); |
| 1382 |
if (!p->comm || (exelen >= 0 && !p->exe) || !p->caps || |
| 1383 |
(has_amb && !p->ambient_caps) || |
| 1384 |
!p->defenses.runs_as_nonroot || |
| 1385 |
!p->defenses.no_new_privs || !p->defenses.seccomp) |
| 1386 |
goto fail; |
| 1387 |
|
| 1388 |
if (m->procs_n == m->procs_cap && vec_grow((void **)&m->procs, |
| 1389 |
&m->procs_cap, sizeof(struct process_info *))) |
| 1390 |
goto fail; |
| 1391 |
m->procs[m->procs_n++] = p; |
| 1392 |
return p; |
| 1393 |
|
| 1394 |
fail: |
| 1395 |
free_process(p); |
| 1396 |
return NULL; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* |
| 1400 |
* add_inode_proc - add inode->process ownership mapping into @m. |
| 1401 |
* @m: model containing inode map storage. |
| 1402 |
* @inode: socket inode key from procfs/netlink tables. |
| 1403 |
* @p: process entry pointer that must remain valid for @m lifetime. |
| 1404 |
* |
| 1405 |
* Returns 0 on success, -1 on allocation failure. |
| 1406 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1407 |
* procfs/netns state; it does not change kernel configuration. |
| 1408 |
*/ |
| 1409 |
static int add_inode_proc(struct model *m, unsigned long inode, |
| 1410 |
struct process_info *p) |
| 1411 |
{ |
| 1412 |
size_t j; |
| 1413 |
ssize_t idx; |
| 1414 |
struct inode_proc *ip = NULL; |
| 1415 |
|
| 1416 |
idx = inode_hash_find(m, inode); |
| 1417 |
if (idx >= 0) |
| 1418 |
ip = &m->inode_map[idx]; |
| 1419 |
if (!ip) { |
| 1420 |
if (inode_hash_ensure_capacity(m) != 0) |
| 1421 |
return -1; |
| 1422 |
if (m->inode_n == m->inode_cap && vec_grow((void **)&m->inode_map, |
| 1423 |
&m->inode_cap, sizeof(struct inode_proc))) |
| 1424 |
return -1; |
| 1425 |
ip = &m->inode_map[m->inode_n++]; |
| 1426 |
memset(ip, 0, sizeof(*ip)); |
| 1427 |
ip->inode = inode; |
| 1428 |
inode_hash_insert(m, m->inode_n - 1); |
| 1429 |
} |
| 1430 |
for (j = 0; j < ip->n; j++) |
| 1431 |
if (ip->procs[j]->pid == p->pid) |
| 1432 |
return 0; |
| 1433 |
if (ip->n == ip->cap && vec_grow((void **)&ip->procs, &ip->cap, |
| 1434 |
sizeof(struct process_info *))) |
| 1435 |
return -1; |
| 1436 |
ip->procs[ip->n++] = p; |
| 1437 |
return 0; |
| 1438 |
} |
| 1439 |
|
| 1440 |
/* |
| 1441 |
* probe_reuseport - probe SO_REUSEPORT on a target process socket fd. |
| 1442 |
* @pid: process id owning @fdnum. |
| 1443 |
* @fdnum: socket fd number in target process. |
| 1444 |
* |
| 1445 |
* Uses pidfd_open + pidfd_getfd when supported by the running kernel. |
| 1446 |
* Returns 1/0 on successful getsockopt, or -1 when unsupported/inaccessible. |
| 1447 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1448 |
* procfs/netns state; it does not change kernel configuration. |
| 1449 |
*/ |
| 1450 |
static int probe_reuseport(int pid, int fdnum) |
| 1451 |
{ |
| 1452 |
#if defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd) |
| 1453 |
static int warned_unavail; |
| 1454 |
int pidfd; |
| 1455 |
int dupfd; |
| 1456 |
int val = 0; |
| 1457 |
socklen_t len = sizeof(val); |
| 1458 |
int rc; |
| 1459 |
|
| 1460 |
pidfd = syscall(__NR_pidfd_open, pid, 0); |
| 1461 |
if (pidfd < 0) { |
| 1462 |
if (errno == ENOSYS && !warned_unavail) { |
| 1463 |
diag_dbg("pidfd_getfd unavailable; SO_REUSEPORT detection disabled"); |
| 1464 |
warned_unavail = 1; |
| 1465 |
} |
| 1466 |
return -1; |
| 1467 |
} |
| 1468 |
|
| 1469 |
dupfd = syscall(__NR_pidfd_getfd, pidfd, fdnum, 0); |
| 1470 |
if (dupfd < 0) { |
| 1471 |
if (errno == ENOSYS && !warned_unavail) { |
| 1472 |
diag_dbg("pidfd_getfd unavailable; SO_REUSEPORT detection disabled"); |
| 1473 |
warned_unavail = 1; |
| 1474 |
} |
| 1475 |
close(pidfd); |
| 1476 |
return -1; |
| 1477 |
} |
| 1478 |
|
| 1479 |
rc = getsockopt(dupfd, SOL_SOCKET, SO_REUSEPORT, &val, &len); |
| 1480 |
close(dupfd); |
| 1481 |
close(pidfd); |
| 1482 |
if (rc < 0) |
| 1483 |
return -1; |
| 1484 |
|
| 1485 |
return val ? 1 : 0; |
| 1486 |
#else |
| 1487 |
(void)pid; |
| 1488 |
(void)fdnum; |
| 1489 |
return -1; |
| 1490 |
#endif |
| 1491 |
} |
| 1492 |
|
| 1493 |
/* |
| 1494 |
* collect_proc_inodes - build inode ownership map from /proc/<pid>/fd links. |
| 1495 |
* @m: model receiving process entries and inode->process associations. |
| 1496 |
* |
| 1497 |
* This is best-effort and skips tasks/fds hidden by permissions or races. |
| 1498 |
* Returns no value. |
| 1499 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1500 |
* procfs/netns state; it does not change kernel configuration. |
| 1501 |
*/ |
| 1502 |
static void collect_proc_inodes(struct model *m) |
| 1503 |
{ |
| 1504 |
DIR *d; |
| 1505 |
struct dirent *ent; |
| 1506 |
|
| 1507 |
d = opendir("/proc"); |
| 1508 |
if (!d) |
| 1509 |
return; |
| 1510 |
while ((ent = readdir(d))) { |
| 1511 |
int pid; |
| 1512 |
DIR *fds; |
| 1513 |
struct dirent *fdent; |
| 1514 |
char fdpath[64]; |
| 1515 |
struct process_info *p; |
| 1516 |
|
| 1517 |
if (ent->d_name[0] < '0' || ent->d_name[0] > '9') |
| 1518 |
continue; |
| 1519 |
pid = atoi(ent->d_name); |
| 1520 |
if (pid <= 0) |
| 1521 |
continue; |
| 1522 |
p = add_process(m, pid); |
| 1523 |
if (!p) |
| 1524 |
continue; |
| 1525 |
snprintf(fdpath, sizeof(fdpath), "/proc/%d/fd", pid); |
| 1526 |
fds = opendir(fdpath); |
| 1527 |
if (!fds) |
| 1528 |
continue; |
| 1529 |
while ((fdent = readdir(fds))) { |
| 1530 |
char lpath[128], link[256], *s; |
| 1531 |
ssize_t l; |
| 1532 |
unsigned long inode; |
| 1533 |
int fdnum; |
| 1534 |
int reuseport; |
| 1535 |
struct inode_proc *ip; |
| 1536 |
|
| 1537 |
if (fdent->d_name[0] == '.') |
| 1538 |
continue; |
| 1539 |
snprintf(lpath, sizeof(lpath), "%s/%s", fdpath, fdent->d_name); |
| 1540 |
l = readlink(lpath, link, sizeof(link) - 1); |
| 1541 |
if (l < 0) |
| 1542 |
continue; |
| 1543 |
link[l] = 0; |
| 1544 |
/* |
| 1545 |
* procfs may expose socket links in kernel-dependent formats. |
| 1546 |
* Handle both common "socket:[inode]" and "[0000]:inode" forms. |
| 1547 |
*/ |
| 1548 |
if (strncmp(link, "socket:[", 8) == 0) { |
| 1549 |
s = link + 8; |
| 1550 |
} else if (strncmp(link, "[0000]:", 7) == 0) { |
| 1551 |
s = link + 7; |
| 1552 |
} else { |
| 1553 |
continue; |
| 1554 |
} |
| 1555 |
inode = strtoul(s, NULL, 10); |
| 1556 |
if (!inode) |
| 1557 |
continue; |
| 1558 |
add_inode_proc(m, inode, p); |
| 1559 |
|
| 1560 |
fdnum = atoi(fdent->d_name); |
| 1561 |
if (fdnum < 0) |
| 1562 |
continue; |
| 1563 |
/* |
| 1564 |
* Probe SO_REUSEPORT here because this is the only stage with both |
| 1565 |
* target pid and fd number for pidfd_getfd; store on inode_proc so |
| 1566 |
* endpoint projection can reuse the result later. |
| 1567 |
*/ |
| 1568 |
reuseport = probe_reuseport(pid, fdnum); |
| 1569 |
if (reuseport != 1) |
| 1570 |
continue; |
| 1571 |
ip = lookup_inode(m, inode); |
| 1572 |
if (ip) |
| 1573 |
ip->reuseport |= 1; |
| 1574 |
} |
| 1575 |
closedir(fds); |
| 1576 |
} |
| 1577 |
closedir(d); |
| 1578 |
} |
| 1579 |
|
| 1580 |
/* |
| 1581 |
* lookup_inode - locate inode ownership entry in @m. |
| 1582 |
* @m: model containing inode ownership map. |
| 1583 |
* @inode: socket inode key to resolve. |
| 1584 |
* |
| 1585 |
* Returns mutable map entry pointer, or NULL if inode is unknown. |
| 1586 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1587 |
* procfs/netns state; it does not change kernel configuration. |
| 1588 |
*/ |
| 1589 |
static struct inode_proc *lookup_inode(struct model *m, unsigned long inode) |
| 1590 |
{ |
| 1591 |
ssize_t idx; |
| 1592 |
|
| 1593 |
idx = inode_hash_find(m, inode); |
| 1594 |
if (idx < 0) |
| 1595 |
return NULL; |
| 1596 |
return &m->inode_map[idx]; |
| 1597 |
} |
| 1598 |
|
| 1599 |
/* |
| 1600 |
* add_endpoint - add/merge one inet or packet endpoint in @m. |
| 1601 |
* @m: model receiving endpoint data. |
| 1602 |
* @proto/@bind/@ifname/@ifaddr: copied strings for endpoint identity. |
| 1603 |
* @port/@plane: endpoint attributes for rendering/grouping. |
| 1604 |
* @attrs: wildcard/reuseport flags carried from parser/projection stages. |
| 1605 |
* @ip: inode-owner mapping whose process pointers are attached to endpoint. |
| 1606 |
* |
| 1607 |
* Returns 0 on success, -1 on allocation failure. |
| 1608 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1609 |
* procfs/netns state; it does not change kernel configuration. |
| 1610 |
*/ |
| 1611 |
static int add_endpoint(struct model *m, const char *proto, const char *bind, |
| 1612 |
unsigned int port, enum plane_kind plane, const char *ifname, |
| 1613 |
const char *ifaddr, const struct endpoint_attrs *attrs, |
| 1614 |
struct inode_proc *ip) |
| 1615 |
{ |
| 1616 |
size_t i, j; |
| 1617 |
struct endpoint *e; |
| 1618 |
char label[256]; |
| 1619 |
|
| 1620 |
if (strchr(bind, ':')) |
| 1621 |
snprintf(label, sizeof(label), "%s:[%s]:%u", proto, bind, port); |
| 1622 |
else |
| 1623 |
snprintf(label, sizeof(label), "%s:%s:%u", proto, bind, port); |
| 1624 |
|
| 1625 |
for (i = 0; i < m->eps_n; i++) { |
| 1626 |
e = &m->eps[i]; |
| 1627 |
if (strcmp(e->label, label) == 0 && strcmp(e->ifname, ifname) == 0 && |
| 1628 |
strcmp(e->ifaddr, ifaddr) == 0) { |
| 1629 |
e->reuseport |= attrs->reuseport; |
| 1630 |
goto add_procs; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
if (m->eps_n == m->eps_cap && vec_grow((void **)&m->eps, &m->eps_cap, |
| 1634 |
sizeof(struct endpoint))) |
| 1635 |
return -1; |
| 1636 |
e = &m->eps[m->eps_n]; |
| 1637 |
memset(e, 0, sizeof(*e)); |
| 1638 |
e->proto = xstrdup(proto); |
| 1639 |
e->bind = xstrdup(bind); |
| 1640 |
e->label = xstrdup(label); |
| 1641 |
e->port = port; |
| 1642 |
e->vsock_cid = 0; |
| 1643 |
e->has_vsock = 0; |
| 1644 |
e->plane = plane; |
| 1645 |
e->ifname = xstrdup(ifname); |
| 1646 |
e->ifaddr = xstrdup(ifaddr); |
| 1647 |
e->wildcard_bind = attrs->wildcard; |
| 1648 |
e->reuseport = attrs->reuseport; |
| 1649 |
if (!e->proto || !e->bind || !e->label || !e->ifname || !e->ifaddr) { |
| 1650 |
free(e->proto); |
| 1651 |
free(e->bind); |
| 1652 |
free(e->label); |
| 1653 |
free(e->ifname); |
| 1654 |
free(e->ifaddr); |
| 1655 |
memset(e, 0, sizeof(*e)); |
| 1656 |
return -1; |
| 1657 |
} |
| 1658 |
m->eps_n++; |
| 1659 |
add_procs: |
| 1660 |
for (j = 0; j < ip->n; j++) { |
| 1661 |
size_t k; |
| 1662 |
for (k = 0; k < e->procs_n; k++) |
| 1663 |
if (e->procs[k]->pid == ip->procs[j]->pid) |
| 1664 |
goto next; |
| 1665 |
if (e->procs_n == e->procs_cap && vec_grow((void **)&e->procs, |
| 1666 |
&e->procs_cap, sizeof(struct process_info *))) |
| 1667 |
return -1; |
| 1668 |
e->procs[e->procs_n++] = ip->procs[j]; |
| 1669 |
next: |
| 1670 |
; |
| 1671 |
} |
| 1672 |
return 0; |
| 1673 |
} |
| 1674 |
|
| 1675 |
/* |
| 1676 |
* add_vsock_endpoint - add/merge one VSOCK endpoint in @m. |
| 1677 |
* @m: model receiving endpoint data. |
| 1678 |
* @type: socket type label (stream/dgram/seqpacket) copied into model. |
| 1679 |
* @cid/@port: source CID/port; @cid may be VMADDR_CID_ANY. |
| 1680 |
* @ip: inode-owner mapping whose process pointers are attached to endpoint. |
| 1681 |
* |
| 1682 |
* Returns 0 on success, -1 on allocation failure. |
| 1683 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1684 |
* procfs/netns state; it does not change kernel configuration. |
| 1685 |
*/ |
| 1686 |
static int add_vsock_endpoint(struct model *m, const char *type, |
| 1687 |
unsigned int cid, unsigned int port, struct inode_proc *ip) |
| 1688 |
{ |
| 1689 |
size_t i, j; |
| 1690 |
struct endpoint *e; |
| 1691 |
char label[128]; |
| 1692 |
char cidbuf[32]; |
| 1693 |
|
| 1694 |
if (cid == VMADDR_CID_ANY) |
| 1695 |
strcpy(cidbuf, "ANY"); |
| 1696 |
else |
| 1697 |
snprintf(cidbuf, sizeof(cidbuf), "%u", cid); |
| 1698 |
snprintf(label, sizeof(label), "%s:cid=%s:%u", type, cidbuf, port); |
| 1699 |
|
| 1700 |
for (i = 0; i < m->eps_n; i++) { |
| 1701 |
e = &m->eps[i]; |
| 1702 |
if (e->plane == PLANE_VSOCK && strcmp(e->label, label) == 0) |
| 1703 |
goto add_procs; |
| 1704 |
} |
| 1705 |
if (m->eps_n == m->eps_cap && vec_grow((void **)&m->eps, &m->eps_cap, |
| 1706 |
sizeof(struct endpoint))) |
| 1707 |
return -1; |
| 1708 |
e = &m->eps[m->eps_n]; |
| 1709 |
memset(e, 0, sizeof(*e)); |
| 1710 |
e->proto = xstrdup(type); |
| 1711 |
e->bind = xstrdup(cidbuf); |
| 1712 |
e->label = xstrdup(label); |
| 1713 |
e->port = port; |
| 1714 |
e->vsock_cid = cid; |
| 1715 |
e->has_vsock = 1; |
| 1716 |
e->plane = PLANE_VSOCK; |
| 1717 |
e->ifname = xstrdup(""); |
| 1718 |
e->ifaddr = xstrdup(""); |
| 1719 |
e->reuseport = 0; |
| 1720 |
if (!e->proto || !e->bind || !e->label || !e->ifname || !e->ifaddr) { |
| 1721 |
free(e->proto); |
| 1722 |
free(e->bind); |
| 1723 |
free(e->label); |
| 1724 |
free(e->ifname); |
| 1725 |
free(e->ifaddr); |
| 1726 |
memset(e, 0, sizeof(*e)); |
| 1727 |
return -1; |
| 1728 |
} |
| 1729 |
m->eps_n++; |
| 1730 |
|
| 1731 |
add_procs: |
| 1732 |
for (j = 0; j < ip->n; j++) { |
| 1733 |
size_t k; |
| 1734 |
|
| 1735 |
for (k = 0; k < e->procs_n; k++) |
| 1736 |
if (e->procs[k]->pid == ip->procs[j]->pid) |
| 1737 |
goto next; |
| 1738 |
if (e->procs_n == e->procs_cap && vec_grow((void **)&e->procs, |
| 1739 |
&e->procs_cap, sizeof(struct process_info *))) |
| 1740 |
return -1; |
| 1741 |
e->procs[e->procs_n++] = ip->procs[j]; |
| 1742 |
next: |
| 1743 |
; |
| 1744 |
} |
| 1745 |
return 0; |
| 1746 |
} |
| 1747 |
|
| 1748 |
/* |
| 1749 |
* endpoint_to_ifaces - project one inet bind onto iface/address groupings. |
| 1750 |
* @m: model containing iface inventory and endpoint lists. |
| 1751 |
* @proto/@af/@bind/@port: socket identity from procfs/sock_diag. |
| 1752 |
* @ip: inode-owner mapping used to attach owning processes. |
| 1753 |
* |
| 1754 |
* Wildcard binds expand across non-loopback ifaces in current netns. |
| 1755 |
* Returns no value. |
| 1756 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1757 |
* procfs/netns state; it does not change kernel configuration. |
| 1758 |
*/ |
| 1759 |
static void endpoint_to_ifaces(struct model *m, const char *proto, int af, |
| 1760 |
const char *bind, unsigned int port, int reuseport, |
| 1761 |
struct inode_proc *ip) |
| 1762 |
{ |
| 1763 |
size_t i, j; |
| 1764 |
int wildcard = str_is_wildcard(af, bind); |
| 1765 |
int multicast = str_is_multicast(af, bind); |
| 1766 |
int matched = 0; |
| 1767 |
struct endpoint_attrs attrs; |
| 1768 |
|
| 1769 |
attrs.wildcard = wildcard; |
| 1770 |
attrs.reuseport = reuseport; |
| 1771 |
|
| 1772 |
for (i = 0; i < m->ifaces_n; i++) { |
| 1773 |
struct iface_info *ifc = &m->ifaces[i]; |
| 1774 |
for (j = 0; j < ifc->addrs_n; j++) { |
| 1775 |
if (ifc->addrs[j].af != af) |
| 1776 |
continue; |
| 1777 |
if (wildcard) { |
| 1778 |
/* |
| 1779 |
* 0.0.0.0/:: listeners are treated as externally reachable; keep |
| 1780 |
* loopback out of this expansion to avoid duplicate exposure rows. |
| 1781 |
*/ |
| 1782 |
if (strcmp(ifc->name, "lo") == 0) |
| 1783 |
continue; |
| 1784 |
add_endpoint(m, proto, bind, port, PLANE_INET_EXTERNAL, |
| 1785 |
ifc->name, ifc->addrs[j].addr, &attrs, ip); |
| 1786 |
matched = 1; |
| 1787 |
} else if (strcmp(ifc->addrs[j].addr, bind) == 0) { |
| 1788 |
enum plane_kind plane = str_is_loopback(af, bind) ? |
| 1789 |
PLANE_INET_LOOPBACK : PLANE_INET_EXTERNAL; |
| 1790 |
add_endpoint(m, proto, bind, port, plane, ifc->name, |
| 1791 |
ifc->addrs[j].addr, &attrs, ip); |
| 1792 |
matched = 1; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
} |
| 1796 |
if (!matched) |
| 1797 |
add_endpoint(m, proto, bind, port, |
| 1798 |
str_is_loopback(af, bind) ? |
| 1799 |
PLANE_INET_LOOPBACK : PLANE_INET_EXTERNAL, |
| 1800 |
str_is_loopback(af, bind) ? "lo" : |
| 1801 |
(multicast ? "multicast/group" : "unknown"), |
| 1802 |
bind, &attrs, ip); |
| 1803 |
} |
| 1804 |
|
| 1805 |
/* |
| 1806 |
* parse_inet_file - parse one procfs inet socket table into endpoints. |
| 1807 |
* @m: model receiving endpoint mappings. |
| 1808 |
* @path: procfs table path (tcp/udp/raw variants). |
| 1809 |
* @proto: protocol label used in rendered endpoint names. |
| 1810 |
* @af: address family used to decode local bind addresses. |
| 1811 |
* |
| 1812 |
* Non-listeners/unowned sockets are skipped; output is best-effort. |
| 1813 |
* Returns no value. |
| 1814 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1815 |
* procfs/netns state; it does not change kernel configuration. |
| 1816 |
*/ |
| 1817 |
static void parse_inet_file(struct model *m, const char *path, |
| 1818 |
const char *proto, int af) |
| 1819 |
{ |
| 1820 |
FILE *f; |
| 1821 |
char line[512]; |
| 1822 |
int row = 0; |
| 1823 |
|
| 1824 |
f = fopen(path, "rte"); |
| 1825 |
if (!f) |
| 1826 |
return; |
| 1827 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 1828 |
while (fgets(line, sizeof(line), f)) { |
| 1829 |
unsigned int lport, rport, state; |
| 1830 |
unsigned long txq, rxq, tmr, when, retr, inode; |
| 1831 |
int d, uid, timeout; |
| 1832 |
char laddrh[96], raddrh[96], more[128]; |
| 1833 |
char addr[INET6_ADDRSTRLEN]; |
| 1834 |
struct inode_proc *ip; |
| 1835 |
more[0] = 0; |
| 1836 |
if (!row++) |
| 1837 |
continue; |
| 1838 |
if (sscanf(line, "%d: %95[0-9A-Fa-f]:%X %95[0-9A-Fa-f]:%X %X " |
| 1839 |
"%lX:%lX %lX:%lX %lX %d %d %lu %127s", |
| 1840 |
&d, laddrh, &lport, raddrh, &rport, &state, &txq, &rxq, |
| 1841 |
&tmr, &when, &retr, &uid, &timeout, &inode, more) < 14) |
| 1842 |
continue; |
| 1843 |
if ((strcmp(proto, "tcp") == 0 || |
| 1844 |
strcmp(proto, "tcp6") == 0) && state != 0x0A) |
| 1845 |
continue; |
| 1846 |
if ((strcmp(proto, "udp") == 0 || |
| 1847 |
strcmp(proto, "udp6") == 0 || |
| 1848 |
strcmp(proto, "udplite") == 0 || |
| 1849 |
strcmp(proto, "udplite6") == 0) && lport == 0) |
| 1850 |
continue; |
| 1851 |
ip = lookup_inode(m, inode); |
| 1852 |
if (!ip) |
| 1853 |
continue; |
| 1854 |
if (af == AF_INET) { |
| 1855 |
struct in_addr v4; |
| 1856 |
unsigned int host; |
| 1857 |
|
| 1858 |
if (sscanf(laddrh, "%8x", &host) != 1) |
| 1859 |
continue; |
| 1860 |
/* |
| 1861 |
* procfs inet tables print IPv4 addresses as host- |
| 1862 |
* order hex. Assigning directly to s_addr keeps the |
| 1863 |
* bytes correct on both little- and big-endian systems. |
| 1864 |
*/ |
| 1865 |
v4.s_addr = host; |
| 1866 |
if (!inet_ntop(AF_INET, &v4, addr, sizeof(addr))) |
| 1867 |
continue; |
| 1868 |
} else { |
| 1869 |
unsigned char bytes[16] = { 0 }; |
| 1870 |
int i; |
| 1871 |
int ok = 1; |
| 1872 |
if (strlen(laddrh) != 32) |
| 1873 |
continue; |
| 1874 |
for (i = 0; i < 4; i++) { |
| 1875 |
uint32_t host; |
| 1876 |
uint32_t net; |
| 1877 |
|
| 1878 |
if (sscanf(laddrh + (i * 8), "%8x", &host) != 1) { |
| 1879 |
ok = 0; |
| 1880 |
break; |
| 1881 |
} |
| 1882 |
net = htonl(host); |
| 1883 |
memcpy(bytes + (i * 4), &net, sizeof(net)); |
| 1884 |
} |
| 1885 |
if (!ok) |
| 1886 |
continue; |
| 1887 |
if (!inet_ntop(AF_INET6, bytes, addr, sizeof(addr))) |
| 1888 |
continue; |
| 1889 |
} |
| 1890 |
endpoint_to_ifaces(m, proto, af, addr, lport, ip->reuseport, ip); |
| 1891 |
} |
| 1892 |
fclose(f); |
| 1893 |
} |
| 1894 |
|
| 1895 |
/* |
| 1896 |
* parse_packet_file - parse /proc/net/packet and add packet endpoints. |
| 1897 |
* @m: model receiving packet-plane endpoint/process mappings. |
| 1898 |
* |
| 1899 |
* Visibility depends on current netns and procfs access permissions. |
| 1900 |
* Returns no value. |
| 1901 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1902 |
* procfs/netns state; it does not change kernel configuration. |
| 1903 |
*/ |
| 1904 |
static void parse_packet_file(struct model *m) |
| 1905 |
{ |
| 1906 |
FILE *f; |
| 1907 |
char line[512], ifn[IF_NAMESIZE]; |
| 1908 |
int row = 0; |
| 1909 |
|
| 1910 |
f = fopen("/proc/net/packet", "rte"); |
| 1911 |
if (!f) |
| 1912 |
return; |
| 1913 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 1914 |
while (fgets(line, sizeof(line), f)) { |
| 1915 |
unsigned long sk, inode; |
| 1916 |
unsigned int ref, type, proto, iface, r, rmem, uid; |
| 1917 |
struct inode_proc *ip; |
| 1918 |
struct endpoint_attrs attrs; |
| 1919 |
char bind[64], addr[64], name[64]; |
| 1920 |
|
| 1921 |
if (!row++) |
| 1922 |
continue; |
| 1923 |
if (sscanf(line, "%lX %u %u %X %u %u %u %u %lu", |
| 1924 |
&sk, &ref, &type, &proto, &iface, &r, &rmem, &uid, |
| 1925 |
&inode) < 9) |
| 1926 |
continue; |
| 1927 |
ip = lookup_inode(m, inode); |
| 1928 |
if (!ip) |
| 1929 |
continue; |
| 1930 |
if (!if_indextoname(iface, ifn)) |
| 1931 |
strcpy(ifn, "unknown"); |
| 1932 |
snprintf(bind, sizeof(bind), "::"); |
| 1933 |
snprintf(addr, sizeof(addr), "ifindex:%u", iface); |
| 1934 |
snprintf(name, sizeof(name), "packet"); |
| 1935 |
attrs.wildcard = 0; |
| 1936 |
attrs.reuseport = 0; |
| 1937 |
add_endpoint(m, name, bind, proto, PLANE_PACKET, ifn, addr, |
| 1938 |
&attrs, ip); |
| 1939 |
} |
| 1940 |
fclose(f); |
| 1941 |
} |
| 1942 |
|
| 1943 |
/* |
| 1944 |
* parse_u32_hex_or_dec - parse @s as decimal or hexadecimal u32. |
| 1945 |
* @s: numeric token from procfs/netlink text fields. |
| 1946 |
* @out: destination value on successful parse. |
| 1947 |
* |
| 1948 |
* Returns 0 on success, -1 if @s is not a valid integer token. |
| 1949 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1950 |
* procfs/netns state; it does not change kernel configuration. |
| 1951 |
*/ |
| 1952 |
static int parse_u32_hex_or_dec(const char *s, unsigned int *out) |
| 1953 |
{ |
| 1954 |
char *end; |
| 1955 |
unsigned long v; |
| 1956 |
int base = 10; |
| 1957 |
const char *p; |
| 1958 |
|
| 1959 |
for (p = s; *p; p++) { |
| 1960 |
if ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')) { |
| 1961 |
base = 16; |
| 1962 |
break; |
| 1963 |
} |
| 1964 |
} |
| 1965 |
if (strncmp(s, "0x", 2) == 0 || strncmp(s, "0X", 2) == 0) |
| 1966 |
base = 16; |
| 1967 |
if (base == 10 && strlen(s) > 3 && s[0] == '0') |
| 1968 |
base = 16; |
| 1969 |
v = strtoul(s, &end, base); |
| 1970 |
if (end == s || *end) |
| 1971 |
return -1; |
| 1972 |
*out = (unsigned int)v; |
| 1973 |
return 0; |
| 1974 |
} |
| 1975 |
|
| 1976 |
/* |
| 1977 |
* parse_vsock_file - fallback VSOCK parser using /proc/net/vsock. |
| 1978 |
* @m: model receiving parsed VSOCK endpoint/process mappings. |
| 1979 |
* |
| 1980 |
* Used when sock_diag support is unavailable or denied. |
| 1981 |
* Returns no value. |
| 1982 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 1983 |
* procfs/netns state; it does not change kernel configuration. |
| 1984 |
*/ |
| 1985 |
static void parse_vsock_file(struct model *m) |
| 1986 |
{ |
| 1987 |
FILE *f; |
| 1988 |
char line[512]; |
| 1989 |
|
| 1990 |
f = fopen("/proc/net/vsock", "rte"); |
| 1991 |
if (!f) |
| 1992 |
return; |
| 1993 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 1994 |
while (fgets(line, sizeof(line), f)) { |
| 1995 |
char work[512]; |
| 1996 |
char *tok[24]; |
| 1997 |
char *save = NULL; |
| 1998 |
char *local, *s, *sep; |
| 1999 |
int tcnt = 0; |
| 2000 |
unsigned long inode; |
| 2001 |
unsigned int st, type, cid, port; |
| 2002 |
struct inode_proc *ip; |
| 2003 |
const char *kind; |
| 2004 |
|
| 2005 |
if (strstr(line, "Local") || strstr(line, "local") || |
| 2006 |
strstr(line, "Num")) |
| 2007 |
continue; |
| 2008 |
snprintf(work, sizeof(work), "%s", line); |
| 2009 |
s = strtok_r(work, " \t\n", &save); |
| 2010 |
while (s && tcnt < (int)(sizeof(tok) / sizeof(tok[0]))) { |
| 2011 |
tok[tcnt++] = s; |
| 2012 |
s = strtok_r(NULL, " \t\n", &save); |
| 2013 |
} |
| 2014 |
if (tcnt < 5) |
| 2015 |
continue; |
| 2016 |
local = NULL; |
| 2017 |
for (int i = 0; i < tcnt; i++) { |
| 2018 |
if (strchr(tok[i], ':')) { |
| 2019 |
local = tok[i]; |
| 2020 |
break; |
| 2021 |
} |
| 2022 |
} |
| 2023 |
if (!local) |
| 2024 |
continue; |
| 2025 |
sep = strchr(local, ':'); |
| 2026 |
if (!sep) |
| 2027 |
continue; |
| 2028 |
*sep = '\0'; |
| 2029 |
if (parse_u32_hex_or_dec(local, &cid) || |
| 2030 |
parse_u32_hex_or_dec(sep + 1, &port)) |
| 2031 |
continue; |
| 2032 |
|
| 2033 |
if (parse_u32_hex_or_dec(tok[tcnt - 2], &st)) |
| 2034 |
continue; |
| 2035 |
if (parse_u32_hex_or_dec(tok[tcnt - 3], &type)) |
| 2036 |
continue; |
| 2037 |
inode = strtoul(tok[tcnt - 1], NULL, 10); |
| 2038 |
if (!inode) |
| 2039 |
continue; |
| 2040 |
|
| 2041 |
if (type == SOCK_STREAM) { |
| 2042 |
if (st != 0x0A) |
| 2043 |
continue; |
| 2044 |
kind = "stream"; |
| 2045 |
} else if (type == SOCK_SEQPACKET) { |
| 2046 |
if (st != 0x0A) |
| 2047 |
continue; |
| 2048 |
kind = "seqpacket"; |
| 2049 |
} else if (type == SOCK_DGRAM) { |
| 2050 |
if (port == 0) |
| 2051 |
continue; |
| 2052 |
kind = "dgram"; |
| 2053 |
} else { |
| 2054 |
continue; |
| 2055 |
} |
| 2056 |
|
| 2057 |
ip = lookup_inode(m, inode); |
| 2058 |
if (!ip) |
| 2059 |
continue; |
| 2060 |
add_vsock_endpoint(m, kind, cid, port, ip); |
| 2061 |
} |
| 2062 |
fclose(f); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* |
| 2066 |
* vsock_type_to_name - map VSOCK socket type to display label. |
| 2067 |
* @type: SOCK_* type value from kernel socket metadata. |
| 2068 |
* |
| 2069 |
* Returns static string label, or NULL for unknown/unsupported type. |
| 2070 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2071 |
* procfs/netns state; it does not change kernel configuration. |
| 2072 |
*/ |
| 2073 |
static const char *vsock_type_to_name(unsigned int type) |
| 2074 |
{ |
| 2075 |
if (type == SOCK_STREAM) |
| 2076 |
return "stream"; |
| 2077 |
if (type == SOCK_DGRAM) |
| 2078 |
return "dgram"; |
| 2079 |
if (type == SOCK_SEQPACKET) |
| 2080 |
return "seqpacket"; |
| 2081 |
return NULL; |
| 2082 |
} |
| 2083 |
|
| 2084 |
#ifdef HAVE_LINUX_VM_SOCKETS_DIAG_H |
| 2085 |
/* |
| 2086 |
* parse_vsock_diag_messages - consume VSOCK sock_diag dump replies. |
| 2087 |
* @m: model receiving VSOCK endpoint/process mappings. |
| 2088 |
* @fd: open NETLINK_SOCK_DIAG socket with pending VSOCK responses. |
| 2089 |
* |
| 2090 |
* Returns 0 on NLMSG_DONE, or -1 on malformed/error netlink messages. |
| 2091 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2092 |
* procfs/netns state; it does not change kernel configuration. |
| 2093 |
*/ |
| 2094 |
static int parse_vsock_diag_messages(struct model *m, int fd) |
| 2095 |
{ |
| 2096 |
char buf[8192]; |
| 2097 |
ssize_t len; |
| 2098 |
|
| 2099 |
while (1) { |
| 2100 |
len = recv(fd, buf, sizeof(buf), 0); |
| 2101 |
if (len < 0) { |
| 2102 |
if (errno == EINTR) |
| 2103 |
continue; |
| 2104 |
return -1; |
| 2105 |
} |
| 2106 |
if (len == 0) |
| 2107 |
return -1; |
| 2108 |
|
| 2109 |
struct nlmsghdr *nlh; |
| 2110 |
unsigned int rem; |
| 2111 |
|
| 2112 |
if (len > UINT_MAX) |
| 2113 |
return -1; |
| 2114 |
rem = (unsigned int)len; |
| 2115 |
|
| 2116 |
for (nlh = (struct nlmsghdr *)buf; |
| 2117 |
NLMSG_OK(nlh, rem); |
| 2118 |
nlh = NLMSG_NEXT(nlh, rem)) { |
| 2119 |
struct vsock_diag_msg *r; |
| 2120 |
struct inode_proc *ip; |
| 2121 |
const char *kind; |
| 2122 |
|
| 2123 |
if (nlh->nlmsg_type == NLMSG_DONE) |
| 2124 |
return 0; |
| 2125 |
if (nlh->nlmsg_type == NLMSG_ERROR) { |
| 2126 |
struct nlmsgerr *e; |
| 2127 |
|
| 2128 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*e))) |
| 2129 |
return -1; |
| 2130 |
e = NLMSG_DATA(nlh); |
| 2131 |
if (e->error == 0) |
| 2132 |
continue; |
| 2133 |
errno = -e->error; |
| 2134 |
return -1; |
| 2135 |
} |
| 2136 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*r))) |
| 2137 |
continue; |
| 2138 |
|
| 2139 |
r = NLMSG_DATA(nlh); |
| 2140 |
if (r->vdiag_family != AF_VSOCK) |
| 2141 |
continue; |
| 2142 |
kind = vsock_type_to_name(r->vdiag_type); |
| 2143 |
if (!kind) |
| 2144 |
continue; |
| 2145 |
|
| 2146 |
if ((r->vdiag_type == SOCK_STREAM || |
| 2147 |
r->vdiag_type == SOCK_SEQPACKET) && |
| 2148 |
r->vdiag_state != TCP_LISTEN) |
| 2149 |
continue; |
| 2150 |
if (r->vdiag_type == SOCK_DGRAM && |
| 2151 |
r->vdiag_src_port == 0) |
| 2152 |
continue; |
| 2153 |
|
| 2154 |
ip = lookup_inode(m, r->vdiag_ino); |
| 2155 |
if (!ip) |
| 2156 |
continue; |
| 2157 |
|
| 2158 |
diag_dbg("vsock type=%u state=%u src=%u:%u dst=%u:%u ino=%u", |
| 2159 |
r->vdiag_type, r->vdiag_state, r->vdiag_src_cid, |
| 2160 |
r->vdiag_src_port, r->vdiag_dst_cid, r->vdiag_dst_port, |
| 2161 |
r->vdiag_ino); |
| 2162 |
add_vsock_endpoint(m, kind, r->vdiag_src_cid, |
| 2163 |
r->vdiag_src_port, ip); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
} |
| 2167 |
|
| 2168 |
/* |
| 2169 |
* parse_vsock_diag - request VSOCK listener dump via sock_diag netlink. |
| 2170 |
* @m: model receiving parsed VSOCK endpoint/process mappings. |
| 2171 |
* |
| 2172 |
* Returns 0 on success, -1 with errno on permission/support/socket errors. |
| 2173 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2174 |
* procfs/netns state; it does not change kernel configuration. |
| 2175 |
*/ |
| 2176 |
static int parse_vsock_diag(struct model *m) |
| 2177 |
{ |
| 2178 |
struct { |
| 2179 |
struct nlmsghdr nlh; |
| 2180 |
struct vsock_diag_req req; |
| 2181 |
} req; |
| 2182 |
struct sockaddr_nl sa; |
| 2183 |
int fd; |
| 2184 |
int rc = -1; |
| 2185 |
|
| 2186 |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
| 2187 |
if (fd < 0) |
| 2188 |
return -1; |
| 2189 |
|
| 2190 |
memset(&req, 0, sizeof(req)); |
| 2191 |
req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req.req)); |
| 2192 |
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; |
| 2193 |
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 2194 |
req.req.sdiag_family = AF_VSOCK; |
| 2195 |
req.req.sdiag_protocol = 0; |
| 2196 |
req.req.vdiag_states = ~0U; |
| 2197 |
|
| 2198 |
memset(&sa, 0, sizeof(sa)); |
| 2199 |
sa.nl_family = AF_NETLINK; |
| 2200 |
sa.nl_pid = 0; |
| 2201 |
|
| 2202 |
if (sendto(fd, &req, req.nlh.nlmsg_len, 0, |
| 2203 |
(struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 2204 |
goto out; |
| 2205 |
rc = parse_vsock_diag_messages(m, fd); |
| 2206 |
out: |
| 2207 |
close(fd); |
| 2208 |
return rc; |
| 2209 |
} |
| 2210 |
#else |
| 2211 |
/* |
| 2212 |
* parse_vsock_diag - unsupported-build stub for VSOCK sock_diag path. |
| 2213 |
* @m: unused model pointer. |
| 2214 |
* |
| 2215 |
* Returns -1 and sets errno=EOPNOTSUPP. |
| 2216 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2217 |
* procfs/netns state; it does not change kernel configuration. |
| 2218 |
*/ |
| 2219 |
static int parse_vsock_diag(struct model *m) |
| 2220 |
{ |
| 2221 |
(void)m; |
| 2222 |
errno = EOPNOTSUPP; |
| 2223 |
return -1; |
| 2224 |
} |
| 2225 |
#endif |
| 2226 |
|
| 2227 |
/* |
| 2228 |
* parse_diag_messages - consume inet sock_diag replies for @proto/@af. |
| 2229 |
* @m: model receiving parsed endpoint mappings. |
| 2230 |
* @fd: open NETLINK_SOCK_DIAG socket with pending responses. |
| 2231 |
* @proto: requested protocol (SCTP/DCCP). |
| 2232 |
* @af: requested address family (AF_INET/AF_INET6). |
| 2233 |
* |
| 2234 |
* Returns 0 on NLMSG_DONE, or -1 on malformed/error netlink messages. |
| 2235 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2236 |
* procfs/netns state; it does not change kernel configuration. |
| 2237 |
*/ |
| 2238 |
static int parse_diag_messages(struct model *m, int fd, int proto, int af) |
| 2239 |
{ |
| 2240 |
char buf[8192]; |
| 2241 |
ssize_t len; |
| 2242 |
|
| 2243 |
while (1) { |
| 2244 |
len = recv(fd, buf, sizeof(buf), 0); |
| 2245 |
if (len < 0) { |
| 2246 |
if (errno == EINTR) |
| 2247 |
continue; |
| 2248 |
return -1; |
| 2249 |
} |
| 2250 |
if (len == 0) |
| 2251 |
return -1; |
| 2252 |
|
| 2253 |
diag_dbg("recv proto=%d af=%d len=%zd", proto, af, len); |
| 2254 |
|
| 2255 |
struct nlmsghdr *nlh; |
| 2256 |
unsigned int rem; |
| 2257 |
|
| 2258 |
if (len > UINT_MAX) |
| 2259 |
return -1; |
| 2260 |
rem = (unsigned int)len; |
| 2261 |
|
| 2262 |
for (nlh = (struct nlmsghdr *)buf; |
| 2263 |
NLMSG_OK(nlh, rem); |
| 2264 |
nlh = NLMSG_NEXT(nlh, rem)) { |
| 2265 |
struct inet_diag_msg *r; |
| 2266 |
char addr[INET6_ADDRSTRLEN]; |
| 2267 |
unsigned int port; |
| 2268 |
struct inode_proc *ip; |
| 2269 |
|
| 2270 |
if (nlh->nlmsg_type == NLMSG_DONE) |
| 2271 |
return 0; |
| 2272 |
if (nlh->nlmsg_type == NLMSG_ERROR) { |
| 2273 |
struct nlmsgerr *e; |
| 2274 |
|
| 2275 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*e))) |
| 2276 |
return -1; |
| 2277 |
e = NLMSG_DATA(nlh); |
| 2278 |
if (e->error == 0) |
| 2279 |
continue; |
| 2280 |
errno = -e->error; |
| 2281 |
diag_dbg("error proto=%d af=%d err=%d", proto, af, |
| 2282 |
-e->error); |
| 2283 |
return -1; |
| 2284 |
} |
| 2285 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*r))) |
| 2286 |
continue; |
| 2287 |
|
| 2288 |
r = NLMSG_DATA(nlh); |
| 2289 |
ip = lookup_inode(m, r->idiag_inode); |
| 2290 |
if (!ip) |
| 2291 |
continue; |
| 2292 |
port = ntohs(r->id.idiag_sport); |
| 2293 |
if (!port) |
| 2294 |
continue; |
| 2295 |
|
| 2296 |
if (af == AF_INET) { |
| 2297 |
if (!inet_ntop(AF_INET, r->id.idiag_src, addr, |
| 2298 |
sizeof(addr))) |
| 2299 |
continue; |
| 2300 |
} else { |
| 2301 |
if (!inet_ntop(AF_INET6, r->id.idiag_src, addr, |
| 2302 |
sizeof(addr))) |
| 2303 |
continue; |
| 2304 |
} |
| 2305 |
endpoint_to_ifaces(m, |
| 2306 |
proto == IPPROTO_SCTP ? "sctp" : "dccp", |
| 2307 |
af, addr, port, ip->reuseport, ip); |
| 2308 |
} |
| 2309 |
} |
| 2310 |
} |
| 2311 |
|
| 2312 |
/* |
| 2313 |
* parse_diag_for_proto_af - issue one inet sock_diag listener dump request. |
| 2314 |
* @m: model receiving parsed endpoint mappings. |
| 2315 |
* @proto: protocol selector for the request. |
| 2316 |
* @af: address family selector for the request. |
| 2317 |
* |
| 2318 |
* Best-effort helper; failures are tolerated by callers. |
| 2319 |
* Returns no value. |
| 2320 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2321 |
* procfs/netns state; it does not change kernel configuration. |
| 2322 |
*/ |
| 2323 |
static void parse_diag_for_proto_af(struct model *m, int proto, int af) |
| 2324 |
{ |
| 2325 |
struct { |
| 2326 |
struct nlmsghdr nlh; |
| 2327 |
struct inet_diag_req_v2 req; |
| 2328 |
} req; |
| 2329 |
struct sockaddr_nl sa; |
| 2330 |
int fd; |
| 2331 |
|
| 2332 |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
| 2333 |
if (fd < 0) |
| 2334 |
return; |
| 2335 |
|
| 2336 |
memset(&req, 0, sizeof(req)); |
| 2337 |
req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req.req)); |
| 2338 |
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; |
| 2339 |
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 2340 |
req.req.sdiag_family = af; |
| 2341 |
req.req.sdiag_protocol = proto; |
| 2342 |
req.req.idiag_states = 1U << TCP_LISTEN; |
| 2343 |
|
| 2344 |
memset(&sa, 0, sizeof(sa)); |
| 2345 |
sa.nl_family = AF_NETLINK; |
| 2346 |
sa.nl_pid = 0; |
| 2347 |
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 2348 |
goto out; |
| 2349 |
|
| 2350 |
diag_dbg("send proto=%d af=%d len=%u", proto, af, |
| 2351 |
req.nlh.nlmsg_len); |
| 2352 |
if (send(fd, &req, req.nlh.nlmsg_len, 0) < 0) |
| 2353 |
goto out; |
| 2354 |
parse_diag_messages(m, fd, proto, af); |
| 2355 |
out: |
| 2356 |
close(fd); |
| 2357 |
} |
| 2358 |
|
| 2359 |
/* |
| 2360 |
* parse_diag_listeners - collect SCTP/DCCP listeners via sock_diag. |
| 2361 |
* @m: model receiving discovered listener endpoints. |
| 2362 |
* |
| 2363 |
* Returns no value. |
| 2364 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2365 |
* procfs/netns state; it does not change kernel configuration. |
| 2366 |
*/ |
| 2367 |
static void parse_diag_listeners(struct model *m) |
| 2368 |
{ |
| 2369 |
parse_diag_for_proto_af(m, IPPROTO_SCTP, AF_INET); |
| 2370 |
parse_diag_for_proto_af(m, IPPROTO_SCTP, AF_INET6); |
| 2371 |
parse_diag_for_proto_af(m, IPPROTO_DCCP, AF_INET); |
| 2372 |
parse_diag_for_proto_af(m, IPPROTO_DCCP, AF_INET6); |
| 2373 |
} |
| 2374 |
|
| 2375 |
/* |
| 2376 |
* collect_endpoints - gather all endpoint classes into @m. |
| 2377 |
* @m: model receiving inet, diag, packet, and vsock endpoint mappings. |
| 2378 |
* |
| 2379 |
* Data source is current netns procfs/netlink visibility. |
| 2380 |
* Returns no value. |
| 2381 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2382 |
* procfs/netns state; it does not change kernel configuration. |
| 2383 |
*/ |
| 2384 |
static void collect_endpoints(struct model *m) |
| 2385 |
{ |
| 2386 |
parse_inet_file(m, "/proc/net/tcp", "tcp", AF_INET); |
| 2387 |
parse_inet_file(m, "/proc/net/tcp6", "tcp6", AF_INET6); |
| 2388 |
parse_inet_file(m, "/proc/net/udp", "udp", AF_INET); |
| 2389 |
parse_inet_file(m, "/proc/net/udp6", "udp6", AF_INET6); |
| 2390 |
parse_inet_file(m, "/proc/net/udplite", "udplite", AF_INET); |
| 2391 |
parse_inet_file(m, "/proc/net/udplite6", "udplite6", AF_INET6); |
| 2392 |
parse_inet_file(m, "/proc/net/raw", "raw", AF_INET); |
| 2393 |
parse_inet_file(m, "/proc/net/raw6", "raw6", AF_INET6); |
| 2394 |
parse_diag_listeners(m); |
| 2395 |
parse_packet_file(m); |
| 2396 |
if (parse_vsock_diag(m) < 0) { |
| 2397 |
diag_dbg("vsock diag unavailable (%s), falling back to /proc", |
| 2398 |
strerror(errno)); |
| 2399 |
parse_vsock_file(m); |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
/* |
| 2404 |
* wrap_to - choose a safe wrap index for one output line. |
| 2405 |
* @text: source string being wrapped. |
| 2406 |
* @from: starting offset in @text. |
| 2407 |
* @limit: maximum columns to consume from @from. |
| 2408 |
* |
| 2409 |
* Returns next index to continue rendering from. |
| 2410 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2411 |
* procfs/netns state; it does not change kernel configuration. |
| 2412 |
*/ |
| 2413 |
enum color_state { |
| 2414 |
COLOR_STATE_NONE, |
| 2415 |
COLOR_STATE_ORANGE, |
| 2416 |
COLOR_STATE_YELLOW, |
| 2417 |
COLOR_STATE_GREEN, |
| 2418 |
}; |
| 2419 |
|
| 2420 |
/* |
| 2421 |
* color_state_code - map parser color state to ANSI start sequence. |
| 2422 |
* @st: tracked color state carried across wrapped lines. |
| 2423 |
* |
| 2424 |
* Returns SGR color code for @st, or NULL for default color. |
| 2425 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2426 |
* procfs/netns state; it does not change kernel configuration. |
| 2427 |
*/ |
| 2428 |
static const char *color_state_code(enum color_state st) |
| 2429 |
{ |
| 2430 |
switch (st) { |
| 2431 |
case COLOR_STATE_ORANGE: |
| 2432 |
return COLOR_ORANGE; |
| 2433 |
case COLOR_STATE_YELLOW: |
| 2434 |
return COLOR_YELLOW; |
| 2435 |
case COLOR_STATE_GREEN: |
| 2436 |
return COLOR_GREEN; |
| 2437 |
default: |
| 2438 |
return NULL; |
| 2439 |
} |
| 2440 |
} |
| 2441 |
|
| 2442 |
/* |
| 2443 |
* skip_ansi_sgr - advance index past one ANSI SGR escape sequence. |
| 2444 |
* @text: source string potentially containing SGR escapes. |
| 2445 |
* @i: current index, expected at ESC byte when sequence starts. |
| 2446 |
* |
| 2447 |
* Returns new index after sequence (or unchanged when not at SGR). |
| 2448 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2449 |
* procfs/netns state; it does not change kernel configuration. |
| 2450 |
*/ |
| 2451 |
static int skip_ansi_sgr(const char *text, int i) |
| 2452 |
{ |
| 2453 |
if (text[i] != '\033' || text[i + 1] != '[') |
| 2454 |
return i; |
| 2455 |
i += 2; |
| 2456 |
while (text[i] && text[i] != 'm') |
| 2457 |
i++; |
| 2458 |
if (text[i] == 'm') |
| 2459 |
i++; |
| 2460 |
return i; |
| 2461 |
} |
| 2462 |
|
| 2463 |
/* |
| 2464 |
* scan_color_state - track active color state across wrapped text spans. |
| 2465 |
* @text: source string segment being scanned. |
| 2466 |
* @from: start index (inclusive) of rendered segment. |
| 2467 |
* @to: end index (exclusive) of rendered segment. |
| 2468 |
* @st: incoming color state before scanning @text[@from:@to]. |
| 2469 |
* |
| 2470 |
* Returns resulting color state after processing embedded SGR escapes. |
| 2471 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2472 |
* procfs/netns state; it does not change kernel configuration. |
| 2473 |
*/ |
| 2474 |
static enum color_state scan_color_state(const char *text, int from, int to, |
| 2475 |
enum color_state st) |
| 2476 |
{ |
| 2477 |
int i; |
| 2478 |
|
| 2479 |
for (i = from; i < to && text[i]; ) { |
| 2480 |
if (text[i] == '\033' && text[i + 1] == '[') { |
| 2481 |
if (strncmp(text + i, COLOR_ORANGE, strlen(COLOR_ORANGE)) == 0) { |
| 2482 |
st = COLOR_STATE_ORANGE; |
| 2483 |
i += strlen(COLOR_ORANGE); |
| 2484 |
continue; |
| 2485 |
} |
| 2486 |
if (strncmp(text + i, COLOR_YELLOW, strlen(COLOR_YELLOW)) == 0) { |
| 2487 |
st = COLOR_STATE_YELLOW; |
| 2488 |
i += strlen(COLOR_YELLOW); |
| 2489 |
continue; |
| 2490 |
} |
| 2491 |
if (strncmp(text + i, COLOR_GREEN, strlen(COLOR_GREEN)) == 0) { |
| 2492 |
st = COLOR_STATE_GREEN; |
| 2493 |
i += strlen(COLOR_GREEN); |
| 2494 |
continue; |
| 2495 |
} |
| 2496 |
if (strncmp(text + i, COLOR_RESET, strlen(COLOR_RESET)) == 0) { |
| 2497 |
st = COLOR_STATE_NONE; |
| 2498 |
i += strlen(COLOR_RESET); |
| 2499 |
continue; |
| 2500 |
} |
| 2501 |
i = skip_ansi_sgr(text, i); |
| 2502 |
continue; |
| 2503 |
} |
| 2504 |
i++; |
| 2505 |
} |
| 2506 |
return st; |
| 2507 |
} |
| 2508 |
|
| 2509 |
static int wrap_to(const char *text, int from, int limit) |
| 2510 |
{ |
| 2511 |
int i; |
| 2512 |
int vis = 0; |
| 2513 |
int space = -1; |
| 2514 |
|
| 2515 |
for (i = from; text[i] && vis < limit; ) { |
| 2516 |
if (text[i] == '\033' && text[i + 1] == '[') { |
| 2517 |
i = skip_ansi_sgr(text, i); |
| 2518 |
continue; |
| 2519 |
} |
| 2520 |
if (text[i] == ' ') |
| 2521 |
space = i; |
| 2522 |
i++; |
| 2523 |
vis++; |
| 2524 |
} |
| 2525 |
if (!text[i]) |
| 2526 |
return i; |
| 2527 |
if (space > from) |
| 2528 |
return space; |
| 2529 |
if (i == from) |
| 2530 |
return from + 1; |
| 2531 |
return i; |
| 2532 |
} |
| 2533 |
|
| 2534 |
/* |
| 2535 |
* print_tree_node - render one tree node line (with wrapping) to stdout. |
| 2536 |
* @prefix: precomputed branch prefix glyphs. |
| 2537 |
* @is_last: non-zero when this node is the last child. |
| 2538 |
* @txt: node text to render. |
| 2539 |
* @width: target display width used for wrapping. |
| 2540 |
* |
| 2541 |
* Returns no value. |
| 2542 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2543 |
* procfs/netns state; it does not change kernel configuration. |
| 2544 |
*/ |
| 2545 |
static void print_tree_node(const char *prefix, int is_last, const char *txt, |
| 2546 |
int width) |
| 2547 |
{ |
| 2548 |
char head[512]; |
| 2549 |
char cont[512]; |
| 2550 |
int pos = 0; |
| 2551 |
int first = 1; |
| 2552 |
enum color_state st = COLOR_STATE_NONE; |
| 2553 |
|
| 2554 |
snprintf(head, sizeof(head), "%s%s", prefix, |
| 2555 |
is_last ? "└─ " : "├─ "); |
| 2556 |
snprintf(cont, sizeof(cont), "%s%s", prefix, |
| 2557 |
is_last ? " " : "│ "); |
| 2558 |
while (1) { |
| 2559 |
const char *lead = first ? head : cont; |
| 2560 |
int lead_len = strlen(lead); |
| 2561 |
int avail = width - lead_len; |
| 2562 |
int to; |
| 2563 |
const char *code; |
| 2564 |
|
| 2565 |
if (avail < 10) |
| 2566 |
avail = 10; |
| 2567 |
if (!txt[pos]) { |
| 2568 |
printf("%s\n", lead); |
| 2569 |
return; |
| 2570 |
} |
| 2571 |
to = wrap_to(txt, pos, avail); |
| 2572 |
printf("%s", lead); |
| 2573 |
code = color_state_code(st); |
| 2574 |
if (!first && code) |
| 2575 |
fputs(code, stdout); |
| 2576 |
printf("%.*s", to - pos, txt + pos); |
| 2577 |
st = scan_color_state(txt, pos, to, st); |
| 2578 |
if (st != COLOR_STATE_NONE) |
| 2579 |
fputs(COLOR_RESET, stdout); |
| 2580 |
putchar('\n'); |
| 2581 |
while (txt[to] == ' ') |
| 2582 |
to++; |
| 2583 |
pos = to; |
| 2584 |
first = 0; |
| 2585 |
if (!txt[pos]) |
| 2586 |
return; |
| 2587 |
} |
| 2588 |
} |
| 2589 |
|
| 2590 |
/* |
| 2591 |
* build_child_prefix - extend tree prefix glyphs for child nodes. |
| 2592 |
* @dst: output buffer receiving generated prefix text. |
| 2593 |
* @dst_sz: size of @dst in bytes. |
| 2594 |
* @prefix: parent prefix string. |
| 2595 |
* @parent_is_last: non-zero when parent is the last sibling. |
| 2596 |
* |
| 2597 |
* Returns no value. |
| 2598 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2599 |
* procfs/netns state; it does not change kernel configuration. |
| 2600 |
*/ |
| 2601 |
static void build_child_prefix(char *dst, size_t dst_sz, const char *prefix, |
| 2602 |
int parent_is_last) |
| 2603 |
{ |
| 2604 |
snprintf(dst, dst_sz, "%s%s", prefix, |
| 2605 |
parent_is_last ? " " : "│ "); |
| 2606 |
} |
| 2607 |
|
| 2608 |
/* |
| 2609 |
* endpoint_cmp - qsort comparator for stable endpoint grouping. |
| 2610 |
* @a: pointer to first endpoint element. |
| 2611 |
* @b: pointer to second endpoint element. |
| 2612 |
* |
| 2613 |
* Sort order is plane, interface name, interface address, then label. |
| 2614 |
* Returns negative/zero/positive qsort ordering result. |
| 2615 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2616 |
* procfs/netns state; it does not change kernel configuration. |
| 2617 |
*/ |
| 2618 |
static int endpoint_cmp(const void *a, const void *b) |
| 2619 |
{ |
| 2620 |
const struct endpoint *ea = a, *eb = b; |
| 2621 |
/* |
| 2622 |
* Sort by plane for single-pass render grouping, then interface/protocol, |
| 2623 |
* then bind (wildcards first), then port, then ifaddr/label for stability. |
| 2624 |
*/ |
| 2625 |
if (ea->plane != eb->plane) |
| 2626 |
return ea->plane - eb->plane; |
| 2627 |
if (strcmp(ea->ifname, eb->ifname) != 0) |
| 2628 |
return strcmp(ea->ifname, eb->ifname); |
| 2629 |
if (strcmp(ea->proto, eb->proto) != 0) |
| 2630 |
return strcmp(ea->proto, eb->proto); |
| 2631 |
if (bind_sort_cmp(ea->bind, eb->bind) != 0) |
| 2632 |
return bind_sort_cmp(ea->bind, eb->bind); |
| 2633 |
if (ea->port != eb->port) |
| 2634 |
return ea->port < eb->port ? -1 : 1; |
| 2635 |
if (strcmp(ea->ifaddr, eb->ifaddr) != 0) |
| 2636 |
return strcmp(ea->ifaddr, eb->ifaddr); |
| 2637 |
return strcmp(ea->label, eb->label); |
| 2638 |
} |
| 2639 |
|
| 2640 |
|
| 2641 |
/* |
| 2642 |
* format_bind_node - normalize bind text for tree display. |
| 2643 |
* @dst: output buffer receiving display text. |
| 2644 |
* @dst_sz: size of @dst in bytes. |
| 2645 |
* @bind: endpoint bind address to format. |
| 2646 |
* |
| 2647 |
* Returns no value. |
| 2648 |
* Side effects/assumptions: Writes formatted bind label into @dst. |
| 2649 |
*/ |
| 2650 |
static void format_bind_node(char *dst, size_t dst_sz, const char *bind) |
| 2651 |
{ |
| 2652 |
if (strcmp(bind, "0.0.0.0") == 0 || strcmp(bind, "::") == 0) |
| 2653 |
snprintf(dst, dst_sz, "*"); |
| 2654 |
else if (strchr(bind, ':')) |
| 2655 |
snprintf(dst, dst_sz, "[%s]", bind); |
| 2656 |
else |
| 2657 |
snprintf(dst, dst_sz, "%s", bind); |
| 2658 |
} |
| 2659 |
|
| 2660 |
/* |
| 2661 |
* bind_sort_cmp - compare bind addresses with wildcard-first ordering. |
| 2662 |
* @a: first bind address. |
| 2663 |
* @b: second bind address. |
| 2664 |
* |
| 2665 |
* Returns negative/zero/positive ordering suitable for qsort tie-breaks. |
| 2666 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2667 |
* procfs/netns state; it does not change kernel configuration. |
| 2668 |
*/ |
| 2669 |
static int bind_sort_cmp(const char *a, const char *b) |
| 2670 |
{ |
| 2671 |
int a_star = strcmp(a, "0.0.0.0") == 0 || strcmp(a, "::") == 0; |
| 2672 |
int b_star = strcmp(b, "0.0.0.0") == 0 || strcmp(b, "::") == 0; |
| 2673 |
|
| 2674 |
if (a_star != b_star) |
| 2675 |
return a_star ? -1 : 1; |
| 2676 |
return strcmp(a, b); |
| 2677 |
} |
| 2678 |
|
| 2679 |
/* |
| 2680 |
* render_tree_process_details - emit one process subtree under an endpoint. |
| 2681 |
* @prefix: parent tree prefix for process node. |
| 2682 |
* @is_last: non-zero when process node is final sibling. |
| 2683 |
* @p: process metadata to render. |
| 2684 |
* @e: endpoint context supplying endpoint-derived flags. |
| 2685 |
* @width: display width used for wrapping rendered nodes. |
| 2686 |
* |
| 2687 |
* Returns no value. |
| 2688 |
* Side effects/assumptions: Writes formatted tree output to stdout. |
| 2689 |
*/ |
| 2690 |
static void render_tree_process_details(const char *prefix, |
| 2691 |
int is_last, |
| 2692 |
struct process_info *p, |
| 2693 |
const struct endpoint *e, |
| 2694 |
int width) |
| 2695 |
{ |
| 2696 |
char pfx_child[256], pfx_def[256], pfx_flags[256]; |
| 2697 |
char line[4096]; |
| 2698 |
const char *def_nodes[8]; |
| 2699 |
char def_buf[8][512]; |
| 2700 |
size_t def_n = 0; |
| 2701 |
size_t ai; |
| 2702 |
unsigned int flags = 0; |
| 2703 |
enum cap_severity priv_sev = caps_worst_severity(p->caps); |
| 2704 |
|
| 2705 |
snprintf(line, sizeof(line), "%s (pid=%d uid=%d%s%s%s%s)", |
| 2706 |
p->comm, p->pid, p->uid, |
| 2707 |
p->exe ? " exe=" : "", |
| 2708 |
p->exe ? p->exe : "", |
| 2709 |
p->unit ? " unit=" : "", |
| 2710 |
p->unit ? p->unit : ""); |
| 2711 |
print_tree_node(prefix, is_last, line, width); |
| 2712 |
build_child_prefix(pfx_child, sizeof(pfx_child), prefix, is_last); |
| 2713 |
|
| 2714 |
if (strcmp(p->caps, "(full)") == 0 || strcmp(p->caps, "(none)") == 0) { |
| 2715 |
const char *c = strcmp(p->caps, "(full)") == 0 ? COLOR_ORANGE : |
| 2716 |
COLOR_GREEN; |
| 2717 |
if (use_color) |
| 2718 |
snprintf(line, sizeof(line), "caps: %s%s%s", c, p->caps, |
| 2719 |
COLOR_RESET); |
| 2720 |
else |
| 2721 |
snprintf(line, sizeof(line), "caps: %s", p->caps); |
| 2722 |
} else { |
| 2723 |
char capsbuf[3072] = ""; |
| 2724 |
char *tmp = xstrdup(p->caps); |
| 2725 |
char *save = NULL; |
| 2726 |
char *tok; |
| 2727 |
int first = 1; |
| 2728 |
|
| 2729 |
if (!tmp) { |
| 2730 |
snprintf(line, sizeof(line), "caps: %s", p->caps); |
| 2731 |
} else { |
| 2732 |
for (tok = strtok_r(tmp, ",", &save); tok; |
| 2733 |
tok = strtok_r(NULL, ",", &save)) { |
| 2734 |
char part[256]; |
| 2735 |
char *t = tok; |
| 2736 |
enum cap_severity sev; |
| 2737 |
while (*t == ' ') |
| 2738 |
t++; |
| 2739 |
sev = cap_name_severity(t); |
| 2740 |
if (use_color && sev_color(sev)) |
| 2741 |
snprintf(part, sizeof(part), "%s%s%s", |
| 2742 |
sev_color(sev), t, COLOR_RESET); |
| 2743 |
else |
| 2744 |
snprintf(part, sizeof(part), "%s", t); |
| 2745 |
if (!first) |
| 2746 |
strncat(capsbuf, ", ", sizeof(capsbuf)-strlen(capsbuf)-1); |
| 2747 |
strncat(capsbuf, part, sizeof(capsbuf)-strlen(capsbuf)-1); |
| 2748 |
first = 0; |
| 2749 |
} |
| 2750 |
free(tmp); |
| 2751 |
if (p->ambient_present) { |
| 2752 |
if (use_color) |
| 2753 |
strncat(capsbuf, " [\033[38;5;208mambient-present\033[0m]", |
| 2754 |
sizeof(capsbuf)-strlen(capsbuf)-1); |
| 2755 |
else |
| 2756 |
strncat(capsbuf, " [ambient-present]", |
| 2757 |
sizeof(capsbuf)-strlen(capsbuf)-1); |
| 2758 |
} |
| 2759 |
if (p->open_ended_bounding) { |
| 2760 |
if (use_color && strcmp(p->caps, "(full)") != 0 && |
| 2761 |
caps_contains_token(p->caps, "setpcap")) |
| 2762 |
strncat(capsbuf, |
| 2763 |
" [\033[38;5;208mopen-ended-bounding\033[0m]", |
| 2764 |
sizeof(capsbuf) - strlen(capsbuf) - 1); |
| 2765 |
else |
| 2766 |
strncat(capsbuf, " [open-ended-bounding]", |
| 2767 |
sizeof(capsbuf) - strlen(capsbuf) - 1); |
| 2768 |
} |
| 2769 |
snprintf(line, sizeof(line), "caps: %s", capsbuf); |
| 2770 |
} |
| 2771 |
} |
| 2772 |
print_tree_node(pfx_child, 0, line, width); |
| 2773 |
|
| 2774 |
if (p->ambient_caps) { |
| 2775 |
if (use_color) |
| 2776 |
snprintf(line, sizeof(line), "ambient: %s%s%s", COLOR_ORANGE, |
| 2777 |
p->ambient_caps, COLOR_RESET); |
| 2778 |
else |
| 2779 |
snprintf(line, sizeof(line), "ambient: %s", p->ambient_caps); |
| 2780 |
print_tree_node(pfx_child, 0, line, width); |
| 2781 |
} |
| 2782 |
|
| 2783 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), |
| 2784 |
DEFENSES_RUNS_AS_KEY ": %s%s%s", |
| 2785 |
strcmp(p->defenses.runs_as_nonroot, "yes") == 0 && use_color ? COLOR_GREEN : |
| 2786 |
(strcmp(p->defenses.runs_as_nonroot, "no") == 0 && use_color ? COLOR_YELLOW : ""), |
| 2787 |
p->defenses.runs_as_nonroot, |
| 2788 |
use_color && (strcmp(p->defenses.runs_as_nonroot, "yes") == 0 || |
| 2789 |
strcmp(p->defenses.runs_as_nonroot, "no") == 0) ? COLOR_RESET : ""); |
| 2790 |
def_nodes[def_n] = def_buf[def_n]; |
| 2791 |
def_n++; |
| 2792 |
|
| 2793 |
if (strcmp(p->defenses.no_new_privs, "yes") == 0 && use_color) |
| 2794 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "no_new_privs: %syes%s", |
| 2795 |
COLOR_GREEN, COLOR_RESET); |
| 2796 |
else if (strcmp(p->defenses.no_new_privs, "no") == 0 && use_color) |
| 2797 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "no_new_privs: %sno%s", |
| 2798 |
COLOR_YELLOW, COLOR_RESET); |
| 2799 |
else |
| 2800 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "no_new_privs: %s", |
| 2801 |
p->defenses.no_new_privs); |
| 2802 |
def_nodes[def_n] = def_buf[def_n]; |
| 2803 |
def_n++; |
| 2804 |
|
| 2805 |
if ((strcmp(p->defenses.seccomp, "filter") == 0 || |
| 2806 |
strcmp(p->defenses.seccomp, "strict") == 0) && use_color) |
| 2807 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "seccomp: %s%s%s", |
| 2808 |
COLOR_GREEN, p->defenses.seccomp, COLOR_RESET); |
| 2809 |
else if (strcmp(p->defenses.seccomp, "disabled") == 0 && use_color) |
| 2810 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "seccomp: %sdisabled%s", |
| 2811 |
COLOR_YELLOW, COLOR_RESET); |
| 2812 |
else |
| 2813 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "seccomp: %s", |
| 2814 |
p->defenses.seccomp); |
| 2815 |
def_nodes[def_n] = def_buf[def_n]; |
| 2816 |
def_n++; |
| 2817 |
|
| 2818 |
if (p->defenses.lsm_label) { |
| 2819 |
if (use_color && strstr(p->defenses.lsm_label, "unconfined_t")) |
| 2820 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "lsm: %s%s%s", |
| 2821 |
COLOR_ORANGE, p->defenses.lsm_label, COLOR_RESET); |
| 2822 |
else if (use_color && p->defenses.lsm_label[0]) |
| 2823 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "lsm: %s%s%s", |
| 2824 |
COLOR_GREEN, p->defenses.lsm_label, COLOR_RESET); |
| 2825 |
else |
| 2826 |
snprintf(def_buf[def_n], sizeof(def_buf[def_n]), "lsm: %s", |
| 2827 |
p->defenses.lsm_label); |
| 2828 |
def_nodes[def_n] = def_buf[def_n]; |
| 2829 |
def_n++; |
| 2830 |
} |
| 2831 |
print_tree_node(pfx_child, 0, "defenses", width); |
| 2832 |
build_child_prefix(pfx_def, sizeof(pfx_def), pfx_child, 0); |
| 2833 |
for (ai = 0; ai < def_n; ai++) |
| 2834 |
print_tree_node(pfx_def, ai + 1 == def_n, def_nodes[ai], width); |
| 2835 |
|
| 2836 |
if (e->plane == PLANE_VSOCK) { |
| 2837 |
flags |= FLAG_HYPERVISOR_PLANE; |
| 2838 |
if (e->port == 22) |
| 2839 |
flags |= FLAG_SSH_VSOCK_22; |
| 2840 |
} else { |
| 2841 |
if (e->wildcard_bind) |
| 2842 |
flags |= FLAG_WILDCARD_BIND; |
| 2843 |
if (e->reuseport) |
| 2844 |
flags |= FLAG_REUSEPORT; |
| 2845 |
} |
| 2846 |
if (p->has_privileged_caps) |
| 2847 |
flags |= FLAG_PRIVILEGED_CAPS; |
| 2848 |
|
| 2849 |
print_tree_node(pfx_child, 1, "flags", width); |
| 2850 |
build_child_prefix(pfx_flags, sizeof(pfx_flags), pfx_child, 1); |
| 2851 |
print_flag_nodes(pfx_flags, width, flags, priv_sev); |
| 2852 |
} |
| 2853 |
|
| 2854 |
|
| 2855 |
/* |
| 2856 |
* render_json_process - emit one process object inside endpoint JSON arrays. |
| 2857 |
* @p: process metadata record to serialize. |
| 2858 |
* @ep: endpoint context contributing endpoint-related flags. |
| 2859 |
* @indent: indentation prefix already prepared by caller. |
| 2860 |
* |
| 2861 |
* Returns no value. |
| 2862 |
* Side effects/assumptions: Writes JSON fragments to stdout. |
| 2863 |
*/ |
| 2864 |
static void render_json_process(struct process_info *p, |
| 2865 |
const struct endpoint *ep, |
| 2866 |
const char *indent) |
| 2867 |
{ |
| 2868 |
int firstf = 1; |
| 2869 |
|
| 2870 |
printf("%s{\"comm\": ", indent); |
| 2871 |
json_escape(p->comm); |
| 2872 |
if (p->exe) { |
| 2873 |
printf(", \"exe\": "); |
| 2874 |
json_escape(p->exe); |
| 2875 |
} |
| 2876 |
printf(", \"pid\": %d, \"uid\": %d", p->pid, p->uid); |
| 2877 |
if (p->unit) { |
| 2878 |
printf(", \"unit\": "); |
| 2879 |
json_escape(p->unit); |
| 2880 |
} |
| 2881 |
printf(", \"caps\": "); |
| 2882 |
json_escape(p->caps); |
| 2883 |
printf(", \"ambient_present\": %s", |
| 2884 |
p->ambient_present ? "true" : "false"); |
| 2885 |
if (p->ambient_caps) { |
| 2886 |
printf(", \"ambient_caps\": "); |
| 2887 |
json_escape(p->ambient_caps); |
| 2888 |
} |
| 2889 |
printf(", \"open_ended_bounding\": %s", |
| 2890 |
p->open_ended_bounding ? "true" : "false"); |
| 2891 |
printf(", \"defenses\": {\"" DEFENSES_RUNS_AS_KEY "\": "); |
| 2892 |
json_escape(p->defenses.runs_as_nonroot); |
| 2893 |
printf(", \"no_new_privs\": "); |
| 2894 |
json_escape(p->defenses.no_new_privs); |
| 2895 |
printf(", \"seccomp\": "); |
| 2896 |
json_escape(p->defenses.seccomp); |
| 2897 |
if (p->defenses.lsm_label) { |
| 2898 |
printf(", \"lsm\": "); |
| 2899 |
json_escape(p->defenses.lsm_label); |
| 2900 |
} |
| 2901 |
printf("}, \"flags\": ["); |
| 2902 |
|
| 2903 |
if (ep->plane == PLANE_VSOCK) { |
| 2904 |
json_escape("hypervisor-plane"); |
| 2905 |
firstf = 0; |
| 2906 |
if (ep->port == 22) { |
| 2907 |
printf(", "); |
| 2908 |
json_escape("ssh-on-vsock-port-22"); |
| 2909 |
} |
| 2910 |
} else { |
| 2911 |
if (ep->wildcard_bind) { |
| 2912 |
if (!firstf) |
| 2913 |
printf(", "); |
| 2914 |
json_escape("wildcard-bind"); |
| 2915 |
firstf = 0; |
| 2916 |
} |
| 2917 |
if (ep->reuseport) { |
| 2918 |
if (!firstf) |
| 2919 |
printf(", "); |
| 2920 |
json_escape("reuseport"); |
| 2921 |
firstf = 0; |
| 2922 |
} |
| 2923 |
} |
| 2924 |
if (p->has_privileged_caps) { |
| 2925 |
if (!firstf) |
| 2926 |
printf(", "); |
| 2927 |
json_escape("privileged-caps"); |
| 2928 |
} |
| 2929 |
printf("]}"); |
| 2930 |
} |
| 2931 |
|
| 2932 |
|
| 2933 |
/* |
| 2934 |
* render_tree - print human-readable advanced report as a tree. |
| 2935 |
* @m: model to render; endpoint array is sorted in place before printing. |
| 2936 |
* |
| 2937 |
* Returns no value. |
| 2938 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 2939 |
* procfs/netns state; it does not change kernel configuration. |
| 2940 |
*/ |
| 2941 |
static void render_tree(struct model *m) |
| 2942 |
{ |
| 2943 |
size_t i; |
| 2944 |
int planes[PLANE_COUNT]; |
| 2945 |
size_t plane_n = 0; |
| 2946 |
int width = get_width(); |
| 2947 |
|
| 2948 |
if (m->eps_n > 1) |
| 2949 |
qsort(m->eps, m->eps_n, sizeof(struct endpoint), endpoint_cmp); |
| 2950 |
puts("netcap --advanced"); |
| 2951 |
|
| 2952 |
for (i = 0; i < PLANE_COUNT; i++) { |
| 2953 |
size_t j; |
| 2954 |
for (j = 0; j < m->eps_n; j++) { |
| 2955 |
if (m->eps[j].plane == (enum plane_kind)i) { |
| 2956 |
planes[plane_n++] = i; |
| 2957 |
break; |
| 2958 |
} |
| 2959 |
} |
| 2960 |
} |
| 2961 |
|
| 2962 |
for (i = 0; i < plane_n; i++) { |
| 2963 |
/* Tree level: plane (INET external/loopback, packet, vsock). */ |
| 2964 |
int plane = planes[i]; |
| 2965 |
int plane_last = (i + 1 == plane_n); |
| 2966 |
char pfx_plane[256] = ""; |
| 2967 |
char pfx_iface[256]; |
| 2968 |
const char *plane_name = plane == PLANE_INET_EXTERNAL ? |
| 2969 |
"INET (external)" : |
| 2970 |
plane == PLANE_INET_LOOPBACK ? "INET (loopback)" : |
| 2971 |
plane == PLANE_PACKET ? PLANE_PACKET_NAME : "VSOCK"; |
| 2972 |
size_t j = 0; |
| 2973 |
|
| 2974 |
print_tree_node(pfx_plane, plane_last, plane_name, width); |
| 2975 |
build_child_prefix(pfx_iface, sizeof(pfx_iface), pfx_plane, |
| 2976 |
plane_last); |
| 2977 |
if (plane == PLANE_VSOCK) { |
| 2978 |
/* VSOCK has no iface/address hierarchy, so print endpoint-first. */ |
| 2979 |
for (j = 0; j < m->eps_n; j++) { |
| 2980 |
struct endpoint *e = &m->eps[j]; |
| 2981 |
char pfx_proc[256]; |
| 2982 |
int ep_last; |
| 2983 |
size_t k; |
| 2984 |
|
| 2985 |
if (e->plane != PLANE_VSOCK) |
| 2986 |
continue; |
| 2987 |
ep_last = 1; |
| 2988 |
for (size_t n = j + 1; n < m->eps_n; n++) { |
| 2989 |
if (m->eps[n].plane == PLANE_VSOCK) { |
| 2990 |
ep_last = 0; |
| 2991 |
break; |
| 2992 |
} |
| 2993 |
} |
| 2994 |
print_tree_node(pfx_iface, ep_last, e->label, width); |
| 2995 |
build_child_prefix(pfx_proc, sizeof(pfx_proc), pfx_iface, |
| 2996 |
ep_last); |
| 2997 |
for (k = 0; k < e->procs_n; k++) { |
| 2998 |
struct process_info *p = e->procs[k]; |
| 2999 |
int proc_last = (k + 1 == e->procs_n); |
| 3000 |
|
| 3001 |
render_tree_process_details(pfx_proc, proc_last, p, e, |
| 3002 |
width); |
| 3003 |
} |
| 3004 |
} |
| 3005 |
continue; |
| 3006 |
} |
| 3007 |
|
| 3008 |
while (j < m->eps_n) { |
| 3009 |
/* Tree level: interface grouping within the current plane. */ |
| 3010 |
size_t iface_start, iface_end; |
| 3011 |
char iface_line[160]; |
| 3012 |
char pfx_iface_child[256]; |
| 3013 |
int iface_last; |
| 3014 |
|
| 3015 |
if (m->eps[j].plane != (enum plane_kind)plane) { |
| 3016 |
j++; |
| 3017 |
continue; |
| 3018 |
} |
| 3019 |
iface_start = j; |
| 3020 |
iface_end = j + 1; |
| 3021 |
while (iface_end < m->eps_n && |
| 3022 |
m->eps[iface_end].plane == (enum plane_kind)plane && |
| 3023 |
strcmp(m->eps[iface_end].ifname, |
| 3024 |
m->eps[iface_start].ifname) == 0) |
| 3025 |
iface_end++; |
| 3026 |
iface_last = 1; |
| 3027 |
if (iface_end < m->eps_n && |
| 3028 |
m->eps[iface_end].plane == (enum plane_kind)plane) |
| 3029 |
iface_last = 0; |
| 3030 |
|
| 3031 |
snprintf(iface_line, sizeof(iface_line), "%s", |
| 3032 |
m->eps[iface_start].ifname); |
| 3033 |
print_tree_node(pfx_iface, iface_last, iface_line, width); |
| 3034 |
build_child_prefix(pfx_iface_child, sizeof(pfx_iface_child), |
| 3035 |
pfx_iface, iface_last); |
| 3036 |
|
| 3037 |
{ |
| 3038 |
char pfx_proto_root[256]; |
| 3039 |
|
| 3040 |
snprintf(pfx_proto_root, sizeof(pfx_proto_root), "%s", |
| 3041 |
pfx_iface_child); |
| 3042 |
|
| 3043 |
for (j = iface_start; j < iface_end; ) { |
| 3044 |
/* Tree level: protocol grouping on this interface. */ |
| 3045 |
size_t proto_start = j, proto_end; |
| 3046 |
char pfx_bind[256]; |
| 3047 |
int proto_last; |
| 3048 |
|
| 3049 |
proto_end = j + 1; |
| 3050 |
while (proto_end < iface_end && |
| 3051 |
strcmp(m->eps[proto_end].proto, |
| 3052 |
m->eps[proto_start].proto) == 0) |
| 3053 |
proto_end++; |
| 3054 |
proto_last = (proto_end == iface_end); |
| 3055 |
|
| 3056 |
/* Highlight higher-risk raw/packet protocol families. */ |
| 3057 |
if (use_color && (strcmp(m->eps[proto_start].proto, "raw") == 0 || |
| 3058 |
strcmp(m->eps[proto_start].proto, "raw6") == 0 || |
| 3059 |
strcmp(m->eps[proto_start].proto, "packet") == 0)) { |
| 3060 |
char pbuf[64]; |
| 3061 |
|
| 3062 |
snprintf(pbuf, sizeof(pbuf), "%s%s%s", COLOR_YELLOW, |
| 3063 |
m->eps[proto_start].proto, COLOR_RESET); |
| 3064 |
print_tree_node(pfx_proto_root, proto_last, pbuf, width); |
| 3065 |
} else { |
| 3066 |
print_tree_node(pfx_proto_root, proto_last, |
| 3067 |
m->eps[proto_start].proto, width); |
| 3068 |
} |
| 3069 |
build_child_prefix(pfx_bind, sizeof(pfx_bind), |
| 3070 |
pfx_proto_root, proto_last); |
| 3071 |
|
| 3072 |
{ |
| 3073 |
size_t bi = proto_start; |
| 3074 |
|
| 3075 |
while (bi < proto_end) { |
| 3076 |
/* Tree level: bind address (wildcard/specific). */ |
| 3077 |
size_t bind_start = bi; |
| 3078 |
size_t bind_end; |
| 3079 |
char bind_line[128], pfx_port[256]; |
| 3080 |
int bind_last; |
| 3081 |
|
| 3082 |
bind_end = bi + 1; |
| 3083 |
while (bind_end < proto_end && |
| 3084 |
strcmp(m->eps[bind_end].bind, |
| 3085 |
m->eps[bind_start].bind) == 0) |
| 3086 |
bind_end++; |
| 3087 |
bind_last = (bind_end == proto_end); |
| 3088 |
|
| 3089 |
format_bind_node(bind_line, sizeof(bind_line), |
| 3090 |
m->eps[bind_start].bind); |
| 3091 |
print_tree_node(pfx_bind, bind_last, bind_line, width); |
| 3092 |
build_child_prefix(pfx_port, sizeof(pfx_port), |
| 3093 |
pfx_bind, bind_last); |
| 3094 |
|
| 3095 |
for (bi = bind_start; bi < bind_end; ) { |
| 3096 |
/* Tree level: port number under each bind. */ |
| 3097 |
size_t port_start = bi; |
| 3098 |
size_t port_end; |
| 3099 |
char pfx_proc[256], port_line[64]; |
| 3100 |
int port_last; |
| 3101 |
size_t k; |
| 3102 |
struct pidset seen; |
| 3103 |
|
| 3104 |
port_end = bi + 1; |
| 3105 |
while (port_end < bind_end && |
| 3106 |
m->eps[port_end].port == |
| 3107 |
m->eps[port_start].port) |
| 3108 |
port_end++; |
| 3109 |
port_last = (port_end == bind_end); |
| 3110 |
|
| 3111 |
snprintf(port_line, sizeof(port_line), "%u", |
| 3112 |
m->eps[port_start].port); |
| 3113 |
print_tree_node(pfx_port, port_last, |
| 3114 |
port_line, width); |
| 3115 |
build_child_prefix(pfx_proc, sizeof(pfx_proc), |
| 3116 |
pfx_port, port_last); |
| 3117 |
|
| 3118 |
if (pidset_init(&seen)) { |
| 3119 |
bi = port_end; |
| 3120 |
continue; |
| 3121 |
} |
| 3122 |
|
| 3123 |
for (k = port_start; k < port_end; k++) { |
| 3124 |
/* Tree level: process details under the current port. */ |
| 3125 |
struct endpoint *e = &m->eps[k]; |
| 3126 |
size_t pi; |
| 3127 |
|
| 3128 |
for (pi = 0; pi < e->procs_n; pi++) { |
| 3129 |
int seen_rc; |
| 3130 |
struct process_info *p = e->procs[pi]; |
| 3131 |
int proc_last; |
| 3132 |
|
| 3133 |
/* |
| 3134 |
* Deduplicate processes that appear under multiple |
| 3135 |
* endpoints sharing this grouped port. |
| 3136 |
*/ |
| 3137 |
seen_rc = pidset_test_and_add(&seen, p->pid); |
| 3138 |
if (seen_rc) |
| 3139 |
continue; |
| 3140 |
|
| 3141 |
proc_last = (k + 1 == port_end) && |
| 3142 |
(pi + 1 == e->procs_n); |
| 3143 |
|
| 3144 |
render_tree_process_details(pfx_proc, |
| 3145 |
proc_last, p, e, width); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
|
| 3149 |
pidset_free(&seen); |
| 3150 |
bi = port_end; |
| 3151 |
} |
| 3152 |
} |
| 3153 |
} |
| 3154 |
j = proto_end; |
| 3155 |
} |
| 3156 |
} |
| 3157 |
j = iface_end; |
| 3158 |
} |
| 3159 |
} |
| 3160 |
} |
| 3161 |
|
| 3162 |
/* |
| 3163 |
* render_interfaces_text - print interface inventory in human-readable form. |
| 3164 |
* @m: model whose interface inventory is rendered. |
| 3165 |
* |
| 3166 |
* Returns no value. |
| 3167 |
*/ |
| 3168 |
static void render_interfaces_text(struct model *m) |
| 3169 |
{ |
| 3170 |
size_t i; |
| 3171 |
|
| 3172 |
sort_interfaces(m); |
| 3173 |
puts("netcap --advanced --list-interfaces"); |
| 3174 |
for (i = 0; i < m->ifaces_n; i++) |
| 3175 |
printf("%s\n", m->ifaces[i].name); |
| 3176 |
} |
| 3177 |
|
| 3178 |
/* |
| 3179 |
* json_escape - write @s as a quoted JSON string to stdout. |
| 3180 |
* @s: UTF-8/text string to emit as one JSON string literal. |
| 3181 |
* |
| 3182 |
* Control characters and quotes are escaped; caller handles separators. |
| 3183 |
* Returns no value. |
| 3184 |
* Side effects/assumptions: Writes to stdout and may read procfs/netns |
| 3185 |
* state indirectly via caller-supplied model-derived strings. |
| 3186 |
*/ |
| 3187 |
static void json_escape(const char *s) |
| 3188 |
{ |
| 3189 |
const unsigned char *p = (const unsigned char *)s; |
| 3190 |
putchar('"'); |
| 3191 |
for (; *p; p++) { |
| 3192 |
if (*p == '"') |
| 3193 |
fputs("\\\"", stdout); |
| 3194 |
else if (*p == '\\') |
| 3195 |
fputs("\\\\", stdout); |
| 3196 |
else if (*p < 0x20) |
| 3197 |
printf("\\u%04x", *p); |
| 3198 |
else |
| 3199 |
putchar(*p); |
| 3200 |
} |
| 3201 |
putchar('"'); |
| 3202 |
} |
| 3203 |
|
| 3204 |
/* |
| 3205 |
* render_json - print machine-readable advanced report JSON to stdout. |
| 3206 |
* @m: model to render; endpoint array is sorted in place before printing. |
| 3207 |
* |
| 3208 |
* Returns no value. |
| 3209 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 3210 |
* procfs/netns state; it does not change kernel configuration. |
| 3211 |
*/ |
| 3212 |
static void render_json(struct model *m) |
| 3213 |
{ |
| 3214 |
size_t i, j, k, l; |
| 3215 |
|
| 3216 |
if (m->eps_n > 1) |
| 3217 |
qsort(m->eps, m->eps_n, sizeof(struct endpoint), endpoint_cmp); |
| 3218 |
puts("{"); |
| 3219 |
puts(" \"schema_version\": 1,"); |
| 3220 |
puts(" \"planes\": ["); |
| 3221 |
for (i = 0; i < PLANE_COUNT; i++) { |
| 3222 |
const char *pname = i == PLANE_INET_EXTERNAL ? "INET" : |
| 3223 |
i == PLANE_INET_LOOPBACK ? "INET" : |
| 3224 |
i == PLANE_PACKET ? PLANE_PACKET_NAME : "VSOCK"; |
| 3225 |
const char *scope = i == PLANE_INET_EXTERNAL ? "external" : |
| 3226 |
i == PLANE_INET_LOOPBACK ? "loopback" : NULL; |
| 3227 |
struct strset seen_ifaces = { 0 }; |
| 3228 |
int first_vsock = 1; |
| 3229 |
int first_if = 1; |
| 3230 |
|
| 3231 |
printf(" {\"name\": "); |
| 3232 |
json_escape(pname); |
| 3233 |
if (scope) { |
| 3234 |
printf(", \"scope\": "); |
| 3235 |
json_escape(scope); |
| 3236 |
} |
| 3237 |
if (i == PLANE_VSOCK) |
| 3238 |
puts(", \"endpoints\": ["); |
| 3239 |
else |
| 3240 |
puts(", \"ifaces\": ["); |
| 3241 |
|
| 3242 |
if (i == PLANE_VSOCK) { |
| 3243 |
for (j = 0; j < m->eps_n; j++) { |
| 3244 |
struct endpoint *ep = &m->eps[j]; |
| 3245 |
|
| 3246 |
if (ep->plane != PLANE_VSOCK) |
| 3247 |
continue; |
| 3248 |
if (!first_vsock) |
| 3249 |
puts(","); |
| 3250 |
first_vsock = 0; |
| 3251 |
printf(" {\"label\": "); |
| 3252 |
json_escape(ep->label); |
| 3253 |
printf(", \"vsock_type\": "); |
| 3254 |
json_escape(ep->proto); |
| 3255 |
printf(", \"cid\": "); |
| 3256 |
if (ep->vsock_cid == VMADDR_CID_ANY) |
| 3257 |
json_escape("ANY"); |
| 3258 |
else |
| 3259 |
printf("%u", ep->vsock_cid); |
| 3260 |
printf(", \"port\": %u", ep->port); |
| 3261 |
puts(", \"processes\": ["); |
| 3262 |
for (size_t pi = 0; pi < ep->procs_n; pi++) { |
| 3263 |
struct process_info *p = ep->procs[pi]; |
| 3264 |
|
| 3265 |
render_json_process(p, ep, " "); |
| 3266 |
if (pi + 1 != ep->procs_n) |
| 3267 |
puts(","); |
| 3268 |
else |
| 3269 |
putchar('\n'); |
| 3270 |
} |
| 3271 |
puts(" ]}"); |
| 3272 |
} |
| 3273 |
puts(" ]}"); |
| 3274 |
if (i + 1 != PLANE_COUNT) |
| 3275 |
puts(","); |
| 3276 |
strset_free(&seen_ifaces); |
| 3277 |
continue; |
| 3278 |
} |
| 3279 |
|
| 3280 |
for (j = 0; j < m->eps_n; j++) { |
| 3281 |
struct strset seen_addrs = { 0 }; |
| 3282 |
const char *ifn = m->eps[j].ifname; |
| 3283 |
int first_addr = 1; |
| 3284 |
int seen; |
| 3285 |
|
| 3286 |
if (m->eps[j].plane != (enum plane_kind)i) |
| 3287 |
continue; |
| 3288 |
seen = strset_add(&seen_ifaces, ifn); |
| 3289 |
if (seen < 0) { |
| 3290 |
strset_free(&seen_addrs); |
| 3291 |
continue; |
| 3292 |
} |
| 3293 |
if (seen == 0) |
| 3294 |
continue; |
| 3295 |
if (!first_if) |
| 3296 |
puts(","); |
| 3297 |
first_if = 0; |
| 3298 |
printf(" {\"name\": "); |
| 3299 |
json_escape(ifn); |
| 3300 |
puts(", \"addrs\": ["); |
| 3301 |
|
| 3302 |
for (k = 0; k < m->eps_n; k++) { |
| 3303 |
const char *ifa = m->eps[k].ifaddr; |
| 3304 |
int first_ep = 1; |
| 3305 |
int addr_seen; |
| 3306 |
if (m->eps[k].plane != (enum plane_kind)i || |
| 3307 |
strcmp(m->eps[k].ifname, ifn) != 0) |
| 3308 |
continue; |
| 3309 |
addr_seen = strset_add(&seen_addrs, ifa); |
| 3310 |
if (addr_seen < 0) |
| 3311 |
break; |
| 3312 |
if (addr_seen == 0) |
| 3313 |
continue; |
| 3314 |
if (!first_addr) |
| 3315 |
puts(","); |
| 3316 |
first_addr = 0; |
| 3317 |
printf(" {\"addr\": "); |
| 3318 |
json_escape(ifa); |
| 3319 |
puts(", \"endpoints\": ["); |
| 3320 |
for (l = 0; l < m->eps_n; l++) { |
| 3321 |
struct endpoint *ep = &m->eps[l]; |
| 3322 |
if (ep->plane != (enum plane_kind)i || |
| 3323 |
strcmp(ep->ifname, ifn) != 0 || |
| 3324 |
strcmp(ep->ifaddr, ifa) != 0) |
| 3325 |
continue; |
| 3326 |
if (!first_ep) |
| 3327 |
puts(","); |
| 3328 |
first_ep = 0; |
| 3329 |
printf(" {\"label\": "); |
| 3330 |
json_escape(ep->label); |
| 3331 |
printf(", \"proto\": "); |
| 3332 |
json_escape(ep->proto); |
| 3333 |
printf(", \"bind\": "); |
| 3334 |
json_escape(ep->bind); |
| 3335 |
printf(", \"port\": %u", ep->port); |
| 3336 |
puts(", \"processes\": ["); |
| 3337 |
for (size_t pi = 0; pi < ep->procs_n; pi++) { |
| 3338 |
struct process_info *p = ep->procs[pi]; |
| 3339 |
|
| 3340 |
render_json_process(p, ep, " "); |
| 3341 |
if (pi + 1 != ep->procs_n) |
| 3342 |
puts(","); |
| 3343 |
else |
| 3344 |
putchar('\n'); |
| 3345 |
} |
| 3346 |
puts(" ]}"); |
| 3347 |
} |
| 3348 |
puts(" ]}"); |
| 3349 |
} |
| 3350 |
puts(" ]}"); |
| 3351 |
strset_free(&seen_addrs); |
| 3352 |
} |
| 3353 |
puts(" ]}"); |
| 3354 |
strset_free(&seen_ifaces); |
| 3355 |
if (i + 1 != PLANE_COUNT) |
| 3356 |
puts(","); |
| 3357 |
} |
| 3358 |
puts(" ]"); |
| 3359 |
puts("}"); |
| 3360 |
} |
| 3361 |
|
| 3362 |
/* |
| 3363 |
* render_interfaces_json - print interface inventory as JSON. |
| 3364 |
* @m: model whose interface inventory is rendered. |
| 3365 |
* |
| 3366 |
* Returns no value. |
| 3367 |
*/ |
| 3368 |
static void render_interfaces_json(struct model *m) |
| 3369 |
{ |
| 3370 |
size_t i; |
| 3371 |
|
| 3372 |
sort_interfaces(m); |
| 3373 |
puts("{"); |
| 3374 |
puts(" \"schema_version\": 1,"); |
| 3375 |
puts(" \"interfaces\": ["); |
| 3376 |
for (i = 0; i < m->ifaces_n; i++) { |
| 3377 |
printf(" "); |
| 3378 |
json_escape(m->ifaces[i].name); |
| 3379 |
printf("%s\n", i + 1 == m->ifaces_n ? "" : ","); |
| 3380 |
} |
| 3381 |
puts(" ]"); |
| 3382 |
puts("}"); |
| 3383 |
} |
| 3384 |
|
| 3385 |
/* |
| 3386 |
* free_endpoint - free one endpoint and all owned dynamic fields. |
| 3387 |
* @e: endpoint entry pointer, or NULL. |
| 3388 |
* |
| 3389 |
* Returns no value. |
| 3390 |
*/ |
| 3391 |
static void free_endpoint(struct endpoint *e) |
| 3392 |
{ |
| 3393 |
if (!e) |
| 3394 |
return; |
| 3395 |
free(e->proto); |
| 3396 |
free(e->bind); |
| 3397 |
free(e->label); |
| 3398 |
free(e->ifname); |
| 3399 |
free(e->ifaddr); |
| 3400 |
free(e->procs); |
| 3401 |
} |
| 3402 |
|
| 3403 |
/* |
| 3404 |
* filter_model_interface - keep only one named interface in @m. |
| 3405 |
* @m: model container to prune in place. |
| 3406 |
* @ifname: interface name to retain. |
| 3407 |
* |
| 3408 |
* Returns no value. |
| 3409 |
*/ |
| 3410 |
static void filter_model_interface(struct model *m, const char *ifname) |
| 3411 |
{ |
| 3412 |
size_t i, out; |
| 3413 |
|
| 3414 |
if (!m || !ifname) |
| 3415 |
return; |
| 3416 |
|
| 3417 |
for (i = 0, out = 0; i < m->ifaces_n; i++) { |
| 3418 |
if (strcmp(m->ifaces[i].name, ifname) == 0) { |
| 3419 |
if (out != i) |
| 3420 |
m->ifaces[out] = m->ifaces[i]; |
| 3421 |
out++; |
| 3422 |
continue; |
| 3423 |
} |
| 3424 |
free(m->ifaces[i].name); |
| 3425 |
for (size_t j = 0; j < m->ifaces[i].addrs_n; j++) |
| 3426 |
free(m->ifaces[i].addrs[j].addr); |
| 3427 |
free(m->ifaces[i].addrs); |
| 3428 |
} |
| 3429 |
m->ifaces_n = out; |
| 3430 |
|
| 3431 |
for (i = 0, out = 0; i < m->eps_n; i++) { |
| 3432 |
if (strcmp(m->eps[i].ifname, ifname) == 0) { |
| 3433 |
if (out != i) |
| 3434 |
m->eps[out] = m->eps[i]; |
| 3435 |
out++; |
| 3436 |
continue; |
| 3437 |
} |
| 3438 |
free_endpoint(&m->eps[i]); |
| 3439 |
} |
| 3440 |
m->eps_n = out; |
| 3441 |
} |
| 3442 |
|
| 3443 |
/* |
| 3444 |
* free_process - free one process_info and all owned dynamic fields. |
| 3445 |
* @p: process entry pointer, or NULL. |
| 3446 |
* |
| 3447 |
* Returns no value. |
| 3448 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 3449 |
* procfs/netns state; it does not change kernel configuration. |
| 3450 |
*/ |
| 3451 |
static void free_process(struct process_info *p) |
| 3452 |
{ |
| 3453 |
if (!p) |
| 3454 |
return; |
| 3455 |
free(p->comm); |
| 3456 |
free(p->exe); |
| 3457 |
free(p->unit); |
| 3458 |
free(p->caps); |
| 3459 |
free(p->ambient_caps); |
| 3460 |
free(p->defenses.runs_as_nonroot); |
| 3461 |
free(p->defenses.no_new_privs); |
| 3462 |
free(p->defenses.seccomp); |
| 3463 |
free(p->defenses.lsm_label); |
| 3464 |
free(p); |
| 3465 |
} |
| 3466 |
|
| 3467 |
/* |
| 3468 |
* free_model - free all heap allocations referenced by @m. |
| 3469 |
* @m: model container whose internal arrays/strings are released. |
| 3470 |
* |
| 3471 |
* Returns no value. |
| 3472 |
* Side effects/assumptions: Operates on in-memory data and may read |
| 3473 |
* procfs/netns state; it does not change kernel configuration. |
| 3474 |
*/ |
| 3475 |
static void free_model(struct model *m) |
| 3476 |
{ |
| 3477 |
size_t i, j; |
| 3478 |
for (i = 0; i < m->ifaces_n; i++) { |
| 3479 |
free(m->ifaces[i].name); |
| 3480 |
for (j = 0; j < m->ifaces[i].addrs_n; j++) |
| 3481 |
free(m->ifaces[i].addrs[j].addr); |
| 3482 |
free(m->ifaces[i].addrs); |
| 3483 |
} |
| 3484 |
free(m->ifaces); |
| 3485 |
for (i = 0; i < m->procs_n; i++) |
| 3486 |
free_process(m->procs[i]); |
| 3487 |
free(m->procs); |
| 3488 |
for (i = 0; i < m->inode_n; i++) |
| 3489 |
free(m->inode_map[i].procs); |
| 3490 |
free(m->inode_map); |
| 3491 |
free(m->inode_slots); |
| 3492 |
for (i = 0; i < m->eps_n; i++) |
| 3493 |
free_endpoint(&m->eps[i]); |
| 3494 |
free(m->eps); |
| 3495 |
} |
| 3496 |
|
| 3497 |
/* |
| 3498 |
* netcap_advanced_main - entry point for "netcap --advanced" mode. |
| 3499 |
* @opts: parsed options; must be non-NULL and have @advanced set. |
| 3500 |
* |
| 3501 |
* Returns 0 after rendering advanced output, or 1 when advanced mode |
| 3502 |
* is not requested. |
| 3503 |
* Side effects/assumptions: Reads procfs/netlink in the current network |
| 3504 |
* namespace, prints to stdout/stderr, and root is typically needed for a |
| 3505 |
* fuller process-to-socket ownership mapping. |
| 3506 |
*/ |
| 3507 |
int netcap_advanced_main(const struct netcap_opts *opts) |
| 3508 |
{ |
| 3509 |
struct model m; |
| 3510 |
|
| 3511 |
if (!opts || !opts->advanced) |
| 3512 |
return 1; |
| 3513 |
memset(&m, 0, sizeof(m)); |
| 3514 |
if (collect_interfaces(&m) != 0) { |
| 3515 |
fprintf(stderr, "warning: failed to enumerate interfaces\n"); |
| 3516 |
} |
| 3517 |
if (opts->interface) |
| 3518 |
filter_model_interface(&m, opts->interface); |
| 3519 |
if (opts->list_interfaces) { |
| 3520 |
if (opts->json) |
| 3521 |
render_interfaces_json(&m); |
| 3522 |
else |
| 3523 |
render_interfaces_text(&m); |
| 3524 |
free_model(&m); |
| 3525 |
return 0; |
| 3526 |
} |
| 3527 |
collect_proc_inodes(&m); |
| 3528 |
collect_endpoints(&m); |
| 3529 |
if (opts->interface) |
| 3530 |
filter_model_interface(&m, opts->interface); |
| 3531 |
use_color = !opts->json && !opts->no_color && isatty(STDOUT_FILENO); |
| 3532 |
if (opts->json) |
| 3533 |
render_json(&m); |
| 3534 |
else |
| 3535 |
render_tree(&m); |
| 3536 |
free_model(&m); |
| 3537 |
return 0; |
| 3538 |
} |