446cf2
commit ee7a3144c9922808181009b7b3e50e852fb4999b
446cf2
Author: Andreas Schwab <schwab@suse.de>
446cf2
Date:   Mon Dec 21 08:56:43 2020 +0530
446cf2
446cf2
    Fix buffer overrun in EUC-KR conversion module (bz #24973)
446cf2
    
446cf2
    The byte 0xfe as input to the EUC-KR conversion denotes a user-defined
446cf2
    area and is not allowed.  The from_euc_kr function used to skip two bytes
446cf2
    when told to skip over the unknown designation, potentially running over
446cf2
    the buffer end.
446cf2
446cf2
# Conflicts:
446cf2
#	iconvdata/Makefile
446cf2
446cf2
diff --git a/iconvdata/Makefile b/iconvdata/Makefile
446cf2
index 06e161d9b8f67118..a47a4c07cd2e3d1b 100644
446cf2
--- a/iconvdata/Makefile
446cf2
+++ b/iconvdata/Makefile
446cf2
@@ -73,7 +73,7 @@ modules.so := $(addsuffix .so, $(modules))
446cf2
 ifeq (yes,$(build-shared))
446cf2
 tests = bug-iconv1 bug-iconv2 tst-loading tst-e2big tst-iconv4 bug-iconv4 \
446cf2
 	tst-iconv6 bug-iconv5 bug-iconv6 tst-iconv7 bug-iconv8 bug-iconv9 \
446cf2
-	bug-iconv10 bug-iconv11 bug-iconv12
446cf2
+	bug-iconv10 bug-iconv11 bug-iconv12 bug-iconv13
446cf2
 ifeq ($(have-thread-library),yes)
446cf2
 tests += bug-iconv3
446cf2
 endif
446cf2
diff --git a/iconvdata/bug-iconv13.c b/iconvdata/bug-iconv13.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..87aaff398e0f6167
446cf2
--- /dev/null
446cf2
+++ b/iconvdata/bug-iconv13.c
446cf2
@@ -0,0 +1,53 @@
446cf2
+/* bug 24973: Test EUC-KR module
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <errno.h>
446cf2
+#include <iconv.h>
446cf2
+#include <stdio.h>
446cf2
+#include <support/check.h>
446cf2
+
446cf2
+static int
446cf2
+do_test (void)
446cf2
+{
446cf2
+  iconv_t cd = iconv_open ("UTF-8//IGNORE", "EUC-KR");
446cf2
+  TEST_VERIFY_EXIT (cd != (iconv_t) -1);
446cf2
+
446cf2
+  /* 0xfe (->0x7e : row 94) and 0xc9 (->0x49 : row 41) are user-defined
446cf2
+     areas, which are not allowed and should be skipped over due to
446cf2
+     //IGNORE.  The trailing 0xfe also is an incomplete sequence, which
446cf2
+     should be checked first.  */
446cf2
+  char input[4] = { '\xc9', '\xa1', '\0', '\xfe' };
446cf2
+  char *inptr = input;
446cf2
+  size_t insize = sizeof (input);
446cf2
+  char output[4];
446cf2
+  char *outptr = output;
446cf2
+  size_t outsize = sizeof (output);
446cf2
+
446cf2
+  /* This used to crash due to buffer overrun.  */
446cf2
+  TEST_VERIFY (iconv (cd, &inptr, &insize, &outptr, &outsize) == (size_t) -1);
446cf2
+  TEST_VERIFY (errno == EINVAL);
446cf2
+  /* The conversion should produce one character, the converted null
446cf2
+     character.  */
446cf2
+  TEST_VERIFY (sizeof (output) - outsize == 1);
446cf2
+
446cf2
+  TEST_VERIFY_EXIT (iconv_close (cd) != -1);
446cf2
+
446cf2
+  return 0;
446cf2
+}
446cf2
+
446cf2
+#include <support/test-driver.c>
446cf2
diff --git a/iconvdata/euc-kr.c b/iconvdata/euc-kr.c
446cf2
index 73e02817a07e873d..dc7eaa6596f5d4d4 100644
446cf2
--- a/iconvdata/euc-kr.c
446cf2
+++ b/iconvdata/euc-kr.c
446cf2
@@ -80,11 +80,7 @@ euckr_from_ucs4 (uint32_t ch, unsigned char *cp)
446cf2
 									      \
446cf2
     if (ch <= 0x9f)							      \
446cf2
       ++inptr;								      \
446cf2
-    /* 0xfe(->0x7e : row 94) and 0xc9(->0x59 : row 41) are		      \
446cf2
-       user-defined areas.  */						      \
446cf2
-    else if (__builtin_expect (ch == 0xa0, 0)				      \
446cf2
-	     || __builtin_expect (ch > 0xfe, 0)				      \
446cf2
-	     || __builtin_expect (ch == 0xc9, 0))			      \
446cf2
+    else if (__glibc_unlikely (ch == 0xa0))				      \
446cf2
       {									      \
446cf2
 	/* This is illegal.  */						      \
446cf2
 	STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \
446cf2
diff --git a/iconvdata/ksc5601.h b/iconvdata/ksc5601.h
446cf2
index 5588d3a14b667b42..fa2d30677c41f46a 100644
446cf2
--- a/iconvdata/ksc5601.h
446cf2
+++ b/iconvdata/ksc5601.h
446cf2
@@ -50,15 +50,15 @@ ksc5601_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
446cf2
   unsigned char ch2;
446cf2
   int idx;
446cf2
 
446cf2
+  if (avail < 2)
446cf2
+    return 0;
446cf2
+
446cf2
   /* row 94(0x7e) and row 41(0x49) are user-defined area in KS C 5601 */
446cf2
 
446cf2
   if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) >= 0x7e
446cf2
       || (ch - offset) == 0x49)
446cf2
     return __UNKNOWN_10646_CHAR;
446cf2
 
446cf2
-  if (avail < 2)
446cf2
-    return 0;
446cf2
-
446cf2
   ch2 = (*s)[1];
446cf2
   if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
446cf2
     return __UNKNOWN_10646_CHAR;