Skip to content

Commit 4ee1368

Browse files
robnbehlendorf
authored andcommitted
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 <[email protected]> Reviewed-by: Pavel Snajdr <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Kyle Evans <[email protected]> Reviewed-by: Tony Hutter <[email protected]>
1 parent 0a73e91 commit 4ee1368

File tree

16 files changed

+116
-112
lines changed

16 files changed

+116
-112
lines changed

include/os/freebsd/spl/sys/policy.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ struct znode;
3838

3939
int secpolicy_nfs(cred_t *cr);
4040
int secpolicy_zfs(cred_t *crd);
41-
int secpolicy_zfs_proc(cred_t *cr, proc_t *proc);
4241
int secpolicy_sys_config(cred_t *cr, int checkonly);
4342
int secpolicy_zinject(cred_t *cr);
4443
int secpolicy_fs_unmount(cred_t *cr, struct mount *vfsp);

include/os/linux/zfs/sys/policy.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ int secpolicy_vnode_setids_setgids(const cred_t *, gid_t, zidmap_t *,
5151
struct user_namespace *);
5252
int secpolicy_zinject(const cred_t *);
5353
int secpolicy_zfs(const cred_t *);
54-
int secpolicy_zfs_proc(const cred_t *, proc_t *);
5554
void secpolicy_setid_clear(vattr_t *, cred_t *);
5655
int secpolicy_setid_setsticky_clear(struct inode *, vattr_t *,
5756
const vattr_t *, cred_t *, zidmap_t *, struct user_namespace *);

include/sys/dmu_recv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ typedef struct dmu_recv_cookie {
5959
uint64_t drc_ivset_guid;
6060
void *drc_owner;
6161
cred_t *drc_cred;
62-
proc_t *drc_proc;
6362
nvlist_t *drc_begin_nvl;
6463

6564
objset_t *drc_os;

include/sys/dsl_dataset.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ typedef struct dsl_dataset_promote_arg {
283283
uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
284284
nvlist_t *err_ds;
285285
cred_t *cr;
286-
proc_t *proc;
287286
} dsl_dataset_promote_arg_t;
288287

289288
typedef struct dsl_dataset_rollback_arg {
@@ -298,7 +297,6 @@ typedef struct dsl_dataset_snapshot_arg {
298297
nvlist_t *ddsa_props;
299298
nvlist_t *ddsa_errors;
300299
cred_t *ddsa_cr;
301-
proc_t *ddsa_proc;
302300
} dsl_dataset_snapshot_arg_t;
303301

304302
typedef struct dsl_dataset_rename_snapshot_arg {
@@ -458,7 +456,7 @@ int dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
458456
void dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
459457
dsl_dataset_t *origin_head, dmu_tx_t *tx);
460458
int dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
461-
dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr, proc_t *proc);
459+
dmu_tx_t *tx, boolean_t recv, uint64_t cnt, cred_t *cr);
462460
void dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
463461
dmu_tx_t *tx);
464462

include/sys/dsl_dir.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ int dsl_dir_set_reservation(const char *ddname, zprop_source_t source,
184184
uint64_t reservation);
185185
int dsl_dir_activate_fs_ss_limit(const char *);
186186
int dsl_fs_ss_limit_check(dsl_dir_t *, uint64_t, zfs_prop_t, dsl_dir_t *,
187-
cred_t *, proc_t *);
187+
cred_t *);
188188
void dsl_fs_ss_count_adjust(dsl_dir_t *, int64_t, const char *, dmu_tx_t *);
189189
int dsl_dir_rename(const char *oldname, const char *newname);
190190
int dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd,
191-
uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *, proc_t *);
191+
uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *);
192192
boolean_t dsl_dir_is_clone(dsl_dir_t *dd);
193193
void dsl_dir_new_refreservation(dsl_dir_t *dd, struct dsl_dataset *ds,
194194
uint64_t reservation, cred_t *cr, dmu_tx_t *tx);

include/sys/zcp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ typedef struct zcp_run_info {
7575
* rather than the 'current' thread's.
7676
*/
7777
cred_t *zri_cred;
78-
proc_t *zri_proc;
7978

8079
/*
8180
* The tx in which this channel program is running.

include/sys/zfs_context.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ extern void delay(clock_t ticks);
629629
#define kcred NULL
630630
#define CRED() NULL
631631

632+
#define crhold(cr) ((void)cr)
633+
#define crfree(cr) ((void)cr)
634+
632635
#define ptob(x) ((x) * PAGESIZE)
633636

634637
#define NN_DIVISOR_1000 (1U << 0)
@@ -741,7 +744,6 @@ extern int zfs_secpolicy_rename_perms(const char *from, const char *to,
741744
cred_t *cr);
742745
extern int zfs_secpolicy_destroy_perms(const char *name, cred_t *cr);
743746
extern int secpolicy_zfs(const cred_t *cr);
744-
extern int secpolicy_zfs_proc(const cred_t *cr, proc_t *proc);
745747
extern zoneid_t getzoneid(void);
746748

747749
/* SID stuff */

lib/libzpool/kernel.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,6 @@ secpolicy_zfs(const cred_t *cr)
914914
return (0);
915915
}
916916

917-
int
918-
secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
919-
{
920-
(void) cr, (void) proc;
921-
return (0);
922-
}
923-
924917
ksiddomain_t *
925918
ksid_lookupdomain(const char *dom)
926919
{

module/os/freebsd/spl/spl_policy.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ secpolicy_zfs(cred_t *cr)
5151
return (priv_check_cred(cr, PRIV_VFS_MOUNT));
5252
}
5353

54-
int
55-
secpolicy_zfs_proc(cred_t *cr, proc_t *proc)
56-
{
57-
58-
return (priv_check_cred(cr, PRIV_VFS_MOUNT));
59-
}
60-
6154
int
6255
secpolicy_sys_config(cred_t *cr, int checkonly __unused)
6356
{

module/os/linux/zfs/policy.c

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2424
* Copyright 2013, Joyent, Inc. All rights reserved.
2525
* Copyright (C) 2016 Lawrence Livermore National Security, LLC.
26+
* Copyright (c) 2025, Rob Norris <[email protected]>
2627
*
2728
* For Linux the vast majority of this enforcement is already handled via
2829
* the standard Linux VFS permission checks. However certain administrative
@@ -34,28 +35,32 @@
3435
#include <linux/security.h>
3536
#include <linux/vfs_compat.h>
3637

37-
/*
38-
* The passed credentials cannot be directly verified because Linux only
39-
* provides and interface to check the *current* process credentials. In
40-
* order to handle this the capable() test is only run when the passed
41-
* credentials match the current process credentials or the kcred. In
42-
* all other cases this function must fail and return the passed err.
43-
*/
4438
static int
4539
priv_policy_ns(const cred_t *cr, int capability, int err,
4640
struct user_namespace *ns)
4741
{
48-
if (cr != CRED() && (cr != kcred))
49-
return (err);
42+
/*
43+
* The passed credentials cannot be directly verified because Linux
44+
* only provides an interface to check the *current* process
45+
* credentials. In order to handle this we check if the passed in
46+
* creds match the current process credentials or the kcred. If not,
47+
* we swap the passed credentials into the current task, perform the
48+
* check, and then revert it before returning.
49+
*/
50+
const cred_t *old =
51+
(cr != CRED() && cr != kcred) ? override_creds(cr) : NULL;
5052

5153
#if defined(CONFIG_USER_NS)
52-
if (!(ns ? ns_capable(ns, capability) : capable(capability)))
54+
if (ns ? ns_capable(ns, capability) : capable(capability))
5355
#else
54-
if (!capable(capability))
56+
if (capable(capability))
5557
#endif
56-
return (err);
58+
err = 0;
5759

58-
return (0);
60+
if (old)
61+
revert_creds(old);
62+
63+
return (err);
5964
}
6065

6166
static int
@@ -248,19 +253,6 @@ secpolicy_zfs(const cred_t *cr)
248253
return (priv_policy(cr, CAP_SYS_ADMIN, EACCES));
249254
}
250255

251-
/*
252-
* Equivalent to secpolicy_zfs(), but works even if the cred_t is not that of
253-
* the current process. Takes both cred_t and proc_t so that this can work
254-
* easily on all platforms.
255-
*/
256-
int
257-
secpolicy_zfs_proc(const cred_t *cr, proc_t *proc)
258-
{
259-
if (!has_capability(proc, CAP_SYS_ADMIN))
260-
return (EACCES);
261-
return (0);
262-
}
263-
264256
void
265257
secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
266258
{

0 commit comments

Comments
 (0)