Skip to content

Commit 366fcbc

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 <[email protected]>
1 parent e10b4c8 commit 366fcbc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

gvfs-helper.c

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

1893+
/*
1894+
* Wrapper for read_loose_object() to read and verify the hash of a
1895+
* loose object, and discard the contents buffer.
1896+
*
1897+
* Returns 0 on success, negative on error (details may be written to stderr).
1898+
*/
1899+
static int verify_loose_object(const char *path,
1900+
const struct object_id *expected_oid)
1901+
{
1902+
enum object_type type;
1903+
void *contents = NULL;
1904+
unsigned long size;
1905+
int ret;
1906+
struct object_info oi = OBJECT_INFO_INIT;
1907+
struct object_id real_oid = *null_oid(the_hash_algo);
1908+
oi.typep = &type;
1909+
oi.sizep = &size;
1910+
1911+
ret = read_loose_object(the_repository, path, expected_oid, &real_oid, &contents, &oi);
1912+
free(contents);
1913+
1914+
return ret;
1915+
}
1916+
18931917
/*
18941918
* Convert the tempfile into a permanent loose object in the ODB.
18951919
*/
@@ -1921,6 +1945,19 @@ static void install_loose(struct gh__request_params *params,
19211945
strbuf_addstr(&tmp_path, get_tempfile_path(params->tempfile));
19221946
close_tempfile_gently(params->tempfile);
19231947

1948+
/*
1949+
* Compute the hash of the received content (while it is still
1950+
* in a temp file) and verify that it matches the OID that we
1951+
* requested and was not corrupted.
1952+
*/
1953+
if (verify_loose_object(tmp_path.buf, &params->loose_oid)) {
1954+
strbuf_addf(&status->error_message,
1955+
"hash failed for received loose object '%s'",
1956+
oid_to_hex(&params->loose_oid));
1957+
status->ec = GH__ERROR_CODE__COULD_NOT_INSTALL_LOOSE;
1958+
goto cleanup;
1959+
}
1960+
19241961
/*
19251962
* Try to install the tempfile as the actual loose object.
19261963
*

0 commit comments

Comments
 (0)