Setuid usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setuid usage
# 1  
Old 11-28-2018
Setuid usage

I'm trying - as an ordinary user - to create a file in the root directory of my system. For that purpose I wrote a simple script that echoes a string into a file. I made the file executable, used sudo to change ownership to root. Like this:

Code:
$ cat hello

#!/bin/bash
echo hello > /hello

$ ls -l hello
-rwxr-xr-x 1 pi pi 33 Nov 29 02:28 hello
$ sudo chown root hello
$ sudo chgrp root hello
$ sudo chmod u+s hello
$ ls -l
total 4
-rwsr-xr-x 1 root root 33 Nov 29 02:28 hello

In my understanding if I now run the script as an ordinary user it should run with root privileges and create that file.
However, I get this error message:

Code:
$ ./hello
./hello: line 3: /hello: Permission denied

It works, however, if I use sudo to run the script:

Code:
$ sudo ./hello
$ ls -l /hello
-rw-r--r-- 1 root root 6 Nov 29 02:46 /hello

What am I missing?
# 2  
Old 11-28-2018
The first line of your script should be the #!/bin/bash; not the 2nd line. Since the 1st line of your script does not start with #!, the second line of your script is just a comment and has no effect on the program used to interpret your script.

The diagnostic you're seeing is from the echo. You don't have permission to overwrite your script with the echo since the echo is not running as the root user.

And, of course, giving a non-root user permission to overwrite a set-uid root script, allows any user on that system with that permission to do absolutely anything that user wants to do to that system. Please, never do this!
# 3  
Old 11-28-2018
Of course I will never do this. I just try to understand how the set-user-id bit is used in principle - for example by /usr/bin/passwd et. al. Isn't that in principle the same approach?

My intention is not to overwrite my script but to write the string "hello" into the newly created file with filename hello in the root directory.

Why does the echo not run as the root user? It is in a script that belongs to the root user, is executable and the set-user-id bit is set.

How do I get this to work?

(And the #!/bin/bash was in the first line where it belongs. The code just looked a bit cramped so I added a line.)
And no, I will not do this in any serious context and give other users access to this kind of code. Never ever.

------ Post updated at 08:29 PM ------

I just read elsewhere that the operating system ignores the setuid bit for executable shell scripts - for security reasons. Understandable. If that is so then I'll try it in C. Just to check it out.
# 4  
Old 11-28-2018
Yes, Linux ignores the suid bit on scripts. You need a binary that has setuid(0); because the suid bit sets the euid only.

(In contrast, setuid scripts do work on most if not all Unix versions.)
# 5  
Old 11-28-2018
You still haven't told us what operating system you're using...

Some operating systems NEVER allow shell scripts to be run set-uid.

Some operating systems NEVER allow a running program to be overwritten.

Some operating systems only allow shells specified in a system file to be used to run set-uid scripts.

All of the above are possible reasons why what you're trying might not work on an unspecified operating system.
# 6  
Old 11-28-2018
Worked like a charm:

Code:
#include <stdio.h>
#include <stdlib.h>

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

   FILE* fp = fopen( "/hi" , "w" );
   fprintf( fp, "%s\n", "hi there" );
   fclose ( fp );
   exit(1);
}

And then:

Code:
$ gcc hi.c
$ mv a.out sayHi
$ sudo chown root sayHi
$ sudo chgrp root sayHi
$ sudo chmod u+s sayHi
$ ls -l sayHi
-rwsr-xr-x 1 root root 8256 Nov 29 06:04 sayHi
$ ./sayHi
$ ls -l /hi
-rw-r--r-- 1 root pi 9 Nov 29 06:06 /hi
$ cat /hi
hi there
$

Alles klar.

------ Post updated at 10:23 PM ------

Quote:
Originally Posted by Don Cragun
You still haven't told us what operating system you're using...

Some operating systems NEVER allow shell scripts to be run set-uid.

Some operating systems NEVER allow a running program to be overwritten.

Some operating systems only allow shells specified in a system file to be used to run set-uid scripts.

All of the above are possible reasons why what you're trying might not work on an unspecified operating system.
I wasn't sure what information was relevant. Originally I thought a shell script could be used to experiment but apparently not.
I'm using Bash v4.4 on a Raspberry Pi running Raspbian, a Debian based Linux distribution. And Bash v4.4 on Kali Linux (Stretch), also Debian based.

I'm actually not trying to overwrite a running program. It may have looked like that because originally I tried to write the string "hello" into a file with filename "hello" and the script was also named hello. My apologies for the confusion.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What keeps me from abusing setuid(0) and programs with setuid bit set?

Just learning about the privilege escalation method provided by setuid. Correct me if I am wrong but what it does is change the uid of the current process to whatever uid I set. Right ? So what stops me from writing my own C program and calling setuid(0) within it and gaining root privileges ? ... (2 Replies)
Discussion started by: sreyan32
2 Replies

2. UNIX for Dummies Questions & Answers

Restricting Usage of Setuid Programs to the Admin User In MacOsx

This is a quote from the Apple security configuration (you can download it from Apple) " Using ACLs to Restrict Usage of Setuid Programs The ACL feature of Mac OS X can also be used to restrict the execution of setuid programs. Restricting the execution of setuid programs to administrators... (3 Replies)
Discussion started by: Vera
3 Replies

3. Solaris

Need help with setuid.

Hi Gurus, I need your suggestions,to implement setuid. Here is the situation. I have a user xyz on a solaris zone.He needs to install a package using a pkgadd command but i guess only a root can run that .Is there any way I can set the setuid bit on the pkgadd which is in the location... (6 Replies)
Discussion started by: rama krishna
6 Replies

4. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

5. Solaris

setuid and guid

Hi All, Can someone give me some info about setuid or guid topic? Also about sticky bit. Thanks in advance, itik (9 Replies)
Discussion started by: itik
9 Replies

6. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

7. UNIX for Dummies Questions & Answers

setuid

could u plz give me clear idea of spcial permissions setuid,getuid and striky bit . (1 Reply)
Discussion started by: Prem
1 Replies

8. UNIX for Advanced & Expert Users

cc, setuid, and LD_LIBRARY_PATH

Hi, This question deals with Solaris 2.8 and setuid programs. From research I've done so far, setuid programs ignore LD_LIBRARY_PATH; I've proven this and am OK with it. The thing I am not certain of how the C compiler is supposed to behave when it is invoked via a setuid program. Basically,... (0 Replies)
Discussion started by: WolfBoy
0 Replies

9. UNIX for Dummies Questions & Answers

Using setuid and setgid

Hi, I have been looking at setuid and setgid. I understand that setuid determines who owns the file and setgid determines which group of people can access the file... yeah?! But i need to know how to actually use setuid and setgid. I'm guessing chmod will feature somewhere.. Any help... (1 Reply)
Discussion started by: crispy
1 Replies

10. UNIX for Advanced & Expert Users

setuid

I have a C wrapper programme which basically execute a shell script. The shell script has 700 as permission and oracle is owner of the shell script. The C execuatble has 4711 permission so that means that it has setuid bit set and group and others can execute the C executable. The reason why I am... (2 Replies)
Discussion started by: sanjay92
2 Replies
Login or Register to Ask a Question