FAILED: patch "[PATCH] selinux: reject an unclaimed class value in" failed to apply to 6.1-stable tree
Subsystems (2)
[email protected]
maintainerThe patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x 22b05fec62c0fe9864cfceb52f7d0f3a34d9b1dd
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026082039-waffle-overlabor-d7cc@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 22b05fec62c0fe9864cfceb52f7d0f3a34d9b1dd Mon Sep 17 00:00:00 2001
From: Bryam Vargas <[email protected]>
Date: Fri, 31 Jul 2026 12:44:11 -0500
Subject: [PATCH] selinux: reject an unclaimed class value in
security_get_classes()
security_get_classes() sizes an array by p_classes.nprim and fills it at value - 1, so a class value the policy never defines leaves a NULL. sel_make_classes() passes every entry to sel_make_dir(), reaching the same d_alloc_name() dereference as the permission array. The class symbol table is allowed to be sparse (policydb_class_isvalid() exists to absorb that), but this getter builds its own array straight from the hash table and has no such predicate.
Fail the lookup when a value went unclaimed instead of handing out the NULL. Conforming policies define every class they declare and are unaffected.
security/selinux/ss/services.c +19 -5
@@ -3304,6 +3304,7 @@ int security_get_classes(struct selinux_policy *policy, char ***classes, u32 *nclasses) { struct policydb *policydb; +u32 i;int rc; policydb = &policy->policydb;
@@ -3316,16 +3317,29 @@ int security_get_classes(struct selinux_policy *policy, rc =hashtab_map (&policydb->p_classes.table, get_classes_callback, *classes); -if (rc) { -u32 i; +if (rc) + goto err; -for (i =0 ; i < *nclasses; i++) -kfree ((*classes)[i]); -kfree (*classes); +/* +* The class symtab may be sparse, which policydb_class_isvalid() exists +* to absorb; the callback fills this array by value, so an unclaimed +* one leaves a NULL that sel_make_classes() hands to sel_make_dir(). +*/ +for (i =0 ; i < *nclasses; i++) { +if (!(*classes)[i]) { + rc = -EINVAL ; + goto err; + } } out:return rc; + +err: +for (i =0 ; i < *nclasses; i++) +kfree ((*classes)[i]); +kfree (*classes); +return rc; }static int get_permissions_callback (void *k,void *d,void *args)
Sasha Levin
maintainerPATCH 1/3 selinux: uninline unlikely parts of avc_has_perm_noaudit()
From: Paul Moore <[email protected]>
[ Upstream commit f62ca0b6e31d82e0622a8e31ce5562e80edf6c3c ]
This is based on earlier patch posted to the list by Linus, his commit description read:
"avc_has_perm_noaudit()is one of those hot functions that end up
being used by almost all filesystem operations (through "avc_has_perm()") and it's intended to be cheap enough to inline.
However, it turns out that the unlikely parts of it (where it doesn't find an existing avc node) need a fair amount of stack space for the automatic replacement node, so if it were to be inlined (at least clang does not) it would just use stack space unnecessarily.
So split the unlikely part out of it, and mark that part noinline. That improves the actual likely part."
The basic idea behind the patch was reasonable, but there were minor nits (double indenting, etc.) and the RCU read lock unlock/re-lock in avc_compute_av() began to look even more ugly. This patch builds on Linus' first effort by cleaning things up a bit and removing the RCU unlock/lock dance in avc_compute_av().
Removing the RCU lock dance in avc_compute_av() is safe as there are currently two callers of avc_compute_av(): avc_has_perm_noaudit() and avc_has_extended_perms(). The first caller in avc_has_perm_noaudit() does not require a RCU lock as there is no avc_node to protect so the RCU lock can be dropped before calling avc_compute_av(). The second caller, avc_has_extended_perms(), is similar in that there is no avc_node that requires RCU protection, but the code is simplified by holding the RCU look around the avc_compute_av() call, and given that we enter a RCU critical section in security_compute_av() (called from av_compute_av()) the impact will likely be unnoticeable. It is also worth noting that avc_has_extended_perms() is only called from the SELinux ioctl() access control hook at the moment.
Stable-dep-of: 22b05fec62c0 ("selinux: reject an unclaimed class value in security_get_classes()")
--- security/selinux/avc.c | 85 ++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 28 deletions(-)
security/selinux/avc.c +57 -28
@@ -988,25 +988,26 @@ int avc_ss_reset(struct selinux_avc *avc, u32 seqno)return rc; } -/* -* Slow-path helper function for avc_has_perm_noaudit, -* when the avc_node lookup fails. We get called with -* the RCU read lock held, and need to return with it -* still held, but drop if for the security compute. +/** +* avc_compute_av - Add an entry to the AVC based on the security policy +* @state: SELinux state pointer +* @ssid: subject +* @tsid: object/target +* @tclass: object class +* @avd: access vector decision +* @xp_node: AVC extended permissions node * -* Don't inline this, since it's the slow-path and just -* results in a bigger stack frame. +* Slow-path helper function for avc_has_perm_noaudit, when the avc_node lookup +* fails. Don't inline this, since it's the slow-path and just results in a +* bigger stack frame. */ -static noinline -struct avc_node *avc_compute_av(struct selinux_state *state, -u32 ssid,u32 tsid, -u16 tclass,struct av_decision *avd, -struct avc_xperms_node *xp_node) +static noinline struct avc_node *avc_compute_av(struct selinux_state *state, +u32 ssid,u32 tsid,u16 tclass, +struct av_decision *avd, +struct avc_xperms_node *xp_node) { -rcu_read_unlock ();INIT_LIST_HEAD (&xp_node->xpd_head);security_compute_av (state, ssid, tsid, tclass, avd, &xp_node->xp); -rcu_read_lock ();return avc_insert (state->avc, ssid, tsid, tclass, avd, xp_node); }
@@ -1112,6 +1113,36 @@ int avc_has_extended_perms(struct selinux_state *state,return rc; } +/** +* avc_perm_nonode - Add an entry to the AVC +* @state: SELinux state pointer +* @ssid: subject +* @tsid: object/target +* @tclass: object class +* @requested: requested permissions +* @flags: AVC flags +* @avd: access vector decision +* +* This is the "we have no node" part of avc_has_perm_noaudit(), which is +* unlikely and needs extra stack space for the new node that we generate, so +* don't inline it. +*/ +static noinline int avc_perm_nonode(struct selinux_state *state, +u32 ssid,u32 tsid,u16 tclass, +u32 requested,unsigned int flags, +struct av_decision *avd) +{ +u32 denied; +struct avc_xperms_node xp_node; + +avc_compute_av (state, ssid, tsid, tclass, avd, &xp_node); + denied = requested & ~(avd->allowed); +if (unlikely (denied)) +return avc_denied (state, ssid, tsid, tclass, requested,0 ,0 , + flags, avd); +return 0 ; +} +/** * avc_has_perm_noaudit - Check permissions but perform no auditing. * @state: SELinux state
@@ -1139,29 +1170,27 @@ inline int avc_has_perm_noaudit(struct selinux_state *state,unsigned int flags, struct av_decision *avd) { - struct avc_node *node; -struct avc_xperms_node xp_node; -int rc =0 ; u32 denied; +struct avc_node *node;if (WARN_ON (!requested))return -EACCES ;rcu_read_lock (); - node =avc_lookup (state->avc, ssid, tsid, tclass); -if (unlikely (!node)) -avc_compute_av (state, ssid, tsid, tclass, avd, &xp_node); -else -memcpy (avd, &node->ae.avd,sizeof (*avd)); +if (unlikely (!node)) { +rcu_read_unlock (); +return avc_perm_nonode (state, ssid, tsid, tclass, requested, + flags, avd); + } + denied = requested & ~node->ae.avd.allowed; +memcpy (avd, &node->ae.avd,sizeof (*avd)); +rcu_read_unlock (); - denied = requested & ~(avd->allowed);if (unlikely (denied)) - rc =avc_denied (state, ssid, tsid, tclass, requested,0 ,0 , - flags, avd); - -rcu_read_unlock (); -return rc; +return avc_denied (state, ssid, tsid, tclass, requested,0 ,0 , + flags, avd); +return 0 ; }/**
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 selinux: avoid implicit conversions in services code
From: Christian Göttsche <[email protected]>
[ Upstream commit c50e125d057152bc68dfd5669b73611343653eb7 ]
Use u32 as the output parameter type in security_get_classes() and security_get_permissions(), based on the type of the symtab nprim member.
Declare the read-only class string parameter ofsecurity_get_permissions() const.
Avoid several implicit conversions by using the identical type for the destination.
Use the type identical to the source for local variables.
Stable-dep-of: 22b05fec62c0 ("selinux: reject an unclaimed class value in security_get_classes()")
--- security/selinux/include/security.h | 4 ++-- security/selinux/selinuxfs.c | 7 ++++--- security/selinux/ss/services.c | 23 ++++++++++++----------- 3 files changed, 18 insertions(+), 16 deletions(-)
security/selinux/include/security.h +2 -2
@@ -379,9 +379,9 @@ int security_net_peersid_resolve(struct selinux_state *state, u32 *peer_sid);int security_get_classes (struct selinux_policy *policy, -char ***classes,int *nclasses); +char ***classes,u32 *nclasses);int security_get_permissions (struct selinux_policy *policy, -char *class,char ***perms,int *nperms); +const char *class,char ***perms,u32 *nperms);int security_get_reject_unknown (struct selinux_state *state);int security_get_allow_unknown (struct selinux_state *state);
security/selinux/selinuxfs.c +4 -3
@@ -1874,7 +1874,8 @@ static int sel_make_perm_files(struct selinux_policy *newpolicy, char *objclass, int classvalue, struct dentry *dir) { - int i, rc, nperms; + u32 i, nperms; +int rc;char **perms; rc =security_get_permissions (newpolicy, objclass, &perms, &nperms);
@@ -1944,8 +1945,8 @@ static int sel_make_classes(struct selinux_policy *newpolicy,struct dentry *class_dir, unsigned long *last_class_ino) { - - int rc, nclasses, i; + u32 i, nclasses; +int rc;char **classes; rc =security_get_classes (newpolicy, &classes, &nclasses);
security/selinux/ss/services.c +12 -11
@@ -869,7 +869,7 @@ int security_bounded_transition(struct selinux_state *state,struct sidtab *sidtab;struct sidtab_entry *old_entry, *new_entry;struct type_datum *type; -int index; +u32 index;int rc;if (!selinux_initialized (state))
@@ -1537,7 +1537,7 @@ static int security_context_to_sid_core(struct selinux_state *state,return -ENOMEM ;if (!selinux_initialized (state)) { -int i; +u32 i;for (i =1 ; i <SECINITSID_NUM ; i++) {const char *s = initial_sid_to_string[i];
@@ -2886,7 +2886,6 @@ static inline int __security_genfs_sid(struct selinux_policy *policy, {struct policydb *policydb = &policy->policydb;struct sidtab *sidtab = policy->sidtab; -int len;u16 sclass;struct genfs *genfs;struct ocontext *c;
@@ -2908,7 +2907,7 @@ static inline int __security_genfs_sid(struct selinux_policy *policy,return -ENOENT ;for (c = genfs->head; c; c = c->next) { - len =strlen (c->u.name); +size_t len =strlen (c->u.name);if ((!c->v.sclass || sclass == c->v.sclass) && (strncmp (c->u.name, path, len) ==0 ))break ;
@@ -3402,7 +3401,7 @@ static int get_classes_callback(void *k, void *d, void *args) {struct class_datum *datum = d;char *name = k, **classes = args; -int value = datum->value -1 ; +u32 value = datum->value -1 ; classes[value] =kstrdup (name,GFP_ATOMIC );if (!classes[value])
@@ -3412,7 +3411,7 @@ static int get_classes_callback(void *k, void *d, void *args) }int security_get_classes (struct selinux_policy *policy, -char ***classes,int *nclasses) +char ***classes,u32 *nclasses) {struct policydb *policydb;int rc;
@@ -3428,7 +3427,8 @@ int security_get_classes(struct selinux_policy *policy, rc =hashtab_map (&policydb->p_classes.table, get_classes_callback, *classes);if (rc) { -int i; +u32 i; +for (i =0 ; i < *nclasses; i++)kfree ((*classes)[i]);kfree (*classes);
@@ -3442,7 +3442,7 @@ static int get_permissions_callback(void *k, void *d, void *args) {struct perm_datum *datum = d;char *name = k, **perms = args; -int value = datum->value -1 ; +u32 value = datum->value -1 ; perms[value] =kstrdup (name,GFP_ATOMIC );if (!perms[value])
@@ -3452,10 +3452,11 @@ static int get_permissions_callback(void *k, void *d, void *args) }int security_get_permissions (struct selinux_policy *policy, -char *class,char ***perms,int *nperms) +const char *class,char ***perms,u32 *nperms) {struct policydb *policydb; -int rc, i; +u32 i; +int rc;struct class_datum *match; policydb = &policy->policydb;
@@ -3675,7 +3676,7 @@ int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule,/* Check to see if the rule contains any selinux fields */ int selinux_audit_rule_known (struct audit_krule *rule) { -int i; +u32 i;for (i =0 ; i < rule->field_count; i++) {struct audit_field *f = &rule->fields[i];
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 selinux: reject an unclaimed class value in security_get_classes()
In reply to Sasha LevinFrom: Bryam Vargas <[email protected]>
[ Upstream commit 22b05fec62c0fe9864cfceb52f7d0f3a34d9b1dd ]
security_get_classes() sizes an array by p_classes.nprim and fills it at value - 1, so a class value the policy never defines leaves a NULL. sel_make_classes() passes every entry to sel_make_dir(), reaching the same d_alloc_name() dereference as the permission array. The class symbol table is allowed to be sparse (policydb_class_isvalid() exists to absorb that), but this getter builds its own array straight from the hash table and has no such predicate.
Fail the lookup when a value went unclaimed instead of handing out the NULL. Conforming policies define every class they declare and are unaffected.
--- security/selinux/ss/services.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +19 -5
@@ -3414,6 +3414,7 @@ int security_get_classes(struct selinux_policy *policy, char ***classes, u32 *nclasses) { struct policydb *policydb; +u32 i;int rc; policydb = &policy->policydb;
@@ -3426,16 +3427,29 @@ int security_get_classes(struct selinux_policy *policy, rc =hashtab_map (&policydb->p_classes.table, get_classes_callback, *classes); -if (rc) { -u32 i; +if (rc) + goto err; -for (i =0 ; i < *nclasses; i++) -kfree ((*classes)[i]); -kfree (*classes); +/* +* The class symtab may be sparse, which policydb_class_isvalid() exists +* to absorb; the callback fills this array by value, so an unclaimed +* one leaves a NULL that sel_make_classes() hands to sel_make_dir(). +*/ +for (i =0 ; i < *nclasses; i++) { +if (!(*classes)[i]) { + rc = -EINVAL ; + goto err; + } } out:return rc; + +err: +for (i =0 ; i < *nclasses; i++) +kfree ((*classes)[i]); +kfree (*classes); +return rc; }static int get_permissions_callback (void *k,void *d,void *args)
Signature · 1 line
2.53.0