Skip to content

Commit d3df430

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 1eb0318 commit d3df430

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

gvfs-helper.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,33 @@ static void install_packfile(struct gh__request_params *params,
18841884
child_process_clear(&ip);
18851885
}
18861886

1887+
/*
1888+
* Wrapper for read_loose_object() to read and verify the hash of a
1889+
* loose object, and discard the contents buffer.
1890+
*
1891+
* Returns 0 on success, negative on error (details may be written to stderr).
1892+
*/
1893+
static int verify_loose_object(const char *path,
1894+
const struct object_id *expected_oid)
1895+
{
1896+
enum object_type type;
1897+
void *contents = NULL;
1898+
unsigned long size;
1899+
struct strbuf type_name = STRBUF_INIT;
1900+
int ret;
1901+
struct object_info oi = OBJECT_INFO_INIT;
1902+
struct object_id real_oid = *null_oid();
1903+
oi.typep = &type;
1904+
oi.sizep = &size;
1905+
oi.type_name = &type_name;
1906+
1907+
ret = read_loose_object(path, expected_oid, &real_oid, &contents, &oi);
1908+
if (!ret)
1909+
free(contents);
1910+
1911+
return ret;
1912+
}
1913+
18871914
/*
18881915
* Convert the tempfile into a permanent loose object in the ODB.
18891916
*/
@@ -1915,6 +1942,19 @@ static void install_loose(struct gh__request_params *params,
19151942
strbuf_addstr(&tmp_path, get_tempfile_path(params->tempfile));
19161943
close_tempfile_gently(params->tempfile);
19171944

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

0 commit comments

Comments
 (0)