From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Derrick Stolee <derrickstolee@github.com>
Subject: [PATCH 2/5] transport: deep-copy object-filter struct for fetch-pack
Date: Thu, 8 Sep 2022 00:57:29 -0400 [thread overview]
Message-ID: <Yxl2OSCCYDFqCp1/@coredump.intra.peff.net> (raw)
In-Reply-To: <Yxl1BNQoy6Drf0Oe@coredump.intra.peff.net>
When the transport code for the git protocol calls into fetch_pack(), it
has to fill out a fetch_pack_args struct that is mostly taken from the
transport options. We pass along any object-filter data by doing a
struct assignment of the list_objects_filter_options struct. But doing
so isn't safe; it contains allocated pointers in its filter_spec
string_list, which could lead to a double-free if one side mutates or
frees the string_list.
And indeed, the fetch-pack code does clear and rewrite the list via
expand_list_objects_filter_spec(), leaving the transport code with
dangling pointers.
This hasn't been a problem so far, though, because the transport code
doesn't look further at the filter struct. But it should, because in
some cases (when fetch-pack doesn't rewrite the list), it ends up
leaking the string_list.
So let's start by turning this shallow copy into a deep one, which
should let us fix the transport leak in a subsequent patch. Likewise,
we'll free the deep copy we made here when we're done with it (to avoid
leaking).
Note that it would also work to pass fetch-pack a pointer to our filter
struct, rather than a copy. But it's awkward for fetch-pack to take a
pointer in its arg struct; the actual git-fetch-pack command allocates a
fetch_pack_args struct on the stack and expects it to contain the filter
options. It could be rewritten to avoid this, but a deep copy serves our
purposes just as well.
Signed-off-by: Jeff King <peff@peff.net>
---
I wrote up the "pass a pointer" version and it's really not too bad. But
on the other hand, the deep copy here did turn up the bug in the
previous patch (once we start freeing both copies, it caused a
double-free of the sparse_oid_name field).
transport.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/transport.c b/transport.c
index b51e991e44..24540f642a 100644
--- a/transport.c
+++ b/transport.c
@@ -386,7 +386,8 @@ static int fetch_refs_via_pack(struct transport *transport,
args.cloning = transport->cloning;
args.update_shallow = data->options.update_shallow;
args.from_promisor = data->options.from_promisor;
- args.filter_options = data->options.filter_options;
+ list_objects_filter_copy(&args.filter_options,
+ &data->options.filter_options);
args.refetch = data->options.refetch;
args.stateless_rpc = transport->stateless_rpc;
args.server_options = transport->server_options;
@@ -453,6 +454,7 @@ static int fetch_refs_via_pack(struct transport *transport,
free_refs(refs_tmp);
free_refs(refs);
+ list_objects_filter_release(&args.filter_options);
return ret;
}
--
2.37.3.1139.g47294c03c7
next prev parent reply other threads:[~2022-09-08 4:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-08 4:52 [PATCH 0/5] plugging some list-objects-filter leaks Jeff King
2022-09-08 4:54 ` [PATCH 1/5] list_objects_filter_copy(): deep-copy sparse_oid_name field Jeff King
2022-09-08 4:57 ` Jeff King [this message]
2022-09-08 4:58 ` [PATCH 3/5] transport: free filter options in disconnect_git() Jeff King
2022-09-08 5:01 ` [PATCH 4/5] list_objects_filter_options: plug leak of filter_spec strings Jeff King
2022-09-08 5:02 ` [PATCH 5/5] prepare_repo_settings(): plug leak of config values Jeff King
2022-09-09 14:20 ` [PATCH 0/5] plugging some list-objects-filter leaks Derrick Stolee
2022-09-11 4:51 ` Jeff King
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Yxl2OSCCYDFqCp1/@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=derrickstolee@github.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).