CVE-2021-43396.patch 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. From ff012870b2c02a62598c04daa1e54632e020fd7d Mon Sep 17 00:00:00 2001
  2. From: Nikita Popov <npv1310@gmail.com>
  3. Date: Tue, 2 Nov 2021 13:21:42 +0500
  4. Subject: [PATCH] gconv: Do not emit spurious NUL character in ISO-2022-JP-3
  5. (bug 28524)
  6. Bugfix 27256 has introduced another issue:
  7. In conversion from ISO-2022-JP-3 encoding, it is possible
  8. to force iconv to emit extra NUL character on internal state reset.
  9. To do this, it is sufficient to feed iconv with escape sequence
  10. which switches active character set.
  11. The simplified check 'data->__statep->__count != ASCII_set'
  12. introduced by the aforementioned bugfix picks that case and
  13. behaves as if '\0' character has been queued thus emitting it.
  14. To eliminate this issue, these steps are taken:
  15. * Restore original condition
  16. '(data->__statep->__count & ~7) != ASCII_set'.
  17. It is necessary since bits 0-2 may contain
  18. number of buffered input characters.
  19. * Check that queued character is not NUL.
  20. Similar step is taken for main conversion loop.
  21. Bundled test case follows following logic:
  22. * Try to convert ISO-2022-JP-3 escape sequence
  23. switching active character set
  24. * Reset internal state by providing NULL as input buffer
  25. * Ensure that nothing has been converted.
  26. Signed-off-by: Nikita Popov <npv1310@gmail.com>
  27. CVE: CVE-2021-43396
  28. Upstream-Status: Backport [ff012870b2c02a62598c04daa1e54632e020fd7d]
  29. ---
  30. iconvdata/Makefile | 5 +++-
  31. iconvdata/bug-iconv15.c | 60 +++++++++++++++++++++++++++++++++++++++
  32. iconvdata/iso-2022-jp-3.c | 28 ++++++++++++------
  33. 3 files changed, 84 insertions(+), 9 deletions(-)
  34. create mode 100644 iconvdata/bug-iconv15.c
  35. Index: git/iconvdata/Makefile
  36. ===================================================================
  37. --- git.orig/iconvdata/Makefile
  38. +++ git/iconvdata/Makefile
  39. @@ -1,4 +1,5 @@
  40. # Copyright (C) 1997-2021 Free Software Foundation, Inc.
  41. +# Copyright (C) The GNU Toolchain Authors.
  42. # This file is part of the GNU C Library.
  43. # The GNU C Library is free software; you can redistribute it and/or
  44. @@ -74,7 +75,7 @@ ifeq (yes,$(build-shared))
  45. tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
  46. tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \
  47. bug-iconv10 bug-iconv11 bug-iconv12 tst-iconv-big5-hkscs-to-2ucs4 \
  48. - bug-iconv13 bug-iconv14
  49. + bug-iconv13 bug-iconv14 bug-iconv15
  50. ifeq ($(have-thread-library),yes)
  51. tests += bug-iconv3
  52. endif
  53. @@ -327,6 +328,8 @@ $(objpfx)bug-iconv12.out: $(addprefix $(
  54. $(addprefix $(objpfx),$(modules.so))
  55. $(objpfx)bug-iconv14.out: $(addprefix $(objpfx), $(gconv-modules)) \
  56. $(addprefix $(objpfx),$(modules.so))
  57. +$(objpfx)bug-iconv15.out: $(addprefix $(objpfx), $(gconv-modules)) \
  58. + $(addprefix $(objpfx),$(modules.so))
  59. $(objpfx)iconv-test.out: run-iconv-test.sh \
  60. $(addprefix $(objpfx), $(gconv-modules)) \
  61. Index: git/iconvdata/bug-iconv15.c
  62. ===================================================================
  63. --- /dev/null
  64. +++ git/iconvdata/bug-iconv15.c
  65. @@ -0,0 +1,60 @@
  66. +/* Bug 28524: Conversion from ISO-2022-JP-3 with iconv
  67. + may emit spurious NUL character on state reset.
  68. + Copyright (C) The GNU Toolchain Authors.
  69. + This file is part of the GNU C Library.
  70. +
  71. + The GNU C Library is free software; you can redistribute it and/or
  72. + modify it under the terms of the GNU Lesser General Public
  73. + License as published by the Free Software Foundation; either
  74. + version 2.1 of the License, or (at your option) any later version.
  75. +
  76. + The GNU C Library is distributed in the hope that it will be useful,
  77. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  78. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  79. + Lesser General Public License for more details.
  80. +
  81. + You should have received a copy of the GNU Lesser General Public
  82. + License along with the GNU C Library; if not, see
  83. + <https://www.gnu.org/licenses/>. */
  84. +
  85. +#include <stddef.h>
  86. +#include <iconv.h>
  87. +#include <support/check.h>
  88. +
  89. +static int
  90. +do_test (void)
  91. +{
  92. + char in[] = "\x1b(I";
  93. + char *inbuf = in;
  94. + size_t inleft = sizeof (in) - 1;
  95. + char out[1];
  96. + char *outbuf = out;
  97. + size_t outleft = sizeof (out);
  98. + iconv_t cd;
  99. +
  100. + cd = iconv_open ("UTF8", "ISO-2022-JP-3");
  101. + TEST_VERIFY_EXIT (cd != (iconv_t) -1);
  102. +
  103. + /* First call to iconv should alter internal state.
  104. + Now, JISX0201_Kana_set is selected and
  105. + state value != ASCII_set. */
  106. + TEST_VERIFY (iconv (cd, &inbuf, &inleft, &outbuf, &outleft) != (size_t) -1);
  107. +
  108. + /* No bytes should have been added to
  109. + the output buffer at this point. */
  110. + TEST_VERIFY (outbuf == out);
  111. + TEST_VERIFY (outleft == sizeof (out));
  112. +
  113. + /* Second call shall emit spurious NUL character in unpatched glibc. */
  114. + TEST_VERIFY (iconv (cd, NULL, NULL, &outbuf, &outleft) != (size_t) -1);
  115. +
  116. + /* No characters are expected to be produced. */
  117. + TEST_VERIFY (outbuf == out);
  118. + TEST_VERIFY (outleft == sizeof (out));
  119. +
  120. + TEST_VERIFY_EXIT (iconv_close (cd) != -1);
  121. +
  122. + return 0;
  123. +}
  124. +
  125. +#include <support/test-driver.c>
  126. Index: git/iconvdata/iso-2022-jp-3.c
  127. ===================================================================
  128. --- git.orig/iconvdata/iso-2022-jp-3.c
  129. +++ git/iconvdata/iso-2022-jp-3.c
  130. @@ -1,5 +1,6 @@
  131. /* Conversion module for ISO-2022-JP-3.
  132. Copyright (C) 1998-2021 Free Software Foundation, Inc.
  133. + Copyright (C) The GNU Toolchain Authors.
  134. This file is part of the GNU C Library.
  135. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998,
  136. and Bruno Haible <bruno@clisp.org>, 2002.
  137. @@ -81,20 +82,31 @@ enum
  138. the output state to the initial state. This has to be done during the
  139. flushing. */
  140. #define EMIT_SHIFT_TO_INIT \
  141. - if (data->__statep->__count != ASCII_set) \
  142. + if ((data->__statep->__count & ~7) != ASCII_set) \
  143. { \
  144. if (FROM_DIRECTION) \
  145. { \
  146. - if (__glibc_likely (outbuf + 4 <= outend)) \
  147. + uint32_t ch = data->__statep->__count >> 6; \
  148. + \
  149. + if (__glibc_unlikely (ch != 0)) \
  150. { \
  151. - /* Write out the last character. */ \
  152. - *((uint32_t *) outbuf) = data->__statep->__count >> 6; \
  153. - outbuf += sizeof (uint32_t); \
  154. - data->__statep->__count = ASCII_set; \
  155. + if (__glibc_likely (outbuf + 4 <= outend)) \
  156. + { \
  157. + /* Write out the last character. */ \
  158. + put32u (outbuf, ch); \
  159. + outbuf += 4; \
  160. + data->__statep->__count &= 7; \
  161. + data->__statep->__count |= ASCII_set; \
  162. + } \
  163. + else \
  164. + /* We don't have enough room in the output buffer. */ \
  165. + status = __GCONV_FULL_OUTPUT; \
  166. } \
  167. else \
  168. - /* We don't have enough room in the output buffer. */ \
  169. - status = __GCONV_FULL_OUTPUT; \
  170. + { \
  171. + data->__statep->__count &= 7; \
  172. + data->__statep->__count |= ASCII_set; \
  173. + } \
  174. } \
  175. else \
  176. { \