Skip to content

Commit 5ca75fd

Browse files
alexcrichtonpks-t
authored andcommitted
curl_stream: check for -1 after CURLINFO_LASTSOCKET
We're recently trying to upgrade to the current master of libgit2 in Cargo but we're unfortunately hitting a segfault in one of our tests. This particular test is just a small smoke test that https works (e.g. it's configured in libgit2). It attempts to clone from a URL which simply immediately drops connections after they're accepted (e.g. terminate abnormally). We expect to see a standard error from libgit2 but unfortunately we're seeing a segfault. This segfault is happening inside of the `wait_for` function of `curl_stream.c` at the line `FD_SET(fd, &errfd)` because `fd` is -1. This ends up doing an out-of-bounds array access that faults the program. I tracked back to where this -1 came from to the line here (returned by `CURLINFO_LASTSOCKET`) and added a check to return an error.
1 parent 5fe5557 commit 5ca75fd

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/curl_stream.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ static int curls_connect(git_stream *stream)
121121
return seterr_curl(s);
122122
}
123123

124+
if (sockextr == -1) {
125+
giterr_set(GITERR_NET, "curl socket is no longer valid");
126+
return -1;
127+
}
128+
124129
s->socket = sockextr;
125130

126131
if (s->parent.encrypted && failed_cert)
@@ -198,6 +203,7 @@ static int wait_for(curl_socket_t fd, bool reading)
198203
FD_ZERO(&outfd);
199204
FD_ZERO(&errfd);
200205

206+
assert(fd >= 0);
201207
FD_SET(fd, &errfd);
202208
if (reading)
203209
FD_SET(fd, &infd);

0 commit comments

Comments
 (0)