You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.8 KiB

  1. BASH PATCH REPORT
  2. =================
  3. Bash-Release: 4.3
  4. Patch-ID: bash43-027
  5. Bug-Reported-by: Florian Weimer <fweimer@redhat.com>
  6. Bug-Reference-ID:
  7. Bug-Reference-URL:
  8. Bug-Description:
  9. This patch changes the encoding bash uses for exported functions to avoid
  10. clashes with shell variables and to avoid depending only on an environment
  11. variable's contents to determine whether or not to interpret it as a shell
  12. function.
  13. Patch (apply with `patch -p0'):
  14. --- a/variables.c
  15. +++ b/variables.c
  16. @@ -83,6 +83,11 @@
  17. #define ifsname(s) ((s)[0] == 'I' && (s)[1] == 'F' && (s)[2] == 'S' && (s)[3] == '\0')
  18. +#define BASHFUNC_PREFIX "BASH_FUNC_"
  19. +#define BASHFUNC_PREFLEN 10 /* == strlen(BASHFUNC_PREFIX */
  20. +#define BASHFUNC_SUFFIX "%%"
  21. +#define BASHFUNC_SUFFLEN 2 /* == strlen(BASHFUNC_SUFFIX) */
  22. +
  23. extern char **environ;
  24. /* Variables used here and defined in other files. */
  25. @@ -279,7 +284,7 @@ static void push_temp_var __P((PTR_T));
  26. static void propagate_temp_var __P((PTR_T));
  27. static void dispose_temporary_env __P((sh_free_func_t *));
  28. -static inline char *mk_env_string __P((const char *, const char *));
  29. +static inline char *mk_env_string __P((const char *, const char *, int));
  30. static char **make_env_array_from_var_list __P((SHELL_VAR **));
  31. static char **make_var_export_array __P((VAR_CONTEXT *));
  32. static char **make_func_export_array __P((void));
  33. @@ -349,22 +354,33 @@ initialize_shell_variables (env, privmod
  34. /* If exported function, define it now. Don't import functions from
  35. the environment in privileged mode. */
  36. - if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4))
  37. + if (privmode == 0 && read_but_dont_execute == 0 &&
  38. + STREQN (BASHFUNC_PREFIX, name, BASHFUNC_PREFLEN) &&
  39. + STREQ (BASHFUNC_SUFFIX, name + char_index - BASHFUNC_SUFFLEN) &&
  40. + STREQN ("() {", string, 4))
  41. {
  42. + size_t namelen;
  43. + char *tname; /* desired imported function name */
  44. +
  45. + namelen = char_index - BASHFUNC_PREFLEN - BASHFUNC_SUFFLEN;
  46. +
  47. + tname = name + BASHFUNC_PREFLEN; /* start of func name */
  48. + tname[namelen] = '\0'; /* now tname == func name */
  49. +
  50. string_length = strlen (string);
  51. - temp_string = (char *)xmalloc (3 + string_length + char_index);
  52. + temp_string = (char *)xmalloc (namelen + string_length + 2);
  53. - strcpy (temp_string, name);
  54. - temp_string[char_index] = ' ';
  55. - strcpy (temp_string + char_index + 1, string);
  56. + memcpy (temp_string, tname, namelen);
  57. + temp_string[namelen] = ' ';
  58. + memcpy (temp_string + namelen + 1, string, string_length + 1);
  59. /* Don't import function names that are invalid identifiers from the
  60. environment, though we still allow them to be defined as shell
  61. variables. */
  62. - if (legal_identifier (name))
  63. - parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
  64. + if (absolute_program (tname) == 0 && (posixly_correct == 0 || legal_identifier (tname)))
  65. + parse_and_execute (temp_string, tname, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
  66. - if (temp_var = find_function (name))
  67. + if (temp_var = find_function (tname))
  68. {
  69. VSETATTR (temp_var, (att_exported|att_imported));
  70. array_needs_making = 1;
  71. @@ -377,8 +393,11 @@ initialize_shell_variables (env, privmod
  72. array_needs_making = 1;
  73. }
  74. last_command_exit_value = 1;
  75. - report_error (_("error importing function definition for `%s'"), name);
  76. + report_error (_("error importing function definition for `%s'"), tname);
  77. }
  78. +
  79. + /* Restore original suffix */
  80. + tname[namelen] = BASHFUNC_SUFFIX[0];
  81. }
  82. #if defined (ARRAY_VARS)
  83. # if ARRAY_EXPORT
  84. @@ -2954,7 +2973,7 @@ assign_in_env (word, flags)
  85. var->context = variable_context; /* XXX */
  86. INVALIDATE_EXPORTSTR (var);
  87. - var->exportstr = mk_env_string (name, value);
  88. + var->exportstr = mk_env_string (name, value, 0);
  89. array_needs_making = 1;
  90. @@ -3852,21 +3871,42 @@ merge_temporary_env ()
  91. /* **************************************************************** */
  92. static inline char *
  93. -mk_env_string (name, value)
  94. +mk_env_string (name, value, isfunc)
  95. const char *name, *value;
  96. + int isfunc;
  97. {
  98. - int name_len, value_len;
  99. - char *p;
  100. + size_t name_len, value_len;
  101. + char *p, *q;
  102. name_len = strlen (name);
  103. value_len = STRLEN (value);
  104. - p = (char *)xmalloc (2 + name_len + value_len);
  105. - strcpy (p, name);
  106. - p[name_len] = '=';
  107. +
  108. + /* If we are exporting a shell function, construct the encoded function
  109. + name. */
  110. + if (isfunc && value)
  111. + {
  112. + p = (char *)xmalloc (BASHFUNC_PREFLEN + name_len + BASHFUNC_SUFFLEN + value_len + 2);
  113. + q = p;
  114. + memcpy (q, BASHFUNC_PREFIX, BASHFUNC_PREFLEN);
  115. + q += BASHFUNC_PREFLEN;
  116. + memcpy (q, name, name_len);
  117. + q += name_len;
  118. + memcpy (q, BASHFUNC_SUFFIX, BASHFUNC_SUFFLEN);
  119. + q += BASHFUNC_SUFFLEN;
  120. + }
  121. + else
  122. + {
  123. + p = (char *)xmalloc (2 + name_len + value_len);
  124. + memcpy (p, name, name_len);
  125. + q = p + name_len;
  126. + }
  127. +
  128. + q[0] = '=';
  129. if (value && *value)
  130. - strcpy (p + name_len + 1, value);
  131. + memcpy (q + 1, value, value_len + 1);
  132. else
  133. - p[name_len + 1] = '\0';
  134. + q[1] = '\0';
  135. +
  136. return (p);
  137. }
  138. @@ -3952,7 +3992,7 @@ make_env_array_from_var_list (vars)
  139. /* Gee, I'd like to get away with not using savestring() if we're
  140. using the cached exportstr... */
  141. list[list_index] = USE_EXPORTSTR ? savestring (value)
  142. - : mk_env_string (var->name, value);
  143. + : mk_env_string (var->name, value, function_p (var));
  144. if (USE_EXPORTSTR == 0)
  145. SAVE_EXPORTSTR (var, list[list_index]);
  146. --- a/patchlevel.h
  147. +++ b/patchlevel.h
  148. @@ -25,6 +25,6 @@
  149. regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
  150. looks for to find the patch level (for the sccs version string). */
  151. -#define PATCHLEVEL 26
  152. +#define PATCHLEVEL 27
  153. #endif /* _PATCHLEVEL_H_ */