CVE-2016-2381_duplicate_env.diff 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. From 83e7ebed7afa79a2f50eca6b6330eae7c3a02d36 Mon Sep 17 00:00:00 2001
  2. From: Tony Cook <tony@develop-help.com>
  3. Date: Wed, 27 Jan 2016 11:52:15 +1100
  4. Subject: remove duplicate environment variables from environ
  5. If we see duplicate environment variables while iterating over
  6. environ[]:
  7. a) make sure we use the same value in %ENV that getenv() returns.
  8. Previously on a duplicate, %ENV would have the last entry for the name
  9. from environ[], but a typical getenv() would return the first entry.
  10. Rather than assuming all getenv() implementations return the first entry
  11. explicitly call getenv() to ensure they agree.
  12. b) remove duplicate entries from environ
  13. Previously if there was a duplicate definition for a name in environ[]
  14. setting that name in %ENV could result in an unsafe value being passed
  15. to a child process, so ensure environ[] has no duplicates.
  16. Patch-Name: fixes/CVE-2016-2381_duplicate_env.diff
  17. ---
  18. perl.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++--
  19. 1 file changed, 49 insertions(+), 2 deletions(-)
  20. diff --git a/perl.c b/perl.c
  21. index 80a76c2..ed25429 100644
  22. --- a/perl.c
  23. +++ b/perl.c
  24. @@ -4303,23 +4303,70 @@ S_init_postdump_symbols(pTHX_ int argc, char **argv, char **env)
  25. }
  26. if (env) {
  27. char *s, *old_var;
  28. + STRLEN nlen;
  29. SV *sv;
  30. + HV *dups = newHV();
  31. +
  32. for (; *env; env++) {
  33. old_var = *env;
  34. if (!(s = strchr(old_var,'=')) || s == old_var)
  35. continue;
  36. + nlen = s - old_var;
  37. #if defined(MSDOS) && !defined(DJGPP)
  38. *s = '\0';
  39. (void)strupr(old_var);
  40. *s = '=';
  41. #endif
  42. - sv = newSVpv(s+1, 0);
  43. - (void)hv_store(hv, old_var, s - old_var, sv, 0);
  44. + if (hv_exists(hv, old_var, nlen)) {
  45. + const char *name = savepvn(old_var, nlen);
  46. +
  47. + /* make sure we use the same value as getenv(), otherwise code that
  48. + uses getenv() (like setlocale()) might see a different value to %ENV
  49. + */
  50. + sv = newSVpv(PerlEnv_getenv(name), 0);
  51. +
  52. + /* keep a count of the dups of this name so we can de-dup environ later */
  53. + if (hv_exists(dups, name, nlen))
  54. + ++SvIVX(*hv_fetch(dups, name, nlen, 0));
  55. + else
  56. + (void)hv_store(dups, name, nlen, newSViv(1), 0);
  57. +
  58. + Safefree(name);
  59. + }
  60. + else {
  61. + sv = newSVpv(s+1, 0);
  62. + }
  63. + (void)hv_store(hv, old_var, nlen, sv, 0);
  64. if (env_is_not_environ)
  65. mg_set(sv);
  66. }
  67. + if (HvKEYS(dups)) {
  68. + /* environ has some duplicate definitions, remove them */
  69. + HE *entry;
  70. + hv_iterinit(dups);
  71. + while ((entry = hv_iternext_flags(dups, 0))) {
  72. + STRLEN nlen;
  73. + const char *name = HePV(entry, nlen);
  74. + IV count = SvIV(HeVAL(entry));
  75. + IV i;
  76. + SV **valp = hv_fetch(hv, name, nlen, 0);
  77. +
  78. + assert(valp);
  79. +
  80. + /* try to remove any duplicate names, depending on the
  81. + * implementation used in my_setenv() the iteration might
  82. + * not be necessary, but let's be safe.
  83. + */
  84. + for (i = 0; i < count; ++i)
  85. + my_setenv(name, 0);
  86. +
  87. + /* and set it back to the value we set $ENV{name} to */
  88. + my_setenv(name, SvPV_nolen(*valp));
  89. + }
  90. + }
  91. + SvREFCNT_dec_NN(dups);
  92. }
  93. #endif /* USE_ENVIRON_ARRAY */
  94. #endif /* !PERL_MICRO */