Blame SOURCES/CVE-2019-20446.patch

f50567
From faec595a1721a2496e9c258917facbb564f85854 Mon Sep 17 00:00:00 2001
f50567
From: rpm-build <rpm-build>
f50567
Date: Wed, 13 May 2020 17:53:13 -0500
f50567
Subject: [PATCH] CVE-2019-20446.patch
f50567
f50567
---
f50567
 librsvg/rsvg-base.c                           |  90 +++++++++---
f50567
 librsvg/rsvg-private.h                        |   5 +-
f50567
 rsvg_internals/src/drawing_ctx.rs             |  23 ++--
f50567
 rsvg_internals/src/structure.rs               |  21 ++-
f50567
 tests/errors.c                                |  52 ++++++-
f50567
 .../errors/308-doubly-recursive-use.svg       |  13 ++
f50567
 tests/fixtures/errors/308-recursive-use.svg   |   9 ++
f50567
 tests/fixtures/errors/308-use-self-ref.svg    |   7 +
f50567
 .../errors/515-pattern-billion-laughs.svg     | 130 ++++++++++++++++++
f50567
 .../errors/515-too-many-elements.svgz         | Bin 0 -> 40811 bytes
f50567
 10 files changed, 310 insertions(+), 40 deletions(-)
f50567
 create mode 100644 tests/fixtures/errors/308-doubly-recursive-use.svg
f50567
 create mode 100644 tests/fixtures/errors/308-recursive-use.svg
f50567
 create mode 100644 tests/fixtures/errors/308-use-self-ref.svg
f50567
 create mode 100644 tests/fixtures/errors/515-pattern-billion-laughs.svg
f50567
 create mode 100644 tests/fixtures/errors/515-too-many-elements.svgz
f50567
f50567
diff --git a/librsvg/rsvg-base.c b/librsvg/rsvg-base.c
f50567
index dbad819..af3d43c 100644
f50567
--- a/librsvg/rsvg-base.c
f50567
+++ b/librsvg/rsvg-base.c
f50567
@@ -431,12 +431,29 @@ node_set_atts (RsvgNode * node, RsvgHandle *handle, const NodeCreator *creator,
f50567
     }
f50567
 }
f50567
 
f50567
+static gboolean
f50567
+loading_limits_exceeded (RsvgHandle *handle)
f50567
+{
f50567
+    /* This is a mitigation for SVG files which create millions of elements
f50567
+     * in an attempt to exhaust memory.  We don't allow loading more than
f50567
+     * this number of elements during the initial streaming load process.
f50567
+     */
f50567
+    return handle->priv->num_loaded_elements > 200000;
f50567
+}
f50567
+
f50567
 static void
f50567
 rsvg_standard_element_start (RsvgHandle *handle, const char *name, RsvgPropertyBag * atts)
f50567
 {
f50567
     const NodeCreator *creator;
f50567
     RsvgNode *newnode = NULL;
f50567
 
f50567
+    if (loading_limits_exceeded (handle)) {
f50567
+        g_set_error (handle->priv->error, RSVG_ERROR, 0, "instancing limit");
f50567
+
f50567
+        xmlStopParser (handle->priv->ctxt);
f50567
+        return;
f50567
+    }
f50567
+
f50567
     creator = get_node_creator_for_element_name (name);
f50567
     g_assert (creator != NULL && creator->create_fn != NULL);
f50567
 
f50567
@@ -456,6 +473,7 @@ rsvg_standard_element_start (RsvgHandle *handle, const char *name, RsvgPropertyB
f50567
         handle->priv->treebase = rsvg_node_ref (newnode);
f50567
     }
f50567
 
f50567
+    handle->priv->num_loaded_elements += 1;
f50567
     handle->priv->currentnode = rsvg_node_ref (newnode);
f50567
 
f50567
     node_set_atts (newnode, handle, creator, atts);
f50567
@@ -1641,6 +1659,52 @@ rsvg_push_discrete_layer (RsvgDrawingCtx * ctx)
f50567
     ctx->render->push_discrete_layer (ctx);
f50567
 }
f50567
 
f50567
+void
f50567
+rsvg_drawing_ctx_increase_num_elements_acquired (RsvgDrawingCtx *draw_ctx)
f50567
+{
f50567
+    draw_ctx->num_elements_acquired++;
f50567
+}
f50567
+
f50567
+/* This is a mitigation for the security-related bugs:
f50567
+ * https://gitlab.gnome.org/GNOME/librsvg/issues/323
f50567
+ * https://gitlab.gnome.org/GNOME/librsvg/issues/515
f50567
+ *
f50567
+ * Imagine the XML [billion laughs attack], but done in SVG's terms:
f50567
+ *
f50567
+ * - #323 above creates deeply nested groups of `<use>` elements.
f50567
+ * The first one references the second one ten times, the second one
f50567
+ * references the third one ten times, and so on.  In the file given,
f50567
+ * this causes 10^17 objects to be rendered.  While this does not
f50567
+ * exhaust memory, it would take a really long time.
f50567
+ *
f50567
+ * - #515 has deeply nested references of `<pattern>` elements.  Each
f50567
+ * object inside each pattern has an attribute
f50567
+ * fill="url(#next_pattern)", so the number of final rendered objects
f50567
+ * grows exponentially.
f50567
+ *
f50567
+ * We deal with both cases by placing a limit on how many references
f50567
+ * will be resolved during the SVG rendering process, that is,
f50567
+ * how many `url(#foo)` will be resolved.
f50567
+ *
f50567
+ * [billion laughs attack]: https://bitbucket.org/tiran/defusedxml
f50567
+ */
f50567
+static gboolean
f50567
+limits_exceeded (RsvgDrawingCtx *draw_ctx)
f50567
+{
f50567
+    return draw_ctx->num_elements_acquired > 500000;
f50567
+}
f50567
+
f50567
+RsvgNode *
f50567
+rsvg_drawing_ctx_acquire_node_ref (RsvgDrawingCtx * ctx, RsvgNode *node)
f50567
+{
f50567
+  if (g_slist_find (ctx->acquired_nodes, node))
f50567
+    return NULL;
f50567
+
f50567
+  ctx->acquired_nodes = g_slist_prepend (ctx->acquired_nodes, node);
f50567
+
f50567
+  return node;
f50567
+}
f50567
+
f50567
 /*
f50567
  * rsvg_drawing_ctx_acquire_node:
f50567
  * @ctx: The drawing context in use
f50567
@@ -1668,16 +1732,15 @@ rsvg_drawing_ctx_acquire_node (RsvgDrawingCtx * ctx, const char *url)
f50567
   if (url == NULL)
f50567
       return NULL;
f50567
 
f50567
+  rsvg_drawing_ctx_increase_num_elements_acquired (ctx);
f50567
+  if (limits_exceeded (ctx))
f50567
+      return NULL;
f50567
+
f50567
   node = rsvg_defs_lookup (ctx->defs, url);
f50567
   if (node == NULL)
f50567
     return NULL;
f50567
 
f50567
-  if (g_slist_find (ctx->acquired_nodes, node))
f50567
-    return NULL;
f50567
-
f50567
-  ctx->acquired_nodes = g_slist_prepend (ctx->acquired_nodes, node);
f50567
-
f50567
-  return node;
f50567
+  return rsvg_drawing_ctx_acquire_node_ref (ctx, node);
f50567
 }
f50567
 
f50567
 /**
f50567
@@ -1734,18 +1797,9 @@ rsvg_drawing_ctx_release_node (RsvgDrawingCtx * ctx, RsvgNode *node)
f50567
   if (node == NULL)
f50567
     return;
f50567
 
f50567
-  g_return_if_fail (ctx->acquired_nodes != NULL);
f50567
-  g_return_if_fail (ctx->acquired_nodes->data == node);
f50567
-
f50567
   ctx->acquired_nodes = g_slist_remove (ctx->acquired_nodes, node);
f50567
 }
f50567
 
f50567
-void
f50567
-rsvg_drawing_ctx_increase_num_elements_rendered_through_use (RsvgDrawingCtx *draw_ctx)
f50567
-{
f50567
-    draw_ctx->num_elements_rendered_through_use++;
f50567
-}
f50567
-
f50567
 void
f50567
 rsvg_drawing_ctx_add_node_and_ancestors_to_stack (RsvgDrawingCtx *draw_ctx, RsvgNode *node)
f50567
 {
f50567
@@ -1759,12 +1813,6 @@ rsvg_drawing_ctx_add_node_and_ancestors_to_stack (RsvgDrawingCtx *draw_ctx, Rsvg
f50567
     }
f50567
 }
f50567
 
f50567
-static gboolean
f50567
-limits_exceeded (RsvgDrawingCtx *draw_ctx)
f50567
-{
f50567
-    return draw_ctx->num_elements_rendered_through_use > 500000;
f50567
-}
f50567
-
f50567
 gboolean
f50567
 rsvg_drawing_ctx_draw_node_from_stack (RsvgDrawingCtx *ctx, RsvgNode *node, int dominate)
f50567
 {
f50567
diff --git a/librsvg/rsvg-private.h b/librsvg/rsvg-private.h
f50567
index aeec8d5..06f4c2b 100644
f50567
--- a/librsvg/rsvg-private.h
f50567
+++ b/librsvg/rsvg-private.h
f50567
@@ -164,6 +164,7 @@ struct RsvgHandlePrivate {
f50567
      */
f50567
     RsvgSaxHandler *handler;
f50567
     int handler_nest;
f50567
+    gsize num_loaded_elements;
f50567
 
f50567
     GHashTable *entities;       /* g_malloc'd string -> xmlEntityPtr */
f50567
 
f50567
@@ -200,7 +201,7 @@ struct RsvgDrawingCtx {
f50567
     RsvgState *state;
f50567
     GError **error;
f50567
     RsvgDefs *defs;
f50567
-    gsize num_elements_rendered_through_use;
f50567
+    gsize num_elements_acquired;
f50567
     PangoContext *pango_context;
f50567
     double dpi_x, dpi_y;
f50567
     RsvgViewBox vb;
f50567
@@ -502,6 +503,8 @@ RsvgNode *rsvg_drawing_ctx_acquire_node         (RsvgDrawingCtx * ctx, const cha
f50567
 G_GNUC_INTERNAL
f50567
 RsvgNode *rsvg_drawing_ctx_acquire_node_of_type (RsvgDrawingCtx * ctx, const char *url, RsvgNodeType type);
f50567
 G_GNUC_INTERNAL
f50567
+RsvgNode *rsvg_drawing_ctx_acquire_node_ref     (RsvgDrawingCtx * ctx, RsvgNode *node);
f50567
+G_GNUC_INTERNAL
f50567
 void rsvg_drawing_ctx_release_node              (RsvgDrawingCtx * ctx, RsvgNode *node);
f50567
 
f50567
 G_GNUC_INTERNAL
f50567
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
f50567
index 79f0c9f..631b073 100644
f50567
--- a/rsvg_internals/src/drawing_ctx.rs
f50567
+++ b/rsvg_internals/src/drawing_ctx.rs
f50567
@@ -32,6 +32,11 @@ extern "C" {
f50567
 
f50567
     fn rsvg_drawing_ctx_pop_view_box(draw_ctx: *const RsvgDrawingCtx);
f50567
 
f50567
+    fn rsvg_drawing_ctx_acquire_node_ref(
f50567
+        draw_ctx: *const RsvgDrawingCtx,
f50567
+        node: *const RsvgNode,
f50567
+    ) -> *mut RsvgNode;
f50567
+
f50567
     fn rsvg_drawing_ctx_acquire_node(
f50567
         draw_ctx: *const RsvgDrawingCtx,
f50567
         url: *const libc::c_char,
f50567
@@ -45,8 +50,6 @@ extern "C" {
f50567
 
f50567
     fn rsvg_drawing_ctx_release_node(draw_ctx: *const RsvgDrawingCtx, node: *mut RsvgNode);
f50567
 
f50567
-    fn rsvg_drawing_ctx_increase_num_elements_rendered_through_use(draw_ctx: *const RsvgDrawingCtx);
f50567
-
f50567
     fn rsvg_drawing_ctx_get_current_state_affine(draw_ctx: *const RsvgDrawingCtx) -> cairo::Matrix;
f50567
 
f50567
     fn rsvg_drawing_ctx_set_current_state_affine(
f50567
@@ -149,6 +152,16 @@ pub fn pop_view_box(draw_ctx: *const RsvgDrawingCtx) {
f50567
     }
f50567
 }
f50567
 
f50567
+pub fn acquire_node_ref(draw_ctx: *const RsvgDrawingCtx, node: *const RsvgNode) -> Option<AcquiredNode> {
f50567
+    let raw_node = unsafe { rsvg_drawing_ctx_acquire_node_ref(draw_ctx, node) };
f50567
+
f50567
+    if raw_node.is_null() {
f50567
+        None
f50567
+    } else {
f50567
+        Some(AcquiredNode(draw_ctx, raw_node))
f50567
+    }
f50567
+}
f50567
+
f50567
 pub fn get_acquired_node(draw_ctx: *const RsvgDrawingCtx, url: &str) -> Option<AcquiredNode> {
f50567
     let raw_node = unsafe { rsvg_drawing_ctx_acquire_node(draw_ctx, str::to_glib_none(url).0) };
f50567
 
f50567
@@ -290,12 +303,6 @@ pub fn state_pop(draw_ctx: *const RsvgDrawingCtx) {
f50567
     }
f50567
 }
f50567
 
f50567
-pub fn increase_num_elements_rendered_through_use(draw_ctx: *const RsvgDrawingCtx) {
f50567
-    unsafe {
f50567
-        rsvg_drawing_ctx_increase_num_elements_rendered_through_use(draw_ctx);
f50567
-    }
f50567
-}
f50567
-
f50567
 pub struct AcquiredNode(*const RsvgDrawingCtx, *mut RsvgNode);
f50567
 
f50567
 impl Drop for AcquiredNode {
f50567
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
f50567
index 71c9ff0..e4234ae 100644
f50567
--- a/rsvg_internals/src/structure.rs
f50567
+++ b/rsvg_internals/src/structure.rs
f50567
@@ -278,6 +278,20 @@ impl NodeTrait for NodeUse {
f50567
             return;
f50567
         }
f50567
 
f50567
+        // <use> is an element that is used directly, unlike
f50567
+        // <pattern>, which is used through a fill="url(#...)"
f50567
+        // reference.  However, <use> will always reference another
f50567
+        // element, potentially itself or an ancestor of itself (or
f50567
+        // another <use> which references the first one, etc.).  So,
f50567
+        // we acquire the <use> element itself so that circular
f50567
+        // references can be caught.
f50567
+        let self_box = box_node(node.clone());
f50567
+        let self_acquired = drawing_ctx::acquire_node_ref(draw_ctx, self_box);
f50567
+        rsvg_node_unref(self_box);
f50567
+        if self_acquired.is_none() {
f50567
+            return;
f50567
+        }
f50567
+
f50567
         let child = if let Some(acquired) =
f50567
             drawing_ctx::get_acquired_node(draw_ctx, link.as_ref().unwrap())
f50567
         {
f50567
@@ -286,13 +300,6 @@ impl NodeTrait for NodeUse {
f50567
             return;
f50567
         };
f50567
 
f50567
-        if Node::is_ancestor(node.clone(), child.clone()) {
f50567
-            // or, if we're <use>'ing ourselves
f50567
-            return;
f50567
-        }
f50567
-
f50567
-        drawing_ctx::increase_num_elements_rendered_through_use(draw_ctx);
f50567
-
f50567
         let nx = self.x.get().normalize(draw_ctx);
f50567
         let ny = self.y.get().normalize(draw_ctx);
f50567
 
f50567
diff --git a/tests/errors.c b/tests/errors.c
f50567
index f370d60..ab5898a 100644
f50567
--- a/tests/errors.c
f50567
+++ b/tests/errors.c
f50567
@@ -22,10 +22,29 @@ get_test_filename (const char *basename) {
f50567
                              basename,
f50567
                              NULL);
f50567
 }
f50567
+
f50567
+static void
f50567
+test_loading_error (gconstpointer data)
f50567
+{
f50567
+    const char *basename = data;
f50567
+    char *filename = get_test_filename (basename);
f50567
+    RsvgHandle *handle;
f50567
+    GError *error = NULL;
f50567
+
f50567
+    handle = rsvg_handle_new_from_file (filename, &error);
f50567
+    g_free (filename);
f50567
+
f50567
+    g_assert (handle == NULL);
f50567
+    g_assert (g_error_matches (error, RSVG_ERROR, RSVG_ERROR_FAILED));
f50567
+
f50567
+    g_error_free (error);
f50567
+}
f50567
+
f50567
 static void
f50567
-test_instancing_limit (void)
f50567
+test_instancing_limit (gconstpointer data)
f50567
 {
f50567
-    char *filename = get_test_filename ("323-nested-use.svg");
f50567
+    const char *basename = data;
f50567
+    char *filename = get_test_filename (basename);
f50567
     RsvgHandle *handle;
f50567
     GError *error = NULL;
f50567
     cairo_surface_t *surf;
f50567
@@ -49,7 +68,34 @@ main (int argc, char **argv)
f50567
 {
f50567
     g_test_init (&argc, &argv, NULL);
f50567
 
f50567
-    g_test_add_func ("/errors/instancing_limit", test_instancing_limit);
f50567
+    g_test_add_data_func_full ("/errors/instancing_limit/323-nested-use.svg",
f50567
+                               "323-nested-use.svg",
f50567
+                               test_instancing_limit,
f50567
+                               NULL);
f50567
+
f50567
+    g_test_add_data_func_full ("/errors/instancing_limit/515-pattern-billion-laughs.svg",
f50567
+                               "515-pattern-billion-laughs.svg",
f50567
+                               test_instancing_limit,
f50567
+                               NULL);
f50567
+
f50567
+    g_test_add_data_func_full ("/errors/instancing_limit/308-use-self-ref.svg",
f50567
+                               "308-use-self-ref.svg",
f50567
+                               test_instancing_limit,
f50567
+                               NULL);
f50567
+    g_test_add_data_func_full ("/errors/instancing_limit/308-recursive-use.svg",
f50567
+                               "308-recursive-use.svg",
f50567
+                               test_instancing_limit,
f50567
+                               NULL);
f50567
+    g_test_add_data_func_full ("/errors/instancing_limit/308-doubly-recursive-use.svg",
f50567
+                               "308-doubly-recursive-use.svg",
f50567
+                               test_instancing_limit,
f50567
+                               NULL);
f50567
+
f50567
+    g_test_add_data_func_full ("/errors/515-too-many-elements.svgz",
f50567
+                               "515-too-many-elements.svgz",
f50567
+                               test_loading_error,
f50567
+                               NULL);
f50567
+
f50567
 
f50567
     return g_test_run ();
f50567
 }
f50567
diff --git a/tests/fixtures/errors/308-doubly-recursive-use.svg b/tests/fixtures/errors/308-doubly-recursive-use.svg
f50567
new file mode 100644
f50567
index 0000000..9b248a6
f50567
--- /dev/null
f50567
+++ b/tests/fixtures/errors/308-doubly-recursive-use.svg
f50567
@@ -0,0 +1,13 @@
f50567
+<svg>
f50567
+  <defs>
f50567
+    <g id="one">
f50567
+      <use xlink:href="#two"/>
f50567
+    </g>
f50567
+
f50567
+    <g id="two">
f50567
+      <use xlink:href="#one"/>
f50567
+    </g>
f50567
+  </defs>
f50567
+
f50567
+  <use xlink:href="#one"/>
f50567
+</svg>
f50567
diff --git a/tests/fixtures/errors/308-recursive-use.svg b/tests/fixtures/errors/308-recursive-use.svg
f50567
new file mode 100644
f50567
index 0000000..f5d00bf
f50567
--- /dev/null
f50567
+++ b/tests/fixtures/errors/308-recursive-use.svg
f50567
@@ -0,0 +1,9 @@
f50567
+<svg>
f50567
+  <defs>
f50567
+    <g id="one">
f50567
+      <use xlink:href="#one"/>
f50567
+    </g>
f50567
+  </defs>
f50567
+
f50567
+  <use xlink:href="#one"/>
f50567
+</svg>
f50567
diff --git a/tests/fixtures/errors/308-use-self-ref.svg b/tests/fixtures/errors/308-use-self-ref.svg
f50567
new file mode 100644
f50567
index 0000000..dbf14c5
f50567
--- /dev/null
f50567
+++ b/tests/fixtures/errors/308-use-self-ref.svg
f50567
@@ -0,0 +1,7 @@
f50567
+<svg>
f50567
+  <defs>
f50567
+    <use id="one" xlink:href="#one"/>
f50567
+  </defs>
f50567
+
f50567
+  <use xlink:href="#one"/>
f50567
+</svg>
f50567
diff --git a/tests/fixtures/errors/515-pattern-billion-laughs.svg b/tests/fixtures/errors/515-pattern-billion-laughs.svg
f50567
new file mode 100644
f50567
index 0000000..a306960
f50567
--- /dev/null
f50567
+++ b/tests/fixtures/errors/515-pattern-billion-laughs.svg
f50567
@@ -0,0 +1,130 @@
f50567
+
f50567
+
f50567
+  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
f50567
+
f50567
+     xmlns="http://www.w3.org/2000/svg">
f50567
+  <defs>
f50567
+    <pattern id="z" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(10,10)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#i)" stroke="yellow"/>
f50567
+    </pattern>
f50567
+
f50567
+	<pattern id="i" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#h)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#h)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#h)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#h)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#h)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#h)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#h)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#h)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#h)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#h)" stroke="cyan" />
f50567
+    </pattern>
f50567
+
f50567
+	<pattern id="h" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#g)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#g)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#g)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#g)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#g)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#g)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#g)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#g)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#g)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#g)" stroke="cyan" />
f50567
+    </pattern>
f50567
+
f50567
+	<pattern id="g" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#f)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#f)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#f)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#f)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#f)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#f)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#f)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#f)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#f)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#f)" stroke="cyan" />
f50567
+    </pattern>
f50567
+
f50567
+	<pattern id="f" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#e)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#e)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#e)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#e)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#e)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#e)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#e)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#e)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#e)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#e)" stroke="cyan" />
f50567
+    </pattern>
f50567
+
f50567
+	<pattern id="e" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#d)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#d)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#d)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#d)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#d)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#d)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#d)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#d)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#d)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#d)" stroke="cyan" />
f50567
+    </pattern>
f50567
+
f50567
+    <pattern id="d" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#c)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#c)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#c)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#c)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#c)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#c)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#c)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#c)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#c)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#c)" stroke="cyan" />
f50567
+    </pattern>
f50567
+    <pattern id="c" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="url(#b)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#b)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#b)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#b)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#b)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#b)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#b)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#b)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#b)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#b)" stroke="cyan" />
f50567
+    </pattern>
f50567
+    <pattern id="b" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+	  <rect x="0" y="0" width="20" height="20" fill="url(#a)" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="url(#a)" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="url(#a)" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="url(#a)" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="url(#a)" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="url(#a)" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="url(#a)" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="url(#a)" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="url(#a)" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="url(#a)" stroke="cyan" />
f50567
+
f50567
+    </pattern>
f50567
+    <pattern id="a" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
f50567
+      <rect x="0" y="0" width="20" height="20" fill="none" stroke="green" />
f50567
+	  <rect x="1" y="1" width="20" height="20" fill="none" stroke="brown" />
f50567
+	  <rect x="2" y="2" width="20" height="20" fill="none" stroke="pink" />
f50567
+	  <rect x="3" y="3" width="20" height="20" fill="none" stroke="grey" />
f50567
+	  <rect x="4" y="3" width="20" height="20" fill="none" stroke="cyan" />
f50567
+	  <rect x="5" y="3" width="20" height="20" fill="none" stroke="green" />
f50567
+	  <rect x="6" y="3" width="20" height="20" fill="none" stroke="brown" />
f50567
+	  <rect x="7" y="3" width="20" height="20" fill="none" stroke="pink" />
f50567
+	  <rect x="8" y="3" width="20" height="20" fill="none" stroke="grey" />
f50567
+	  <rect x="9" y="3" width="20" height="20" fill="none" stroke="cyan" />
f50567
+    </pattern>
f50567
+  </defs>
f50567
+
f50567
+  
f50567
+           cx="400" cy="200" rx="350" ry="150" />
f50567
+
f50567
+</svg>
f50567
\ No newline at end of file
f50567
diff --git a/tests/fixtures/errors/515-too-many-elements.svgz b/tests/fixtures/errors/515-too-many-elements.svgz
f50567
new file mode 100644
f50567
index 0000000000000000000000000000000000000000..a7f7cf678ca2f29af6df61078d1c6a86c73c2d1a
f50567
GIT binary patch
f50567
literal 40811
f50567
zcmeIuO)I1U007{3c1mhf$VD-7q(+J1MDM}L%|UFTsL8?1JBN{yP&im}QQ{&|QhvZj
f50567
zljI=9MY%ail5kSH<)g@tkhY%ZCp
f50567
zerz~+JUc3x2`j>*LibxH_PE;`Ph^fuo4Xpr-qW6!wVUeOr_1r`p~-)LTcZB@uIs(k
f50567
zp^3xx{A%Lt>ENbsBzwPKxl_B*S@%4>{ZPBL{_?u~dmaM@3>YwAz<>b*1`HT5V8DO@
f50567
z0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*Vq
f50567
ziwEvbqN?)X)ARdf*>V8`1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*
f50567
z1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwA;JXJdqN
f50567
zYVLd>)r0{91`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>f$?1`2;_+E#M0g
f50567
I9YGlT0Agk%tpET3
f50567
f50567
literal 0
f50567
HcmV?d00001
f50567
f50567
-- 
f50567
2.26.2
f50567