<?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>154310</bug_id>
          
          <creation_ts>2006-11-06 22:19 0000</creation_ts>
          <short_desc>mail-client/mutt &lt;= 1.5.12 temp file issue (CVE-2006-529[78])</short_desc>
          <delta_ts>2007-09-01 21:33:59 0000</delta_ts>
          
          
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Gentoo Security</product>
          <component>Vulnerabilities</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://marc.theaimsgroup.com/?l=mutt-dev&amp;m=115999486426292&amp;w=2</bug_file_loc>
          <status_whiteboard>B? [noglsa] vorlon</status_whiteboard>
          
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          <dependson>178003</dependson>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>jaervosz@gentoo.org</reporter>
          <assigned_to>security@gentoo.org</assigned_to>
          <cc>agriffis@gentoo.org</cc>
    
    <cc>ferdy@gentoo.org</cc>
    
    <cc>net-mail@gentoo.org</cc>

      

      
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2006-11-06 22:19:38 0000</bug_when>
            <thetext>It would seem that Mutt&apos;s temp file creation mechanisms all suffer
from a potentially exploitable race condition.  Actually there are
two: one in functions that call safe_open(), which only affects users
creating temp files on NFS file systems (due to the O_EXCL problem),
and one in functions that make use of safe_fopen(), because the
resulting file is not adequately checked to determine if other users
can modify it before it is written to.

On Wed, Oct 04, 2006 at 11:19:26AM -0400, Kyle Wheeler wrote:
&gt;     char tmpfile[POSIX_PATH_MAX] = &quot;/tmp/muttXXXXXX&quot;;
&gt;     char tmpfile2[POSIX_PATH_MAX];
&gt;     char *extension = &quot;foo&quot;;
&gt;     // mkstemp should come first
&gt;     fd = mkstemp(tmpfile);
&gt;     sprintf(tmpfile2, &quot;%s.%s&quot;, tmpfile, extension);
&gt;     rename(tmpfile, tmpfile2);

The trouble with this is that while tmpfile is &quot;guaranteed&quot; unique,
tmpfile.foo may well not be.  :(  Granted, it would be hard to
exploit, but theoretically not impossible.  You still need to be
careful to open the file with O_CREAT|O_EXCL and expect that it could
fail (which I believe mutt does, though I didn&apos;t verify every possible
case).  

Unfortunately even that isn&apos;t really enough given that lots of people
set TMPDIR to something in their home directory, and have home
directories on NFS.  Opening a file this way (with O_EXCL) on NFS is
broken (and thus so are any implementations of mkstemp() family
functions that rely on it). You can do the link(2) trick too, but
AFAICT there&apos;s no way to guarantee that the file you&apos;re creating is
unique on NFS without encountering a race condition.  This is a
weakness in the NFS protocol IIRC, not in the implementation of
mkstemp(). [This may not be the case with NFSv4, I am uncertain.]
The man page for open(2) mentions this.  That&apos;s problem #1.

The scheme that mutt currently uses for most of its temp files
(_mutt_mktemp()/safe_open()) involves using the host name, UID, pid,
and an internal counter maintained by mutt.  The trouble is, it&apos;s
fairly likely that a user will go through the exact same steps when
they boot their workstation (i.e. when starting mutt immediately after
booting the system), so for any particular user the likelihood is
surprisingly high that all of these pieces (including the PID) except
for the counter will frequently be the same for any given user, thus
very guessable.  Only the counter will change, and it&apos;s always
initialized to zero, so that also is not hard to guess.  At a glance,
I don&apos;t see mutt doing anything to check to see if for such files, the
user is the only one who can read and/or write to the file.  Certainly
functions that call safe_open() use O_EXCL, but due to the problems
with that working on NFS, that may not be enough.  

Also NFS or no NFS, with regard to mutt_adv_mktemp() for attachments,
calls to safe_fopen() generally use &quot;w+&quot; which means the file will be
truncated if it exists, but that doesn&apos;t do anything to ensure that
the file so created couldn&apos;t be moved aside and replaced with a
malicious file should the directory be writable by someone else; nor
does it do anything to prevent the race where someone else creates the
file in between the calls to mktemp() and safe_fopen().  In the latter
case, the file will be truncated, but the file is still writable by
the owner and can potentially be modified in a malicious way before
mutt runs the MIME type&apos;s helper program on it. 

Is this exploitable?  It would seem so, though executing such a
maneuver is probably exceedingly difficult.  An attacker would need to
be able to guess the temp file name in advance, know what kind of data
was going into it, and be able to craft malicious data which would
exploit some problem in the program it was going to be fed into...

Still, mutt really ought to be creating a unique directory in which to
create its temp files, and before doing so it should make sure the
permissions on that directory are 700 and owned by the user.  It
should also make sure that the parent directory either allows access
only to the user, or that it has the sticky bit set.  There&apos;s still a
possible race condition involving creating a unique directory name,
but fortunately mkdir(2) always fails if the directory already exists,
which largely solves the problem.  Further, checking the permissions
and ownership on the directory before creating the temp file should
eliminate any remaining doubt.  


On Wed, Oct 04, 2006 at 11:36:43AM -0400, Kyle Wheeler wrote:
&gt; On the other hand, mkstemps() would solve the problem, at least for 
&gt; the OS&apos;s that support it, i.e. all modern Unixes (it works on Tru64 
&gt; Unix, Solaris, FreeBSD, OpenBSD, NetBSD, MacOS X, OpenSolaris, 
&gt; Solaris, and Linux). 

$ uname -smr
Linux 2.4.20-20.9 i686
$ man mkstemps
No manual entry for mkstemps
$ gcc mkstemps.c
/home/ddm/tmp/ccvsG8nh.o(.text+0x17): In function `main&apos;:
: undefined reference to `mkstemps&apos;
collect2: ld returned 1 exit status

You were saying?

This function is not sufficiently portable to rely upon its presence.
At best you&apos;d need to provide an alternate implementation, in which
case there&apos;s no obvious benefit to using it... mutt_adv_mktemp()
basically does the same thing already.

Note that mkstemps also relies on O_EXCL so it also suffers from a
race condition on NFS.

-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail.  Sorry for the inconvenience.  Thank the spammers.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2006-11-06 22:21:16 0000</bug_when>
            <thetext>OWL has a large patch reworking tmp file handling in mutt: http://cvsweb.openwall.com/cgi/cvsweb.cgi/Owl/packages/mutt/mutt-1.4.2.1-owl-tmp.diff</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vorlon@gentoo.org</who>
            <bug_when>2006-11-07 03:12:46 0000</bug_when>
            <thetext>opening the bug since the mail can be found at 
&lt;http://marc.theaimsgroup.com/?l=mutt-dev&amp;m=115999486426292&amp;w=2&gt;

&lt;http://secunia.com/advisories/22613/&gt;</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vorlon@gentoo.org</who>
            <bug_when>2006-11-09 06:10:32 0000</bug_when>
            <thetext>CC&apos;ing maintainers

Ubuntu announcement can be found at
http://www.ubuntu.com/usn/usn-373-1
so there should be patches available too</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2006-11-20 22:41:21 0000</bug_when>
            <thetext>Pulling in herd for advise/bumping.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ferdy@gentoo.org</who>
            <bug_when>2006-11-20 23:49:09 0000</bug_when>
            <thetext>Ok,

I&apos;ve digged in the recent mutt commits and the only reference to temp files / security is the attached one.

I&apos;ll apply it later today if any of you can ACK the patch.

- ferdy</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ferdy@gentoo.org</who>
            <bug_when>2006-11-20 23:50:13 0000</bug_when>
            <thetext>Created an attachment (id=102446)
paranoid-temp-file.patch

Proposed patch from upstream.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ferdy@gentoo.org</who>
            <bug_when>2006-11-22 00:17:58 0000</bug_when>
            <thetext>That patch is being applied in mail-client/mutt-1.5.13-r2. If it is not enough to fix this bug please state so and I&apos;ll try to get in touch with upstream.

- ferdy</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2006-11-22 00:29:03 0000</bug_when>
            <thetext>Vorlon is this patch enough?</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vorlon@gentoo.org</who>
            <bug_when>2006-11-27 06:55:39 0000</bug_when>
            <thetext>could someone pls verify the patch? (tavis/...?)</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ferdy@gentoo.org</who>
            <bug_when>2007-03-20 20:04:33 0000</bug_when>
            <thetext>mutt-1.5.14 is in the tree now (with this patch included too). I haven&apos;t seen more patches related to this bug.

Is this bug still considered valid? Do I have to do anything else?

- ferdy</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2007-03-25 11:00:32 0000</bug_when>
            <thetext>Tavis please advise.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2007-04-18 05:55:09 0000</bug_when>
            <thetext>Tavis please advise.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2007-05-02 12:10:14 0000</bug_when>
            <thetext>Tavis please advise.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2007-05-20 07:31:42 0000</bug_when>
            <thetext>Tavis please advise.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>falco@gentoo.org</who>
            <bug_when>2007-06-09 21:25:47 0000</bug_when>
            <thetext>&gt;     char tmpfile[POSIX_PATH_MAX] = &quot;/tmp/muttXXXXXX&quot;;
&gt;     char tmpfile2[POSIX_PATH_MAX];
&gt;     char *extension = &quot;foo&quot;;
&gt;     // mkstemp should come first
&gt;     fd = mkstemp(tmpfile);
&gt;     sprintf(tmpfile2, &quot;%s.%s&quot;, tmpfile, extension);
&gt;     rename(tmpfile, tmpfile2);

rename() is not vulnerable to a symlink attack, unlike open(). The race conditions explained in the mail needs other successful attacks, or administrator misconfigurations. Non-issue for me.


For the O_EXCL which doesn&apos;t work on NFS, that may be a weakness, but this would affect all other apps using O_EXCL. It&apos;s not mutt specific and it can&apos;t simply be solved. Furthermore, /tmp on a NFS... sounds... eccentric. (why not, but in that case it is also a misconfiguration since that will affect many applications). Non-issue for me. 

I&apos;m an addicted mutt user and despite of that, I think we can close this bug as Invalid.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>falco@gentoo.org</who>
            <bug_when>2007-06-09 21:50:09 0000</bug_when>
            <thetext>mmm after a deeper look to the mailing list, the syscall to rename() should be checked (return value equals 0 or -1). Indeed, if the files are:

-rw------- 1 falco   falco    0 Jun  9 23:46 muttjlNOGW
-rw-r--r-- 1 foo     foo     5 Jun  9 23:46 muttjlNOGW.foo

Then the rename() call fails, mutt continues, and read/write from/to the crafted muttjlNOGW.foo file. Yes, that&apos;s a bug (another user could view the mails). Not a security issue (DoS, code exec, or so).
</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>py@gentoo.org</who>
            <bug_when>2007-07-21 10:15:24 0000</bug_when>
            <thetext>according to the CVE refs, this affects versions 1.5.12 and earlier, and we have version .13 stable and versions .14 and .15 unstable in the tree, so what should we do here? Ferdy/Aggrifis/net-mail, please advise.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ferdy@gentoo.org</who>
            <bug_when>2007-08-08 09:47:44 0000</bug_when>
            <thetext>I just commited mutt-1.5.16. There is no point in stabling .15 so I&apos;d go for .16 myself once arch people deem it appropiate. We can start removing vulnerable unused versions though.

- ferdy</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>jaervosz@gentoo.org</who>
            <bug_when>2007-08-14 10:32:20 0000</bug_when>
            <thetext>Handling stable marking on bug #178003</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>py@gentoo.org</who>
            <bug_when>2007-09-01 21:33:59 0000</bug_when>
            <thetext>finally closing without GLSA wrt the discussion on bug 178003, feel free to reopen  if you disagree.</thetext>
          </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>102446</attachid>
            <date>2006-11-20 23:50 0000</date>
            <desc>paranoid-temp-file.patch</desc>
            <filename>paranoid-temp-file.patch</filename>
            <type>text/plain</type>
            <data encoding="base64">Y29tbWl0I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SF9NQVhdOwogCisgICAgaWYgKG11dHRfbWt3cmFwZGlyIChwYXRoLCBz
YWZlX2ZpbGUsIHNpemVvZiAoc2FmZV9maWxlKSwKKwkJCXNhZmVfZGlyLCBzaXplb2YgKHNhZmVf
ZGlyKSkgPT0gLTEpCisgICAgICByZXR1cm4gLTE7CisgICAgCisgICAgaWYgKChmZCA9IG9wZW4g
KHNhZmVfZmlsZSwgZmxhZ3MsIDA2MDApKSA8IDApCisgICAgeworICAgICAgcm1kaXIgKHNhZmVf
ZGlyKTsKKyAgICAgIHJldHVybiBmZDsKKyAgICB9CisgICAgCisgICAgaWYgKG11dHRfcHV0X2Zp
bGVfaW5fcGxhY2UgKHBhdGgsIHNhZmVfZmlsZSwgc2FmZV9kaXIpID09IC0xKQorICAgIHsKKyAg
ICAgIGNsb3NlIChmZCk7CisgICAgICByZXR1cm4gLTE7CisgICAgfQorICB9CisgIGVsc2UKKyAg
eworICAgIGlmICgoZmQgPSBvcGVuIChwYXRoLCBmbGFncywgMDYwMCkpIDwgMCkKKyAgICAgIHJl
dHVybiBmZDsKKyAgfQorICAgIAogICAvKiBtYWtlIHN1cmUgdGhlIGZpbGUgaXMgbm90IHN5bWxp
bmsgKi8KICAgaWYgKGxzdGF0IChwYXRoLCAmb3NiKSA8IDAgfHwgZnN0YXQgKGZkLCAmbnNiKSA8
IDAgfHwKICAgICAgIGNvbXBhcmVfc3RhdCgmb3NiLCAmbnNiKSA9PSAtMSkK
</data>        

          </attachment>
    </bug>

</bugzilla>