<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: OS X Applications Insecurely Installing World-Writable Files	</title>
	<atom:link href="/articles/os-x-applications-insecurely-installing-world-writable-files/feed/" rel="self" type="application/rss+xml" />
	<link>/articles/os-x-applications-insecurely-installing-world-writable-files/</link>
	<description>Software Engineer and Consultant</description>
	<lastBuildDate>Sat, 29 Oct 2016 01:50:59 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Arndt Schönewald		</title>
		<link>/articles/os-x-applications-insecurely-installing-world-writable-files/comment-page-1/#comment-61914</link>

		<dc:creator><![CDATA[Arndt Schönewald]]></dc:creator>
		<pubDate>Sat, 03 Aug 2013 13:42:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.blackdown.de/?p=586#comment-61914</guid>

					<description><![CDATA[Besides dangerous file and directory permissions I am also concerned with ownerships: everything in /System, /Library and /Applications should be owned by system uids and not by real human users as the security implications are quite similar. Unfortunately, the OSX-typical practice of users installing apps by moving them into /Applications will create just this scenario, and also apps which do come with installers will often have their items installed using undesirable user and group ownerships (even non-existing users and groups which probably slipped through from the development or build systems).

This problem has bothered me enough to come up with below script to check and fix the /Applications and /Library directory trees. (I leave /System, /usr, /bin etc. alone since I found no problems here on my machines, but the script will still display a short report about &quot;noteworthy stuff&quot; in these directories, too.)

Enjoy!
Arndt


#!/bin/bash -e
# vi: set ts=4 sw=4 ai:

# normalize file ownerhip and permissions for /Applications and /Library
# version: 1.0 2013-08-03 for Mac OS X 10.8.4

DOIT=-1   # default: ask for confirmation before making any changes
AREA=ALO  # default: do /Applications, /Library and other checks

while (($# &#062; 0))
do
    case &quot;$1&quot; in
        -y&#124;--yes)   DOIT=1;; # don&#039;t ask, just do it
        -n&#124;--no)    DOIT=0;; # don&#039;t ask, don&#039;t do it
        -A)         OAPP=1;; # select /Applications
        -L)         OLIB=1;; # select /Library
        -O)         OOTH=1;; # select other checks
        *)          echo &#062;&#038;2 &quot;${0##*/}: bad args&quot;; exit 2;;
    esac
    shift
done

[[ -z &quot;$OAPP$OLIB$OOTH&quot; ]] &#124;&#124; AREA=&quot;${OAPP:+A}${OLIB:+L}${OOTH:+O}&quot;

if [[ $(id -u) != 0 ]]
then
    echo &quot;${0##*/}: please run this script as root&quot;
    exit 2
fi

M() { (IFS=&quot; &quot;; printf &quot;\n### %s\n\n&quot; &quot;$*&quot;) }

A()
{
    if ((${#ITEMS[*]} == 0))
    then
        echo &quot;(nothing to do)&quot;
        return 0
    fi
    
    # list all offending files and dirs
    printf &quot;%s\n&quot; &quot;${ITEMS[@]}&quot; &#124; tr &#039;12&#039; &#039;&#039; &#124; xargs -0 ls -leOd

    if (($DOIT == 0))
    then
        return 0

    elif (($DOIT &#060; 0))
    then
        while :
        do
            (IFS=&#034; &#034;; echo -n &#034;Command for these items: $* -- execute (y&#124;n) ? &#034;)
            read yn
            [[ $yn = n ]] &#038;&#038; return 2
            [[ $yn = y ]] &#038;&#038; break
        done
    fi

    # execute the given command on all offending files and dirs
    if [[ $1 != SetOwnerGroup ]]
    then
        printf &#034;%s\n&#034; &#034;${ITEMS[@]}&#034; &#124; tr &#039;12&#039; &#039;&#039; &#124; xargs -0t &#034;$@&#034;
        return $?
    else
        for i in &#034;${ITEMS[@]}&#034;
        do
            grp=$(stat -f %Sg &#034;${i%/*}&#034;) &#038;&#038; [[ $grp = admin ]] &#124;&#124; grp=wheel
            (set -x; chown -h &#034;root:$grp&#034; &#034;$i&#034;)
        done
    fi
}

# IFS must be set to \n for the ITEMS=($(...)) assignments below
IFS=&#034;${IFS#??}&#034;

if [[ $AREA = *A* ]]
then
    echo
    echo ====== /Applications ======

    M remove .DS_Store files except those inside an app and belonging to root

    ITEMS=($(
        find -s /Applications \
            -name .DS_Store &#034;(&#034; ! -user root -or ! -path &#034;*.app/.DS_Store&#034; ! -path &#034;*.app/*/.DS_Store&#034; &#034;)&#034;
    ))

    A rm -f


    M make everything below /Applications world readable and executable

    ITEMS=($(
        find -s /Applications ! -perm -0044 -or -perm +0100 ! -perm -0011
    ))

    A chmod -h go+rX


    M revoke write permission for group and other

    ITEMS=($(
        # valid as of 10.8.4:
        # all files and dirs should be ???r-Xr-X, except
        #   - g=w: /Applications/, /Applications/System Preferences.app//
        #   - g=w is okay if user=root and group=admin
        # A violation of the exception condition (i.e. if o=w) will cause
        # the removal of the g=w permission, too.
        find -sE /Applications \
            ! &#034;(&#034; -regex &#034;/Applications/System Preferences.app(/.*)?&#034; -or -user root -group admin &#034;)&#034; -perm +0022 \
            -or -perm +0002
    ))

    A chmod -h go-w


    M assign all apps, subdirectories and files to user root, group wheel

    ITEMS=($(
        # 10.8.4: all files and dirs should be root:wheel or root:admin
        find -s /Applications ! &#034;(&#034; -user root &#034;(&#034; -group wheel -or -group admin &#034;)&#034; &#034;)&#034;
    ))

    A chown -h root:wheel
fi

if [[ $AREA = *L* ]]
then
    echo
    echo &#034;====== /Library ======&#034;

    M remove all .DS_Store files

    ITEMS=($(
        find -s /Library -name .DS_Store
    ))

    A rm -f


    M ensure universal readability and executability

    ITEMS=($(
        find -s /Library \
            -user root -or -path &#034;/Library/Caches/*&#034; \
            -or &#034;(&#034; ! -perm -0044 -or -perm +0100 ! -perm -0011 &#034;)&#034; -print
    ))

    A chmod -h go+rX


    M revoke write permission for other

    ITEMS=($(
        # generally leave files alone which are owned by root, but make sure
        # /Library/Application Support// gets fixed
        find -sE /Library \
            -regex &#034;/Library/Application Support(/.*)?&#034; -perm +0002 -print \
            -or -user root -or -path &#034;/Library/Caches/*&#034; -or -path &#034;/Library/Preferences/*&#034; \
            -or -perm +0002 -print
    ))

    A chmod -h o-w


    M assign stray items of group wheel or admin to user root

    ITEMS=($(
        find -s /Library \
            ! -path &#034;/Library/Caches/*&#034; ! -path &#034;/Library/Logs/*&#034; \
            ! -user root &#034;(&#034; -group wheel -or -group admin &#034;)&#034; &#124;
        while read i; do
            # filter by uid to ignore items owned by system users
            [[ $(stat -f %u &#034;$i&#034;) -lt 500 &#038;&#038; $(stat -f %Su &#034;$i&#034;) != &#034;(&#034;*&#034;)&#034; ]] &#124;&#124; echo &#034;$i&#034;
        done
    ))

    A chown -h root

    # only proceed if above step has been successfully executed
    if (($? == 0 &#038;&#038; $DOIT != 0))
    then
        M for stray items of other groups, use the group of the parent dir
        
        ITEMS=($(
            find -s /Library \
                ! -path &#034;/Library/Caches/*&#034; ! -path &#034;/Library/Logs/*&#034; \
                &#034;(&#034; ! -user root -or ! -group wheel ! -group admin &#034;)&#034; &#124;
                perl -e &#039;print reverse &#039; &#124;
            while read i; do
                # filter by uid to ignore items owned by system users
                [[ $(stat -f %u &quot;$i&quot;) -lt 500 &#038;&#038; $(stat -f %Su &quot;$i&quot;) != &quot;(&quot;*&quot;)&quot; ]] &#124;&#124; echo &quot;$i&quot;
            done
        ))

        A SetOwnerGroup
    fi
fi

if [[ $AREA = *O* ]]
then
    echo
    echo &quot;====== other checks ======&quot;

    M check for unsual permissions and ownerships in /bin, /etc, /sbin, /usr, /System

    # ignores softlinks, and items owned by root with group wheel or admin
    # which don&#039;t have the setuid, setgid and sticky bit set and which are
    # only writable by user; lists everything else
    find -s /bin /etc /sbin /usr /System \
        -type l -or -user root &quot;(&quot; -group wheel -o -group admin &quot;)&quot; ! -perm +7022 -or -ls
fi

###
]]></description>
			<content:encoded><![CDATA[<p>Besides dangerous file and directory permissions I am also concerned with ownerships: everything in /System, /Library and /Applications should be owned by system uids and not by real human users as the security implications are quite similar. Unfortunately, the OSX-typical practice of users installing apps by moving them into /Applications will create just this scenario, and also apps which do come with installers will often have their items installed using undesirable user and group ownerships (even non-existing users and groups which probably slipped through from the development or build systems).</p>
<p>This problem has bothered me enough to come up with below script to check and fix the /Applications and /Library directory trees. (I leave /System, /usr, /bin etc. alone since I found no problems here on my machines, but the script will still display a short report about &#8220;noteworthy stuff&#8221; in these directories, too.)</p>
<p>Enjoy!<br />
Arndt</p>
<p>#!/bin/bash -e<br />
# vi: set ts=4 sw=4 ai:</p>
<p># normalize file ownerhip and permissions for /Applications and /Library<br />
# version: 1.0 2013-08-03 for Mac OS X 10.8.4</p>
<p>DOIT=-1   # default: ask for confirmation before making any changes<br />
AREA=ALO  # default: do /Applications, /Library and other checks</p>
<p>while (($# &gt; 0))<br />
do<br />
    case &#8220;$1&#8221; in<br />
        -y|&#8211;yes)   DOIT=1;; # don&#8217;t ask, just do it<br />
        -n|&#8211;no)    DOIT=0;; # don&#8217;t ask, don&#8217;t do it<br />
        -A)         OAPP=1;; # select /Applications<br />
        -L)         OLIB=1;; # select /Library<br />
        -O)         OOTH=1;; # select other checks<br />
        *)          echo &gt;&amp;2 &#8220;${0##*/}: bad args&#8221;; exit 2;;<br />
    esac<br />
    shift<br />
done</p>
<p>[[ -z &#8220;$OAPP$OLIB$OOTH&#8221; ]] || AREA=&#8221;${OAPP:+A}${OLIB:+L}${OOTH:+O}&#8221;</p>
<p>if [[ $(id -u) != 0 ]]<br />
then<br />
    echo &#8220;${0##*/}: please run this script as root&#8221;<br />
    exit 2<br />
fi</p>
<p>M() { (IFS=&#8221; &#8220;; printf &#8220;\n### %s\n\n&#8221; &#8220;$*&#8221;) }</p>
<p>A()<br />
{<br />
    if ((${#ITEMS[*]} == 0))<br />
    then<br />
        echo &#8220;(nothing to do)&#8221;<br />
        return 0<br />
    fi</p>
<p>    # list all offending files and dirs<br />
    printf &#8220;%s\n&#8221; &#8220;${ITEMS[@]}&#8221; | tr &#8217;12&#8217; &#8221; | xargs -0 ls -leOd</p>
<p>    if (($DOIT == 0))<br />
    then<br />
        return 0</p>
<p>    elif (($DOIT &lt; 0))<br />
    then<br />
        while :<br />
        do<br />
            (IFS=&quot; &quot;; echo -n &quot;Command for these items: $* &#8212; execute (y|n) ? &quot;)<br />
            read yn<br />
            [[ $yn = n ]] &amp;&amp; return 2<br />
            [[ $yn = y ]] &amp;&amp; break<br />
        done<br />
    fi</p>
<p>    # execute the given command on all offending files and dirs<br />
    if [[ $1 != SetOwnerGroup ]]<br />
    then<br />
        printf &quot;%s\n&quot; &quot;${ITEMS[@]}&quot; | tr &#039;12&#039; &#039;&#039; | xargs -0t &quot;$@&quot;<br />
        return $?<br />
    else<br />
        for i in &quot;${ITEMS[@]}&quot;<br />
        do<br />
            grp=$(stat -f %Sg &quot;${i%/*}&quot;) &amp;&amp; [[ $grp = admin ]] || grp=wheel<br />
            (set -x; chown -h &quot;root:$grp&quot; &quot;$i&quot;)<br />
        done<br />
    fi<br />
}</p>
<p># IFS must be set to \n for the ITEMS=($(&#8230;)) assignments below<br />
IFS=&quot;${IFS#??}&quot;</p>
<p>if [[ $AREA = *A* ]]<br />
then<br />
    echo<br />
    echo ====== /Applications ======</p>
<p>    M remove .DS_Store files except those inside an app and belonging to root</p>
<p>    ITEMS=($(<br />
        find -s /Applications \<br />
            -name .DS_Store &quot;(&quot; ! -user root -or ! -path &quot;*.app/.DS_Store&quot; ! -path &quot;*.app/*/.DS_Store&quot; &quot;)&quot;<br />
    ))</p>
<p>    A rm -f</p>
<p>    M make everything below /Applications world readable and executable</p>
<p>    ITEMS=($(<br />
        find -s /Applications ! -perm -0044 -or -perm +0100 ! -perm -0011<br />
    ))</p>
<p>    A chmod -h go+rX</p>
<p>    M revoke write permission for group and other</p>
<p>    ITEMS=($(<br />
        # valid as of 10.8.4:<br />
        # all files and dirs should be ???r-Xr-X, except<br />
        #   &#8211; g=w: /Applications/, /Applications/System Preferences.app//<br />
        #   &#8211; g=w is okay if user=root and group=admin<br />
        # A violation of the exception condition (i.e. if o=w) will cause<br />
        # the removal of the g=w permission, too.<br />
        find -sE /Applications \<br />
            ! &quot;(&quot; -regex &quot;/Applications/System Preferences.app(/.*)?&quot; -or -user root -group admin &quot;)&quot; -perm +0022 \<br />
            -or -perm +0002<br />
    ))</p>
<p>    A chmod -h go-w</p>
<p>    M assign all apps, subdirectories and files to user root, group wheel</p>
<p>    ITEMS=($(<br />
        # 10.8.4: all files and dirs should be root:wheel or root:admin<br />
        find -s /Applications ! &quot;(&quot; -user root &quot;(&quot; -group wheel -or -group admin &quot;)&quot; &quot;)&quot;<br />
    ))</p>
<p>    A chown -h root:wheel<br />
fi</p>
<p>if [[ $AREA = *L* ]]<br />
then<br />
    echo<br />
    echo &quot;====== /Library ======&quot;</p>
<p>    M remove all .DS_Store files</p>
<p>    ITEMS=($(<br />
        find -s /Library -name .DS_Store<br />
    ))</p>
<p>    A rm -f</p>
<p>    M ensure universal readability and executability</p>
<p>    ITEMS=($(<br />
        find -s /Library \<br />
            -user root -or -path &quot;/Library/Caches/*&quot; \<br />
            -or &quot;(&quot; ! -perm -0044 -or -perm +0100 ! -perm -0011 &quot;)&quot; -print<br />
    ))</p>
<p>    A chmod -h go+rX</p>
<p>    M revoke write permission for other</p>
<p>    ITEMS=($(<br />
        # generally leave files alone which are owned by root, but make sure<br />
        # /Library/Application Support// gets fixed<br />
        find -sE /Library \<br />
            -regex &quot;/Library/Application Support(/.*)?&quot; -perm +0002 -print \<br />
            -or -user root -or -path &quot;/Library/Caches/*&quot; -or -path &quot;/Library/Preferences/*&quot; \<br />
            -or -perm +0002 -print<br />
    ))</p>
<p>    A chmod -h o-w</p>
<p>    M assign stray items of group wheel or admin to user root</p>
<p>    ITEMS=($(<br />
        find -s /Library \<br />
            ! -path &quot;/Library/Caches/*&quot; ! -path &quot;/Library/Logs/*&quot; \<br />
            ! -user root &quot;(&quot; -group wheel -or -group admin &quot;)&quot; |<br />
        while read i; do<br />
            # filter by uid to ignore items owned by system users<br />
            [[ $(stat -f %u &quot;$i&quot;) -lt 500 &amp;&amp; $(stat -f %Su &quot;$i&quot;) != &quot;(&quot;*&quot;)&quot; ]] || echo &quot;$i&quot;<br />
        done<br />
    ))</p>
<p>    A chown -h root</p>
<p>    # only proceed if above step has been successfully executed<br />
    if (($? == 0 &amp;&amp; $DOIT != 0))<br />
    then<br />
        M for stray items of other groups, use the group of the parent dir</p>
<p>        ITEMS=($(<br />
            find -s /Library \<br />
                ! -path &quot;/Library/Caches/*&quot; ! -path &quot;/Library/Logs/*&quot; \<br />
                &quot;(&quot; ! -user root -or ! -group wheel ! -group admin &quot;)&quot; |<br />
                perl -e &#039;print reverse &#8216; |<br />
            while read i; do<br />
                # filter by uid to ignore items owned by system users<br />
                [[ $(stat -f %u &#8220;$i&#8221;) -lt 500 &amp;&amp; $(stat -f %Su &#8220;$i&#8221;) != &#8220;(&#8220;*&#8221;)&#8221; ]] || echo &#8220;$i&#8221;<br />
            done<br />
        ))</p>
<p>        A SetOwnerGroup<br />
    fi<br />
fi</p>
<p>if [[ $AREA = *O* ]]<br />
then<br />
    echo<br />
    echo &#8220;====== other checks ======&#8221;</p>
<p>    M check for unsual permissions and ownerships in /bin, /etc, /sbin, /usr, /System</p>
<p>    # ignores softlinks, and items owned by root with group wheel or admin<br />
    # which don&#8217;t have the setuid, setgid and sticky bit set and which are<br />
    # only writable by user; lists everything else<br />
    find -s /bin /etc /sbin /usr /System \<br />
        -type l -or -user root &#8220;(&#8221; -group wheel -o -group admin &#8220;)&#8221; ! -perm +7022 -or -ls<br />
fi</p>
<p>###</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Injerto		</title>
		<link>/articles/os-x-applications-insecurely-installing-world-writable-files/comment-page-1/#comment-61805</link>

		<dc:creator><![CDATA[Injerto]]></dc:creator>
		<pubDate>Fri, 04 May 2012 20:48:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.blackdown.de/?p=586#comment-61805</guid>

					<description><![CDATA[It&#039;s very disgusting, indeed. I wonder what happens with applications installed via Appstore. The way you show it, if you grant remote access to a guest account, they (or some malicious script running on their machines) could replace your applications with infected executables. May be a chmod -R could help?]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s very disgusting, indeed. I wonder what happens with applications installed via Appstore. The way you show it, if you grant remote access to a guest account, they (or some malicious script running on their machines) could replace your applications with infected executables. May be a chmod -R could help?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
