Subject: NetBSD Security Advisory 2001-009: Race condition between sugid-exec and ptrace(2)
To: None <netbsd-announce@netbsd.org>
From: NetBSD Security Officer <security-officer@netbsd.org>
List: current-users
Date: 07/24/2001 14:55:10
-----BEGIN PGP SIGNED MESSAGE-----

                 NetBSD Security Advisory 2001-009
                 =================================

Topic:		Race condition between sugid-exec and ptrace(2)

Version:	All official releases up to and including 1.5

Severity:	Local user may gain superuser privileges

Fixed:		NetBSD-current:		June 15, 2001
		NetBSD-1.5 branch:	June 17, 2001 (1.5.1 includes the fix)
		NetBSD-1.4 branch:	July 19, 2001

Abstract
========

A race condition between the setuid/setgid handling in the
execve(2) system call and the ptrace(2) system call can allow
a local user to cause a setuid-root executable to execute
arbitrary code as the superuser.


Technical Details
=================

The execve(2) system call causes a process to begin execution of
a new program image.  This system call has a facility known as
sugid, which allows certain programs to run with the user and/or
group ID indicated by the file system permissions.  This facility
is used by, e.g. the su(1) program to allow certain users to gain
superuser privileges.  Once a process uses the sugid facility,
it is marked as having done so, in order for other kernel facilities
to make decisions based on this information.

The ptrace(2) system call is a debugging facility.  It provides
the means for debuggers, such as gdb(1), to access the memory
and registers of another process.  The kernel allows a process
to ptrace(2) another process only if they are owned by the same
user ID, or if the tracing process has superuser privileges.  If
a process was exec'd using sugid, tracing of that process is
disallowed unless done by the superuser.

The execve(2) code contains a check to skip sugid processing if
a process is currently being traced by ptrace(2).  However, in
vulnerable systems, two problems exist:

	* The check for a traced process in the sugid-exec
	  code is performed too early.  The check is made
	  before the new executable image is opened, which
	  may block.

	* The marking of a process as sugid happens too late.
	  The process is marked as sugid after a call which
	  allocates memory.  The memory allocation may block.

While the process exec'ing the new program image is asleep in
either of these two places, another process owned by the same
user can ptrace(2) the process.  Once attached to the sugid
process, the attacker can modify the sugid process's memory
image and modify the program counter register, allowing the
attacker to execute arbitrary code.


Solutions and Workarounds
=========================

Kernel sources must be updated and a new kernel built and installed.
The instructions for updating your kernel sources depend upon which
particular NetBSD release you are running.

* NetBSD-current:

	Systems running NetBSD-current dated from before 2001-06-15
	should be upgraded to NetBSD-current dated 2001-06-15 or later.

	The following source directories need to be updated from
	the netbsd-current CVS branch (aka HEAD):
		src/sys/compat/netbsd32
		src/sys/kern

	Alternatively, apply the following patch (with potential offset
	differences):
		ftp://ftp.netbsd.org/pub/NetBSD/security/patches/SA2001-009-ptrace-1.5.patch


* NetBSD 1.5:

	Systems running NetBSD 1.5 dated from before 2001-06-17 should be
	upgraded from NetBSD 1.5 sources dated 2001-06-17 or later.

	The following source directories need to be updated from the
	netbsd-1-5 CVS branch:
		src/sys/compat/netbsd32
		src/sys/kern

	Alternatively, apply the following patch (with potential offset
	differences):
		ftp://ftp.netbsd.org/pub/NetBSD/security/patches/SA2001-009-ptrace-1.5.patch

	NetBSD 1.5.1 is not vulnerable.


* NetBSD 1.4, 1.4.1, 1.4.2, 1.4.3:


	It is believed the 1.4 versions are vulnerable to this issue, but
	a working exploit could not be produced. The following is recommended
	action for 1.4 systems.

	Systems running NetBSD 1.4 dated from before 2001-07-19 should be
	upgraded from NetBSD 1.4 sources dated 2001-07-19 or later.

	The following source directory needs to be updated from
	the netbsd-1-4 CVS branch:
		src/sys/kern

	Alternatively, apply the following patch (with potential offset
	differences):
		ftp://ftp.netbsd.org/pub/NetBSD/security/patches/SA2001-009-ptrace-1.4.patch


Once the kernel sources have been updated, rebuild the kernel,
install it, and reboot.  For more information on how to do this,
see:

    http://www.netbsd.org/Documentation/kernel/#building_a_kernel


Thanks To
=========

Georgi Guninski for pointing out that the problem existed on the
Bugtraq mailing list.

Artur Grabowski for some initial discussion about the problem.

Jason R. Thorpe for investigating and fixing the problem.

Bill Sommerfeld for additional discussion and code review.


Revision History
================

	2001-07-20	Initial revision


More Information
================

An up-to-date PGP signed copy of this release will be maintained at
  ftp://ftp.netbsd.org/pub/NetBSD/security/advisories/NetBSD-SA2001-009.txt.asc

Information about NetBSD and NetBSD security can be found at
http://www.NetBSD.ORG/ and http://www.NetBSD.ORG/Security/.


Copyright 2001, The NetBSD Foundation, Inc.  All Rights Reserved.

$NetBSD: NetBSD-SA2001-009.txt,v 1.9 2001/07/20 01:14:34 lukem Exp $
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (NetBSD)
Comment: For info see http://www.gnupg.org

iQCVAwUBO1eSDj5Ru2/4N2IFAQH40gP/bIylOl0aB3J3quxc27DxDv2Qtri6rUql
FKIyGqxkT+Bjeem3mJSYMWPCDRbZrcgxJdZopphbwe05DYLi06rAE56SmVFWsOLA
9UDPKfFoFUVah1j/VKiLuGtRoxQn2TCrQWy5k30thaRM+iQs29ahVRaLgyhEOjXQ
BhC+i1tM4SY=
=kdL7
-----END PGP SIGNATURE-----