views:

97

answers:

3

I have a web project for which I need to run a command when a specific URL is requested, but that command requires root privileges.

The project is served with a Python process (Django), of course running it with root privileges is not an option.

The command's parameters are hardcoded making it impossible to inject anything and it's a right protected application so I can be slightly more liberal on security since the users who will have access to it will be trustworthy (hopefully). However ideally I would like to do it securely.

.

+4  A: 

Use setuid: http://en.wikipedia.org/wiki/Setuid

"setuid and setgid (short for set user ID upon execution and set group ID upon execution, respectively) are Unix access rights flags that allow users to run an executable with the permissions of the executable's owner or group."

...And be very, very careful!

RichieHindle
Yeah, don't shoot yourself in the foot. What you are asking for screams for being exploited ;-)
lothar
+5  A: 

Call out to the command via sudo with the NOPASSWD: option, that allows you fine grained access control and gives you auditing in syslog for free. Avoid using a shell and use an exec variant that takes the parameters directly as array.

David Schmitt
+1  A: 

By all means avoid using setuid and setgid, you want to keep the HTTP server with as low permissions as possible. For the process that requires root privileges, use the http server to create a whole new separate process and invoke sudo. You should however not include the HTTP server uid or gid in the sudoers, but some other user that is the only one that can access the program, and that program is the only one that can run as root. The HTTP SERVER then starts a new process, it changing the UID to the dumb user and then executes the process with sudo as root.

daniel
If my understanding is correct, the proper way is not setting gid/uid on the HTTP server itself, but on a cloaked copy of the binary you want to run.For example I want to run the command "svnserve" by the user www-data. I can copy the "svnserve" binary in the home folder of www-data and setuid on *this* binary. This way the only user who can run "svnserve" without root privileges is www-data. And this user can't run any other executable that requires root privileges.
h3
Not quite, you create a user called svnserver-runner, and add svnserver-runner to the sudoers list which can run svnserve as root.Then the http server creaters a new thread, an changes the UID of the thread to svnserver-runner, and that thread then execute svnserve using SUDO, to get it to run as root.
daniel