public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] avoid setting __SLBF and __SNBF on a file descriptor at the same time
@ 2016-12-15 16:40 Giuseppe Musumeci
  2016-12-15 17:15 ` Jeff Johnston
  0 siblings, 1 reply; 2+ messages in thread
From: Giuseppe Musumeci @ 2016-12-15 16:40 UTC (permalink / raw)
  To: newlib

Hi all,
I think I found an issue in __smakebuf_r which could lead to memory trashing.

__sinit initialises some common file descriptors as line buffered and
relies on the first users of such FDs to call __smakebuf_r. If
__smakebuf_r realises there's no space for a buffer (malloc returns
NULL), it makes them unbuffered. However, while setting the __SNBF
bit, it doesn't clear the __SLBF bit in the flags. Depending on the
order in which functions check buffering flags in the FD, sometime
they assume it's line buffered (e.g. __sfvwrite_r), trashing
application memory that's not really been allocated to them.

The following patch should solve the problem:


diff --git a/newlib/libc/stdio/makebuf.c b/newlib/libc/stdio/makebuf.c
index c8a50c5..e4cfd36 100644
--- a/newlib/libc/stdio/makebuf.c
+++ b/newlib/libc/stdio/makebuf.c
@@ -100,7 +100,7 @@ _DEFUN(__smakebuf_r, (ptr, fp),
     {
       if (!(fp->_flags & __SSTR))
        {
-         fp->_flags |= __SNBF;
+         fp->_flags = (fp->_flags & ~__SLBF) | __SNBF;
          fp->_bf._base = fp->_p = fp->_nbuf;
          fp->_bf._size = 1;
        }
@@ -115,6 +115,6 @@ _DEFUN(__smakebuf_r, (ptr, fp),
        * even when not outputting to a terminal to ease debugging. */
       if (fp->_file == 1 || fp->_file == 2 ||
           (couldbetty && _isatty_r (ptr, fp->_file)))
-       fp->_flags |= __SLBF;
+       fp->_flags = (fp->_flags & ~__SNBF) | __SLBF;
     }
 }


Thanks and regards,
Giuseppe Musumeci

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] avoid setting __SLBF and __SNBF on a file descriptor at the same time
  2016-12-15 16:40 [PATCH] avoid setting __SLBF and __SNBF on a file descriptor at the same time Giuseppe Musumeci
@ 2016-12-15 17:15 ` Jeff Johnston
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Johnston @ 2016-12-15 17:15 UTC (permalink / raw)
  To: Giuseppe Musumeci; +Cc: newlib

Makes sense.  Patch applied.

Thanks,

-- Jeff J.

----- Original Message -----
> Hi all,
> I think I found an issue in __smakebuf_r which could lead to memory trashing.
> 
> __sinit initialises some common file descriptors as line buffered and
> relies on the first users of such FDs to call __smakebuf_r. If
> __smakebuf_r realises there's no space for a buffer (malloc returns
> NULL), it makes them unbuffered. However, while setting the __SNBF
> bit, it doesn't clear the __SLBF bit in the flags. Depending on the
> order in which functions check buffering flags in the FD, sometime
> they assume it's line buffered (e.g. __sfvwrite_r), trashing
> application memory that's not really been allocated to them.
> 
> The following patch should solve the problem:
> 
> 
> diff --git a/newlib/libc/stdio/makebuf.c b/newlib/libc/stdio/makebuf.c
> index c8a50c5..e4cfd36 100644
> --- a/newlib/libc/stdio/makebuf.c
> +++ b/newlib/libc/stdio/makebuf.c
> @@ -100,7 +100,7 @@ _DEFUN(__smakebuf_r, (ptr, fp),
>      {
>        if (!(fp->_flags & __SSTR))
>         {
> -         fp->_flags |= __SNBF;
> +         fp->_flags = (fp->_flags & ~__SLBF) | __SNBF;
>           fp->_bf._base = fp->_p = fp->_nbuf;
>           fp->_bf._size = 1;
>         }
> @@ -115,6 +115,6 @@ _DEFUN(__smakebuf_r, (ptr, fp),
>         * even when not outputting to a terminal to ease debugging. */
>        if (fp->_file == 1 || fp->_file == 2 ||
>            (couldbetty && _isatty_r (ptr, fp->_file)))
> -       fp->_flags |= __SLBF;
> +       fp->_flags = (fp->_flags & ~__SNBF) | __SLBF;
>      }
>  }
> 
> 
> Thanks and regards,
> Giuseppe Musumeci
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-12-15 17:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-15 16:40 [PATCH] avoid setting __SLBF and __SNBF on a file descriptor at the same time Giuseppe Musumeci
2016-12-15 17:15 ` Jeff Johnston

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).