commit daf4cb35f2cef3ba7b74aa8900e9c762e48a32ec from: Omar Polo via: Thomas Adam date: Sun Nov 10 11:01:43 2024 UTC plug some leak around imsg_init() error paths tested and ok stsp@ commit - 5735c9f6fa945e745aa45bc164ccccb7fddbefca commit + daf4cb35f2cef3ba7b74aa8900e9c762e48a32ec blob - a73395f68544e7ab4cfba3397442e39af584d394 blob + b6c82dd28a9f8aef9f345568105d44610402a314 --- gotd/repo_read.c +++ gotd/repo_read.c @@ -263,15 +263,10 @@ list_refs(struct imsg *imsg) size_t datalen; struct gotd_imsg_reflist irefs; struct imsgbuf ibuf; - int client_fd; struct got_object_id *head_target_id = NULL; TAILQ_INIT(&refs); - client_fd = imsg_get_fd(imsg); - if (client_fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_LEN); @@ -282,9 +277,11 @@ list_refs(struct imsg *imsg) } repo_read.refs_listed = 1; - client->fd = client_fd; + client->fd = imsg_get_fd(imsg); + if (client->fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); - imsg_init(&ibuf, client_fd); + imsg_init(&ibuf, client->fd); err = got_ref_list(&refs, repo_read.repo, "", got_ref_cmp_by_name, NULL); blob - ea922fda862cec518bb34fccb83d41257d6bec44 blob + 58219b54d1c2ae9d34d51338eb944eda43105fca --- gotd/repo_write.c +++ gotd/repo_write.c @@ -250,14 +250,9 @@ list_refs(struct imsg *imsg) size_t datalen; struct gotd_imsg_reflist irefs; struct imsgbuf ibuf; - int client_fd; TAILQ_INIT(&refs); - client_fd = imsg_get_fd(imsg); - if (client_fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_LEN); @@ -268,13 +263,16 @@ list_refs(struct imsg *imsg) } repo_write.refs_listed = 1; - client->fd = client_fd; + client->fd = imsg_get_fd(imsg); + if (client->fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + client->nref_updates = 0; client->nref_del = 0; client->nref_new = 0; client->nref_move = 0; - imsg_init(&ibuf, client_fd); + imsg_init(&ibuf, client->fd); err = got_ref_list(&refs, repo_write.repo, "", got_ref_cmp_by_name, NULL); @@ -1240,7 +1238,7 @@ recv_packfile(int *have_packfile, struct imsg *imsg) err = got_delta_cache_alloc(&pack->delta_cache); if (err) - return err; + goto done; for (i = 0; i < nitems(repo_tempfiles); i++) { struct repo_tempfile *t = &repo_tempfiles[i]; blob - 2bb64eceaff2476797ea46cd6e94349d6d17ae7e blob + 737c3fde2ecd44b03cf457be5b3727221a611dc2 --- lib/object_open_privsep.c +++ lib/object_open_privsep.c @@ -359,6 +359,8 @@ start_child(struct got_repository *repo, int type) pid = fork(); if (pid == -1) { err = got_error_from_errno("fork"); + close(imsg_fds[0]); + close(imsg_fds[1]); free(ibuf); return err; } @@ -369,6 +371,7 @@ start_child(struct got_repository *repo, int type) if (close(imsg_fds[1]) == -1) { err = got_error_from_errno("close"); + close(imsg_fds[0]); free(ibuf); return err; } blob - 7eb2c8a99caefb269b150c01c90af12ab9e277c5 blob + 76207e49a17dadd18e86609ab7833b09d308c6d2 --- lib/pack.c +++ lib/pack.c @@ -762,6 +762,8 @@ got_pack_start_privsep_child(struct got_pack *pack, st pid = fork(); if (pid == -1) { err = got_error_from_errno("fork"); + close(imsg_fds[0]); + close(imsg_fds[1]); goto done; } else if (pid == 0) { set_max_datasize(); @@ -770,8 +772,11 @@ got_pack_start_privsep_child(struct got_pack *pack, st /* not reached */ } - if (close(imsg_fds[1]) == -1) - return got_error_from_errno("close"); + if (close(imsg_fds[1]) == -1) { + err = got_error_from_errno("close"); + close(imsg_fds[0]); + goto done; + } pack->privsep_child->imsg_fd = imsg_fds[0]; pack->privsep_child->pid = pid; imsg_init(ibuf, imsg_fds[0]); blob - 4f69e0379f9bf1a344bab19dedbe8006d963fd97 blob + 746c2864eb29a5a281a4c584b7a025275d80fff6 --- lib/send.c +++ lib/send.c @@ -336,7 +336,7 @@ got_send_pack(const char *remote_name, struct got_path struct got_repository *repo, got_send_progress_cb progress_cb, void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg) { - int imsg_sendfds[2]; + int imsg_sendfds[2] = { -1, -1 }; int npackfd = -1, nsendfd = -1; int sendstatus, done = 0; const struct got_error *err; @@ -450,7 +450,7 @@ got_send_pack(const char *remote_name, struct got_path if (sendpid == -1) { err = got_error_from_errno("fork"); goto done; - } else if (sendpid == 0){ + } else if (sendpid == 0) { got_privsep_exec_child(imsg_sendfds, GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo)); } @@ -459,6 +459,7 @@ got_send_pack(const char *remote_name, struct got_path err = got_error_from_errno("close"); goto done; } + imsg_sendfds[1] = -1; imsg_init(&sendibuf, imsg_sendfds[0]); nsendfd = dup(sendfd); if (nsendfd == -1) { @@ -687,6 +688,10 @@ done: if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL) err = got_error_from_errno("waitpid"); } + if (imsg_sendfds[0] != -1 && close(imsg_sendfds[0]) == -1 && err == NULL) + err = got_error_from_errno("close"); + if (imsg_sendfds[1] != -1 && close(imsg_sendfds[1]) == -1 && err == NULL) + err = got_error_from_errno("close"); if (packfd != -1 && close(packfd) == -1 && err == NULL) err = got_error_from_errno("close"); if (delta_cache && fclose(delta_cache) == EOF && err == NULL)