CVE-2023-52076: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: Atril Document Viewer is the default document reader of the MATE desktop environment for Linux. A path traversal and arbitrary file write vulnerability exists in versions of Atril prior to 1.26.2. This vulnerability is capable of writing arbitrary files anywhere on the filesystem to which the user opening a crafted document has access. The only limitation is that this vulnerability cannot be exploited to overwrite existing files, but that doesn't stop an attacker from achieving Remote Command Execution on the target system. Version 1.26.2 of Atril contains a patch for this vulnerability.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 7.8
Published: 2024-01-25T16:15Z
Repository: https://github.com/mate-desktop/atril
Language: C
Files changed: backend/epub/epub-document.c
Commit date: 2024-01-17 22:39:09 +0000
Commit message: epub: Prevent path traversal when extracting files

Test each file's resolved path against the temporary directory
before extracting.

Diff: diff --git a/backend/epub/epub-document.c b/backend/epub/epub-document.c
index b025c630..38ab4c83 100644
--- a/backend/epub/epub-document.c
+++ b/backend/epub/epub-document.c
@@ -633,11 +633,11 @@ check_mime_type(const gchar* uri,GError** error)
         guint i;
 
         for (i = 0; i < g_strv_length (mimetypes); i++) {
            if (strcmp(mimeFromFile, mimetypes[i]) == 0)
                 return TRUE;
-	}
+        }
 
         /* fallback for malformed epub files */
         if (strcmp (mimeFromFile, "application/zip") == 0)
         {
             mimeFromFile = ev_file_get_mime_type (uri, TRUE, &err);
@@ -669,11 +669,11 @@ check_mime_type(const gchar* uri,GError** error)
 
     return FALSE;
 }
 
 static gboolean
-extract_one_file(EpubDocument* epub_document,GError ** error)
+extract_one_file(EpubDocument* epub_document, GFile *tmp_gfile, GError ** error)
 {
     GFile * outfile ;
     gsize writesize = 0;
     GString * gfilepath ;
     unz_file_info64 info ;
@@ -696,10 +696,24 @@ extract_one_file(EpubDocument* epub_document,GError ** error)
         directory++;
 
     gfilepath = g_string_new(epub_document->tmp_archive_dir) ;
     g_string_append_printf(gfilepath,"/%s",(gchar*)currentfilename);
 
+    outfile = g_file_new_for_path (gfilepath->str);
+    g_autofree gchar *rpath = g_file_get_relative_path (tmp_gfile, outfile);
+
+    if (rpath == NULL)
+    {
+        g_set_error_literal (error,
+                             EV_DOCUMENT_ERROR,
+                             EV_DOCUMENT_ERROR_INVALID,
+                             _("epub file is invalid or corrupt"));
+        g_critical ("Invalid filename in Epub container - '%s'", (gchar *) currentfilename);
+        result = FALSE;
+        goto out;
+    }
+
     /*if we encounter a directory, make a directory inside our temporary folder.*/
     if (directory != NULL && *directory == '\0')
     {
         g_mkdir(gfilepath->str,0777);
         goto out;
@@ -723,11 +737,10 @@ extract_one_file(EpubDocument* epub_document,GError ** error)
 
         g_mkdir_with_parents(dir_create->str,0777);
         g_string_free(dir_create,TRUE);
     }
 
-    outfile = g_file_new_for_path(gfilepath->str);
     outstream = g_file_create(outfile,G_FILE_CREATE_PRIVATE,NULL,error);
     gpointer buffer = g_malloc0(512);
     while ( (writesize = unzReadCurrentFile(epub_document->epubDocument,buffer,512) ) != 0 )
     {
         if ( g_output_stream_write((GOutputStream*)outstream,buffer,writesize,NULL,error) == -1 )
@@ -736,14 +749,14 @@ extract_one_file(EpubDocument* epub_document,GError ** error)
             break;
         }
     }
     g_free(buffer);
     g_output_stream_close((GOutputStream*)outstream,NULL,error);
-    g_object_unref(outfile) ;
-    g_object_unref(outstream) ;
+    g_object_unref(outstream);
 
 out:
+    g_object_unref(outfile);
     unzCloseCurrentFile (epub_document->epubDocument) ;
     g_string_free(gfilepath,TRUE);
     g_free(currentfilename);
     return result;
 }
@@ -751,10 +764,11 @@ out:
 static gboolean
 extract_epub_from_container (const gchar* uri,
                              EpubDocument *epub_document,
                              GError ** error)
 {
+    GFile *tmp_gfile = NULL;
     GError *err = NULL;
     epub_document->archivename = g_filename_from_uri(uri,NULL,error);
 
     if ( !epub_document->archivename )
     {
@@ -812,13 +826,14 @@ extract_epub_from_container (const gchar* uri,
                          _("could not extract archive"));
         }
         goto out;
     }
 
+    tmp_gfile = g_file_new_for_path (epub_document->tmp_archive_dir);
     while ( TRUE )
     {
-        if ( extract_one_file(epub_document,&err) == FALSE )
+        if ( extract_one_file(epub_document, tmp_gfile, &err) == FALSE )
         {
             if (err) {
                 g_propagate_error (error, err);
             }
             else    {
@@ -835,10 +850,11 @@ extract_epub_from_container (const gchar* uri,
             break;
         }
     }
 
 out:
+    g_clear_object (&tmp_gfile);
     unzClose(epub_document->epubDocument);
     return result;
 }
 
 static gchar*

Vulnerable code: 	}
extract_one_file(EpubDocument* epub_document,GError ** error)
    outfile = g_file_new_for_path(gfilepath->str);
    g_object_unref(outfile) ;
    g_object_unref(outstream) ;
        if ( extract_one_file(epub_document,&err) == FALSE )
Fixed code:         }
extract_one_file(EpubDocument* epub_document, GFile *tmp_gfile, GError ** error)
    outfile = g_file_new_for_path (gfilepath->str);
    g_autofree gchar *rpath = g_file_get_relative_path (tmp_gfile, outfile);

    if (rpath == NULL)
    {
        g_set_error_literal (error,
                             EV_DOCUMENT_ERROR,
                             EV_DOCUMENT_ERROR_INVALID,
                             _("epub file is invalid or corrupt"));
        g_critical ("Invalid filename in Epub container - '%s'", (gchar *) currentfilename);
        result = FALSE;
        goto out;
    }

    g_object_unref(outstream);
    g_object_unref(outfile);
    GFile *tmp_gfile = NULL;
    tmp_gfile = g_file_new_for_path (epub_document->tmp_archive_dir);
        if ( extract_one_file(epub_document, tmp_gfile, &err) == FALSE )
    g_clear_object (&tmp_gfile);