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

483aca
From c76bf1cb7cc419534006631ea139ae20801fd824 Mon Sep 17 00:00:00 2001
483aca
From: Nancy Durgin <nancy.durgin@artifex.com>
483aca
Date: Tue, 18 Sep 2018 11:54:58 -0700
483aca
Subject: [PATCH 1/3] Undefine some additional internal operators.
483aca
483aca
.type, .writecvs, .setSMask, .currentSMask
483aca
483aca
These don't seem to be referenced anywhere outside of the initialization code,
483aca
which binds their usages.  Passes cluster if they are removed.
483aca
---
483aca
 Resource/Init/gs_init.ps | 3 ++-
483aca
 1 file changed, 2 insertions(+), 1 deletion(-)
483aca
483aca
diff --git a/Resource/Init/gs_init.ps b/Resource/Init/gs_init.ps
483aca
index 55d6923..4aff036 100644
483aca
--- a/Resource/Init/gs_init.ps
483aca
+++ b/Resource/Init/gs_init.ps
483aca
@@ -2212,6 +2212,7 @@ SAFER { .setsafeglobal } if
483aca
   /.localvmarray /.localvmdict /.localvmpackedarray /.localvmstring /.systemvmarray /.systemvmdict /.systemvmpackedarray /.systemvmstring /.systemvmfile /.systemvmlibfile
483aca
   /.systemvmSFD /.settrapparams /.currentsystemparams /.currentuserparams /.getsystemparam /.getuserparam /.setsystemparams /.setuserparams
483aca
   /.checkpassword /.locale_to_utf8 /.currentglobal /.gcheck /.imagepath /.currentoutputdevice
483aca
+  /.type /.writecvs /.setSMask /.currentSMask
483aca
 
483aca
   % Used by a free user in the Library of Congress. Apparently this is used to
483aca
   % draw a partial page, which is then filled in by the results of a barcode
483aca
@@ -2230,7 +2231,7 @@ SAFER { .setsafeglobal } if
483aca
   % test files/utilities, or engineers expressed a desire to keep them visible.
483aca
   %
483aca
   %/currentdevice /.sort /.buildfont0 /.buildfont1 /.buildfont2 /.buildfont3 /.buildfont4 /.buildfont9 /.buildfont10 /.buildfont11
483aca
-  %/.buildfotn32 /.buildfont42 /.type9mapcid /.type11mapcid /.swapcolors
483aca
+  %/.buildfont32 /.buildfont42 /.type9mapcid /.type11mapcid /.swapcolors
483aca
   %/currentdevice  /.quit /.setuseciecolor /.needinput /.setoverprintmode /.special_op /.dicttomark /.knownget
483aca
   %/.FAPIavailable /.FAPIpassfont /.FAPIrebuildfont /.FAPIBuildGlyph /.FAPIBuildChar /.FAPIBuildGlyph9
483aca
   %/.tempfile /.numicc_components /.set_outputintent  /.max /.min /.vmreclaim /.getpath /.setglobal
483aca
-- 
483aca
2.17.2
483aca
483aca
483aca
From f8ccc7dfb990336b1ca55f65f2e1a8ecdcb76adf Mon Sep 17 00:00:00 2001
483aca
From: Chris Liddell <chris.liddell@artifex.com>
483aca
Date: Tue, 25 Sep 2018 15:38:14 +0100
483aca
Subject: [PATCH 2/3] Bug 699793: Hide the .needinput operator
483aca
483aca
This removes the .needinput operator from systemdict, ensuring it can only
483aca
be used in the initialization code, and not called erroneously from random
483aca
Postscript.
483aca
---
483aca
 Resource/Init/gs_init.ps | 20 +++++++++++++++++---
483aca
 1 file changed, 17 insertions(+), 3 deletions(-)
483aca
483aca
diff --git a/Resource/Init/gs_init.ps b/Resource/Init/gs_init.ps
483aca
index 4aff036..6a5c1df 100644
483aca
--- a/Resource/Init/gs_init.ps
483aca
+++ b/Resource/Init/gs_init.ps
483aca
@@ -831,12 +831,26 @@ userdict /.currentresourcefile //null put
483aca
 /.runstring {
483aca
   0 0 .systemvmstring .systemvmSFD cvx { .runexec } execute0
483aca
 } bind def
483aca
+
483aca
 % Define the procedure that the C code uses to set up for executing
483aca
 % a string that may be received in pieces.
483aca
+%
483aca
+% Immediate evaluation doesn't work on operators (like .needinput)
483aca
+% so calling .runstringbegin will throw an undefined error if we
483aca
+% undefined .needinput so it cannot be accessed outside the init
483aca
+% code. But, we can store the operator in an array, use immediate
483aca
+% evaluation on the array to get the operator, then undefined the
483aca
+% array (and because they are both of the same name, the operator
483aca
+% get undefined too).
483aca
+% This prevents random Postscript from erroneously calling .needinput
483aca
+% and forcing the interpreter into an invalid state.
483aca
+/.needinput
483aca
+1 .systemvmarray dup 0 /.needinput load put
483aca
+def
483aca
 /.runstringbegin {
483aca
-  1 .systemvmarray dup 0 /.needinput load put cvx	% { .needinput } in systemvm
483aca
+  1 .systemvmarray dup 0 //.needinput 0 get put cvx	% { .needinput } in systemvm
483aca
   0 0 .systemvmstring .systemvmSFD cvx .runexec
483aca
-} bind def
483aca
+} bind executeonly def
483aca
 
483aca
 % Define a special version of runlibfile that aborts on errors.
483aca
 /runlibfile0
483aca
@@ -2212,7 +2226,7 @@ SAFER { .setsafeglobal } if
483aca
   /.localvmarray /.localvmdict /.localvmpackedarray /.localvmstring /.systemvmarray /.systemvmdict /.systemvmpackedarray /.systemvmstring /.systemvmfile /.systemvmlibfile
483aca
   /.systemvmSFD /.settrapparams /.currentsystemparams /.currentuserparams /.getsystemparam /.getuserparam /.setsystemparams /.setuserparams
483aca
   /.checkpassword /.locale_to_utf8 /.currentglobal /.gcheck /.imagepath /.currentoutputdevice
483aca
-  /.type /.writecvs /.setSMask /.currentSMask
483aca
+  /.type /.writecvs /.setSMask /.currentSMask /.needinput
483aca
 
483aca
   % Used by a free user in the Library of Congress. Apparently this is used to
483aca
   % draw a partial page, which is then filled in by the results of a barcode
483aca
-- 
483aca
2.17.2
483aca
483aca
483aca
From 34cc326eb2c5695833361887fe0b32e8d987741c Mon Sep 17 00:00:00 2001
483aca
From: Chris Liddell <chris.liddell@artifex.com>
483aca
Date: Wed, 10 Oct 2018 15:38:10 +0100
483aca
Subject: [PATCH 3/3] Bug 699927: don't include operator arrays in execstack
483aca
 output
483aca
483aca
When we transfer the contents of the execution stack into the array, take the
483aca
extra step of replacing any operator arrays on the stack with the operator
483aca
that reference them.
483aca
483aca
This prevents the contents of Postscript defined, internal only operators (those
483aca
created with .makeoperator) being exposed via execstack (and thus, via error
483aca
handling).
483aca
483aca
This necessitates a change in the resource remapping 'resource', which contains
483aca
a procedure which relies on the contents of the operators arrays being present.
483aca
As we already had internal-only variants of countexecstack and execstack
483aca
(.countexecstack and .execstack) - using those, and leaving thier operation
483aca
including the operator arrays means the procedure continues to work correctly.
483aca
483aca
Both .countexecstack and .execstack are undefined after initialization.
483aca
483aca
Also, when we store the execstack (or part thereof) for an execstackoverflow
483aca
error, make the same oparray/operator substitution as above for execstack.
483aca
---
483aca
 Resource/Init/gs_init.ps  |  4 ++--
483aca
 Resource/Init/gs_resmp.ps |  2 +-
483aca
 psi/int.mak               |  2 +-
483aca
 psi/interp.c              | 14 +++++++++++---
483aca
 psi/interp.h              |  2 ++
483aca
 psi/zcontrol.c            | 13 ++++++++++---
483aca
 6 files changed, 27 insertions(+), 10 deletions(-)
483aca
483aca
diff --git a/Resource/Init/gs_init.ps b/Resource/Init/gs_init.ps
483aca
index 6a5c1df..5bec480 100644
483aca
--- a/Resource/Init/gs_init.ps
483aca
+++ b/Resource/Init/gs_init.ps
483aca
@@ -2187,7 +2187,7 @@ SAFER { .setsafeglobal } if
483aca
   %% but can be easily restored (just delete the name from the list in the array). In future
483aca
   %% we may remove the operator and the code implementation entirely.
483aca
   [
483aca
-  /.bitadd /.charboxpath /.cond /.countexecstack /.execstack /.runandhide /.popdevicefilter
483aca
+  /.bitadd /.charboxpath /.cond /.runandhide /.popdevicefilter
483aca
   /.execfile /.filenamesplit /.file_name_parent
483aca
   /.setdefaultmatrix /.isprocfilter /.unread /.psstringencode
483aca
   /.buildsampledfunction /.isencapfunction /.currentaccuratecurves /.currentcurvejoin /.currentdashadapt /.currentdotlength
483aca
@@ -2226,7 +2226,7 @@ SAFER { .setsafeglobal } if
483aca
   /.localvmarray /.localvmdict /.localvmpackedarray /.localvmstring /.systemvmarray /.systemvmdict /.systemvmpackedarray /.systemvmstring /.systemvmfile /.systemvmlibfile
483aca
   /.systemvmSFD /.settrapparams /.currentsystemparams /.currentuserparams /.getsystemparam /.getuserparam /.setsystemparams /.setuserparams
483aca
   /.checkpassword /.locale_to_utf8 /.currentglobal /.gcheck /.imagepath /.currentoutputdevice
483aca
-  /.type /.writecvs /.setSMask /.currentSMask /.needinput
483aca
+  /.type /.writecvs /.setSMask /.currentSMask /.needinput /.countexecstack /.execstack
483aca
 
483aca
   % Used by a free user in the Library of Congress. Apparently this is used to
483aca
   % draw a partial page, which is then filled in by the results of a barcode
483aca
diff --git a/Resource/Init/gs_resmp.ps b/Resource/Init/gs_resmp.ps
483aca
index 7cacaf8..9bb4263 100644
483aca
--- a/Resource/Init/gs_resmp.ps
483aca
+++ b/Resource/Init/gs_resmp.ps
483aca
@@ -183,7 +183,7 @@ setpacking
483aca
   % We don't check them.
483aca
 
483aca
   currentglobal //false setglobal                  % <object> bGlobal
483aca
-  countexecstack array execstack                   % <object> bGlobal [execstack]
483aca
+  //false .countexecstack array //false .execstack % <object> bGlobal [execstack]
483aca
   dup //null exch                                  % <object> bGlobal [execstack] null [execstack]
483aca
   length 3 sub -1 0 {                              % <object> bGlobal [execstack] null i
483aca
     2 index exch get                               % <object> bGlobal [execstack] null proc
483aca
diff --git a/psi/int.mak b/psi/int.mak
483aca
index 5d9b3d5..6ab5bf0 100644
483aca
--- a/psi/int.mak
483aca
+++ b/psi/int.mak
483aca
@@ -323,7 +323,7 @@ $(PSOBJ)zarray.$(OBJ) : $(PSSRC)zarray.c $(OP) $(memory__h)\
483aca
 
483aca
 $(PSOBJ)zcontrol.$(OBJ) : $(PSSRC)zcontrol.c $(OP) $(string__h)\
483aca
  $(estack_h) $(files_h) $(ipacked_h) $(iutil_h) $(store_h) $(stream_h)\
483aca
- $(INT_MAK) $(MAKEDIRS)
483aca
+ $(interp_h) $(INT_MAK) $(MAKEDIRS)
483aca
 	$(PSCC) $(PSO_)zcontrol.$(OBJ) $(C_) $(PSSRC)zcontrol.c
483aca
 
483aca
 $(PSOBJ)zdict.$(OBJ) : $(PSSRC)zdict.c $(OP)\
483aca
diff --git a/psi/interp.c b/psi/interp.c
483aca
index 3dd5f7a..1dec9b6 100644
483aca
--- a/psi/interp.c
483aca
+++ b/psi/interp.c
483aca
@@ -142,7 +142,6 @@ static int oparray_pop(i_ctx_t *);
483aca
 static int oparray_cleanup(i_ctx_t *);
483aca
 static int zerrorexec(i_ctx_t *);
483aca
 static int zfinderrorobject(i_ctx_t *);
483aca
-static int errorexec_find(i_ctx_t *, ref *);
483aca
 static int errorexec_pop(i_ctx_t *);
483aca
 static int errorexec_cleanup(i_ctx_t *);
483aca
 static int zsetstackprotect(i_ctx_t *);
483aca
@@ -737,7 +736,7 @@ copy_stack(i_ctx_t *i_ctx_p, const ref_stack_t * pstack, int skip, ref * arr)
483aca
 {
483aca
     uint size = ref_stack_count(pstack) - skip;
483aca
     uint save_space = ialloc_space(idmemory);
483aca
-    int code;
483aca
+    int code, i;
483aca
 
483aca
     if (size > 65535)
483aca
         size = 65535;
483aca
@@ -746,6 +745,15 @@ copy_stack(i_ctx_t *i_ctx_p, const ref_stack_t * pstack, int skip, ref * arr)
483aca
     if (code >= 0)
483aca
         code = ref_stack_store(pstack, arr, size, 0, 1, true, idmemory,
483aca
                                "copy_stack");
483aca
+    /* If we are copying the exec stack, try to replace any oparrays with
483aca
+     * with the operator than references them
483aca
+     */
483aca
+    if (pstack == &e_stack) {
483aca
+        for (i = 0; i < size; i++) {
483aca
+            if (errorexec_find(i_ctx_p, &arr->value.refs[i]) < 0)
483aca
+                make_null(&arr->value.refs[i]);
483aca
+        }
483aca
+    }
483aca
     ialloc_set_space(idmemory, save_space);
483aca
     return code;
483aca
 }
483aca
@@ -1910,7 +1918,7 @@ zfinderrorobject(i_ctx_t *i_ctx_p)
483aca
  * .errorexec with errobj != null, store it in *perror_object and return 1,
483aca
  * otherwise return 0;
483aca
  */
483aca
-static int
483aca
+int
483aca
 errorexec_find(i_ctx_t *i_ctx_p, ref *perror_object)
483aca
 {
483aca
     long i;
483aca
diff --git a/psi/interp.h b/psi/interp.h
483aca
index e9275b9..4f551d1 100644
483aca
--- a/psi/interp.h
483aca
+++ b/psi/interp.h
483aca
@@ -91,5 +91,7 @@ void gs_interp_reset(i_ctx_t *i_ctx_p);
483aca
 /* Define the top-level interface to the interpreter. */
483aca
 int gs_interpret(i_ctx_t **pi_ctx_p, ref * pref, int user_errors,
483aca
                  int *pexit_code, ref * perror_object);
483aca
+int
483aca
+errorexec_find(i_ctx_t *i_ctx_p, ref *perror_object);
483aca
 
483aca
 #endif /* interp_INCLUDED */
483aca
diff --git a/psi/zcontrol.c b/psi/zcontrol.c
483aca
index 36da22c..0362cf4 100644
483aca
--- a/psi/zcontrol.c
483aca
+++ b/psi/zcontrol.c
483aca
@@ -24,6 +24,7 @@
483aca
 #include "ipacked.h"
483aca
 #include "iutil.h"
483aca
 #include "store.h"
483aca
+#include "interp.h"
483aca
 
483aca
 /* Forward references */
483aca
 static int check_for_exec(const_os_ptr);
483aca
@@ -787,7 +788,7 @@ zexecstack2(i_ctx_t *i_ctx_p)
483aca
 /* Continuation operator to do the actual transfer. */
483aca
 /* r_size(op1) was set just above. */
483aca
 static int
483aca
-do_execstack(i_ctx_t *i_ctx_p, bool include_marks, os_ptr op1)
483aca
+do_execstack(i_ctx_t *i_ctx_p, bool include_marks, bool include_oparrays, os_ptr op1)
483aca
 {
483aca
     os_ptr op = osp;
483aca
     ref *arefs = op1->value.refs;
483aca
@@ -829,6 +830,12 @@ do_execstack(i_ctx_t *i_ctx_p, bool include_marks, os_ptr op1)
483aca
                                   strlen(tname), (const byte *)tname);
483aca
                 break;
483aca
             }
483aca
+            case t_array:
483aca
+            case t_shortarray:
483aca
+            case t_mixedarray:
483aca
+                if (!include_oparrays && errorexec_find(i_ctx_p, rq) < 0)
483aca
+                    make_null(rq);
483aca
+                break;
483aca
             default:
483aca
                 ;
483aca
         }
483aca
@@ -841,14 +848,14 @@ execstack_continue(i_ctx_t *i_ctx_p)
483aca
 {
483aca
     os_ptr op = osp;
483aca
 
483aca
-    return do_execstack(i_ctx_p, false, op);
483aca
+    return do_execstack(i_ctx_p, false, false, op);
483aca
 }
483aca
 static int
483aca
 execstack2_continue(i_ctx_t *i_ctx_p)
483aca
 {
483aca
     os_ptr op = osp;
483aca
 
483aca
-    return do_execstack(i_ctx_p, op->value.boolval, op - 1);
483aca
+    return do_execstack(i_ctx_p, op->value.boolval, true, op - 1);
483aca
 }
483aca
 
483aca
 /* - .needinput - */
483aca
-- 
483aca
2.17.2
483aca