LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 08-12-2011, 06:27 AM   #1
jazzmo
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Rep: Reputation: Disabled
suid bit on executable wont spawn a shell


Hi,

I've trying to understand the concept of buffer overflows and I tried to understand how the shellcode is executed. To start simple I've created the following program
Code:
// shell.c 
int main(){ 
  char *name[2]; 

  name[0] = "/bin/sh"; 
  name[1] = 0x0; 
  execve(name[0], name, 0x0); 
  exit(0); 
}
now when i compile this code and
Code:
# chown root:root shell 
# chmod 4755 shell
# ls -ahl
# -rwsr-sr-x  1 root   root  8.0K Aug 11 17:08 shell
I would expect when I run this as a regular user I get a root shell. What happens is that a shell with my user is spawned. On the other hand when I compile this code
Code:
//suidshell.c 
#include <sys/types.h> 
#include <unistd.h> 
#include <stdio.h> 

int main(void) { 
    printf( 
        "Real      UID = %d\n" 
        "Effective UID = %d\n" 
        "Real      GID = %d\n" 
        "Effective GID = %d\n", 
        getuid (), 
        geteuid(), 
        getgid (), 
        getegid() 
    ); 
    return 0; 
}
compile it, and set the owner and permissions as in the example above i get as output
Code:
Real UID = 1001 
Effective UID = 0 
Real GID = 100 
Effective GID = 100
so it looks like there is some mechanism (in the kernel?) which prevents a suid program from executing a root shell. Is there any way to "switch" this off? I've learned for example that ASLR can be switched off with

Code:
#  sysctl -w kernel.randomize_va_space=0
thanks for the answers,
and kind regards
 
Old 08-12-2011, 08:06 AM   #2
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
Quote:
so it looks like there is some mechanism (in the kernel?) which prevents a suid program from executing a root shell.
Yes, there is. I was looking into this a few months back and ran into similar "problems" and discovered that there are more security mechanism built into setuid than just setting the bit and changing the ownership to root. I apologize, but it has been too long since I worked on it to recall the exact details, but if you do a google search for setuid tutorials you should find the references which will show you how to make it work.
 
Old 08-12-2011, 09:38 AM   #3
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
/bin/sh is typically a link to /bin/bash. For a while now, bash will always switch to real user-id. If you add a call in your shell example:
Code:
 setuid(0);
You should find that it works as expected. NOTE: this is because of how setuid behaves when euid = 0. As a confirmation, change the setuid sticky bit on your bash shell and watch what happens :P

One additional thing to note is that this helps to enforce the mindset of "don't use setuid scripts." There are many reasons why we want to avoid them, not the least of which is because people can find TONS of ways to inject execution into your setuid script without you even knowing it.

-Aaron
 
Old 08-23-2011, 09:36 AM   #4
jazzmo
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi,

I finally found the answer to the question:

i downloaded the bash source and grepped geteuid

Code:
grep -r getuid bash-3.01
two hits were found. One in ./shell.c another in ./lib/intl/dcigettext.c
after replacing the occurences with geteuid it just worked fine.

Thanks for helping.

Last edited by jazzmo; 08-24-2011 at 01:05 AM.
 
Old 08-23-2011, 07:13 PM   #5
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Gentoo
Posts: 2,125

Rep: Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781Reputation: 781
Jazzmo, would you please elaborate on your approach. I don't mean to offend, but modifying the bash source code without performing some serious regression testing might be a really good way to open up some vulnerabilities.

I might also suggest that you take a look at this link: http://www.tuxation.com/setuid-on-shell-scripts.html
I am reasonably certain that it is the one that I used when I was experimenting with setuid and had the same problem that I think you are running into.
 
Old 08-24-2011, 01:14 AM   #6
jazzmo
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Noway2 View Post
Jazzmo, would you please elaborate on your approach. I don't mean to offend, but modifying the bash source code without performing some serious regression testing might be a really good way to open up some vulnerabilities.

I might also suggest that you take a look at this link: http://www.tuxation.com/setuid-on-shell-scripts.html
I am reasonably certain that it is the one that I used when I was experimenting with setuid and had the same problem that I think you are running into.
Hi Noway2,

as I wrote in the beginning, i'm experimenting with buffer overflows. Mostly they spawn a shell. I didn't understand how someone would get a root shell by exploiting a setuid executable. Now I understand that the shell itself prevents this, so the shellcode must do something different. If the process is run by root the exploit would result in a root shell. Of course I wouldn't weaken the security of my productive systems this way, although I also learned that this could be a simple backdoor too.

cheers
 
Old 08-25-2011, 03:42 AM   #7
rodrifra
Member
 
Registered: Mar 2007
Location: Spain
Distribution: Debian
Posts: 202

Rep: Reputation: 36
If you want to experiment and learn, try http://roothack.org/games/sirens/info first server is too simple, just privilege scalation. Sencond server is the interesting one, there you have to do buffer overflows, use string vulnerabilities and so on. I learnt a lot there.
 
Old 08-25-2011, 06:21 AM   #8
jazzmo
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
On more note on this. Since /bin/dash isn't checking the uid it WILL spawn a root shell, so compared to bash it is less secure in my opinion.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected curly braces in expect script spawn command & bash suid problem slinx Programming 1 05-02-2008 01:47 PM
inetd-like program that wont spawn new instances, but kills service on disconnection? PureRumble Linux - Software 0 04-28-2007 08:15 PM
suid bit on an executable is ignored during boot process. rob_of_ownsboro Linux - General 3 11-22-2005 12:34 PM
SUID file drops suid bit on append? c_coder Programming 1 03-12-2004 07:59 AM
Diferrence between suid and executable by all? lugoteehalt Linux - General 5 10-18-2003 04:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 01:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration