remote-curl: error on incomplete packet
Currently, remote-curl acts as a proxy and blindly forwards packets between an HTTP server and fetch-pack. In the case of a stateless RPC connection where the connection is terminated with a partially written packet, remote-curl will blindly send the partially written packet before waiting on more input from fetch-pack. Meanwhile, fetch-pack will read the partial packet and continue reading, expecting more input. This results in a deadlock between the two processes. For a stateless connection, inspect packets before sending them and error out if a packet line packet is incomplete. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
101736a14c
commit
74b082ad34
@ -679,9 +679,53 @@ static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct check_pktline_state {
|
||||||
|
char len_buf[4];
|
||||||
|
int len_filled;
|
||||||
|
int remaining;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
|
||||||
|
{
|
||||||
|
while (size) {
|
||||||
|
if (!state->remaining) {
|
||||||
|
int digits_remaining = 4 - state->len_filled;
|
||||||
|
if (digits_remaining > size)
|
||||||
|
digits_remaining = size;
|
||||||
|
memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
|
||||||
|
state->len_filled += digits_remaining;
|
||||||
|
ptr += digits_remaining;
|
||||||
|
size -= digits_remaining;
|
||||||
|
|
||||||
|
if (state->len_filled == 4) {
|
||||||
|
state->remaining = packet_length(state->len_buf);
|
||||||
|
if (state->remaining < 0) {
|
||||||
|
die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
|
||||||
|
} else if (state->remaining < 4) {
|
||||||
|
state->remaining = 0;
|
||||||
|
} else {
|
||||||
|
state->remaining -= 4;
|
||||||
|
}
|
||||||
|
state->len_filled = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state->remaining) {
|
||||||
|
int remaining = state->remaining;
|
||||||
|
if (remaining > size)
|
||||||
|
remaining = size;
|
||||||
|
ptr += remaining;
|
||||||
|
size -= remaining;
|
||||||
|
state->remaining -= remaining;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct rpc_in_data {
|
struct rpc_in_data {
|
||||||
struct rpc_state *rpc;
|
struct rpc_state *rpc;
|
||||||
struct active_request_slot *slot;
|
struct active_request_slot *slot;
|
||||||
|
int check_pktline;
|
||||||
|
struct check_pktline_state pktline_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -702,6 +746,8 @@ static size_t rpc_in(char *ptr, size_t eltsize,
|
|||||||
return size;
|
return size;
|
||||||
if (size)
|
if (size)
|
||||||
data->rpc->any_written = 1;
|
data->rpc->any_written = 1;
|
||||||
|
if (data->check_pktline)
|
||||||
|
check_pktline(&data->pktline_state, ptr, size);
|
||||||
write_or_die(data->rpc->in, ptr, size);
|
write_or_die(data->rpc->in, ptr, size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -778,7 +824,7 @@ static curl_off_t xcurl_off_t(size_t len)
|
|||||||
* If flush_received is true, do not attempt to read any more; just use what's
|
* If flush_received is true, do not attempt to read any more; just use what's
|
||||||
* in rpc->buf.
|
* in rpc->buf.
|
||||||
*/
|
*/
|
||||||
static int post_rpc(struct rpc_state *rpc, int flush_received)
|
static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
|
||||||
{
|
{
|
||||||
struct active_request_slot *slot;
|
struct active_request_slot *slot;
|
||||||
struct curl_slist *headers = http_copy_default_headers();
|
struct curl_slist *headers = http_copy_default_headers();
|
||||||
@ -920,6 +966,8 @@ retry:
|
|||||||
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
|
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
|
||||||
rpc_in_data.rpc = rpc;
|
rpc_in_data.rpc = rpc;
|
||||||
rpc_in_data.slot = slot;
|
rpc_in_data.slot = slot;
|
||||||
|
rpc_in_data.check_pktline = stateless_connect;
|
||||||
|
memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
|
||||||
curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
|
curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
|
||||||
curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
|
curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
|
||||||
|
|
||||||
@ -936,6 +984,11 @@ retry:
|
|||||||
if (!rpc->any_written)
|
if (!rpc->any_written)
|
||||||
err = -1;
|
err = -1;
|
||||||
|
|
||||||
|
if (rpc_in_data.pktline_state.len_filled)
|
||||||
|
err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
|
||||||
|
if (rpc_in_data.pktline_state.remaining)
|
||||||
|
err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
|
||||||
|
|
||||||
curl_slist_free_all(headers);
|
curl_slist_free_all(headers);
|
||||||
free(gzip_body);
|
free(gzip_body);
|
||||||
return err;
|
return err;
|
||||||
@ -985,7 +1038,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
|
|||||||
break;
|
break;
|
||||||
rpc->pos = 0;
|
rpc->pos = 0;
|
||||||
rpc->len = n;
|
rpc->len = n;
|
||||||
err |= post_rpc(rpc, 0);
|
err |= post_rpc(rpc, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(client.in);
|
close(client.in);
|
||||||
@ -1342,7 +1395,7 @@ static int stateless_connect(const char *service_name)
|
|||||||
BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
|
BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
|
||||||
if (status == PACKET_READ_EOF)
|
if (status == PACKET_READ_EOF)
|
||||||
break;
|
break;
|
||||||
if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
|
if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
|
||||||
/* We would have an err here */
|
/* We would have an err here */
|
||||||
break;
|
break;
|
||||||
/* Reset the buffer for next request */
|
/* Reset the buffer for next request */
|
||||||
|
@ -129,6 +129,8 @@ install_script () {
|
|||||||
prepare_httpd() {
|
prepare_httpd() {
|
||||||
mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
|
mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
|
||||||
cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
|
cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
|
||||||
|
install_script incomplete-length-upload-pack-v2-http.sh
|
||||||
|
install_script incomplete-body-upload-pack-v2-http.sh
|
||||||
install_script broken-smart-http.sh
|
install_script broken-smart-http.sh
|
||||||
install_script error-smart-http.sh
|
install_script error-smart-http.sh
|
||||||
install_script error.sh
|
install_script error.sh
|
||||||
|
@ -117,6 +117,8 @@ Alias /auth/dumb/ www/auth/dumb/
|
|||||||
SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
|
SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
|
||||||
SetEnv GIT_HTTP_EXPORT_ALL
|
SetEnv GIT_HTTP_EXPORT_ALL
|
||||||
</LocationMatch>
|
</LocationMatch>
|
||||||
|
ScriptAlias /smart/incomplete_length/git-upload-pack incomplete-length-upload-pack-v2-http.sh/
|
||||||
|
ScriptAlias /smart/incomplete_body/git-upload-pack incomplete-body-upload-pack-v2-http.sh/
|
||||||
ScriptAliasMatch /error_git_upload_pack/(.*)/git-upload-pack error.sh/
|
ScriptAliasMatch /error_git_upload_pack/(.*)/git-upload-pack error.sh/
|
||||||
ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
|
ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
|
||||||
ScriptAlias /broken_smart/ broken-smart-http.sh/
|
ScriptAlias /broken_smart/ broken-smart-http.sh/
|
||||||
@ -126,6 +128,12 @@ ScriptAliasMatch /one_time_perl/(.*) apply-one-time-perl.sh/$1
|
|||||||
<Directory ${GIT_EXEC_PATH}>
|
<Directory ${GIT_EXEC_PATH}>
|
||||||
Options FollowSymlinks
|
Options FollowSymlinks
|
||||||
</Directory>
|
</Directory>
|
||||||
|
<Files incomplete-length-upload-pack-v2-http.sh>
|
||||||
|
Options ExecCGI
|
||||||
|
</Files>
|
||||||
|
<Files incomplete-body-upload-pack-v2-http.sh>
|
||||||
|
Options ExecCGI
|
||||||
|
</Files>
|
||||||
<Files broken-smart-http.sh>
|
<Files broken-smart-http.sh>
|
||||||
Options ExecCGI
|
Options ExecCGI
|
||||||
</Files>
|
</Files>
|
||||||
|
3
t/lib-httpd/incomplete-body-upload-pack-v2-http.sh
Normal file
3
t/lib-httpd/incomplete-body-upload-pack-v2-http.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
printf "Content-Type: text/%s\n" "application/x-git-upload-pack-result"
|
||||||
|
echo
|
||||||
|
printf "%s%s" "0079" "45"
|
3
t/lib-httpd/incomplete-length-upload-pack-v2-http.sh
Normal file
3
t/lib-httpd/incomplete-length-upload-pack-v2-http.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
printf "Content-Type: text/%s\n" "application/x-git-upload-pack-result"
|
||||||
|
echo
|
||||||
|
printf "%s" "00"
|
@ -586,6 +586,40 @@ test_expect_success 'clone with http:// using protocol v2' '
|
|||||||
! grep "Send header: Transfer-Encoding: chunked" log
|
! grep "Send header: Transfer-Encoding: chunked" log
|
||||||
'
|
'
|
||||||
|
|
||||||
|
test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline length' '
|
||||||
|
test_when_finished "rm -f log" &&
|
||||||
|
|
||||||
|
git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" &&
|
||||||
|
test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" file &&
|
||||||
|
|
||||||
|
test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
|
||||||
|
clone "$HTTPD_URL/smart/incomplete_length" incomplete_length_child 2>err &&
|
||||||
|
|
||||||
|
# Client requested to use protocol v2
|
||||||
|
grep "Git-Protocol: version=2" log &&
|
||||||
|
# Server responded using protocol v2
|
||||||
|
grep "git< version 2" log &&
|
||||||
|
# Client reported appropriate failure
|
||||||
|
test_i18ngrep "bytes of length header were received" err
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline body' '
|
||||||
|
test_when_finished "rm -f log" &&
|
||||||
|
|
||||||
|
git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" &&
|
||||||
|
test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" file &&
|
||||||
|
|
||||||
|
test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
|
||||||
|
clone "$HTTPD_URL/smart/incomplete_body" incomplete_body_child 2>err &&
|
||||||
|
|
||||||
|
# Client requested to use protocol v2
|
||||||
|
grep "Git-Protocol: version=2" log &&
|
||||||
|
# Server responded using protocol v2
|
||||||
|
grep "git< version 2" log &&
|
||||||
|
# Client reported appropriate failure
|
||||||
|
test_i18ngrep "bytes of body are still expected" err
|
||||||
|
'
|
||||||
|
|
||||||
test_expect_success 'clone big repository with http:// using protocol v2' '
|
test_expect_success 'clone big repository with http:// using protocol v2' '
|
||||||
test_when_finished "rm -f log" &&
|
test_when_finished "rm -f log" &&
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user