diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 3b98096..0693d3d 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -3241,6 +3241,15 @@ do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp) PROC_LOCK(p); oldcred = crcopysafe(p, newcred); newcred->cr_prison = pr; + /* + * Per-credential MAC lookup gating is for unjailed credentials: a + * policy that sets MPC_LOADTIME_FLAG_PERCRED_LOOKUP promises its + * lookup/readlink hooks are no-ops for jailed ones (mac_policy.h). + * Drop the bit so a process the marked parent attaches to a jail + * (every jexec a poudriere builder runs) gets the lockless lookup + * back instead of inheriting the slow path for the whole build. + */ + newcred->cr_flags &= ~CRED_FLAG_MAC_SLOWLOOKUP; #ifdef RACCT racct_proc_ucred_changed(p, oldcred, newcred); #endif diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c index 4dd69ee..c7fc369 100644 --- a/sys/kern/vfs_cache.c +++ b/sys/kern/vfs_cache.c @@ -4263,6 +4263,13 @@ cache_fast_lookup_enabled_recalc(void) #ifdef MAC mac_on = mac_vnode_check_lookup_enabled(); mac_on |= mac_vnode_check_readlink_enabled(); + /* + * If every policy hooking lookup/readlink gates per credential, + * keep the fast path on; cache_can_fplookup() skips it only for + * credentials marked CRED_FLAG_MAC_SLOWLOOKUP. + */ + if (mac_on && atomic_load_bool(&mac_lookup_percred_only)) + mac_on = 0; #else mac_on = 0; #endif @@ -4625,6 +4632,16 @@ cache_can_fplookup(struct cache_fpl *fpl) cache_fpl_aborted_early(fpl); return (false); } + /* + * A MAC policy that gates lookup per credential has marked the + * credentials it restricts; those take the locked path so the + * policy's lookup/readlink hooks run. Everyone else stays fast. + */ + if (__predict_false((cnp->cn_cred->cr_flags & + CRED_FLAG_MAC_SLOWLOOKUP) != 0)) { + cache_fpl_aborted_early(fpl); + return (false); + } if ((cnp->cn_flags & ~CACHE_FPL_SUPPORTED_CN_FLAGS) != 0) { cache_fpl_aborted_early(fpl); return (false); diff --git a/sys/security/mac/mac_framework.c b/sys/security/mac/mac_framework.c index e1733f1..a25fdd4 100644 --- a/sys/security/mac/mac_framework.c +++ b/sys/security/mac/mac_framework.c @@ -137,6 +137,15 @@ bool __read_mostly mac_##f##_fp_flag FPFLAG(priv_check); FPFLAG(priv_grant); FPFLAG(vnode_check_lookup); +/* + * True while every registered policy that implements vnode_check_lookup + * or vnode_check_readlink has set MPC_LOADTIME_FLAG_PERCRED_LOOKUP, i.e. + * all of them gate per credential via CRED_FLAG_MAC_SLOWLOOKUP. Then the + * lockless lookup need not be disabled system-wide; it is skipped only + * for credentials carrying the bit. Maintained by + * mac_policy_fastpath_percred_recalc(); read by cache_fast_lookup_enabled_recalc(). + */ +bool __read_frequently mac_lookup_percred_only = true; FPFLAG(vnode_check_open); FPFLAG(vnode_check_stat); FPFLAG(vnode_check_read); @@ -223,6 +232,7 @@ u_int mac_policy_count; /* Registered policy count. */ static void mac_policy_xlock(void); static void mac_policy_xlock_assert(void); static void mac_policy_xunlock(void); +static void mac_policy_lookup_percred_recalc(void); void mac_policy_slock_nosleep(struct rm_priotracker *tracker) @@ -409,9 +419,49 @@ mac_policy_update(void) mac_policy_count++; } + mac_policy_lookup_percred_recalc(); cache_fast_lookup_enabled_recalc(); } +/* + * mac_lookup_percred_only is true when every policy that hooks name + * lookup or readlink has opted into per-credential gating. One policy + * that hooks these without opting in forces the system-wide disable, as + * before this flag existed: a policy cannot be assumed to gate per + * credential unless it says so. + */ +static bool +mac_policy_lookup_hooked(const struct mac_policy_conf *mpc) +{ + + return (mpc->mpc_ops->mpo_vnode_check_lookup != NULL || + mpc->mpc_ops->mpo_vnode_check_readlink != NULL); +} + +static void +mac_policy_lookup_percred_recalc(void) +{ + struct mac_policy_conf *mpc; + bool percred; + + mac_policy_xlock_assert(); + + percred = true; + LIST_FOREACH(mpc, &mac_static_policy_list, mpc_list) { + if (mac_policy_lookup_hooked(mpc) && + (mpc->mpc_loadtime_flags & + MPC_LOADTIME_FLAG_PERCRED_LOOKUP) == 0) + percred = false; + } + LIST_FOREACH(mpc, &mac_policy_list, mpc_list) { + if (mac_policy_lookup_hooked(mpc) && + (mpc->mpc_loadtime_flags & + MPC_LOADTIME_FLAG_PERCRED_LOOKUP) == 0) + percred = false; + } + atomic_store_bool(&mac_lookup_percred_only, percred); +} + /* * There are frequently used code paths which check for rarely installed * policies. Gross hack below enables doing it in a cheap manner. diff --git a/sys/security/mac/mac_framework.h b/sys/security/mac/mac_framework.h index 09e5c96..39af9c4 100644 --- a/sys/security/mac/mac_framework.h +++ b/sys/security/mac/mac_framework.h @@ -558,8 +558,10 @@ int mac_vnode_check_lookup_impl(struct ucred *cred, struct vnode *dvp, struct componentname *cnp); #ifdef MAC extern bool mac_vnode_check_lookup_fp_flag; +extern bool mac_lookup_percred_only; #else #define mac_vnode_check_lookup_fp_flag false +#define mac_lookup_percred_only true #endif #define mac_vnode_check_lookup_enabled() __predict_false(mac_vnode_check_lookup_fp_flag) static inline int diff --git a/sys/security/mac/mac_policy.h b/sys/security/mac/mac_policy.h index d651dd2..4f14763 100644 --- a/sys/security/mac/mac_policy.h +++ b/sys/security/mac/mac_policy.h @@ -1086,6 +1086,22 @@ struct mac_policy_conf { /* Flags for the mpc_loadtime_flags field. */ #define MPC_LOADTIME_FLAG_NOTLATE 0x00000001 #define MPC_LOADTIME_FLAG_UNLOADOK 0x00000002 +/* + * The policy implements vnode_check_lookup and/or vnode_check_readlink + * but only ever acts on credentials it has marked with + * CRED_FLAG_MAC_SLOWLOOKUP (sys/ucred.h). With this flag set, those + * hooks do not disable the lockless name lookup fast path system-wide; + * the fast path is skipped per credential, only for the marked ones. + * + * The policy is promising two things: that its lookup/readlink hooks are + * no-ops for any credential without the bit, and that they are no-ops + * for jailed credentials -- the bit is cleared when a credential + * attaches to a jail (kern_jail.c), so processes a marked parent runs + * inside jails take the fast path. A policy whose hooks must act on + * jailed credentials cannot use this flag and keeps the system-wide + * disable. + */ +#define MPC_LOADTIME_FLAG_PERCRED_LOOKUP 0x00000004 /* Flags for the mpc_runtime_flags field. */ #define MPC_RUNTIME_FLAG_REGISTERED 0x00000001 diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h index 19b45a5..bc3cb4c 100644 --- a/sys/sys/ucred.h +++ b/sys/sys/ucred.h @@ -45,6 +45,15 @@ */ #define CRED_FLAG_CAPMODE 0x00000001 /* In capability mode. */ #define CRED_FLAG_GROUPSET 0x00000002 /* Groups have been set. */ +/* + * A MAC policy needs the locked (slow) name lookup for this credential, + * so the lockless fast path must not be used for it. Set by a policy + * that opts into per-credential lookup gating (see mac_policy.h, + * MPC_LOADTIME_FLAG_PERCRED_LOOKUP) on exactly the credentials it + * restricts; propagates with the credential through fork/exec/setuid + * like the rest of cr_flags. + */ +#define CRED_FLAG_MAC_SLOWLOOKUP 0x00000004 /* * Number of groups inlined in 'struct ucred'. It must stay reasonably low as