Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-forge/blob/main/infra/test/leakprobe.c # interplane-forge INTERPLANE Forge Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-forge / infra / test / leakprobe.c` 310 lines ยท 12048 bytes Raw Blame History 1 /* leakprobe: test instrument for the sandbox gate (infra/test/sandbox-netns.sh). It is not part of 2 * the server or of any build output. It runs inside the sandbox in place of forged. 3 * 4 * Why C: it must call ioctl and setsockopt with options outside Omega's allowlist. Omega's host 5 * words expose no ioctl and only six scalar socket options ("no arbitrary syscall", 6 * docs/one_layer/OMEGESE.md at omega-mv `f4df8239`), so the questions this probe asks cannot be 7 * written in seeded Omega at the pinned compiler. The probe is a verification instrument only. 8 * 9 * Usage: leakprobe [canary=TEXT] [accept] [--inherited-listener (ignored; the wrapper adds it)] 10 * Descriptors: lists every open descriptor above 2 and reports any whose bytes contain TEXT. 11 * Listener: when descriptor 0 is a socket, asks the questions that reveal the network of the 12 * namespace the socket was made in: SIOCGIFCONF, SIOCGIFNAME for indexes 1..64, SIOCGIFADDR, 13 * SIOCGIFHWADDR and SIOCGIFNETMASK for each name found plus a fixed list of host names, 14 * SO_BINDTOIFINDEX followed by getsockopt SO_BINDTODEVICE (names by index), and getsockname. 15 * accept: also accepts one connection on descriptor 0 (5 s at most) and asks SIOCGIFCONF on the 16 * accepted socket, which belongs to the same namespace as the listener. 17 * Netlink: an RTM_GETADDR dump on a new route socket. 18 * Each finding prints "LEAK " for any interface other than lo or any IPv4 address other 19 * than 127.0.0.1. Each refused call prints " errno N". 20 * Exit 0: nothing leaked. 1: something leaked. 2: usage. 21 * 22 * leakprobe poison=MODE (alone): instead of the questions, try to give away the listener on 23 * descriptor 0, which the supervisor holds too (infra/test/listener-poison.py judges the result): 24 * shutdown-rd shutdown(0, SHUT_RD) 25 * shutdown-rdwr shutdown(0, SHUT_RDWR) 26 * unspec-connect connect(0, {AF_UNSPEC}): disconnects a listening socket like shutdown 27 * reuseport setsockopt(0, SOL_SOCKET, SO_REUSEPORT, 1): lets another socket share the port 28 * Prints "poison MODE rc R errno E" and exits 0. */ 29 #define _GNU_SOURCE 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 47 #ifndef SO_BINDTOIFINDEX 48 #define SO_BINDTOIFINDEX 62 49 #endif 50 51 static int leaks; 52 static char names[96][IFNAMSIZ]; 53 static int nnames; 54 55 static void leak(const char *how, const char *what) { 56 printf("LEAK %s %s\n", how, what); 57 leaks++; 58 } 59 60 static void addname(const char *n) { 61 int i; 62 if (!n[0]) return; 63 for (i = 0; i < nnames; i++) if (!strncmp(names[i], n, IFNAMSIZ)) return; 64 if (nnames < 96) { strncpy(names[nnames], n, IFNAMSIZ - 1); names[nnames][IFNAMSIZ - 1] = 0; nnames++; } 65 } 66 67 static void sawname(const char *how, const char *n) { 68 printf("%s name %s\n", how, n); 69 if (strcmp(n, "lo")) leak(how, n); 70 addname(n); 71 } 72 73 static void sawaddr(const char *how, const char *name, struct in_addr a) { 74 char buf[64]; 75 printf("%s %s %s\n", how, name, inet_ntoa(a)); 76 if (a.s_addr != htonl(INADDR_LOOPBACK)) { 77 snprintf(buf, sizeof buf, "%s=%s", name, inet_ntoa(a)); 78 leak(how, buf); 79 } 80 } 81 82 static void fds(const char *canary) { 83 DIR *d = opendir("/proc/self/fd"); 84 struct dirent *e; 85 int n = 0; 86 if (!d) { printf("fd list errno %d\n", errno); return; } 87 while ((e = readdir(d))) { 88 char path[64], target[512], buf[65536]; 89 int fd; 90 ssize_t k; 91 if (e->d_name[0] == '.') continue; 92 fd = atoi(e->d_name); 93 if (fd == dirfd(d)) continue; 94 snprintf(path, sizeof path, "/proc/self/fd/%d", fd); 95 k = readlink(path, target, sizeof target - 1); 96 target[k > 0 ? k : 0] = 0; 97 printf("fd %d -> %s\n", fd, target); 98 n++; 99 if (fd <= 2) continue; 100 { 101 char what[600]; 102 snprintf(what, sizeof what, "%d->%s", fd, target); 103 leak("fd-inherited", what); 104 } 105 if (canary && canary[0]) { 106 k = pread(fd, buf, sizeof buf - 1, 0); 107 if (k < 0) k = read(fd, buf, sizeof buf - 1); 108 if (k > 0) { 109 buf[k] = 0; 110 if (memmem(buf, (size_t)k, canary, strlen(canary))) { 111 char what[64]; 112 snprintf(what, sizeof what, "fd %d holds the canary", fd); 113 leak("fd-canary", what); 114 } 115 } 116 } 117 } 118 closedir(d); 119 printf("fd count %d\n", n); 120 } 121 122 static void ifconf(int s, const char *how) { 123 struct ifreq ifr[64]; 124 struct ifconf ifc; 125 int i; 126 memset(&ifc, 0, sizeof ifc); 127 ifc.ifc_len = sizeof ifr; 128 ifc.ifc_req = ifr; 129 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { printf("%s errno %d\n", how, errno); return; } 130 printf("%s ok bytes %d\n", how, ifc.ifc_len); 131 for (i = 0; i < ifc.ifc_len / (int)sizeof ifr[0]; i++) { 132 sawname(how, ifr[i].ifr_name); 133 if (ifr[i].ifr_addr.sa_family == AF_INET) 134 sawaddr(how, ifr[i].ifr_name, ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr); 135 } 136 } 137 138 /* Bind a socket to the first existing interface index from 2 up (1 is lo) and read the name back. 139 * Linux lets an unprivileged process bind a socket that is not yet bound to a device; a second 140 * bind needs CAP_NET_RAW, so each socket answers once. */ 141 static void bindidx(int s, const char *how) { 142 int i; 143 for (i = 2; i <= 64; i++) { 144 char dev[IFNAMSIZ + 1]; 145 socklen_t dl = sizeof dev; 146 int v = i; 147 memset(dev, 0, sizeof dev); 148 if (setsockopt(s, SOL_SOCKET, SO_BINDTOIFINDEX, &v, sizeof v) < 0) { 149 if (errno == ENODEV) continue; 150 printf("%s errno %d\n", how, errno); 151 return; 152 } 153 if (getsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, dev, &dl) == 0 && dev[0]) sawname(how, dev); 154 else printf("%s bound index %d, name unread errno %d\n", how, i, errno); 155 return; 156 } 157 printf("%s no index 2..64 exists\n", how); 158 } 159 160 static void listener(int accept_one) { 161 struct sockaddr_in sa; 162 socklen_t sl = sizeof sa; 163 struct ifreq r; 164 int i, refused = 0; 165 static const char *fixed[] = {"lo", "eth0", "enP7s7", "enP7s7np0", "wlan0", "wlP9s9", "tailscale0", "docker0", "virbr0", "br0"}; 166 167 if (getsockname(0, (struct sockaddr *)&sa, &sl) < 0) printf("getsockname errno %d\n", errno); 168 else if (sa.sin_family == AF_INET) printf("getsockname %s:%d\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); 169 else printf("getsockname family %d\n", sa.sin_family); 170 171 ifconf(0, "SIOCGIFCONF"); 172 173 for (i = 1; i <= 64; i++) { 174 memset(&r, 0, sizeof r); 175 r.ifr_ifindex = i; 176 if (ioctl(0, SIOCGIFNAME, &r) < 0) { 177 if (errno != ENODEV) { printf("SIOCGIFNAME errno %d\n", errno); refused = 1; break; } 178 continue; 179 } 180 sawname("SIOCGIFNAME", r.ifr_name); 181 } 182 if (!refused) printf("SIOCGIFNAME scanned 1..64\n"); 183 184 for (i = 0; i < (int)(sizeof fixed / sizeof fixed[0]); i++) addname(fixed[i]); 185 for (i = 0; i < nnames; i++) { 186 memset(&r, 0, sizeof r); 187 snprintf(r.ifr_name, IFNAMSIZ, "%.15s", names[i]); 188 if (ioctl(0, SIOCGIFADDR, &r) == 0) { 189 if (strcmp(names[i], "lo")) leak("SIOCGIFADDR-exists", names[i]); 190 sawaddr("SIOCGIFADDR", names[i], ((struct sockaddr_in *)&r.ifr_addr)->sin_addr); 191 } else if (errno != ENODEV && errno != EADDRNOTAVAIL) { 192 printf("SIOCGIFADDR %s errno %d\n", names[i], errno); 193 } else if (errno == EADDRNOTAVAIL && strcmp(names[i], "lo")) { 194 leak("SIOCGIFADDR-exists", names[i]); 195 } 196 memset(&r, 0, sizeof r); 197 snprintf(r.ifr_name, IFNAMSIZ, "%.15s", names[i]); 198 if (ioctl(0, SIOCGIFHWADDR, &r) == 0) { 199 unsigned char *m = (unsigned char *)r.ifr_hwaddr.sa_data; 200 char mac[64]; 201 snprintf(mac, sizeof mac, "%.15s=%02x:%02x:%02x:%02x:%02x:%02x", names[i], m[0], m[1], m[2], m[3], m[4], m[5]); 202 printf("SIOCGIFHWADDR %s\n", mac); 203 if (strcmp(names[i], "lo")) leak("SIOCGIFHWADDR", mac); 204 } else if (errno != ENODEV) { 205 printf("SIOCGIFHWADDR %s errno %d\n", names[i], errno); 206 } 207 memset(&r, 0, sizeof r); 208 snprintf(r.ifr_name, IFNAMSIZ, "%.15s", names[i]); 209 if (ioctl(0, SIOCGIFNETMASK, &r) == 0) { 210 if (strcmp(names[i], "lo")) leak("SIOCGIFNETMASK-exists", names[i]); 211 } else if (errno != ENODEV && errno != EADDRNOTAVAIL) { 212 printf("SIOCGIFNETMASK %s errno %d\n", names[i], errno); 213 } 214 } 215 216 if (accept_one) { 217 struct pollfd p = {0, POLLIN, 0}; 218 int c; 219 if (poll(&p, 1, 5000) == 1 && (c = accept(0, NULL, NULL)) >= 0) { 220 printf("accepted a connection\n"); 221 ifconf(c, "accepted-SIOCGIFCONF"); 222 bindidx(c, "accepted-SO_BINDTOIFINDEX"); 223 close(c); 224 } else { 225 printf("accept none errno %d\n", errno); 226 } 227 } 228 /* Last, because binding the listener to a device changes what it accepts (this private run only). */ 229 bindidx(0, "SO_BINDTOIFINDEX"); 230 } 231 232 static void netlink(void) { 233 struct { struct nlmsghdr h; struct ifaddrmsg m; } q; 234 char buf[65536]; 235 int s = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE), done = 0, rounds = 0; 236 if (s < 0) { printf("netlink socket errno %d\n", errno); return; } 237 memset(&q, 0, sizeof q); 238 q.h.nlmsg_len = sizeof q; 239 q.h.nlmsg_type = RTM_GETADDR; 240 q.h.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; 241 q.h.nlmsg_seq = 1; 242 q.m.ifa_family = AF_UNSPEC; 243 if (send(s, &q, sizeof q, 0) < 0) { printf("netlink send errno %d\n", errno); close(s); return; } 244 while (!done && rounds++ < 64) { 245 ssize_t n = recv(s, buf, sizeof buf, 0); 246 struct nlmsghdr *h; 247 if (n <= 0) { printf("netlink recv errno %d\n", errno); break; } 248 for (h = (struct nlmsghdr *)buf; NLMSG_OK(h, (size_t)n); h = NLMSG_NEXT(h, n)) { 249 struct ifaddrmsg *m; 250 struct rtattr *a; 251 int l; 252 char label[IFNAMSIZ] = ""; 253 struct in_addr v4; 254 int have4 = 0; 255 if (h->nlmsg_type == NLMSG_DONE || h->nlmsg_type == NLMSG_ERROR) { done = 1; break; } 256 if (h->nlmsg_type != RTM_NEWADDR) continue; 257 m = NLMSG_DATA(h); 258 l = IFA_PAYLOAD(h); 259 for (a = IFA_RTA(m); RTA_OK(a, l); a = RTA_NEXT(a, l)) { 260 if (a->rta_type == IFA_LABEL) snprintf(label, sizeof label, "%s", (char *)RTA_DATA(a)); 261 if (a->rta_type == IFA_LOCAL && m->ifa_family == AF_INET) { memcpy(&v4, RTA_DATA(a), 4); have4 = 1; } 262 } 263 if (m->ifa_family == AF_INET) { 264 if (label[0]) sawname("netlink", label); 265 if (have4) sawaddr("netlink", label, v4); 266 } else { 267 printf("netlink family %d index %u\n", m->ifa_family, m->ifa_index); 268 } 269 } 270 } 271 close(s); 272 } 273 274 static int poison(const char *mode) { 275 int rc, one = 1; 276 struct sockaddr sa; 277 errno = 0; 278 if (!strcmp(mode, "shutdown-rd")) rc = shutdown(0, SHUT_RD); 279 else if (!strcmp(mode, "shutdown-rdwr")) rc = shutdown(0, SHUT_RDWR); 280 else if (!strcmp(mode, "unspec-connect")) { 281 memset(&sa, 0, sizeof sa); 282 sa.sa_family = AF_UNSPEC; 283 rc = connect(0, &sa, sizeof sa); 284 } else if (!strcmp(mode, "reuseport")) rc = setsockopt(0, SOL_SOCKET, SO_REUSEPORT, &one, sizeof one); 285 else { fprintf(stderr, "leakprobe: unknown poison %s\n", mode); return 2; } 286 printf("poison %s rc %d errno %d\n", mode, rc, rc < 0 ? errno : 0); 287 return 0; 288 } 289 290 int main(int argc, char **argv) { 291 const char *canary = NULL; 292 int i, accept_one = 0; 293 struct stat st; 294 setvbuf(stdout, NULL, _IOLBF, 0); 295 for (i = 1; i < argc; i++) 296 if (!strncmp(argv[i], "poison=", 7)) return poison(argv[i] + 7); 297 for (i = 1; i < argc; i++) { 298 if (!strncmp(argv[i], "canary=", 7)) canary = argv[i] + 7; 299 else if (!strcmp(argv[i], "accept")) accept_one = 1; 300 else if (!strcmp(argv[i], "--inherited-listener")) continue; /* added by the wrapper */ 301 else { fprintf(stderr, "usage: leakprobe [canary=TEXT] [accept] | leakprobe poison=MODE\n"); return 2; } 302 } 303 printf("leakprobe start\n"); 304 fds(canary); 305 if (fstat(0, &st) == 0 && S_ISSOCK(st.st_mode)) { printf("descriptor 0 is a socket\n"); listener(accept_one); } 306 else printf("descriptor 0 is not a socket\n"); 307 netlink(); 308 printf("leakprobe leaks %d\n", leaks); 309 return leaks ? 1 : 0; 310 }