From: Eric Biggers <ebiggers@kernel.org>
To: Luis Henriques <lhenriques@suse.de>
Cc: David Howells <dhowells@redhat.com>,
Jarkko Sakkinen <jarkko@kernel.org>,
keyrings@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] keys: flush work when accessing /proc/key-users
Date: Wed, 6 Dec 2023 18:43:23 -0800 [thread overview]
Message-ID: <20231207024323.GA1994@sol.localdomain> (raw)
In-Reply-To: <87bkb3z047.fsf@suse.de>
On Wed, Dec 06, 2023 at 05:55:52PM +0000, Luis Henriques wrote:
> David Howells <dhowells@redhat.com> writes:
>
> > Luis Henriques <lhenriques@suse.de> wrote:
> >
> >> 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.
> >
> > Unfortunately, I don't think your patch helps. If the GC hasn't started yet,
> > it won't achieve anything and the GC can still be triggered at any time after
> > the flush and thus race.
> >
> > What is it you're actually trying to determine?
> >
> > And is it only for doing the test?
>
> OK, let me try to describe what the generic/581 fstest does.
>
> After doing a few fscrypt related things, which involve adding and
> removing keys, the test will:
>
> 1. Get the number of keys for user 'fsgqa' from '/proc/key-users'
> 2. Set the maxkeys to 5 + <keys the user had in 1.>
> 3. In a loop, try to add 6 new keys, to confirm the last one will fail
>
> Most of the time the test passes, i.e., the 6th key fails to be added.
> However, if, for example, the test is executed in a loop, it is possible
> to have it fail because the 6th key was successfully added. The reason
> is, obviously, because the test is racy: the GC can kick-in too late,
> after the maxkeys is set in step 2.
>
> So, this is mostly an issue with the test itself, but I couldn't figure
> out a way to work around it.
>
> Another solution I thought but I didn't look too deep into was to try to
> move the
>
> atomic_dec(&key->user->nkeys);
>
> out of the GC, in function key_gc_unused_keys(). Decrementing it
> synchronously in key_put() (or whatever other functions could schedule GC)
> should solve the problem with this test. But as I said I didn't went too
> far looking into that, so I don't really know if that's feasible.
>
> Finally, the test itself could be hacked so that the loop in step 3. would
> update the maxkeys value if needed, i.e. if the current number of keys for
> the user isn't what was expected in each loop iteration. But even that
> would still be racy.
If there was a function that fully and synchronously releases a key's quota,
fs/crypto/ could call it before unlinking the key. key_payload_reserve(key, 0)
almost does the trick, but it would release the key's bytes, not the key itself.
However, that would only fix the flakiness of the key quota for fs/crypto/, not
for other users of the keyrings service. Maybe this suggests that key_put()
should release the key's quota right away if the key's refcount drops to 0?
Either way, note that where fs/crypto/ does key_put() on a whole keyring at
once, it would first need to call keyring_clear() to clear it synchronously.
A third solution would be to make fs/crypto/ completely stop using 'struct key',
and handle quotas itself. It would do it correctly, i.e. synchronously so that
the results are predictable. This would likely mean separate accounting, where
adding an fscrypt key counts against an fscrypt key quota, not the regular
keyrings service quota as it does now. That should be fine, though the same
limits might still need to be used, in case users are relying on the sysctls...
The last solution seems quite attractive at this point, given the number of
times that issues in the keyrings service have caused problems for fs/crypto/.
Any thoughts are appreciated, though.
- Eric
next prev parent reply other threads:[~2023-12-07 2:43 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=20231207024323.GA1994@sol.localdomain \
--to=ebiggers@kernel.org \
--cc=dhowells@redhat.com \
--cc=jarkko@kernel.org \
--cc=keyrings@vger.kernel.org \
--cc=lhenriques@suse.de \
--cc=linux-kernel@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