8000 Merge pull request #6730 from libgit2/ethomson/revparse · libgit2/libgit2@5f9e67a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f9e67a

Browse files
authored
Merge pull request #6730 from libgit2/ethomson/revparse
Correct `git_revparse_single` and add revparse fuzzing
2 parents cbff31d + d353cf4 commit 5f9e67a

File tree

10 files changed

+147
-34
lines changed

10 files changed

+147
-34
lines changed

.github/workflows/main.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ jobs:
137137
ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10
138138
UBSAN_OPTIONS: print_stacktrace=1
139139
os: ubuntu-latest
140+
- name: "Sanitizer (Address)"
141+
id: sanitizer-address
142+
container:
143+
name: noble
144+
env:
145+
CC: clang
146+
CFLAGS: -fsanitize=address -ggdb -fsanitize-blacklist=/home/libgit2/source/script/sanitizers.supp -fno-optimize-sibling-calls -fno-omit-frame-pointer
147+
CMAKE_OPTIONS: -DCMAKE_PREFIX_PATH=/usr/local -DUSE_HTTPS=mbedTLS -DUSE_SHA1=HTTPS -DREGEX_BACKEND=pcre -DDEPRECATE_HARD=ON -DUSE_BUNDLED_ZLIB=ON -DUSE_SSH=ON
148+
CMAKE_GENERATOR: Ninja
149+
SKIP_SSH_TESTS: true
150+
SKIP_NEGOTIATE_TESTS: true
151+
ASAN_SYMBOLIZER_PATH: /usr/bin/llvm-symbolizer-10
152+
UBSAN_OPTIONS: print_stacktrace=1
153+
os: ubuntu-latest
140154
- name: "Sanitizer (UndefinedBehavior)"
141155
id: sanitizer-ub
142156
os: ubuntu-latest

fuzzers/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ foreach(fuzz_target_src ${SRC_FUZZERS})
1212
string(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src})
1313
string(REPLACE "_fuzzer" "" fuzz_name ${fuzz_target_name})
1414

15-
set(${fuzz_target_name}_SOURCES ${fuzz_target_src} ${LIBGIT2_OBJECTS})
15+
set(${fuzz_target_name}_SOURCES
16+
${fuzz_target_src} "fuzzer_utils.c" ${LIBGIT2_OBJECTS})
17+
1618
if(USE_STANDALONE_FUZZERS)
1719
list(APPEND ${fuzz_target_name}_SOURCES "standalone_driver.c")
1820
endif()
21+
1922
add_executable(${fuzz_target_name} ${${fuzz_target_name}_SOURCES})
2023
set_target_properties(${fuzz_target_name} PROPERTIES C_STANDARD 90)
2124

fuzzers/corpora/revparse/head

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HEAD

fuzzers/corpora/revparse/revat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
xxxxxxxxxxxxxxxx@

fuzzers/download_refs_fuzzer.c

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "futils.h"
1717

1818
#include "standalone_driver.h"
19+
#include "fuzzer_utils.h"
1920

2021
#define UNUSED(x) (void)(x)
2122

@@ -157,46 +158,18 @@ static int fuzzer_transport_cb(git_transport **out, git_remote *owner, void *par
157158
return git_transport_smart(out, owner, &def);
158159
}
159160

160-
static void fuzzer_git_abort(const char *op)
161-
{
162-
const git_error *err = git_error_last();
163-
fprintf(stderr, "unexpected libgit error: %s: %s\n",
164-
op, err ? err->message : "<none>");
165-
abort();
166-
}
167-
168161
int LLVMFuzzerInitialize(int *argc, char ***argv)
169162
{
170-
#if defined(_WIN32)
171-
char tmpdir[MAX_PATH], path[MAX_PATH];
172-
173-
if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
174-
abort();
175-
176-
if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
177-
abort();
178-
179-
if (git_futils_mkdir(path, 0700, 0) < 0)
180-
abort();
181-
#else
182-
char path[] = "/tmp/git2.XXXXXX";
183-
184-
if (mkdtemp(path) != path)
185-
abort();
186-
#endif
163+
UNUSED(argc);
164+
UNUSED(argv);
187165

188166
if (git_libgit2_init() < 0)
189167
abort();
190168

191169
if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
192170
abort();
193171

194-
UNUSED(argc);
195-
UNUSED(argv);
196-
197-
if (git_repository_init(&repo, path, 1) < 0)
198-
fuzzer_git_abort("git_repository_init");
199-
172+
repo = fuzzer_repo_init();
200173
return 0;
201174
}
202175

fuzzers/fuzzer_utils.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
#include "git2.h"
13+
#include "futils.h"
14+
15+
#include "fuzzer_utils.h"
16+
17+
void fuzzer_git_abort(const char *op)
18+
{
19+
const git_error *err = git_error_last();
20+
fprintf(stderr, "unexpected libgit error: %s: %s\n",
21+
op, err ? err->message : "<none>");
22+
abort();
23+
}
24+
25+
git_repository *fuzzer_repo_init(void)
26+
{
27+
git_repository *repo;
28+
29+
#if defined(_WIN32)
30+
char tmpdir[MAX_PATH], path[MAX_PATH];
31+
32+
if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
33+
abort();
34+
35+
if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
36+
abort();
37+
38+
if (git_futils_mkdir(path, 0700, 0) < 0)
39+
abort();
40+
#else
41+
char path[] = "/tmp/git2.XXXXXX";
42+
43+
if (mkdtemp(path) != path)
44+
abort();
45+
#endif
46+
47+
if (git_repository_init(&repo, path, 1) < 0)
48+
fuzzer_git_abort("git_repository_init");
49+
50+
return repo;
51+
}

fuzzers/fuzzer_utils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_fuzzer_utils_h__
9+
#define INCLUDE_fuzzer_utils_h__
10+
11+
extern void fuzzer_git_abort(const char *op);
12+
extern git_repository *fuzzer_repo_init(void);
13+
14+
#endif

fuzzers/revparse_fuzzer.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* libgit2 revparse fuzzer target.
3+
*
4+
* Copyright (C) the libgit2 contributors. All rights reserved.
5+
*
6+
* This file is part of libgit2, distributed under the GNU GPL v2 with
7+
* a Linking Exception. For full terms see the included COPYING file.
8+
*/
9+
10+
#include <stdio.h>
11+
#include <string.h>
12+
13+
#include "git2.h"
14+
15+
#include "standalone_driver.h"
16+
#include "fuzzer_utils.h"
17+
18+
#define UNUSED(x) (void)(x)
19+
20+
static git_repository *repo;
21+
22+
int LLVMFuzzerInitialize(int *argc, char ***argv)
23+
{
24+
UNUSED(argc);
25+
UNUSED(argv);
26+
27+
if (git_libgit2_init() < 0)
28+
abort();
29+
30+
if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
31+
abort();
32+
33+
repo = fuzzer_repo_init();
34+
return 0;
35+
}
36+
37+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
38+
{
39+
git_object *obj = NULL;
40+
char *c;
41+
42+
if ((c = calloc(1, size + 1)) == NULL)
43+
abort();
44+
45+
memcpy(c, data, size);
46+
47+
git_revparse_single(&obj, repo, c);
48+
git_object_free(obj);
49+
free(c);
50+
51+
return 0;
52+
}

src/libgit2/revparse.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ static int revparse(
701701
git_object *base_rev = NULL;
702702

703703
bool should_return_reference = true;
704+
bool parsed = false;
704705

705706
GIT_ASSERT_ARG(object_out);
706707
GIT_ASSERT_ARG(reference_out);
@@ -710,7 +711,7 @@ static int revparse(
710711
*object_out = NULL;
711712
*reference_out = NULL;
712713

713-
while (spec[pos]) {
714+
while (!parsed && spec[pos]) {
714715
switch (spec[pos]) {
715716
case '^':
716717
should_return_reference = false;
@@ -817,6 +818,8 @@ static int revparse(
817818
break;
818819
} else if (spec[pos+1] == '\0') {
819820
spec = "HEAD";
821+
identifier_len = 4;
822+
parsed = true;
820823
break;
821824
}
822825
/* fall through */

src/libgit2/transports/smart_pkt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ static int set_data(
232232

233233
GIT_ASSERT_ARG(data);
234234

235-
if ((caps = memchr(line, '\0', len)) != NULL) {
235+
if ((caps = memchr(line, '\0', len)) != NULL &&
236+
len > (size_t)((caps - line) + 1)) {
236237
caps++;
237238

238239
if (strncmp(caps, "object-format=", CONST_STRLEN("object-format=")) == 0)

0 commit comments

Comments
 (0)
0