Improve company productivity with a Business Account.Sign Up


run a script as root

Posted on 2002-07-11
Medium Priority
Last Modified: 2010-04-21
We would like to call a script from within a C program that runs a bash script as the root user. The script has functions like "init q" and "fuser"  to reset a serial port from one mode (respawn)to another (off). We have the root password available to use, but not sure the best way to run the bash script as root, short of running an expect script to answer the password question.
Question by:medent
LVL 51

Expert Comment

ID: 7148682
simply make your call within the C program (with system() for example), then do follwoing to the compiled and linked binary:
   chmod 4555 your-prog
   chown root your-prog

Author Comment

ID: 7148826
Thank You that helped. The binary appears to run as root, but when it calls to the shell script, the shell script does not run as root, does that make sense?
LVL 51

Expert Comment

ID: 7150139
hmm, thought it works this way with scripts too ...
Try to use seteuid() in your program before calling the script. See also setsid()
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 7151635
The shell at least some times (maybe always) resets the effective uid to the real uid when it starts up.

The point is moot in this case, though, because 'init' bases prvilege on the real uid of the sender of a command, not the effective uid.

So you just need to do setreuid (not seteuid) before doing the system().

You still need to have the setuid flag on the program, though, because otherwise it doesn't have permission to do setreuid().
LVL 51

Expert Comment

ID: 7152089
damn, missed to mention setreuid() too, thanks bryanh

Expert Comment

ID: 7167520

run chmod with s+u and s+g under root. this will run the script as root whom ever are running it. This is a dangerous thing to do. If your scrip gets broken the user will we loged into an console as root, be aware


Expert Comment

ID: 7185425
Compiled binaries run as root, shell scripts don't.
LVL 51

Expert Comment

ID: 7186279
> .. this will run the script as root whom ever are running it.
> Compiled binaries run as root, shell scripts don't.
General answer: NO (to both statements).

In ancient times any program (script, or not) was executed with root permissions if owned by root and SUID root.
Modern kernels prohibit SUID scripts.
You need to check the kernel settings ...

Expert Comment

ID: 7186973
Are you sure about that?  I thought the story was that in ancient times scripts could be setuid, then in recent times they couldn't, and in modern times they can again.  It definitely varies from one Unix to another.

The issue, in case anyone following this is interested, is that on ancient Unix systems, with a setuid script, the kernel would load the interpreter (shell) and set the effective uid, and then start the interpreter and pass to it the name of the program (script).  The interpreter would then interpret the program, with the set effective uid.

The problem with that is that a malicious user might change things immediately after the interpreter starts so that the program name refers to some other non-setuid program.  
(An easy way to do this is to have the program name be a symlink and update the symlink).  Now the interpreter, running with the set effective uid, interprets some arbitrary program.

When the engineers noted that problem, they disabled setuid for an interpreted program (shell script).

But the ultimate solution is better than that:  instead of having the kernel pass the program name to the interpreter, have it keep the file open (it had to open it to find out what interpreter to invoke anyway) and pass the file handle to the interpreter.  Now the user can change names all he wants and the only program that's going to get interpreted is the one that had the setuid flag set.  I thought Linux did that.

Well, I just checked my Linux 2.2 system, and it does in fact fail any attempt to exec an interpreted program with the setuid flag set.
LVL 51

Expert Comment

ID: 7187421
> Are you sure about that?

> It definitely varies from one Unix to another.
I agree.

Just to clear it out a bit:
  I'm not shure about the exact version where which behaviour starts (I'm still collecting this information, let me know if you have details:).

A trick to get rid of SUID programs and scripts which works on most UNIX/Linux is to use the nosuid option in /etc/[v]fstab (or the mount command itself)

Accepted Solution

pjb1008 earned 900 total points
ID: 7251043
Writing setuid scripts is a good way to introduce security problems. Successfully cleaning up the environment (by which I mean timers, signal blocks, strange fd, as well as environment variables) is hard.

The reasonable two approaches are:
1) use a setuid program to clean up the evironment then call the script. This is what sudo does.
2) use a setuid program to pass your credentials, together with a request to run a program, to a daemon that starts the script.

(1) is fairly easy to understand and configure, as is probably part of your Linux distribution.
The advantage of (2) is that is starts the program in a known-clean environment, so you don't have to know about every trick that can be used for privilege escalation. This makes it much easier to run shell scripts that run in a different privilege domain from the calling user.

An implementation of (2) is at:

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question