PDA

Bekijk Volledige Versie : Red Hat 9: free tickets



Michal Zalewski
02/07/03, 22:05
[ This is not strictly a new vulnerability - but a description of
a flaw that can be combined with any of the minor vulnerabilities
that pop up once a week to turn them into a major vulnerability.
I will leave it up to the moderators of BUGTRAQ and VulnWatch to
approve or reject it... ]

The reason for this post is that largely under-appreciated file creation
vulnerabilities can now get a higher profile, I think. Below are some
hopefully interesting thoughts on turning almost any O_CREAT w/o O_EXCL in
a world-writable directory - or similar issues, you name it - into an
instant, generic root compromise scenario on modern Linux boxes (primarily
Red Hat and derivates)...

Until recently, you could basically exercise three possible active attack
vectors against such a /tmp vulnerability - none of which is particularly
tempting in terms of privilege escalation:

1) Denial of Service:

Just create an appropriate symlink to create /etc/nologin,
truncate /lib/libc.so.6 or do something of a similar nature once
followed by a broken application. This is possible in almost all
cases when a vulnerable application is run by a privileged user.
Fortunately or unfortunately, there's very little benefit for the
attacker in this scenario.

2) Contents manipulation:

Attack the application itself by securing a write access to the
soon-to-be-accessed temporary file (it's usually enough to create
it in advance). Then stuff its temporary file with some unexpected
data. This could be successful only if the application uses file
contents in some interesting way later on. While there are some
nice examples (for example, older versions of GCC), this is seldom
a feasible scenario, and is very application-specific.

3) Contents redirection:

If you can control the data written by the application to a temporary
file, you could use a symlink to force writing to a file like
/etc/ld.so.preload, /etc/passwd, ~/.ssh/authorized_keys or such. In
most cases, however, races occur in boot scripts, cron scripts,
non-suid applications run by privileged users, etc - and the attacker
can exercise very little control over the contents of such a file.

As far as I know, there was no neat and generic way to exploit an insecure
/tmp file creation alone - well, until now.

Starting release 9, Red Hat ships and uses pam_timestamp_check.so module
(accompanied by /sbin/pam_timestamp_check setuid helper), a part of the
new pam-0.75 (Pluggable Authentication Modules) package. PAM is a generic
centralized authentication and session management component that is also
shipped by an increasing number of other distributions, so it is
reasonable that the code is about to propagate to other distros.

The module mentioned implements a credential caching functionality, very
closely inspired on a tty ticketing system used in sudo. Most sudo users
are familiar with the fact they are not prompted for password if a
subsequent sudo session is opened shortly after a previous one on the same
terminal - and this is exactly what pam_timestamp_check tries to implement
for other services.

The system used in sudo is somewhat naive and does have its problems, but
the impact caused by an eventual ticket stealing attack is fairly minimal
- the user has to be trusted and listed in /etc/sudoers in the first
place, and the credentials that are cached are for his own account (sudo
users enter their own password, as opposed to root's).

The way Red Hat deployed this mechanism is badly broken, since they use it
to cache root credentials for access to critical components of the system,
and there are no restrictions as to who can use those components. While in
sudo, stealing or spoofing a ticket is worth exactly as much as knowing
the password for the account you already have access to, and the account
has to be trusted, in Red Hat, it is worth root's password almost all the
time, and any user can use it. As such, there should me much more caution
exercised with such a mechanism. But there is not, causing an obvious
exposure.

The way the module (and sudo) works, in essence, is that it gets current
pseudo-terminal name A (which can be trivially spoofed, but this is of no
relevance at the moment), current user name B, and the user for which
credentials are cached, C (usually root for Red Hat applications, user
himself for sudo). Then the code checks for /var/run/sudo/B/A:C (or
/var/run/sudo/B/A if B == C), and if the file is recent, the module
returns success, and enables the user to skip the usual password
authentication.

The mechanism is used in Red Hat to make it easier for users to perform
administrative tasks without having to switch to root via su or sudo,
granted they know the admin password. There is a number of management
applications that can be invoked via a single setuid PAM-enabled wrapper,
/usr/sbin/userhelper, that all have pam_timestamp_check.so included in their
PAM configs. From quite harmless ones, such as redhat-config-mouse,
to pretty much instant root scenarios once the mechanism is compromised
- say, redhat-config-rootpassword, redhat-install-packages, up2date-config,
redhat-config-services, etc.

A noble concept indeed, but there is a nasty issue - since there's no
check for file origin, it should be more than obvious that suddenly, any
insecure file creation problem in an application used by a superuser (or
from privileged scripts, such as boot rc files, crontabs, etc), it is
possible to spoof a ticket in /var/run and bypass root password prompt and
other checks, and perform administrative tasks, easily modifying system
config, installing custom components (say, a rootshell), etc. All this by
crafting a single symlink that is later opened with O_CREAT with no O_EXCL
or O_NOFOLLOW.

The simplest workaround for all concerned users is to first remove all
occurrences of pam_timestamp_check.so from /etc/pam.d, and replace
/sbin/pam_timestamp_check standalone helper, if possible. Perhaps
reconsider the necessity of having /usr/sbin/userhelper mechanism
implemented at all.

For Red Hat, my suggestion is to verify ticket contents. Say, have a
host-wide random key K, and put user_name, expire_time, MD5(K + user_name
+ expire_time) in every ticket. The check code would verify the MD5
signature to make sure the origin of the ticket is sane, and the
originating application performed a specific operation on a not publicly
readable key.

On a side note, the per-terminal ticketing in pam_timestamp_check the way
it is has absolutely no significance and adds no protection, since the A
element of the path can be easily manipulated. Just an example (there are
other possible ways of accomplishing this):

ln `tty` /tmp/tty1
/usr/sbin/userhelper -w -t redhat-install-packages </tmp/tty1

As such, those tickets effectively become per-user, and an attacker who
compromised the account can snatch a ticket granted to the legitimate user
who already authenticated. Consider dropping the honor tty system and
granting per-user tickets to avoid giving users a false sense of security.

I mailed pam_timestamp_check maintainer at Red Hat (Nalin Dahyabhai) about
a week ago, but never heard back from him. Since this is not an issue
alone, I decided to post the information here.

--
------------------------- bash$ :(){ :|:&};: --
Michal Zalewski * [http://lcamtuf.coredump.cx]
Did you know that clones never use mirrors?
--------------------------- 2003-07-02 11:07 --

Michal Zalewski
03/07/03, 15:20
On Wed, 2 Jul 2003, Carlos Villegas wrote:

> This way of attack seems useless to me. This is also used on RH 8.0
> systems, and for both 8.0 and 9 systems:
>
> drwx------ 4 root root 4096 Jun 27 08:43 /var/run/sudo
>
> Which means that if the packages are properly built (and will make sure
> that this directory gets this permissions if it existed before the
> rpm is installed), this attack will gain you nothing, since you need
> to be root to exploit it.

You have missed a point.

Please look at any vulnerability archives on the net, there is one to
several insecure file creation reports every week in applications that
either are run as root, or are invoked from boot scripts, or from cron
jobs. In most of those cases, it is possible to create a dangling symlink
and then exploit this problem to create a file in a location the attacker
have chosen, with permissions of the victim (root).

Those vulnerabilities are generally considered a lesser threat, as there
seemed to be no practical method to easily gain root privileges just by
creating a file when no control over its contents can be exercised (again,
most cases). There is less interest in finding and fixing those problems,
and administrators are not that quick about addressing them.

Thanks to pam_timestamp_check[.so] and the way it is used in Red Hat, it
is now possible to gain root in a generic way in those scenarios.

That's all. I could post it along with results of a quick grep and a bunch
of programs that do create files this way, but I believe it would only
confuse the reader. I think it's pam_timestamp_check that should be fixed,
because it makes it needlessly trivial to exploit this vector.

--
------------------------- bash$ :(){ :|:&};: --
Michal Zalewski * [http://lcamtuf.coredump.cx]
Did you know that clones never use mirrors?
--------------------------- 2003-07-02 23:06 --

Carlos Villegas
03/07/03, 15:20
This way of attack seems useless to me. This is also used on RH 8.0
systems, and for both 8.0 and 9 systems:

drwx------ 4 root root 4096 Jun 27 08:43 /var/run/sudo

Which means that if the packages are properly built (and will make sure
that this directory gets this permissions if it existed before the
rpm is installed), this attack will gain you nothing, since you need
to be root to exploit it. If you can get root access to make this
attack possible, then you might as well launch a shell instead.

Carlos

Jon Hart
12/07/03, 23:35
On Sun, Jul 06, 2003 at 12:30:34PM -0700, Stephen Samuel wrote:
> The way it works is:
>
> ln -s /var/run/sudo/mylogin/0:root /tmp/likely_tmp_name
>
> Then you wait for or cause some setuid progrem to attempt to
> (insecurely) write to /tmp/likely_tmp_name . When that happens,
> /var/run/sudo/mylogin/0:root is created, and pam_timestamp_check
> now allows you to run any helper application as if you'd
> typed in the root password...
>
> Since these helper applicaitons tend to allow all sorts
> of nasty work (like creating a uid=0 user), you've now got a
> *serious* security violation on your hands.
>
> The face that /var/run/sudo is rwx root only is no protection
> because the open is doen by an suid-root program, and the symlink
> in /tmp can be created by anybody.
>
> Proof of concept:
>
> as youreslf:
> ln -s /var/run/sudo/$USER/unknown:root /tmp/oops
>
> as root:
> touch /tmp/oops
>
> as yourself: open the system-settings/users&groups utility from
> the desktop menu.
>
> You can now create an account with uid=0
>
> Now ANY exploit that can cause a setuid/root program to create
> an arbitrary file (regardless of content) can be used to create
> an arbitrary root user.
>
> Finding such an exploit is left as an exercise for the user.

Actually, I'm not sure this entirely true. Well, it is, but there is
another important condition that must be met for this (or similar)
attacks to work properly -- /var/run/sudo/$USER/ must exist. This means
that the user must have previously sudo'd at lease once and
/var/run/sudo/$USER/ will have been created.

I'm sure there are ways to work around this, but in my experiments,
/var/run/sudo/$USER/ must exist if you hope to exploit something like
this with the predictable file name creation + symlink trick.

-jon