Solved

run a script as root

Posted on 2002-07-11
11
269 Views
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.
0
Comment
Question by:medent
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
0
 

Author Comment

by:medent
Comment Utility
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?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
hmm, thought it works this way with scripts too ...
Try to use seteuid() in your program before calling the script. See also setsid()
0
 
LVL 5

Expert Comment

by:bryanh
Comment Utility
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().
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
damn, missed to mention setreuid() too, thanks bryanh
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Expert Comment

by:RonaldMundell
Comment Utility
Hi

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

ROnald
0
 
LVL 3

Expert Comment

by:DVB
Comment Utility
Compiled binaries run as root, shell scripts don't.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> .. 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 ...
0
 
LVL 5

Expert Comment

by:bryanh
Comment Utility
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
bryanh,
> Are you sure about that?
YES.

> 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)
0
 
LVL 3

Accepted Solution

by:
pjb1008 earned 300 total points
Comment Utility
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:
http://www.chiark.greenend.org.uk/~ian/userv/
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now