On the Usefulness of SO_PEERPIDFD
Kernel 6.5 added a few new pidfd functions:
SO_PEERPIDFD. The idea behind them is the same as
SO_PEERCRED respectively. The only difference is that the
PIDFD functions return not a plain, numerical PID but a file descriptor instead.
A plain PID is small number of type
pid_t that is incremented for each new process and wraps over when too many processes have been created. This PID is usually used to look up some information about the process via files in
/proc/$PID. While a process is looking up some information, it is possible that the process that PID initially referred to has terminated and a new process with this PID has been created. The looked up information is now incorrect, possibly resulting in a security vulnerability.
The pidfd on the other hand always refers to one process and can be queried about the state of the process. This allows one to look up information from
/proc/$PID without the race mentioned earlier. The
SO_PEERCRED functionality in particular is interesting because it allows a service to query the pidfd of a connected client.
Or so it seems. For flatpak, wayland compositors and D-Bus services also want to authenticate their clients but they do not rely on this functionality. Instead the preferred approach taken here (implemented in wayland as the security-context protocol, still in discussion for D-Bus as the org.freedesktop.DBus.Containers1 interface) is to create a new wayland or D-Bus socket for each application instance and make sure that those sockets are the only way to connect to the services (specifically the “normal” host sockets must not be made available to the application instances). Flatpak is responsible for creating those sockets, attaching some metadata, and then mounting them. Currently the metadata is a triple: the sandboxing engine (flatpak, snap, …), the application id and the application instance. There are plans to extend this for further metadata.
The question is, why add all of this complexity to authenticate a process when the pidfd approach is so much easier. Turns out that the hard part is knowing what to do with the pid, where to look up the information that you need. For flatpak,
/proc/$PID/root/.flatpak-info is a file that cannot be changed from processes in the sandbox and contains among other things the flatpak
instance-id which can be used to look up more data from
$XDG_RUNTIME_DIR/.flatpak/$instance-id. For snap the whole process is very different and other technologies like firejail have basically no way to do this lookup (don’t take my word on it).
(Aside: xdg-desktop-portal does does look up flatpak information from
/proc/$PID and I just said it doesn’t use
SO_PEERCRED so why is this not broken? Flatpak makes sure there is a process in the sandbox that stays alive the entire time and acts as a proxy between the app and the D-Bus broker to enforce access control. The PID of all connections from inside the sandbox are the PID of the proxy. This is still technically racy but it becomes much harder to pull off an attack. Implementing
SO_PEERCRED to get rid of the race entirely in xdg-desktop-portal would be nice.)
Implementing all kinds of different, subtle mechanisms to look up information from different sandbox engines you might not even know exist doesn’t scale and that makes pidfd with
SO_PEERCRED much less useful than one would expect in a lot of cases.