lib: Validate metadata structure more consistently during pull

Previously we were doing e.g. `ot_util_filename_validate()` specifically inline
in dirtree objects, but only *after* writing them into the staging directory (by
default). In (non-default) cases such as not using a transaction, such an object
could be written directly into the repo.

A notable gap here is that `pull-local --untrusted` was *not* doing
this verification, just checksums.  We harden that (and also the
static delta writing path, really *everything* that calls
`ostree_repo_write_metadata()` to also do "structure" validation
which includes path traversal checks.  Basically, let's try hard
to avoid having badly structured objects even in the repo.

One thing that sucks in this patch is that we need to allocate a "bounce buffer"
for metadata in the static delta path, because GVariant imposes alignment
requirements, which I screwed up and didn't fulfill when designing deltas. It
actually didn't matter before because we weren't parsing them, but now we are.
In theory we could check alignment but ...eh, not worth it, at least not until
we change the delta compiler to emit aligned metadata which actually may be
quite tricky.  (Big picture I doubt this really matters much right now
but I'm not going to pull out a profiler yet for this)

The pull test was extended to check we didn't even write a dirtree
with path traversal into the staging directory.

There's a bit of code motion in extracting
`_ostree_validate_structureof_metadata()` from `fsck_metadata_object()`.

Then `_ostree_verify_metadata_object()` builds on that to do checksum
verification too.

Closes: #1412
Approved by: jlebon
This commit is contained in:
Colin Walters authored and Atomic Bot committed 2018-01-12 19:38:34 +00:00
1 parent f3ae36ff43
commit 8e6e64a5ad
8 files changed
+138 -54

No files matched your search

+68
View File
@@ -2071,6 +2071,74 @@ validate_variant (GVariant *variant,
return TRUE;
}
/* TODO: make this public later; just wraps the previously public
* commit/dirtree/dirmeta verifiers.
*/
gboolean
_ostree_validate_structureof_metadata (OstreeObjectType objtype,
GVariant *metadata,
GError **error)
{
g_assert (OSTREE_OBJECT_TYPE_IS_META (objtype));
switch (objtype)
{
case OSTREE_OBJECT_TYPE_COMMIT:
if (!ostree_validate_structureof_commit (metadata, error))
return FALSE;
break;
case OSTREE_OBJECT_TYPE_DIR_TREE:
if (!ostree_validate_structureof_dirtree (metadata, error))
return FALSE;
break;
case OSTREE_OBJECT_TYPE_DIR_META:
if (!ostree_validate_structureof_dirmeta (metadata, error))
return FALSE;
break;
case OSTREE_OBJECT_TYPE_TOMBSTONE_COMMIT:
case OSTREE_OBJECT_TYPE_COMMIT_META:
/* TODO */
break;
case OSTREE_OBJECT_TYPE_FILE:
g_assert_not_reached ();
break;
}
return TRUE;
}
/* Used by fsck as well as pull. Verify the checksum of a metadata object
* and its "structure" or the additional schema we impose on GVariants such
* as ensuring the "ay" checksum entries are of length 32. Another important
* one is checking for path traversal in dirtree objects.
*/
gboolean
_ostree_verify_metadata_object (OstreeObjectType objtype,
const char *expected_checksum,
GVariant *metadata,
GError **error)
{
g_assert (expected_checksum);
g_auto(OtChecksum) hasher = { 0, };
ot_checksum_init (&hasher);
ot_checksum_update (&hasher, g_variant_get_data (metadata), g_variant_get_size (metadata));
char actual_checksum[OSTREE_SHA256_STRING_LEN+1];
ot_checksum_get_hexdigest (&hasher, actual_checksum, sizeof (actual_checksum));
if (!_ostree_compare_object_checksum (objtype, expected_checksum, actual_checksum, error))
return FALSE;
/* Add the checksum + objtype prefix here */
{ const char *error_prefix = glnx_strjoina (expected_checksum, ".", ostree_object_type_to_string (objtype));
GLNX_AUTO_PREFIX_ERROR(error_prefix, error);
if (!_ostree_validate_structureof_metadata (objtype, metadata, error))
return FALSE;
}
return TRUE;
}
/**
* ostree_validate_structureof_commit:
* @commit: A commit object, %OSTREE_OBJECT_TYPE_COMMIT