ca483c
Author: Charles Fol <folcharles@gmail.com>
ca483c
Date:   Thu Mar 28 12:25:38 2024 -0300
ca483c
ca483c
    iconv: ISO-2022-CN-EXT: fix out-of-bound writes when writing escape sequence (CVE-2024-2961)
ca483c
ca483c
    ISO-2022-CN-EXT uses escape sequences to indicate character set changes
ca483c
    (as specified by RFC 1922).  While the SOdesignation has the expected
ca483c
    bounds checks, neither SS2designation nor SS3designation have its;
ca483c
    allowing a write overflow of 1, 2, or 3 bytes with fixed values:
ca483c
    '$+I', '$+J', '$+K', '$+L', '$+M', or '$*H'.
ca483c
ca483c
    Checked on aarch64-linux-gnu.
ca483c
ca483c
    Co-authored-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
ca483c
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
ca483c
    Tested-by: Carlos O'Donell <carlos@redhat.com>
ca483c
ca483c
Conflicts:
ca483c
	iconvdata/Makefile
ca483c
	  (usual tests conflict)
ca483c
ca483c
diff --git a/iconvdata/Makefile b/iconvdata/Makefile
ca483c
index 6f440e3f6122f56a..f8886d0f76cdac30 100644
ca483c
--- a/iconvdata/Makefile
ca483c
+++ b/iconvdata/Makefile
ca483c
@@ -72,7 +72,8 @@ endif
ca483c
 ifeq (yes,$(build-shared))
ca483c
 tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
ca483c
 	tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \
ca483c
-	bug-iconv10 bug-iconv11 bug-iconv12 bug-iconv13
ca483c
+	bug-iconv10 bug-iconv11 bug-iconv12 bug-iconv13 \
ca483c
+	tst-iconv-iso-2022-cn-ext
ca483c
 ifeq ($(have-thread-library),yes)
ca483c
 tests += bug-iconv3
ca483c
 endif
ca483c
@@ -306,6 +307,8 @@ $(objpfx)tst-iconv4.out: $(objpfx)gconv-modules \
ca483c
 			 $(addprefix $(objpfx),$(modules.so))
ca483c
 $(objpfx)tst-iconv7.out: $(objpfx)gconv-modules \
ca483c
 			 $(addprefix $(objpfx),$(modules.so))
ca483c
+$(objpfx)tst-iconv-iso-2022-cn-ext.out: $(addprefix $(objpfx), $(gconv-modules)) \
ca483c
+					$(addprefix $(objpfx),$(modules.so))
ca483c
 
ca483c
 $(objpfx)iconv-test.out: run-iconv-test.sh $(objpfx)gconv-modules \
ca483c
 			 $(addprefix $(objpfx),$(modules.so)) \
ca483c
diff --git a/iconvdata/iso-2022-cn-ext.c b/iconvdata/iso-2022-cn-ext.c
ca483c
index c86d8616d417f21b..661a55f1785c2bea 100644
ca483c
--- a/iconvdata/iso-2022-cn-ext.c
ca483c
+++ b/iconvdata/iso-2022-cn-ext.c
ca483c
@@ -564,6 +564,12 @@ enum
ca483c
 	      {								      \
ca483c
 		const char *escseq;					      \
ca483c
 									      \
ca483c
+		if (outptr + 4 > outend)				      \
ca483c
+		  {							      \
ca483c
+		    result = __GCONV_FULL_OUTPUT;			      \
ca483c
+		    break;						      \
ca483c
+		  }							      \
ca483c
+									      \
ca483c
 		assert (used == CNS11643_2_set); /* XXX */		      \
ca483c
 		escseq = "*H";						      \
ca483c
 		*outptr++ = ESC;					      \
ca483c
@@ -577,6 +583,12 @@ enum
ca483c
 	      {								      \
ca483c
 		const char *escseq;					      \
ca483c
 									      \
ca483c
+		if (outptr + 4 > outend)				      \
ca483c
+		  {							      \
ca483c
+		    result = __GCONV_FULL_OUTPUT;			      \
ca483c
+		    break;						      \
ca483c
+		  }							      \
ca483c
+									      \
ca483c
 		assert ((used >> 5) >= 3 && (used >> 5) <= 7);		      \
ca483c
 		escseq = "+I+J+K+L+M" + ((used >> 5) - 3) * 2;		      \
ca483c
 		*outptr++ = ESC;					      \
ca483c
diff --git a/iconvdata/tst-iconv-iso-2022-cn-ext.c b/iconvdata/tst-iconv-iso-2022-cn-ext.c
ca483c
new file mode 100644
ca483c
index 0000000000000000..96a8765fd5369681
ca483c
--- /dev/null
ca483c
+++ b/iconvdata/tst-iconv-iso-2022-cn-ext.c
ca483c
@@ -0,0 +1,128 @@
ca483c
+/* Verify ISO-2022-CN-EXT does not write out of the bounds.
ca483c
+   Copyright (C) 2024 Free Software Foundation, Inc.
ca483c
+   This file is part of the GNU C Library.
ca483c
+
ca483c
+   The GNU C Library is free software; you can redistribute it and/or
ca483c
+   modify it under the terms of the GNU Lesser General Public
ca483c
+   License as published by the Free Software Foundation; either
ca483c
+   version 2.1 of the License, or (at your option) any later version.
ca483c
+
ca483c
+   The GNU C Library is distributed in the hope that it will be useful,
ca483c
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ca483c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ca483c
+   Lesser General Public License for more details.
ca483c
+
ca483c
+   You should have received a copy of the GNU Lesser General Public
ca483c
+   License along with the GNU C Library; if not, see
ca483c
+   <https://www.gnu.org/licenses/>.  */
ca483c
+
ca483c
+#include <stdio.h>
ca483c
+#include <string.h>
ca483c
+
ca483c
+#include <errno.h>
ca483c
+#include <iconv.h>
ca483c
+#include <sys/mman.h>
ca483c
+
ca483c
+#include <support/xunistd.h>
ca483c
+#include <support/check.h>
ca483c
+#include <support/support.h>
ca483c
+
ca483c
+/* The test sets up a two memory page buffer with the second page marked
ca483c
+   PROT_NONE to trigger a fault if the conversion writes beyond the exact
ca483c
+   expected amount.  Then we carry out various conversions and precisely
ca483c
+   place the start of the output buffer in order to trigger a SIGSEGV if the
ca483c
+   process writes anywhere between 1 and page sized bytes more (only one
ca483c
+   PROT_NONE page is setup as a canary) than expected.  These tests exercise
ca483c
+   all three of the cases in ISO-2022-CN-EXT where the converter must switch
ca483c
+   character sets and may run out of buffer space while doing the
ca483c
+   operation.  */
ca483c
+
ca483c
+static int
ca483c
+do_test (void)
ca483c
+{
ca483c
+  iconv_t cd = iconv_open ("ISO-2022-CN-EXT", "UTF-8");
ca483c
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
ca483c
+
ca483c
+  char *ntf;
ca483c
+  size_t ntfsize;
ca483c
+  char *outbufbase;
ca483c
+  {
ca483c
+    int pgz = getpagesize ();
ca483c
+    TEST_VERIFY_EXIT (pgz > 0);
ca483c
+    ntfsize = 2 * pgz;
ca483c
+
ca483c
+    ntf = xmmap (NULL, ntfsize, PROT_READ | PROT_WRITE, MAP_PRIVATE
ca483c
+		 | MAP_ANONYMOUS, -1);
ca483c
+    xmprotect (ntf + pgz, pgz, PROT_NONE);
ca483c
+
ca483c
+    outbufbase = ntf + pgz;
ca483c
+  }
ca483c
+
ca483c
+  /* Check if SOdesignation escape sequence does not trigger an OOB write.  */
ca483c
+  {
ca483c
+    char inbuf[] = "\xe4\xba\xa4\xe6\x8d\xa2";
ca483c
+
ca483c
+    for (int i = 0; i < 9; i++)
ca483c
+      {
ca483c
+	char *inp = inbuf;
ca483c
+	size_t inleft = sizeof (inbuf) - 1;
ca483c
+
ca483c
+	char *outp = outbufbase - i;
ca483c
+	size_t outleft = i;
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft)
ca483c
+			  == (size_t) -1);
ca483c
+	TEST_COMPARE (errno, E2BIG);
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0);
ca483c
+      }
ca483c
+  }
ca483c
+
ca483c
+  /* Same as before for SS2designation.  */
ca483c
+  {
ca483c
+    char inbuf[] = "㴽 \xe3\xb4\xbd";
ca483c
+
ca483c
+    for (int i = 0; i < 14; i++)
ca483c
+      {
ca483c
+	char *inp = inbuf;
ca483c
+	size_t inleft = sizeof (inbuf) - 1;
ca483c
+
ca483c
+	char *outp = outbufbase - i;
ca483c
+	size_t outleft = i;
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft)
ca483c
+			  == (size_t) -1);
ca483c
+	TEST_COMPARE (errno, E2BIG);
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0);
ca483c
+      }
ca483c
+  }
ca483c
+
ca483c
+  /* Same as before for SS3designation.  */
ca483c
+  {
ca483c
+    char inbuf[] = "劄 \xe5\x8a\x84";
ca483c
+
ca483c
+    for (int i = 0; i < 14; i++)
ca483c
+      {
ca483c
+	char *inp = inbuf;
ca483c
+	size_t inleft = sizeof (inbuf) - 1;
ca483c
+
ca483c
+	char *outp = outbufbase - i;
ca483c
+	size_t outleft = i;
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, &inp, &inleft, &outp, &outleft)
ca483c
+			  == (size_t) -1);
ca483c
+	TEST_COMPARE (errno, E2BIG);
ca483c
+
ca483c
+	TEST_VERIFY_EXIT (iconv (cd, NULL, NULL, NULL, NULL) == 0);
ca483c
+      }
ca483c
+  }
ca483c
+
ca483c
+  TEST_VERIFY_EXIT (iconv_close (cd) != -1);
ca483c
+
ca483c
+  xmunmap (ntf, ntfsize);
ca483c
+
ca483c
+  return 0;
ca483c
+}
ca483c
+
ca483c
+#include <support/test-driver.c>