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;
}

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;

View File

@ -31,6 +31,15 @@ typedef struct
guchar *data;
gsize size;
gsize depth; /* same semantics as GVariant.depth */
/* 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.
*
* This is both read and set by g_variant_serialised_get_child for arrays of
* non-fixed-width types */
gsize ordered_offsets_up_to;
} GVariantSerialised;
/* deserialization */

View File

@ -1,5 +1,6 @@
/*
* Copyright © 2010 Codethink Limited
* Copyright © 2020 William Manley
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
@ -1281,6 +1282,7 @@ random_instance_filler (GVariantSerialised *serialised,
serialised->size = instance->size;
serialised->depth = 0;
serialised->ordered_offsets_up_to = 0;
g_assert_true (serialised->type_info == instance->type_info);
g_assert_cmpuint (serialised->size, ==, instance->size);
@ -5143,6 +5145,47 @@ test_normal_checking_array_offsets (void)
g_variant_unref (variant);
}
/* This is a regression test that we can't have non-normal values that take up
* significantly more space than the normal equivalent, by specifying the
* offset table entries so that array elements overlap.
*
* See https://gitlab.gnome.org/GNOME/glib/-/issues/2121#note_832242 */
static void
test_normal_checking_array_offsets2 (void)
{
const guint8 data[] = {
'h', 'i', '\0',
0x03, 0x00, 0x03,
0x06, 0x00, 0x06,
0x09, 0x00, 0x09,
0x0c, 0x00, 0x0c,
0x0f, 0x00, 0x0f,
0x12, 0x00, 0x12,
0x15, 0x00, 0x15,
};
gsize size = sizeof (data);
const GVariantType *aaaaaaas = G_VARIANT_TYPE ("aaaaaaas");
GVariant *variant = NULL;
GVariant *normal_variant = NULL;
GVariant *expected = NULL;
variant = g_variant_new_from_data (aaaaaaas, data, size, FALSE, NULL, NULL);
g_assert_nonnull (variant);
normal_variant = g_variant_get_normal_form (variant);
g_assert_nonnull (normal_variant);
g_assert_cmpuint (g_variant_get_size (normal_variant), <=, size * 2);
expected = g_variant_new_parsed (
"[[[[[[['hi', '', ''], [], []], [], []], [], []], [], []], [], []], [], []]");
g_assert_cmpvariant (expected, variant);
g_assert_cmpvariant (expected, normal_variant);
g_variant_unref (expected);
g_variant_unref (normal_variant);
g_variant_unref (variant);
}
/* Test that a tuple with invalidly large values in its offset table is
* normalised successfully without looping infinitely. */
static void
@ -5311,6 +5354,8 @@ main (int argc, char **argv)
test_normal_checking_tuples);
g_test_add_func ("/gvariant/normal-checking/array-offsets",
test_normal_checking_array_offsets);
g_test_add_func ("/gvariant/normal-checking/array-offsets2",
test_normal_checking_array_offsets2);
g_test_add_func ("/gvariant/normal-checking/tuple-offsets",
test_normal_checking_tuple_offsets);
g_test_add_func ("/gvariant/normal-checking/empty-object-path",