Skip to content

ev-window.c: Only use the base name of attachments when saving - #736

Open
dualfroz wants to merge 1 commit into
linuxmint:masterfrom
dualfroz:fix/714-attachment-path-traversal
Open

dualfroz wants to merge 1 commit into
linuxmint:masterfrom
dualfroz:fix/714-attachment-path-traversal

Conversation

@dualfroz

Copy link
Copy Markdown

the attachment name comes straight from the pdf, so with a name like ../Desktop/test.desktop "Save Attachment As..." wrote the file outside the folder you picked. both the folder path (g_file_get_child) and the suggested name in the dialog for a single attachment had this, the dialog one too since gtk resolves the ../ on save. they now use g_path_get_basename of the name, same as ev_attachment_open already does for its temp file. checked with a pdf that has ../escaped.txt and inside.txt attached, both end up in the picked folder now. Fixes #714

The attachment name comes from the document and was used as is to
build the destination, so a name like ../Desktop/x.desktop wrote the
file outside the folder picked in Save Attachment As. The same name was
also suggested in the save dialog for a single attachment, where GTK
resolves it the same way. Use g_path_get_basename for both, like
ev_attachment_open already does for its temporary file.
@ziggy471

Copy link
Copy Markdown

I think there is one more place that needs the same treatment: dragging an attachment out of the sidebar.

ev_sidebar_attachments_drag_data_get() in shell/ev-sidebar-attachments.c still builds the temp file template from the raw name:

template = g_strdup_printf ("%s.XXXXXX", ev_attachment_get_name (attachment));
file = ev_mkstemp_file (template, &error);

ev_mkstemp() joins that template to the temp directory with g_build_filename(), so a name containing ../ can still place the file outside it. The random suffix limits the impact, but it is the same issue. I found this by reading the code; I have not run an exploit
against this path.

g_path_get_basename() can also return ., .. or /, and the attachment name may be missing.

An alternative is to sanitize the name once, when the EvAttachment is created. That covers saving, the suggested name in the dialog, drag and drop and opening, as well as any later users of ev_attachment_get_name():

--- a/libdocument/ev-attachment.c
+++ b/libdocument/ev-attachment.c
@@ -104,6 +104,24 @@
        G_OBJECT_CLASS (ev_attachment_parent_class)->finalize (object);
 }

+/* The name comes from the document and is later used to build file names
+ * (save, save as, drag and drop, open). Reduce it to a single path component
+ * so that it can never point outside the destination directory. */
+static gchar *
+ev_attachment_safe_name (const gchar *name)
+{
+       gchar *basename = g_path_get_basename (name ? name : "");
+
+       if (g_strcmp0 (basename, ".") == 0 ||
+           g_strcmp0 (basename, "..") == 0 ||
+           g_strcmp0 (basename, G_DIR_SEPARATOR_S) == 0) {
+               g_free (basename);
+               basename = g_strdup ("attachment");
+       }
+
+       return basename;
+}
+
 static void
 ev_attachment_set_property (GObject      *object,
                            guint         prop_id,
@@ -114,7 +132,7 @@

        switch (prop_id) {
        case PROP_NAME:
-               attachment->priv->name = g_value_dup_string (value);
+               attachment->priv->name = ev_attachment_safe_name (g_value_get_string (value));
                break;
        case PROP_DESCRIPTION:
                attachment->priv->description = g_value_dup_string (value);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] - An attacker can write files with exact names to arbitrary locations on the filesystem

2 participants