LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2008, 12:34 AM   #1
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Rep: Reputation: 15
SUID and SGID ? Why don't they work ?


I've read somewhere that when you run a script file with SUID, all the commands in the script file will have the owner's permissions. The same with SGID. So I have a test:

Script file:
Code:
-rwsr-xr-x myacc myacc  usertest
Content:
Code:
whoami
cat secret.txt


Text file:
Code:
-rw------- myacc myacc secret.txt
Content: not your concern .

I login as user "youracc", execute the script, and this is the result:
Code:
youracc@mydesktop:/home/myacc$ ./usertest 
youracc
cat: secret.txt: Permission denied
I don't get it . Why doesn't SUID work ? The same problem with SGID.

Any helps will be appreciated .
 
Old 08-25-2008, 12:48 AM   #2
normscherer
Member
 
Registered: Sep 2005
Location: On the road
Distribution: Ubuntu 8.10
Posts: 41

Rep: Reputation: 15
It depends on the shell you use. Some will change to the effective uid before they execute so what you want to do will not work. Try other shells.
 
Old 08-25-2008, 02:56 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by normscherer View Post
It depends on the shell you use. Some will change to the effective uid before they execute so what you want to do will not work. Try other shells.
This makes no sense.

The problem is that the linux kernel prohibits setuid/setgid shell scripts. Write a program using a language such as C or C++.

Setuid/setgid shell scripts have been a source of security problems, and have been strongly discouraged for years.

See:
http://www.tuxation.com/setuid-on-shell-scripts.html
http://www.samag.com/documents/s=114...106a/0106a.htm

Last edited by Mr. C.; 08-25-2008 at 02:57 AM.
 
Old 08-25-2008, 10:23 AM   #4
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
@ Mr. C.: Thank you. That's what I need .
@ normscherer: Thanks for your quick reply, the problem does not really come from shell-kind.
 
Old 08-25-2008, 01:02 PM   #5
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
Following the second link from Mr. C., I found this line of script:
cp /bin/sh /tmp/sh; chown root /tmp/sh; chmod 4755 /tmp/sh

If I get it right, it means: copy a shell to /tmp, set the shell's owner to be root, set SUID for the shell. Now we have a shell which can run as root .

I've tried running the script and "chown: Operation not permitted" is what I've received. It makes me confused: to do a chown, we must have root privileges (actually I don't know the differences between "to be root" and "to have root privileges" ), using sudo or something like that. But if we can "sudo", or have root privileges, why do we want to have a shell which "runs as root" ?
 
Old 08-25-2008, 01:38 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
A user and a group are simple concepts in Unix/Linux. All processes have a UID/GID/EUID/EGID. The acronyms should be obvious, with the exception being the E, meaning "effective".

To be root is layman's terms to be logged in as root, or to have a shell that has been created with su or sudo to be a root shell. This is nothing more than a process running (shell) with UID=0.

To have root privileges means the process is running as UID=0. I'll ignore GID/EGID, as these group counterparts apply in similar fashion.

It should be clear that UID=0 is what determines permission to access a resource for a given mode (read/write/execute/examine). Names like "root", "toor", "admin" are all just decorative, and administrative candy above UIDs. Same for groups.

The line of code:

Code:
cp /bin/sh /tmp/sh; chown root /tmp/sh; chmod 4755 /tmp/sh
is EXTREMELY dangerous and highly insecure. DON'T DO IT, find another way.

As an aside, /tmp is often mounted with the nosetuid flag to DISABLE setuid, and often the noexec flags to deny running an executable from /tmp.

chown requires the process running it to have the UID of either the owner of the object itself, or UID=0. And chmod requires UID=0 if the running process is not the owner, or belong to one of the object's groups if group write is enabled.
 
Old 08-25-2008, 10:32 PM   #7
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
- Thanks for reminding me of EUID . Btw, my /tmp is not mount separately, it stays inside / partition.
- The code is a way to get a root shell by exploiting SUID
- After the "cp" command, I am the owner of /tmp/sh:
Code:
-rwxr-xr-x 1 myacc myacc 79988 2008-08-26 09:04 /tmp/sh
Then why can't I chown it ? The /tmp has permissions: rwxrwxrwt, and I'm using Ubuntu 8.04. Maybe the code above is written for older linux OSs, which could be vulnerable for this.

Last edited by tungvs; 08-25-2008 at 10:35 PM.
 
Old 08-25-2008, 10:38 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
A non-root user can NEVER chown files to another user (eg. you can't force files onto another user; consider how this would be a DoS attack on a user with quota's enabled). Only root can do this.

The code above looks like code I've seen from a piece of scripted malware, try to gain a root shell, and planting a trojan horse.
 
Old 08-28-2008, 11:45 AM   #9
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
One more interesting issue I've encountered:
This is my program used to test SUID:
Code:
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;

int main(int argc, char** argv) {

    // Toggle comment to test
    // seteuid(0);

    // Call system command
    system("/usr/bin/whoami");
    system("/bin/cat /etc/shadow");

    uid_t euid;
    uid_t ruid;
    uid_t suid;

    // Get RUID, EUID, SUID.
    // Prototype:     int getresuid(uid_t *, uid_t *, uid_t *);
    // Return: 0 - success, 1 - failed
    int result = getresuid(&ruid, &euid, &suid);

    if(result == 1)
    {
        cout << "GetUID failed.";
    }
    else
    {
        cout << "EUID: " << euid << endl;
        cout << "RUID: " << ruid << endl;
        cout << "SUID: " << suid << endl;
    }


    return (EXIT_SUCCESS);
}
After compiling the file with g++:
Code:
g++ source -o binary
I've chown and chmod the binary file. Its permission:
Code:
-rwsr-xr-x 1 root   myacc 9588 2008-08-28 22:16 binary
Then I used myacc user to execute the file. The myacc is a normal user.
There's a difference of results between RHEL 4 and ubuntu 8.04:

Result in Ubuntu 8.04:
Quote:
myacc@myacc-desktop:~/NetBeansProjects/useridtest$ ./binary
root
//
// My /etc/shadow content
//
EUID: 0
RUID: 1000
SUID: 0
Result in RHEL 4:
Quote:
myacc@myacc-desktop:~/NetBeansProjects/useridtest$ ./binary
myacc
/bin/cat: /etc/shadow: Permission denied
EUID: 0
RUID: 500
SUID: 0
The result in Ubuntu is exactly what I expected, but I can't understand the result in RHEL. As I understand, linux will determine privileges of the process from EUID.

Please explain what happened to my knowledge .
Thanks in advance.

Last edited by tungvs; 08-28-2008 at 11:53 AM.
 
Old 08-28-2008, 12:25 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
One word: SELinux.
 
Old 08-29-2008, 01:30 AM   #11
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
@Mr. C.: Thanks for giving a clue, but it didn't help much, because for avoiding troubles related to SELinux (I've heard SELinux is quite difficult to config), I've disabled SELinux from installing steps of RHEL 4. I've just checked it again, and its state was currently disabled.

Please give me more details (I hope this time you'll leave me more than one word ). Thanks.
 
Old 08-29-2008, 01:39 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Does the mounted file system allow setuid programs? Check the mount options with mount.
 
Old 08-29-2008, 06:48 AM   #13
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
I think my file system allows suid.
Quote:
LABEL=/ / ext3 defaults 1 1
because in mount man page, there's a paragraph:
Quote:
defaults: Use default options: rw, suid, dev, exec, auto, nouser and async

PS: by the way, please pay attention to my above result in RedHat: EUID = 0 but `whoami` still calls me 'myacc', and I cannot read /etc/shadow file. Could it be RedHat user authentication based on RUID, not EUID ?

Last edited by tungvs; 08-29-2008 at 06:52 AM.
 
Old 08-31-2008, 08:42 PM   #14
tungvs
Member
 
Registered: May 2008
Distribution: Centos; Ubuntu; Fedora
Posts: 98

Original Poster
Rep: Reputation: 15
There's no explain up to now.
Please help. Thanks.
 
Old 08-31-2008, 11:54 PM   #15
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
There are only three things I know of that affect root setuid for binary executables: SELinux/AppArmor, file system mount options, and ACLs.

What are the ACLs for the binary and containing directory? If this lends no clues, I'm out of options.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
which suid sgid to unset? hank43 Linux - Security 1 10-09-2006 02:46 AM
suid on directory - doesn't, sgid ok? pingu Linux - Security 1 01-18-2006 05:04 PM
suid/sgid question plan9 Linux - Security 1 07-08-2004 08:15 AM
Question: the concept of suid and sgid feetyouwell Linux - Software 4 02-03-2004 05:12 PM
SGID and SUID on Directories mikeyt_333 Linux - General 1 03-26-2002 03:41 PM


All times are GMT -5. The time now is 10:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration