Blame SOURCES/ghostscript-cve-2018-16541.patch

09061c
From: Chris Liddell <chris.liddell@artifex.com>
09061c
Date: Thu, 23 Aug 2018 14:41:18 +0000 (+0100)
09061c
Subject: Bug 699664: Ensure the correct is in place before cleanup
09061c
09061c
Bug 699664: Ensure the correct is in place before cleanup
09061c
09061c
If the PS job replaces the device and leaves that graphics state in place, we
09061c
wouldn't cleanup the default device in the normal way, but rely on the garbage
09061c
collector.
09061c
09061c
This works (but isn't ideal), *except* when the job replaces the device with
09061c
the null device (using the nulldevice operator) - this means that
09061c
.uninstallpagedevice doesn't replace the existing device with the nulldevice
09061c
(since it is already installed), the device from the graphics ends up being
09061c
freed - and as it is the nulldevice, which we rely on, memory corruption
09061c
and a segfault can happen.
09061c
09061c
We avoid this by checking if the current device is the nulldevice, and if so,
09061c
restoring it away, before continuing with the device cleanup.
09061c
09061c
http://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=241d91112771a6104de10b3948c3f350d6690c1d
09061c
---
09061c
09061c
diff -up ghostscript-9.07/psi/imain.c.cve-2018-16541 ghostscript-9.07/psi/imain.c
09061c
--- ghostscript-9.07/psi/imain.c.cve-2018-16541	2018-11-29 15:54:54.640496328 +0100
09061c
+++ ghostscript-9.07/psi/imain.c	2018-11-29 15:56:00.652563801 +0100
09061c
@@ -846,6 +846,16 @@ gs_main_finit(gs_main_instance * minst,
09061c
             i_ctx_p = minst->i_ctx_p; /* interp_reclaim could change it. */
09061c
         }
09061c
 #ifndef PSI_INCLUDED
09061c
+        if (i_ctx_p->pgs != NULL && i_ctx_p->pgs->device != NULL &&
09061c
+            gx_device_is_null(i_ctx_p->pgs->device)) {
09061c
+            /* if the job replaced the device with the nulldevice, we we need to grestore
09061c
+               away that device, so the block below can properly dispense
09061c
+               with the default device.
09061c
+             */
09061c
+            int code = gs_grestoreall(i_ctx_p->pgs);
09061c
+            if (code < 0) return_error(gs_error_Fatal);
09061c
+        }
09061c
+
09061c
         if (i_ctx_p->pgs != NULL && i_ctx_p->pgs->device != NULL) {
09061c
             gx_device *pdev = i_ctx_p->pgs->device;
09061c
             const char * dname = pdev->dname;