mirror_zfs/module/os/linux/zfs
Rob Norris 4ee13684b9 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>
2025-06-02 16:49:51 -07:00
..
abd_os.c abd_os: split userspace and Linux kernel code 2024-11-15 10:15:00 -08:00
arc_os.c arc_os: split userspace and Linux kernel code 2024-11-15 10:15:00 -08:00
mmp_os.c Cleanup: 64-bit kernel module parameters should use fixed width types 2022-10-13 10:03:29 -07:00
policy.c cred: properly pass and test creds on other threads (#17273) 2025-06-02 16:49:51 -07:00
qat_compress.c Intel QAT 1.7 compatibility 2023-09-07 16:10:52 -07:00
qat_crypt.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
qat.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
spa_misc_os.c Cleanup: 64-bit kernel module parameters should use fixed width types 2022-10-13 10:03:29 -07:00
trace.c Replace dead opensolaris.org license link 2022-07-11 14:16:13 -07:00
vdev_disk.c [zfs-2.2.8] Revert "vdev_disk: default to classic submission for 2.2.x" 2025-05-29 15:29:27 -07:00
vdev_file.c Use <fcntl.h> instead of <sys/fcntl.h> 2025-05-27 14:56:58 -07:00
zfs_acl.c Fix an uninitialized data access (#16511) 2024-11-05 15:43:52 -08:00
zfs_ctldir.c config: remove HAVE_FILEMAP_RANGE_HAS_PAGE 2024-11-15 10:14:59 -08:00
zfs_debug.c zfs_debug: specific variant for userspace 2024-11-15 10:15:01 -08:00
zfs_dir.c Linux 6.3 compat: idmapped mount API changes 2023-04-10 14:15:36 -07:00
zfs_file_os.c zfs_file: rename zfs_file_fallocate to zfs_file_deallocate 2024-11-15 10:15:00 -08:00
zfs_ioctl_os.c Linux 6.3 compat: idmapped mount API changes 2023-04-10 14:15:36 -07:00
zfs_racct.c module: zfs: fix unused, remove argsused 2021-12-23 09:42:47 -08:00
zfs_sysfs.c [2.2.5-only] Make 'rmmod zfs' work after zfs-2.2.4 (#16406) 2024-08-02 18:03:09 -07:00
zfs_uio.c linux/uio: remove "skip" offset for UIO_ITER 2025-05-27 14:41:45 -07:00
zfs_vfsops.c config: remove HAVE_SUPER_SETUP_BDI_NAME 2024-11-15 10:15:00 -08:00
zfs_vnops_os.c add get_name implementation for exports. (#16833) 2024-12-04 14:12:39 -08:00
zfs_znode_os.c Linux 6.12: PG_error flag was removed 2024-11-15 10:15:01 -08:00
zio_crypt.c ZIL: Assert record sizes in different places 2024-01-08 16:11:39 -08:00
zpl_ctldir.c Linux 6.15: mkdir now returns struct dentry * 2025-05-27 14:41:45 -07:00
zpl_export.c add get_name implementation for exports. (#16833) 2024-12-04 14:12:39 -08:00
zpl_file_range.c config: remove HAVE_VFS_COPY_FILE_RANGE 2024-11-15 10:14:59 -08:00
zpl_file.c Linux 6.0 compat: Check for migratepage VFS 2025-05-27 14:41:45 -07:00
zpl_inode.c Linux 6.15: mkdir now returns struct dentry * 2025-05-27 14:41:45 -07:00
zpl_super.c Linux: Fix zfs_prune panics v2 2025-05-27 14:41:45 -07:00
zpl_xattr.c config: remove HAVE_XATTR_LIST_SIMPLE 2024-11-15 10:14:59 -08:00
zvol_os.c Linux 6.14: BLK_MQ_F_SHOULD_MERGE was removed 2025-05-27 14:41:45 -07:00