| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2021 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <stdio.h> | ||
| 12 | #include <unistd.h> | ||
| 13 | #include <sys/time.h> | ||
| 14 | #include <time.h> | ||
| 15 | #include <inttypes.h> | ||
| 16 | #include <dirent.h> | ||
| 17 | |||
| 18 | #include <netlink.h> | ||
| 19 | #include <owner.h> | ||
| 20 | |||
| 21 | 2 | static char *pid2name(pid_t pid) | |
| 22 | { | ||
| 23 | char procname[256], *prog; | ||
| 24 | FILE *fp; | ||
| 25 | int ret; | ||
| 26 | |||
| 27 | 2 | ret = snprintf(procname, sizeof(procname), "/proc/%lu/stat", (unsigned long)pid); | |
| 28 |
2/4✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 2 times.
|
2 | if (ret < 0 || ret > (int)sizeof(procname)) |
| 29 | ✗ | return NULL; | |
| 30 | |||
| 31 | 2 | fp = fopen(procname, "r"); | |
| 32 |
1/2✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 2 times.
|
2 | if (!fp) |
| 33 | ✗ | return NULL; | |
| 34 | |||
| 35 | 2 | ret = fscanf(fp, "%*u (%m[^)]", &prog); | |
| 36 | |||
| 37 | 2 | fclose(fp); | |
| 38 | |||
| 39 |
1/2✓ Branch 0 (10→11) taken 2 times.
✗ Branch 1 (10→12) not taken.
|
2 | if (ret == 1) |
| 40 | 2 | return prog; | |
| 41 | |||
| 42 | ✗ | return NULL; | |
| 43 | } | ||
| 44 | |||
| 45 | 2 | static char *portid2name(pid_t pid, uint32_t portid, unsigned long inode) | |
| 46 | { | ||
| 47 | const struct dirent *ent; | ||
| 48 | char procname[256]; | ||
| 49 | DIR *dir; | ||
| 50 | int ret; | ||
| 51 | |||
| 52 | 2 | ret = snprintf(procname, sizeof(procname), "/proc/%lu/fd/", (unsigned long)pid); | |
| 53 |
2/4✓ Branch 0 (2→3) taken 2 times.
✗ Branch 1 (2→4) not taken.
✗ Branch 2 (3→4) not taken.
✓ Branch 3 (3→5) taken 2 times.
|
2 | if (ret < 0 || ret >= (int)sizeof(procname)) |
| 54 | ✗ | return NULL; | |
| 55 | |||
| 56 | 2 | dir = opendir(procname); | |
| 57 |
1/2✓ Branch 0 (6→7) taken 2 times.
✗ Branch 1 (6→8) not taken.
|
2 | if (!dir) |
| 58 | ✗ | return NULL; | |
| 59 | |||
| 60 | 10 | for (;;) { | |
| 61 | unsigned long ino; | ||
| 62 | char tmp[128]; | ||
| 63 | ssize_t rl; | ||
| 64 | |||
| 65 | 12 | ent = readdir(dir); | |
| 66 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→13) taken 12 times.
|
12 | if (!ent) |
| 67 | ✗ | break; | |
| 68 | |||
| 69 |
2/2✓ Branch 0 (13→14) taken 4 times.
✓ Branch 1 (13→15) taken 8 times.
|
12 | if (ent->d_type != DT_LNK) |
| 70 | 4 | continue; | |
| 71 | |||
| 72 | 8 | ret = snprintf(procname, sizeof(procname), "/proc/%d/fd/%s", | |
| 73 | 8 | pid, ent->d_name); | |
| 74 |
2/4✓ Branch 0 (15→16) taken 8 times.
✗ Branch 1 (15→17) not taken.
✗ Branch 2 (16→17) not taken.
✓ Branch 3 (16→18) taken 8 times.
|
8 | if (ret < 0 || ret >= (int)sizeof(procname)) |
| 75 | ✗ | continue; | |
| 76 | |||
| 77 | 8 | rl = readlink(procname, tmp, sizeof(tmp)); | |
| 78 |
2/4✓ Branch 0 (19→20) taken 8 times.
✗ Branch 1 (19→21) not taken.
✗ Branch 2 (20→21) not taken.
✓ Branch 3 (20→22) taken 8 times.
|
8 | if (rl <= 0 || rl >= (ssize_t)sizeof(tmp)) |
| 79 | ✗ | continue; | |
| 80 | |||
| 81 | 8 | tmp[rl] = 0; | |
| 82 | |||
| 83 | 8 | ret = sscanf(tmp, "socket:[%lu]", &ino); | |
| 84 |
3/4✓ Branch 0 (22→23) taken 2 times.
✓ Branch 1 (22→27) taken 6 times.
✓ Branch 2 (23→24) taken 2 times.
✗ Branch 3 (23→27) not taken.
|
8 | if (ret == 1 && ino == inode) { |
| 85 | 2 | closedir(dir); | |
| 86 | 2 | return pid2name(pid); | |
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | ✗ | closedir(dir); | |
| 91 | ✗ | return NULL; | |
| 92 | } | ||
| 93 | |||
| 94 | 2 | static char *name_by_portid(uint32_t portid, unsigned long inode) | |
| 95 | { | ||
| 96 | const struct dirent *ent; | ||
| 97 | char *prog; | ||
| 98 | DIR *dir; | ||
| 99 | |||
| 100 | /* Many netlink users use their process ID to allocate the first port id. */ | ||
| 101 | 2 | prog = portid2name(portid, portid, inode); | |
| 102 |
1/2✓ Branch 0 (3→4) taken 2 times.
✗ Branch 1 (3→5) not taken.
|
2 | if (prog) |
| 103 | 2 | return prog; | |
| 104 | |||
| 105 | /* no luck, search harder. */ | ||
| 106 | ✗ | dir = opendir("/proc"); | |
| 107 | ✗ | if (!dir) | |
| 108 | ✗ | return NULL; | |
| 109 | |||
| 110 | ✗ | for (;;) { | |
| 111 | unsigned long pid; | ||
| 112 | char *end; | ||
| 113 | |||
| 114 | ✗ | ent = readdir(dir); | |
| 115 | ✗ | if (!ent) | |
| 116 | ✗ | break; | |
| 117 | |||
| 118 | ✗ | if (ent->d_type != DT_DIR) | |
| 119 | ✗ | continue; | |
| 120 | |||
| 121 | ✗ | pid = strtoul(ent->d_name, &end, 10); | |
| 122 | ✗ | if (pid <= 1 || *end) | |
| 123 | ✗ | continue; | |
| 124 | |||
| 125 | ✗ | if (pid == portid) /* already tried */ | |
| 126 | ✗ | continue; | |
| 127 | |||
| 128 | ✗ | prog = portid2name(pid, portid, inode); | |
| 129 | ✗ | if (prog) | |
| 130 | ✗ | break; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | closedir(dir); | |
| 134 | ✗ | return prog; | |
| 135 | } | ||
| 136 | |||
| 137 | 2 | char *get_progname(uint32_t portid) | |
| 138 | { | ||
| 139 | 2 | FILE *fp = fopen("/proc/net/netlink", "r"); | |
| 140 | uint32_t portid_check; | ||
| 141 | unsigned long inode; | ||
| 142 | int ret, prot; | ||
| 143 | |||
| 144 |
1/2✓ Branch 0 (3→4) taken 2 times.
✗ Branch 1 (3→5) not taken.
|
2 | if (!fp) |
| 145 | ✗ | return NULL; | |
| 146 | |||
| 147 | 14 | for (;;) { | |
| 148 | char line[256]; | ||
| 149 | |||
| 150 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 16 times.
|
16 | if (!fgets(line, sizeof(line), fp)) |
| 151 | ✗ | break; | |
| 152 | |||
| 153 | 16 | ret = sscanf(line, "%*x %d %u %*x %*d %*d %*x %*d %*u %lu\n", | |
| 154 | &prot, &portid_check, &inode); | ||
| 155 | |||
| 156 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 16 times.
|
16 | if (ret == EOF) |
| 157 | ✗ | break; | |
| 158 | |||
| 159 |
5/6✓ Branch 0 (11→12) taken 14 times.
✓ Branch 1 (11→20) taken 2 times.
✓ Branch 2 (12→13) taken 2 times.
✓ Branch 3 (12→20) taken 12 times.
✓ Branch 4 (13→14) taken 2 times.
✗ Branch 5 (13→20) not taken.
|
16 | if (ret == 3 && portid_check == portid && prot == NETLINK_NETFILTER) { |
| 160 | static uint32_t last_portid; | ||
| 161 | static uint32_t last_inode; | ||
| 162 | static char *last_program; | ||
| 163 | char *prog; | ||
| 164 | |||
| 165 | 2 | fclose(fp); | |
| 166 | |||
| 167 |
1/4✗ Branch 0 (15→16) not taken.
✓ Branch 1 (15→18) taken 2 times.
✗ Branch 2 (16→17) not taken.
✗ Branch 3 (16→18) not taken.
|
2 | if (last_portid == portid && last_inode == inode) |
| 168 | 2 | return last_program; | |
| 169 | |||
| 170 | 2 | prog = name_by_portid(portid, inode); | |
| 171 | |||
| 172 | 2 | free(last_program); | |
| 173 | 2 | last_program = prog; | |
| 174 | 2 | last_portid = portid; | |
| 175 | 2 | last_inode = inode; | |
| 176 | 2 | return prog; | |
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | ✗ | fclose(fp); | |
| 181 | ✗ | return NULL; | |
| 182 | } | ||
| 183 |