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