From patchwork Sun Jun 20 15:11:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333435 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8C4AC48BDF for ; Sun, 20 Jun 2021 15:12:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B9650610CD for ; Sun, 20 Jun 2021 15:12:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229945AbhFTPOZ (ORCPT ); Sun, 20 Jun 2021 11:14:25 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:7234 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbhFTPOY (ORCPT ); Sun, 20 Jun 2021 11:14:24 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 7AF6FED6 for ; Sun, 20 Jun 2021 17:12:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201929; x=1626016330; bh=Y+UnCjy/Ao+yHBYhkwGq/aWbwO2UMnj/gzeQRubH48w=; b=Tc1YLASdEFIG 2DjJpeA/2fE6YsnRpT1zXTbmCBB/1dWyQMmUMV88VnsubaQztyl0TDL5KT0Ov49o G4HQI6tiwJqy65jITXVstquWIsFfAcq1scKOdCBPgczq+AK3gyfcNvIWsZMQVtei KdWtSBVHDtRGkeRkCD3q0P5T0VAZ1d/1OaRYS/Cw+asXypTFaMUV7oRC/NZb/Vma bmq+Cpc81DCuhn49NuivV4SUBn2l4HEA1Bp6RPg5ejVxqfMS+/uDm2ZLbJt/vZy4 aO0LjOv47YdUs8SqdFn1YleIbCAbLfmDgQ43cLjQcYWpEi5XuFtMcCRbe70M0Gyh cUpsdui7T8mrhjVIvVxWo2A+u0KzicWNeLbYNXbpQx9KRu7f0enSDBP5gq4VmoS4 zO6SSX6p3JXxRJWOC4vz5uGGbROQaTY/MV97Eq4mEPInCMvCrq8u7zF0cQwnUK7C RRnGNs65aOfvXT1aDBBD0P5e+LEHqCV+Vbl7ErsUsBQDt6mBjJR7jO6Ur4CwUutJ FmEOaP1wEicOdZGW6JbmY83qlw+bO1w9sbgjbXSqBC7ztr/cqmwOQo2oxn6e+5+/ PkfbhhohK6oSnihkLPyHtNZofB7HuT7/uBs84+hTK+e8/CnCWExOPh8f66jvX2Rf Ddxl0/CWtPyIOpRxDNlQfH8LaKvAt64= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out001.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ozz3GORrSlMS for ; Sun, 20 Jun 2021 17:12:09 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id 92CD7660 for ; Sun, 20 Jun 2021 17:12:09 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx003.mykolab.com (Postfix) with ESMTPS id 2FB21A2E; Sun, 20 Jun 2021 17:12:08 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 01/12] fmt-merge-msg: free newly allocated temporary strings when done Date: Sun, 20 Jun 2021 17:11:53 +0200 Message-Id: <20210620151204.19260-2-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt origin starts off pointing to somewhere within line, which is owned by the caller. Later we might allocate a new string using xmemdupz() or xstrfmt(). To avoid leaking these new strings, we introduce a to_free pointer - which allows us to safely free the newly allocated string when we're done (we cannot just free origin directly as it might still be pointing to line). LSAN output from t0090: Direct leak of 8 byte(s) in 1 object(s) allocated from: #0 0x49a82d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3 #1 0xa71f49 in do_xmalloc wrapper.c:41:8 #2 0xa720b0 in do_xmallocz wrapper.c:75:8 #3 0xa720b0 in xmallocz wrapper.c:83:9 #4 0xa720b0 in xmemdupz wrapper.c:99:16 #5 0x8092ba in handle_line fmt-merge-msg.c:187:23 #6 0x8092ba in fmt_merge_msg fmt-merge-msg.c:666:7 #7 0x5ce2e6 in prepare_merge_message builtin/merge.c:1119:2 #8 0x5ce2e6 in collect_parents builtin/merge.c:1215:3 #9 0x5c9c1e in cmd_merge builtin/merge.c:1454:16 #10 0x4ce83e in run_builtin git.c:475:11 #11 0x4ccafe in handle_builtin git.c:729:3 #12 0x4cb01c in run_argv git.c:818:4 #13 0x4cb01c in cmd_main git.c:949:19 #14 0x6b3fad in main common-main.c:52:11 #15 0x7fb929620349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 8 byte(s) leaked in 1 allocation(s). Signed-off-by: Andrzej Hunt --- fmt-merge-msg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index 0f66818e0f..b969dc6ebb 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -105,90 +105,92 @@ static void add_merge_parent(struct merge_parents *table, static int handle_line(char *line, struct merge_parents *merge_parents) { int i, len = strlen(line); struct origin_data *origin_data; char *src; const char *origin, *tag_name; + char *to_free = NULL; struct src_data *src_data; struct string_list_item *item; int pulling_head = 0; struct object_id oid; const unsigned hexsz = the_hash_algo->hexsz; if (len < hexsz + 3 || line[hexsz] != '\t') return 1; if (starts_with(line + hexsz + 1, "not-for-merge")) return 0; if (line[hexsz + 1] != '\t') return 2; i = get_oid_hex(line, &oid); if (i) return 3; if (!find_merge_parent(merge_parents, &oid, NULL)) return 0; /* subsumed by other parents */ CALLOC_ARRAY(origin_data, 1); oidcpy(&origin_data->oid, &oid); if (line[len - 1] == '\n') line[len - 1] = 0; line += hexsz + 2; /* * At this point, line points at the beginning of comment e.g. * "branch 'frotz' of git://that/repository.git". * Find the repository name and point it with src. */ src = strstr(line, " of "); if (src) { *src = 0; src += 4; pulling_head = 0; } else { src = line; pulling_head = 1; } item = unsorted_string_list_lookup(&srcs, src); if (!item) { item = string_list_append(&srcs, src); item->util = xcalloc(1, sizeof(struct src_data)); init_src_data(item->util); } src_data = item->util; if (pulling_head) { origin = src; src_data->head_status |= 1; } else if (skip_prefix(line, "branch ", &origin)) { origin_data->is_local_branch = 1; string_list_append(&src_data->branch, origin); src_data->head_status |= 2; } else if (skip_prefix(line, "tag ", &tag_name)) { origin = line; string_list_append(&src_data->tag, tag_name); src_data->head_status |= 2; } else if (skip_prefix(line, "remote-tracking branch ", &origin)) { string_list_append(&src_data->r_branch, origin); src_data->head_status |= 2; } else { origin = src; string_list_append(&src_data->generic, line); src_data->head_status |= 2; } if (!strcmp(".", src) || !strcmp(src, origin)) { int len = strlen(origin); if (origin[0] == '\'' && origin[len - 1] == '\'') - origin = xmemdupz(origin + 1, len - 2); + origin = to_free = xmemdupz(origin + 1, len - 2); } else - origin = xstrfmt("%s of %s", origin, src); + origin = to_free = xstrfmt("%s of %s", origin, src); if (strcmp(".", src)) origin_data->is_local_branch = 0; string_list_append(&origins, origin)->util = origin_data; + free(to_free); return 0; } From patchwork Sun Jun 20 15:11:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333437 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4249CC49EA2 for ; Sun, 20 Jun 2021 15:12:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 277A0610CD for ; Sun, 20 Jun 2021 15:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbhFTPO3 (ORCPT ); Sun, 20 Jun 2021 11:14:29 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:21526 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229984AbhFTPO0 (ORCPT ); Sun, 20 Jun 2021 11:14:26 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id C89834112C for ; Sun, 20 Jun 2021 17:12:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201932; x=1626016333; bh=7L8zKENofrIsSsrsrOBmvwBuKyUHkeAJxiLYIsETYsU=; b=c4A5ZMmdkIPl rd5arXjvm6Y4p2s5bzcQ8URqbuRPjUKtfq89LryLFfMWcBsLpZRKnn518WnMlD6e VyCRL6bE0FbYLKCsqjLC4RW6ykNBc+z6UX4fPfDSOGU6rhlNCxdL4CgE2Mu/XeI+ R2zlpHsmGO8mNXygJcagvt2zuQrh1oq3cdwDrBPrxRiIDc+DZHevh1wehA5aOmjo eUyA9TFvegMeaN3l66LjaJG3CWRy6LRhni6mm1MQV3gaA/VZliEUhNdvVXEJ0EMS zhV6Y6BmgLbjcz300khUmSOnjEBvVxU7hh6XMzIB+8hyw314c0g0vik5sFMGJggj 6YQxg7ob19w2jr5JUF85mtdggO8sA4LRQtWuYXtPTsw0hufwiVqRyyApJgAo5vKN PF3yx0wgwVBH9tzbB619I1hxu38tXbX/nGaJH9yfgwtGfPjvGiKiIyA+twwDWcbF 8rmZbfUvzjnrZ2K+4aeSCfbEpqLMZxXoFwInjsTFCVdBfn93eYIs29SMF/InslFt uHqJJdvjb9I5LYMbV8FbBiTtC+H+X8YLkonvliJjU7TVdrhlgzNcX9VvmftwcY8b aATF84djjRDppwdRK9ziNrpfdnxavhwCgN7NGjcehrdC4aFCt3wI5Kkb7o12ExU2 Yl+fMDmpohKf1MiFUbsUwo73qCWMC3k= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out003.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vO7IHNJaFKXZ for ; Sun, 20 Jun 2021 17:12:12 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 4AACC407B7 for ; Sun, 20 Jun 2021 17:12:12 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx002.mykolab.com (Postfix) with ESMTPS id C267F3357; Sun, 20 Jun 2021 17:12:10 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 02/12] environment: move strbuf into block to plug leak Date: Sun, 20 Jun 2021 17:11:54 +0200 Message-Id: <20210620151204.19260-3-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt realpath is only populated if we execute the git_work_tree_initialized block. However that block also causes us to return early, meaning we never actually release the strbuf in the case where we populated it. Therefore we move all strbuf related code into the block to guarantee that we can't leak it. LSAN output from t0095: Direct leak of 129 byte(s) in 1 object(s) allocated from: #0 0x49a9b9 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0x78f585 in xrealloc wrapper.c:126:8 #2 0x713ff4 in strbuf_grow strbuf.c:98:2 #3 0x713ff4 in strbuf_getcwd strbuf.c:597:3 #4 0x4f0c18 in strbuf_realpath_1 abspath.c:99:7 #5 0x5ae4a4 in set_git_work_tree environment.c:259:3 #6 0x6fdd8a in setup_discovered_git_dir setup.c:931:2 #7 0x6fdd8a in setup_git_directory_gently setup.c:1235:12 #8 0x4cb50d in get_bloom_filter_for_commit t/helper/test-bloom.c:41:2 #9 0x4cb50d in cmd__bloom t/helper/test-bloom.c:95:3 #10 0x4caa1f in cmd_main t/helper/test-tool.c:124:11 #11 0x4caded in main common-main.c:52:11 #12 0x7f0869f02349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 129 byte(s) leaked in 1 allocation(s). It looks like this leak has existed since realpath was first added to set_git_work_tree() in: 3d7747e318 (real_path: remove unsafe API, 2020-03-10) Signed-off-by: Andrzej Hunt --- environment.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/environment.c b/environment.c index 2f27008424..d6b22ede7e 100644 --- a/environment.c +++ b/environment.c @@ -249,25 +249,24 @@ static int git_work_tree_initialized; /* * Note. This works only before you used a work tree. This was added * primarily to support git-clone to work in a new repository it just * created, and is not meant to flip between different work trees. */ void set_git_work_tree(const char *new_work_tree) { - struct strbuf realpath = STRBUF_INIT; - if (git_work_tree_initialized) { + struct strbuf realpath = STRBUF_INIT; + strbuf_realpath(&realpath, new_work_tree, 1); new_work_tree = realpath.buf; if (strcmp(new_work_tree, the_repository->worktree)) die("internal error: work tree has already been set\n" "Current worktree: %s\nNew worktree: %s", the_repository->worktree, new_work_tree); + strbuf_release(&realpath); return; } git_work_tree_initialized = 1; repo_set_worktree(the_repository, new_work_tree); - - strbuf_release(&realpath); } const char *get_git_work_tree(void) From patchwork Sun Jun 20 15:11:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333439 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B64BC49361 for ; Sun, 20 Jun 2021 15:12:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33CD16109F for ; Sun, 20 Jun 2021 15:12:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230028AbhFTPOb (ORCPT ); Sun, 20 Jun 2021 11:14:31 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:15344 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbhFTPO2 (ORCPT ); Sun, 20 Jun 2021 11:14:28 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 917F712FC for ; Sun, 20 Jun 2021 17:12:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201934; x=1626016335; bh=M7xBIB3jR/9acNZxTOVZ64Wz03YiJeP6dE8TUECT3To=; b=Hj0eqvqvbCCR QYZ471hycQ9NMaNVKul4di/0qg5eS4yg4YrxN2hx1QrNbPeB4mylw435LSngeBra sGx0qhIKHgtSxJsqYfYvEdYpREnabhqB99pGZMqy1CGBc3ny2V3z1JRhd+KVDIIY JeF9o3Haxgb1CthclJ8lbNgbt79XNbD3H3z6EyJoq2o/VMsz7/BWNYo/dxE0F55c AheaXGiOc7KCVaUa6TB9hbpecp4NYkj56+D5g4HbSE61ExnT+QqYLMkB2f1jvA88 zyrPp+JEQEskjJikZXLmQHnSal+URsFk9yWBdH/eeO/L8h7k/dUSjzInegXfk4us GZnIKdilux11ISAbDK9iAAsCOL75s6DQHVDpEe/4uSgj2AibnnhAs8OoFTF6MVHo ayvYdIhJnwjx6zPiJUl7HsImosZFgDdlmujDwCQwp63o1JAMTJ1/OKFVF0RVP4hH 2S8P3PXhmG7sh9mhRr0q5gvIqJv5Eq+JVIPCqTCnQ7sNPKZVT26Xy8A4QVqYhDVZ rHHeDXEFg6bQszh4EPTad+CE+nYkVjhU0DvJHEpifJJiYcQjpu496YKeim5OfEBg 2jUxhguzc22auHtoDwYY2C0yt3R2gmyFwzcRo3Q3zXVf9DK5S0wgKDcQcPrQlO+L mzmwq9YGTXEibAVW4lC0Ez3DH2HkElo= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Iu9ocvUo3f5r for ; Sun, 20 Jun 2021 17:12:14 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 0D3C85C1 for ; Sun, 20 Jun 2021 17:12:13 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 9E87334B; Sun, 20 Jun 2021 17:12:12 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 03/12] builtin/submodule--helper: release unused strbuf to avoid leak Date: Sun, 20 Jun 2021 17:11:55 +0200 Message-Id: <20210620151204.19260-4-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt relative_url() populates sb. In the normal return path, its buffer is detached using strbuf_detach(). However the early return path does nothing with sb, which means that sb's memory is leaked - therefore we add a release to avoid this leak. The reset is also only necessary for the normal return path, hence we move it down to after the early-return to avoid unnecessary work. LSAN output from t0060: Direct leak of 121 byte(s) in 1 object(s) allocated from: #0 0x7f31246f28b0 in realloc (/usr/lib64/libasan.so.4+0xdc8b0) #1 0x98d7d6 in xrealloc wrapper.c:126 #2 0x909a60 in strbuf_grow strbuf.c:98 #3 0x90bf00 in strbuf_vaddf strbuf.c:401 #4 0x90c321 in strbuf_addf strbuf.c:335 #5 0x5cb78d in relative_url builtin/submodule--helper.c:182 #6 0x5cbe46 in resolve_relative_url_test builtin/submodule--helper.c:248 #7 0x410dcd in run_builtin git.c:475 #8 0x410dcd in handle_builtin git.c:729 #9 0x414087 in run_argv git.c:818 #10 0x414087 in cmd_main git.c:949 #11 0x40e9ec in main common-main.c:52 #12 0x7f3123c41349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 121 byte(s) leaked in 1 allocation(s). Signed-off-by: Andrzej Hunt --- builtin/submodule--helper.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index ae6174ab05..4015d114b3 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -188,11 +188,13 @@ static char *relative_url(const char *remote_url, out = xstrdup(sb.buf + 2); else out = xstrdup(sb.buf); - strbuf_reset(&sb); - if (!up_path || !is_relative) + if (!up_path || !is_relative) { + strbuf_release(&sb); return out; + } + strbuf_reset(&sb); strbuf_addf(&sb, "%s%s", up_path, out); free(out); return strbuf_detach(&sb, NULL); From patchwork Sun Jun 20 15:11:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333441 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2DAAC48BDF for ; Sun, 20 Jun 2021 15:12:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D96B610CD for ; Sun, 20 Jun 2021 15:12:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230051AbhFTPOe (ORCPT ); Sun, 20 Jun 2021 11:14:34 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:15358 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230014AbhFTPOb (ORCPT ); Sun, 20 Jun 2021 11:14:31 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id F02A212FC for ; Sun, 20 Jun 2021 17:12:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201936; x=1626016337; bh=L7+XtauHck06ubzHlBTpnUza3s+jfaVwP9HiJ74yi2I=; b=r4gjZwNsN5Tc lrbDaEcFrAOzRDpB2+L1zHGgGbTAAc8YYzrIc/iuBGWLXIwz0zHceaGIuwoYRLpF wuag+y/XoCEGmgggMt2HSUs1P03DaSsddnqs+m0Kqbe75EpiGqwesHi70gHp8NaR DhqVFtcd0WMPrxbLAiGPF0Ui0Mzu98fAff3hdXcYO2cNKvFir4iLWMWfE1iyJpbi xHlJRsvCa1pJGiudCaAU7j+w1AXlxgJZHW2xZcJck/Z26c9hhhrMkz3VNdRAcN4I c+tJygtWtd+VbOabzQHrJ5VBuwlCtQEWG2rlnkbF7L6QL0kJPkWAp9TzA51IGgIn XuxGDrwYuxVkUnfcJzK7uEaRYVJUelzj4F02LXX7XaQ1j6H9CmUeLjnpnnV0KrSw k55r8LThs9UxHwCzHoeM2aV1qMgwSkdCptxWkSGuj4g5Hr4Dr+qWToBK8INtd/NQ mWOr0qBOchF06sEB/0IPwGp69TseziTT0sg6sIWJtkvu87IG+3NU5Tw0z++TGWZ/ IDs4yJmDtHdYBLwksBIsA9sxJiU/bUqMO8FbBEXvagpAynfOqCqDUH3wkNpot0eE WihSw7LpNzVu2R2xHL8ygslVmDyav7oqAWQeluEx++82VjilNBuh8zn1PsSb89fg lSS1OAEjCEx6fb1CqCkIas7NUVCfeQ0= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gnSa79a3ojyf for ; Sun, 20 Jun 2021 17:12:16 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 69E595C1 for ; Sun, 20 Jun 2021 17:12:16 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx003.mykolab.com (Postfix) with ESMTPS id CB815A2E; Sun, 20 Jun 2021 17:12:14 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 04/12] builtin/for-each-repo: remove unnecessary argv copy to plug leak Date: Sun, 20 Jun 2021 17:11:56 +0200 Message-Id: <20210620151204.19260-5-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt cmd_for_each_repo() copies argv into args (a strvec), which is later passed into run_command_on_repo(), which in turn copies that strvec onto the end of child.args. The initial copy is unnecessary (we never modify args). We therefore choose to just pass argv directly into run_command_on_repo(), which lets us avoid the copy and fixes the leak. LSAN output from t0068: Direct leak of 192 byte(s) in 1 object(s) allocated from: #0 0x7f63bd4ab8b0 in realloc (/usr/lib64/libasan.so.4+0xdc8b0) #1 0x98d7e6 in xrealloc wrapper.c:126 #2 0x916914 in strvec_push_nodup strvec.c:19 #3 0x916a6e in strvec_push strvec.c:26 #4 0x4be4eb in cmd_for_each_repo builtin/for-each-repo.c:49 #5 0x410dcd in run_builtin git.c:475 #6 0x410dcd in handle_builtin git.c:729 #7 0x414087 in run_argv git.c:818 #8 0x414087 in cmd_main git.c:949 #9 0x40e9ec in main common-main.c:52 #10 0x7f63bc9fa349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 22 byte(s) in 2 object(s) allocated from: #0 0x7f63bd445e30 in __interceptor_strdup (/usr/lib64/libasan.so.4+0x76e30) #1 0x98d698 in xstrdup wrapper.c:29 #2 0x916a63 in strvec_push strvec.c:26 #3 0x4be4eb in cmd_for_each_repo builtin/for-each-repo.c:49 #4 0x410dcd in run_builtin git.c:475 #5 0x410dcd in handle_builtin git.c:729 #6 0x414087 in run_argv git.c:818 #7 0x414087 in cmd_main git.c:949 #8 0x40e9ec in main common-main.c:52 #9 0x7f63bc9fa349 in __libc_start_main (/lib64/libc.so.6+0x24349) See also discussion about the original implementation below - this code appears to have evolved from a callback explaining the double-strvec-copy pattern, but there's no strong reason to keep that now: https://lore.kernel.org/git/68bbeca5-314b-08ee-ef36-040e3f3814e9@gmail.com/ Signed-off-by: Andrzej Hunt --- builtin/for-each-repo.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/builtin/for-each-repo.c b/builtin/for-each-repo.c index 52be64a437..fd86e5a861 100644 --- a/builtin/for-each-repo.c +++ b/builtin/for-each-repo.c @@ -10,18 +10,16 @@ static const char * const for_each_repo_usage[] = { NULL }; -static int run_command_on_repo(const char *path, - void *cbdata) +static int run_command_on_repo(const char *path, int argc, const char ** argv) { int i; struct child_process child = CHILD_PROCESS_INIT; - struct strvec *args = (struct strvec *)cbdata; child.git_cmd = 1; strvec_pushl(&child.args, "-C", path, NULL); - for (i = 0; i < args->nr; i++) - strvec_push(&child.args, args->v[i]); + for (i = 0; i < argc; i++) + strvec_push(&child.args, argv[i]); return run_command(&child); } @@ -29,37 +27,33 @@ static int run_command_on_repo(const char *path, int cmd_for_each_repo(int argc, const char **argv, const char *prefix) { static const char *config_key = NULL; int i, result = 0; const struct string_list *values; - struct strvec args = STRVEC_INIT; const struct option options[] = { OPT_STRING(0, "config", &config_key, N_("config"), N_("config key storing a list of repository paths")), OPT_END() }; argc = parse_options(argc, argv, prefix, options, for_each_repo_usage, PARSE_OPT_STOP_AT_NON_OPTION); if (!config_key) die(_("missing --config=")); - for (i = 0; i < argc; i++) - strvec_push(&args, argv[i]); - values = repo_config_get_value_multi(the_repository, config_key); /* * Do nothing on an empty list, which is equivalent to the case * where the config variable does not exist at all. */ if (!values) return 0; for (i = 0; !result && i < values->nr; i++) - result = run_command_on_repo(values->items[i].string, &args); + result = run_command_on_repo(values->items[i].string, argc, argv); return result; } From patchwork Sun Jun 20 15:11:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333443 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 068C9C48BDF for ; Sun, 20 Jun 2021 15:12:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DF13A6109F for ; Sun, 20 Jun 2021 15:12:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230032AbhFTPOi (ORCPT ); Sun, 20 Jun 2021 11:14:38 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:7256 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230031AbhFTPOc (ORCPT ); Sun, 20 Jun 2021 11:14:32 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id F1751ED6 for ; Sun, 20 Jun 2021 17:12:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201938; x=1626016339; bh=wVEwSTb9W40/ToQrLAMmbnizQ/Dtkw76tTg6aM7UZJA=; b=mWKDuPuGPUX8 YwQWFSMyuD4zh8ygqPxkO/L32wd8oftx91fTZ1yN84JZWJCc0VRkrE9n0MLRsB3C JggS+Y1Znfy+AJC/lglgGdFlOZdNWvYWCR6rREf4x79uj6UQPEiwkt+Li/RMAQ77 Wa0Z+6iFE+wal71ukhyGJMVU2yMj/dLxNCOOIRRT829l6taXscJbIextDPUMMNvf 0ZrMePiIulmVJ4inZCwwfTHCLp/yrCNJYzQXyhQI/eXoq/QzgqTeQ1XiBPceZEyx FPuMklVYg3DD700TR9DbrbZCN1Jww2dcSRg2CLxG66qsDYVFtGVO2dR4xG4OtlMr hskCg/Hnwams+fgd0HG5bHaZo/ctWiOetz+2n4Q+stiBG42yZwx2+nMuRCibd8O8 mppYUXEOBGZvuwVn1EJCRWYPxdF/HNxlHxf6lRYNCm/fN9SHRX78VaeY8U7EhG5n 4QLGams+UWYtWj+/uhpv2zU5KY08nXCraNonwfNnGmzRJQOoIFqBQmczPhwQ7tyS U+S+knQPZEOkZIVjUS4CfWk9alqm0ef+fNOCDkEYHxK4OC2Qp/Qv1QbFY0hY9Hfy bo/5nc//FgAo7TlJF9BXqtYHqLO2qCjagl1wN9rvwy9mCMMWMg1YSSrEwU819wlL xJTEdlaPdCXiwxfxvHcDEyuWHmWQ4JY= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out001.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KaeK9bzvisi5 for ; Sun, 20 Jun 2021 17:12:18 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id 4B407660 for ; Sun, 20 Jun 2021 17:12:18 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 39A9934B; Sun, 20 Jun 2021 17:12:17 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 05/12] diffcore-rename: move old_dir/new_dir definition to plug leak Date: Sun, 20 Jun 2021 17:11:57 +0200 Message-Id: <20210620151204.19260-6-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt old_dir/new_dir are free()'d at the end of update_dir_rename_counts, however if we return early we'll never free those strings. Therefore we should move all new allocations after the possible early return, avoiding a leak. This seems like a fairly recent leak, that started happening since the early-return was added in: 1ad69eb0dc (diffcore-rename: compute dir_rename_counts in stages, 2021-02-27) LSAN output from t0022: Direct leak of 7 byte(s) in 1 object(s) allocated from: #0 0x486804 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3 #1 0xa71e48 in xstrdup wrapper.c:29:14 #2 0x7db9c7 in update_dir_rename_counts diffcore-rename.c:464:12 #3 0x7db6ae in find_renames diffcore-rename.c:1062:3 #4 0x7d76c3 in diffcore_rename_extended diffcore-rename.c:1472:18 #5 0x7b4cfc in diffcore_std diff.c:6705:4 #6 0x855e46 in log_tree_diff_flush log-tree.c:846:2 #7 0x856574 in log_tree_diff log-tree.c:955:3 #8 0x856574 in log_tree_commit log-tree.c:986:10 #9 0x9a9c67 in print_commit_summary sequencer.c:1329:7 #10 0x52e623 in cmd_commit builtin/commit.c:1862:3 #11 0x4ce83e in run_builtin git.c:475:11 #12 0x4ccafe in handle_builtin git.c:729:3 #13 0x4cb01c in run_argv git.c:818:4 #14 0x4cb01c in cmd_main git.c:949:19 #15 0x6b3f3d in main common-main.c:52:11 #16 0x7fe397c7a349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 7 byte(s) in 1 object(s) allocated from: #0 0x486804 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3 #1 0xa71e48 in xstrdup wrapper.c:29:14 #2 0x7db9bc in update_dir_rename_counts diffcore-rename.c:463:12 #3 0x7db6ae in find_renames diffcore-rename.c:1062:3 #4 0x7d76c3 in diffcore_rename_extended diffcore-rename.c:1472:18 #5 0x7b4cfc in diffcore_std diff.c:6705:4 #6 0x855e46 in log_tree_diff_flush log-tree.c:846:2 #7 0x856574 in log_tree_diff log-tree.c:955:3 #8 0x856574 in log_tree_commit log-tree.c:986:10 #9 0x9a9c67 in print_commit_summary sequencer.c:1329:7 #10 0x52e623 in cmd_commit builtin/commit.c:1862:3 #11 0x4ce83e in run_builtin git.c:475:11 #12 0x4ccafe in handle_builtin git.c:729:3 #13 0x4cb01c in run_argv git.c:818:4 #14 0x4cb01c in cmd_main git.c:949:19 #15 0x6b3f3d in main common-main.c:52:11 #16 0x7fe397c7a349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 14 byte(s) leaked in 2 allocation(s). Signed-off-by: Andrzej Hunt --- diffcore-rename.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/diffcore-rename.c b/diffcore-rename.c index 3375e24659..f7c728fe47 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -455,9 +455,9 @@ static void update_dir_rename_counts(struct dir_rename_info *info, const char *oldname, const char *newname) { - char *old_dir = xstrdup(oldname); - char *new_dir = xstrdup(newname); - char new_dir_first_char = new_dir[0]; + char *old_dir; + char *new_dir; + const char new_dir_first_char = newname[0]; int first_time_in_loop = 1; if (!info->setup) @@ -482,6 +482,10 @@ static void update_dir_rename_counts(struct dir_rename_info *info, */ return; + + old_dir = xstrdup(oldname); + new_dir = xstrdup(newname); + while (1) { int drd_flag = NOT_RELEVANT; From patchwork Sun Jun 20 15:11:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F37FAC48BDF for ; Sun, 20 Jun 2021 15:12:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DDAEB6109F for ; Sun, 20 Jun 2021 15:12:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230146AbhFTPOn (ORCPT ); Sun, 20 Jun 2021 11:14:43 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:21552 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbhFTPOg (ORCPT ); Sun, 20 Jun 2021 11:14:36 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id AF4D34112C for ; Sun, 20 Jun 2021 17:12:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201941; x=1626016342; bh=PSGkEFO1THQYjwYuHdoRxHq2p5ZO/zKHc7cftucOtrw=; b=UlrYPFk51j1a 9f1zSvTs6nITn4dOQ6MHy5MYATOEECHD1topdjjj9nH9gbkaAHwW1gJrWUmXor8Z J0vzkRFTtWL13zxmk7nNTpPglZtv30FnkI2XeJwFobdKnOEEQU9KgNjFTpJJnDN2 XX6zselkaBceLv6KAOWKEOJsySZIkl2l2RipH4/759hJ62hLltcCeA5NPvCCrfh1 BqmSfuxBPTx6eYibiL0tkFLzjMcwuJSRiF/hW0g6elkYFVSVx6iE99z0imEJuo8f djjioTtMW1czFLLNH8fwDHunmMgPnO5wSX3oApTepPJF/5omxmnQtRY05qLqY6wH So2xssmkGBFwQgRhC0erTLn4+6OxKkIxO19cqwxwxHBrw8OpbrJ/xoQvw6n3EmZ/ 5qZ3KD5RHeDmLUoOpfASCybPRV4ZCElVoGIsQysNvjoXbY8EhuYiz4+4kCBYGVxk nhckByL3QcHKjlonU8GHmkxo7MUcbvUnHrINQVopclIF4ds9EW3dDWJrebnG8Hhs xMiOT73iuPplshh+WgIH3le3dtYidA1vB2xAEHN73tc3iO02iEV21xJOjuP1C9AM DHK3OWk7fa5Mi69PAD2z/cZoMk1/rZQD4FebdmlWR/hocbxUHpfMLHklSnpXoPNM /jRtvHIPsPyC/rDQJJxcgeqGISCwtRA= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out003.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Cam4O9W98ux1 for ; Sun, 20 Jun 2021 17:12:21 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 21F25407B7 for ; Sun, 20 Jun 2021 17:12:20 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id DACCA34B; Sun, 20 Jun 2021 17:12:19 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 06/12] ref-filter: also free head for ATOM_HEAD to avoid leak Date: Sun, 20 Jun 2021 17:11:58 +0200 Message-Id: <20210620151204.19260-7-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt u.head is populated using resolve_refdup(), which returns a newly allocated string - hence we also need to free() it. Found while running t0041 with LSAN: Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x486804 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3 #1 0xa8be98 in xstrdup wrapper.c:29:14 #2 0x9481db in head_atom_parser ref-filter.c:549:17 #3 0x9408c7 in parse_ref_filter_atom ref-filter.c:703:30 #4 0x9400e3 in verify_ref_format ref-filter.c:974:8 #5 0x4f9e8b in print_ref_list builtin/branch.c:439:6 #6 0x4f9e8b in cmd_branch builtin/branch.c:757:3 #7 0x4ce83e in run_builtin git.c:475:11 #8 0x4ccafe in handle_builtin git.c:729:3 #9 0x4cb01c in run_argv git.c:818:4 #10 0x4cb01c in cmd_main git.c:949:19 #11 0x6bdc2d in main common-main.c:52:11 #12 0x7f96edf86349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s). Signed-off-by: Andrzej Hunt --- ref-filter.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 4db0e40ff4..f8bfd25ae4 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2225,8 +2225,12 @@ void ref_array_clear(struct ref_array *array) FREE_AND_NULL(array->items); array->nr = array->alloc = 0; - for (i = 0; i < used_atom_cnt; i++) - free((char *)used_atom[i].name); + for (i = 0; i < used_atom_cnt; i++) { + struct used_atom *atom = &used_atom[i]; + if (atom->atom_type == ATOM_HEAD) + free(atom->u.head); + free((char *)atom->name); + } FREE_AND_NULL(used_atom); used_atom_cnt = 0; From patchwork Sun Jun 20 15:11:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333447 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55BCFC48BDF for ; Sun, 20 Jun 2021 15:12:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4138F6109F for ; Sun, 20 Jun 2021 15:12:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230204AbhFTPOw (ORCPT ); Sun, 20 Jun 2021 11:14:52 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:15368 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230082AbhFTPOh (ORCPT ); Sun, 20 Jun 2021 11:14:37 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 15E4F12FC for ; Sun, 20 Jun 2021 17:12:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201942; x=1626016343; bh=7xJGAO3HmotPtfpYR5xOy+rsQdBXInpdymVSWPWfFvg=; b=Hre5Fsq7OoVt EDmpkXUrXyysd97My+ewUXX6hiliDJFkp392+vCHrlep2JRZZJOMNz881ySb9+8y qDYs0vTf8yt/FcrnPypoWEr1cBjt0L4J7g5QcMh/3jVMsFLs2kY/D0Fe+ay8Mowu qzFprnuDvxP9xpkbOXi0VcXFI/uXsF1vWCMSZoYFlwWLABZgApPLu6V9NaDteiHE nRpLglsDY6wvk1DJoPZxt/20qJfZJ3DwuFhOHTmY1DTMcoBdyMZztyq3cc1zXNMj Ii+oByjHNePGKkcfD/2xmaC4uPa6QDX96O/NJt/kO8GHsWGzuTaJikoiz6xjWcth UNth6GyIc2/N+TayhoxoqoUJXF5tZWF9PuzZASUl5HdjWCfEmrc/CX+0QA7eRQL4 7MtaltdsNay76p6l/iB9E9eMpCeRYxAa+P2O7ivZSqbaV6PzOjX3uACxxmfFhYzF ifjzKn5K+GXOYmKgjhfDgiTWCD4O0Z9JIV0KKLNEeLyRF42A8ydkGCnjQGmdbnYF 37P1SlUh3npxlUn2PSk63PYl1pYNYvoIM9ygBMy+LUmH9Rh+XDcg+/jW4qoaZ5Ek g0CmHPEQuNmS0CDvGx0QW0XkCK781t/wLEZddDjzjGt0by+bGSqsHlikBR7QTwvb Pv+mYaBNjOl+YA4VoK26763e08ABJrw= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DHOwdht5GxSc for ; Sun, 20 Jun 2021 17:12:22 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 8F7D25C1 for ; Sun, 20 Jun 2021 17:12:22 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id ABB4934B; Sun, 20 Jun 2021 17:12:21 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 07/12] read-cache: call diff_setup_done to avoid leak Date: Sun, 20 Jun 2021 17:11:59 +0200 Message-Id: <20210620151204.19260-8-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt repo_diff_setup() calls through to diff.c's static prep_parse_options(), which in turn allocates a new array into diff_opts.parseopts. diff_setup_done() is responsible for freeing that array, and has the benefit of verifying diff_opts too - hence we add a call to diff_setup_done() to avoid leaking parseopts. Output from the leak as found while running t0090 with LSAN: Direct leak of 7120 byte(s) in 1 object(s) allocated from: #0 0x49a82d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3 #1 0xa8bf89 in do_xmalloc wrapper.c:41:8 #2 0x7a7bae in prep_parse_options diff.c:5636:2 #3 0x7a7bae in repo_diff_setup diff.c:4611:2 #4 0x93716c in repo_index_has_changes read-cache.c:2518:3 #5 0x872233 in unclean merge-ort-wrappers.c:12:14 #6 0x872233 in merge_ort_recursive merge-ort-wrappers.c:53:6 #7 0x5d5b11 in try_merge_strategy builtin/merge.c:752:12 #8 0x5d0b6b in cmd_merge builtin/merge.c:1666:9 #9 0x4ce83e in run_builtin git.c:475:11 #10 0x4ccafe in handle_builtin git.c:729:3 #11 0x4cb01c in run_argv git.c:818:4 #12 0x4cb01c in cmd_main git.c:949:19 #13 0x6bdc2d in main common-main.c:52:11 #14 0x7f551eb51349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 7120 byte(s) leaked in 1 allocation(s) Signed-off-by: Andrzej Hunt --- read-cache.c | 1 + 1 file changed, 1 insertion(+) diff --git a/read-cache.c b/read-cache.c index 77961a3885..212d604dd3 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2487,37 +2487,38 @@ int unmerged_index(const struct index_state *istate) int repo_index_has_changes(struct repository *repo, struct tree *tree, struct strbuf *sb) { struct index_state *istate = repo->index; struct object_id cmp; int i; if (tree) cmp = tree->object.oid; if (tree || !get_oid_tree("HEAD", &cmp)) { struct diff_options opt; repo_diff_setup(repo, &opt); opt.flags.exit_with_status = 1; if (!sb) opt.flags.quick = 1; + diff_setup_done(&opt); do_diff_cache(&cmp, &opt); diffcore_std(&opt); for (i = 0; sb && i < diff_queued_diff.nr; i++) { if (i) strbuf_addch(sb, ' '); strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path); } diff_flush(&opt); return opt.flags.has_changes != 0; } else { /* TODO: audit for interaction with sparse-index. */ ensure_full_index(istate); for (i = 0; sb && i < istate->cache_nr; i++) { if (i) strbuf_addch(sb, ' '); strbuf_addstr(sb, istate->cache[i]->name); } return !!istate->cache_nr; } } From patchwork Sun Jun 20 15:12:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333449 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E127C48BDF for ; Sun, 20 Jun 2021 15:12:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6769C6108E for ; Sun, 20 Jun 2021 15:12:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230064AbhFTPPE (ORCPT ); Sun, 20 Jun 2021 11:15:04 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:21566 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230107AbhFTPOj (ORCPT ); Sun, 20 Jun 2021 11:14:39 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id 8821D411D8 for ; Sun, 20 Jun 2021 17:12:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201945; x=1626016346; bh=YIY1dn33X2wJyIvL14v+OSYsLWLu5lOxPVe3xu+1evo=; b=q3dNV2ElRnWd egbAGqiBqD1eRnWONNcGu7Eo8K5a5swAZdHFTJGgysDWGt1UkFmop51gspX89lre 7FGmBsU6eiwTmYjmJ1ZNwrUI4H9193s94K+2MMOVFFn4rBD1JCXUpNVRnv3hKYcr 6sRPTsrnckWWdQWqNA4bxXYLHUR/OYMqbvUR2iYCR6LX0CyYZzknmSd0N7+e3mO6 qNYBWIFiJDt8WgJLEaYdbTb1mAn67ucOIN8Sg2QsHQriP0+e38myItve+JXOIwTk n7UbfvAKrdwwOosb7Oc/BjvxI79t48NKbyPL/FkSY4gZ4xF5PMUUNvieN2dLSUkG d3eDNbX6MycOhMYhs9BSKH6MGpqh+yw1xXfV6hw+2bsua5kah9CwajlTqu3Z5LBu VCkQXwbglY2R8pliMwTV6eHmhPYkApOgrduJIa/ppGjEZziyaar4Zekxu6mDop2S ATfio2PcX1gSVaqKzVqhkW/lAq4Ppn9symNEUVKoieOP2QrU6rCUsyk+yzR6b4jP PnY3ScPL9mP+jua/uvJTTRLznQv29D8MgWMp9HPVxeoBCta3j7KmzrLcW/BVisg2 vALlHd7Ubh7Q/LF6sPCqqB27XenCEx/tOk/ziaog68/uvjRtafpGpimKbXnRJQDT WC+4cJ5jfEpPMKySc/Mvd0pgDuxwLrs= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out003.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id o1J6KvNisrol for ; Sun, 20 Jun 2021 17:12:25 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id DE593407B7 for ; Sun, 20 Jun 2021 17:12:24 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id F2A8A80F; Sun, 20 Jun 2021 17:12:23 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 08/12] convert: release strbuf to avoid leak Date: Sun, 20 Jun 2021 17:12:00 +0200 Message-Id: <20210620151204.19260-9-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt apply_multi_file_filter and async_query_available_blobs both query subprocess output using subprocess_read_status, which writes data into the identically named filter_status strbuf. We add a strbuf_release to avoid leaking their contents. Leak output seen when running t0021 with LSAN: Direct leak of 24 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa8c2b5 in xrealloc wrapper.c:126:8 #2 0x9ff99d in strbuf_grow strbuf.c:98:2 #3 0x9ff99d in strbuf_addbuf strbuf.c:304:2 #4 0xa101d6 in subprocess_read_status sub-process.c:45:5 #5 0x77793c in apply_multi_file_filter convert.c:886:8 #6 0x77793c in apply_filter convert.c:1042:10 #7 0x77a0b5 in convert_to_git_filter_fd convert.c:1492:7 #8 0x8b48cd in index_stream_convert_blob object-file.c:2156:2 #9 0x8b48cd in index_fd object-file.c:2248:9 #10 0x597411 in hash_fd builtin/hash-object.c:43:9 #11 0x596be1 in hash_object builtin/hash-object.c:59:2 #12 0x596be1 in cmd_hash_object builtin/hash-object.c:153:3 #13 0x4ce83e in run_builtin git.c:475:11 #14 0x4ccafe in handle_builtin git.c:729:3 #15 0x4cb01c in run_argv git.c:818:4 #16 0x4cb01c in cmd_main git.c:949:19 #17 0x6bdc2d in main common-main.c:52:11 #18 0x7f42acf79349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s). Direct leak of 120 byte(s) in 5 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa8c295 in xrealloc wrapper.c:126:8 #2 0x9ff97d in strbuf_grow strbuf.c:98:2 #3 0x9ff97d in strbuf_addbuf strbuf.c:304:2 #4 0xa101b6 in subprocess_read_status sub-process.c:45:5 #5 0x775c73 in async_query_available_blobs convert.c:960:8 #6 0x80029d in finish_delayed_checkout entry.c:183:9 #7 0xa65d1e in check_updates unpack-trees.c:493:10 #8 0xa5f469 in unpack_trees unpack-trees.c:1747:8 #9 0x525971 in checkout builtin/clone.c:815:6 #10 0x525971 in cmd_clone builtin/clone.c:1409:8 #11 0x4ce83e in run_builtin git.c:475:11 #12 0x4ccafe in handle_builtin git.c:729:3 #13 0x4cb01c in run_argv git.c:818:4 #14 0x4cb01c in cmd_main git.c:949:19 #15 0x6bdc2d in main common-main.c:52:11 #16 0x7fa253fce349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 120 byte(s) leaked in 5 allocation(s). Signed-off-by: Andrzej Hunt --- convert.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/convert.c b/convert.c index fd9c84b025..0d6fb3410a 100644 --- a/convert.c +++ b/convert.c @@ -916,6 +916,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len else strbuf_swap(dst, &nbuf); strbuf_release(&nbuf); + strbuf_release(&filter_status); return !err; } @@ -966,6 +967,7 @@ int async_query_available_blobs(const char *cmd, struct string_list *available_p if (err) handle_filter_error(&filter_status, entry, 0); + strbuf_release(&filter_status); return !err; } From patchwork Sun Jun 20 15:12:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333451 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0FC4EC49361 for ; Sun, 20 Jun 2021 15:12:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F197F6109F for ; Sun, 20 Jun 2021 15:12:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230151AbhFTPPH (ORCPT ); Sun, 20 Jun 2021 11:15:07 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:15378 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230061AbhFTPOn (ORCPT ); Sun, 20 Jun 2021 11:14:43 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 344E31303 for ; Sun, 20 Jun 2021 17:12:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201947; x=1626016348; bh=/ggegXae2JaTFVQ3cd/o5SGgrTHuCVFp27usIrpE0pw=; b=sgdUz91qx+HA Ckq3K9yDPBEO9lUyMwFC/ikdgCF6ItZnGtv/JUTLxDtjdfy1wdTgYqINPTTUwj0D kcKsfY7hqlIhKmrjXff0dlWEC5xNDxpBAhkkdwjdEoSNy5yo4gzeA2lY8lKl7IHS aFTIBF0IIAqoV/Qxa+PCy1TpNjmgwlCCTuA6TppfCHXQBX45KjUzcY9P6Pr29JoP rivG2OWQHQQ4mE5kxmdSyF9A8vMn5WLA1dZp8mGQSlopD62EtX+m92GCjR9T5+vL wH5750WUbGsfw/kcJ1FKnA+DzsGTWtZt7methG2w9LDf0i/w2d/EssgU8O85AQol 16SbqL+hvY7QOMZI+TpwMGLfCnECDXCnCiwT7BPOEaw/xPLzGIoSyHtSGpsI5vna L5wlPKOHNkEzKp9MTHY2gGrHv9gZT7DX4eUMprCULegcwhOhwE15xTfT7GHPntAx MH3q6pi9B1pLRmDQS7AZCidnBT3iVPjlsDmtFkJxgMUoVo+smHtN1TJCoRlPn/lQ MIyS2uogyYcqf6Q+qZCNRxg0BcX8HQUxQxENqWVhaVjiqXwPPyBL4xesauI4eUV3 ErPAUmm6uv7VZ4WeiYAPNBfCiBvJS+W6+XixHl+ltmdsoG05qLzd2lI5lZZf9AZj Ha3pdTxTDg+JdzaLTvpf2CxNzMkimRg= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TB_Ma-yYU0QT for ; Sun, 20 Jun 2021 17:12:27 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 86C255C1 for ; Sun, 20 Jun 2021 17:12:27 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx002.mykolab.com (Postfix) with ESMTPS id 6A8CB3357; Sun, 20 Jun 2021 17:12:26 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 09/12] builtin/mv: free or UNLEAK multiple pointers at end of cmd_mv Date: Sun, 20 Jun 2021 17:12:01 +0200 Message-Id: <20210620151204.19260-10-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt These leaks all happen at the end of cmd_mv, hence don't matter in any way. But we still fix the easy ones and squash the rest to get us closer to being able to run tests without leaks. LSAN output from t0050: Direct leak of 384 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa8c015 in xrealloc wrapper.c:126:8 #2 0xa0a7e1 in add_entry string-list.c:44:2 #3 0xa0a7e1 in string_list_insert string-list.c:58:14 #4 0x5dac03 in cmd_mv builtin/mv.c:248:4 #5 0x4ce83e in run_builtin git.c:475:11 #6 0x4ccafe in handle_builtin git.c:729:3 #7 0x4cb01c in run_argv git.c:818:4 #8 0x4cb01c in cmd_main git.c:949:19 #9 0x6bd9ad in main common-main.c:52:11 #10 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x49a82d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3 #1 0xa8bd09 in do_xmalloc wrapper.c:41:8 #2 0x5dbc34 in internal_prefix_pathspec builtin/mv.c:32:2 #3 0x5da575 in cmd_mv builtin/mv.c:158:14 #4 0x4ce83e in run_builtin git.c:475:11 #5 0x4ccafe in handle_builtin git.c:729:3 #6 0x4cb01c in run_argv git.c:818:4 #7 0x4cb01c in cmd_main git.c:949:19 #8 0x6bd9ad in main common-main.c:52:11 #9 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x49a82d in malloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3 #1 0xa8bd09 in do_xmalloc wrapper.c:41:8 #2 0x5dbc34 in internal_prefix_pathspec builtin/mv.c:32:2 #3 0x5da4e4 in cmd_mv builtin/mv.c:148:11 #4 0x4ce83e in run_builtin git.c:475:11 #5 0x4ccafe in handle_builtin git.c:729:3 #6 0x4cb01c in run_argv git.c:818:4 #7 0x4cb01c in cmd_main git.c:949:19 #8 0x6bd9ad in main common-main.c:52:11 #9 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 8 byte(s) in 1 object(s) allocated from: #0 0x49a9a2 in calloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3 #1 0xa8c119 in xcalloc wrapper.c:140:8 #2 0x5da585 in cmd_mv builtin/mv.c:159:22 #3 0x4ce83e in run_builtin git.c:475:11 #4 0x4ccafe in handle_builtin git.c:729:3 #5 0x4cb01c in run_argv git.c:818:4 #6 0x4cb01c in cmd_main git.c:949:19 #7 0x6bd9ad in main common-main.c:52:11 #8 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Direct leak of 4 byte(s) in 1 object(s) allocated from: #0 0x49a9a2 in calloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:154:3 #1 0xa8c119 in xcalloc wrapper.c:140:8 #2 0x5da4f8 in cmd_mv builtin/mv.c:149:10 #3 0x4ce83e in run_builtin git.c:475:11 #4 0x4ccafe in handle_builtin git.c:729:3 #5 0x4cb01c in run_argv git.c:818:4 #6 0x4cb01c in cmd_main git.c:949:19 #7 0x6bd9ad in main common-main.c:52:11 #8 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 65 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa8c015 in xrealloc wrapper.c:126:8 #2 0xa00226 in strbuf_grow strbuf.c:98:2 #3 0xa00226 in strbuf_vaddf strbuf.c:394:3 #4 0xa065c7 in xstrvfmt strbuf.c:981:2 #5 0xa065c7 in xstrfmt strbuf.c:991:8 #6 0x9e7ce7 in prefix_path_gently setup.c:115:15 #7 0x9e7fa6 in prefix_path setup.c:128:12 #8 0x5dbdbf in internal_prefix_pathspec builtin/mv.c:55:23 #9 0x5da575 in cmd_mv builtin/mv.c:158:14 #10 0x4ce83e in run_builtin git.c:475:11 #11 0x4ccafe in handle_builtin git.c:729:3 #12 0x4cb01c in run_argv git.c:818:4 #13 0x4cb01c in cmd_main git.c:949:19 #14 0x6bd9ad in main common-main.c:52:11 #15 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 65 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa8c015 in xrealloc wrapper.c:126:8 #2 0xa00226 in strbuf_grow strbuf.c:98:2 #3 0xa00226 in strbuf_vaddf strbuf.c:394:3 #4 0xa065c7 in xstrvfmt strbuf.c:981:2 #5 0xa065c7 in xstrfmt strbuf.c:991:8 #6 0x9e7ce7 in prefix_path_gently setup.c:115:15 #7 0x9e7fa6 in prefix_path setup.c:128:12 #8 0x5dbdbf in internal_prefix_pathspec builtin/mv.c:55:23 #9 0x5da4e4 in cmd_mv builtin/mv.c:148:11 #10 0x4ce83e in run_builtin git.c:475:11 #11 0x4ccafe in handle_builtin git.c:729:3 #12 0x4cb01c in run_argv git.c:818:4 #13 0x4cb01c in cmd_main git.c:949:19 #14 0x6bd9ad in main common-main.c:52:11 #15 0x7fbfeffc4349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 558 byte(s) leaked in 7 allocation(s). Signed-off-by: Andrzej Hunt --- builtin/mv.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/builtin/mv.c b/builtin/mv.c index 3fccdcb645..c2f96c8e89 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -303,5 +303,10 @@ int cmd_mv(int argc, const char **argv, const char *prefix) COMMIT_LOCK | SKIP_IF_UNCHANGED)) die(_("Unable to write new index file")); + string_list_clear(&src_for_dst, 0); + UNLEAK(source); + UNLEAK(dest_path); + free(submodule_gitfile); + free(modes); return 0; } From patchwork Sun Jun 20 15:12:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333453 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C95DFC49EA2 for ; Sun, 20 Jun 2021 15:12:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0B4B610CD for ; Sun, 20 Jun 2021 15:12:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230087AbhFTPPI (ORCPT ); Sun, 20 Jun 2021 11:15:08 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:7266 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230075AbhFTPOo (ORCPT ); Sun, 20 Jun 2021 11:14:44 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id E1BACED6 for ; Sun, 20 Jun 2021 17:12:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201949; x=1626016350; bh=UDRNazhZu0xBcWUdwt7tGXsgsS8NzC9xF8uRW9jYJLA=; b=Y++Ke/9wI8sb DP3JN4z5FETRt0THhG8P8qZ4AAhY+8PKewhPanxHoIu6D0/kWZd9i3TEOWTVycK7 4fAcLoaRvqOfcabikht2YjfjbMRkzRcQppSrBysaSVga318r8fwHSfRSarkZLf/F f4Txr9bd4sPLWMXXUzCZrF5wH7zL8aqJ2GJ7dzOEUyNGmLbGrDl3WAOmvismnWdw pGjp7FQT24sf6UqWLV7i6XdQXQCaiqvHqRASzD5lZe95bjjS4m6Sa9EeHdh3R+PX /UlNeG8DFoNQaI024gBV1q4I/g4chDzdUX85Vl0P/GhGmP2qDKgec+rXXaBpZbnp HYsXsB4o5TuYoRwLJeRC+yQ6hDSpp2umvW3OGTXJPSWw3GUhf069xG8uzMfyoRid BgbZl6+Y6GBEuhP/o55rPME8AIzGFSjBVsZX7iD4OVmuyz/LnzSURAhUWEvBMXRC 9ahGr7eU9JiSWcK3OVogmYfoGojhM2aQ6qIkjHXA7Xlr7PPl1kz7fg/0SmxsNWum 5ekgw6jl4XVjvXJbGeej15TL48hxNPkETZSCNHxnlWom/2ca5+4mzTSG0CYx+mgq V+4+/hm+JnqnoiIz2pLaCq1VEwW6yNhKVKMTPthQD719usGytbM63gcms2WnWdgo w48bsEBxfJNUNeVWhljG/B8ZFYFzIDg= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out001.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aCdfr1DsBoEN for ; Sun, 20 Jun 2021 17:12:29 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id 6746A660 for ; Sun, 20 Jun 2021 17:12:29 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 20BD534B; Sun, 20 Jun 2021 17:12:28 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 10/12] builtin/merge: free found_ref when done Date: Sun, 20 Jun 2021 17:12:02 +0200 Message-Id: <20210620151204.19260-11-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt merge_name() calls dwim_ref(), which allocates a new string into found_ref. Therefore add a free() to avoid leaking found_ref. LSAN output from t0021: Direct leak of 16 byte(s) in 1 object(s) allocated from: #0 0x486804 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3 #1 0xa8beb8 in xstrdup wrapper.c:29:14 #2 0x954054 in expand_ref refs.c:671:12 #3 0x953cb6 in repo_dwim_ref refs.c:644:22 #4 0x5d3759 in dwim_ref refs.h:162:9 #5 0x5d3759 in merge_name builtin/merge.c:517:6 #6 0x5d3759 in collect_parents builtin/merge.c:1214:5 #7 0x5cf60d in cmd_merge builtin/merge.c:1458:16 #8 0x4ce83e in run_builtin git.c:475:11 #9 0x4ccafe in handle_builtin git.c:729:3 #10 0x4cb01c in run_argv git.c:818:4 #11 0x4cb01c in cmd_main git.c:949:19 #12 0x6bdbfd in main common-main.c:52:11 #13 0x7f0430502349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s). Signed-off-by: Andrzej Hunt --- builtin/merge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/merge.c b/builtin/merge.c index a8a843b1f5..7ad85c044a 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -503,7 +503,7 @@ static void merge_name(const char *remote, struct strbuf *msg) struct strbuf bname = STRBUF_INIT; struct merge_remote_desc *desc; const char *ptr; - char *found_ref; + char *found_ref = NULL; int len, early; strbuf_branchname(&bname, remote, 0); @@ -586,6 +586,7 @@ static void merge_name(const char *remote, struct strbuf *msg) strbuf_addf(msg, "%s\t\tcommit '%s'\n", oid_to_hex(&remote_head->object.oid), remote); cleanup: + free(found_ref); strbuf_release(&buf); strbuf_release(&bname); } From patchwork Sun Jun 20 15:12:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333455 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 382BDC48BDF for ; Sun, 20 Jun 2021 15:12:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1DE0E6109F for ; Sun, 20 Jun 2021 15:12:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230165AbhFTPPK (ORCPT ); Sun, 20 Jun 2021 11:15:10 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:21580 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbhFTPOr (ORCPT ); Sun, 20 Jun 2021 11:14:47 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id 7E42A4112C for ; Sun, 20 Jun 2021 17:12:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201952; x=1626016353; bh=JUXrODa0ozC/XEAws5fQquQbZpv+rVpvh6VHkm9OWtc=; b=RGMRcHgE75tM 2jrmwirfto7o7kkVQnfThwCYn5gECnrm743NgGBgAZh0XhyS/jvBizH7i3awX52x b7Y/2CbsSLkwLEXTuGFWVuvJqgRYeVSpP9MFClegY1xQAdsFEzz/q6jNwSSpG6Z8 p21iqp7t/rKY6r1tyHOraoNayyE3FPav7V/QHO6Way5bZVF+du9OcQtD8fTvTDbr wgrPFoWyPZU+RJ02xcuvOigN1TnZ3RwwW/o5uGlx6Uemqft1bz8p4PTpb7914h4l 6l8YiMeDIvDDtZ9PQUcSenS65iktbUbEbHWPTTV3BPPo6lfR8rmqPsTGjCKTbQaE N15BQZSwenj+QwNUAAbS73ZyFHTODTadhUvXY4ABD9+zTVQ5MuVv3KQrkvWDs3xD x/v1/PNhEQXpMSmJSVbC3J85Jmixz1PWJzgBtG6Gmxk/46AcLlIJKGXzt38Gcyum qqhkdNjxISK+RatWmCGurBWfP1XrBlLaUdTIWNIDqBQyC+bz/4arSECXSZ6LaI9z lppAOm9lLNJaTF/qFl/0D2dofLrOantR6pzfXe8g4b0P/Q26qHjt+TPl1qxH+2y0 5c0425LPm+RyfeBjo5k+iVI0CREdgQ8vV54z33tXk8izmRqLHUyyc3dZzLrkx2t1 Ev0b7tNPsydLbr3lVoKqlMAmVi7t0e8= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out003.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CrazIsYKBBht for ; Sun, 20 Jun 2021 17:12:32 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id D3D9B407B7 for ; Sun, 20 Jun 2021 17:12:31 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 4396F34B; Sun, 20 Jun 2021 17:12:30 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 11/12] builtin/rebase: fix options.strategy memory lifecycle Date: Sun, 20 Jun 2021 17:12:03 +0200 Message-Id: <20210620151204.19260-12-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt This change: - xstrdup()'s all string being used for replace_opts.strategy, to guarantee that replace_opts owns these strings. This is needed because sequencer_remove_state() will free replace_opts.strategy, and it's usually called as part of the usage of replace_opts. - Removes xstrdup()'s being used to populate options.strategy in cmd_rebase(), which avoids leaking options.strategy, even in the case where strategy is never moved/copied into replace_opts. These changes are needed because: - We would always create a new string for options.strategy if we either get a strategy via options (OPT_STRING(...strategy...), or via GIT_TEST_MERGE_ALGORITHM. - But only sometimes is this string copied into replace_opts - in which case it did get free()'d in sequencer_remove_state(). - The rest of the time, the newly allocated string would remain unused, causing a leak. But we can't just add a free because that can result in a double-free in those cases where replace_opts was populated. An alternative approach would be to set options.strategy to NULL when moving the pointer to replace_opts.strategy, combined with always free()'ing options.strategy, but that seems like a more complicated and wasteful approach. This was first seen when running t0021 with LSAN, but t2012 helped catch the fact that we can't just free(options.strategy) at the end of cmd_rebase (as that can cause a double-free). LSAN output from t0021: LSAN output from t0021: Direct leak of 4 byte(s) in 1 object(s) allocated from: #0 0x486804 in strdup ../projects/compiler-rt/lib/asan/asan_interceptors.cpp:452:3 #1 0xa71eb8 in xstrdup wrapper.c:29:14 #2 0x61b1cc in cmd_rebase builtin/rebase.c:1779:22 #3 0x4ce83e in run_builtin git.c:475:11 #4 0x4ccafe in handle_builtin git.c:729:3 #5 0x4cb01c in run_argv git.c:818:4 #6 0x4cb01c in cmd_main git.c:949:19 #7 0x6b3fad in main common-main.c:52:11 #8 0x7f267b512349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s). Signed-off-by: Andrzej Hunt --- builtin/rebase.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 12f093121d..9d81db0f3a 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -139,7 +139,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts) replay.ignore_date = opts->ignore_date; replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt); if (opts->strategy) - replay.strategy = opts->strategy; + replay.strategy = xstrdup_or_null(opts->strategy); else if (!replay.strategy && replay.default_strategy) { replay.strategy = replay.default_strategy; replay.default_strategy = NULL; @@ -1723,7 +1723,6 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } if (options.strategy) { - options.strategy = xstrdup(options.strategy); switch (options.type) { case REBASE_APPLY: die(_("--strategy requires --merge or --interactive")); @@ -1776,7 +1775,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (options.type == REBASE_MERGE && !options.strategy && getenv("GIT_TEST_MERGE_ALGORITHM")) - options.strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM")); + options.strategy = getenv("GIT_TEST_MERGE_ALGORITHM"); switch (options.type) { case REBASE_MERGE: From patchwork Sun Jun 20 15:12:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12333457 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18261C48BDF for ; Sun, 20 Jun 2021 15:13:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F127E6109F for ; Sun, 20 Jun 2021 15:13:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230189AbhFTPPQ (ORCPT ); Sun, 20 Jun 2021 11:15:16 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:15388 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230170AbhFTPOv (ORCPT ); Sun, 20 Jun 2021 11:14:51 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 2E013130B for ; Sun, 20 Jun 2021 17:12:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1624201953; x=1626016354; bh=fxqQkGUklOckyJHTrIVeDmxxwt1yQ8DicutANrUIb9k=; b=ZjGd1Rzq4ZUy BF7tu/5rl3Ekq1jbHbKydIXh7Zp/SlbioJAhO33zEGE6/z0ghnopZFDUwxWJRl3O 8bEPBt7tsYO/A2KtOkWEQBBKcJMh2lYf4pQfK0QpE02OWIa8nDeeoB/TytDUsJ9w FuWGGgcnjUs4kSx+ss728gsTm8vwbS3Jxqk8QH9MPXr163yCV+TErr5ZaU6iGFY/ NSUyIamnPVFI1vHY8d2uDMgWIYuyqSaJlUHz+p8nuc2vdcGyfGZpPQoy3JRk4OOA q2txM4zFtVl0ziNBBneeAFVy/zgEGV5cERSQNh595nen/rg37AahVDyTByqlNC2r rizSCQTdikxQ/xOZ61U2Q5ErPclZyuCxNOcUp18Tx3x34bFD8/HSwW6qYTRXmxKY QfGg/mDMHk0/1K00XwHH9ptoByrfbclNYHQRUcrB0dTVZswTLJ/v8/e2X76uoPZt jgUZGd6TTcWV7HP0vWlajg2+u6IS/fzGSgXtU1sWpM25gfLwIUGiDtyYe7IkAyg1 dzckKfPNQPDjK7zVp+w4Xe0SyufhYU+lvn/7V9vNfQ8nuL+JzBL4wJ1Y01kQUDYJ iPqNYy2QjjgVOmskLHE8DR8C1rs2esbXHHlyf8EH0yBvNFm2vSuRJTBVxcuDYm11 vgvKMQgEyrK3hVKrgUupyQhVskH/NL0= X-Virus-Scanned: amavisd-new at mykolab.com Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id a-cxwJkjfWRS for ; Sun, 20 Jun 2021 17:12:33 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 8BBB85C1 for ; Sun, 20 Jun 2021 17:12:33 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 23D4F34B; Sun, 20 Jun 2021 17:12:32 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org Subject: [PATCH 12/12] reset: clear_unpack_trees_porcelain to plug leak Date: Sun, 20 Jun 2021 17:12:04 +0200 Message-Id: <20210620151204.19260-13-andrzej@ahunt.org> In-Reply-To: <20210620151204.19260-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt setup_unpack_trees_porcelain() populates various fields on unpack_tree_opts, we need to call clear_unpack_trees_porcelain() to avoid leaking them. Specifically, we used to leak unpack_tree_opts.msgs_to_free. We have to do this in leave_reset_head because there are multiple scenarios where unpack_tree_opts has already been configured, followed by a 'goto leave_reset_head'. But we can also 'goto leave_reset_head' prior to having initialised unpack_tree_opts via memset(..., 0, ...). Therefore we also move unpack_tree_opts initialisation to the start of reset_head(), and convert it to use brace initialisation - which guarantees that we can never clear an unitialised unpack_tree_opts. clear_unpack_tree_opts() is always safe to call as long as unpack_tree_opts is at least zero-initialised, i.e. it does not depend on a previous call to setup_unpack_trees_porcelain(). LSAN output from t0021: Direct leak of 192 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa721e5 in xrealloc wrapper.c:126:8 #2 0x9f7861 in strvec_push_nodup strvec.c:19:2 #3 0x9f7861 in strvec_pushf strvec.c:39:2 #4 0xa43e14 in setup_unpack_trees_porcelain unpack-trees.c:129:3 #5 0x97e011 in reset_head reset.c:53:2 #6 0x61dfa5 in cmd_rebase builtin/rebase.c:1991:9 #7 0x4ce83e in run_builtin git.c:475:11 #8 0x4ccafe in handle_builtin git.c:729:3 #9 0x4cb01c in run_argv git.c:818:4 #10 0x4cb01c in cmd_main git.c:949:19 #11 0x6b3f3d in main common-main.c:52:11 #12 0x7fa8addf3349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 147 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa721e5 in xrealloc wrapper.c:126:8 #2 0x9e8d54 in strbuf_grow strbuf.c:98:2 #3 0x9e8d54 in strbuf_vaddf strbuf.c:401:3 #4 0x9f7774 in strvec_pushf strvec.c:36:2 #5 0xa43e14 in setup_unpack_trees_porcelain unpack-trees.c:129:3 #6 0x97e011 in reset_head reset.c:53:2 #7 0x61dfa5 in cmd_rebase builtin/rebase.c:1991:9 #8 0x4ce83e in run_builtin git.c:475:11 #9 0x4ccafe in handle_builtin git.c:729:3 #10 0x4cb01c in run_argv git.c:818:4 #11 0x4cb01c in cmd_main git.c:949:19 #12 0x6b3f3d in main common-main.c:52:11 #13 0x7fa8addf3349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 134 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa721e5 in xrealloc wrapper.c:126:8 #2 0x9e8d54 in strbuf_grow strbuf.c:98:2 #3 0x9e8d54 in strbuf_vaddf strbuf.c:401:3 #4 0x9f7774 in strvec_pushf strvec.c:36:2 #5 0xa43fe4 in setup_unpack_trees_porcelain unpack-trees.c:168:3 #6 0x97e011 in reset_head reset.c:53:2 #7 0x61dfa5 in cmd_rebase builtin/rebase.c:1991:9 #8 0x4ce83e in run_builtin git.c:475:11 #9 0x4ccafe in handle_builtin git.c:729:3 #10 0x4cb01c in run_argv git.c:818:4 #11 0x4cb01c in cmd_main git.c:949:19 #12 0x6b3f3d in main common-main.c:52:11 #13 0x7fa8addf3349 in __libc_start_main (/lib64/libc.so.6+0x24349) Indirect leak of 130 byte(s) in 1 object(s) allocated from: #0 0x49ab49 in realloc ../projects/compiler-rt/lib/asan/asan_malloc_linux.cpp:164:3 #1 0xa721e5 in xrealloc wrapper.c:126:8 #2 0x9e8d54 in strbuf_grow strbuf.c:98:2 #3 0x9e8d54 in strbuf_vaddf strbuf.c:401:3 #4 0x9f7774 in strvec_pushf strvec.c:36:2 #5 0xa43f20 in setup_unpack_trees_porcelain unpack-trees.c:150:3 #6 0x97e011 in reset_head reset.c:53:2 #7 0x61dfa5 in cmd_rebase builtin/rebase.c:1991:9 #8 0x4ce83e in run_builtin git.c:475:11 #9 0x4ccafe in handle_builtin git.c:729:3 #10 0x4cb01c in run_argv git.c:818:4 #11 0x4cb01c in cmd_main git.c:949:19 #12 0x6b3f3d in main common-main.c:52:11 #13 0x7fa8addf3349 in __libc_start_main (/lib64/libc.so.6+0x24349) SUMMARY: AddressSanitizer: 603 byte(s) leaked in 4 allocation(s). Signed-off-by: Andrzej Hunt --- reset.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reset.c b/reset.c index 4bea758053..79310ae071 100644 --- a/reset.c +++ b/reset.c @@ -21,7 +21,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, struct object_id head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; - struct unpack_trees_options unpack_tree_opts; + struct unpack_trees_options unpack_tree_opts = { 0 }; struct tree *tree; const char *reflog_action; struct strbuf msg = STRBUF_INIT; @@ -49,7 +49,6 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, if (refs_only) goto reset_head_refs; - memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); setup_unpack_trees_porcelain(&unpack_tree_opts, action); unpack_tree_opts.head_idx = 1; unpack_tree_opts.src_index = r->index; @@ -134,6 +133,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, leave_reset_head: strbuf_release(&msg); rollback_lock_file(&lock); + clear_unpack_trees_porcelain(&unpack_tree_opts); while (nr) free((void *)desc[--nr].buffer); return ret;