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

@@ -1,6 +1,7 @@
/*
* Copyright © 2007, 2008 Ryan Lortie
* Copyright © 2010 Codethink Limited
* Copyright © 2020 William Manley
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
@@ -266,6 +267,7 @@ gvs_fixed_sized_maybe_get_child (GVariantSerialised value,
value.type_info = g_variant_type_info_element (value.type_info);
g_variant_type_info_ref (value.type_info);
value.depth++;
value.ordered_offsets_up_to = 0;
return value;
}
@@ -297,7 +299,7 @@ gvs_fixed_sized_maybe_serialise (GVariantSerialised value,
{
if (n_children)
{
GVariantSerialised child = { NULL, value.data, value.size, value.depth + 1 };
GVariantSerialised child = { NULL, value.data, value.size, value.depth + 1, 0 };
gvs_filler (&child, children[0]);
}
@@ -319,6 +321,7 @@ gvs_fixed_sized_maybe_is_normal (GVariantSerialised value)
/* proper element size: "Just". recurse to the child. */
value.type_info = g_variant_type_info_element (value.type_info);
value.depth++;
value.ordered_offsets_up_to = 0;
return g_variant_serialised_is_normal (value);
}
@@ -360,6 +363,7 @@ gvs_variable_sized_maybe_get_child (GVariantSerialised value,
value.data = NULL;
value.depth++;
value.ordered_offsets_up_to = 0;
return value;
}
@@ -390,7 +394,7 @@ gvs_variable_sized_maybe_serialise (GVariantSerialised value,
{
if (n_children)
{
GVariantSerialised child = { NULL, value.data, value.size - 1, value.depth + 1 };
GVariantSerialised child = { NULL, value.data, value.size - 1, value.depth + 1, 0 };
/* write the data for the child. */
gvs_filler (&child, children[0]);
@@ -410,6 +414,7 @@ gvs_variable_sized_maybe_is_normal (GVariantSerialised value)
value.type_info = g_variant_type_info_element (value.type_info);
value.size--;
value.depth++;
value.ordered_offsets_up_to = 0;
return g_variant_serialised_is_normal (value);
}
@@ -693,6 +698,32 @@ gvs_variable_sized_array_n_children (GVariantSerialised value)
return gvs_variable_sized_array_get_frame_offsets (value).length;
}
/* Find the index of the first out-of-order element in @data, assuming that
* @data is an array of elements of given @type, starting at index @start and
* containing a further @len-@start elements. */
#define DEFINE_FIND_UNORDERED(type) \
static gsize \
find_unordered_##type (const guint8 *data, gsize start, gsize len) \
{ \
gsize off; \
type current, previous; \
\
memcpy (&previous, data + start * sizeof (current), sizeof (current)); \
for (off = (start + 1) * sizeof (current); off < len * sizeof (current); off += sizeof (current)) \
{ \
memcpy (&current, data + off, sizeof (current)); \
if (current < previous) \
break; \
previous = current; \
} \
return off / sizeof (current) - 1; \
}
DEFINE_FIND_UNORDERED (guint8);
DEFINE_FIND_UNORDERED (guint16);
DEFINE_FIND_UNORDERED (guint32);
DEFINE_FIND_UNORDERED (guint64);
static GVariantSerialised
gvs_variable_sized_array_get_child (GVariantSerialised value,
gsize index_)
@@ -708,6 +739,49 @@ gvs_variable_sized_array_get_child (GVariantSerialised value,
g_variant_type_info_ref (child.type_info);
child.depth = value.depth + 1;
/* If the requested @index_ is beyond the set of indices whose framing offsets
* have been checked, check the remaining offsets to see whether theyre
* normal (in order, no overlapping array elements). */
if (index_ > value.ordered_offsets_up_to)
{
switch (offsets.offset_size)
{
case 1:
{
value.ordered_offsets_up_to = find_unordered_guint8 (
offsets.array, value.ordered_offsets_up_to, index_ + 1);
break;
}
case 2:
{
value.ordered_offsets_up_to = find_unordered_guint16 (
offsets.array, value.ordered_offsets_up_to, index_ + 1);
break;
}
case 4:
{
value.ordered_offsets_up_to = find_unordered_guint32 (
offsets.array, value.ordered_offsets_up_to, index_ + 1);
break;
}
case 8:
{
value.ordered_offsets_up_to = find_unordered_guint64 (
offsets.array, value.ordered_offsets_up_to, index_ + 1);
break;
}
default:
/* gvs_get_offset_size() only returns maximum 8 */
g_assert_not_reached ();
}
}
if (index_ > value.ordered_offsets_up_to)
{
/* Offsets are invalid somewhere, so return an empty child. */
return child;
}
if (index_ > 0)
{
guint alignment;
@@ -842,6 +916,9 @@ gvs_variable_sized_array_is_normal (GVariantSerialised value)
g_assert (offset == offsets.data_size);
/* All offsets have now been checked. */
value.ordered_offsets_up_to = G_MAXSIZE;
return TRUE;
}
@@ -1078,7 +1155,7 @@ gvs_tuple_is_normal (GVariantSerialised value)
for (i = 0; i < length; i++)
{
const GVariantMemberInfo *member_info;
GVariantSerialised child;
GVariantSerialised child = { 0, };
gsize fixed_size;
guint alignment;
gsize end;
@@ -1138,6 +1215,9 @@ gvs_tuple_is_normal (GVariantSerialised value)
offset = end;
}
/* All element bounds have been checked above. */
value.ordered_offsets_up_to = G_MAXSIZE;
{
gsize fixed_size;
guint alignment;