0007-Bug-699927-don-t-include-operator-arrays-in-execstac.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. From 430f39144244ba4fd7b720cf87031e415e0fabce Mon Sep 17 00:00:00 2001
  2. From: Chris Liddell <chris.liddell@artifex.com>
  3. Date: Mon, 5 Nov 2018 15:42:52 +0800
  4. Subject: [PATCH 2/2] Bug 699927: don't include operator arrays in execstack
  5. output
  6. When we transfer the contents of the execution stack into the array, take the
  7. extra step of replacing any operator arrays on the stack with the operator
  8. that reference them.
  9. This prevents the contents of Postscript defined, internal only operators (those
  10. created with .makeoperator) being exposed via execstack (and thus, via error
  11. handling).
  12. This necessitates a change in the resource remapping 'resource', which contains
  13. a procedure which relies on the contents of the operators arrays being present.
  14. As we already had internal-only variants of countexecstack and execstack
  15. (.countexecstack and .execstack) - using those, and leaving thier operation
  16. including the operator arrays means the procedure continues to work correctly.
  17. Both .countexecstack and .execstack are undefined after initialization.
  18. Also, when we store the execstack (or part thereof) for an execstackoverflow
  19. error, make the same oparray/operator substitution as above for execstack.
  20. CVE: CVE-2018-18073
  21. Upstream-Status: Backport [git://git.ghostscript.com/ghostpdl.git]
  22. Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
  23. ---
  24. Resource/Init/gs_init.ps | 4 ++--
  25. Resource/Init/gs_resmp.ps | 2 +-
  26. psi/int.mak | 2 +-
  27. psi/interp.c | 14 +++++++++++---
  28. psi/interp.h | 2 ++
  29. psi/zcontrol.c | 13 ++++++++++---
  30. 6 files changed, 27 insertions(+), 10 deletions(-)
  31. diff --git a/Resource/Init/gs_init.ps b/Resource/Init/gs_init.ps
  32. index 7c71d18..f4c1053 100644
  33. --- a/Resource/Init/gs_init.ps
  34. +++ b/Resource/Init/gs_init.ps
  35. @@ -2191,7 +2191,7 @@ SAFER { .setsafeglobal } if
  36. %% but can be easily restored (just delete the name from the list in the array). In future
  37. %% we may remove the operator and the code implementation entirely.
  38. [
  39. - /.bitadd /.charboxpath /.cond /.countexecstack /.execstack /.runandhide /.popdevicefilter
  40. + /.bitadd /.charboxpath /.cond /.runandhide /.popdevicefilter
  41. /.execfile /.filenamesplit /.file_name_parent
  42. /.setdefaultmatrix /.isprocfilter /.unread /.psstringencode
  43. /.buildsampledfunction /.isencapfunction /.currentaccuratecurves /.currentcurvejoin /.currentdashadapt /.currentdotlength
  44. @@ -2230,7 +2230,7 @@ SAFER { .setsafeglobal } if
  45. /.localvmarray /.localvmdict /.localvmpackedarray /.localvmstring /.systemvmarray /.systemvmdict /.systemvmpackedarray /.systemvmstring /.systemvmfile /.systemvmlibfile
  46. /.systemvmSFD /.settrapparams /.currentsystemparams /.currentuserparams /.getsystemparam /.getuserparam /.setsystemparams /.setuserparams
  47. /.checkpassword /.locale_to_utf8 /.currentglobal /.gcheck /.imagepath
  48. - /.type /.writecvs /.setSMask /.currentSMask
  49. + /.type /.writecvs /.setSMask /.currentSMask /.countexecstack /.execstack
  50. % Used by a free user in the Library of Congress. Apparently this is used to
  51. % draw a partial page, which is then filled in by the results of a barcode
  52. diff --git a/Resource/Init/gs_resmp.ps b/Resource/Init/gs_resmp.ps
  53. index 7cacaf8..9bb4263 100644
  54. --- a/Resource/Init/gs_resmp.ps
  55. +++ b/Resource/Init/gs_resmp.ps
  56. @@ -183,7 +183,7 @@ setpacking
  57. % We don't check them.
  58. currentglobal //false setglobal % <object> bGlobal
  59. - countexecstack array execstack % <object> bGlobal [execstack]
  60. + //false .countexecstack array //false .execstack % <object> bGlobal [execstack]
  61. dup //null exch % <object> bGlobal [execstack] null [execstack]
  62. length 3 sub -1 0 { % <object> bGlobal [execstack] null i
  63. 2 index exch get % <object> bGlobal [execstack] null proc
  64. diff --git a/psi/int.mak b/psi/int.mak
  65. index 5d9b3d5..6ab5bf0 100644
  66. --- a/psi/int.mak
  67. +++ b/psi/int.mak
  68. @@ -323,7 +323,7 @@ $(PSOBJ)zarray.$(OBJ) : $(PSSRC)zarray.c $(OP) $(memory__h)\
  69. $(PSOBJ)zcontrol.$(OBJ) : $(PSSRC)zcontrol.c $(OP) $(string__h)\
  70. $(estack_h) $(files_h) $(ipacked_h) $(iutil_h) $(store_h) $(stream_h)\
  71. - $(INT_MAK) $(MAKEDIRS)
  72. + $(interp_h) $(INT_MAK) $(MAKEDIRS)
  73. $(PSCC) $(PSO_)zcontrol.$(OBJ) $(C_) $(PSSRC)zcontrol.c
  74. $(PSOBJ)zdict.$(OBJ) : $(PSSRC)zdict.c $(OP)\
  75. diff --git a/psi/interp.c b/psi/interp.c
  76. index b70769d..6dc0dda 100644
  77. --- a/psi/interp.c
  78. +++ b/psi/interp.c
  79. @@ -142,7 +142,6 @@ static int oparray_pop(i_ctx_t *);
  80. static int oparray_cleanup(i_ctx_t *);
  81. static int zerrorexec(i_ctx_t *);
  82. static int zfinderrorobject(i_ctx_t *);
  83. -static int errorexec_find(i_ctx_t *, ref *);
  84. static int errorexec_pop(i_ctx_t *);
  85. static int errorexec_cleanup(i_ctx_t *);
  86. static int zsetstackprotect(i_ctx_t *);
  87. @@ -761,7 +760,7 @@ copy_stack(i_ctx_t *i_ctx_p, const ref_stack_t * pstack, int skip, ref * arr)
  88. {
  89. uint size = ref_stack_count(pstack) - skip;
  90. uint save_space = ialloc_space(idmemory);
  91. - int code;
  92. + int code, i;
  93. if (size > 65535)
  94. size = 65535;
  95. @@ -770,6 +769,15 @@ copy_stack(i_ctx_t *i_ctx_p, const ref_stack_t * pstack, int skip, ref * arr)
  96. if (code >= 0)
  97. code = ref_stack_store(pstack, arr, size, 0, 1, true, idmemory,
  98. "copy_stack");
  99. + /* If we are copying the exec stack, try to replace any oparrays with
  100. + * with the operator than references them
  101. + */
  102. + if (pstack == &e_stack) {
  103. + for (i = 0; i < size; i++) {
  104. + if (errorexec_find(i_ctx_p, &arr->value.refs[i]) < 0)
  105. + make_null(&arr->value.refs[i]);
  106. + }
  107. + }
  108. ialloc_set_space(idmemory, save_space);
  109. return code;
  110. }
  111. @@ -1934,7 +1942,7 @@ zfinderrorobject(i_ctx_t *i_ctx_p)
  112. * .errorexec with errobj != null, store it in *perror_object and return 1,
  113. * otherwise return 0;
  114. */
  115. -static int
  116. +int
  117. errorexec_find(i_ctx_t *i_ctx_p, ref *perror_object)
  118. {
  119. long i;
  120. diff --git a/psi/interp.h b/psi/interp.h
  121. index e9275b9..4f551d1 100644
  122. --- a/psi/interp.h
  123. +++ b/psi/interp.h
  124. @@ -91,5 +91,7 @@ void gs_interp_reset(i_ctx_t *i_ctx_p);
  125. /* Define the top-level interface to the interpreter. */
  126. int gs_interpret(i_ctx_t **pi_ctx_p, ref * pref, int user_errors,
  127. int *pexit_code, ref * perror_object);
  128. +int
  129. +errorexec_find(i_ctx_t *i_ctx_p, ref *perror_object);
  130. #endif /* interp_INCLUDED */
  131. diff --git a/psi/zcontrol.c b/psi/zcontrol.c
  132. index 36da22c..0362cf4 100644
  133. --- a/psi/zcontrol.c
  134. +++ b/psi/zcontrol.c
  135. @@ -24,6 +24,7 @@
  136. #include "ipacked.h"
  137. #include "iutil.h"
  138. #include "store.h"
  139. +#include "interp.h"
  140. /* Forward references */
  141. static int check_for_exec(const_os_ptr);
  142. @@ -787,7 +788,7 @@ zexecstack2(i_ctx_t *i_ctx_p)
  143. /* Continuation operator to do the actual transfer. */
  144. /* r_size(op1) was set just above. */
  145. static int
  146. -do_execstack(i_ctx_t *i_ctx_p, bool include_marks, os_ptr op1)
  147. +do_execstack(i_ctx_t *i_ctx_p, bool include_marks, bool include_oparrays, os_ptr op1)
  148. {
  149. os_ptr op = osp;
  150. ref *arefs = op1->value.refs;
  151. @@ -829,6 +830,12 @@ do_execstack(i_ctx_t *i_ctx_p, bool include_marks, os_ptr op1)
  152. strlen(tname), (const byte *)tname);
  153. break;
  154. }
  155. + case t_array:
  156. + case t_shortarray:
  157. + case t_mixedarray:
  158. + if (!include_oparrays && errorexec_find(i_ctx_p, rq) < 0)
  159. + make_null(rq);
  160. + break;
  161. default:
  162. ;
  163. }
  164. @@ -841,14 +848,14 @@ execstack_continue(i_ctx_t *i_ctx_p)
  165. {
  166. os_ptr op = osp;
  167. - return do_execstack(i_ctx_p, false, op);
  168. + return do_execstack(i_ctx_p, false, false, op);
  169. }
  170. static int
  171. execstack2_continue(i_ctx_t *i_ctx_p)
  172. {
  173. os_ptr op = osp;
  174. - return do_execstack(i_ctx_p, op->value.boolval, op - 1);
  175. + return do_execstack(i_ctx_p, op->value.boolval, true, op - 1);
  176. }
  177. /* - .needinput - */
  178. --
  179. 2.7.4