268 points · by todsacerdoti · 4y ago · 166 comments
> however, we note that OpenBSD is not exploitable, because its kernel refuses to execve() a program if argc is 0.
argv[0] always exists in normal means, it is either physical path of the executable or soft link position. Without accidents like run a program from code and forget about passing argv. That couldn't happen at all. And in fact, most cross platform program or shell even just assume argv[0] is the same as executable path (and don't give you a option to change it, because it is not possible on all platforms).
Seems like a good use case for Android style API levels, but only if used very sparingly and with old ones not removed.
Beautiful indeed.
Some weeks ago I wanted to run a game; it showed an error when starting that it failed to set real-time priority for the audio thread, resulting in actual audio stuttering.
ALAudio asks rtkit, rtkit asks polkit, and that somehow fails. Why? Who knows. To even figure this out I had to recompile rtkit with some strategic printf() calls in there, because it just doesn't show at all why it failed. Same for polkit.
I never did find out what the problem was. I just gave up after a while.
It's stuff like this why I dislike all these tools: it all works when it works, but when it fails it really makes your life hard. Here, too, it just exits with 126, no message, and good luck to you as developer or user if something goes wrong.
Call me a radical traditionalist unix greybeard, but I really think tools should tell users what goes wrong, even (or perhaps especially) when they're "internal" tools like this, because this it's just really hard to figure out what the problem is. "fprintf(stderr, "too few arguments\n")" is not very hard, it seems to me shrug.
[1]: https://gitlab.freedesktop.org/polkit/polkit/-/commit/a2bf5c...
Fault them for not changing the loop, which has clearly busted logic, and adding a guard clause instead. But you can't fault them for not providing a user friendly error to shellcoders. The condition here isn't simply "too few arguments".
Either way: when in doubt, best to print a message! You're right this specifically probably isn't the biggest issue, but in general it seems to be design with "this should never happen, just exit/deny" in mind, and turns out that in some odd cases those things do happen.
What is missing in all these complaint threads (and this isn't aimed at you specifically) is the complainers' proposed alternatives. It's very easy to write a glib comment about "sudo already does this" "groups already do this" which indicates only that the person writing the comment doesn't understand the problem that PolKit is trying to solve.
What I lament is not so much that things have changed, but that things have changed in ways I no longer understand or can debug. Back in the Old Days™ you could more or less understand your entire system from a few very basic principles. If something didn't work, you could go in there and usually figure it out on your own.
Not that I have rose-coloured glasses about the Old Days™. I remember having to run that horrible "xfree86 -configure" thing when I was starting out with this whole Linux thing. Clearly, things are better now because the last time I had to look at "configuring X" is so long ago I don't even remember how to do it (or even what that filename was; /etc/x.conf? Or /etc/x11.conf? Not sure).
I don't think it has to be like this. Just because "su" and "sudo" aren't sufficient and the concepts in e.g. Polkit are good doesn't mean the implementation of these concepts also is.
I won't say PolKit's implementation is the best ever. But it exists, and solves real problems, enough so that applications choose to use it and distros choose to ship it.
One thing I can say in PolKit's favour is that it's well documented--which is important for such a critical component.
strace
Nowadays I just reach for it immediately. Powerful, total/complete (since syscalls are everything). The typical unix tools all depend on these hard to discover environment things. Other processes, env vars, files here or there, sockets, dbus, signals...
It's a way to let user processes communicate with privileged processes while potentially, action dependent also prompting the user for a password.
pkexec on the other hand is separate program bundled with polkit which is kinda which let's you combine polkit with running predefined commands, or easily accidentally any command and IMHO should not exist.
there are various programs which work with polkit (even if they don't necessary require it) including:
- systemd, logind, NetworkManager, udisks, flatpack, fwupd, timedate(setting), and others
It also allows rather flexible configurations about what users can do which actions under which conditions, which can be quite useful for a multi-user server (e.g. GPU server).
While I think a program like polkit is a must have for a modern Linux OS I'm not a fan of it's design.
- pkexec is a bad idea for many more reasons then this vulnerability
- the way it's configured and how it handles caching is prone to accidentally make mistakes with security consequences
- pkexec should not exist, even without the vulnerability it's really easy to mess up using it
- having a embedded language for handling permission scripting, and not inventing your own language or pseudo-language config format is a good. But js? I guess there are simple embedded js interpreters.(1)
(1): Note that it only runs small snippets, so no need to have a JS interpreter with modern features like classes, or an JIT ;=)
In fact a duktape backend has just landed:
https://gitlab.freedesktop.org/polkit/polkit/-/merge_request...
Speaking of security though, I'd trust SpiderMonkey much more because it's battle-tested in the most hostile environment imaginable (millions of browser instances loading completely untrusted code 24/7). But in this context we don't execute untrusted code so whatever :)
> pkexec should not exist
Hmm. For things that do want "just a graphical sudo" for whatever reason, it is kinda the perfect answer. Reusing the per-desktop authorization UI that is used for more fine-grained actions for a sudo-like tool is the correct thing to do. No one wants to have more redundant authorization mechanisms and UIs…
(I would have to read through the polkit doc again, but there was some massive trap around how polkit configs work and how pkexec _might_ be used, I think related to the "permission caching feature").
Enough OSes are behind and everyone using spidermonkey is unable to keep up with the porting requirements in the short window of ESR overlap so you can read vulnerabilities for the latest ESR and apply them to everyone. It would be nice if duktape had complete sandboxing, etc, but it has the advantage that hackers have to do all their own research to mount an attack.
We already have sudo and su (and doas) for such things. But those programs need to reinvent the wheel.
For instance, GNOME Settings uses PolKit to ensure that remote control can only be turned on by an admin who makes the request from an active session on a local console.
Or here's a more interesting example. gvfs uses PolKit to allow processes that are part of an active session on a local console to be able to elevate permissions for editing files (e.g., 'gedit admin:///etc/motd').
- systemd, logind, NetworkManager, udisks, flatpack, fwupd, timedate(setting)
you have a good start on the list of common programs that I distrust the most. Plus polkit itself, I guess.
Back then it would segfault randomly for no apparent reason, idle freshly installed fairly minimalist headless fileservers. We immediately blacklisted it from our systems, but were lucky to not actually need anything it brought to the table.
IIRC for quite some time there wasn't even a maintainer, but I think that was pre-polkit rename. Hopefully the situation has improved.
Sounds like they made the right choice. Why would other kernels allow this?
NB, this was partially fixed in gnulib in 2009:
http://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=b06da86eb05ed57e2861061ae5cacf4c7a3686f1
But glib has its own copy of this code, which hasn't been updated since 2008.
Embedded code copies are terrible, people really need to start removing them, or at least learning how to update them.I don't find it on any of the servers I manage, it appears to be installed with graphical desktop only?
In these desktop environments. Polkit is used to gating any remote process from accessing network state related functions (or anything that are not supposed to do remotely).
For example, you can use `nmcli` on the desktop or system termional(Alt+f1~f7) to disconnect the network or change to use other pppoe even you are not currently root(not using sudo to elevate the permission).
But if you try to call `nmcli` through ssh? No, No , No, Polkit will gate you from doing so.
Groups are also not dynamic enough. When evaluating these rules, one of the inputs is whether the client requesting the action is a member of no session at all, or an inactive console session, or an active console session. You can't represent these with groups because you can't grant/revoke group membership to a process while it's running. (A process with a group membership can also stash away a setgid executable that can be used by the user to regain access to a group that they've been removed from later on, so it's not even possible to cast-iron-guarantee that access to a group has been revoked without inspecting the filesystem...)
* rtkit - optional dependency of PulseAudio to grant its processes realtime priority.
* colord - optional color profile dbus service.
* gparted - hard dependency to give it permission to reformat disks.
I like to keep a minimal system, so I went ahead and removed them all. Will see if audio performance suffers.
I wouldn't suggest giving those permissions to your user because it opens up the possibility of a denial of service. With those permissions, any program running as your user can spawn lots of realtime threads that can take over the scheduler and lock the system up. This was detailed in the rtkit announcement, and preventing it is the reason rtkit exists: http://lalists.stanford.edu/lad/2009/06/0191.html
Maybe realtime audio is not important to you, and that's fine. But it's never as simple as "delete these things and now I have a secure system", you may be trading off security elsewhere to get that. Please also note that pkexec is not required to use polkit. You can remove pkexec and still have a functioning polkit installation and still use all those other daemons too. For a really secure system you may want to remove all suid binaries anyway and only use polkit or SELinux or something.
/s
A GUI sudo if you will, with XML and javascript code for its configuration files.
I'm not near my computer, but I would guess pipewire (as it usually runs within the users session) might rely on it to access the sound hardware without needing to run as root. But just guessing.
[~] compile helper..
[~] maybe get shell now?
The value for environment variable XAUTHORITY contains suscipious content
This incident has been reported.
And no root shellOn Arch Linux.
Also by Henry Spencer, from How to Steal Code³:
“Even widely-available library functions often are not used nearly as much as they should be. A conspicuous example is getopt, for command-line argument parsing. Getopt supplies only quite modest help in parsing the command line, but the standardization and consistency that its use produces is still quite valuable; there are far too many pointless variations in command syntax in the hand-cooked argument parsers in most UNIX programs. Public-domain implementations of getopt have been available for years, and AT&T has published (!) the source for the System V implementation. Yet people continue to write their own argument parsers.”
Now, of course, we have argp_parse(), which also comes included in glibc, and is much better than getopt().
1. https://www.gnu.org/software/libc/manual/html_mono/libc.html...
I would also note that even POSIX isn't portable in some contexts (such as trying to write code which can run on windows too).
> a scheme of having a privileged service that acts on the requests of unprivileged processes
That is precisely what polkit is!
Edit: Maybe to make it clear, yes, in theory the polkit project does follow the scheme of a privileged daemon process and unprivileged clients but in practice it doesn't because of the setuid polkit-agent-helper-1 and setuid pkexec programs.
You cannot accomplish sudo without setuid. Note that sudo means gaining privilege in the current context, with all its environment (including inherited file descriptors, the process group, etc.), not doing a privileged action at a distance.
> also true for the /usr/libexec/polkit-agent-helper-1 helper it uses which is a setuid binary but doesn't really have to be
Try making it non-setuid and see what happens.
(It will fail to check your password because most mechanisms for doing so, like the usual pam_unix, are only accessible to root.)
I think it’s akin to real-life locks. Keeps good guys out, but not a determined attacker.
$ sudo chmod 0755 /usr/bin/pkexec
chmod: changing permissions of '/usr/bin/pkexec': Read-only file system
Security preventing security I guess... sudo mount --bind /bin/false /usr/bin/pkexec
This will be gone over reboot, however.If argc is 0, don't look at argv.
Sanity check argv != NULL before looking at it.
Or, maybe write security-critical programs in safer languages like Rust.
People out there write some pretty funky C.
If you're writing a serious kernel in C, you ought to have programmatic debugging assertion constraints for every parameter.
I don't know if this was flagged or not.
I use Linux but do not use a graphics system (X11, Wayland, etc.). When I install, I always start by downloading a rootfs or a bootable image containing a rootfs.
For example, if installing Debian, I might start by downloading boot.img.gz
https://deb.debian.org/debian/dists/bullseye/main/installer-...
However I do not see pkexec "installed by default" in initrd or initrdg.
gzip -d boot.img.gz
mount -o loop boot.img /mnt/img
gzip -d /mnt/img/initrd.gz
cpio -t < /mnt/img/initrd|grep pkexec
gzip -dc /mnt/img/initrdg.gz|cpio -t|grep pkexec
It appears that to install pkexec I would have to choose it at a later stage.I guess the authors are referring to some other installation method. Perhaps one that denies the user the chance to skip installing a graphics system.
You are describing the thing that is loaded and run to load the Debian installation data and install Debian.
One could argue the thing you are running is not "Debian", but Debian's installer tool.
Perhaps it would come down to what exactly in the userland defines Debian as Debian. There are certainly differences in the rootfs as I go from one distribution to another.
It would be like arguing that NetBSD install media is not NetBSD. For me, it is absolutely NetBSD, just a smaller version.
dpkg-statoverride --update --add root root 0755 /usr/bin/pkexecThis architecture was a bad idea when Windows adopted it. Then it got transplanted to Linux and got even worse.
Looked at nosh?