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

09061c
From: Chris Liddell <chris.liddell@artifex.com>
09061c
Date: Sat, 29 Sep 2018 14:34:55 +0000 (+0100)
09061c
Subject: Bug 699816: Improve hiding of security critical custom operators
09061c
09061c
Bug 699816: Improve hiding of security critical custom operators
09061c
09061c
Make procedures that use .forceput/.forcedef/.forceundef into operators.
09061c
09061c
The result of this is that errors get reported against the "top" operator,
09061c
rather than the "called" operator within the procedure.
09061c
09061c
For example:
09061c
/myproc
09061c
{
09061c
  myop
09061c
} bind def
09061c
09061c
If 'myop' throws an error, the error handler will be passed the 'myop'
09061c
operator. Promoting 'myproc' to a operator means the error handler will be
09061c
passed 'myproc'.
09061c
09061c
https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=a54c9e61e7d02bbc620bcba9b1c208462a876afb
09061c
09061c
From: Chris Liddell <chris.liddell@artifex.com>
09061c
Date: Wed, 10 Oct 2018 22:25:51 +0000 (+0100)
09061c
Subject: Bug 699938: .loadfontloop must be an operator
09061c
09061c
Bug 699938: .loadfontloop must be an operator
09061c
09061c
In the fix for Bug 699816, I omitted to make .loadfontloop into an operator, to
09061c
better hide .forceundef and .putgstringcopy.
09061c
09061c
https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=a5a9bf8c6a63aa4ac6874234fe8cd63e72077291
09061c
09061c
From: Chris Liddell <chris.liddell@artifex.com>
09061c
Date: Wed, 28 Nov 2018 17:12:08 +0000 (+0000)
09061c
Subject: Bug 700290: Fix problems with DELAYBIND and font substitution
09061c
09061c
Bug 700290: Fix problems with DELAYBIND and font substitution
09061c
09061c
Judicious use of immediate evaluation for .setnativefontmapbuilt and
09061c
.putgstringcopy to avoid problems with DELAYBIND
09061c
09061c
https://git.ghostscript.com/?p=ghostpdl.git;a=commit;h=2756f0efae1d3966989b15a6526c5d80848b5015
09061c
---
09061c
09061c
diff -up ghostscript-9.07/Resource/Init/gs_diskn.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_diskn.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_diskn.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_diskn.ps	2018-12-10 14:51:03.208407266 +0100
09061c
@@ -53,7 +53,7 @@ systemdict begin
09061c
     exch .setglobal
09061c
   }
09061c
   if
09061c
-} .bind executeonly def % must be bound and hidden for .forceput
09061c
+} .bind executeonly odef % must be bound and hidden for .forceput
09061c
 
09061c
 % Modify .putdevparams to force regeneration of .searchabledevs list
09061c
 /.putdevparams {
09061c
diff -up ghostscript-9.07/Resource/Init/gs_dps.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_dps.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_dps.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_dps.ps	2018-12-10 14:51:03.208407266 +0100
09061c
@@ -71,7 +71,7 @@
09061c
   //true .setglobal
09061c
   //systemdict /savedinitialgstate gstate readonly put
09061c
   .setglobal
09061c
-} .bind def
09061c
+} .bind executeonly odef % must be bound and hidden for .forceput
09061c
 
09061c
 % Initialize local dictionaries and gstate when creating a new context.
09061c
 % Note that until this completes, we are in the anomalous situation of
09061c
diff -up ghostscript-9.07/Resource/Init/gs_fntem.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_fntem.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_fntem.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_fntem.ps	2018-12-10 14:51:03.209407249 +0100
09061c
@@ -432,7 +432,7 @@ currentdict end def
09061c
     exit
09061c
   } loop
09061c
   exch setglobal
09061c
-} bind def
09061c
+} .bind executeonly odef % must be bound and hidden for .forceput
09061c
 
09061c
 currentdict end /ProcSet defineresource pop
09061c
 
09061c
diff -up ghostscript-9.07/Resource/Init/gs_fonts.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_fonts.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_fonts.ps.cve-2018-17961	2018-12-10 14:51:03.002410648 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_fonts.ps	2018-12-10 14:51:03.209407249 +0100
09061c
@@ -375,7 +375,7 @@ FONTPATH length 0 eq { (%END FONTPATH) .
09061c
 % and the access path.
09061c
 /.setnativefontmapbuilt { % set whether we've been run
09061c
   systemdict exch /.nativefontmapbuilt exch .forceput
09061c
-} .bind executeonly def
09061c
+} .bind executeonly odef
09061c
 systemdict /NONATIVEFONTMAP known .setnativefontmapbuilt
09061c
 /.buildnativefontmap {   % - .buildnativefontmap <bool>
09061c
   QUIET not {
09061c
@@ -404,7 +404,7 @@ systemdict /NONATIVEFONTMAP known .setna
09061c
     } forall
09061c
   } if
09061c
   % record that we've been run
09061c
-  //true .setnativefontmapbuilt
09061c
+  //true //.setnativefontmapbuilt
09061c
 } bind def
09061c
 
09061c
 % Create the dictionary that registers the .buildfont procedure
09061c
@@ -1082,7 +1082,7 @@ $error /SubstituteFont { } put
09061c
 
09061c
                 % Check to make sure the font was actually loaded.
09061c
         dup 3 index .fontknownget
09061c
-         { dup /PathLoad 4 index //.putgstringcopy exec
09061c
+         { dup /PathLoad 4 index //.putgstringcopy
09061c
            4 1 roll pop pop pop //true exit
09061c
          } if
09061c
 
09061c
@@ -1094,7 +1094,7 @@ $error /SubstituteFont { } put
09061c
          {            % Stack: origfontname fontdirectory path filefontname
09061c
            2 index 1 index .fontknownget
09061c
             {   % Yes.  Stack: origfontname fontdirectory path filefontname fontdict
09061c
-              dup 4 -1 roll /PathLoad exch //.putgstringcopy exec
09061c
+              dup 4 -1 roll /PathLoad exch //.putgstringcopy
09061c
                       % Stack: origfontname fontdirectory filefontname fontdict
09061c
               3 -1 roll pop exch
09061c
                       % Stack: origfontname fontdict filefontname
09061c
@@ -1122,9 +1122,8 @@ $error /SubstituteFont { } put
09061c
 
09061c
     } loop              % end of loop
09061c
 
09061c
- } bind executeonly def % must be bound and hidden for .putgstringcopy
09061c
-
09061c
-currentdict /.putgstringcopy .undef
09061c
+ } bind executeonly odef % must be bound and hidden for .putgstringcopy
09061c
+currentdict /.putgstringcopy .forceundef
09061c
 
09061c
 % Define a procedure to load all known fonts.
09061c
 % This isn't likely to be very useful.
09061c
diff -up ghostscript-9.07/Resource/Init/gs_lev2.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_lev2.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_lev2.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_lev2.ps	2018-12-10 14:51:03.210407233 +0100
09061c
@@ -163,9 +163,10 @@ end
09061c
         % Set them again to the new values.  From here on, we are safe,
09061c
         % since a context switch will consult userparams.
09061c
   .setuserparams
09061c
-} .bind def
09061c
+} .bind executeonly odef % must be bound and hidden for .forceput
09061c
 /setuserparams {		% <dict> setuserparams -
09061c
-    .setuserparams2
09061c
+    {.setuserparams2} stopped
09061c
+    {/setuserparams load $error /errorname get signalerror} if
09061c
 } .bind odef
09061c
 % Initialize user parameters managed here.
09061c
 /JobName () .definepsuserparam
09061c
@@ -414,7 +415,9 @@ psuserparams /ProcessDSCComment {.checkp
09061c
 
09061c
 % VMReclaim and VMThreshold are user parameters.
09061c
 /setvmthreshold {		% <int> setvmthreshold -
09061c
-  mark /VMThreshold 2 .argindex .dicttomark .setuserparams2 pop
09061c
+  mark /VMThreshold 2 .argindex .dicttomark {.setuserparams2} stopped
09061c
+  {pop /setvmthreshold load $error /errorname get signalerror}
09061c
+  {pop} ifelse
09061c
 } odef
09061c
 /vmreclaim {			% <int> vmreclaim -
09061c
   dup 0 gt {
09061c
@@ -426,7 +429,9 @@ psuserparams /ProcessDSCComment {.checkp
09061c
     ifelse
09061c
   } {
09061c
     % VMReclaim userparam controls enable/disable GC
09061c
-    mark /VMReclaim 2 index .dicttomark .setuserparams2 pop
09061c
+    mark /VMReclaim 2 index .dicttomark {.setuserparams2} stopped
09061c
+    {pop /vmreclaim load $error /errorname get signalerror}
09061c
+    {pop} ifelse
09061c
   } ifelse
09061c
 } odef
09061c
 -1 setvmthreshold
09061c
diff -up ghostscript-9.07/Resource/Init/gs_pdfwr.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_pdfwr.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_pdfwr.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_pdfwr.ps	2018-12-10 14:51:03.210407233 +0100
09061c
@@ -547,8 +547,7 @@ currentdict /.pdfmarkparams .undef
09061c
   } {
09061c
     pop
09061c
   } ifelse
09061c
-}
09061c
-bind def
09061c
+} .bind executeonly odef % must be bound and hidden for .forceput
09061c
 
09061c
 % Use the DSC processing hook to pass DSC comments to the driver.
09061c
 % We use a pseudo-parameter named DSC whose value is an array:
09061c
diff -up ghostscript-9.07/Resource/Init/gs_setpd.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_setpd.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_setpd.ps.cve-2018-17961	2018-12-10 14:51:03.194407496 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_setpd.ps	2018-12-10 14:51:03.210407233 +0100
09061c
@@ -544,6 +544,20 @@ NOMEDIAATTRS {
09061c
 % in the <failed> dictionary with the policy value,
09061c
 % and we replace the key in the <merged> dictionary with its prior value
09061c
 % (or remove it if it had no prior value).
09061c
+
09061c
+% Making this an operator means we can properly hide
09061c
+% the contents - specifically .forceput
09061c
+/1Policy
09061c
+{
09061c
+  % Roll back the failed request to its previous status.
09061c
+  SETPDDEBUG { (Rolling back.) = pstack flush } if
09061c
+  3 index 2 index 3 -1 roll .forceput
09061c
+  4 index 1 index .knownget
09061c
+   { 4 index 3 1 roll .forceput }
09061c
+   { 3 index exch .undef }
09061c
+  ifelse
09061c
+} bind executeonly odef
09061c
+
09061c
 /.policyprocs mark
09061c
 % These procedures are called with the following on the stack:
09061c
 %   <orig> <merged> <failed> <Policies> <key> <policy>
09061c
@@ -567,14 +581,7 @@ NOMEDIAATTRS {
09061c
         /setpagedevice .systemvar /configurationerror signalerror
09061c
       } ifelse
09061c
   } bind
09061c
-  1 {		% Roll back the failed request to its previous status.
09061c
-SETPDDEBUG { (Rolling back.) = pstack flush } if
09061c
-        3 index 2 index 3 -1 roll .forceput
09061c
-        4 index 1 index .knownget
09061c
-         { 4 index 3 1 roll .forceput }
09061c
-         { 3 index exch .undef }
09061c
-        ifelse
09061c
-  } .bind
09061c
+  1 /1Policy load
09061c
   7 {		% For PageSize only, just impose the request.
09061c
         1 index /PageSize eq
09061c
          { pop pop 1 index /PageSize 7 put }
09061c
@@ -582,6 +589,8 @@ SETPDDEBUG { (Rolling back.) = pstack fl
09061c
         ifelse
09061c
   } bind
09061c
 .dicttomark readonly def
09061c
+currentdict /1Policy undef
09061c
+
09061c
 /.applypolicies		% <orig> <merged> <failed> .applypolicies
09061c
                         %   <orig> <merged'> <failed'>
09061c
  { 1 index /Policies get 1 index
09061c
diff -up ghostscript-9.07/Resource/Init/gs_typ32.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_typ32.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_typ32.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_typ32.ps	2018-12-10 14:51:03.211407216 +0100
09061c
@@ -79,15 +79,19 @@ systemdict /.removeglyphs .undef
09061c
 .dicttomark /ProcSet defineresource pop
09061c
 
09061c
 /.cidfonttypes where { pop } { /.cidfonttypes 6 dict def } ifelse
09061c
-.cidfonttypes begin
09061c
-
09061c
-4	% CIDFontType 4 = FontType 32
09061c
-{ dup /FontType 32 .forceput
09061c
+/CIDFontType4
09061c
+{
09061c
+  dup /FontType 32 .forceput
09061c
   dup /CharStrings 20 dict .forceput
09061c
   1 index exch .buildfont32 exch pop
09061c
-} bind def
09061c
+} .bind executeonly odef
09061c
+.cidfonttypes begin
09061c
+
09061c
+
09061c
+4 /CIDFontType4 load def % CIDFontType 4 = FontType 32
09061c
 
09061c
 end		% .cidfonttypes
09061c
+currentdict /CIDFontType4 .forceundef
09061c
 
09061c
 % Define the BuildGlyph procedure.
09061c
 % Since Type 32 fonts are indexed by CID, there is no BuildChar procedure.
09061c
diff -up ghostscript-9.07/Resource/Init/gs_type1.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/gs_type1.ps
09061c
--- ghostscript-9.07/Resource/Init/gs_type1.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/gs_type1.ps	2018-12-10 14:51:03.211407216 +0100
09061c
@@ -215,7 +215,7 @@ currentdict /closesourcedict .undef
09061c
   } if
09061c
   2 copy /WeightVector exch .forceput
09061c
   .setweightvector
09061c
-} .bind executeonly def
09061c
+} .bind executeonly odef
09061c
 end
09061c
 
09061c
 % Register the font types for definefont.
09061c
diff -up ghostscript-9.07/Resource/Init/pdf_base.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/pdf_base.ps
09061c
--- ghostscript-9.07/Resource/Init/pdf_base.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/pdf_base.ps	2018-12-10 14:51:03.211407216 +0100
09061c
@@ -177,7 +177,7 @@ currentdict /num-chars-dict .undef
09061c
       } ifelse
09061c
     } ifelse
09061c
   } ifelse
09061c
-} bind def
09061c
+} bind executeonly odef
09061c
 /PDFScanRules_true << /PDFScanRules //true >> def
09061c
 /PDFScanRules_null << /PDFScanRules //null >> def
09061c
 /.pdfrun {			% <file> <opdict> .pdfrun -
09061c
diff -up ghostscript-9.07/Resource/Init/pdf_draw.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/pdf_draw.ps
09061c
--- ghostscript-9.07/Resource/Init/pdf_draw.ps.cve-2018-17961	2018-12-10 14:51:03.177407775 +0100
09061c
+++ ghostscript-9.07/Resource/Init/pdf_draw.ps	2018-12-10 14:51:03.212407200 +0100
09061c
@@ -948,7 +948,7 @@ currentdict end readonly def
09061c
   Q
09061c
   PDFDEBUG { pdfdict /PDFSTEPcount .knownget { 1 le } { //true } ifelse { (%End PaintProc) print dup === flush } if } if
09061c
   PDFfile exch setfileposition
09061c
-} bdef
09061c
+}bind executeonly odef
09061c
 
09061c
 /resolvepattern {	% <patternstreamdict> resolvepattern <patterndict>
09061c
                 % Don't do the resolvestream now: just capture the data
09061c
@@ -1809,7 +1809,7 @@ currentdict /last-ditch-bpc-csp undef
09061c
   } if
09061c
   pop
09061c
   /pdfemptycount exch store
09061c
-} bdef
09061c
+} bind executeonly odef
09061c
 
09061c
 /_dops_save 1 array def
09061c
 
09061c
diff -up ghostscript-9.07/Resource/Init/pdf_font.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/pdf_font.ps
09061c
--- ghostscript-9.07/Resource/Init/pdf_font.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/pdf_font.ps	2018-12-10 14:51:03.213407183 +0100
09061c
@@ -641,7 +641,7 @@ currentdict end readonly def
09061c
     } if
09061c
   } if
09061c
   pop pop pop
09061c
-} bind def
09061c
+} bind executeonly odef
09061c
 
09061c
 % ---------------- Descriptors ---------------- %
09061c
 
09061c
@@ -1097,7 +1097,7 @@ currentdict /eexec_pdf_param_dict .undef
09061c
     } bdef
09061c
     dup currentdict Encoding .processToUnicode
09061c
     currentdict end .completefont exch pop
09061c
-} bdef
09061c
+} bind executeonly odef
09061c
 /.adjustcharwidth {	% <wx> <wy> .adjustcharwidth <wx'> <wy'>
09061c
   % Enforce the metrics, in glyph space, to the values found in the PDF Font object
09061c
   % - force wy == 0 (assumed, and not stored in the PDF font)
09061c
@@ -1794,7 +1794,7 @@ currentdict /CMap_read_dict undef
09061c
     } if
09061c
     /findresource cvx /undefined signalerror
09061c
   } loop
09061c
-} bdef
09061c
+} bind executeonly odef
09061c
 
09061c
 /buildCIDType0 {	% <CIDFontType0-font-resource> buildCIDType0 <font>
09061c
   dup /BaseFont get findCIDFont exch pop
09061c
@@ -1964,7 +1964,7 @@ currentdict /CMap_read_dict undef
09061c
   /Type0 //buildType0
09061c
   /Type1 //buildType1
09061c
   /MMType1 //buildType1
09061c
-  /Type3 //buildType3
09061c
+  /Type3 /buildType3 load
09061c
   /TrueType //buildTrueType
09061c
   /CIDFontType0 //buildCIDType0
09061c
   /CIDFontType2 //buildCIDType2
09061c
diff -up ghostscript-9.07/Resource/Init/pdf_main.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/pdf_main.ps
09061c
--- ghostscript-9.07/Resource/Init/pdf_main.ps.cve-2018-17961	2018-12-10 14:51:03.168407922 +0100
09061c
+++ ghostscript-9.07/Resource/Init/pdf_main.ps	2018-12-10 14:51:03.213407183 +0100
09061c
@@ -382,7 +382,7 @@ currentdict /runpdfstring .undef
09061c
     } forall
09061c
     pop
09061c
   } ifelse
09061c
-} bind def
09061c
+} bind executeonly odef
09061c
 
09061c
 currentdict /pdf_collection_files .undef
09061c
 
09061c
@@ -1878,7 +1878,7 @@ currentdict /PDF2PS_matrix_key undef
09061c
   Repaired		% pass Repaired state around the restore
09061c
   PDFSave restore
09061c
   /Repaired exch def
09061c
-} bind def
09061c
+} bind executeonly odef
09061c
 
09061c
 % Display the contents of a page (including annotations).
09061c
 /showpagecontents {	% <pagedict> showpagecontents -
09061c
diff -up ghostscript-9.07/Resource/Init/pdf_ops.ps.cve-2018-17961 ghostscript-9.07/Resource/Init/pdf_ops.ps
09061c
--- ghostscript-9.07/Resource/Init/pdf_ops.ps.cve-2018-17961	2013-02-14 08:58:16.000000000 +0100
09061c
+++ ghostscript-9.07/Resource/Init/pdf_ops.ps	2018-12-10 14:51:03.214407167 +0100
09061c
@@ -128,7 +128,7 @@ nodict readonly pop
09061c
   { (\n   **** File has unbalanced q/Q operators \(too many Q's\) ****\n)
09061c
     pdfformaterror
09061c
   } if
09061c
-} bdef
09061c
+} bind executeonly odef
09061c
 
09061c
 % Save PDF gstate
09061c
 /qstate {       % - qstate <qstate>
09061c
@@ -282,7 +282,7 @@ nodict readonly pop
09061c
 } bdef
09061c
 /ca { /FillConstantAlpha gput } bdef
09061c
 /CA { /StrokeConstantAlpha gput } bdef
09061c
-/SMask { /SoftMask gput } bdef
09061c
+/SMask { /SoftMask gput } bind executeonly odef
09061c
 /AIS { /AlphaIsShape gput } bdef
09061c
 /BM {
09061c
   /.setblendmode where {