Skip to content

keys: Fix quota-owner reads and ownership accounting

Chengfeng Ye keyrings linux-security-module lkml stable 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v4 Latest2 patches 8 repliesIn review

Changes in v4 from the cover letter

- Keep the v3 topic order: lifetime/namespace readers in patch 1, accounting in patch 2. Both fixes remain independently applicable.
- Put every accounting critical section wholly in patch 2.
- Make patch 1 independent using existing lifetime locking plus explicit pointer access marking.
- Explain the root cause and every critical section, plus the existing protection at unchanged sites. Keep allocation, notifications and reference release outside the new accounting scopes.
- Correct the reader patch's Fixes tags to the namespace-filter commits.
- Rebase onto for-next-keys at 22430ae5d90ab and request fresh review.

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2keys: Protect quota-owner reads in namespace filterssecurity/keys/keyctl.c security/keys/keyring.c security/keys/proc.c+20 -44 replies
2/2keys: Serialize ownership transfers with key accountingsecurity/keys/key.c security/keys/keyctl.c+17 -23 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [PATCH v4 0/2] keys: Fix quota-owner reads and ownership accounting

This revision keeps the v3 topic order: the lifetime/namespace-reader fix is patch 1, and ownership accounting is patch 2. Each patch can be applied independently to the base, and both orders produce the same final tree.

Patch 1 protects only the three quota-owner UID snapshots. It relies on key_user_lock already held by lookup and final reference removal. Its READ_ONCE() pointer loads pair with a WRITE_ONCE() replacement in chown. The reference-release ordering and lock handoff keep a sampled owner alive; this does not require patch 2's new accounting critical section. The commit message gives the lifetime argument, including a final put by another task.

Patch 2 serializes chown with quota reservation and positive and negative instantiation. Each critical section is complete within this commit. Atomic counter operations alone do not serialize owner selection and count/state publication with chown, and instantiation does not require the target key's semaphore.

The messages explain why keeping a key alive does not pin its former quota owner, and why key->sem does not exclude instantiation. They map those two requirements to the three reader scopes and four accounting scopes, and explain the existing protection at unchanged lifecycle sites. All seven scopes in the combined v3 series are retained. Reservation and instantiation locking move from v3 1/2 into the complete accounting fix in v4 2/2; no lock scope is split between commits.

The procfs changes cover the originally reported proc_keys_start() fault. The filters continue to use the quota-owner UID, since key_fsuid_changed() can change a thread keyring's key->uid without moving its quota owner.

Changes in v4:
- Keep the v3 topic order: lifetime/namespace readers in patch 1, accounting in patch 2. Both fixes remain independently applicable.
- Put every accounting critical section wholly in patch 2.
- Make patch 1 independent using existing lifetime locking plus explicit pointer access marking.
- Explain the root cause and every critical section, plus the existing protection at unchanged sites. Keep allocation, notifications and reference release outside the new accounting scopes.
- Correct the reader patch's Fixes tags to the namespace-filter commits.
- Rebase onto for-next-keys at 22430ae5d90ab and request fresh review.

Validation:
- Built security/keys/ with patch 1 only, patch 2 only, and both patches. KEYS, PROC_FS, USER_NS, KEY_NOTIFICATIONS, BIG_KEYS, ENCRYPTED_KEYS and PROVE_LOCKING were enabled. No compiler warnings or errors.
- Both exported patches pass checkpatch --strict with zero diagnostics.
- Both patches apply directly to the base. Ordered git am in either order reproduces the expected intermediate and final trees.
- No new runtime, lockdep or Sashiko run. The KASAN excerpt is historical.

OpenAI Codex assisted with the review audit, series split, pointer access marking, commit messages and build checks. The existing author sign-off is retained from v3 for human review before submission.

Previous revisions:
v3: https://lore.kernel.org/r/[email protected]/
v2: https://lore.kernel.org/r/[email protected]/
v1: https://lore.kernel.org/r/[email protected]/
Review:
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/r/[email protected]/
https://lore.kernel.org/r/[email protected]/

Chengfeng Ye (2):
keys: Protect quota-owner reads in namespace filters
keys: Serialize ownership transfers with key accounting

 security/keys/key.c     | 15 +++++++++++++--
 security/keys/keyctl.c  |  6 +++++-
 security/keys/keyring.c |  7 ++++++-
 security/keys/proc.c    | 15 +++++++++++++--
 4 files changed, 37 insertions(+), 6 deletions(-)

base-commit: 22430ae5d90ab288b0ee2ad99ae941f4a666b694

Signature · 1 line
2.43.0