CVE-2017-14604: Improper Input Validation (CWE-20)
value: GNOME Nautilus before 3.23.90 allows attackers to spoof a file type by using the .desktop file extension, as demonstrated by an attack in which a .desktop file's Name field ends in .pdf but this file's Exec field launches a malicious "sh -c" command. In other words, Nautilus provides no UI indication that a file actually has the potentially unsafe .desktop extension; instead, the UI only shows the .pdf extension. One (slightly) mitigating factor is that an attack requires the .desktop file to have execute permission. The solution is to ask the user to confirm that the file is supposed to be treated as a .desktop file, and then remember the user's answer in the metadata::trusted field.
CWE description: The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
CVSS v3 base score: 6.5
Published: 2017-09-20T08:29Z
Repository: https://github.com/GNOME/nautilus
Language: C
Files changed: src/nautilus-directory-async.c, src/nautilus-file-operations.c, src/nautilus-file-operations.h, src/nautilus-metadata.c, src/nautilus-metadata.h, src/nautilus-mime-actions.c
Commit date: 2017-02-06 18:53:08 +0100
Commit message: mime-actions: use file metadata for trusting desktop files

Currently we only trust desktop files that have the executable bit
set, and don't replace the displayed icon or the displayed name until
it's trusted, which prevents for running random programs by a malicious
desktop file.

However, the executable permission is preserved if the desktop file
comes from a compressed file.

To prevent this, add a metadata::trusted metadata to the file once the
user acknowledges the file as trusted. This adds metadata to the file,
which cannot be added unless it has access to the computer.

Also remove the SHEBANG "trusted" content we were putting inside the
desktop file, since that doesn't add more security since it can come
with the file itself.

https://bugzilla.gnome.org/show_bug.cgi?id=777991

Diff: diff --git a/src/nautilus-directory-async.c b/src/nautilus-directory-async.c
index e8a10cd3b..b02e3de87 100644
--- a/src/nautilus-directory-async.c
+++ b/src/nautilus-directory-async.c
@@ -28,10 +28,11 @@
 #include "nautilus-file-utilities.h"
 #include "nautilus-signaller.h"
 #include "nautilus-global-preferences.h"
 #include "nautilus-link.h"
 #include "nautilus-profile.h"
+#include "nautilus-metadata.h"
 #include <eel/eel-glib-extensions.h>
 #include <gtk/gtk.h>
 #include <libxml/parser.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -3578,17 +3579,21 @@ static gboolean
 is_link_trusted (NautilusFile *file,
                  gboolean      is_launcher)
 {
     GFile *location;
     gboolean res;
+    g_autofree gchar* trusted = NULL;
 
     if (!is_launcher)
     {
         return TRUE;
     }
 
-    if (nautilus_file_can_execute (file))
+    trusted = nautilus_file_get_metadata (file,
+                                          NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
+                                          NULL);
+    if (nautilus_file_can_execute (file) && trusted != NULL)
     {
         return TRUE;
     }
 
     res = FALSE;
diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c
index 89bc93d1a..cd8ea0bdc 100644
--- a/src/nautilus-file-operations.c
+++ b/src/nautilus-file-operations.c
@@ -233,14 +233,14 @@ typedef struct
 #define MERGE _("_Merge")
 #define MERGE_ALL _("Merge _All")
 #define COPY_FORCE _("Copy _Anyway")
 
 static void
-mark_desktop_file_trusted (CommonJob    *common,
-                           GCancellable *cancellable,
-                           GFile        *file,
-                           gboolean      interactive);
+mark_desktop_file_executable (CommonJob    *common,
+                              GCancellable *cancellable,
+                              GFile        *file,
+                              gboolean      interactive);
 
 static gboolean
 is_all_button_text (const char *button_text)
 {
     g_assert (button_text != NULL);
@@ -5288,14 +5288,14 @@ retry:
          *  mark it as trusted. */
         if (copy_job->desktop_location != NULL &&
             g_file_equal (copy_job->desktop_location, dest_dir) &&
             is_trusted_desktop_file (src, job->cancellable))
         {
-            mark_desktop_file_trusted (job,
-                                       job->cancellable,
-                                       dest,
-                                       FALSE);
+            mark_desktop_file_executable (job,
+                                          job->cancellable,
+                                          dest,
+                                          FALSE);
         }
 
         if (job->undo_info != NULL)
         {
             nautilus_file_undo_info_ext_add_origin_target_pair (NAUTILUS_FILE_UNDO_INFO_EXT (job->undo_info),
@@ -7885,13 +7885,13 @@ nautilus_file_operations_empty_trash (GtkWidget *parent_view)
     g_task_run_in_thread (task, empty_trash_thread_func);
     g_object_unref (task);
 }
 
 static void
-mark_trusted_task_done (GObject      *source_object,
-                        GAsyncResult *res,
-                        gpointer      user_data)
+mark_desktop_file_executable_task_done (GObject      *source_object,
+                                        GAsyncResult *res,
+                                        gpointer      user_data)
 {
     MarkTrustedJob *job = user_data;
 
     g_object_unref (job->file);
 
@@ -7905,114 +7905,23 @@ mark_trusted_task_done (GObject      *source_object,
 }
 
 #define TRUSTED_SHEBANG "#!/usr/bin/env xdg-open\n"
 
 static void
-mark_desktop_file_trusted (CommonJob    *common,
-                           GCancellable *cancellable,
-                           GFile        *file,
-                           gboolean      interactive)
+mark_desktop_file_executable (CommonJob    *common,
+                              GCancellable *cancellable,
+                              GFile        *file,
+                              gboolean      interactive)
 {
-    char *contents, *new_contents;
-    gsize length, new_length;
     GError *error;
     guint32 current_perms, new_perms;
     int response;
     GFileInfo *info;
 
 retry:
-    error = NULL;
-    if (!g_file_load_contents (file,
-                               cancellable,
-                               &contents, &length,
-                               NULL, &error))
-    {
-        if (interactive)
-        {
-            response = run_error (common,
-                                  g_strdup (_("Unable to mark launcher trusted (executable)")),
-                                  error->message,
-                                  NULL,
-                                  FALSE,
-                                  CANCEL, RETRY,
-                                  NULL);
-        }
-        else
-        {
-            response = 0;
-        }
-
-
-        if (response == 0 || response == GTK_RESPONSE_DELETE_EVENT)
-        {
-            abort_job (common);
-        }
-        else if (response == 1)
-        {
-            goto retry;
-        }
-        else
-        {
-            g_assert_not_reached ();
-        }
-
-        goto out;
-    }
-
-    if (!g_str_has_prefix (contents, "#!"))
-    {
-        new_length = length + strlen (TRUSTED_SHEBANG);
-        new_contents = g_malloc (new_length);
-
-        strcpy (new_contents, TRUSTED_SHEBANG);
-        memcpy (new_contents + strlen (TRUSTED_SHEBANG),
-                contents, length);
-
-        if (!g_file_replace_contents (file,
-                                      new_contents,
-                                      new_length,
-                                      NULL,
-                                      FALSE, 0,
-                                      NULL, cancellable, &error))
-        {
-            g_free (contents);
-            g_free (new_contents);
-
-            if (interactive)
-            {
-                response = run_error (common,
-                                      g_strdup (_("Unable to mark launcher trusted (executable)")),
-                                      error->message,
-                                      NULL,
-                                      FALSE,
-                                      CANCEL, RETRY,
-                                      NULL);
-            }
-            else
-            {
-                response = 0;
-            }
-
-            if (response == 0 || response == GTK_RESPONSE_DELETE_EVENT)
-            {
-                abort_job (common);
-            }
-            else if (response == 1)
-            {
-                goto retry;
-            }
-            else
-            {
-                g_assert_not_reached ();
-            }
-
-            goto out;
-        }
-        g_free (new_contents);
-    }
-    g_free (contents);
 
+    error = NULL;
     info = g_file_query_info (file,
                               G_FILE_ATTRIBUTE_STANDARD_TYPE ","
                               G_FILE_ATTRIBUTE_UNIX_MODE,
                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                               common->cancellable,
@@ -8099,47 +8008,47 @@ retry:
 out:
     ;
 }
 
 static void
-mark_trusted_task_thread_func (GTask        *task,
-                               gpointer      source_object,
-                               gpointer      task_data,
-                               GCancellable *cancellable)
+mark_desktop_file_executable_task_thread_func (GTask        *task,
+                                               gpointer      source_object,
+                                               gpointer      task_data,
+                                               GCancellable *cancellable)
 {
     MarkTrustedJob *job = task_data;
     CommonJob *common;
 
     common = (CommonJob *) job;
 
     nautilus_progress_info_start (job->common.progress);
 
-    mark_desktop_file_trusted (common,
-                               cancellable,
-                               job->file,
-                               job->interactive);
+    mark_desktop_file_executable (common,
+                                  cancellable,
+                                  job->file,
+                                  job->interactive);
 }
 
 void
-nautilus_file_mark_desktop_file_trusted (GFile              *file,
-                                         GtkWindow          *parent_window,
-                                         gboolean            interactive,
-                                         NautilusOpCallback  done_callback,
-                                         gpointer            done_callback_data)
+nautilus_file_mark_desktop_file_executable (GFile              *file,
+                                            GtkWindow          *parent_window,
+                                            gboolean            interactive,
+                                            NautilusOpCallback  done_callback,
+                                            gpointer            done_callback_data)
 {
     GTask *task;
     MarkTrustedJob *job;
 
     job = op_job_new (MarkTrustedJob, parent_window);
     job->file = g_object_ref (file);
     job->interactive = interactive;
     job->done_callback = done_callback;
     job->done_callback_data = done_callback_data;
 
-    task = g_task_new (NULL, NULL, mark_trusted_task_done, job);
+    task = g_task_new (NULL, NULL, mark_desktop_file_executable_task_done, job);
     g_task_set_task_data (task, job, NULL);
-    g_task_run_in_thread (task, mark_trusted_task_thread_func);
+    g_task_run_in_thread (task, mark_desktop_file_executable_task_thread_func);
     g_object_unref (task);
 }
 
 static void
 extract_task_done (GObject      *source_object,
diff --git a/src/nautilus-file-operations.h b/src/nautilus-file-operations.h
index 31b782dba..a479ee6e0 100644
--- a/src/nautilus-file-operations.h
+++ b/src/nautilus-file-operations.h
@@ -144,15 +144,15 @@ void nautilus_file_operations_link      (GList                *files,
 					 GArray               *relative_item_points,
 					 GFile                *target_dir,
 					 GtkWindow            *parent_window,
 					 NautilusCopyCallback  done_callback,
 					 gpointer              done_callback_data);
-void nautilus_file_mark_desktop_file_trusted (GFile           *file,
-					      GtkWindow        *parent_window,
-					      gboolean          interactive,
-					      NautilusOpCallback done_callback,
-					      gpointer          done_callback_data);
+void nautilus_file_mark_desktop_file_executable (GFile           *file,
+                                                 GtkWindow        *parent_window,
+                                                 gboolean          interactive,
+                                                 NautilusOpCallback done_callback,
+                                                 gpointer          done_callback_data);
 void nautilus_file_operations_extract_files (GList                   *files,
                                              GFile                   *destination_directory,
                                              GtkWindow               *parent_window,
                                              NautilusExtractCallback  done_callback,
                                              gpointer                 done_callback_data);
diff --git a/src/nautilus-metadata.c b/src/nautilus-metadata.c
index 8316426f9..bee04e7ca 100644
--- a/src/nautilus-metadata.c
+++ b/src/nautilus-metadata.c
@@ -49,10 +49,11 @@ static char *used_metadata_names[] =
     NAUTILUS_METADATA_KEY_ICON_SCALE,
     NAUTILUS_METADATA_KEY_CUSTOM_ICON,
     NAUTILUS_METADATA_KEY_CUSTOM_ICON_NAME,
     NAUTILUS_METADATA_KEY_SCREEN,
     NAUTILUS_METADATA_KEY_EMBLEMS,
+    NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
     NULL
 };
 
 guint
 nautilus_metadata_get_id (const char *metadata)
diff --git a/src/nautilus-metadata.h b/src/nautilus-metadata.h
index 7a734af31..c4a303ec5 100644
--- a/src/nautilus-metadata.h
+++ b/src/nautilus-metadata.h
@@ -65,8 +65,10 @@
 #define NAUTILUS_METADATA_KEY_CUSTOM_ICON                	"custom-icon"
 #define NAUTILUS_METADATA_KEY_CUSTOM_ICON_NAME                	"custom-icon-name"
 #define NAUTILUS_METADATA_KEY_SCREEN				"screen"
 #define NAUTILUS_METADATA_KEY_EMBLEMS				"emblems"
 
+#define NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED				"trusted"
+
 guint nautilus_metadata_get_id (const char *metadata);
 
 #endif /* NAUTILUS_METADATA_H */
diff --git a/src/nautilus-mime-actions.c b/src/nautilus-mime-actions.c
index 036b9679d..14fe44bc3 100644
--- a/src/nautilus-mime-actions.c
+++ b/src/nautilus-mime-actions.c
@@ -40,10 +40,11 @@
 #include "nautilus-file-operations.h"
 #include "nautilus-metadata.h"
 #include "nautilus-program-choosing.h"
 #include "nautilus-global-preferences.h"
 #include "nautilus-signaller.h"
+#include "nautilus-metadata.h"
 
 #define DEBUG_FLAG NAUTILUS_DEBUG_MIME
 #include "nautilus-debug.h"
 
 typedef enum
@@ -219,11 +220,10 @@ struct
 #define DELAY_UNTIL_CANCEL_MSECS 5000
 
 #define RESPONSE_RUN 1000
 #define RESPONSE_DISPLAY 1001
 #define RESPONSE_RUN_IN_TERMINAL 1002
-#define RESPONSE_MARK_TRUSTED 1003
 
 #define SILENT_WINDOW_OPEN_LIMIT 5
 #define SILENT_OPEN_LIMIT 5
 
 /* This number controls a maximum character count for a URL that is
@@ -1515,28 +1515,39 @@ untrusted_launcher_response_callback (GtkDialog                 *dialog,
     char *uri;
     GFile *file;
 
     switch (response_id)
     {
-        case RESPONSE_RUN:
+        case GTK_RESPONSE_OK:
         {
+            file = nautilus_file_get_location (parameters->file);
+
+            /* We need to do this in order to prevent malicious desktop files
+             * with the executable bit already set.
+             * See https://bugzilla.gnome.org/show_bug.cgi?id=777991
+             */
+            nautilus_file_set_metadata (parameters->file, NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
+                                        NULL,
+                                        "yes");
+
+            nautilus_file_mark_desktop_file_executable (file,
+                                                        parameters->parent_window,
+                                                        TRUE,
+                                                        NULL, NULL);
+
+            /* Need to force a reload of the attributes so is_trusted is marked
+             * correctly. Not sure why the general monitor doesn't fire in this
+             * case when setting the metadata
+             */
+            nautilus_file_invalidate_all_attributes (parameters->file);
+
             screen = gtk_widget_get_screen (GTK_WIDGET (parameters->parent_window));
             uri = nautilus_file_get_uri (parameters->file);
             DEBUG ("Launching untrusted launcher %s", uri);
             nautilus_launch_desktop_file (screen, uri, NULL,
                                           parameters->parent_window);
             g_free (uri);
-        }
-        break;
-
-        case RESPONSE_MARK_TRUSTED:
-        {
-            file = nautilus_file_get_location (parameters->file);
-            nautilus_file_mark_desktop_file_trusted (file,
-                                                     parameters->parent_window,
-                                                     TRUE,
-                                                     NULL, NULL);
             g_object_unref (file);
         }
         break;
 
         default:
@@ -1588,21 +1599,20 @@ activate_desktop_file (ActivateParameters *parameters,
                                          NULL);
         g_object_set (dialog,
                       "text", primary,
                       "secondary-text", secondary,
                       NULL);
+
         gtk_dialog_add_button (GTK_DIALOG (dialog),
-                               _("_Launch Anyway"), RESPONSE_RUN);
+                               _("_Cancel"), GTK_RESPONSE_CANCEL);
+
+        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
         if (nautilus_file_can_set_permissions (file))
         {
             gtk_dialog_add_button (GTK_DIALOG (dialog),
-                                   _("Mark as _Trusted"), RESPONSE_MARK_TRUSTED);
+                                   _("Trust and _Launch"), GTK_RESPONSE_OK);
         }
-        gtk_dialog_add_button (GTK_DIALOG (dialog),
-                               _("_Cancel"), GTK_RESPONSE_CANCEL);
-        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
-
         g_signal_connect (dialog, "response",
                           G_CALLBACK (untrusted_launcher_response_callback),
                           parameters_desktop);
         gtk_widget_show (dialog);
 

Vulnerable code:     if (nautilus_file_can_execute (file))
mark_desktop_file_trusted (CommonJob    *common,
                           GCancellable *cancellable,
                           GFile        *file,
                           gboolean      interactive);
            mark_desktop_file_trusted (job,
                                       job->cancellable,
                                       dest,
                                       FALSE);
mark_trusted_task_done (GObject      *source_object,
                        GAsyncResult *res,
                        gpointer      user_data)
mark_desktop_file_trusted (CommonJob    *common,
                           GCancellable *cancellable,
                           GFile        *file,
                           gboolean      interactive)
    char *contents, *new_contents;
    gsize length, new_length;
    error = NULL;
    if (!g_file_load_contents (file,
                               cancellable,
                               &contents, &length,
                               NULL, &error))
    {
        if (interactive)
        {
            response = run_error (common,
                                  g_strdup (_("Unable to mark launcher trusted (executable)")),
                                  error->message,
                                  NULL,
                                  FALSE,
                                  CANCEL, RETRY,
                                  NULL);
        }
        else
        {
            response = 0;
        }


        if (response == 0 || response == GTK_RESPONSE_DELETE_EVENT)
        {
            abort_job (common);
        }
        else if (response == 1)
        {
            goto retry;
        }
        else
        {
            g_assert_not_reached ();
        }

        goto out;
    }

    if (!g_str_has_prefix (contents, "#!"))
    {
        new_length = length + strlen (TRUSTED_SHEBANG);
        new_contents = g_malloc (new_length);

        strcpy (new_contents, TRUSTED_SHEBANG);
        memcpy (new_contents + strlen (TRUSTED_SHEBANG),
                contents, length);

        if (!g_file_replace_contents (file,
                                      new_contents,
                                      new_length,
                                      NULL,
                                      FALSE, 0,
                                      NULL, cancellable, &error))
        {
            g_free (contents);
            g_free (new_contents);

            if (interactive)
            {
                response = run_error (common,
                                      g_strdup (_("Unable to mark launcher trusted (executable)")),
                                      error->message,
                                      NULL,
                                      FALSE,
                                      CANCEL, RETRY,
                                      NULL);
            }
            else
            {
                response = 0;
            }

            if (response == 0 || response == GTK_RESPONSE_DELETE_EVENT)
            {
                abort_job (common);
            }
            else if (response == 1)
            {
                goto retry;
            }
            else
            {
                g_assert_not_reached ();
            }

            goto out;
        }
        g_free (new_contents);
    }
    g_free (contents);
mark_trusted_task_thread_func (GTask        *task,
                               gpointer      source_object,
                               gpointer      task_data,
                               GCancellable *cancellable)
    mark_desktop_file_trusted (common,
                               cancellable,
                               job->file,
                               job->interactive);
nautilus_file_mark_desktop_file_trusted (GFile              *file,
                                         GtkWindow          *parent_window,
                                         gboolean            interactive,
                                         NautilusOpCallback  done_callback,
                                         gpointer            done_callback_data)
    task = g_task_new (NULL, NULL, mark_trusted_task_done, job);
    g_task_run_in_thread (task, mark_trusted_task_thread_func);
void nautilus_file_mark_desktop_file_trusted (GFile           *file,
					      GtkWindow        *parent_window,
					      gboolean          interactive,
					      NautilusOpCallback done_callback,
					      gpointer          done_callback_data);
#define RESPONSE_MARK_TRUSTED 1003
        case RESPONSE_RUN:
        }
        break;

        case RESPONSE_MARK_TRUSTED:
        {
            file = nautilus_file_get_location (parameters->file);
            nautilus_file_mark_desktop_file_trusted (file,
                                                     parameters->parent_window,
                                                     TRUE,
                                                     NULL, NULL);
                               _("_Launch Anyway"), RESPONSE_RUN);
                                   _("Mark as _Trusted"), RESPONSE_MARK_TRUSTED);
        gtk_dialog_add_button (GTK_DIALOG (dialog),
                               _("_Cancel"), GTK_RESPONSE_CANCEL);
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);

Fixed code: #include "nautilus-metadata.h"
    g_autofree gchar* trusted = NULL;
    trusted = nautilus_file_get_metadata (file,
                                          NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
                                          NULL);
    if (nautilus_file_can_execute (file) && trusted != NULL)
mark_desktop_file_executable (CommonJob    *common,
                              GCancellable *cancellable,
                              GFile        *file,
                              gboolean      interactive);
            mark_desktop_file_executable (job,
                                          job->cancellable,
                                          dest,
                                          FALSE);
mark_desktop_file_executable_task_done (GObject      *source_object,
                                        GAsyncResult *res,
                                        gpointer      user_data)
mark_desktop_file_executable (CommonJob    *common,
                              GCancellable *cancellable,
                              GFile        *file,
                              gboolean      interactive)
    error = NULL;
mark_desktop_file_executable_task_thread_func (GTask        *task,
                                               gpointer      source_object,
                                               gpointer      task_data,
                                               GCancellable *cancellable)
    mark_desktop_file_executable (common,
                                  cancellable,
                                  job->file,
                                  job->interactive);
nautilus_file_mark_desktop_file_executable (GFile              *file,
                                            GtkWindow          *parent_window,
                                            gboolean            interactive,
                                            NautilusOpCallback  done_callback,
                                            gpointer            done_callback_data)
    task = g_task_new (NULL, NULL, mark_desktop_file_executable_task_done, job);
    g_task_run_in_thread (task, mark_desktop_file_executable_task_thread_func);
void nautilus_file_mark_desktop_file_executable (GFile           *file,
                                                 GtkWindow        *parent_window,
                                                 gboolean          interactive,
                                                 NautilusOpCallback done_callback,
                                                 gpointer          done_callback_data);
    NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
#define NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED				"trusted"

#include "nautilus-metadata.h"
        case GTK_RESPONSE_OK:
            file = nautilus_file_get_location (parameters->file);

            /* We need to do this in order to prevent malicious desktop files
             * with the executable bit already set.
             * See https://bugzilla.gnome.org/show_bug.cgi?id=777991
             */
            nautilus_file_set_metadata (parameters->file, NAUTILUS_METADATA_KEY_DESKTOP_FILE_TRUSTED,
                                        NULL,
                                        "yes");

            nautilus_file_mark_desktop_file_executable (file,
                                                        parameters->parent_window,
                                                        TRUE,
                                                        NULL, NULL);

            /* Need to force a reload of the attributes so is_trusted is marked
             * correctly. Not sure why the general monitor doesn't fire in this
             * case when setting the metadata
             */
            nautilus_file_invalidate_all_attributes (parameters->file);


                               _("_Cancel"), GTK_RESPONSE_CANCEL);

        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
                                   _("Trust and _Launch"), GTK_RESPONSE_OK);