LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SUID and SGID ? Why don't they work ? (https://www.linuxquestions.org/questions/linux-newbie-8/suid-and-sgid-why-dont-they-work-665142/)

tungvs 08-24-2008 11:34 PM

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 :D.

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 :).

normscherer 08-24-2008 11:48 PM

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.

Mr. C. 08-25-2008 01:56 AM

Quote:

Originally Posted by normscherer (Post 3258570)
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

tungvs 08-25-2008 09:23 AM

@ Mr. C.: Thank you. That's what I need :D.
@ normscherer: Thanks for your quick reply, the problem does not really come from shell-kind. :)

tungvs 08-25-2008 12:02 PM

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" ? :D

Mr. C. 08-25-2008 12:38 PM

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.

tungvs 08-25-2008 09:32 PM

- Thanks for reminding me of EUID :D. 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.

Mr. C. 08-25-2008 09:38 PM

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.

tungvs 08-28-2008 10:45 AM

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.

Mr. C. 08-28-2008 11:25 AM

One word: SELinux.

tungvs 08-29-2008 12:30 AM

@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.

Mr. C. 08-29-2008 12:39 AM

Does the mounted file system allow setuid programs? Check the mount options with mount.

tungvs 08-29-2008 05:48 AM

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 ?

tungvs 08-31-2008 07:42 PM

There's no explain up to now.
Please help. Thanks.

Mr. C. 08-31-2008 10:54 PM

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.


All times are GMT -5. The time now is 02:07 PM.