| 1 |
/* |
| 2 |
* netcap.c - A program that lists network apps with capabilities |
| 3 |
* Copyright (c) 2009-10,2012,2020 Red Hat Inc. |
| 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 |
* The /proc/net/tcp|udp|raw parsing code was borrowed from netstat.c |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
#include <arpa/inet.h> |
| 29 |
#ifdef HAVE_NETCAP_ADVANCED |
| 30 |
#include <linux/inet_diag.h> |
| 31 |
#include <linux/netlink.h> |
| 32 |
#include <linux/sock_diag.h> |
| 33 |
#include <linux/vm_sockets.h> |
| 34 |
#ifdef HAVE_LINUX_VM_SOCKETS_DIAG_H |
| 35 |
#include <linux/vm_sockets_diag.h> |
| 36 |
#endif |
| 37 |
#endif |
| 38 |
#include <limits.h> |
| 39 |
#include <netinet/in.h> |
| 40 |
#include <netinet/tcp.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdio_ext.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <errno.h> |
| 45 |
#include <string.h> |
| 46 |
#include <dirent.h> |
| 47 |
#include <fcntl.h> |
| 48 |
#include <pwd.h> |
| 49 |
#include "cap-ng.h" |
| 50 |
#include "proc-llist.h" |
| 51 |
#include "netcap-advanced.h" |
| 52 |
#include "proc-sanitize.h" |
| 53 |
|
| 54 |
static llist l; |
| 55 |
static int perm_warn = 0, header = 0, last_uid = -1; |
| 56 |
static char *tacct = NULL; |
| 57 |
|
| 58 |
static void usage(void) |
| 59 |
{ |
| 60 |
fprintf(stderr, "usage: netcap [--advanced " |
| 61 |
"[--interface IFACE] [--list-interfaces] [--json] " |
| 62 |
"[--no-color]]\n"); |
| 63 |
exit(1); |
| 64 |
} |
| 65 |
|
| 66 |
static int collect_process_info(void) |
| 67 |
{ |
| 68 |
DIR *d, *f; |
| 69 |
struct dirent *ent; |
| 70 |
d = opendir("/proc"); |
| 71 |
if (d == NULL) { |
| 72 |
fprintf(stderr, "Can't open /proc: %s\n", strerror(errno)); |
| 73 |
return 1; |
| 74 |
} |
| 75 |
while (( ent = readdir(d) )) { |
| 76 |
FILE *sf; |
| 77 |
int pid, ppid; |
| 78 |
capng_results_t caps; |
| 79 |
char buf[100]; |
| 80 |
char *safe_cmd; |
| 81 |
char *tmp, cmd[16], state; |
| 82 |
char *text = NULL, *bounds = NULL, *ambient = NULL; |
| 83 |
int fd, len, euid = -1; |
| 84 |
|
| 85 |
// Skip non-process dir entries |
| 86 |
if(*ent->d_name<'0' || *ent->d_name>'9') |
| 87 |
continue; |
| 88 |
errno = 0; |
| 89 |
pid = strtol(ent->d_name, NULL, 10); |
| 90 |
if (errno) |
| 91 |
continue; |
| 92 |
|
| 93 |
// Parse up the stat file for the proc |
| 94 |
snprintf(buf, sizeof(buf), "/proc/%d/stat", pid); |
| 95 |
fd = open(buf, O_RDONLY|O_CLOEXEC, 0); |
| 96 |
if (fd < 0) |
| 97 |
continue; |
| 98 |
len = read(fd, buf, sizeof buf - 1); |
| 99 |
close(fd); |
| 100 |
if (len < 40) |
| 101 |
continue; |
| 102 |
buf[len] = 0; |
| 103 |
tmp = strrchr(buf, ')'); |
| 104 |
if (tmp) |
| 105 |
*tmp = 0; |
| 106 |
else |
| 107 |
continue; |
| 108 |
memset(cmd, 0, sizeof(cmd)); |
| 109 |
if (sscanf(buf, "%d (%15c", &ppid, cmd) != 2) |
| 110 |
continue; |
| 111 |
if (sscanf(tmp+2, "%c %d", &state, &ppid) != 2) |
| 112 |
continue; |
| 113 |
|
| 114 |
// Skip kthreads |
| 115 |
if (pid == 2 || ppid == 2) |
| 116 |
continue; |
| 117 |
|
| 118 |
// now get the capabilities |
| 119 |
capng_clear(CAPNG_SELECT_ALL); |
| 120 |
capng_setpid(pid); |
| 121 |
if (capng_get_caps_process()) |
| 122 |
continue; |
| 123 |
caps = capng_have_capabilities(CAPNG_SELECT_CAPS); |
| 124 |
if (caps <= CAPNG_NONE) |
| 125 |
continue; |
| 126 |
if (caps == CAPNG_FULL) { |
| 127 |
text = strdup("full"); |
| 128 |
if (!text) { |
| 129 |
fprintf(stderr, "Out of memory\n"); |
| 130 |
continue; |
| 131 |
} |
| 132 |
} else { |
| 133 |
text = capng_print_caps_text(CAPNG_PRINT_BUFFER, |
| 134 |
CAPNG_PERMITTED); |
| 135 |
if (text == NULL) { |
| 136 |
fprintf(stderr, "Out of memory doing pid %d\n", |
| 137 |
pid); |
| 138 |
continue; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Get the effective uid |
| 143 |
snprintf(buf, sizeof(buf), "/proc/%d/status", pid); |
| 144 |
sf = fopen(buf, "rte"); |
| 145 |
if (sf == NULL) |
| 146 |
euid = 0; |
| 147 |
else { |
| 148 |
int line = 0; |
| 149 |
__fsetlocking(sf, FSETLOCKING_BYCALLER); |
| 150 |
while (fgets(buf, sizeof(buf), sf)) { |
| 151 |
if (line == 0) { |
| 152 |
line++; |
| 153 |
continue; |
| 154 |
} |
| 155 |
if (memcmp(buf, "Uid:", 4) == 0) { |
| 156 |
int id; |
| 157 |
if (sscanf(buf, "Uid: %d %d", |
| 158 |
&id, &euid) == 2) |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
fclose(sf); |
| 163 |
if (euid == -1) |
| 164 |
euid = 0; |
| 165 |
} |
| 166 |
|
| 167 |
caps = capng_have_capabilities(CAPNG_SELECT_AMBIENT); |
| 168 |
if (caps > CAPNG_NONE) |
| 169 |
ambient = strdup("@"); |
| 170 |
else |
| 171 |
ambient = strdup(""); |
| 172 |
if (!ambient) { |
| 173 |
fprintf(stderr, "Out of memory\n"); |
| 174 |
free(text); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// Now record the bounding set information |
| 179 |
caps = capng_have_capabilities(CAPNG_SELECT_BOUNDS); |
| 180 |
if (caps > CAPNG_NONE) |
| 181 |
bounds = strdup("+"); |
| 182 |
else |
| 183 |
bounds = strdup(""); |
| 184 |
if (!bounds) { |
| 185 |
fprintf(stderr, "Out of memory\n"); |
| 186 |
free(text); |
| 187 |
free(ambient); |
| 188 |
continue; |
| 189 |
} |
| 190 |
|
| 191 |
// Now lets get the inodes each process has open |
| 192 |
snprintf(buf, sizeof(buf), "/proc/%d/fd", pid); |
| 193 |
f = opendir(buf); |
| 194 |
if (f == NULL) { |
| 195 |
if (errno == EACCES) { |
| 196 |
if (perm_warn == 0) { |
| 197 |
fprintf(stderr, |
| 198 |
"You may need to be root to " |
| 199 |
"get a full report\n"); |
| 200 |
perm_warn = 1; |
| 201 |
} |
| 202 |
} else |
| 203 |
fprintf(stderr, "Can't open %s: %s\n", buf, |
| 204 |
strerror(errno)); |
| 205 |
free(text); |
| 206 |
free(bounds); |
| 207 |
free(ambient); |
| 208 |
continue; |
| 209 |
} |
| 210 |
// For each file in the fd dir... |
| 211 |
struct dirent *fd_ent; |
| 212 |
while (( fd_ent = readdir(f) )) { |
| 213 |
char line[256], ln[256], *s, *e; |
| 214 |
unsigned long inode; |
| 215 |
lnode node; |
| 216 |
int llen; |
| 217 |
|
| 218 |
if (fd_ent->d_name[0] == '.') |
| 219 |
continue; |
| 220 |
snprintf(ln, 256, "%s/%s", buf, fd_ent->d_name); |
| 221 |
if ((llen = readlink(ln, line, sizeof(line)-1)) < 0) |
| 222 |
continue; |
| 223 |
line[llen] = 0; |
| 224 |
|
| 225 |
// Only look at the socket entries |
| 226 |
if (memcmp(line, "socket:", 7) == 0) { |
| 227 |
// Type 1 sockets |
| 228 |
s = strchr(line+7, '['); |
| 229 |
if (s == NULL) |
| 230 |
continue; |
| 231 |
s++; |
| 232 |
e = strchr(s, ']'); |
| 233 |
if (e == NULL) |
| 234 |
continue; |
| 235 |
*e = 0; |
| 236 |
} else if (memcmp(line, "[0000]:", 7) == 0) { |
| 237 |
// Type 2 sockets |
| 238 |
s = line + 8; |
| 239 |
} else |
| 240 |
continue; |
| 241 |
errno = 0; |
| 242 |
inode = strtoul(s, NULL, 10); |
| 243 |
if (errno) |
| 244 |
continue; |
| 245 |
safe_cmd = sanitize_untrusted_field(cmd); |
| 246 |
if (!safe_cmd) |
| 247 |
continue; |
| 248 |
node.ppid = ppid; |
| 249 |
node.pid = pid; |
| 250 |
node.uid = euid; |
| 251 |
node.cmd = safe_cmd; |
| 252 |
node.inode = inode; |
| 253 |
node.capabilities = strdup(text); |
| 254 |
node.bounds = strdup(bounds); |
| 255 |
node.ambient = strdup(ambient); |
| 256 |
if (node.cmd && node.capabilities && node.bounds && |
| 257 |
node.ambient) |
| 258 |
// We make one entry for each socket inode |
| 259 |
list_append(&l, &node); |
| 260 |
else { |
| 261 |
free(node.cmd); |
| 262 |
free(node.capabilities); |
| 263 |
free(node.bounds); |
| 264 |
free(node.ambient); |
| 265 |
} |
| 266 |
} |
| 267 |
closedir(f); |
| 268 |
free(text); |
| 269 |
free(bounds); |
| 270 |
free(ambient); |
| 271 |
} |
| 272 |
closedir(d); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
static void report_finding(unsigned int port, const char *type, const char *ifc) |
| 277 |
{ |
| 278 |
struct passwd *p; |
| 279 |
lnode *n = list_get_cur(&l); |
| 280 |
|
| 281 |
// And print out anything with capabilities |
| 282 |
if (header == 0) { |
| 283 |
printf("%-7s %-7s %-16s %-15s %-8s %-15s %s\n", |
| 284 |
"ppid", "pid", "acct", "command", "type", "port", |
| 285 |
"capabilities"); |
| 286 |
header = 1; |
| 287 |
} |
| 288 |
if (n->uid == 0) { |
| 289 |
// Take short cut for this one |
| 290 |
tacct = "root"; |
| 291 |
last_uid = 0; |
| 292 |
} else if (last_uid != (int)n->uid) { |
| 293 |
// Only look up if name changed |
| 294 |
p = getpwuid(n->uid); |
| 295 |
last_uid = n->uid; |
| 296 |
if (p) |
| 297 |
tacct = p->pw_name; |
| 298 |
// If not taking this branch, use last val |
| 299 |
} |
| 300 |
if (tacct) { |
| 301 |
printf("%-7d %-7d %-16s", n->ppid, n->pid, tacct); |
| 302 |
} else |
| 303 |
printf("%-7d %-7d %-16d", n->ppid, n->pid, last_uid); |
| 304 |
printf(" %-15s %-8s", n->cmd, type); |
| 305 |
if (ifc) |
| 306 |
printf(" %-15s", ifc); |
| 307 |
else |
| 308 |
printf(" %-15u", port); |
| 309 |
printf(" %s %s%s\n", n->capabilities, n->ambient, n->bounds); |
| 310 |
} |
| 311 |
|
| 312 |
static void read_net(const char *proc, const char *type, int use_local_port) |
| 313 |
{ |
| 314 |
int line = 0; |
| 315 |
FILE *f; |
| 316 |
char buf[256]; |
| 317 |
unsigned long rxq, txq, time_len, retr, inode; |
| 318 |
unsigned int local_port, rem_port, state, timer_run; |
| 319 |
int d, uid, timeout; |
| 320 |
char rem_addr[128], local_addr[128], more[512]; |
| 321 |
|
| 322 |
f = fopen(proc, "rte"); |
| 323 |
if (f == NULL) { |
| 324 |
if (errno != ENOENT) |
| 325 |
fprintf(stderr, "Can't open %s: %s\n", |
| 326 |
proc, strerror(errno)); |
| 327 |
return; |
| 328 |
} |
| 329 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 330 |
while (fgets(buf, sizeof(buf), f)) { |
| 331 |
if (line == 0) { |
| 332 |
line++; |
| 333 |
continue; |
| 334 |
} |
| 335 |
more[0] = 0; |
| 336 |
if (sscanf(buf, "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X " |
| 337 |
"%lX:%lX %X:%lX %lX %d %d %lu %511s\n", |
| 338 |
&d, local_addr, &local_port, rem_addr, &rem_port, |
| 339 |
&state, &txq, &rxq, &timer_run, &time_len, &retr, |
| 340 |
&uid, &timeout, &inode, more) < 14) |
| 341 |
continue; |
| 342 |
if (list_find_inode(&l, inode)) |
| 343 |
report_finding(use_local_port ? local_port : 0, |
| 344 |
type, NULL); |
| 345 |
} |
| 346 |
fclose(f); |
| 347 |
} |
| 348 |
|
| 349 |
// Caller must have buffer >= 65 bytes |
| 350 |
static void get_interface(unsigned int iface, char *ifc) |
| 351 |
{ |
| 352 |
unsigned int line = 0; |
| 353 |
FILE *f; |
| 354 |
char buf[256], more[256]; |
| 355 |
|
| 356 |
// Terminate the interface in case of error |
| 357 |
*ifc = 0; |
| 358 |
|
| 359 |
// Offset the interface number since header is 2 lines long |
| 360 |
iface += 2; |
| 361 |
|
| 362 |
f = fopen("/proc/net/dev", "rte"); |
| 363 |
if (f == NULL) { |
| 364 |
if (errno != ENOENT) |
| 365 |
fprintf(stderr, "Can't open /proc/net/dev: %s\n", |
| 366 |
strerror(errno)); |
| 367 |
return; |
| 368 |
} |
| 369 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 370 |
while (fgets(buf, sizeof(buf), f)) { |
| 371 |
if (line == iface) { |
| 372 |
char *c; |
| 373 |
char *safe_ifc; |
| 374 |
|
| 375 |
sscanf(buf, "%16s: %255s\n", ifc, more); |
| 376 |
c = strchr(ifc, ':'); |
| 377 |
if (c) |
| 378 |
*c = 0; |
| 379 |
safe_ifc = sanitize_untrusted_field(ifc); |
| 380 |
if (safe_ifc) { |
| 381 |
strncpy(ifc, safe_ifc, 64); |
| 382 |
ifc[64] = '\0'; |
| 383 |
free(safe_ifc); |
| 384 |
} else |
| 385 |
*ifc = 0; |
| 386 |
fclose(f); |
| 387 |
return; |
| 388 |
} |
| 389 |
line++; |
| 390 |
} |
| 391 |
fclose(f); |
| 392 |
} |
| 393 |
|
| 394 |
static void read_packet(void) |
| 395 |
{ |
| 396 |
int line = 0; |
| 397 |
FILE *f; |
| 398 |
char buf[256]; |
| 399 |
unsigned long sk, inode; |
| 400 |
unsigned int ref_cnt, type, proto, iface, r, rmem, uid; |
| 401 |
char more[256], ifc[65]; |
| 402 |
|
| 403 |
f = fopen("/proc/net/packet", "rte"); |
| 404 |
if (f == NULL) { |
| 405 |
if (errno != ENOENT) |
| 406 |
fprintf(stderr, "Can't open /proc/net/packet: %s\n", |
| 407 |
strerror(errno)); |
| 408 |
return; |
| 409 |
} |
| 410 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 411 |
while (fgets(buf, sizeof(buf), f)) { |
| 412 |
if (line == 0) { |
| 413 |
line++; |
| 414 |
continue; |
| 415 |
} |
| 416 |
more[0] = 0; |
| 417 |
if (sscanf(buf, "%lX %u %u %X %u %u %u %u %lu %255s\n", |
| 418 |
&sk, &ref_cnt, &type, &proto, &iface, |
| 419 |
&r, &rmem, &uid, &inode, more) < 9) |
| 420 |
continue; |
| 421 |
get_interface(iface, ifc); |
| 422 |
if (list_find_inode(&l, inode)) |
| 423 |
report_finding(0, "pkt", ifc); |
| 424 |
} |
| 425 |
fclose(f); |
| 426 |
} |
| 427 |
|
| 428 |
#ifdef HAVE_NETCAP_ADVANCED |
| 429 |
static int parse_u32_hex_or_dec(const char *s, unsigned int *out) |
| 430 |
{ |
| 431 |
char *end; |
| 432 |
unsigned long v; |
| 433 |
int base = 10; |
| 434 |
const char *p; |
| 435 |
|
| 436 |
for (p = s; *p; p++) { |
| 437 |
if ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')) { |
| 438 |
base = 16; |
| 439 |
break; |
| 440 |
} |
| 441 |
} |
| 442 |
if (strncmp(s, "0x", 2) == 0 || strncmp(s, "0X", 2) == 0) |
| 443 |
base = 16; |
| 444 |
if (base == 10 && strlen(s) > 3 && s[0] == '0') |
| 445 |
base = 16; |
| 446 |
v = strtoul(s, &end, base); |
| 447 |
if (end == s || *end) |
| 448 |
return -1; |
| 449 |
*out = (unsigned int)v; |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
static int read_diag_messages(int fd, int proto, const char *type) |
| 454 |
{ |
| 455 |
char buf[8192]; |
| 456 |
ssize_t len; |
| 457 |
struct sockaddr_nl nladdr; |
| 458 |
socklen_t nladdr_len; |
| 459 |
|
| 460 |
while (1) { |
| 461 |
nladdr_len = sizeof(nladdr); |
| 462 |
memset(&nladdr, 0, sizeof(nladdr)); |
| 463 |
len = recvfrom(fd, buf, sizeof(buf), 0, |
| 464 |
(struct sockaddr *)&nladdr, &nladdr_len); |
| 465 |
if (len < 0) { |
| 466 |
if (errno == EINTR) |
| 467 |
continue; |
| 468 |
return -1; |
| 469 |
} |
| 470 |
if (len == 0) |
| 471 |
return -1; |
| 472 |
if (nladdr_len < sizeof(nladdr) || nladdr.nl_pid != 0) |
| 473 |
continue; |
| 474 |
|
| 475 |
struct nlmsghdr *nlh; |
| 476 |
unsigned int rem; |
| 477 |
|
| 478 |
if (len > UINT_MAX) |
| 479 |
return -1; |
| 480 |
rem = (unsigned int)len; |
| 481 |
|
| 482 |
for (nlh = (struct nlmsghdr *)buf; |
| 483 |
NLMSG_OK(nlh, rem); |
| 484 |
nlh = NLMSG_NEXT(nlh, rem)) { |
| 485 |
struct inet_diag_msg *r; |
| 486 |
unsigned int port; |
| 487 |
|
| 488 |
if (nlh->nlmsg_type == NLMSG_DONE) |
| 489 |
return 0; |
| 490 |
if (nlh->nlmsg_type == NLMSG_ERROR) { |
| 491 |
struct nlmsgerr *e; |
| 492 |
|
| 493 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*e))) |
| 494 |
return -1; |
| 495 |
e = NLMSG_DATA(nlh); |
| 496 |
if (e->error == 0) |
| 497 |
continue; |
| 498 |
errno = -e->error; |
| 499 |
return -1; |
| 500 |
} |
| 501 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*r))) |
| 502 |
continue; |
| 503 |
|
| 504 |
r = NLMSG_DATA(nlh); |
| 505 |
if (!list_find_inode(&l, r->idiag_inode)) |
| 506 |
continue; |
| 507 |
port = ntohs(r->id.idiag_sport); |
| 508 |
if (!port) |
| 509 |
continue; |
| 510 |
|
| 511 |
if (proto == IPPROTO_SCTP || proto == IPPROTO_DCCP) |
| 512 |
report_finding(port, type, NULL); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
static int read_diag_for_proto_af(int proto, int af, const char *type) |
| 518 |
{ |
| 519 |
struct { |
| 520 |
struct nlmsghdr nlh; |
| 521 |
struct inet_diag_req_v2 req; |
| 522 |
} req; |
| 523 |
struct sockaddr_nl sa; |
| 524 |
int fd; |
| 525 |
int rc = -1; |
| 526 |
|
| 527 |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
| 528 |
if (fd < 0) |
| 529 |
return -1; |
| 530 |
|
| 531 |
memset(&req, 0, sizeof(req)); |
| 532 |
req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req.req)); |
| 533 |
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; |
| 534 |
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 535 |
req.req.sdiag_family = af; |
| 536 |
req.req.sdiag_protocol = proto; |
| 537 |
req.req.idiag_states = 1U << TCP_LISTEN; |
| 538 |
|
| 539 |
memset(&sa, 0, sizeof(sa)); |
| 540 |
sa.nl_family = AF_NETLINK; |
| 541 |
sa.nl_pid = 0; |
| 542 |
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 543 |
goto out; |
| 544 |
|
| 545 |
if (send(fd, &req, req.nlh.nlmsg_len, 0) < 0) |
| 546 |
goto out; |
| 547 |
|
| 548 |
rc = read_diag_messages(fd, proto, type); |
| 549 |
out: |
| 550 |
close(fd); |
| 551 |
return rc; |
| 552 |
} |
| 553 |
|
| 554 |
static void read_diag_listeners(void) |
| 555 |
{ |
| 556 |
int sctp_ok = 0; |
| 557 |
int dccp_ok = 0; |
| 558 |
|
| 559 |
if (read_diag_for_proto_af(IPPROTO_SCTP, AF_INET, "sctp") == 0) |
| 560 |
sctp_ok = 1; |
| 561 |
if (read_diag_for_proto_af(IPPROTO_SCTP, AF_INET6, "sctp") == 0) |
| 562 |
sctp_ok = 1; |
| 563 |
if (read_diag_for_proto_af(IPPROTO_DCCP, AF_INET, "dccp") == 0) |
| 564 |
dccp_ok = 1; |
| 565 |
if (read_diag_for_proto_af(IPPROTO_DCCP, AF_INET6, "dccp") == 0) |
| 566 |
dccp_ok = 1; |
| 567 |
|
| 568 |
if (!dccp_ok) { |
| 569 |
read_net("/proc/net/dccp", "dccp", 1); |
| 570 |
read_net("/proc/net/dccp6", "dccp", 1); |
| 571 |
} |
| 572 |
(void)sctp_ok; |
| 573 |
} |
| 574 |
|
| 575 |
#ifdef HAVE_LINUX_VM_SOCKETS_DIAG_H |
| 576 |
static int read_vsock_diag_messages(int fd) |
| 577 |
{ |
| 578 |
char buf[8192]; |
| 579 |
ssize_t len; |
| 580 |
struct sockaddr_nl nladdr; |
| 581 |
socklen_t nladdr_len; |
| 582 |
|
| 583 |
while (1) { |
| 584 |
nladdr_len = sizeof(nladdr); |
| 585 |
memset(&nladdr, 0, sizeof(nladdr)); |
| 586 |
len = recvfrom(fd, buf, sizeof(buf), 0, |
| 587 |
(struct sockaddr *)&nladdr, &nladdr_len); |
| 588 |
if (len < 0) { |
| 589 |
if (errno == EINTR) |
| 590 |
continue; |
| 591 |
return -1; |
| 592 |
} |
| 593 |
if (len == 0) |
| 594 |
return -1; |
| 595 |
if (nladdr_len < sizeof(nladdr) || nladdr.nl_pid != 0) |
| 596 |
continue; |
| 597 |
|
| 598 |
struct nlmsghdr *nlh; |
| 599 |
unsigned int rem; |
| 600 |
|
| 601 |
if (len > UINT_MAX) |
| 602 |
return -1; |
| 603 |
rem = (unsigned int)len; |
| 604 |
|
| 605 |
for (nlh = (struct nlmsghdr *)buf; |
| 606 |
NLMSG_OK(nlh, rem); |
| 607 |
nlh = NLMSG_NEXT(nlh, rem)) { |
| 608 |
struct vsock_diag_msg *r; |
| 609 |
|
| 610 |
if (nlh->nlmsg_type == NLMSG_DONE) |
| 611 |
return 0; |
| 612 |
if (nlh->nlmsg_type == NLMSG_ERROR) { |
| 613 |
struct nlmsgerr *e; |
| 614 |
|
| 615 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*e))) |
| 616 |
return -1; |
| 617 |
e = NLMSG_DATA(nlh); |
| 618 |
if (e->error == 0) |
| 619 |
continue; |
| 620 |
errno = -e->error; |
| 621 |
return -1; |
| 622 |
} |
| 623 |
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*r))) |
| 624 |
continue; |
| 625 |
|
| 626 |
r = NLMSG_DATA(nlh); |
| 627 |
if (r->vdiag_family != AF_VSOCK) |
| 628 |
continue; |
| 629 |
if (r->vdiag_type != SOCK_STREAM || |
| 630 |
r->vdiag_state != TCP_LISTEN) |
| 631 |
continue; |
| 632 |
if (!list_find_inode(&l, r->vdiag_ino)) |
| 633 |
continue; |
| 634 |
if (r->vdiag_src_port == 0) |
| 635 |
continue; |
| 636 |
|
| 637 |
report_finding(r->vdiag_src_port, "vsock", NULL); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
static int read_vsock_diag(void) |
| 643 |
{ |
| 644 |
struct { |
| 645 |
struct nlmsghdr nlh; |
| 646 |
struct vsock_diag_req req; |
| 647 |
} req; |
| 648 |
struct sockaddr_nl sa; |
| 649 |
int fd; |
| 650 |
int rc = -1; |
| 651 |
|
| 652 |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG); |
| 653 |
if (fd < 0) |
| 654 |
return -1; |
| 655 |
|
| 656 |
memset(&req, 0, sizeof(req)); |
| 657 |
req.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(req.req)); |
| 658 |
req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY; |
| 659 |
req.nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 660 |
req.req.sdiag_family = AF_VSOCK; |
| 661 |
req.req.sdiag_protocol = 0; |
| 662 |
req.req.vdiag_states = ~0U; |
| 663 |
|
| 664 |
memset(&sa, 0, sizeof(sa)); |
| 665 |
sa.nl_family = AF_NETLINK; |
| 666 |
sa.nl_pid = 0; |
| 667 |
|
| 668 |
if (sendto(fd, &req, req.nlh.nlmsg_len, 0, |
| 669 |
(struct sockaddr *)&sa, sizeof(sa)) < 0) |
| 670 |
goto out; |
| 671 |
|
| 672 |
rc = read_vsock_diag_messages(fd); |
| 673 |
out: |
| 674 |
close(fd); |
| 675 |
return rc; |
| 676 |
} |
| 677 |
#else |
| 678 |
static int read_vsock_diag(void) |
| 679 |
{ |
| 680 |
errno = EOPNOTSUPP; |
| 681 |
return -1; |
| 682 |
} |
| 683 |
#endif |
| 684 |
|
| 685 |
static void read_vsock_proc(void) |
| 686 |
{ |
| 687 |
FILE *f; |
| 688 |
char line[512]; |
| 689 |
|
| 690 |
f = fopen("/proc/net/vsock", "rte"); |
| 691 |
if (f == NULL) { |
| 692 |
if (errno != ENOENT) |
| 693 |
fprintf(stderr, "Can't open /proc/net/vsock: %s\n", |
| 694 |
strerror(errno)); |
| 695 |
return; |
| 696 |
} |
| 697 |
__fsetlocking(f, FSETLOCKING_BYCALLER); |
| 698 |
while (fgets(line, sizeof(line), f)) { |
| 699 |
char work[512]; |
| 700 |
char *tok[24]; |
| 701 |
char *save = NULL; |
| 702 |
char *local, *sep, *s; |
| 703 |
int tcnt = 0; |
| 704 |
unsigned long inode; |
| 705 |
unsigned int st, type, cid, port; |
| 706 |
|
| 707 |
if (strstr(line, "Local") || strstr(line, "local") || |
| 708 |
strstr(line, "Num")) |
| 709 |
continue; |
| 710 |
snprintf(work, sizeof(work), "%s", line); |
| 711 |
s = strtok_r(work, " \t\n", &save); |
| 712 |
while (s && tcnt < (int)(sizeof(tok) / sizeof(tok[0]))) { |
| 713 |
tok[tcnt++] = s; |
| 714 |
s = strtok_r(NULL, " \t\n", &save); |
| 715 |
} |
| 716 |
if (tcnt < 5) |
| 717 |
continue; |
| 718 |
|
| 719 |
local = NULL; |
| 720 |
int i; |
| 721 |
|
| 722 |
for (i = 0; i < tcnt; i++) { |
| 723 |
if (strchr(tok[i], ':')) { |
| 724 |
local = tok[i]; |
| 725 |
break; |
| 726 |
} |
| 727 |
} |
| 728 |
if (!local) |
| 729 |
continue; |
| 730 |
sep = strchr(local, ':'); |
| 731 |
if (!sep) |
| 732 |
continue; |
| 733 |
*sep = '\0'; |
| 734 |
if (parse_u32_hex_or_dec(local, &cid) || |
| 735 |
parse_u32_hex_or_dec(sep + 1, &port)) |
| 736 |
continue; |
| 737 |
|
| 738 |
if (parse_u32_hex_or_dec(tok[tcnt - 2], &st)) |
| 739 |
continue; |
| 740 |
if (parse_u32_hex_or_dec(tok[tcnt - 3], &type)) |
| 741 |
continue; |
| 742 |
inode = strtoul(tok[tcnt - 1], NULL, 10); |
| 743 |
if (!inode) |
| 744 |
continue; |
| 745 |
|
| 746 |
if (type != SOCK_STREAM || st != 0x0A || port == 0) |
| 747 |
continue; |
| 748 |
if (!list_find_inode(&l, inode)) |
| 749 |
continue; |
| 750 |
|
| 751 |
(void)cid; |
| 752 |
report_finding(port, "vsock", NULL); |
| 753 |
} |
| 754 |
fclose(f); |
| 755 |
} |
| 756 |
|
| 757 |
static void read_vsock(void) |
| 758 |
{ |
| 759 |
if (read_vsock_diag() < 0) |
| 760 |
read_vsock_proc(); |
| 761 |
} |
| 762 |
#endif |
| 763 |
|
| 764 |
int main(int argc, char **argv) |
| 765 |
{ |
| 766 |
struct netcap_opts opts = { 0, 0, 0, 0, NULL }; |
| 767 |
int i; |
| 768 |
|
| 769 |
for (i = 1; i < argc; i++) { |
| 770 |
if (strcmp(argv[i], "--advanced") == 0) |
| 771 |
opts.advanced = 1; |
| 772 |
else if (strcmp(argv[i], "--interface") == 0) { |
| 773 |
if (i + 1 >= argc) { |
| 774 |
fputs("--interface requires an argument\n", |
| 775 |
stderr); |
| 776 |
usage(); |
| 777 |
} |
| 778 |
opts.interface = argv[++i]; |
| 779 |
} else if (strncmp(argv[i], "--interface=", 12) == 0) { |
| 780 |
if (argv[i][12] == 0) { |
| 781 |
fputs("--interface requires an argument\n", |
| 782 |
stderr); |
| 783 |
usage(); |
| 784 |
} |
| 785 |
opts.interface = argv[i] + 12; |
| 786 |
} |
| 787 |
else if (strcmp(argv[i], "--list-interfaces") == 0) |
| 788 |
opts.list_interfaces = 1; |
| 789 |
else if (strcmp(argv[i], "--json") == 0) |
| 790 |
opts.json = 1; |
| 791 |
else if (strcmp(argv[i], "--no-color") == 0) |
| 792 |
opts.no_color = 1; |
| 793 |
else { |
| 794 |
fprintf(stderr, "Unknown option: %s\n", argv[i]); |
| 795 |
usage(); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
if (opts.json && !opts.advanced) { |
| 800 |
fputs("--json is only valid with --advanced\n", stderr); |
| 801 |
usage(); |
| 802 |
} |
| 803 |
if (opts.list_interfaces && !opts.advanced) { |
| 804 |
fputs("--list-interfaces is only valid with --advanced\n", |
| 805 |
stderr); |
| 806 |
usage(); |
| 807 |
} |
| 808 |
if (opts.interface && !opts.advanced) { |
| 809 |
fputs("--interface is only valid with --advanced\n", stderr); |
| 810 |
usage(); |
| 811 |
} |
| 812 |
|
| 813 |
if (opts.advanced) { |
| 814 |
#ifdef HAVE_NETCAP_ADVANCED |
| 815 |
return netcap_advanced_main(&opts); |
| 816 |
#else |
| 817 |
fputs("netcap --advanced was disabled at configure time\n", |
| 818 |
stderr); |
| 819 |
fputs("because required kernel headers were not available\n", |
| 820 |
stderr); |
| 821 |
return 1; |
| 822 |
#endif |
| 823 |
} |
| 824 |
|
| 825 |
if (argc > 1) { |
| 826 |
fputs("Too many arguments\n", stderr); |
| 827 |
usage(); |
| 828 |
} |
| 829 |
|
| 830 |
list_create(&l); |
| 831 |
collect_process_info(); |
| 832 |
|
| 833 |
// Now we check the tcp socket list... |
| 834 |
read_net("/proc/net/tcp", "tcp", 1); |
| 835 |
read_net("/proc/net/tcp6", "tcp6", 1); |
| 836 |
|
| 837 |
// Next udp sockets... |
| 838 |
read_net("/proc/net/udp", "udp", 1); |
| 839 |
read_net("/proc/net/udp6", "udp6", 1); |
| 840 |
read_net("/proc/net/udplite", "udplite", 1); |
| 841 |
read_net("/proc/net/udplite6", "udplite6", 1); |
| 842 |
|
| 843 |
// Next, raw sockets... |
| 844 |
read_net("/proc/net/raw", "raw", 0); |
| 845 |
read_net("/proc/net/raw6", "raw6", 0); |
| 846 |
|
| 847 |
// And last, read packet sockets |
| 848 |
read_packet(); |
| 849 |
|
| 850 |
// Add listeners from protocols supported in advanced mode |
| 851 |
#ifdef HAVE_NETCAP_ADVANCED |
| 852 |
read_diag_listeners(); |
| 853 |
read_vsock(); |
| 854 |
#endif |
| 855 |
|
| 856 |
// Could also do icmp,netlink,unix |
| 857 |
|
| 858 |
list_clear(&l); |
| 859 |
return 0; |
| 860 |
} |