Best Way Elevate the Privileges Programmatically Under Different Versions of Linux

How to programmatically gain root privileges?

Original answer

You might consider the setuid switch on the executable itself. Wikipedia has an article on it which even shows you the difference between geteuid() and getuid() quite effectively, the former being for finding out who you're "emulating" and the latter for who you "are". The sudo process, for example, geteuid should return 0 (root) and getuid your user's id, however, its sub-processes do truly run as root (you can verify this with sudo id -u -r).

I don't think there's a way to easily programmatically gain root access - after all, applying the principle of least privilege, why would you need to? Common practise is to run only limited parts of code with elevated privileges. A lot of daemons etc are also set up under modern systems to run as their own user with most of the privileges they need. It's only for very specific operations (mounting etc) that root privileges are truly needed.

2013 update

My original answer stands (although my 2013 self might make a better job of it than my 2010 one), but if you are designing an application that requires root access, you may want to consider exactly what sort of root access is needed and consider the use of POSIX Capabilities (man page). These are different to capability-based security as implemented in L4 et al. POSIX capabilities allow your application to be granted a subset of root's powers. For example CAP_SYS_MODULE will allow you to insert kernel modules, but give you no other root powers. This is in use in distributions e.g. Fedora has a feature to completely remove setuid binaries with indiscriminate root access.

This matters because as a programmer, your code is obviously perfect! But, the libraries on which you depend (sigh, if only you'd written them!) might have vulnerabilities in them. Using capabilities, you can limit the use of this exploit, and save yourself and your company from security-related scrutiny. This makes everyone happier.

Qt: Make program ask for user elevation at execution(windows)

Security models are rather platform-specific. Qt does not AFAIK address this sort of thing. (Case in point: the Qt Creator installer itself choked when I didn't run it as root.)

You'll presumably need to make native calls or interact with some daemon designed for the purpose. Often easiest to try whatever it is you think you should be able to do, and check for failure, and if you can't do what you want then ask the user to explicitly re-run with higher privileges.

Linux:

Best way elevate the privileges programmatically under different versions of Linux?

Windows:

http://msdn.microsoft.com/en-us/magazine/cc163486.aspx

Mac:

Escalate App Privileges Programmatically OS X

(Note: Often if you have to ask a question about something like this, there may be a better way to do whatever it is you're trying to do. Consider posting a question that is more general about what you want, and you might get suggestions on a way to do it more cleanly.)

Programmatically requesting elevated rights in Linux

I would write a separate program altogether. Something along the lines of this philosophy. Basically - write a simple program that does exactly what you need, and control its behaviour with file permissions on the filesystem. Mainly,

Do as little as possible in setuid programs.

A setuid program must operate in a
very dangerous environment: a user is
under complete control of its fds,
args, environ, cwd, tty, rlimits,
timers, signals, and more. Even worse,
the list of controlled items varies
from one vendor's UNIX to the next, so
it is very difficult to write portable
code that cleans up everything.

Of the twenty most recent sendmail
security holes, eleven worked only
because the entire sendmail system is
setuid.

Only one qmail program is setuid:
qmail-queue. Its only purpose is to
add a new mail message to the outgoing
queue.

And,

Do as little as possible as root.

The entire sendmail system runs as
root, so there's no way that its
mistakes can be caught by the
operating system's built-in
protections. In contrast, only two
qmail programs, qmail-start and
qmail-lspawn, run as root.

Elevating process privilege programmatically?

You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:

startInfo.Verb = "runas";

This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.

This does mean the UAC prompt will come up and will need to be acknowledged by the user: if this is undesirable (for example because it would happen in the middle of a lengthy process), you'll need to run your entire host process with elevated permissions by Create and Embed an Application Manifest (UAC) to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.

Edit: I see you just edited your question to state that "runas" didn't work for you. That's really strange, as it should (and does for me in several production apps). Requiring the parent process to run with elevated rights by embedding the manifest should definitely work, though.

QT application rights for sudo command

Just searching around for "elevate privileges" in linux, this is what I found.

Best way elevate the privileges programmatically under different versions of Linux?

With an answer that pointed me here:

http://en.wikipedia.org/wiki/PolicyKit

http://www.freedesktop.org/wiki/Software/polkit/

Hope that helps.

Where do you put global application data on Linux (Mint 13 specifically)?

What is normally done is the application is installed using the system's packaging tools. But you could write your own installer script as well. During the installation process, run as root, a new group may be created, and a new directory under /var/lib is created owned by that group. For example, the locate command does this:

$ ls -l /usr/bin/locate
-rwx--s--x 1 root locate 42032 Nov 17 19:33 /usr/bin/locate

And it's data dir:

drwxr-x--- 2 root locate 4096 Nov 15  2010 /var/lib/slocate

The locate command uses setgid feature to set its own process group ID to locate, that gives it permission to read the /var/lib/slocate subdirectory.

In your case you (your installer) would set write permissions as well so the app can write data there on the users behalf.



Related Topics



Leave a reply



Submit