public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] keys: flush work when accessing /proc/key-users
@ 2023-12-06 14:57 Luis Henriques
  2023-12-06 16:04 ` David Howells
  2023-12-07  4:33 ` [RFC PATCH] keys: flush work when accessing /proc/key-users Jarkko Sakkinen
  0 siblings, 2 replies; 16+ messages in thread
From: Luis Henriques @ 2023-12-06 14:57 UTC (permalink / raw)
  To: David Howells, Jarkko Sakkinen, Eric Biggers
  Cc: keyrings, linux-kernel, Luis Henriques

Make sure the garbage collector has been run before cycling through all
the user keys.

Signed-off-by: Luis Henriques <lhenriques@suse.de>
---
Hi!

This patch is mostly for getting some feedback on how to fix an fstest
failing for ext4/fscrypt (generic/581).  Basically, the test relies on the
data read from /proc/key-users to be up-to-date regarding the number of
keys a given user currently has.  However, this file can't be trusted
because it races against the keys GC.

Using flush_work() seems to work (I can't reproduce the failure), but it
may be overkill.  Or simply not acceptable.  Maybe, as Eric suggested
elsewhere [1], there could be a synchronous key_put/revoke/invalidate/...,
which would wait for the key GC to do its work, although that probably
would require some more code re-work.

[1] https://lore.kernel.org/all/20231128173734.GD1148@sol.localdomain/

 security/keys/gc.c       | 6 ++++++
 security/keys/internal.h | 1 +
 security/keys/proc.c     | 1 +
 3 files changed, 8 insertions(+)

diff --git a/security/keys/gc.c b/security/keys/gc.c
index 3c90807476eb..57b5a54490a0 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -44,6 +44,12 @@ struct key_type key_type_dead = {
 	.name = ".dead",
 };
 
+void key_flush_gc(void)
+{
+	kenter("");
+	flush_work(&key_gc_work);
+}
+
 /*
  * Schedule a garbage collection run.
  * - time precision isn't particularly important
diff --git a/security/keys/internal.h b/security/keys/internal.h
index 471cf36dedc0..fee1d0025d96 100644
--- a/security/keys/internal.h
+++ b/security/keys/internal.h
@@ -170,6 +170,7 @@ extern void keyring_restriction_gc(struct key *keyring,
 extern void key_schedule_gc(time64_t gc_at);
 extern void key_schedule_gc_links(void);
 extern void key_gc_keytype(struct key_type *ktype);
+extern void key_flush_gc(void);
 
 extern int key_task_permission(const key_ref_t key_ref,
 			       const struct cred *cred,
diff --git a/security/keys/proc.c b/security/keys/proc.c
index d0cde6685627..2837e00a240a 100644
--- a/security/keys/proc.c
+++ b/security/keys/proc.c
@@ -277,6 +277,7 @@ static void *proc_key_users_start(struct seq_file *p, loff_t *_pos)
 	struct rb_node *_p;
 	loff_t pos = *_pos;
 
+	key_flush_gc();
 	spin_lock(&key_user_lock);
 
 	_p = key_user_first(seq_user_ns(p), &key_user_tree);

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-01-29 11:24 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-06 14:57 [RFC PATCH] keys: flush work when accessing /proc/key-users Luis Henriques
2023-12-06 16:04 ` David Howells
2023-12-06 17:55   ` Luis Henriques
2023-12-07  2:43     ` Eric Biggers
2023-12-11 14:02       ` David Howells
2023-12-12  3:03         ` Eric Biggers
2023-12-14 14:44         ` Luís Henriques
2024-01-15 12:03         ` [RFC PATCH v2] keys: update key quotas in key_put() Luis Henriques
2024-01-19 21:10           ` Jarkko Sakkinen
2024-01-22 11:50             ` Luis Henriques
2024-01-22 19:45               ` Jarkko Sakkinen
2024-01-24 22:12           ` Eric Biggers
2024-01-26 16:12             ` Luis Henriques
2024-01-27  6:42               ` Eric Biggers
2024-01-29 11:23                 ` Luis Henriques
2023-12-07  4:33 ` [RFC PATCH] keys: flush work when accessing /proc/key-users Jarkko Sakkinen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox