1d4c55
commit c6fad4fa149485a307207f707e5851216f190fc8
1d4c55
Author: Florian Weimer <fweimer@redhat.com>
1d4c55
Date:   Thu Mar 19 18:32:28 2020 -0300
1d4c55
1d4c55
    stdio: Remove memory leak from multibyte convertion [BZ#25691]
1d4c55
    
1d4c55
    This is an updated version of a previous patch [1] with the
1d4c55
    following changes:
1d4c55
    
1d4c55
      - Use compiler overflow builtins on done_add_func function.
1d4c55
      - Define the scratch +utstring_converted_wide_string using
1d4c55
        CHAR_T.
1d4c55
      - Added a testcase and mention the bug report.
1d4c55
    
1d4c55
    Both default and wide printf functions might leak memory when
1d4c55
    manipulate multibyte characters conversion depending of the size
1d4c55
    of the input (whether __libc_use_alloca trigger or not the fallback
1d4c55
    heap allocation).
1d4c55
    
1d4c55
    This patch fixes it by removing the extra memory allocation on
1d4c55
    string formatting with conversion parts.
1d4c55
    
1d4c55
    The testcase uses input argument size that trigger memory leaks
1d4c55
    on unpatched code (using a scratch buffer the threashold to use
1d4c55
    heap allocation is lower).
1d4c55
    
1d4c55
    Checked on x86_64-linux-gnu and i686-linux-gnu.
1d4c55
    
1d4c55
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
1d4c55
    
1d4c55
    [1] https://sourceware.org/pipermail/libc-alpha/2017-June/082098.html
1d4c55
    
1d4c55
    (cherry picked from commit 3cc4a8367c23582b7db14cf4e150e4068b7fd461)
1d4c55
1d4c55
diff --git a/stdio-common/vfprintf.c b/stdio-common/vfprintf.c
1d4c55
index ae412e4b8444aea2..dab56b6ba2c7bdbe 100644
1d4c55
--- a/stdio-common/vfprintf.c
1d4c55
+++ b/stdio-common/vfprintf.c
1d4c55
@@ -31,6 +31,7 @@
1d4c55
 #include <locale/localeinfo.h>
1d4c55
 #include <stdio.h>
1d4c55
 #include <scratch_buffer.h>
1d4c55
+#include <intprops.h>
1d4c55
 
1d4c55
 /* This code is shared between the standard stdio implementation found
1d4c55
    in GNU C library and the libio implementation originally found in
1d4c55
@@ -64,23 +65,40 @@
1d4c55
     } while (0)
1d4c55
 #define UNBUFFERED_P(S) ((S)->_flags & _IO_UNBUFFERED)
1d4c55
 
1d4c55
-#define done_add(val) \
1d4c55
-  do {									      \
1d4c55
-    unsigned int _val = val;						      \
1d4c55
-    assert ((unsigned int) done < (unsigned int) INT_MAX);		      \
1d4c55
-    if (__glibc_unlikely (INT_MAX - done < _val))			      \
1d4c55
-      {									      \
1d4c55
-	done = -1;							      \
1d4c55
-	 __set_errno (EOVERFLOW);					      \
1d4c55
-	goto all_done;							      \
1d4c55
-      }									      \
1d4c55
-    done += _val;							      \
1d4c55
-  } while (0)
1d4c55
+/* Add LENGTH to DONE.  Return the new value of DONE, or -1 on
1d4c55
+   overflow (and set errno accordingly).  */
1d4c55
+static inline int
1d4c55
+done_add_func (size_t length, int done)
1d4c55
+{
1d4c55
+  if (done < 0)
1d4c55
+    return done;
1d4c55
+  int ret;
1d4c55
+  if (INT_ADD_WRAPV (done, length, &ret))
1d4c55
+    {
1d4c55
+      __set_errno (EOVERFLOW);
1d4c55
+      return -1;
1d4c55
+    }
1d4c55
+  return ret;
1d4c55
+}
1d4c55
+
1d4c55
+#define done_add(val)							\
1d4c55
+  do									\
1d4c55
+    {									\
1d4c55
+      /* Ensure that VAL has a type similar to int.  */			\
1d4c55
+      _Static_assert (sizeof (val) == sizeof (int), "value int size");	\
1d4c55
+      _Static_assert ((__typeof__ (val)) -1 < 0, "value signed");	\
1d4c55
+      done = done_add_func ((val), done);				\
1d4c55
+      if (done < 0)							\
1d4c55
+	goto all_done;							\
1d4c55
+    }									\
1d4c55
+  while (0)
1d4c55
 
1d4c55
 #ifndef COMPILE_WPRINTF
1d4c55
 # define vfprintf	_IO_vfprintf_internal
1d4c55
 # define CHAR_T		char
1d4c55
+# define CHAR_T		char
1d4c55
 # define UCHAR_T	unsigned char
1d4c55
+# define OTHER_CHAR_T   wchar_t
1d4c55
 # define INT_T		int
1d4c55
 typedef const char *THOUSANDS_SEP_T;
1d4c55
 # define L_(Str)	Str
1d4c55
@@ -88,22 +106,10 @@ typedef const char *THOUSANDS_SEP_T;
1d4c55
 # define STR_LEN(Str)	strlen (Str)
1d4c55
 
1d4c55
 # define PUT(F, S, N)	_IO_sputn ((F), (S), (N))
1d4c55
-# define PAD(Padchar) \
1d4c55
-  do {									      \
1d4c55
-    if (width > 0)							      \
1d4c55
-      {									      \
1d4c55
-	ssize_t written = _IO_padn (s, (Padchar), width);		      \
1d4c55
-	if (__glibc_unlikely (written != width))			      \
1d4c55
-	  {								      \
1d4c55
-	    done = -1;							      \
1d4c55
-	    goto all_done;						      \
1d4c55
-	  }								      \
1d4c55
-	done_add (written);						      \
1d4c55
-      }									      \
1d4c55
-  } while (0)
1d4c55
 # define PUTC(C, F)	_IO_putc_unlocked (C, F)
1d4c55
 # define ORIENT		if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
1d4c55
 			  return -1
1d4c55
+# define CONVERT_FROM_OTHER_STRING __wcsrtombs
1d4c55
 #else
1d4c55
 # define vfprintf	_IO_vfwprintf
1d4c55
 # define CHAR_T		wchar_t
1d4c55
@@ -118,21 +124,11 @@ typedef wchar_t THOUSANDS_SEP_T;
1d4c55
 # include <_itowa.h>
1d4c55
 
1d4c55
 # define PUT(F, S, N)	_IO_sputn ((F), (S), (N))
1d4c55
-# define PAD(Padchar) \
1d4c55
-  do {									      \
1d4c55
-    if (width > 0)							      \
1d4c55
-      {									      \
1d4c55
-	ssize_t written = _IO_wpadn (s, (Padchar), width);		      \
1d4c55
-	if (__glibc_unlikely (written != width))			      \
1d4c55
-	  {								      \
1d4c55
-	    done = -1;							      \
1d4c55
-	    goto all_done;						      \
1d4c55
-	  }								      \
1d4c55
-	done_add (written);						      \
1d4c55
-      }									      \
1d4c55
-  } while (0)
1d4c55
 # define PUTC(C, F)	_IO_putwc_unlocked (C, F)
1d4c55
 # define ORIENT		if (_IO_fwide (s, 1) != 1) return -1
1d4c55
+# define CONVERT_FROM_OTHER_STRING __mbsrtowcs
1d4c55
+# define CHAR_T		wchar_t
1d4c55
+# define OTHER_CHAR_T   char
1d4c55
 
1d4c55
 # undef _itoa
1d4c55
 # define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
1d4c55
@@ -141,6 +137,33 @@ typedef wchar_t THOUSANDS_SEP_T;
1d4c55
 # define EOF WEOF
1d4c55
 #endif
1d4c55
 
1d4c55
+static inline int
1d4c55
+pad_func (FILE *s, CHAR_T padchar, int width, int done)
1d4c55
+{
1d4c55
+  if (width > 0)
1d4c55
+    {
1d4c55
+      ssize_t written;
1d4c55
+#ifndef COMPILE_WPRINTF
1d4c55
+      written = _IO_padn (s, padchar, width);
1d4c55
+#else
1d4c55
+      written = _IO_wpadn (s, padchar, width);
1d4c55
+#endif
1d4c55
+      if (__glibc_unlikely (written != width))
1d4c55
+	return -1;
1d4c55
+      return done_add_func (width, done);
1d4c55
+    }
1d4c55
+  return done;
1d4c55
+}
1d4c55
+
1d4c55
+#define PAD(Padchar)							\
1d4c55
+  do									\
1d4c55
+    {									\
1d4c55
+      done = pad_func (s, (Padchar), width, done);			\
1d4c55
+      if (done < 0)							\
1d4c55
+	goto all_done;							\
1d4c55
+    }									\
1d4c55
+  while (0)
1d4c55
+
1d4c55
 #include "_i18n_number.h"
1d4c55
 
1d4c55
 /* Include the shared code for parsing the format string.  */
1d4c55
@@ -160,24 +183,115 @@ typedef wchar_t THOUSANDS_SEP_T;
1d4c55
     }									      \
1d4c55
   while (0)
1d4c55
 
1d4c55
-#define outstring(String, Len)						      \
1d4c55
-  do									      \
1d4c55
-    {									      \
1d4c55
-      assert ((size_t) done <= (size_t) INT_MAX);			      \
1d4c55
-      if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len))		      \
1d4c55
-	{								      \
1d4c55
-	  done = -1;							      \
1d4c55
-	  goto all_done;						      \
1d4c55
-	}								      \
1d4c55
-      if (__glibc_unlikely (INT_MAX - done < (Len)))			      \
1d4c55
-      {									      \
1d4c55
-	done = -1;							      \
1d4c55
-	 __set_errno (EOVERFLOW);					      \
1d4c55
-	goto all_done;							      \
1d4c55
-      }									      \
1d4c55
-      done += (Len);							      \
1d4c55
-    }									      \
1d4c55
-  while (0)
1d4c55
+static inline int
1d4c55
+outstring_func (FILE *s, const UCHAR_T *string, size_t length, int done)
1d4c55
+{
1d4c55
+  assert ((size_t) done <= (size_t) INT_MAX);
1d4c55
+  if ((size_t) PUT (s, string, length) != (size_t) (length))
1d4c55
+    return -1;
1d4c55
+  return done_add_func (length, done);
1d4c55
+}
1d4c55
+
1d4c55
+#define outstring(String, Len)						\
1d4c55
+  do									\
1d4c55
+    {									\
1d4c55
+      const void *string_ = (String);					\
1d4c55
+      done = outstring_func (s, string_, (Len), done);			\
1d4c55
+      if (done < 0)							\
1d4c55
+	goto all_done;							\
1d4c55
+    }									\
1d4c55
+   while (0)
1d4c55
+
1d4c55
+/* Write the string SRC to S.  If PREC is non-negative, write at most
1d4c55
+   PREC bytes.  If LEFT is true, perform left justification.  */
1d4c55
+static int
1d4c55
+outstring_converted_wide_string (FILE *s, const OTHER_CHAR_T *src, int prec,
1d4c55
+				 int width, bool left, int done)
1d4c55
+{
1d4c55
+  /* Use a small buffer to combine processing of multiple characters.
1d4c55
+     CONVERT_FROM_OTHER_STRING expects the buffer size in (wide)
1d4c55
+     characters, and buf_length counts that.  */
1d4c55
+  enum { buf_length = 256 / sizeof (CHAR_T) };
1d4c55
+  CHAR_T buf[buf_length];
1d4c55
+  _Static_assert (sizeof (buf) > MB_LEN_MAX,
1d4c55
+		  "buffer is large enough for a single multi-byte character");
1d4c55
+
1d4c55
+  /* Add the initial padding if needed.  */
1d4c55
+  if (width > 0 && !left)
1d4c55
+    {
1d4c55
+      /* Make a first pass to find the output width, so that we can
1d4c55
+	 add the required padding.  */
1d4c55
+      mbstate_t mbstate = { 0 };
1d4c55
+      const OTHER_CHAR_T *src_copy = src;
1d4c55
+      size_t total_written;
1d4c55
+      if (prec < 0)
1d4c55
+	total_written = CONVERT_FROM_OTHER_STRING
1d4c55
+	  (NULL, &src_copy, 0, &mbstate);
1d4c55
+      else
1d4c55
+	{
1d4c55
+	  /* The source might not be null-terminated.  Enforce the
1d4c55
+	     limit manually, based on the output length.  */
1d4c55
+	  total_written = 0;
1d4c55
+	  size_t limit = prec;
1d4c55
+	  while (limit > 0 && src_copy != NULL)
1d4c55
+	    {
1d4c55
+	      size_t write_limit = buf_length;
1d4c55
+	      if (write_limit > limit)
1d4c55
+		write_limit = limit;
1d4c55
+	      size_t written = CONVERT_FROM_OTHER_STRING
1d4c55
+		(buf, &src_copy, write_limit, &mbstate);
1d4c55
+	      if (written == (size_t) -1)
1d4c55
+		return -1;
1d4c55
+	      if (written == 0)
1d4c55
+		break;
1d4c55
+	      total_written += written;
1d4c55
+	      limit -= written;
1d4c55
+	    }
1d4c55
+	}
1d4c55
+
1d4c55
+      /* Output initial padding.  */
1d4c55
+      if (total_written < width)
1d4c55
+	{
1d4c55
+	  done = pad_func (s, L_(' '), width - total_written, done);
1d4c55
+	  if (done < 0)
1d4c55
+	    return done;
1d4c55
+	}
1d4c55
+    }
1d4c55
+
1d4c55
+  /* Convert the input string, piece by piece.  */
1d4c55
+  size_t total_written = 0;
1d4c55
+  {
1d4c55
+    mbstate_t mbstate = { 0 };
1d4c55
+    /* If prec is negative, remaining is not decremented, otherwise,
1d4c55
+      it serves as the write limit.  */
1d4c55
+    size_t remaining = -1;
1d4c55
+    if (prec >= 0)
1d4c55
+      remaining = prec;
1d4c55
+    while (remaining > 0 && src != NULL)
1d4c55
+      {
1d4c55
+	size_t write_limit = buf_length;
1d4c55
+	if (remaining < write_limit)
1d4c55
+	  write_limit = remaining;
1d4c55
+	size_t written = CONVERT_FROM_OTHER_STRING
1d4c55
+	  (buf, &src, write_limit, &mbstate);
1d4c55
+	if (written == (size_t) -1)
1d4c55
+	  return -1;
1d4c55
+	if (written == 0)
1d4c55
+	  break;
1d4c55
+	done = outstring_func (s, (const UCHAR_T *) buf, written, done);
1d4c55
+	if (done < 0)
1d4c55
+	  return done;
1d4c55
+	total_written += written;
1d4c55
+	if (prec >= 0)
1d4c55
+	  remaining -= written;
1d4c55
+      }
1d4c55
+  }
1d4c55
+
1d4c55
+  /* Add final padding.  */
1d4c55
+  if (width > 0 && left && total_written < width)
1d4c55
+    return pad_func (s, L_(' '), width - total_written, done);
1d4c55
+  return done;
1d4c55
+}
1d4c55
 
1d4c55
 /* For handling long_double and longlong we use the same flag.  If
1d4c55
    `long' and `long long' are effectively the same type define it to
1d4c55
@@ -975,7 +1089,6 @@ static const uint8_t jump_table[] =
1d4c55
     LABEL (form_string):						      \
1d4c55
       {									      \
1d4c55
 	size_t len;							      \
1d4c55
-	int string_malloced;						      \
1d4c55
 									      \
1d4c55
 	/* The string argument could in fact be `char *' or `wchar_t *'.      \
1d4c55
 	   But this should not make a difference here.  */		      \
1d4c55
@@ -987,7 +1100,6 @@ static const uint8_t jump_table[] =
1d4c55
 	/* Entry point for printing other strings.  */			      \
1d4c55
       LABEL (print_string):						      \
1d4c55
 									      \
1d4c55
-	string_malloced = 0;						      \
1d4c55
 	if (string == NULL)						      \
1d4c55
 	  {								      \
1d4c55
 	    /* Write "(null)" if there's space.  */			      \
1d4c55
@@ -1004,41 +1116,12 @@ static const uint8_t jump_table[] =
1d4c55
 	  }								      \
1d4c55
 	else if (!is_long && spec != L_('S'))				      \
1d4c55
 	  {								      \
1d4c55
-	    /* This is complicated.  We have to transform the multibyte	      \
1d4c55
-	       string into a wide character string.  */			      \
1d4c55
-	    const char *mbs = (const char *) string;			      \
1d4c55
-	    mbstate_t mbstate;						      \
1d4c55
-									      \
1d4c55
-	    len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
1d4c55
-									      \
1d4c55
-	    /* Allocate dynamically an array which definitely is long	      \
1d4c55
-	       enough for the wide character version.  Each byte in the	      \
1d4c55
-	       multi-byte string can produce at most one wide character.  */  \
1d4c55
-	    if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t)))	      \
1d4c55
-	      {								      \
1d4c55
-		__set_errno (EOVERFLOW);				      \
1d4c55
-		done = -1;						      \
1d4c55
-		goto all_done;						      \
1d4c55
-	      }								      \
1d4c55
-	    else if (__libc_use_alloca (len * sizeof (wchar_t)))	      \
1d4c55
-	      string = (CHAR_T *) alloca (len * sizeof (wchar_t));	      \
1d4c55
-	    else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t)))    \
1d4c55
-		     == NULL)						      \
1d4c55
-	      {								      \
1d4c55
-		done = -1;						      \
1d4c55
-		goto all_done;						      \
1d4c55
-	      }								      \
1d4c55
-	    else							      \
1d4c55
-	      string_malloced = 1;					      \
1d4c55
-									      \
1d4c55
-	    memset (&mbstate, '\0', sizeof (mbstate_t));		      \
1d4c55
-	    len = __mbsrtowcs (string, &mbs, len, &mbstate);		      \
1d4c55
-	    if (len == (size_t) -1)					      \
1d4c55
-	      {								      \
1d4c55
-		/* Illegal multibyte character.  */			      \
1d4c55
-		done = -1;						      \
1d4c55
-		goto all_done;						      \
1d4c55
-	      }								      \
1d4c55
+	    done = outstring_converted_wide_string			      \
1d4c55
+	      (s, (const char *) string, prec, width, left, done);	      \
1d4c55
+	    if (done < 0)						      \
1d4c55
+	      goto all_done;						      \
1d4c55
+	    /* The padding has already been written.  */		      \
1d4c55
+	    break;							      \
1d4c55
 	  }								      \
1d4c55
 	else								      \
1d4c55
 	  {								      \
1d4c55
@@ -1061,8 +1144,6 @@ static const uint8_t jump_table[] =
1d4c55
 	outstring (string, len);					      \
1d4c55
 	if (left)							      \
1d4c55
 	  PAD (L' ');							      \
1d4c55
-	if (__glibc_unlikely (string_malloced))				      \
1d4c55
-	  free (string);						      \
1d4c55
       }									      \
1d4c55
       break;
1d4c55
 #else
1d4c55
@@ -1111,7 +1192,6 @@ static const uint8_t jump_table[] =
1d4c55
     LABEL (form_string):						      \
1d4c55
       {									      \
1d4c55
 	size_t len;							      \
1d4c55
-	int string_malloced;						      \
1d4c55
 									      \
1d4c55
 	/* The string argument could in fact be `char *' or `wchar_t *'.      \
1d4c55
 	   But this should not make a difference here.  */		      \
1d4c55
@@ -1123,7 +1203,6 @@ static const uint8_t jump_table[] =
1d4c55
 	/* Entry point for printing other strings.  */			      \
1d4c55
       LABEL (print_string):						      \
1d4c55
 									      \
1d4c55
-	string_malloced = 0;						      \
1d4c55
 	if (string == NULL)						      \
1d4c55
 	  {								      \
1d4c55
 	    /* Write "(null)" if there's space.  */			      \
1d4c55
@@ -1149,51 +1228,12 @@ static const uint8_t jump_table[] =
1d4c55
 	  }								      \
1d4c55
 	else								      \
1d4c55
 	  {								      \
1d4c55
-	    const wchar_t *s2 = (const wchar_t *) string;		      \
1d4c55
-	    mbstate_t mbstate;						      \
1d4c55
-									      \
1d4c55
-	    memset (&mbstate, '\0', sizeof (mbstate_t));		      \
1d4c55
-									      \
1d4c55
-	    if (prec >= 0)						      \
1d4c55
-	      {								      \
1d4c55
-		/* The string `s2' might not be NUL terminated.  */	      \
1d4c55
-		if (__libc_use_alloca (prec))				      \
1d4c55
-		  string = (char *) alloca (prec);			      \
1d4c55
-		else if ((string = (char *) malloc (prec)) == NULL)	      \
1d4c55
-		  {							      \
1d4c55
-		    done = -1;						      \
1d4c55
-		    goto all_done;					      \
1d4c55
-		  }							      \
1d4c55
-		else							      \
1d4c55
-		  string_malloced = 1;					      \
1d4c55
-		len = __wcsrtombs (string, &s2, prec, &mbstate);	      \
1d4c55
-	      }								      \
1d4c55
-	    else							      \
1d4c55
-	      {								      \
1d4c55
-		len = __wcsrtombs (NULL, &s2, 0, &mbstate);		      \
1d4c55
-		if (len != (size_t) -1)					      \
1d4c55
-		  {							      \
1d4c55
-		    assert (__mbsinit (&mbstate));			      \
1d4c55
-		    s2 = (const wchar_t *) string;			      \
1d4c55
-		    if (__libc_use_alloca (len + 1))			      \
1d4c55
-		      string = (char *) alloca (len + 1);		      \
1d4c55
-		    else if ((string = (char *) malloc (len + 1)) == NULL)    \
1d4c55
-		      {							      \
1d4c55
-			done = -1;					      \
1d4c55
-			goto all_done;					      \
1d4c55
-		      }							      \
1d4c55
-		    else						      \
1d4c55
-		      string_malloced = 1;				      \
1d4c55
-		    (void) __wcsrtombs (string, &s2, len + 1, &mbstate);      \
1d4c55
-		  }							      \
1d4c55
-	      }								      \
1d4c55
-									      \
1d4c55
-	    if (len == (size_t) -1)					      \
1d4c55
-	      {								      \
1d4c55
-		/* Illegal wide-character string.  */			      \
1d4c55
-		done = -1;						      \
1d4c55
-		goto all_done;						      \
1d4c55
-	      }								      \
1d4c55
+	    done = outstring_converted_wide_string			      \
1d4c55
+	      (s, (const wchar_t *) string, prec, width, left, done);	      \
1d4c55
+	    if (done < 0)						      \
1d4c55
+	      goto all_done;						      \
1d4c55
+	    /* The padding has already been written.  */		      \
1d4c55
+	    break;							      \
1d4c55
 	  }								      \
1d4c55
 									      \
1d4c55
 	if ((width -= len) < 0)						      \
1d4c55
@@ -1207,8 +1247,6 @@ static const uint8_t jump_table[] =
1d4c55
 	outstring (string, len);					      \
1d4c55
 	if (left)							      \
1d4c55
 	  PAD (' ');							      \
1d4c55
-	if (__glibc_unlikely (string_malloced))			              \
1d4c55
-	  free (string);						      \
1d4c55
       }									      \
1d4c55
       break;
1d4c55
 #endif