commit f2286bbdf72e2307676016716312cbd7a6e33e95 from: Tracey Emery date: Wed Aug 26 15:05:35 2020 UTC clean up some dereference problems, free problems, and logic problems commit - 58c163fbf46a6eb7305e22d853f30a5b1dd9a9f1 commit + f2286bbdf72e2307676016716312cbd7a6e33e95 blob - 8ed04eb1c1bdaeb2ba0a7bb37fe4bf59f1e5f9d7 blob + c5818a241aa3779410d9bd929d110ab491590c16 --- thingsctl/thingsctl.c +++ thingsctl/thingsctl.c @@ -114,8 +114,8 @@ main(int argc, char *argv[]) case LOG_BRIEF: case KILL_CLIENT: case SHOW_PACKETS: - /* if (geteuid() != 0) */ - /* errx(1, "need root privileges"); */ + if (geteuid() != 0) + errx(1, "need root privileges"); break; default: break; @@ -235,18 +235,17 @@ main(int argc, char *argv[]) done = 1; break; } + ctl_pkt = NULL; ctl_pkt = calloc(IMSG_DATA_SIZE(&imsg), sizeof(*ctl_pkt)); if (ctl_pkt == NULL) err(1, "%s: calloc", __func__); - if ((ctl_pkt = strndup(imsg.data, - IMSG_DATA_SIZE(&imsg))) == NULL) { - free(ctl_pkt); - break; - } + ctl_pkt = strndup(imsg.data, + IMSG_DATA_SIZE(&imsg)); + if (ctl_pkt == NULL) + err(1, "%s: calloc", __func__); printf("%s\n", ctl_pkt); free(ctl_pkt); - ctl_pkt = NULL; break; case SHOW_PARENT: done = show_parent_msg(&imsg); @@ -259,7 +258,6 @@ main(int argc, char *argv[]) } printf("\n"); close(ctl_sock); - free(ctl_pkt); free(ibuf); return (0); blob - 6e0499e86b64ecc0cb533aa89b9e3b4254855565 blob + 87f051e1ac40fe3d0fe2e42309f89851765c3044 --- thingsd/client.c +++ thingsd/client.c @@ -170,11 +170,11 @@ err: client_inflight--; if (client_fd != -1) close(client_fd); - if (sock->tls) - tls_free(client->tls_ctx); - if (client->subscriptions != NULL) - free(client->subscriptions); if (client != NULL) { + if (sock->tls) + tls_free(client->tls_ctx); + if (client->subscriptions != NULL) + free(client->subscriptions); free(client->ev); free(client); } @@ -280,12 +280,13 @@ client_rd(struct bufferevent *bev, void *arg) TAILQ_FOREACH(tclient, env->clients, entry) { if (tclient->fd == fd) { client = tclient; - if (client == NULL) - return; break; } } + if (client == NULL) + return; + client->evb = EVBUFFER_INPUT(bev); len = EVBUFFER_LENGTH(client->evb); @@ -337,7 +338,7 @@ client_tls_readcb(int fd, short event, void *arg) { struct bufferevent *bufev = (struct bufferevent *)arg; struct thingsd *env = bufev->cbarg; - struct client *client, *tclient; + struct client *client = NULL, *tclient; char pkt[PKT_BUFF]; ssize_t ret; size_t len; @@ -348,6 +349,9 @@ client_tls_readcb(int fd, short event, void *arg) client = tclient; } + if (client == NULL) + return; + memset(pkt, 0, sizeof(pkt)); ret = tls_read(client->tls_ctx, pkt, PKT_BUFF); @@ -458,6 +462,9 @@ client_tls_writecb(int fd, short event, void *arg) } if (EVBUFFER_LENGTH(bufev->output)) { + if (client == NULL) + return; + ret = tls_write(client->tls_ctx, EVBUFFER_DATA(bufev->output), EVBUFFER_LENGTH(bufev->output)); blob - 4edebd007d9eb41edf61af59e71cd07319ceba3d blob + ebc380249113146b5fa64a0053704158a15ba211 --- thingsd/events.c +++ thingsd/events.c @@ -30,23 +30,28 @@ void udp_event(int fd, short event, void *arg) { struct thingsd *env = (struct thingsd *)arg; - struct thing *thing = NULL, *tthing; - struct client *client; - struct subscription *sub; + struct thing *thing = NULL, *tthing = NULL; + struct client *client = NULL; + struct subscription *sub = NULL; char pkt[PKT_BUFF]; int len; socklen_t *addrlen = NULL; struct sockaddr *addr = NULL; + bool fail = true; memset(pkt, 0, sizeof(pkt)); TAILQ_FOREACH(tthing, env->things, entry) { if (tthing->fd == fd) { thing = tthing; + fail = false; break; } } + if (fail) + return; + len = recvfrom(fd, pkt, sizeof(pkt), 0, addr, addrlen); if (len > 0) { blob - 7fa8b55aa3c8ddfa8e2c2fdfd131757bfe3fed4c blob + a5d75fc22aba5c3c780bfdc5a673925b416cb39e --- thingsd/parse.y +++ thingsd/parse.y @@ -261,7 +261,6 @@ locationopts1 : bindopts2 if (pn >= sizeof(new_thing->parity)) fatalx("%s: new_thing->parity " "too long", __func__); - free($2); continue; } } @@ -270,6 +269,7 @@ locationopts1 : bindopts2 yyerror("parity syntax error"); YYERROR; } + free($2); } | SOFTWARE NUMBER { if ($2 > 1 || $2 < 0) { @@ -417,7 +417,7 @@ subopts : { ; subthings : THING '{' STRING optcomma STRING '}' optcomma { - struct thing *thing; + struct thing *thing = NULL; struct subscription *sub, *nsub; bool fail = false; @@ -454,7 +454,7 @@ subthings : THING '{' STRING optcomma STRING '}' optco } } - if (fail) + if (thing == NULL || fail) goto done; if (strcmp(thing->password, $5) == 0) { @@ -471,15 +471,14 @@ subthings : THING '{' STRING optcomma STRING '}' optco client->name, thing->name); } } - done: free($3); free($5); } ; -subthings2 : subthings2 subthings nl - | subthings optnl +subthings2 : subthings2 subthings + | subthings ; thing : THING STRING { @@ -824,7 +823,7 @@ int pushback_index = 0; int lgetc(int quotec) { - int c, next; + int c = 0, next; if (parsebuf) { /* Read character from the parsebuffer instead of input. */ blob - e598a18fa02a0a644f47076f5377462f5c7f48ef blob + 36628df5556dd5edae78dff6866e32d8c0862d27 --- thingsd/sockets.c +++ thingsd/sockets.c @@ -378,6 +378,7 @@ get_ifaddrs(char *name) } + free(addr); freeifaddrs(ifaps); return NULL; } blob - f8d113e659d0be189908917f0112f0acdb2cc313 blob + 7143a1b701146e1575f922cd0b7eda80e7640bce --- thingsd/tls.c +++ thingsd/tls.c @@ -265,6 +265,9 @@ socket_tls_handshake(int fd, short event, void *arg) } } + if (client == NULL) + return; + ret = tls_handshake(client->tls_ctx); if (ret == 0) {