Skip to content

Commit e2e8145

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 c562a11 commit e2e8145

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
@@ -1877,6 +1877,33 @@ static void install_packfile(struct gh__request_params *params,
18771877
child_process_clear(&ip);
18781878
}
18791879

1880+
/*
1881+
* Wrapper for read_loose_object() to read and verify the hash of a
1882+
* loose object, and discard the contents buffer.
1883+
*
1884+
* Returns 0 on success, negative on error (details may be written to stderr).
1885+
*/
1886+
static int verify_loose_object(const char *path,
1887+
const struct object_id *expected_oid)
1888+
{
1889+
enum object_type type;
1890+
void *contents = NULL;
1891+
unsigned long size;
1892+
struct strbuf type_name = STRBUF_INIT;
1893+
int ret;
1894+
struct object_info oi = OBJECT_INFO_INIT;
1895+
struct object_id real_oid = *null_oid();
1896+
oi.typep = &type;
1897+
oi.sizep = &size;
1898+
oi.type_name = &type_name;
1899+
1900+
ret = read_loose_object(path, expected_oid, &real_oid, &contents, &oi);
1901+
if (!ret)
1902+
free(contents);
1903+
1904+
return ret;
1905+
}
1906+
18801907
/*
18811908
* Convert the tempfile into a permanent loose object in the ODB.
18821909
*/
@@ -1908,6 +1935,19 @@ static void install_loose(struct gh__request_params *params,
19081935
strbuf_addstr(&tmp_path, get_tempfile_path(params->tempfile));
19091936
close_tempfile_gently(params->tempfile);
19101937

1938+
/*
1939+
* Compute the hash of the received content (while it is still
1940+
* in a temp file) and verify that it matches the OID that we
1941+
* requested and was not corrupted.
1942+
*/
1943+
if (verify_loose_object(tmp_path.buf, &params->loose_oid)) {
1944+
strbuf_addf(&status->error_message,
1945+
"hash failed for received loose object '%s'",
1946+
oid_to_hex(&params->loose_oid));
1947+
status->ec = GH__ERROR_CODE__COULD_NOT_INSTALL_LOOSE;
1948+
goto cleanup;
1949+
}
1950+
19111951
/*
19121952
* Try to install the tempfile as the actual loose object.
19131953
*

0 commit comments

Comments
 (0)