bdc76f
commit 21cc130b78a4db9113fb6695e2b951e697662440
bdc76f
Author: Dmitry V. Levin <ldv@altlinux.org>
bdc76f
Date:   Wed Feb 13 01:20:51 2019 +0000
bdc76f
bdc76f
    libio: do not attempt to free wide buffers of legacy streams [BZ #24228]
bdc76f
    
bdc76f
    Commit a601b74d31ca086de38441d316a3dee24c866305 aka glibc-2.23~693
bdc76f
    ("In preparation for fixing BZ#16734, fix failure in misc/tst-error1-mem
bdc76f
    when _G_HAVE_MMAP is turned off.") introduced a regression:
bdc76f
    _IO_unbuffer_all now invokes _IO_wsetb to free wide buffers of all
bdc76f
    files, including legacy standard files which are small statically
bdc76f
    allocated objects that do not have wide buffers and the _mode member,
bdc76f
    causing memory corruption.
bdc76f
    
bdc76f
    Another memory corruption in _IO_unbuffer_all happens when -1
bdc76f
    is assigned to the _mode member of legacy standard files that
bdc76f
    do not have it.
bdc76f
    
bdc76f
    [BZ #24228]
bdc76f
    * libio/genops.c (_IO_unbuffer_all)
bdc76f
    [SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)]: Do not attempt to free wide
bdc76f
    buffers and access _IO_FILE_complete members of legacy libio streams.
bdc76f
    * libio/tst-bz24228.c: New file.
bdc76f
    * libio/tst-bz24228.map: Likewise.
bdc76f
    * libio/Makefile [build-shared] (tests): Add tst-bz24228.
bdc76f
    [build-shared] (generated): Add tst-bz24228.mtrace and
bdc76f
    tst-bz24228.check.
bdc76f
    [run-built-tests && build-shared] (tests-special): Add
bdc76f
    $(objpfx)tst-bz24228-mem.out.
bdc76f
    (LDFLAGS-tst-bz24228, tst-bz24228-ENV): New variables.
bdc76f
    ($(objpfx)tst-bz24228-mem.out): New rule.
bdc76f
bdc76f
# Conflicts:
bdc76f
#	libio/Makefile
bdc76f
bdc76f
diff --git a/libio/Makefile b/libio/Makefile
bdc76f
index cbfaf3832a45fc22..314e03d5ce72be2d 100644
bdc76f
--- a/libio/Makefile
bdc76f
+++ b/libio/Makefile
bdc76f
@@ -73,6 +73,9 @@ ifeq (yes,$(build-shared))
bdc76f
 # Add test-fopenloc only if shared library is enabled since it depends on
bdc76f
 # shared localedata objects.
bdc76f
 tests += tst-fopenloc
bdc76f
+# Add tst-bz24228 only if shared library is enabled since it can never meet its
bdc76f
+# objective with static linking because the relevant code just is not there.
bdc76f
+tests += tst-bz24228
bdc76f
 endif
bdc76f
 test-srcs = test-freopen
bdc76f
 
bdc76f
@@ -153,11 +156,14 @@ CFLAGS-oldtmpfile.c += -fexceptions
bdc76f
 
bdc76f
 CFLAGS-tst_putwc.c += -DOBJPFX=\"$(objpfx)\"
bdc76f
 
bdc76f
+LDFLAGS-tst-bz24228 = -Wl,--version-script=tst-bz24228.map
bdc76f
+
bdc76f
 tst_wprintf2-ARGS = "Some Text"
bdc76f
 
bdc76f
 test-fmemopen-ENV = MALLOC_TRACE=$(objpfx)test-fmemopen.mtrace
bdc76f
 tst-fopenloc-ENV = MALLOC_TRACE=$(objpfx)tst-fopenloc.mtrace
bdc76f
 tst-bz22415-ENV = MALLOC_TRACE=$(objpfx)tst-bz22415.mtrace
bdc76f
+tst-bz24228-ENV = MALLOC_TRACE=$(objpfx)tst-bz24228.mtrace
bdc76f
 
bdc76f
 generated += test-fmemopen.mtrace test-fmemopen.check
bdc76f
 generated += tst-fopenloc.mtrace tst-fopenloc.check
bdc76f
@@ -166,6 +172,7 @@ generated += tst-bz22415.mtrace tst-bz22415.check
bdc76f
 aux	:= fileops genops stdfiles stdio strops
bdc76f
 
bdc76f
 ifeq ($(build-shared),yes)
bdc76f
+generated += tst-bz24228.mtrace tst-bz24228.check
bdc76f
 aux	+= oldfileops oldstdfiles
bdc76f
 endif
bdc76f
 
bdc76f
@@ -180,7 +187,8 @@ tests-special += $(objpfx)test-freopen.out $(objpfx)test-fmemopen-mem.out \
bdc76f
 ifeq (yes,$(build-shared))
bdc76f
 # Run tst-fopenloc-cmp.out and tst-openloc-mem.out only if shared
bdc76f
 # library is enabled since they depend on tst-fopenloc.out.
bdc76f
-tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out
bdc76f
+tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \
bdc76f
+		 $(objpfx)tst-bz24228-mem.out
bdc76f
 endif
bdc76f
 endif
bdc76f
 
bdc76f
@@ -232,3 +240,7 @@ $(objpfx)tst-fopenloc-mem.out: $(objpfx)tst-fopenloc.out
bdc76f
 $(objpfx)tst-bz22415-mem.out: $(objpfx)tst-bz22415.out
bdc76f
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-bz22415.mtrace > $@; \
bdc76f
 	$(evaluate-test)
bdc76f
+
bdc76f
+$(objpfx)tst-bz24228-mem.out: $(objpfx)tst-bz24228.out
bdc76f
+	$(common-objpfx)malloc/mtrace $(objpfx)tst-bz24228.mtrace > $@; \
bdc76f
+	$(evaluate-test)
bdc76f
diff --git a/libio/genops.c b/libio/genops.c
bdc76f
index 2fec221b99729718..a8241dd26640bbcb 100644
bdc76f
--- a/libio/genops.c
bdc76f
+++ b/libio/genops.c
bdc76f
@@ -789,9 +789,16 @@ _IO_unbuffer_all (void)
bdc76f
 
bdc76f
   for (fp = (FILE *) _IO_list_all; fp; fp = fp->_chain)
bdc76f
     {
bdc76f
+      int legacy = 0;
bdc76f
+
bdc76f
+#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
bdc76f
+      if (__glibc_unlikely (_IO_vtable_offset (fp) != 0))
bdc76f
+	legacy = 1;
bdc76f
+#endif
bdc76f
+
bdc76f
       if (! (fp->_flags & _IO_UNBUFFERED)
bdc76f
 	  /* Iff stream is un-orientated, it wasn't used. */
bdc76f
-	  && fp->_mode != 0)
bdc76f
+	  && (legacy || fp->_mode != 0))
bdc76f
 	{
bdc76f
 #ifdef _IO_MTSAFE_IO
bdc76f
 	  int cnt;
bdc76f
@@ -805,7 +812,7 @@ _IO_unbuffer_all (void)
bdc76f
 	      __sched_yield ();
bdc76f
 #endif
bdc76f
 
bdc76f
-	  if (! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
bdc76f
+	  if (! legacy && ! dealloc_buffers && !(fp->_flags & _IO_USER_BUF))
bdc76f
 	    {
bdc76f
 	      fp->_flags |= _IO_USER_BUF;
bdc76f
 
bdc76f
@@ -816,7 +823,7 @@ _IO_unbuffer_all (void)
bdc76f
 
bdc76f
 	  _IO_SETBUF (fp, NULL, 0);
bdc76f
 
bdc76f
-	  if (fp->_mode > 0)
bdc76f
+	  if (! legacy && fp->_mode > 0)
bdc76f
 	    _IO_wsetb (fp, NULL, NULL, 0);
bdc76f
 
bdc76f
 #ifdef _IO_MTSAFE_IO
bdc76f
@@ -827,7 +834,8 @@ _IO_unbuffer_all (void)
bdc76f
 
bdc76f
       /* Make sure that never again the wide char functions can be
bdc76f
 	 used.  */
bdc76f
-      fp->_mode = -1;
bdc76f
+      if (! legacy)
bdc76f
+	fp->_mode = -1;
bdc76f
     }
bdc76f
 
bdc76f
 #ifdef _IO_MTSAFE_IO
bdc76f
diff --git a/libio/tst-bz24228.c b/libio/tst-bz24228.c
bdc76f
new file mode 100644
bdc76f
index 0000000000000000..6a74500d473ceeab
bdc76f
--- /dev/null
bdc76f
+++ b/libio/tst-bz24228.c
bdc76f
@@ -0,0 +1,29 @@
bdc76f
+/* BZ #24228 check for memory corruption in legacy libio
bdc76f
+   Copyright (C) 2019 Free Software Foundation, Inc.
bdc76f
+   This file is part of the GNU C Library.
bdc76f
+
bdc76f
+   The GNU C Library is free software; you can redistribute it and/or
bdc76f
+   modify it under the terms of the GNU Lesser General Public
bdc76f
+   License as published by the Free Software Foundation; either
bdc76f
+   version 2.1 of the License, or (at your option) any later version.
bdc76f
+
bdc76f
+   The GNU C Library is distributed in the hope that it will be useful,
bdc76f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bdc76f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bdc76f
+   Lesser General Public License for more details.
bdc76f
+
bdc76f
+   You should have received a copy of the GNU Lesser General Public
bdc76f
+   License along with the GNU C Library; if not, see
bdc76f
+   <https://www.gnu.org/licenses/>.  */
bdc76f
+
bdc76f
+#include <mcheck.h>
bdc76f
+#include <support/test-driver.h>
bdc76f
+
bdc76f
+static int
bdc76f
+do_test (void)
bdc76f
+{
bdc76f
+  mtrace ();
bdc76f
+  return 0;
bdc76f
+}
bdc76f
+
bdc76f
+#include <support/test-driver.c>
bdc76f
diff --git a/libio/tst-bz24228.map b/libio/tst-bz24228.map
bdc76f
new file mode 100644
bdc76f
index 0000000000000000..4383e0817d7f5583
bdc76f
--- /dev/null
bdc76f
+++ b/libio/tst-bz24228.map
bdc76f
@@ -0,0 +1,5 @@
bdc76f
+# Hide the symbol from libc.so.6 to switch to the libio/oldfileops.c
bdc76f
+# implementation when it is available for the architecture.
bdc76f
+{
bdc76f
+  local: _IO_stdin_used;
bdc76f
+};