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

a60c09
From 693baf02152119af6e6afd30bb8ec76d14f84bbf Mon Sep 17 00:00:00 2001
a60c09
From: Ken Sharp <ken.sharp@artifex.com>
a60c09
Date: Thu, 8 Nov 2018 14:43:32 +0000
a60c09
Subject: [PATCH] PS interpreter - check the Implementation of a Pattern before
a60c09
 use
a60c09
a60c09
Bug #700141 "Type confusion in setpattern"
a60c09
a60c09
As the bug thread says, we were not checking that the Implementation
a60c09
of a pattern dictionary was a structure type, leading to a crash when
a60c09
we tried to treat it as one.
a60c09
a60c09
Here we make the st_pattern1_instance and st_pattern2_instance
a60c09
structures public definitions and in zsetcolor we check the object
a60c09
stored under the Implementation key in the supplied dictionary to see if
a60c09
its a t_struct or t_astruct type, and if it is that its a
a60c09
st_pattern1_instance or st_pattern2_instance structure.
a60c09
a60c09
If either check fails we throw a typecheck error.
a60c09
a60c09
We need to make the st_pattern1_instance and st_pattern2_instance
a60c09
definitions public as they are defined in the graphics library and we
a60c09
need to check in the interpreter.
a60c09
---
a60c09
 base/gsptype1.c |  2 +-
a60c09
 base/gsptype2.c |  6 +++---
a60c09
 base/gsptype2.h |  4 ++--
a60c09
 base/gxcolor2.h |  4 ++--
a60c09
 psi/zcolor.c    | 11 ++++++++---
a60c09
 5 files changed, 16 insertions(+), 11 deletions(-)
a60c09
a60c09
diff --git a/base/gsptype1.c b/base/gsptype1.c
a60c09
index 27fdd5a..e98dde1 100644
a60c09
--- a/base/gsptype1.c
a60c09
+++ b/base/gsptype1.c
a60c09
@@ -50,7 +50,7 @@
a60c09
 
a60c09
 /* GC descriptors */
a60c09
 private_st_pattern1_template();
a60c09
-private_st_pattern1_instance();
a60c09
+public_st_pattern1_instance();
a60c09
 
a60c09
 /* GC procedures */
a60c09
 static ENUM_PTRS_BEGIN(pattern1_instance_enum_ptrs) {
a60c09
diff --git a/base/gsptype2.c b/base/gsptype2.c
a60c09
index 791e538..c53eb2e 100644
a60c09
--- a/base/gsptype2.c
a60c09
+++ b/base/gsptype2.c
a60c09
@@ -33,7 +33,7 @@
a60c09
 
a60c09
 /* GC descriptors */
a60c09
 private_st_pattern2_template();
a60c09
-private_st_pattern2_instance();
a60c09
+public_st_pattern2_instance();
a60c09
 
a60c09
 /* GC procedures */
a60c09
 static ENUM_PTRS_BEGIN(pattern2_instance_enum_ptrs) {
a60c09
@@ -206,10 +206,10 @@ gs_pattern2_set_color(const gs_client_color * pcc, gs_gstate * pgs)
a60c09
 
a60c09
     pinst->saved->overprint_mode = pgs->overprint_mode;
a60c09
     pinst->saved->overprint = pgs->overprint;
a60c09
-    
a60c09
+
a60c09
     num_comps = pgs->device->color_info.num_components;
a60c09
     for (k = 0; k < num_comps; k++) {
a60c09
-        pgs->color_component_map.color_map[k] = 
a60c09
+        pgs->color_component_map.color_map[k] =
a60c09
             pinst->saved->color_component_map.color_map[k];
a60c09
     }
a60c09
     code = pcs->type->set_overprint(pcs, pgs);
a60c09
diff --git a/base/gsptype2.h b/base/gsptype2.h
a60c09
index f0f26d1..4186201 100644
a60c09
--- a/base/gsptype2.h
a60c09
+++ b/base/gsptype2.h
a60c09
@@ -57,8 +57,8 @@ typedef struct gs_pattern2_instance_s {
a60c09
     bool shfill;
a60c09
 } gs_pattern2_instance_t;
a60c09
 
a60c09
-#define private_st_pattern2_instance() /* in gsptype2.c */\
a60c09
-  gs_private_st_composite(st_pattern2_instance, gs_pattern2_instance_t,\
a60c09
+#define public_st_pattern2_instance() /* in gsptype2.c */\
a60c09
+  gs_public_st_composite(st_pattern2_instance, gs_pattern2_instance_t,\
a60c09
     "gs_pattern2_instance_t", pattern2_instance_enum_ptrs,\
a60c09
     pattern2_instance_reloc_ptrs)
a60c09
 
a60c09
diff --git a/base/gxcolor2.h b/base/gxcolor2.h
a60c09
index 62ec05e..d5b1095 100644
a60c09
--- a/base/gxcolor2.h
a60c09
+++ b/base/gxcolor2.h
a60c09
@@ -92,8 +92,8 @@ struct gs_pattern1_instance_s {
a60c09
     gx_bitmap_id id;		/* key for cached bitmap (= id of mask) */
a60c09
 };
a60c09
 
a60c09
-#define private_st_pattern1_instance() /* in gsptype1.c */\
a60c09
-  gs_private_st_composite(st_pattern1_instance, gs_pattern1_instance_t,\
a60c09
+#define public_st_pattern1_instance() /* in gsptype1.c */\
a60c09
+  gs_public_st_composite(st_pattern1_instance, gs_pattern1_instance_t,\
a60c09
     "gs_pattern1_instance_t", pattern1_instance_enum_ptrs,\
a60c09
     pattern1_instance_reloc_ptrs)
a60c09
 
a60c09
diff --git a/psi/zcolor.c b/psi/zcolor.c
a60c09
index 7a00d4e..fe81e79 100644
a60c09
--- a/psi/zcolor.c
a60c09
+++ b/psi/zcolor.c
a60c09
@@ -65,6 +65,8 @@ static const float default_0_1[] = {0, 1, 0, 1, 0, 1, 0, 1};
a60c09
 
a60c09
 /* imported from gsht.c */
a60c09
 extern  void    gx_set_effective_transfer(gs_gstate *);
a60c09
+extern_st(st_pattern1_instance);
a60c09
+extern_st(st_pattern2_instance);
a60c09
 
a60c09
 /* Essential forward declarations */
a60c09
 static int validate_spaces(i_ctx_t *i_ctx_p, ref *arr, int *depth);
a60c09
@@ -289,6 +291,9 @@ zsetcolor(i_ctx_t * i_ctx_p)
a60c09
                 code = array_get(imemory, pImpl, 0, &pPatInst);
a60c09
                 if (code < 0)
a60c09
                     return code;
a60c09
+                if (!r_is_struct(&pPatInst) || (!r_has_stype(&pPatInst, imemory, st_pattern1_instance) && !r_has_stype(&pPatInst, imemory, st_pattern2_instance)))
a60c09
+                    return_error(gs_error_typecheck);
a60c09
+
a60c09
                 cc.pattern = r_ptr(&pPatInst, gs_pattern_instance_t);
a60c09
                 n_numeric_comps = ( pattern_instance_uses_base_space(cc.pattern)
a60c09
                       ? n_comps - 1
a60c09
@@ -4421,7 +4426,7 @@ static int setindexedspace(i_ctx_t * i_ctx_p, ref *r, int *stage, int *cont, int
a60c09
         /* If we have a named color profile and the base space is DeviceN or
a60c09
            Separation use a different set of procedures to ensure the named
a60c09
            color remapping code is used */
a60c09
-        if (igs->icc_manager->device_named != NULL && 
a60c09
+        if (igs->icc_manager->device_named != NULL &&
a60c09
             (base_type == gs_color_space_index_Separation ||
a60c09
              base_type == gs_color_space_index_DeviceN))
a60c09
             pcs = gs_cspace_alloc(imemory, &gs_color_space_type_Indexed_Named);
a60c09
@@ -5573,7 +5578,7 @@ static int iccompareproc(i_ctx_t *i_ctx_p, ref *space, ref *testspace)
a60c09
         return 0;
a60c09
 
a60c09
     /* As a quick check see if current is same as new */
a60c09
-    if (ICCdict1.value.bytes == ICCdict2.value.bytes) 
a60c09
+    if (ICCdict1.value.bytes == ICCdict2.value.bytes)
a60c09
          return 1;
a60c09
 
a60c09
     /* Need to check all the various parts */
a60c09
@@ -5593,7 +5598,7 @@ static int iccompareproc(i_ctx_t *i_ctx_p, ref *space, ref *testspace)
a60c09
     code2 = dict_find_string(&ICCdict2, "DataSource", &tempref2);
a60c09
     if (code2 <= 0)
a60c09
         return 0;
a60c09
-    if (r_size(tempref1) != r_size(tempref2)) 
a60c09
+    if (r_size(tempref1) != r_size(tempref2))
a60c09
         return 0;
a60c09
 
a60c09
     buff_size = r_size(tempref1);
a60c09
-- 
a60c09
2.17.2
a60c09