On Wed, Apr 24, 2024 at 3:57 AM stsp <stsp2@xxxxxxxxx> wrote: > > 23.04.2024 19:44, Andy Lutomirski пишет: > >> On Apr 23, 2024, at 4:02 AM, Stas Sergeev <stsp2@xxxxxxxxx> wrote: > >> > >> This patch-set implements the OA2_INHERIT_CRED flag for openat2() syscall. > >> It is needed to perform an open operation with the creds that were in > >> effect when the dir_fd was opened. This allows the process to pre-open > >> some dirs and switch eUID (and other UIDs/GIDs) to the less-privileged > >> user, while still retaining the possibility to open/create files within > >> the pre-opened directory set. > > I like the concept, as it’s a sort of move toward a capability system. But I think that making a dirfd into this sort of capability would need to be much more explicit. Right now, any program could do this entirely by accident, and applying OA2_INHERIT_CRED to an fd fished out of /proc seems hazardous. > > While I still don't quite understand > the threat of /proc symlinks, I posted > v4 which disallows them. > I like that, but you're blocking it the wrong way. My concern is that someone does dfd = open("/proc/PID/fd/3") and then openat(dfd, ..., OA2_INHERIT_CRED); IIRC open("/proc/PID/fd/3") is extremely magical and returns the _same open file description_ (struct file) as PID's fd 3. > > So perhaps if an open file description for a directory could have something like FMODE_CRED, and if OA2_INHERIT_CRED also blocked .., magic links, symlinks to anywhere above the dirfd (or maybe all symlinks) and absolute path lookups, then this would be okay. > > So I think this all is now done. But you missed the FMODE_CRED part! So here's the problem: right now, in current Linux, a dirfd pointing to a directory that you can open anyway doesn't convey any new powers. So, if I'm a regular program, and I do open("/etc", O_PATH), I get an fd. And if I get an fd pointing at /etc from somewhere else, I get the same thing (possibly with different f_cred, but f_cred is largely a hack to restrict things that would otherwise be insecure because they were designed a bit wrong from the beginning). But, with your patch, these fds suddenly convey a very strong privilege: that of their f_cred *over the entire subtree to which they refer*. And you can attack it using exactly your intended use case: if any program opens a dirfd and then drops privileges, well, oops, it didn't actually fully drop privilege. So I think that, if this whole concept has any chance of working well, it needs to be opt-in *at the time of the original open*. So a privilege-carrying open would be an entirely new option like O_CAPTURE_CREDS or FMODE_CREDS. And OA2_INHERIT_CREDS is rejected if the dirfd doesn't have that special mode.