Skip to content

Commit 16c08a7

Browse files
derrickstoleedscho
authored andcommitted
gvfs-helper: verify loose objects after write
It is possible that a loose object that is written from a GVFS protocol "get object" request does not match the expected hash. Error out in this case. 2021-10-30: The prototype for read_loose_object() changed in 31deb28 (fsck: don't hard die on invalid object types, 2021-10-01) and 96e41f5 (fsck: report invalid object type-path combinations, 2021-10-01). Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
1 parent a97f685 commit 16c08a7

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

‎gvfs-helper.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,30 @@ static void install_packfile(struct gh__request_params *params,
18971897
child_process_clear(&ip);
18981898
}
18991899

1900+
/*
1901+
* Wrapper for read_loose_object() to read and verify the hash of a
1902+
* loose object, and discard the contents buffer.
1903+
*
1904+
* Returns 0 on success, negative on error (details may be written to stderr).
1905+
*/
1906+
static int verify_loose_object(const char *path,
1907+
const struct object_id *expected_oid)
1908+
{
1909+
enum object_type type;
1910+
void *contents = NULL;
1911+
unsigned long size;
1912+
int ret;
1913+
struct object_info oi = OBJECT_INFO_INIT;
1914+
struct object_id real_oid = *null_oid(the_hash_algo);
1915+
oi.typep = &type;
1916+
oi.sizep = &size;
1917+
1918+
ret = read_loose_object(the_repository, path, expected_oid, &real_oid, &contents, &oi);
1919+
free(contents);
1920+
1921+
return ret;
1922+
}
1923+
19001924
/*
19011925
* Convert the tempfile into a permanent loose object in the ODB.
19021926
*/
@@ -1928,6 +1952,19 @@ static void install_loose(struct gh__request_params *params,
19281952
strbuf_addstr(&tmp_path, get_tempfile_path(params->tempfile));
19291953
close_tempfile_gently(params->tempfile);
19301954

1955+
/*
1956+
* Compute the hash of the received content (while it is still
1957+
* in a temp file) and verify that it matches the OID that we
1958+
* requested and was not corrupted.
1959+
*/
1960+
if (verify_loose_object(tmp_path.buf, &params->loose_oid)) {
1961+
strbuf_addf(&status->error_message,
1962+
"hash failed for received loose object '%s'",
1963+
oid_to_hex(&params->loose_oid));
1964+
status->ec = GH__ERROR_CODE__COULD_NOT_INSTALL_LOOSE;
1965+
goto cleanup;
1966+
}
1967+
19311968
/*
19321969
* Try to install the tempfile as the actual loose object.
19331970
*

0 commit comments

Comments
 (0)