From patchwork Sun Jul 25 13:08:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398105 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 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 7A3FEC4338F for ; Sun, 25 Jul 2021 13:09:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5FE0E60E78 for ; Sun, 25 Jul 2021 13:09:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230471AbhGYM24 (ORCPT ); Sun, 25 Jul 2021 08:28:56 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:26132 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230192AbhGYM2W (ORCPT ); Sun, 25 Jul 2021 08:28:22 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 63A71AB0; Sun, 25 Jul 2021 15:08:37 +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=1627218516; x=1629032917; bh=VqjrI+/TJRXkejnTF3C7M+Xu7sNgcudEVHyrtpY9rDc=; b=T4+z6N16oKZl WKNcmu3IiwlHg/E0zNGR8X8aw4/p3x2byRt3gXQJO/8La0RSLx4c2OrJX9M7F8ci Tj9kFuvCiKJnWwBobpsyflL1qPAPYMmZiYYODypdsLXFRmUJuzCg6e+r2tYSEteD ZR6MRYglQ6Fn3RD/7yBZ5zRojKUwBifbPqihZQuO423eWSA4Ubydt8DX8ZN9PY5L CeBznW7TKSAZlUzq2DH2gnmhCgPN3lpREsmPNBRL3NuCKXMDBYjJZDZhkCnv/xpt Hfb0lWVJ4crJ8NgLBWQGtqJAGMtga6LOOTW/s147ICV5QfNzGC5/lPULJketj4nF dRUcsbHlPXZ0opAtPu2CSF5NjnLNMTJEGZ78RyYshBiNxid99PF6c2jFkaapSWvr spmVTz29lmIqTDP3EQKkOTDmlo6S5qt03X7kkLnBtmCoTCCGsEzKQztEerxzD4GD V2Faf99IFk5mBkOpKMXWBzpcqg32pSPMmUtfBpClxNI3MY+hHjKC6vI8FYJlYgCr bvdS9gTKv39E9cfQxwdacL7P0JXwTlbkEQMi5TH1hnTu930bzqxtTKNqieF+uxZS uDLHz37DgPyIjSqnSy/FqBSFmWCTM7qXmYnWMD5PRpfPqoSNGSnLi93sbhxQPluM 1+F0FOrliQdLP/H09uSVPXWM7tGUbNU= 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 vXNtkQBMku0M; Sun, 25 Jul 2021 15:08:36 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id B4C3C702; Sun, 25 Jul 2021 15:08:36 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 44CB917C; Sun, 25 Jul 2021 15:08:35 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 01/12] fmt-merge-msg: free newly allocated temporary strings when done Date: Sun, 25 Jul 2021 15:08:19 +0200 Message-Id: <20210725130830.5145-2-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 @@ -108,6 +108,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents) 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; @@ -183,12 +184,13 @@ static int handle_line(char *line, struct merge_parents *merge_parents) 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 Jul 25 13:08:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398107 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 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 99A32C432BE for ; Sun, 25 Jul 2021 13:09:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7E2D960E78 for ; Sun, 25 Jul 2021 13:09:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230472AbhGYM27 (ORCPT ); Sun, 25 Jul 2021 08:28:59 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:38618 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230244AbhGYM2W (ORCPT ); Sun, 25 Jul 2021 08:28:22 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id A58E140BCA; Sun, 25 Jul 2021 15:08:40 +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=1627218520; x=1629032921; bh=8/FKFnvqTqw73g96MjnuKPOwSdBPD8pGOoyeNu6u2+Q=; b=vy+5Zv926mRJ XdOC6QPlcWlDTKKBGCenifg5dR+lEw6Zxa8cAwyjnJ63ku0awNJm7jcEh+/RBX32 z27MGT8UzrjrD6tkYnSyld0IsU0FWW7ce/rqtOmXBtErvEtAJ6RQ7bp5zo/u6Nfs CHgNlcnZvtnvTn/KoiaUdCn/nOXqEW4ZXVp+LgYG9Bc5pJ9oTQRcljQj/4slEP4x 2yFVt6eI8LT1bXTP6sQBUBvue+gNXNieWeM0nbdvLYfwH29/i8AQnbmUJQDRgIyy VwpyTD3RHlN2vPFsealnqvExL3Bv30yq+dl9jUcLQLJsr1E3BTEaHwJ49+YqiTyv QiQBAcza+euHULamuKPgPlBfdo5fnChszFWtcmqXH2l79j9f/cGPOVhzg7BQmnVv 1uJH6luikLRmYoV3S+mgeSi9DeIb1dYeV6IJM9LisdwB85g4oqCIZ7FbT70Z22Cm 9dv9qyWmxAmzR1WOb2yZz1p3Rwv5BSfzYvYZXyJpjWovXcswQStvHyMK1t3hORFZ wm4dHptASY5losZphkXj9NY0NVnhoOOH05bXup3OHAtgcPxvU+4yHyXkQsD4mBLU 7u5qdiaYcaiFxAz1L46ZOwsgvZX/NWTdRckubT/uPgA7/eEr4aivjTJkQ0AtUtc3 Pocrdy95xPcfnmG+TKM10R+aRp0r1MY= 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 YpEpx9n8wkwB; Sun, 25 Jul 2021 15:08:40 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 074C0404BD; Sun, 25 Jul 2021 15:08:39 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id D089D17C; Sun, 25 Jul 2021 15:08:37 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 02/12] environment: move strbuf into block to plug leak Date: Sun, 25 Jul 2021 15:08:20 +0200 Message-Id: <20210725130830.5145-3-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 @@ -253,21 +253,20 @@ static int git_work_tree_initialized; */ 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 Jul 25 13:08:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398109 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 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 A97D8C4338F for ; Sun, 25 Jul 2021 13:09:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8C94360E78 for ; Sun, 25 Jul 2021 13:09:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230328AbhGYM3D (ORCPT ); Sun, 25 Jul 2021 08:29:03 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:12488 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230343AbhGYM2n (ORCPT ); Sun, 25 Jul 2021 08:28:43 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 6D3521323; Sun, 25 Jul 2021 15:08:43 +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=1627218523; x=1629032924; bh=N61Kbb5rr9mc5ydLsEy0KFYLJcLMsEhXtxob2QT9x78=; b=LTdaw75Cw1d7 ZGDXbQDAZuEdqXeXWvfYPSvMBJwjKRkg6Fupyxhjt8UBnAqGbZ45XXArEk7anwRb ph1WxNBCriovDe35B02yexNRRPSmzZL15oteCdzKx4KKYyoT61vxI0nz0UncfJ19 HM+sHmGOCZJiNqBb9P3UMvhVaT4imjHzk9E2ktHBJEk8lPJcxwXQgtzQPtbTtdZb NrLHFOGpTTvcq/14yiqbXBcy7s04zkQF/RRwbmrk2zLvxSFz8h0+FBCgTSe8EWMn U+8ckISQRgPd9d8cqXnTunnQOHVbDp/L5JdBd5eddFpTfla7Apl8N2wvBQz4q6Ey AyapRYRiicHep1SKbb7Huk39bw+UY0kYy4pET4I4aGzVJQ3QCy6wengmF/3Z+AKO YDi6B1qzUxcJPk2u6lX7kGaT5eJk7+J419McJVwBbPG9d26homtAcGQ1ssT9KbN8 oQz8BP8c5ZB+DzeWpYXSeC8qCQQzruu1soQpqH7tA20D+Oaw6scwZbYf418oTRrP ZYibPu4yOZY64zL9IRKVzlqDHsB1GVKUXhPi0IRF3/kANRFH670VcXXc8zvsETVl 67VLsTn5vQ51AR+54CZXvxLiv2+VEAMkTkqZcu7BONRY0FLitM/OhjzpesgMwB2h GQFtnHeKOlxzq5vx5epx9kG2Gac1y50= 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 sJNgIdRJIcTA; Sun, 25 Jul 2021 15:08:43 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id DD31FBCD; Sun, 25 Jul 2021 15:08:42 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx003.mykolab.com (Postfix) with ESMTPS id AE9792469; Sun, 25 Jul 2021 15:08:40 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 03/12] builtin/submodule--helper: release unused strbuf to avoid leak Date: Sun, 25 Jul 2021 15:08:21 +0200 Message-Id: <20210725130830.5145-4-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 414fcb63ea..528a78eed8 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -187,11 +187,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 Jul 25 13:08:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398111 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 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 56272C432BE for ; Sun, 25 Jul 2021 13:09:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DD9260F11 for ; Sun, 25 Jul 2021 13:09:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231163AbhGYM3L (ORCPT ); Sun, 25 Jul 2021 08:29:11 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:26146 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230356AbhGYM2n (ORCPT ); Sun, 25 Jul 2021 08:28:43 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 8A363D65; Sun, 25 Jul 2021 15:08:45 +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=1627218525; x=1629032926; bh=GH+iH7ohguPndgzqgfRzdQnYPzn47Zsc5b/cyc6Q3LE=; b=gIXXpUxtdpfD 19GNDEK0jTGxwqE493m1rGhJ52N//NRwQd2LgQe7kdModg5nd5RzUB3N6zejvwBV oWuP0SfohTpE3g2e0hxILoIijDlQ3Xb6PEeH1Yh6d9DW+oQQOvk15W8ceEehZEcz czu9MfNI84xqxWHlIbSM8+p1383NP8JdyktPSqWglV1SMVqB3vy3faQTVLLm6Xt5 ZPipFG7shBPJjbdjg5fFB55/tka0JTc8pmbLH7gi42rQ+A11IhlCjfjsvLUQG7hy Cx31FKfG7u3RpHRUdJ+VHc6c65jJNTliBe/FbM/LERqWtv9YDx87KQbiysT8Z7SN eIWbIsjt+2MRNdw2o5h4JZqmOq+Hc3CDIhbxMpakWC1uZESdFENdm+qxr8O/HZUd BTNiQqeyFu+hBni020Ukng5lyFuoX4XmNj6Qd+b+kE2m2kqrymq46UM4Wn+u/PUD SkrMIqDTnBbg7wGID8p3WFH1cExzqWmPCz2uwaur3UXIPjImJ2ybpZgdYGcU3U5j 0e/3wlwonay21AhfmWaFRNA9FRK/sxavsZQ738X4KQ730jwDbcu74CGmg8Hx0zgO zYwOuV5V2JyfVBn3WgqwPZ5cObyCKvMDs95i4XA8Zt5VvgDyBKb84P0FTImowI7P KceK16sNW1jApJ3wxDYdPEeHNWcO5mM= 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 HQZnKwyNYmnE; Sun, 25 Jul 2021 15:08:45 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id C4478702; Sun, 25 Jul 2021 15:08:44 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx002.mykolab.com (Postfix) with ESMTPS id 1107B3F28; Sun, 25 Jul 2021 15:08:42 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 04/12] builtin/for-each-repo: remove unnecessary argv copy to plug leak Date: Sun, 25 Jul 2021 15:08:22 +0200 Message-Id: <20210725130830.5145-5-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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); } @@ -31,7 +29,6 @@ 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"), @@ -45,9 +42,6 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix) 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); @@ -59,7 +53,7 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix) 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 Jul 25 13:08:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398117 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 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 B4469C4338F for ; Sun, 25 Jul 2021 13:09:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 90CB460E78 for ; Sun, 25 Jul 2021 13:09:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230515AbhGYM3S (ORCPT ); Sun, 25 Jul 2021 08:29:18 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:38630 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230407AbhGYM24 (ORCPT ); Sun, 25 Jul 2021 08:28:56 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id 2736E40DA3; Sun, 25 Jul 2021 15:08:47 +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=1627218526; x=1629032927; bh=GNeahixVkafQRAY3BFeO9RDRUhm3PxsrtB3d0UN4CkQ=; b=ZJqy3ZN6rhcs N2lRjfl0QkwIYC0QBuppDwKya+Xn/bvWoh3r+KWhZyHNT51nw+OdkUI7DPZa8drk IXcaDNPawZvs6vNTd1KdCS8XSu6Jbo7JFlSWqChavK4onadS0oWqXaAverrMLkui tWLO3S7tvvv4doD1d9pw9VuNhC6Qyfryyo7c25bf2hRQTelvHMAvzxoC14BrLB1t FMyF0g2LE8KAqrK9VAaGXphMlqpfmCKT6Ya4Btc+iYfexrPvy7/nPmGSOOFxFbHa W89IQoNYmuUjvmteNtRXXBqrrUtiM5/3lHWR2NNhKJaxhGCKwaUMzKCDQnxPGIue YpRFZ4fPxRJHbHwrdj1ZTC/xfxieskzCyl8BvLXXAWoGcErAWNH4P3F2M/HArlU7 0caotgN94ok7LBs5FPjigakdHo7LqRr4ErpdQA5Gs/zPt91F0dpTwhfxSxme8tBU zsP43jPEo4OeXTZJUPrbMUa6ZX84cAshoX+rmLCLiq2hUsrZRp+6v/Z2v8+NyvG3 nWSbfouD8xXXe26lhEsCYqDNon8yZpDqTPFvPUUmJ+YRMotwuIDDC0PyQi5KJO7D fgRq0Gfh5cmWfKq8qSffAZRsV1KNrOzpvxJ/HkTU75nPDuzUvONPtpKZj4/G5YM7 wMT+5OzinkYXgfq5liiEYbC1TIFNrkg= 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 b5KcehDG_CDK; Sun, 25 Jul 2021 15:08:46 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 948DD404BD; Sun, 25 Jul 2021 15:08:46 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 17A3417C; Sun, 25 Jul 2021 15:08:45 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 05/12] diffcore-rename: move old_dir/new_dir definition to plug leak Date: Sun, 25 Jul 2021 15:08:23 +0200 Message-Id: <20210725130830.5145-6-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 2618bb07c1..c95857b51f 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -448,9 +448,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) @@ -475,6 +475,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 Jul 25 13:08:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398113 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 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 6D52AC4320A for ; Sun, 25 Jul 2021 13:09:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4C0F860F39 for ; Sun, 25 Jul 2021 13:09:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231180AbhGYM3N (ORCPT ); Sun, 25 Jul 2021 08:29:13 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:12502 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230390AbhGYM2y (ORCPT ); Sun, 25 Jul 2021 08:28:54 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 4B5BA12A9; Sun, 25 Jul 2021 15:08:50 +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=1627218529; x=1629032930; bh=cjFr4qStq4vm+X6P1fFqaTwl9jK85bEC+EcsER0/dgM=; b=WXy8/WO6XEfg RLVoP7SxBR7Czmi/IGrYM+vU0O5aKw3sBQTLSyGskva6C2aOlJLHa+vqimaH4sYm Fbh6xeJ0N1Dfa6B4nD1MIVmhVK5mRV5rZ7f28uIzbHZTSTRmJCj0FtzSZkafyenj e6pJkAfJ5qbCAOCYCd4yJd0opnprUM5FRndpks3eyeqJGdqqsf19oCLX56SOmz1A 5kMoFWSsg/cAJq1hJ1V1ts+Ocxk60fYjdMD6c7htRV+upGoP/ghMAbMBfFGTy+GI Iq9SBmrmvOpnY0jRIOOkUlWbBP+kWigFXymqpAMsyvhoc8zBFy9GBHNvCjRPjNg+ tx+HWxvoSAmv1SJtXo9QLZU8xmhbsquzdrYkZAkVUSKRfEP9AvubPm2TBTOM6T0W SQ5vRfeukgTifV11A1xAWLojaRjI4phZXgJkAGYTooMDq7IPDDIAZQPiA0JsUGAT uvNxxcwA9Prcg1UbEUUIdfH1eumdeFNYIE9i3xjn45JisU709TQtsTTxo1QgrPwD 46FpTcFbUsk+UKk6aH4CQvhJ7yI3qPwzCIsaONowReXCtPEyPK2HDz1TtojPwAJS ZQ+SEMpXulfsGNpmNFLdvZj7qVls9vWqR97lKoHnghlUVge6nWmMbWT851B7aaEj i+elCsnDuobVIRXWQ8PkuhIe6OBxwdY= 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 eXqGD1Hs9iyO; Sun, 25 Jul 2021 15:08:49 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id C3BE5BCD; Sun, 25 Jul 2021 15:08:49 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx003.mykolab.com (Postfix) with ESMTPS id 8CB2C2469; Sun, 25 Jul 2021 15:08:47 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 06/12] ref-filter: also free head for ATOM_HEAD to avoid leak Date: Sun, 25 Jul 2021 15:08:24 +0200 Message-Id: <20210725130830.5145-7-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 f45d3a1b26..0cfef7b719 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2226,8 +2226,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 Jul 25 13:08:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398115 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 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 566A9C4338F for ; Sun, 25 Jul 2021 13:09:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 33CD760F11 for ; Sun, 25 Jul 2021 13:09:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230260AbhGYM3O (ORCPT ); Sun, 25 Jul 2021 08:29:14 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:26156 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230393AbhGYM24 (ORCPT ); Sun, 25 Jul 2021 08:28:56 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 6B96312AF; Sun, 25 Jul 2021 15:08:51 +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=1627218531; x=1629032932; bh=A4jfMVoN5EZDxkwMeNoVN0kq2ZhI2EM9h6L6F87MdK0=; b=o4GarRYzNCx0 tEG0Iw9M7uQXLuvGx1cgGPZZfpcD+1SlkLXojdhM6M9DkLth7bHbHTv5RDQSwgLc 0GrTYjbEuF/d1ZJonzKD5X2VRDemoZ7toTNPt6KvFqbCUDVunUSxsA2DuXjP8MlJ EhRnp13duR8iRqqqn+Cwu+QL/v4BwBHbApNEEWeVe/FBuvM0Y4PS3P9nFcz0pFAn HSXr5rIK7MK5TCnO32JeQqpZICeON7tVxi0ES2lYN3E0Cjgd69e9LgiTKRWfHJdZ ECgxfxfrwAs0gG7XHgaAQfwblXQI5haWLbNI779NaM7ivSRZvZlt8UFfLcboUuWT 2YNNYya+YnwEc7LXRN0tjrWHep857UMFTm+Jzj+fI1jqT18f0Pl+GcE0C4Cm0Y2/ VaW4knmF7P8CHfEVCFMXYKdcvmIcGnYKHi/e3gk0rac9DyV67W5ik1nyfRyT6fJY G2KM3G4oBvV4sHVbYbjS62CW0kJ74TTIqyrd37waG7KNWRNRFlE577P1gBkY/14e /OMKSlOZIU6xq/VQlBNR/dgDBOhWFxyTwksi0AFKcSx+1I9UtdqqLIHxnKYEaELz d99XZfwVwvtTRljfweJo5cZQQHUJmMFsBIGjyKQwDBijOOCvmicAKuk5z+ZoxLVp ybP+iqijC+gm/IYh5yWPKdq6ySl90R8= 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 OuZiXe8XszjL; Sun, 25 Jul 2021 15:08:51 +0200 (CEST) Received: from int-mx003.mykolab.com (unknown [10.9.13.3]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id D30B5702; Sun, 25 Jul 2021 15:08:50 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx003.mykolab.com (Postfix) with ESMTPS id 4160A3B2A; Sun, 25 Jul 2021 15:08:49 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 07/12] read-cache: call diff_setup_done to avoid leak Date: Sun, 25 Jul 2021 15:08:25 +0200 Message-Id: <20210725130830.5145-8-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 46ccd66f34..83d1817ad0 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2505,6 +2505,7 @@ int repo_index_has_changes(struct repository *repo, 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++) { From patchwork Sun Jul 25 13:08:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398121 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 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 1362AC4338F for ; Sun, 25 Jul 2021 13:10:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF8A560F11 for ; Sun, 25 Jul 2021 13:09:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231143AbhGYM3X (ORCPT ); Sun, 25 Jul 2021 08:29:23 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:38640 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230414AbhGYM25 (ORCPT ); Sun, 25 Jul 2021 08:28:57 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id 267E740F59; Sun, 25 Jul 2021 15:08:54 +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=1627218533; x=1629032934; bh=YIY1dn33X2wJyIvL14v+OSYsLWLu5lOxPVe3xu+1evo=; b=0th43hfqFevs dPZYrxJLb6yhLj/Dt1YwnIt7LimBZMfilX8AHeeAv1l61/HoSAbyx9rSSYus+svh n67LeFXqB2XMfeZMr8FdY13a4nmcAlVD2C80abgH923NUDWY/sgGuC5n+CxS7Usf HlQ8bLEs3VuCM9Lz1ylNeegXBJNvsdg0RlVu+zAU5ilYg0sIuv6Su4lu9mRIpErL G2JewBZZo+C42TDzWI6WUi6ElVcU8y1cSVIQhuCUlVKb8nfIlCg6VyNuxJjNBybP Epe3EECe2uIv8wb/DvHuwnFfwaFpbfzMQ+oyOeEB/NKoqr3lRVmpWnufj9JOZtB3 JSzDB3GleRwzF7RWxX1qpLkoWCMr15jXET/b5Ruuv8mU0Gy0CnMqFx4/LeGn5M9D 5pvrF0L3wl6Hx0E/6BvEsbCJbRuu4RxQonvKJKlcxNRaz20cxAFzzBLSuXJgH3Lg MFj+Ikp03jpqnqpcfQxPAoUjvgsU900/UTGc/FZfP+xYbjBu4wwgJci2GQqtjhgi vNZEdKEL6qLZxJ0S0aWkHxZYa9WGMfZ8ouXYfh/Pcdsn1ifoEWS9z8dRMyTWlxTe 46KtLn2UJiWw2Xc5OqJbDdL4ecbfpUMJ9utl2P1X8nr5JuWaCHtc2s01oo5pKX4U HR7xMlxs0G8bbpaEJ82ZdvM07mKiDPw= 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 YVF__5j1xdQt; Sun, 25 Jul 2021 15:08:53 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 7EE3B404BD; Sun, 25 Jul 2021 15:08:53 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 56AEE17C; Sun, 25 Jul 2021 15:08:51 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 08/12] convert: release strbuf to avoid leak Date: Sun, 25 Jul 2021 15:08:26 +0200 Message-Id: <20210725130830.5145-9-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 Jul 25 13:08:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398119 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 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 BD02BC432BE for ; Sun, 25 Jul 2021 13:09:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A679160E78 for ; Sun, 25 Jul 2021 13:09:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230480AbhGYM3V (ORCPT ); Sun, 25 Jul 2021 08:29:21 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:38650 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230421AbhGYM25 (ORCPT ); Sun, 25 Jul 2021 08:28:57 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id 3314040BCA; Sun, 25 Jul 2021 15:08:56 +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=1627218535; x=1629032936; bh=/ggegXae2JaTFVQ3cd/o5SGgrTHuCVFp27usIrpE0pw=; b=jdJe0vLjjaeS geqBrZpJDGUtDPXUCyFnAaUDikAf+nhMtpQ5z09xcgWWbbh0EWD7VYza99z6YwXh o6s4oypJ/KMDq3SCrZavWIkVUdAFQdAErtSP+pUWA3v6LxJiTRGr/awSRbuGJFnk AOFuV/TM30ATJKYeXgrlQZDA96f4lwD8AMKPBjjyz8Y4HiMlmBO/mWFl9OIbFkF9 6CXZY7rzUTXyi3NNa8eU087b+K9AQBGB26xU1pC5SSfMeI93JpxG4StZH886g7tf e8PkuMseS693syQMUe+3KAmvxMgPFlmtsu0Mai7O/pRo8u6iQQ4bR28m3RrmLUbx MBNIbV9U5vWAOCBj66kDjejpAfsNC6J5H60OJ0S+XB74hWV0qCLyMwut4KAec6RJ IDKbBpj8vt6JCei+ZR5MPjUzYGSF6tx8n9aykiI6ikr9NEHSkmX+OlJ74J/NMwaU d8KLYO5VkLjWpQ12jKwrz1Lbcf0qh7hpE1UWIWTxiNoTgQg4BUNl54DngOtLnFjV SFKl6X652u1t/j5T+mDMYxJl8bi5Yog6W63gWGjBTALJIZQjcE01BAvCzfFCpfcQ MqtuVpNiJuJ0HMJpxp9pHeQDax8vknloeiULFZEv/1oecL4oNgNRUIZCy4WZHbyK RQWcHWM1z1hqZvrJGLutC4nk2bAEwNY= 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 F80yMCSGN1lK; Sun, 25 Jul 2021 15:08:55 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 811B4404BD; Sun, 25 Jul 2021 15:08:55 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx002.mykolab.com (Postfix) with ESMTPS id 439653F28; Sun, 25 Jul 2021 15:08:53 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 09/12] builtin/mv: free or UNLEAK multiple pointers at end of cmd_mv Date: Sun, 25 Jul 2021 15:08:27 +0200 Message-Id: <20210725130830.5145-10-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 Jul 25 13:08:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398123 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 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 26466C4338F for ; Sun, 25 Jul 2021 13:10:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0972E60F11 for ; Sun, 25 Jul 2021 13:10:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231156AbhGYM3a (ORCPT ); Sun, 25 Jul 2021 08:29:30 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:26178 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230439AbhGYM27 (ORCPT ); Sun, 25 Jul 2021 08:28:59 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 60D54AB0; Sun, 25 Jul 2021 15:08:58 +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=1627218538; x=1629032939; bh=UDRNazhZu0xBcWUdwt7tGXsgsS8NzC9xF8uRW9jYJLA=; b=tma95Vbs67vA NLpScbqpcyJ+M0+l7f9Y2Yr/q7APqkNmGSjQ3EiWDWGOqIKqFuXeqVH+Js44mAec T4MVkczb+vOYFvIwxT9/FSHkKEtHi6R9Yjct4JCrHdI5APh7u+PU14YtUjTVQdz1 Ao0Sq9Giz+WcuHbZXRPSEPwrE+++PpjVOteecrAJk3wkHkcX906p/BdPR4zr8BP9 91oluE75PRpenifb2fmAZ6EjaqSRS1oukBJqDbVsD3gyWDqVhh2pwWzYrFkBvrsU tFBntAbpvYQKqNEC4Wfp14MmqU0mQ4aTOa9SGexCbAWPiS2bKKKhMGlBlTBdmYi9 Lc/OJvYERT3xL4epea13jct9t3ylpU2tnYPXQv23z2RUUVMa307wxb6nnqFsmVQF qDG3DTt7LUG8HHEb7/W1uPeADRiyuqEMyMA2P/GpFjO4xhVLt1UFhYYiwYwr1uU/ gx7KUhHXT3rp/ps2gFwSoTYKhcyBayKGmQypvqIgWs14+/BUy7lR0J1B2ETiVBMe +HNVEebJRiOtxSXSInv0hY6SBRSXWQrfVnRdmCmXnl4++Gh/eZoh6vfJzbjGl0Xs M4s3QZHUDgLF8YZZX5RyZyllK1UsX4pLKBFlUb99R8nV4XOazs/uCpEGXhipn476 FA8ifBWc/251BUot00sRjyxOKmOSOCE= 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 A_t2TZl90Eru; Sun, 25 Jul 2021 15:08:58 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id C5907702; Sun, 25 Jul 2021 15:08:57 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 4A75817C; Sun, 25 Jul 2021 15:08:55 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 10/12] builtin/merge: free found_ref when done Date: Sun, 25 Jul 2021 15:08:28 +0200 Message-Id: <20210725130830.5145-11-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 Jul 25 13:08:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398127 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 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 637C0C4338F for ; Sun, 25 Jul 2021 13:10:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 45EE260F11 for ; Sun, 25 Jul 2021 13:10:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231196AbhGYM3g (ORCPT ); Sun, 25 Jul 2021 08:29:36 -0400 Received: from mx.kolabnow.com ([95.128.36.40]:38660 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230441AbhGYM27 (ORCPT ); Sun, 25 Jul 2021 08:28:59 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTP id BC2C440F72; Sun, 25 Jul 2021 15:09:00 +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=1627218540; x=1629032941; bh=lPmtv4bRQ09TN5IjM56n2WMLnpAgUIeY4fQlM2QADh8=; b=h0z5Ya6WSBTz kz4lW8yJONi09GA+hyYhf6oHKDvLRWd4UQPFGHMhqWsSHUQft3we+LUW/LFqHSmA rg40u+nqhldzP2LUfbFE4mS8kNOsuufksBa3KxbluUotH5NLaZPW6j6GypD8+ZD2 ktpfq9N35N8B9JRthz6lMZZXHSoVwvb4KKRpO/AMD3EJnfu3o41wPUbyJXX6mMBE hM9Y3vHfFZ0h3AwaGWD1/9nRFq89rR8oAYLMjkBfSM1w+kdtMhf8BMntJW7rv5/Q hdWo+UXhbYPncLRsUB7p2RcBmDwjc226DUhvcDv8ZYnuUcAC9+NRKDLRli1yLkuf KivDs/ch5HK6cDV253bwGY6mCpdxNhrvN9aJzQz4bhzihnkhPaFWJd43ru5q1L/B d1+m9SUOZZNyh0TWBHoEFcz2kN4doWCnQjEI9tFy7KzcJkePX/t7Ktd+TtIp40qf c8nSVtjqRPKkOWd1xZxVg75M/f0TtvfhBXkzP96sO5Kw2v3jVoGVTNTaRnn2mHTe 57cX6MoJtiZ+KQm+NdZ04WSEyktL6fCvWb5AgzzzJnNM6QyLn4UVJkp5o/CzLezb tdLOmwYmditEOO1GEvM+vn6CQ9+aKIue9+MnsWQBRLSOJKJXLw+P3KDgKSHOXc2R QUP+h98WkMz6d4b+2t6oxsGy2doufx8= 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 TLTLWYAx0oQV; Sun, 25 Jul 2021 15:09:00 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out003.mykolab.com (Postfix) with ESMTPS id 3B1AA404BD; Sun, 25 Jul 2021 15:09:00 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 8028649B; Sun, 25 Jul 2021 15:08:57 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 11/12] builtin/rebase: fix options.strategy memory lifecycle Date: Sun, 25 Jul 2021 15:08:29 +0200 Message-Id: <20210725130830.5145-12-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-1-andrzej@ahunt.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Andrzej Hunt - cmd_rebase populates rebase_options.strategy with newly allocated strings, hence we need to free those strings at the end of cmd_rebase to avoid a leak. - In some cases: get_replay_opts() is called, which prepares replay_opts using data from rebase_options. We used to simply copy the pointer from rebase_options.strategy, however that would now result in a double-free because sequencer_remove_state() is eventually used to free replay_opts.strategy. To avoid this we xstrdup() strategy when adding it to replay_opts. The original leak happens because we always populate rebase_options.strategy, but we don't always enter the path that calls get_replay_opts() and later sequencer_remove_state() - in other words we'd always allocate a new string into rebase_options.strategy but only sometimes did we free it. We now make sure that rebase_options and replay_opts both own their own copies of strategy, and each copy is free'd independently. 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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/rebase.c b/builtin/rebase.c index 12f093121d..33e0961900 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; @@ -2109,6 +2109,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) free(options.head_name); free(options.gpg_sign_opt); free(options.cmd); + free(options.strategy); strbuf_release(&options.git_format_patch_opt); free(squash_onto_name); return ret; From patchwork Sun Jul 25 13:08:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrzej Hunt X-Patchwork-Id: 12398125 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 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 6EAF8C4320A for ; Sun, 25 Jul 2021 13:10:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 52D0060F11 for ; Sun, 25 Jul 2021 13:10:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231161AbhGYM3d (ORCPT ); Sun, 25 Jul 2021 08:29:33 -0400 Received: from mx.kolabnow.com ([95.128.36.41]:12532 "EHLO mx.kolabnow.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230445AbhGYM3A (ORCPT ); Sun, 25 Jul 2021 08:29:00 -0400 Received: from localhost (unknown [127.0.0.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTP id 054E11387; Sun, 25 Jul 2021 15:09:03 +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=1627218542; x=1629032943; bh=F31RBPr9F+9zMAXfWJXCalpzC6otZs8pVrfl5kjHlL8=; b=Dwq5GcElVBXa v5CqBPd2CTzc+W1wY8dej+MD42rl9ERb9NnqAyeXbLOqgwIGIDbWlzabPfKQGpsR Hmfx7dXg9vg4kqqoL1t6XJiRa1Avb/bXuaj8VcFiygu6RD6TSJCvpWsQg1GE4bdK WJcOnhW6BB8p2JZEE3JA8fTvb7g1sKlxFE8u/yOJh1fS9wMVuZZUehVfdgabUui1 gQDcAcQQVgAlDzEnpxW1eYqrayjlktKoTV3s+0ADcqxkMBzRkJQiGbrX2fYNEUre i+TQm+V9HsIoIZx+AjjM6gFl7H05oQb1iCW+oG7fKTz1BiFMB/Oj3vuYUIDlylPM +5AczQfO6VJ2S00ATMrqh/3Wl9bR2BkwG3IrKfPXxPpahQbgjgjtrURILZcOeBCr mCzZ1rxcIcuTM9pK1t2L87neMhahkApyzqjcy7EQYK09B6ScacmXlcM9EtZusba4 Ui4gUlAJ8v2hhFCy0/aRAtBgreGHpGalolh69/Va0LhnmLgOVv0WmITJILzEeoG6 Y4zIKEjY1QL46XmE+j2eSa3bDPuCW9rImQlozzFLdHhWdZOaevv1n8q9kT9zrSCp zk6zmdSRebX1evY0upPvJTAQkRyaFD6p6G1rGr7pcjmMsP/VdV8VjALzG7XhWkGP b5s9vzZz383ck5F3DfLuCDN+y6561dw= 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 xUVbmp7jTw-G; Sun, 25 Jul 2021 15:09:02 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 4D610BCD; Sun, 25 Jul 2021 15:09:02 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id E681017C; Sun, 25 Jul 2021 15:08:59 +0200 (CEST) From: andrzej@ahunt.org To: git@vger.kernel.org Cc: andrzej@ahunt.org, phillip.wood123@gmail.com, newren@gmail.com Subject: [PATCH v2 12/12] reset: clear_unpack_trees_porcelain to plug leak Date: Sun, 25 Jul 2021 15:08:30 +0200 Message-Id: <20210725130830.5145-13-andrzej@ahunt.org> In-Reply-To: <20210725130830.5145-1-andrzej@ahunt.org> References: <20210620151204.19260-1-andrzej@ahunt.org> <20210725130830.5145-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 uninitialised 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;