<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "http://bugs.gentoo.org/bugzilla.dtd">

<bugzilla version="2.22.7"
          urlbase="http://bugs.gentoo.org/"
          maintainer="bugzilla@gentoo.org"
>

    <bug>
          <bug_id>73858</bug_id>
          
          <creation_ts>2004-12-08 18:25 0000</creation_ts>
          <short_desc>sys-apps/ed produces incorrect output (bug 66400 redux)</short_desc>
          <delta_ts>2005-01-09 12:39:57 0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Gentoo Linux</product>
          <component>Core system</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          <bug_file_loc>http://savannah.gnu.org/patch/index.php?func=detailitem&amp;item_id=3628</bug_file_loc>
          
          
          <priority>P2</priority>
          <bug_severity>critical</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>66400</blocked>
          
          <everconfirmed>1</everconfirmed>
          <reporter>robbat2@gentoo.org</reporter>
          <assigned_to>base-system@gentoo.org</assigned_to>
          <cc>security@gentoo.org</cc>
    
    <cc>ulm@gentoo.org</cc>

      

      
          <long_desc isprivate="0">
            <who>robbat2@gentoo.org</who>
            <bug_when>2004-12-08 18:25:29 0000</bug_when>
            <thetext>Since the security fix in bug #66400 was applied to ed, it no longer produces a correct output file if the stderr file description is closed. Instead it produces a corrupted output file.

If the patch from #66400 is NOT used, then the bug does not occur.

Reproducible: Always
Steps to Reproduce:
see attached test script files.
Actual Results:  
if stderr is closed when ed runs, the output file is corrupted when the &apos;write&apos; 
command is run in ed.

Expected Results:  
The output file should not be corrupted.

Portage 2.0.51-r3 (!/usr/portage/profiles/default-linux/x86/2004.3, gcc-3.3.5, 
glibc-2.3.4.20041102-r0, 2.6.7-mm4 i686)
=================================================================
System uname: 2.6.7-mm4 i686 AMD Athlon(tm) XP 2400+
Gentoo Base System version 1.6.6
distcc 2.18 i686-pc-linux-gnu (protocols 1 and 2) (default port 3632) [enabled]
ccache version 2.3 [enabled]
Autoconf: sys-devel/autoconf-2.59-r5
Automake: sys-devel/automake-1.8.5-r1
Binutils: sys-devel/binutils-2.15.92.0.2-r1
Headers:  sys-kernel/linux26-headers-2.6.8.1-r1
Libtools: sys-devel/libtool-1.5.2-r7
ACCEPT_KEYWORDS=&quot;x86 ~x86&quot;
AUTOCLEAN=&quot;yes&quot;
CFLAGS=&quot;-march=athlon-xp -mcpu=athlon-xp -O3 -pipe&quot;
CHOST=&quot;i686-pc-linux-gnu&quot;
COMPILER=&quot;&quot;
CONFIG_PROTECT=&quot;/etc /usr/X11R6/lib/X11/xkb /usr/kde/2/share/config /usr/kde/3/s
hare/config /usr/share/config /usr/share/texmf/dvipdfm/config/ /usr/share/texmf/
dvips/config/ /usr/share/texmf/tex/generic/config/ /usr/share/texmf/tex/platex/c
onfig/ /usr/share/texmf/xdvi/ /var/bind /var/qmail/alias /var/qmail/control /var
/vpopmail/domains /var/vpopmail/etc&quot;
CONFIG_PROTECT_MASK=&quot;/etc/gconf /etc/terminfo /etc/env.d&quot;
CXXFLAGS=&quot;-march=athlon-xp -mcpu=athlon-xp -O3 -pipe&quot;
DISTDIR=&quot;/usr/gentoo-distfiles&quot;
FEATURES=&quot;autoaddcvs autoconfig buildpkg ccache confcache cvs digest distcc 
distlocks sandbox sfperms sign userpriv&quot;
GENTOO_MIRRORS=&quot;http://gentoo.ccccom.com http://gentoo.seren.com/gentoo&quot;
MAKEOPTS=&quot;-j1&quot;
PKGDIR=&quot;/usr/gentoo-packages&quot;
PORTAGE_TMPDIR=&quot;/var/tmp&quot;
PORTDIR=&quot;/usr/gentoo-cvs/gentoo-x86&quot;
PORTDIR_OVERLAY=&quot;/usr/local/portage&quot;
SYNC=&quot;&quot;
USE=&quot;3dnow aalib acl acpi alsa amd apache2 apm berkdb bitmap-fonts cdr cgi 
clearpasswd crypt cscope cups curl divx4linux f77 fam foomaticdb fortran gd 
gdbm geoip gif imagemagick imap innodb ipalias ipv6 java jikes jpeg junit 
libwww mad maildir mcal md5sum mikmod mmx mpeg multitarget mysql ncurses 
offensive pam pcap pdflib perl pic plotutils png pnp ppds python qmail readline 
samba scanner slp snmp socks5 spell sqlite sse ssl tetex tiff truetype type1 
ungif usb userlocales v4l v4l2 x86 xml xml2 xvid zlib&quot;</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robbat2@gentoo.org</who>
            <bug_when>2004-12-08 18:27:53 0000</bug_when>
            <thetext>Created an attachment (id=45580)
edtest.cpp

Source code for testing binary.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robbat2@gentoo.org</who>
            <bug_when>2004-12-08 18:28:24 0000</bug_when>
            <thetext>Created an attachment (id=45581)
dotest.sh

Script file to run test case and prove the error.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>koon@gentoo.org</who>
            <bug_when>2004-12-22 04:26:40 0000</bug_when>
            <thetext>Here is the patch (from LFS) that was applied by base-system to fix the vulnerability :

============================================================
--- ed-0.2/buf.c	Sat Nov 19 04:37:59 1994
+++ ed-0.2-2/buf.c	Tue May 28 18:38:23 2002
@@ -200,13 +200,13 @@
 int
 open_sbuf ()
 {
-  char *mktemp ();
-  int u;
+  int u, sfd;
 
   isbinary = newline_added = 0;
   u = umask(077);
   strcpy (sfn, &quot;/tmp/ed.XXXXXX&quot;);
-  if (mktemp (sfn) == NULL || (sfp = fopen (sfn, &quot;w+&quot;)) == NULL)
+  sfd = mkstemp(sfn);
+    if ((sfd == -1) || (sfp = fopen (sfn, &quot;w+&quot;)) == NULL)
     {
       fprintf (stderr, &quot;%s: %s\n&quot;, sfn, strerror (errno));
       sprintf (errmsg, &quot;Cannot open temp file&quot;);
=============================================================

I fail to see where it introduces a regression, since apparently it just switched a mktemp for a mkstemp. Maybe someone more literate on those issues will find where it fails...</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robbat2@gentoo.org</who>
            <bug_when>2004-12-23 02:42:56 0000</bug_when>
            <thetext>The error in your patch is that mkstemp returns a file descriptor for an OPEN file already (eg it runs fopen on the file).

so:
+  sfd = mkstemp(sfn);
+    if ((sfd == -1) || (sfp = fopen (sfn, &quot;w+&quot;)) == NULL)

if mkstemp returns a good value, and then &quot;sfp = fopen (sfn, &quot;w+&quot;)&quot; is run, opening the file again.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vapier@gentoo.org</who>
            <bug_when>2004-12-23 13:49:11 0000</bug_when>
            <thetext>-    if ((sfd == -1) || (sfp = fopen (sfn, &quot;w+&quot;)) == NULL)
+    if (sfd == -1)

so this patch will fix it ?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ulm@gentoo.org</who>
            <bug_when>2004-12-27 03:18:25 0000</bug_when>
            <thetext>This wouldn&apos;t work, since not a file descriptor, but a FILE pointer is needed.
However, I believe the patch from LFS is incorrect. Not fopen should be called,
but fdopen on the descriptor:

-    if ((sfd == -1) || (sfp = fopen (sfn, &quot;w+&quot;)) == NULL)
+    if ((sfd == -1) || (sfp = fdopen (sfd, &quot;w+&quot;)) == NULL)
</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vapier@gentoo.org</who>
            <bug_when>2004-12-27 05:37:23 0000</bug_when>
            <thetext>true ... robbat2, that patch looks much more sane, does that resolve this for you ?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robbat2@gentoo.org</who>
            <bug_when>2005-01-09 02:50:32 0000</bug_when>
            <thetext>Ok, your patch change there to use fdopen fixes the problem.
I&apos;ve put it in the tree as r5, but arches need to stabilize.
The changed patch should probably be sent upstream.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vapier@gentoo.org</who>
            <bug_when>2005-01-09 12:31:12 0000</bug_when>
            <thetext>marked stable ;)</thetext>
          </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>45580</attachid>
            <date>2004-12-08 18:27 0000</date>
            <desc>edtest.cpp</desc>
            <filename>edtest.cpp</filename>
            <type>text/plain</type>
            <data encoding="base64">Ly8gZWR0ZXN0LmNwcAojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5j
bHVkZSA8dGltZS5oPgojaW5jbHVkZSA8dW5pc3RkLmg+CiNpbmNsdWRlIDx3YWl0Lmg+CmludCBt
YWluKGludCBhcmdjLCBjaGFyKiogYXJndikgewoJaW50IHBbMl07CgkvL3ByaW50ZigiWzBdIG1h
a2luZyBhIHBpcGVcbiIpOwoJcGlwZShwKTsKCWNoYXIqIGZpbGVuYW1lID0gYXJndlsxXTsKCS8v
cHJpbnRmKCJbMF0gZm9ya2luZ1xuIik7CglwaWRfdCBjcGlkID0gZm9yaygpOwoJaWYoY3BpZCA9
PSAtMSkgewoJCWZwcmludGYoc3RkZXJyLCJmb3JrIGZhaWxlZFxuIik7CgkJZXhpdCgyKTsKCX0K
CWlmKGNwaWQgPT0gMCkgewoJCS8vcHJpbnRmKCJbMV0gY2xvc2luZyBmZHNcbiIpOwoJCWNsb3Nl
KDApOwoJCWNsb3NlKDEpOwojaWZuZGVmIFdPUktJTkcKCQljbG9zZSgyKTsgLy8gaWYgeW91IGNv
bW1lbnQgb3V0IHRoaXMgbGluZSwgdGhlIHByb2dyYW0gd29ya3MgZmluZQojZW5kaWYKCQlkdXAy
KHBbMF0sIDApOwoKCQkvL3ByaW50ZigiWzFdIGV4ZWNscCBcbiIpOwoJCWV4ZWNscCgiZWQiLCAi
ZWQiLCBmaWxlbmFtZSwgMCk7CgkJZXhpdCgxKTsgLy8gd2Ugb25seSByZWFjaCBoZXJlIGluIGNh
c2Ugb2YgYW4gZXJyb3IhCgl9CgkvL3ByaW50ZigiWzBdIG9wZW5pbmcgcGlwZVxuIik7CglGSUxF
ICpjbWQgPSBmZG9wZW4ocFsxXSwgInciKTsKCS8vcHJpbnRmKCJbMF0gc2VuZGluZyBjb21tYW5k
c1xuIik7CglmcHJpbnRmKGNtZCwgIixzL2hlbGxvL2dvb2RieWUvZ1xuIik7CglmcHJpbnRmKGNt
ZCwgIndcbiIpOwoJZnByaW50ZihjbWQsICJRXG4iKTsKCS8vcHJpbnRmKCJbMF0gZmx1c2hpbmcg
Y29tbWFuZHNcbiIpOwoJZmZsdXNoKGNtZCk7CglpbnQgc3RhdHVzOwoJLy9wcmludGYoIlswXSBj
aGVja2luZyBzdGF0dXMgXG4iKTsKCXdhaXRwaWQoMCwgJnN0YXR1cywgMCk7CglpZihXSUZFWElU
RUQoc3RhdHVzKSkgewoJCWZwcmludGYoc3RkZXJyLCAiZWQgcmV0dmFsPSVkXG4iLFdFWElUU1RB
VFVTKHN0YXR1cykpOwoJfSBlbHNlIHsKCQlmcHJpbnRmKHN0ZGVyciwgImVkIHRlcm1pbmF0ZWQg
YWJub3JtYWxseVxuIik7Cgl9CglyZXR1cm4gMDsKfQo=
</data>        

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>45581</attachid>
            <date>2004-12-08 18:28 0000</date>
            <desc>dotest.sh</desc>
            <filename>dotest.sh</filename>
            <type>text/plain</type>
            <data encoding="base64">IyEvYmluL3NoCkYxPS90bXAvdGVzdGZpbGUud29ya2luZwpGMj0vdG1wL3Rlc3RmaWxlLmJyb2tl
bgoKc2hvd2NvbnRlbnRzKCkgewoJZj0iJDEiCgllY2hvIC1lICJDb250ZW50cyBvZiAkZlxuLS0t
IgoJY2F0ICRmCgllY2hvICItLS0iCn0KZWNobyAiQ29tcGlsaW5nIGJpbmFyaWVzIgpnKysgLVdh
bGwgZWR0ZXN0LmNwcCAtbyBlZHRlc3Qud29ya2luZyAtRFdPUktJTkcKZysrIC1XYWxsIGVkdGVz
dC5jcHAgLW8gZWR0ZXN0LmJyb2tlbgoKZWNobyAiIyMjIElucHV0IHRvIHdvcmtpbmcgYmluYXJ5
IgplY2hvIGhlbGxvID4kRjEKc2hvd2NvbnRlbnRzICRGMQplY2hvICIjIyMgUnVubmluZyB3b3Jr
aW5nIGJpbmFyeSIKLi9lZHRlc3Qud29ya2luZyAkRjEKZWNobyAiIyMjIE91dHB1dCBmcm9tIHdv
cmtpbmcgYmluYXJ5IgpzaG93Y29udGVudHMgJEYxCgoKZWNobyAiIyMjIElucHV0IHRvIGJyb2tl
biBiaW5hcnkiCmVjaG8gaGVsbG8gPiRGMgpzaG93Y29udGVudHMgJEYyCmVjaG8gIiMjIyBSdW5u
aW5nIGJyb2tlbiBiaW5hcnkiCi4vZWR0ZXN0LmJyb2tlbiAkRjIKZWNobyAiIyMjIE91dHB1dCBm
cm9tIGJyb2tlbiBiaW5hcnkiCnNob3djb250ZW50cyAkRjIKCg==
</data>        

          </attachment>
    </bug>

</bugzilla>