Re: [PATCH 2/2] openat2: add OA2_INHERIT_CRED flag

From: Al Viro
Date: Wed Apr 24 2024 - 22:31:46 EST


On Wed, Apr 24, 2024 at 01:52:48PM +0300, Stas Sergeev wrote:
> @@ -3793,8 +3828,23 @@ static struct file *path_openat(struct nameidata *nd,
> error = do_o_path(nd, flags, file);
> } else {
> const char *s = path_init(nd, flags);
> - file = alloc_empty_file(open_flags, current_cred());
> - error = PTR_ERR_OR_ZERO(file);
> + const struct cred *old_cred = NULL;
> +
> + error = 0;
> + if (open_flags & OA2_INHERIT_CRED) {
> + /* Only work with O_CLOEXEC dirs. */
> + if (!get_close_on_exec(nd->dfd))
> + error = -EPERM;
> +
> + if (!error)
> + old_cred = openat2_override_creds(nd);
> + }
> + if (!error) {
> + file = alloc_empty_file(open_flags, current_cred());

Consider the following, currently absolutely harmless situation:
* process is owned by luser:students.
* descriptor 69 refers to root-opened root directory (O_RDONLY)
What's the expected result of
fcntl(69, F_SEFTD, O_CLOEXEC);
opening "etc/shadow" with dirfd equal to 69 and your flag given
subsequent read() from the resulting descriptor?

At which point will the kernel say "go fuck yourself, I'm not letting you
read that file", provided that attacker passes that new flag of yours?

As a bonus question, how about opening it for _write_, seeing that this
is an obvious instant roothole?

Again, currently the setup that has a root-opened directory in descriptor
table of a non-root process is safe.

Incidentally, suppose you have the same process run with stdin opened
(r/o) by root. F_SETFD it to O_CLOEXEC, then use your open with
dirfd being 0, pathname - "" and flags - O_RDWR.

AFAICS, without an explicit opt-in by the original opener it's
a non-starter, and TBH I doubt that even with such opt-in (FMODE_CRED,
whatever) it would be a good idea - it gives too much.

NAKed-by: Al Viro <viro@xxxxxxxxxxxxxxxxxx>