e76b7b9
From daf85701dab05f17e924a48a81edc9195b4a04e8 Mon Sep 17 00:00:00 2001
e76b7b9
From: Ken Sharp <ken.sharp@artifex.com>
e76b7b9
Date: Wed, 21 Dec 2016 16:54:14 +0000
e76b7b9
Subject: [PATCH] fix crash with bad data supplied to makeimagedevice
e76b7b9
e76b7b9
Bug #697450 "Null pointer dereference in gx_device_finalize()"
e76b7b9
e76b7b9
The problem here is that the code to finalise a device unconditionally
e76b7b9
frees the icc_struct member of the device structure. However this
e76b7b9
particular (weird) device is not setup as a normal device, probably
e76b7b9
because its very, very ancient. Its possible for the initialisation
e76b7b9
of the device to abort with an error before calling gs_make_mem_device()
e76b7b9
which is where the icc_struct member gets allocated (or set to NULL).
e76b7b9
e76b7b9
If that happens, then the cleanup code tries to free the device, which
e76b7b9
calls finalize() which tries to free a garbage pointer.
e76b7b9
e76b7b9
Setting the device memory to 0x00 after we allocate it means that the
e76b7b9
icc_struct member will be NULL< and our memory manager allows for that
e76b7b9
happily enough, which avoids the problem.
e76b7b9
---
e76b7b9
 base/gsdevmem.c | 12 ++++++++++++
e76b7b9
 1 file changed, 12 insertions(+)
e76b7b9
e76b7b9
diff --git a/base/gsdevmem.c b/base/gsdevmem.c
e76b7b9
index 97b9cf4..fe75bcc 100644
e76b7b9
--- a/base/gsdevmem.c
e76b7b9
+++ b/base/gsdevmem.c
e76b7b9
@@ -225,6 +225,18 @@ gs_makewordimagedevice(gx_device ** pnew_dev, const gs_matrix * pmat,
e76b7b9
 
e76b7b9
     if (pnew == 0)
e76b7b9
         return_error(gs_error_VMerror);
e76b7b9
+
e76b7b9
+    /* Bug #697450 "Null pointer dereference in gx_device_finalize()"
e76b7b9
+     * If we have incorrect data passed to gs_initialise_wordimagedevice() then the
e76b7b9
+     * initialisation will fail, crucially it will fail *before* it calls
e76b7b9
+     * gs_make_mem_device() which initialises the device. This means that the
e76b7b9
+     * icc_struct member will be uninitialsed, but the device finalise method
e76b7b9
+     * will unconditionally free that memory. Since its a garbage pointer, bad things happen.
e76b7b9
+     * Apparently we do still need makeimagedevice to be available from
e76b7b9
+     * PostScript, so in here just zero the device memory, which means that
e76b7b9
+     * the finalise routine won't have a problem.
e76b7b9
+     */
e76b7b9
+    memset(pnew, 0x00, st_device_memory.ssize);
e76b7b9
     code = gs_initialize_wordimagedevice(pnew, pmat, width, height,
e76b7b9
                                          colors, num_colors, word_oriented,
e76b7b9
                                          page_device, mem);
e76b7b9
-- 
e76b7b9
2.9.3
e76b7b9