[COMMITTED 2.43] CVE-2026-121757: Avoid advancing read pointer twice on UHC/CP949 decode error (bug 35016)

Aurelien Jarno aurelien@aurel32.net
Sat Dec 5 14:04:21 GMT 2026


From: Florian Weimer <fweimer@redhat.com>

The call to ksc5601_to_ucs4 advances inptr if the U+327E character is
encountered.  If //IGNORE is active, STANDARD_FROM_LOOP_ERR_HANDLER
(2) advances inptr again, stepping after the end of the buffer.
Restore the previous inptr value before reporting errors.  Passing
zero to STANDARD_FROM_LOOP_ERR_HANDLER on a U+327E character would not
result in the expected behavior for the non-//IGNORE case.

Reported-by: Dickson Wu (SEAL & CertiK)
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
(cherry picked from commit ceaaea8ac1c18c829db40be6cee4895d03d905b3)
---
 NEWS                     |  2 +
 iconvdata/Makefile       |  4 +-
 iconvdata/tst-bug35016.c | 80 ++++++++++++++++++++++++++++++++++++++++
 iconvdata/uhc.c          |  7 +++-
 4 files changed, 90 insertions(+), 3 deletions(-)
 create mode 100644 iconvdata/tst-bug35016.c

diff --git a/NEWS b/NEWS
index b5817de46e..99182f1c1e 100644
--- a/NEWS
+++ b/NEWS
@@ -99,6 +99,8 @@ The following bugs are resolved with this release:
     (CVE-2026-100290)
   [34993] glob: double free in prefix_array after a memory allocation
     failure (CVE-2026-126765)
+  [35016] CVE-2026-121757: Avoid advancing read pointer twice on UHC/CP949
+    decode error
   [35052] math: erfc raises spurious underflow
   [35053] math: math: lgammaf@GLIBC_2.23 writes to signgam since 2.43
 
diff --git a/iconvdata/Makefile b/iconvdata/Makefile
index f05dfe10bf..898050e005 100644
--- a/iconvdata/Makefile
+++ b/iconvdata/Makefile
@@ -77,7 +77,7 @@ tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
 	bug-iconv10 bug-iconv11 bug-iconv12 tst-iconv-big5-hkscs-to-2ucs4 \
 	bug-iconv13 bug-iconv14 bug-iconv15 \
 	tst-iconv-ibm1390 tst-iconv-iso-2022-cn-ext tst-bug33980 \
-	tst-jisx0213-progress
+	tst-jisx0213-progress tst-bug35016
 ifeq ($(have-thread-library),yes)
 tests += bug-iconv3
 endif
@@ -336,6 +336,8 @@ $(objpfx)tst-iconv-iso-2022-cn-ext.out: $(addprefix $(objpfx), $(gconv-modules))
 					$(addprefix $(objpfx),$(modules.so))
 $(objpfx)tst-bug33980.out: $(addprefix $(objpfx), $(gconv-modules)) \
 			   $(addprefix $(objpfx),$(modules.so))
+$(objpfx)tst-bug35016.out: $(addprefix $(objpfx), $(gconv-modules)) \
+			   $(addprefix $(objpfx),$(modules.so))
 $(objpfx)tst-jisx0213-progress.out: \
   $(addprefix $(objpfx), $(gconv-modules)) $(addprefix $(objpfx),$(modules.so))
 $(objpfx)tst-iconv-ibm1390.out: $(addprefix $(objpfx), $(gconv-modules)) \
diff --git a/iconvdata/tst-bug35016.c b/iconvdata/tst-bug35016.c
new file mode 100644
index 0000000000..aa443f17aa
--- /dev/null
+++ b/iconvdata/tst-bug35016.c
@@ -0,0 +1,80 @@
+/* Test that UHC/CP949 decoder stops reading at error (bug 35016).
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <iconv.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/next_to_fault.h>
+
+static int
+do_test (void)
+{
+  struct support_next_to_fault ntf
+    = support_next_to_fault_allocate (2);
+  memcpy (ntf.buffer, "\xa2\xe8", 2);
+
+  /* Test that EILSEQ reporting does not change the input position.  */
+  {
+    iconv_t cd = iconv_open ("UTF-8", "UHC");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+
+    char out_storage[64];
+    char *inbuf = ntf.buffer;
+    size_t inbytesleft = 2;
+    char *outbuf = out_storage;
+    size_t outbytesleft = sizeof (out_storage);
+    errno = 0;
+    TEST_COMPARE (iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft),
+                  (size_t) -1);
+    TEST_COMPARE (errno, EILSEQ);
+    TEST_VERIFY (inbuf == ntf.buffer);
+    TEST_COMPARE (inbytesleft, 2);
+    TEST_VERIFY (outbuf == out_storage);
+    TEST_COMPARE (outbytesleft, sizeof (out_storage));
+
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  /* Test that the decode error advances the input pointer only once.  */
+  {
+    iconv_t cd = iconv_open ("UTF-8//IGNORE", "UHC");
+    TEST_VERIFY_EXIT (cd != (iconv_t) -1);
+
+    char out_storage[64];
+    char *inbuf = ntf.buffer;
+    size_t inbytesleft = 2;
+    char *outbuf = out_storage;
+    size_t outbytesleft = sizeof (out_storage);
+    errno = 0;
+    TEST_COMPARE (iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft),
+                  (size_t) -1);
+    TEST_COMPARE (errno, EILSEQ);
+    TEST_VERIFY (inbuf == ntf.buffer + 2);
+    TEST_COMPARE (inbytesleft, 0);
+    TEST_VERIFY (outbuf == out_storage);
+    TEST_COMPARE (outbytesleft, sizeof (out_storage));
+
+    TEST_COMPARE (iconv_close (cd), 0);
+  }
+
+  support_next_to_fault_free (&ntf);
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/iconvdata/uhc.c b/iconvdata/uhc.c
index 8477aeb0cf..18e904309b 100644
--- a/iconvdata/uhc.c
+++ b/iconvdata/uhc.c
@@ -3133,11 +3133,14 @@ static const char uhc_hangul_from_ucs[11172][2] =
 	  }								      \
 	else								      \
 	  {								      \
+	    /* ksc5601_to_ucs4 conditionally advances inptr.  */	      \
+	    const unsigned char *oldinptr = inptr;			      \
 	    ch = ksc5601_to_ucs4 (&inptr, 2, 0x80);			      \
-	    if (__builtin_expect (ch == __UNKNOWN_10646_CHAR, 0)	      \
-		|| __builtin_expect (ch == 0x327e, 0))			      \
+	    if (__glibc_unlikely (ch == __UNKNOWN_10646_CHAR)		      \
+		|| __glibc_unlikely (ch == 0x327e))			      \
 	      {								      \
 		/* Illegal.  */						      \
+		inptr = oldinptr;					      \
 		STANDARD_FROM_LOOP_ERR_HANDLER (2);			      \
 	      }								      \
 	  }								      \
-- 
2.53.0



More information about the Libc-stable mailing list