linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Jarkko Sakkinen <jarkko@kernel.org>
Cc: David Howells <dhowells@redhat.com>,
	keyrings@vger.kernel.org, linux-security-module@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 6/7] keys: Provide a key_try_get() function and use it
Date: Wed, 21 Aug 2024 13:36:14 +0100	[thread overview]
Message-ID: <20240821123616.60401-7-dhowells@redhat.com> (raw)
In-Reply-To: <20240821123616.60401-1-dhowells@redhat.com>

Add a key_try_get() function to try to get a ref on a key and switch code
that's manipulating the key refcount directly to use it.  This will allow a
tracepoint to be emplaced there later.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Jarkko Sakkinen <jarkko@kernel.org>
cc: keyrings@vger.kernel.org
cc: linux-security-module@vger.kernel.org
---
 include/linux/key.h     |  1 +
 security/keys/key.c     | 16 +++++++++++++++-
 security/keys/keyring.c |  2 +-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/include/linux/key.h b/include/linux/key.h
index 80d736813b89..4e5baf3e7286 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -300,6 +300,7 @@ extern struct key *key_alloc(struct key_type *type,
 extern void key_revoke(struct key *key);
 extern void key_invalidate(struct key *key);
 struct key *key_get(struct key *key);
+struct key *key_try_get(struct key *key);
 extern void key_put(struct key *key);
 extern bool key_put_tag(struct key_tag *tag);
 extern void key_remove_domain(struct key_tag *domain_tag);
diff --git a/security/keys/key.c b/security/keys/key.c
index 14c7ee77ea15..59cffb6f9b94 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -649,6 +649,20 @@ struct key *key_get(struct key *key)
 }
 EXPORT_SYMBOL(key_get);
 
+/**
+ * key_try_get - Get a ref on a key if its refcount is not non-zero.
+ * @key: The key to get a reference on.
+ *
+ * Get a reference on a key unless it has no references and return true if
+ * successful.  @key must not be NULL.
+ */
+struct key *key_try_get(struct key *key)
+{
+	if (!refcount_inc_not_zero(&key->usage))
+		return NULL;
+	return key;
+}
+
 /**
  * key_put - Discard a reference to a key.
  * @key: The key to discard a reference from.
@@ -709,7 +723,7 @@ struct key *key_lookup(key_serial_t id)
 	/* A key is allowed to be looked up only if someone still owns a
 	 * reference to it - otherwise it's awaiting the gc.
 	 */
-	if (!refcount_inc_not_zero(&key->usage))
+	if (!key_try_get(key))
 		goto not_found;
 
 error:
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index e77d927f1d4d..a09a4c2b1bcb 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -1174,7 +1174,7 @@ struct key *find_keyring_by_name(const char *name, bool uid_keyring)
 		/* we've got a match but we might end up racing with
 		 * key_cleanup() if the keyring is currently 'dead'
 		 * (ie. it has a zero usage count) */
-		if (!refcount_inc_not_zero(&keyring->usage))
+		if (!key_try_get(keyring))
 			continue;
 		keyring->last_used_at = ktime_get_real_seconds();
 		goto out;


  parent reply	other threads:[~2024-08-21 12:36 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-21 12:36 [PATCH 0/7] keys: Add tracepoints David Howells
2024-08-21 12:36 ` [PATCH 1/7] keys: Out of line key_is_dead() so it can have tracepoints added in David Howells
2024-08-27 18:22   ` Jarkko Sakkinen
2024-08-21 12:36 ` [PATCH 2/7] keys: Extract struct key_user to its own header for tracing purposes David Howells
2024-08-27 18:23   ` Jarkko Sakkinen
2024-08-21 12:36 ` [PATCH 3/7] keys: Move key_get() out of line so a tracepoint can be added David Howells
2024-08-27 18:23   ` Jarkko Sakkinen
2024-08-21 12:36 ` [PATCH 4/7] keys: Add a key_ref_get() wrapper David Howells
2024-08-27 18:23   ` Jarkko Sakkinen
2024-08-21 12:36 ` [PATCH 5/7] keys: Use key_get() instead of __key_get() David Howells
2024-08-27 18:24   ` Jarkko Sakkinen
2024-08-21 12:36 ` David Howells [this message]
2024-08-27 18:24   ` [PATCH 6/7] keys: Provide a key_try_get() function and use it Jarkko Sakkinen
2024-08-21 12:36 ` [PATCH 7/7] keys: Add tracepoints for the keyrings facility David Howells
2024-08-27 18:27   ` Jarkko Sakkinen
2024-09-28  2:03   ` Justin Stitt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240821123616.60401-7-dhowells@redhat.com \
    --to=dhowells@redhat.com \
    --cc=jarkko@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).