PKEXEC(1)

โœจ pkexec(1) โ€” the manual, but make it cute โœจ

okay bestie, so this is the pkexec page from the pkexec manual, and I'm gonna walk you through it like we're texting at 2am ๐Ÿ’–


๐ŸŽ€ NAME ๐ŸŽ€

pkexec โ€” run a command as a whole other user, no cap ๐Ÿ‘‘


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

pkexec [--version] [--disable-internal-agent] [--help]

pkexec [--keep-cwd] [--user username] PROGRAM [ARGUMENTS...]

๐Ÿ’… DESCRIPTION ๐Ÿ’…

pkexec lets an authorized user run PROGRAM as somebody else. Literally the whole plot. โœจ

If you don't say which PROGRAM, it just runs the default shell. Surprise! ๐Ÿš

And if you don't say which username, then it runs the thing as the administrative super user, root. That's the big one, bestie. That's main character energy fr fr.


๐Ÿ’€ RETURN VALUE ๐Ÿ’€

If everything goes great, the return value is just the return value of PROGRAM. Simple. Iconic. ๐Ÿซถ

But if the calling process isn't authorized, or an authorization couldn't be obtained through authentication, or literally any error happens โ€” pkexec exits with a return value of 127.

And if the authorization couldn't be obtained because the user dismissed the authentication dialog (they hit cancel, they closed it, they said no thank you ๐Ÿ™ˆ), then pkexec exits with a return value of 126.


๐ŸŒธ AUTHENTICATION AGENT ๐ŸŒธ

pkexec, like literally every other polkit application, will use the authentication agent that's registered for the calling process or session. That's just how it do. ๐Ÿ’ซ

BUT โ€” if there's no authentication agent available, then pkexec registers its own textual authentication agent. Cute, self-sufficient, independent. She doesn't need anyone. ๐Ÿ’โ€โ™€๏ธ

You can turn that behavior off by passing the --disable-internal-agent option, if that's more your vibe.


๐Ÿ”ฅ SECURITY NOTES ๐Ÿ”ฅ

okay this is the part where we get serious for a sec. pay attention bestie, this is important.

Running a program as another user is a privileged operation. By default, the action to check for (see the section called "ACTION AND AUTHORIZATIONS") requires administrator authentication. And on top of that, the authentication dialog shown to the user will display the full path to the program that's about to be executed, so the user actually knows what's gonna happen. No sneaky business. ๐Ÿ‘€

The environment that PROGRAM runs in gets set to a minimal known and safe environment, specifically so nobody can inject code through LD_LIBRARY_PATH or similar mechanisms. Sketched behavior = blocked. ๐Ÿ’…

Also, the PKEXEC_UID environment variable gets set to the user id of the process invoking pkexec. ๐Ÿ“

As a result of all that, pkexec will not by default let you run X11 applications as another user, because the $DISPLAY and $XAUTHORITY environment variables are not set. Sorry to the GUI girlies. ๐Ÿ˜ญ

Those two variables will be retained if the org.freedesktop.policykit.exec.allow_gui annotation on an action is set to a nonempty value. But that is discouraged, though, and should only be used for legacy programs. You've been warned. โš ๏ธ

pkexec will run PROGRAM in username's home directory, unless --keep-cwd is used to override this behavior. ๐Ÿ 

Now โ€” note that pkexec does no validation of the ARGUMENTS passed to PROGRAM. In the normal case (where administrator authentication is required every time pkexec is used), this is not a problem, since if the user is an administrator he might as well just run pkexec bash to get root. Like, the horse has left the barn at that point, bestie. ๐Ÿด

However, if an action is used for which the user can retain authorization (or if the user is implicitly authorized), this could be a security hole. Therefore, as a rule of thumb, programs for which the default required authorization is changed should never implicitly trust user input (e.g. like any other well-written suid program). Periodt. ๐ŸŽฏ


๐Ÿ‘‘ ACTION AND AUTHORIZATIONS ๐Ÿ‘‘

By default, the org.freedesktop.policykit.exec action is used. ๐Ÿ“Œ

To use another action, use the org.freedesktop.policykit.exec.path annotation on an action, with the value set to the full path of the program. In addition to specifying the program, the authentication message, description, icon and defaults can be specified. โœจ

If the org.freedesktop.policykit.exec.argv1 annotation is present, the action will only be picked if the first argument to the program matches the value of the annotation. Matching game: won. ๐Ÿ†

Note that authentication messages may reference variables (see the section called "VARIABLES"), for example $(user) will be expanded to the value of the user variable. Cute little template magic. ๐Ÿช„


๐Ÿงš WRAPPER USAGE ๐Ÿงš

So you don't have to go modify existing software to prefix their command-line invocations with pkexec, it's possible to use pkexec in a she-bang wrapper[1] like this:

#!/usr/bin/pkexec /usr/bin/python

import os
import sys

print "Hello, I'm running as uid %d"%(os.getuid())

for n in range(len(sys.argv)):
    print "arg[%d]=`%s'"%(n, sys.argv[n])

If this script is installed into /usr/bin/my-pk-test, then the following annotations

[...]
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/python</annotate>
<annotate key="org.freedesktop.policykit.exec.argv1">/usr/bin/my-pk-test</annotate>
[...]

can be used to select the appropriate polkit action. ๐ŸŽฏ

Be careful to get the latter annotation right, otherwise it will match any pkexec invocation of /usr/bin/python scripts. ๐Ÿ˜ฌ

๐ŸŒˆ VARIABLES ๐ŸŒˆ

The following variables are set by pkexec. They can be used in authorization rules and messages shown in authentication dialogs:

  • program โ€” Fully qualified path to the program to be executed. Example: /bin/cat ๐Ÿฑ
  • command_line โ€” The requested command-line (do not use this for any security checks, it is not secure). Example: cat /srv/xyz/foobar
  • user โ€” The user name of the user to execute the program as. Example: davidz ๐Ÿ’œ
  • user.gecos โ€” The full name of the user to execute the program as. Example: David Zeuthen
  • user.display โ€” A representation of the user to execute the program as that is suitable for display in an authentication dialog. Is typically set to a combination of the user name and the full name. Example: David Zeuthen (davidz) โœจ

๐Ÿ’– AUTHOR ๐Ÿ’–

Written by David Zeuthen <davidz@redhat.com> with a lot of help from many others. ๐Ÿซถ community effort, we love to see it.


๐Ÿ™ˆ BUGS ๐Ÿ™ˆ

Please send bug reports to either the distribution or the polkit-devel mailing list, see https://github.com/polkit-org/polkit#bugs-and-development. They want to hear from you, bestie, don't be shy. ๐Ÿ“ฌ


โญ SEE ALSO โญ

polkit(8), polkitd(8), pkaction(1), pkcheck(1), pkttyagent(1)


๐Ÿ“ NOTES ๐Ÿ“

  1. she-bang wrapper โ€” http://en.wikipedia.org/wiki/Shebang_(Unix)

and that's the whole page bestie!! ๐ŸŽ€โœจ This one came out of the polkit project, from way back in May 2009 โ€” old but iconic, an absolute classic, we stan a legacy utility. mwah ๐Ÿ’‹ go forth and run things as root responsibly fr fr ๐Ÿซถ