mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2026-05-22 10:37:35 +03:00
cred: properly pass and test creds on other threads (#17273)
Various admin operations will be invoked by some userspace task, but the work will be done on a separate kernel thread at a later time. Snapshots are an example, which are triggered through zfs_ioc_snapshot() -> dsl_dataset_snapshot(), but the actual work is from a task dispatched to dp_sync_taskq. Many such tasks end up in dsl_enforce_ds_ss_limits(), where various limits and permissions are enforced. Among other things, it is necessary to ensure that the invoking task (that is, the user) has permission to do things. We can't simply check if the running task has permission; it is a privileged kernel thread, which can do anything. However, in the general case it's not safe to simply query the task for its permissions at the check time, as the task may not exist any more, or its permissions may have changed since it was first invoked. So instead, we capture the permissions by saving CRED() in the user task, and then using it for the check through the secpolicy_* functions. The current code calls CRED() to get the credential, which gets a pointer to the cred_t inside the current task and passes it to the worker task. However, it doesn't take a reference to the cred_t, and so expects that it won't change, and that the task continues to exist. In practice that is always the case, because we don't let the calling task return from the kernel until the work is done. For Linux, we also take a reference to the current task, because the Linux credential APIs for the most part do not check an arbitrary credential, but rather, query what a task can do. See secpolicy_zfs_proc(). Again, we don't take a reference on the task, just a pointer to it. We change to calling crhold() on the task credential, and crfree() when we're done with it. This ensures it stays alive and unchanged for the duration of the call. On the Linux side, we change the main policy checking function priv_policy_ns() to use override_creds()/revert_creds() if necessary to make the provided credential active in the current task, allowing the standard task-permission APIs to do the needed check. Since the task pointer is no longer required, this lets us entirely remove secpolicy_zfs_proc() and the need to carry a task pointer around as well. Sponsored-by: https://despairlabs.com/sponsor/ Signed-off-by: Rob Norris <robn@despairlabs.com> Reviewed-by: Pavel Snajdr <snajpa@snajpa.net> Reviewed-by: Alexander Motin <mav@FreeBSD.org> Reviewed-by: Kyle Evans <kevans@FreeBSD.org> Reviewed-by: Tony Hutter <hutter2@llnl.gov>
This commit is contained in:
committed by
Brian Behlendorf
parent
0a73e91d2c
commit
4ee13684b9
@@ -23,6 +23,7 @@
|
||||
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright 2013, Joyent, Inc. All rights reserved.
|
||||
* Copyright (C) 2016 Lawrence Livermore National Security, LLC.
|
||||
* Copyright (c) 2025, Rob Norris <robn@despairlabs.com>
|
||||
*
|
||||
* For Linux the vast majority of this enforcement is already handled via
|
||||
* the standard Linux VFS permission checks. However certain administrative
|
||||
@@ -34,28 +35,32 @@
|
||||
#include <linux/security.h>
|
||||
#include <linux/vfs_compat.h>
|
||||
|
||||
/*
|
||||
* The passed credentials cannot be directly verified because Linux only
|
||||
* provides and interface to check the *current* process credentials. In
|
||||
* order to handle this the capable() test is only run when the passed
|
||||
* credentials match the current process credentials or the kcred. In
|
||||
* all other cases this function must fail and return the passed err.
|
||||
*/
|
||||
static int
|
||||
priv_policy_ns(const cred_t *cr, int capability, int err,
|
||||
struct user_namespace *ns)
|
||||
{
|
||||
if (cr != CRED() && (cr != kcred))
|
||||
return (err);
|
||||
/*
|
||||
* The passed credentials cannot be directly verified because Linux
|
||||
* only provides an interface to check the *current* process
|
||||
* credentials. In order to handle this we check if the passed in
|
||||
* creds match the current process credentials or the kcred. If not,
|
||||
* we swap the passed credentials into the current task, perform the
|
||||
* check, and then revert it before returning.
|
||||
*/
|
||||
const cred_t *old =
|
||||
(cr != CRED() && cr != kcred) ? override_creds(cr) : NULL;
|
||||
|
||||
#if defined(CONFIG_USER_NS)
|
||||
if (!(ns ? ns_capable(ns, capability) : capable(capability)))
|
||||
if (ns ? ns_capable(ns, capability) : capable(capability))
|
||||
#else
|
||||
if (!capable(capability))
|
||||
if (capable(capability))
|
||||
#endif
|
||||
return (err);
|
||||
err = 0;
|
||||
|
||||
return (0);
|
||||
if (old)
|
||||
revert_creds(old);
|
||||
|
||||
return (err);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -248,19 +253,6 @@ secpolicy_zfs(const cred_t *cr)
|
||||
return (priv_policy(cr, CAP_SYS_ADMIN, EACCES));
|
||||
}
|
||||
|
||||
/*
|
||||
* Equivalent to secpolicy_zfs(), but works even if the cred_t is not that of
|
||||
* the current process. Takes both cred_t and proc_t so that this can work
|
||||
* easily on all platforms.
|
||||
*/
|
||||
int
|
||||
secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
|
||||
{
|
||||
if (!has_capability(proc, CAP_SYS_ADMIN))
|
||||
return (EACCES);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user