Skip to content

Commit f8584f3

Browse files
jeffhostetlerdscho
authored andcommitted
Harden gvfs-helper to validate the packfiles in a multipart prefetch response (#571)
Teach `gvfs-helper` to ignore the optional `.idx` files that may be included in a `prefetch` response and always use `git index-pack` to create them from the `.pack` files received in the data stream. This is a little wasteful in terms of client-side compute and of the network bandwidth, but allows us to use the full packfile verification code contained within `git index-pack` to ensure that the received packfiles are valid.
2 parents 3ff6d8e + 85cff8d commit f8584f3

File tree

3 files changed

+176
-37
lines changed

3 files changed

+176
-37
lines changed

gvfs-helper.c

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
// The GVFS Protocol defines this value as a way to
106106
// request cached packfiles NEWER THAN this timestamp.
107107
//
108+
// --max-retries=<n> // defaults to "6"
109+
//
110+
// Number of retries after transient network errors.
111+
// Set to zero to disable such retries.
112+
//
108113
// server
109114
//
110115
// Interactive/sub-process mode. Listen for a series of commands
@@ -2122,7 +2127,6 @@ static void extract_packfile_from_multipack(
21222127
{
21232128
struct ph ph;
21242129
struct tempfile *tempfile_pack = NULL;
2125-
struct tempfile *tempfile_idx = NULL;
21262130
int result = -1;
21272131
int b_no_idx_in_multipack;
21282132
struct object_id packfile_checksum;
@@ -2156,16 +2160,14 @@ static void extract_packfile_from_multipack(
21562160
b_no_idx_in_multipack = (ph.idx_len == maximum_unsigned_value_of_type(uint64_t) ||
21572161
ph.idx_len == 0);
21582162

2159-
if (b_no_idx_in_multipack) {
2160-
my_create_tempfile(status, 0, "pack", &tempfile_pack, NULL, NULL);
2161-
if (!tempfile_pack)
2162-
goto done;
2163-
} else {
2164-
/* create a pair of tempfiles with the same basename */
2165-
my_create_tempfile(status, 0, "pack", &tempfile_pack, "idx", &tempfile_idx);
2166-
if (!tempfile_pack || !tempfile_idx)
2167-
goto done;
2168-
}
2163+
/*
2164+
* We are going to harden `gvfs-helper` here and ignore the .idx file
2165+
* if it is provided and always compute it locally so that we get the
2166+
* added verification that `git index-pack` provides.
2167+
*/
2168+
my_create_tempfile(status, 0, "pack", &tempfile_pack, NULL, NULL);
2169+
if (!tempfile_pack)
2170+
goto done;
21692171

21702172
/*
21712173
* Copy the current packfile from the open stream and capture
@@ -2192,38 +2194,31 @@ static void extract_packfile_from_multipack(
21922194

21932195
oid_to_hex_r(hex_checksum, &packfile_checksum);
21942196

2195-
if (b_no_idx_in_multipack) {
2196-
/*
2197-
* The server did not send the corresponding .idx, so
2198-
* we have to compute it ourselves.
2199-
*/
2200-
strbuf_addbuf(&temp_path_idx, &temp_path_pack);
2201-
strbuf_strip_suffix(&temp_path_idx, ".pack");
2202-
strbuf_addstr(&temp_path_idx, ".idx");
2197+
/*
2198+
* Always compute the .idx file from the .pack file.
2199+
*/
2200+
strbuf_addbuf(&temp_path_idx, &temp_path_pack);
2201+
strbuf_strip_suffix(&temp_path_idx, ".pack");
2202+
strbuf_addstr(&temp_path_idx, ".idx");
22032203

2204-
my_run_index_pack(params, status,
2205-
&temp_path_pack, &temp_path_idx,
2206-
NULL);
2207-
if (status->ec != GH__ERROR_CODE__OK)
2208-
goto done;
2204+
my_run_index_pack(params, status,
2205+
&temp_path_pack, &temp_path_idx,
2206+
NULL);
2207+
if (status->ec != GH__ERROR_CODE__OK)
2208+
goto done;
22092209

2210-
} else {
2210+
if (!b_no_idx_in_multipack) {
22112211
/*
22122212
* Server sent the .idx immediately after the .pack in the
2213-
* data stream. I'm tempted to verify it, but that defeats
2214-
* the purpose of having it cached...
2213+
* data stream. Skip over it.
22152214
*/
2216-
if (my_copy_fd_len(fd_multipack, get_tempfile_fd(tempfile_idx),
2217-
ph.idx_len) < 0) {
2215+
if (lseek(fd_multipack, ph.idx_len, SEEK_CUR) < 0) {
22182216
strbuf_addf(&status->error_message,
2219-
"could not extract index[%d] in multipack",
2217+
"could not skip index[%d] in multipack",
22202218
k);
22212219
status->ec = GH__ERROR_CODE__COULD_NOT_INSTALL_PREFETCH;
22222220
goto done;
22232221
}
2224-
2225-
strbuf_addstr(&temp_path_idx, get_tempfile_path(tempfile_idx));
2226-
close_tempfile_gently(tempfile_idx);
22272222
}
22282223

22292224
strbuf_addf(&buf_timestamp, "%u", (unsigned int)ph.timestamp);
@@ -2239,7 +2234,6 @@ static void extract_packfile_from_multipack(
22392234

22402235
done:
22412236
delete_tempfile(&tempfile_pack);
2242-
delete_tempfile(&tempfile_idx);
22432237
strbuf_release(&temp_path_pack);
22442238
strbuf_release(&temp_path_idx);
22452239
strbuf_release(&final_path_pack);
@@ -3766,6 +3760,8 @@ static enum gh__error_code do_sub_cmd__prefetch(int argc, const char **argv)
37663760
static const char *since_str;
37673761
static struct option prefetch_options[] = {
37683762
OPT_STRING(0, "since", &since_str, N_("since"), N_("seconds since epoch")),
3763+
OPT_INTEGER('r', "max-retries", &gh__cmd_opts.max_retries,
3764+
N_("retries for transient network errors")),
37693765
OPT_END(),
37703766
};
37713767

@@ -3785,6 +3781,8 @@ static enum gh__error_code do_sub_cmd__prefetch(int argc, const char **argv)
37853781
if (my_parse_since(since_str, &seconds_since_epoch))
37863782
die("could not parse 'since' field");
37873783
}
3784+
if (gh__cmd_opts.max_retries < 0)
3785+
gh__cmd_opts.max_retries = 0;
37883786

37893787
finish_init(1);
37903788

t/helper/test-gvfs-protocol.c

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,82 @@ static int ct_pack_sort_compare(const void *_a, const void *_b)
11541154
return (a->ph.timestamp < b->ph.timestamp) ? -1 : (a->ph.timestamp != b->ph.timestamp);
11551155
}
11561156

1157+
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
1158+
1159+
/*
1160+
* Like copy.c:copy_fd(), but corrupt part of the trailing SHA (if the
1161+
* given mayhem key is defined) as we copy it to the destination file.
1162+
*
1163+
* We don't know (or care) if the input file is a pack file or idx
1164+
* file, just that the final bytes are part of a SHA that we can
1165+
* corrupt.
1166+
*/
1167+
static int copy_fd_with_checksum_mayhem(int ifd, int ofd,
1168+
const char *mayhem_key,
1169+
ssize_t nr_wrong_bytes)
1170+
{
1171+
off_t in_cur, in_len;
1172+
ssize_t bytes_to_copy;
1173+
ssize_t bytes_remaining_to_copy;
1174+
char buffer[8192];
1175+
1176+
if (!mayhem_key || !*mayhem_key || !nr_wrong_bytes ||
1177+
!string_list_has_string(&mayhem_list, mayhem_key))
1178+
return copy_fd(ifd, ofd);
1179+
1180+
in_cur = lseek(ifd, 0, SEEK_CUR);
1181+
if (in_cur < 0)
1182+
return in_cur;
1183+
1184+
in_len = lseek(ifd, 0, SEEK_END);
1185+
if (in_len < 0)
1186+
return in_len;
1187+
1188+
if (lseek(ifd, in_cur, SEEK_SET) < 0)
1189+
return -1;
1190+
1191+
/* Copy the entire file except for the last few bytes. */
1192+
1193+
bytes_to_copy = (ssize_t)in_len - nr_wrong_bytes;
1194+
bytes_remaining_to_copy = bytes_to_copy;
1195+
while (bytes_remaining_to_copy) {
1196+
ssize_t to_read = MY_MIN((ssize_t)sizeof(buffer), bytes_remaining_to_copy);
1197+
ssize_t len = xread(ifd, buffer, to_read);
1198+
1199+
if (!len)
1200+
return -1; /* error on unexpected EOF */
1201+
if (len < 0)
1202+
return -1;
1203+
if (write_in_full(ofd, buffer, len) < 0)
1204+
return -1;
1205+
1206+
bytes_remaining_to_copy -= len;
1207+
}
1208+
1209+
/* Read the trailing bytes so that we can alter them before copying. */
1210+
1211+
while (nr_wrong_bytes) {
1212+
ssize_t to_read = MY_MIN((ssize_t)sizeof(buffer), nr_wrong_bytes);
1213+
ssize_t len = xread(ifd, buffer, to_read);
1214+
ssize_t k;
1215+
1216+
if (!len)
1217+
return -1; /* error on unexpected EOF */
1218+
if (len < 0)
1219+
return -1;
1220+
1221+
for (k = 0; k < len; k++)
1222+
buffer[k] ^= 0xff;
1223+
1224+
if (write_in_full(ofd, buffer, len) < 0)
1225+
return -1;
1226+
1227+
nr_wrong_bytes -= len;
1228+
}
1229+
1230+
return 0;
1231+
}
1232+
11571233
static enum worker_result send_ct_item(const struct ct_pack_item *item)
11581234
{
11591235
struct ph ph_le;
@@ -1175,7 +1251,8 @@ static enum worker_result send_ct_item(const struct ct_pack_item *item)
11751251
trace2_printf("%s: sending prefetch pack '%s'", TR2_CAT, item->path_pack.buf);
11761252

11771253
fd_pack = git_open_cloexec(item->path_pack.buf, O_RDONLY);
1178-
if (fd_pack == -1 || copy_fd(fd_pack, 1)) {
1254+
if (fd_pack == -1 ||
1255+
copy_fd_with_checksum_mayhem(fd_pack, 1, "bad_prefetch_pack_sha", 4)) {
11791256
logerror("could not send packfile");
11801257
wr = WR_IO_ERROR;
11811258
goto done;
@@ -1185,7 +1262,8 @@ static enum worker_result send_ct_item(const struct ct_pack_item *item)
11851262
trace2_printf("%s: sending prefetch idx '%s'", TR2_CAT, item->path_idx.buf);
11861263

11871264
fd_idx = git_open_cloexec(item->path_idx.buf, O_RDONLY);
1188-
if (fd_idx == -1 || copy_fd(fd_idx, 1)) {
1265+
if (fd_idx == -1 ||
1266+
copy_fd_with_checksum_mayhem(fd_idx, 1, "bad_prefetch_idx_sha", 4)) {
11891267
logerror("could not send idx");
11901268
wr = WR_IO_ERROR;
11911269
goto done;

t/t5799-gvfs-helper.sh

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ test_expect_success 'duplicate and busy: vfs- packfile' '
13091309
# content matches the requested SHA.
13101310
#
13111311
test_expect_success 'catch corrupted loose object' '
1312-
# test_when_finished "per_test_cleanup" &&
1312+
test_when_finished "per_test_cleanup" &&
13131313
start_gvfs_protocol_server_with_mayhem corrupt_loose &&
13141314
13151315
test_must_fail \
@@ -1332,4 +1332,67 @@ test_expect_success 'catch corrupted loose object' '
13321332
git -C "$REPO_T1" fsck
13331333
'
13341334

1335+
#################################################################
1336+
# Ensure that we can detect when we receive a corrupted packfile
1337+
# from the server. This is not concerned with network IO errors,
1338+
# but rather cases when the cache or origin server generates or
1339+
# sends an invalid packfile.
1340+
#
1341+
# For example, if the server throws an exception and writes the
1342+
# stack trace to the socket rather than or in addition to the
1343+
# packfile content.
1344+
#
1345+
# Or for example, if the packfile on the server's disk is corrupt
1346+
# and it sends it correctly, but the original data was already
1347+
# garbage, so the client still has garbage (and retrying won't
1348+
# help).
1349+
#################################################################
1350+
1351+
# Send corrupt PACK files w/o IDX files (so that `gvfs-helper`
1352+
# must use `index-pack` to create it. (And as a side-effect,
1353+
# validate the PACK file is not corrupt.)
1354+
test_expect_success 'prefetch corrupt pack without idx' '
1355+
test_when_finished "per_test_cleanup" &&
1356+
start_gvfs_protocol_server_with_mayhem \
1357+
bad_prefetch_pack_sha \
1358+
no_prefetch_idx &&
1359+
1360+
test_must_fail \
1361+
git -C "$REPO_T1" gvfs-helper \
1362+
--cache-server=disable \
1363+
--remote=origin \
1364+
--no-progress \
1365+
prefetch \
1366+
--max-retries=0 \
1367+
--since="1000000000" \
1368+
>OUT.output 2>OUT.stderr &&
1369+
1370+
stop_gvfs_protocol_server &&
1371+
1372+
# Verify corruption detected in pack when building
1373+
# local idx file for it.
1374+
1375+
grep -q "error: .* index-pack failed" <OUT.stderr
1376+
'
1377+
1378+
# Send corrupt PACK files with IDX files. Since the cache server
1379+
# sends both, `gvfs-helper` might fail to verify both of them.
1380+
test_expect_success 'prefetch corrupt pack with corrupt idx' '
1381+
test_when_finished "per_test_cleanup" &&
1382+
start_gvfs_protocol_server_with_mayhem \
1383+
bad_prefetch_pack_sha &&
1384+
1385+
test_must_fail \
1386+
git -C "$REPO_T1" gvfs-helper \
1387+
--cache-server=disable \
1388+
--remote=origin \
1389+
--no-progress \
1390+
prefetch \
1391+
--max-retries=0 \
1392+
--since="1000000000" \
1393+
>OUT.output 2>OUT.stderr &&
1394+
1395+
stop_gvfs_protocol_server
1396+
'
1397+
13351398
test_done

0 commit comments

Comments
 (0)