[tracker/wip/carlosg/bus-fixes: 3/4] libtracker-sparql/bus: Validate column offsets in bus cursors




commit a4172e8a2233c7b3fb1f082dc4e3bb9cb160f3e4
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Jul 19 18:54:00 2022 +0200

    libtracker-sparql/bus: Validate column offsets in bus cursors
    
    We use these to fetch the string values given in a row. Since this
    involves buffer allocations and direct access to the pointers
    described by these locations, ensure the values are sensible.
    
    Should fix newly reported Coverity issues.
    
    CID: #1518980

 src/libtracker-sparql/bus/tracker-bus-cursor.c | 34 ++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
---
diff --git a/src/libtracker-sparql/bus/tracker-bus-cursor.c b/src/libtracker-sparql/bus/tracker-bus-cursor.c
index 14e8afd13..4b1db6504 100644
--- a/src/libtracker-sparql/bus/tracker-bus-cursor.c
+++ b/src/libtracker-sparql/bus/tracker-bus-cursor.c
@@ -181,6 +181,37 @@ tracker_bus_cursor_get_string (TrackerSparqlCursor *cursor,
        return str;
 }
 
+static gboolean
+validate_offsets (gint32  *offsets,
+                 gint     n_columns,
+                 GError **error)
+{
+       gint i;
+
+       for (i = 0; i < n_columns - 1) {
+               gint32 cur = offsets[i];
+               gint32 next = offsets[i + 1];
+
+               if (cur < 0 || cur >= next)
+                       goto error;
+       }
+
+       /* Set a ridiculously high limit on the row size,
+        * but a limit nonetheless. We can store up to 1GB
+        * in a single column/row, so allow 4 of these.
+        */
+       if (offsets[n_columns] > 4 * 1024 * 1024 * 1024)
+               goto error;
+
+       return TRUE;
+ error:
+       g_set_error (error,
+                    G_IO_ERROR,
+                    G_IO_ERROR_INVALID_DATA,
+                    "Corrupted cursor data");
+       return FALSE;
+}
+
 static gboolean
 tracker_bus_cursor_next (TrackerSparqlCursor  *cursor,
                          GCancellable         *cancellable,
@@ -224,6 +255,9 @@ tracker_bus_cursor_next (TrackerSparqlCursor  *cursor,
                                      NULL, NULL, error))
                return FALSE;
 
+       if (!validate_offsets (offsets, n_columns, error))
+               return FALSE;
+
        /* The last offset says how long we have to go to read
         * the whole row data.
         */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]