[RFC PATCH 2/2] keys: Reject keyring creation with overridden credentials
From: Jarkko Sakkinen
Date: Thu Sep 24 2026 - 01:58:48 EST
If the current task is running with overridden credentials calling
commit_creds() is forbidden and triggers a BUG_ON().
Reject keyring creation with -EPERM when credentials are overridden.
Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
Documentation/security/keys/core.rst | 3 ++-
security/keys/process_keys.c | 31 +++++++++++++++++++++++++++-
2 files changed, 32 insertions(+), 2 deletions(-)
diff --git a/Documentation/security/keys/core.rst b/Documentation/security/keys/core.rst
index c81a3a9fe236..9779ce71df47 100644
--- a/Documentation/security/keys/core.rst
+++ b/Documentation/security/keys/core.rst
@@ -168,7 +168,8 @@ The key service provides a number of features besides keys:
If a process attempts to access its session keyring when it doesn't have
one, the default user session keyring for its current UID is returned
without being installed into its credentials. If creation is requested,
- an anonymous session keyring is installed.
+ an anonymous session keyring is installed, failing with -EPERM if
+ credentials are overridden.
* Each user has two quotas against which the keys they own are tracked. One
limits the total number of keys and keyrings, the other limits the total
diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c
index a5aa056a6725..bd68dbc028e2 100644
--- a/security/keys/process_keys.c
+++ b/security/keys/process_keys.c
@@ -346,6 +346,14 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
return 0;
}
+/*
+ * Determine whether the current task is running with overridden credentials.
+ */
+static bool cred_overridden(void)
+{
+ return current_cred() != current_real_cred();
+}
+
/*
* Handle the fsuid changing.
*/
@@ -578,7 +586,8 @@ bool lookup_user_key_possessed(const struct key *key,
* to a key or the best found key was a negative key; -EKEYREVOKED or
* -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
* found key doesn't grant the requested permit or the LSM denied access to it;
- * or -ENOMEM if a special keyring couldn't be created.
+ * -ENOMEM if a special keyring couldn't be created; or -EPERM if creating one
+ * while credentials are overridden.
*
* In the case of a successful return, the possession attribute is set on the
* returned key reference.
@@ -607,6 +616,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
if (!(lflags & KEY_LOOKUP_CREATE))
goto error;
+ if (cred_overridden()) {
+ key_ref = ERR_PTR(-EPERM);
+ goto error;
+ }
+
ret = install_thread_keyring();
if (ret < 0) {
key_ref = ERR_PTR(ret);
@@ -625,6 +639,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
if (!(lflags & KEY_LOOKUP_CREATE))
goto error;
+ if (cred_overridden()) {
+ key_ref = ERR_PTR(-EPERM);
+ goto error;
+ }
+
ret = install_process_keyring();
if (ret < 0) {
key_ref = ERR_PTR(ret);
@@ -646,6 +665,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
if (lflags & KEY_LOOKUP_CREATE) {
key_put(user_session);
+ if (cred_overridden()) {
+ key_ref = ERR_PTR(-EPERM);
+ goto error;
+ }
+
ret = join_session_keyring(NULL);
if (ret < 0)
goto error;
@@ -658,6 +682,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
} else if (test_bit(KEY_FLAG_UID_KEYRING,
&ctx.cred->session_keyring->flags) &&
lflags & KEY_LOOKUP_CREATE) {
+ if (cred_overridden()) {
+ key_ref = ERR_PTR(-EPERM);
+ goto error;
+ }
+
ret = join_session_keyring(NULL);
if (ret < 0)
goto error;
--
2.47.3