bundle: do not free uninitialized memory on bad arguments
PATCH 1/1 bundle: do not free uninitialized memory on bad arguments
"git bundle create" gives up with an error when setup_revisions() leaves an argument it does not understand, as in
$ git bundle create private.bundle main --no-such-option error: unrecognized argument: --no-such-option
Since 11ee9a75e7 (bundle: plug leaks in create_bundle(), 2024-06-11), that error jumps to the cleanup at the end of create_bundle(), which clears "revs_copy.pending". But "revs_copy" is only filled from "revs" further down, so the cleanup reads uninitialized stack memory and may free a garbage pointer. valgrind reports "Conditional jump or move depends on uninitialised value(s)" in object_array_clear() and in free(); whether the command crashes depends on what happens to be on the stack.
Give “revs_copy” its value where it is declared, and add a test that fails under valgrind without this fix.
--- bundle.c | 2 +- t/t5607-clone-bundle.sh | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)
bundle.c +1 -1
@@ -492,7 +492,7 @@ int create_bundle(struct repository *r, const char *path,int bundle_fd =-1 ;int bundle_to_stdout;int ref_count =0 ; -struct rev_info revs, revs_copy; +struct rev_info revs, revs_copy = {0 };int min_version =2 ;struct bundle_prerequisites_info bpi;int ret;
t/t5607-clone-bundle.sh +7 -0
@@ -127,6 +127,13 @@ test_expect_success 'failed bundle creation does not leave cruft' ' test_path_is_missing fail.bundle.lock ' +test_expect_success 'an unknown argument after the revisions is an error' ' + test_must_fail git bundle create unknown.bundle main --no-such-option \ + 2>err && + test_grep "unrecognized argument: --no-such-option" err && + test_path_is_missing unknown.bundle +' + test_expect_success 'fetch SHA-1 from bundle' ' test_create_repo foo && test_commit -C foo x &&
base-commit: a018953688f1b10bddf91bff8747068f5f4746a4
Signature · 1 line
2.43.0
Quoted 6 lines · Olof Fairbrook
"git bundle create" gives up with an error when
setup_revisions()leaves an argument it does not understand, as in$ git bundle create private.bundle main --no-such-option error: unrecognized argument: --no-such-option
Quoted 8 lines
Since
11ee9a75e7(bundle: plug leaks increate_bundle(), 2024-06-11), that error jumps to the cleanup at the end ofcreate_bundle(), which clears "revs_copy.pending". But "revs_copy" is only filled from "revs" further down, so the cleanup reads uninitialized stack memory and may free a garbage pointer. valgrind reports "Conditional jump or move depends on uninitialised value(s)" inobject_array_clear()and infree(); whether the command crashes depends on what happens to be on the stack.
Nit: that last sentence is a bit on the verbose side, as it doesn't really give any more useful information. I'd just drop it.
bundle.c int create_bundle(struct repository *r, const char *path, · line 498
Quoted 13 lines
bundle.c +1
-1@@ -492,7 +492,7 @@ int create_bundle(struct repository *r, const char *path,int bundle_fd =-1 ;int bundle_to_stdout;int ref_count =0 ; -struct rev_info revs, revs_copy; +struct rev_info revs, revs_copy = {0 };int min_version =2 ;struct bundle_prerequisites_info bpi;int ret;
Right. And for those wondering: we also release revs and lock on exit, but we always call setup_revisions(&revs) before any goto out and lock is initialized with LOCK_INIT. So both of those are fine.
t/t5607-clone-bundle.sh test_expect_success 'failed bundle creation does not leave cruft' ' · line 132
Quoted 11 lines
t/t5607-clone-bundle.sh +3
-0@@ -127,6 +127,13 @@ test_expect_success 'failed bundle creation does not leave cruft' ' test_path_is_missing fail.bundle.lock ' +test_expect_success 'an unknown argument after the revisions is an error' ' + test_must_fail git bundle create unknown.bundle main --no-such-option \ + 2>err &&
Wrapping only for the redirect is a bit funny, but oh, well.
t/t5607-clone-bundle.sh test_expect_success 'failed bundle creation does not leave cruft' ' · line 139
Quoted 7 lines
t/t5607-clone-bundle.sh +4
-0+ test_grep "unrecognized argument: --no-such-option" err && + test_path_is_missing unknown.bundle +' + test_expect_success 'fetch SHA-1 from bundle' ' test_create_repo foo && test_commit -C foo x &&
I think overall this looks good enough already, and the nits I have raised do not really need to be addressed.
Thanks!
Patrick
Quoted 5 lines · Patrick Steinhardt
Quoted 2 lines
in
object_array_clear()and infree(); whether the command crashes depends on what happens to be on the stack.Nit: that last sentence is a bit on the verbose side, as it doesn't really give any more useful information. I'd just drop it.
Yes, agreed. Just me over explaining. Will keep that in mind for future messages on the list.
Quoted 9 lines
Quoted 1 line
test_commit-C foo x &&I think overall this looks good enough already, and the nits I have raised do not really need to be addressed.
Thanks!
Patrick
Thank you!
Olof
bundle.c int create_bundle(struct repository *r, const char *path, · line 495
Quoted 2 lines
bundle.c +1
-1-struct rev_info revs, revs_copy; +struct rev_info revs, revs_copy = {0 };
I'm not familiar with this code, but the fix seems simple enough and has a test. Looks good.
Harald
Quoted 9 lines · Harald Nordgren
bundle.c int create_bundle(struct repository *r, const char *path, · line 495
Quoted 2 lines
bundle.c +1
-1-struct rev_info revs, revs_copy; +struct rev_info revs, revs_copy = {0 };I'm not familiar with this code, but the fix seems simple enough and has a test. Looks good.
Harald
Thanks for the review!
Olof
bundle.c int create_bundle(struct repository *r, const char *path, · line 495
Quoted 2 lines · Olof Fairbrook
bundle.c +1
-1-struct rev_info revs, revs_copy; +struct rev_info revs, revs_copy = {0 };
Obviously a sane and sensible thing to do.
Will queue. Thanks.