gvariant: Don’t allow child elements to overlap with each other

If different elements of a variable sized array can overlap with each
other then we can cause a `GVariant` to normalise to a much larger type.

This commit changes the behaviour of `GVariant` with non-normal form data. If
an invalid frame offset is found all subsequent elements are given their
default value.

When retrieving an element at index `n` we scan the frame offsets up to index
`n` and if they are not in order we return an element with the default value
for that type.  This guarantees that elements don't overlap with each
other.  We remember the offset we've scanned up to so we don't need to
repeat this work on subsequent accesses.  We skip these checks for trusted
data.

Unfortunately this makes random access of untrusted data O(n) — at least
on first access.  It doesn't affect the algorithmic complexity of accessing
elements in order, such as when using the `GVariantIter` interface.  Also:
the cost of validation will be amortised as the `GVariant` instance is
continued to be used.

I've implemented this with 4 different functions, 1 for each element size,
rather than looping calling `gvs_read_unaligned_le` in the hope that the
compiler will find it easy to optimise and should produce fairly tight
code.

Fixes: #2121
This commit is contained in:
William Manley
2020-06-29 16:59:44 +01:00
committed by Philip Withnall
parent 298a537d5f
commit ade71fb544
4 changed files with 172 additions and 3 deletions

View File

@@ -67,6 +67,7 @@ struct _GVariant
{
GBytes *bytes;
gconstpointer data;
gsize ordered_offsets_up_to;
} serialised;
struct
@@ -164,6 +165,24 @@ struct _GVariant
* if .data pointed to the appropriate number of nul
* bytes.
*
* .ordered_offsets_up_to: If ordered_offsets_up_to == n this means that all
* the frame offsets up to and including the frame
* offset determining the end of element n are in
* order. This guarantees that the bytes of element
* n don't overlap with any previous element.
*
* For trusted data this is set to G_MAXSIZE and we
* don't check that the frame offsets are in order.
*
* Note: This doesn't imply the offsets are good in
* any way apart from their ordering. In particular
* offsets may be out of bounds for this value or
* may imply that the data overlaps the frame
* offsets themselves.
*
* This field is only relevant for arrays of non
* fixed width types.
*
* .tree: Only valid when the instance is in tree form.
*
* Note that accesses from other threads could result in
@@ -367,6 +386,7 @@ g_variant_to_serialised (GVariant *value)
(gpointer) value->contents.serialised.data,
value->size,
value->depth,
value->contents.serialised.ordered_offsets_up_to,
};
return serialised;
}
@@ -398,6 +418,7 @@ g_variant_serialise (GVariant *value,
serialised.size = value->size;
serialised.data = data;
serialised.depth = value->depth;
serialised.ordered_offsets_up_to = 0;
children = (gpointer *) value->contents.tree.children;
n_children = value->contents.tree.n_children;
@@ -441,6 +462,15 @@ g_variant_fill_gvs (GVariantSerialised *serialised,
g_assert (serialised->size == value->size);
serialised->depth = value->depth;
if (value->state & STATE_SERIALISED)
{
serialised->ordered_offsets_up_to = value->contents.serialised.ordered_offsets_up_to;
}
else
{
serialised->ordered_offsets_up_to = 0;
}
if (serialised->data)
/* g_variant_store() is a public API, so it
* it will reacquire the lock if it needs to.
@@ -483,6 +513,7 @@ g_variant_ensure_serialised (GVariant *value)
bytes = g_bytes_new_take (data, value->size);
value->contents.serialised.data = g_bytes_get_data (bytes, NULL);
value->contents.serialised.bytes = bytes;
value->contents.serialised.ordered_offsets_up_to = G_MAXSIZE;
value->state |= STATE_SERIALISED;
}
}
@@ -563,6 +594,7 @@ g_variant_new_from_bytes (const GVariantType *type,
serialised.type_info = value->type_info;
serialised.data = (guchar *) g_bytes_get_data (bytes, &serialised.size);
serialised.depth = 0;
serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
if (!g_variant_serialised_check (serialised))
{
@@ -613,6 +645,8 @@ g_variant_new_from_bytes (const GVariantType *type,
value->contents.serialised.data = g_bytes_get_data (bytes, &value->size);
}
value->contents.serialised.ordered_offsets_up_to = trusted ? G_MAXSIZE : 0;
g_clear_pointer (&owned_bytes, g_bytes_unref);
return value;
@@ -1132,6 +1166,7 @@ g_variant_get_child_value (GVariant *value,
child->contents.serialised.bytes =
g_bytes_ref (value->contents.serialised.bytes);
child->contents.serialised.data = s_child.data;
child->contents.serialised.ordered_offsets_up_to = s_child.ordered_offsets_up_to;
return child;
}