<?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>214403</bug_id>
          
          <creation_ts>2008-03-23 16:30 0000</creation_ts>
          <short_desc>mail-filter/policyd-weight &lt;0.1.14.17 temporary file vulnerability (CVE-2008-{1569,1670})</short_desc>
          <delta_ts>2008-04-11 17:10:37 0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <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>
          
          <status_whiteboard>B3 [glsa]</status_whiteboard>
          
          <priority>P2</priority>
          <bug_severity>minor</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>vorlon@gentoo.org</reporter>
          <assigned_to>security@gentoo.org</assigned_to>
          <cc>chris@chrishowells.co.uk</cc>
    
    <cc>robtone@ek-muc.de</cc>
    
    <cc>thijs@debian.org</cc>
    
    <cc>ticho@gentoo.org</cc>
    
    <cc>waja@cyconet.org</cc>

      

      
          <long_desc isprivate="0">
            <who>vorlon@gentoo.org</who>
            <bug_when>2008-03-23 16:30:47 0000</bug_when>
            <thetext>The following has been reported to us by Chris Howells (CC&apos;ed), thanks for that.
This bug is confidential for now. Chris, besides security@debian.org did you also try to contact upstream?


Hi,

I believe I have discovered an insecure temporary file vulnerability in policyd-weight, which is in the repositories of Debian etch.

Snippets of code from /usr/sbin/policyd-weight

my $LOCKPATH          = &apos;/tmp/.policyd-weight/&apos;;

Then the function create_lockpath chown&apos;s lockpath:

sub create_lockpath
{
    my $who = shift(@_);

    if(!( -d $LOCKPATH))
    {
        mkdir $LOCKPATH or die &quot;$who: error while creating $LOCKPATH: $!&quot;;
    }

    my $tuid = $USER;

    if($USER =~ /[^0-9]/)
    {
        if( !(defined( $tuid = getpwnam($USER) ) ) )
        {
            mylog(warning=&gt;&quot;User $USER doesn&apos;t exist, create it, or set \$USER&quot;);
        }
    }
    if( !(chown ($tuid, -1, $LOCKPATH)) )
    {
        mylog(warning=&gt;&quot;Couldn&apos;t chown $LOCKPATH to $USER: $!&quot;);
    }
    if( !(chmod (0700, $LOCKPATH)) )
    {
        mylog(warning=&gt;&quot;Couldn&apos;t set permissions on $LOCKPATH: $!&quot;);
    }
}

I have verified that by doing something like this:

mkdir /home/chris/foo
ln -s /home/chris/foo /tmp/.policyd-weight

and starting policyd-weight you can change the ownership or any arbitrary directory around the system - /home/chris/foo got chown&apos;ed to polw:root.

I&apos;m sure you can also play tricks with the creation and deletion of the socket too as they get unlink&apos;ed in a few places, but haven&apos;t bothered trying to exploit them as I think this is plenty to be sure that there is a problem.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vorlon@gentoo.org</who>
            <bug_when>2008-03-23 17:31:05 0000</bug_when>
            <thetext>from Chris, who currently can&apos;t post to this bug cause of bugzilla trouble:

&quot;No, I have not contacted upstream, it doesn&apos;t seem to be very actively developed any more. I have however reported it to the FreeBSD security team in addition to you and Debian.&quot;

There is a note on the website btw:
Policyd-weight is not actively maintained as of 09th Feb 2008.
linking to:
http://news.gmane.org/find-root.php?group=gmane.mail.postfix.policyd-weight&amp;article=810</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-24 02:54:01 0000</bug_when>
            <thetext>Last relevant e-mails on upstream mailinglist mention new developers being added to policyd-weight sf.net project. Currently there are four, two of them with admin rights (including old maintainer, who is thought to be inactive).

I have sent them an e-mail with Chris&apos; report (Chris was also CCd). Let&apos;s see if they respond.

Thanks for the report, Chris!</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-27 09:15:56 0000</bug_when>
            <thetext>Old maintainer responded on policyd-weight mailing list, releasing a new version (0.1.14.15), which should have this issue fixed. I just committed it to portage. Can someone please verify if the fix is working? I have to head to work now and will not be able to test until tonight.

Thanks!</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-27 22:53:35 0000</bug_when>
            <thetext>OK, looks like latest version is no good - I was still able to change ownership of /root to polw:root. I reported this upstream, let&apos;s wait for them.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-03-28 00:20:19 0000</bug_when>
            <thetext>The patch introduces a symlink check, but I do not see how that is safe to race attacks. Looking at the &quot;create_lockpath&quot; function, it should be possible for an attacker to previously create the $LOCKPATH directory, and replace it (after the symlink check) by a symlink.

The socket and data files should be moved to /var/run
The website also states [1]: &quot;Workaround: change $LOCKPATH from /tmp/.policyd-weight to /var/run/.policyd-weight. You need to manually kill policyd-weight and to remove the old directory.&quot;
</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-03-28 00:21:01 0000</bug_when>
            <thetext>public via http://www.us.debian.org/security/2008/dsa-1531</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>thijs@debian.org</who>
            <bug_when>2008-03-28 09:15:43 0000</bug_when>
            <thetext>(In reply to comment #6)
&gt; public via http://www.us.debian.org/security/2008/dsa-1531

Just to note that this was already publicised on the upstream mailinglist last Sunday, the DSA itself did not make it public.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robtone@ek-muc.de</who>
            <bug_when>2008-03-28 13:23:48 0000</bug_when>
            <thetext>(In reply to comment #5)
&gt; The patch introduces a symlink check, but I do not see how that is safe to race
&gt; attacks. Looking at the &quot;create_lockpath&quot; function, it should be possible for
&gt; an attacker to previously create the $LOCKPATH directory, and replace it (after
&gt; the symlink check) by a symlink.
&gt; 
&gt; The socket and data files should be moved to /var/run
&gt; The website also states [1]: &quot;Workaround: change $LOCKPATH from
&gt; /tmp/.policyd-weight to /var/run/.policyd-weight. You need to manually kill
&gt; policyd-weight and to remove the old directory.&quot;

I&apos;d like to change the working directory after handling of such /tmp issues has been sorted out; would the following patch suffice?:

--- old/policyd-weight      Fri Mar 28 10:06:46 2008
+++ new/policyd-weight      Fri Mar 28 14:08:30 2008
@@ -23,9 +23,9 @@
 # see http://spf.pobox.com/
 #
 # AUTHOR:  r.felber@ek-muc.de
-# DATE:    Fri Mar 28 10:08:42 CET 2008
+# DATE:    Fri Mar 28 14:08:20 CET 2008
 # NAME:    policyd-weight
-# VERSION: 0.1.14 beta-16
+# VERSION: 0.1.14 beta-17
 # URL:     http://www.policyd-weight.org/


@@ -65,6 +65,7 @@
 # begin
 use strict;
 use Fcntl;
+use File::Spec;
 use Sys::Syslog qw(:DEFAULT setlogsock);
 use Net::DNS;
 use Net::DNS::Packet qw(dn_expand);
@@ -78,7 +79,7 @@

 use vars qw($csock $s $tcp_socket $sock $new_sock $old_mtime);

-our $VERSION   = &quot;0.1.14 beta-16&quot;;
+our $VERSION   = &quot;0.1.14 beta-17&quot;;
 our $CVERSION  = 5;                 # cache interface version
 our $CMD_DEBUG = 0;                 # -d switch
 our $KILL;                          # -k switch
@@ -3614,6 +3615,9 @@
 }


+
+
+
 #
 # usage: check_symlnk($caller, @files)
 #
@@ -3625,17 +3629,53 @@
     for ( @_ )
     {

-        # strip trailing &apos;/&apos;
-        # perl and test(1) ignore the request for -l/-L and
-        # do a lstat with S_IFDIR (added in 0.1.14 beta-16)
-        s/\/+$//;
+        my $file = File::Spec-&gt;canonpath($_);
+
+        my @stat = lstat( $file );

-        if ( -l $_ )
+        if(! -e _)
         {
-            fatal_exit(&quot;$who: $_ is a symbolic link. Symbolic links are not expected and not allowed within policyd-weight. Exiting!&quot;);
+            next;
         }
+
+
+        # first, file must not be a symlink
+        if ( -l _ )
+        {
+            fatal_exit(&quot;$who: $file is a symbolic link. Symbolic links are not expected and not allowed within policyd-weight. Exiting!&quot;);
+        }
+
+
+        # second, file must be owned by uid root or $USER and
+        # gid root/wheel or $USER
+        if(!
+            (
+                $stat[4] == getpwnam($USER) ||
+                $stat[4] == &quot;0&quot;
+            ) &amp;&amp;
+            (
+                $stat[5] == getgrnam($GROUP) ||
+                $stat[5] == &quot;0&quot;
+            )
+          )
+        {
+            fatal_exit(&quot;$who: $file is owned by UID $stat[4], GID $stat[5]. Exiting!&quot;);
+        }
+
+
+        # second the file/dir must not be world writeable
+        if(sprintf(&quot;%04o&quot;,$stat[2]) =~ /(7|6|3|2)$/)
+        {
+            fatal_exit(&quot;$who: $file is world writeable. Exiting!&quot;);
+        }
+
+
     }
 }
+
+
+
+


 # function for sanitizing floating point output

</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robtone@ek-muc.de</who>
            <bug_when>2008-03-28 14:07:47 0000</bug_when>
            <thetext>(In reply to comment #8)
&gt; (In reply to comment #5)
&gt; &gt; The patch introduces a symlink check, but I do not see how that is safe to race
&gt; &gt; attacks. Looking at the &quot;create_lockpath&quot; function, it should be possible for
&gt; &gt; an attacker to previously create the $LOCKPATH directory, and replace it (after
&gt; &gt; the symlink check) by a symlink.
&gt; &gt; 
&gt; &gt; The socket and data files should be moved to /var/run
&gt; &gt; The website also states [1]: &quot;Workaround: change $LOCKPATH from
&gt; &gt; /tmp/.policyd-weight to /var/run/.policyd-weight. You need to manually kill
&gt; &gt; policyd-weight and to remove the old directory.&quot;
&gt; 
&gt; I&apos;d like to change the working directory after handling of such /tmp issues has
&gt; been sorted out; would the following patch suffice?:

Previous patch was buggy (missing two brackets), here is a patch for the patch:

--- old/policyd-weight      Fri Mar 28 15:01:29 2008
+++ new/policyd-weight      Fri Mar 28 14:45:38 2008
@@ -3648,15 +3648,14 @@

         # second, file must be owned by uid root or $USER and
         # gid root/wheel or $USER
-        if(!(
-                (
-                    $stat[4] == getpwnam($USER) ||
-                    $stat[4] == &quot;0&quot;
-                ) &amp;&amp;
-                (
-                    $stat[5] == getgrnam($GROUP) ||
-                    $stat[5] == &quot;0&quot;
-                )
+        if(!
+            (
+                $stat[4] == getpwnam($USER) ||
+                $stat[4] == &quot;0&quot;
+            ) &amp;&amp;
+            (
+                $stat[5] == getgrnam($GROUP) ||
+                $stat[5] == &quot;0&quot;
             )
           )
         {


</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-28 18:22:03 0000</bug_when>
            <thetext>Robert,

1. Your patch is based on version 0.1.14 beta-16, which I did not even know was released
2. Please do not post patches inline, as it tends to break patch format (line breaks, whitespace) after copy&amp;pasting. Instead, please attach the patch as text/plain.

That said, thanks for the time you are still putting into policyd-weight.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-28 18:28:38 0000</bug_when>
            <thetext>The newly released 0.1.14 beta-17 (fresh in portage as policyd-weight-0.1.14.17) seems like it&apos;s checking for symlinks correctly.

@rbu: Perhaps it&apos;s time to involve arch teams to stabilize this version.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>ticho@gentoo.org</who>
            <bug_when>2008-03-29 00:16:58 0000</bug_when>
            <thetext>I have just updated the ebuild to change default $LOCKPATH to /var/run/policyd-weight, which is created with 0700 permissions and owned by polw:root.

@security: Now would be the best time to involve arch teams.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-03-29 00:56:00 0000</bug_when>
            <thetext>Robert, the check via lstat seems plausibly secure to me. Please consider using the /var/run directory though, as that is what the directory is intended for.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-03-29 00:57:55 0000</bug_when>
            <thetext>Arches, please test and mark stable:
=mail-filter/policyd-weight-0.1.14.17
Target keywords : &quot;release x86&quot;
</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robtone@ek-muc.de</who>
            <bug_when>2008-03-29 09:03:28 0000</bug_when>
            <thetext>(In reply to comment #10)
&gt; Robert,
&gt; 
&gt; 1. Your patch is based on version 0.1.14 beta-16, which I did not even know was
&gt; released

I published a patch containing version information on the ML. Although I didn&apos;t release it.

&gt; 2. Please do not post patches inline, as it tends to break patch format (line
&gt; breaks, whitespace) after copy&amp;pasting. Instead, please attach the patch as
&gt; text/plain.

Sorry. Haven&apos;t seen the box for attachments. I apologize.

&gt; That said, thanks for the time you are still putting into policyd-weight.

I&apos;ll do so also in the future (at least for security related stuff and code
for which I was responsible). I personally still use it, too. But I don&apos;t to development on it anymore.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>robtone@ek-muc.de</who>
            <bug_when>2008-03-29 09:08:52 0000</bug_when>
            <thetext>(In reply to comment #13)
&gt; Robert, the check via lstat seems plausibly secure to me. Please consider using
&gt; the /var/run directory though, as that is what the directory is intended for.
&gt; 

Yes. Will do. This requires some preparation for the unaware user, though. Also I wanted to sort this out, instead of run away to other file locations while keeping the initial checking issues.

I wanted first to sort this out to protect users wich change a possible secure temporarily working directory to an insecure directory like /tmp.

</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>fauli@gentoo.org</who>
            <bug_when>2008-03-29 18:18:45 0000</bug_when>
            <thetext>x86 stable</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-03-29 19:47:44 0000</bug_when>
            <thetext>I vote YES for a GLSA.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>keytoaster@gentoo.org</who>
            <bug_when>2008-03-29 20:06:16 0000</bug_when>
            <thetext>Voting YES and filing request.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>pva@gentoo.org</who>
            <bug_when>2008-03-30 11:13:43 0000</bug_when>
            <thetext>Fixed in release snapshot.</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>rbu@gentoo.org</who>
            <bug_when>2008-04-11 17:10:37 0000</bug_when>
            <thetext>GLSA 200804-11</thetext>
          </long_desc>
      
    </bug>

</bugzilla>