[gnome-software/wip/hughsie/fwupd] Actually verify the firmware checksum before using



commit ff9bcf3b43fbbc1b46b4c299c38b4e574eaa84e5
Author: Richard Hughes <richard hughsie com>
Date:   Wed Mar 18 15:02:16 2015 +0000

    Actually verify the firmware checksum before using

 src/plugins/gs-plugin-fwupd.c |   45 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/src/plugins/gs-plugin-fwupd.c b/src/plugins/gs-plugin-fwupd.c
index 0612af8..75ce356 100644
--- a/src/plugins/gs-plugin-fwupd.c
+++ b/src/plugins/gs-plugin-fwupd.c
@@ -26,6 +26,7 @@
 #include <gio/gio.h>
 #include <gio/gunixfdlist.h>
 #include <libsoup/soup.h>
+#include <glib/gstdio.h>
 
 #include <gs-plugin.h>
 
@@ -194,6 +195,22 @@ gs_plugin_fwupd_add_required_location (GsPlugin *plugin, const gchar *location)
 }
 
 /**
+ * gs_plugin_fwupd_get_file_checksum:
+ */
+static gchar *
+gs_plugin_fwupd_get_file_checksum (const gchar *filename,
+                                  GChecksumType checksum_type,
+                                  GError **error)
+{
+       gsize len;
+       _cleanup_free_ gchar *data = NULL;
+
+       if (!g_file_get_contents (filename, &data, &len, error))
+               return NULL;
+       return g_compute_checksum_for_data (checksum_type, (const guchar *)data, len);
+}
+
+/**
  * gs_plugin_fwupd_add_device:
  */
 static gboolean
@@ -210,6 +227,8 @@ gs_plugin_fwupd_add_device (GsPlugin *plugin,
        const gchar *tmp;
        guint i;
        _cleanup_free_ gchar *basename = NULL;
+       _cleanup_free_ gchar *checksum = NULL;
+       _cleanup_free_ gchar *checksum2 = NULL;
        _cleanup_free_ gchar *filename_cache = NULL;
        _cleanup_free_ gchar *update_location = NULL;
        _cleanup_free_ gchar *update_version = NULL;
@@ -240,6 +259,16 @@ gs_plugin_fwupd_add_device (GsPlugin *plugin,
                if (as_utils_vercmp (as_release_get_version (rel), version) <= 0)
                        continue;
 
+               /* get checksum */
+               tmp = as_release_get_checksum (rel, G_CHECKSUM_SHA1);
+               if (tmp == NULL) {
+                       g_warning ("%s [%s] has no checksum, ignoring as unsafe",
+                                  as_app_get_id (item),
+                                  as_release_get_version (rel));
+                       continue;
+               }
+               checksum = g_strdup (tmp);
+
                /* get the update text, if it exists */
                if (update_version == NULL) {
                        tmp = as_release_get_version (rel);
@@ -298,6 +327,22 @@ gs_plugin_fwupd_add_device (GsPlugin *plugin,
                return FALSE;
        }
 
+       /* does the checksum match */
+       checksum2 = gs_plugin_fwupd_get_file_checksum (filename_cache,
+                                                      G_CHECKSUM_SHA1,
+                                                      error);
+       if (checksum2 == NULL)
+               return FALSE;
+       if (g_strcmp0 (checksum, checksum2) != 0) {
+               g_set_error (error,
+                            GS_PLUGIN_ERROR,
+                            GS_PLUGIN_ERROR_FAILED,
+                            "%s does not match checksum, expected %s, got %s",
+                            filename_cache, checksum, checksum2);
+               g_unlink (filename_cache);
+               return FALSE;
+       }
+
        /* remove trailing newline */
        if (update_desc->len > 0)
                g_string_truncate (update_desc, update_desc->len - 1);


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