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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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()
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.


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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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