Skip to contentSkip to conversation

selinux,smack: properly reference the LSM blob in security_watch_key()

PATCH In review Paul Moore 2 participants 3 messages Sep 19, 2024

Subsystems (3)
lore
Share & tools
lore

Paul Moore

author maintainer
#0 lore raw

PATCH 1/1 selinux,smack: properly reference the LSM blob in security_watch_key()

Unfortunately when we migrated the lifecycle management of the key LSM blob to the LSM framework we forgot to convert the security_watch_key() callbacks for SELinux and Smack. This patch corrects this by making use of the selinux_key() and smack_key() helper functions respectively.

This patch also removes some input checking in the Smack callback as it is no longer needed.

Reported-by [email protected]
Fixes 5f8d28f6d7d5 ("lsm: infrastructure management of the key security blob")
---
 security/selinux/hooks.c   |  2 +-
 security/smack/smack_lsm.c | 13 +++----------
 2 files changed, 4 insertions(+), 11 deletions(-)
security/selinux/hooks.c +1 -1
index 81fbfa5b80d4..67baa487cf7a 100644
@@ -6720,7 +6720,7 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer)
 #ifdef CONFIG_KEY_NOTIFICATIONS
 static int selinux_watch_key(struct key *key)
 {
-	struct key_security_struct *ksec = key->security;
+	struct key_security_struct *ksec = selinux_key(key);
 	u32 sid = current_sid();
 
 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
security/smack/smack_lsm.c +3 -10
index da0c2bffbd08..563fb404f659 100644
@@ -4629,16 +4629,9 @@ static int smack_watch_key(struct key *key)
 {
 	struct smk_audit_info ad;
 	struct smack_known *tkp = smk_of_current();
+	struct smack_known **blob = smack_key(key);
 	int rc;
 
-	if (key == NULL)
-		return -EINVAL;
-	/*
-	 * If the key hasn't been initialized give it access so that
-	 * it may do so.
-	 */
-	if (key->security == NULL)
-		return 0;
 	/*
 	 * This should not occur
 	 */
@@ -4653,8 +4646,8 @@ static int smack_watch_key(struct key *key)
 	ad.a.u.key_struct.key = key->serial;
 	ad.a.u.key_struct.key_desc = key->description;
 #endif
-	rc = smk_access(tkp, key->security, MAY_READ, &ad);
-	rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
+	rc = smk_access(tkp, *blob, MAY_READ, &ad);
+	rc = smk_bu_note("key watch", tkp, *blob, MAY_READ, rc);
 	return rc;
 }
 #endif /* CONFIG_KEY_NOTIFICATIONS */
Signature · 1 line
2.46.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Paul Moore

Unfortunately when we migrated the lifecycle management of the key LSM blob to the LSM framework we forgot to convert the security_watch_key() callbacks for SELinux and Smack. This patch corrects this by making use of the selinux_key() and smack_key() helper functions respectively.

This patch also removes some input checking in the Smack callback as it is no longer needed.

Reported-by [email protected]
Fixes 5f8d28f6d7d5 ("lsm: infrastructure management of the key security blob")
Quoted 51 lines
---
 security/selinux/hooks.c   |  2 +-
 security/smack/smack_lsm.c | 13 +++----------
 2 files changed, 4 insertions(+), 11 deletions(-)
security/selinux/hooks.c +1 -1
index 81fbfa5b80d4..67baa487cf7a 100644
@@ -6720,7 +6720,7 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer)
 #ifdef CONFIG_KEY_NOTIFICATIONS
 static int selinux_watch_key(struct key *key)
 {
-	struct key_security_struct *ksec = key->security;
+	struct key_security_struct *ksec = selinux_key(key);
 	u32 sid = current_sid();
 
 	return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
security/smack/smack_lsm.c +3 -10
index da0c2bffbd08..563fb404f659 100644
@@ -4629,16 +4629,9 @@ static int smack_watch_key(struct key *key)
 {
 	struct smk_audit_info ad;
 	struct smack_known *tkp = smk_of_current();
+	struct smack_known **blob = smack_key(key);
 	int rc;
 
-	if (key == NULL)
-		return -EINVAL;
-	/*
-	 * If the key hasn't been initialized give it access so that
-	 * it may do so.
-	 */
-	if (key->security == NULL)
-		return 0;
 	/*
 	 * This should not occur
 	 */
@@ -4653,8 +4646,8 @@ static int smack_watch_key(struct key *key)
 	ad.a.u.key_struct.key = key->serial;
 	ad.a.u.key_struct.key_desc = key->description;
 #endif
-	rc = smk_access(tkp, key->security, MAY_READ, &ad);
-	rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
+	rc = smk_access(tkp, *blob, MAY_READ, &ad);
+	rc = smk_bu_note("key watch", tkp, *blob, MAY_READ, rc);
 	return rc;
 }
 #endif /* CONFIG_KEY_NOTIFICATIONS */
author maintainer
#2 lore raw
Quoted 6 lines · Casey Schaufler
Quoted 5 lines · Paul Moore

Unfortunately when we migrated the lifecycle management of the key LSM blob to the LSM framework we forgot to convert the security_watch_key() callbacks for SELinux and Smack. This patch corrects this by making use of the selinux_key() and smack_key() helper functions respectively.

Quoted 8 lines
Quoted 6 lines

This patch also removes some input checking in the Smack callback as it is no longer needed.

Reported-by [email protected]
Fixes 5f8d28f6d7d5 ("lsm: infrastructure management of the key security blob")

Thanks for the quick review, it looks like syzbot was happy with the patch too so I've merged this into lsm/stable-6.12 and I'll send it up to Linus soon.

Signature · 1 line
paul-moore.com