From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795988483; b=HNMV/9pD5jnRsXcJUZQT3l1G/jbPRrUEi8XJHtK9DqzfPvOsxlzNPodptuFRRUpMY8QT rNggdSiwJheuE3d33XMjCxePkD3SHq76O4QQYznTUG/4UJsP4R0c6B1DDGmoaCgJzP28E lMEya8PBVCAMMF3kVviUJU8wK8Uqii24AvXQodswi2cJEZcLFAc3BskH1RM5t9JcZ441K yTB4DJvV6tl9Xr4NpZGlYrKhtW3O+SvvkbRMrsFv8dSwpstTG1p4DbigJN+ct63abKK6/ eV8QPW6GuhfrFTIQibOG0z5gLH1sz3aoTJj8tvaYB17WTiMXnNrSx5QhOn5/tx6sh3A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795988483; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=nY6zUm6Wj99LULzuy9o59+OtkWMuzjr6ZV01CGut4Eg=; b=bE8iOdec934pvlVxiqjC5v7PA1Nj/7UqSjzNidvjGROnnYNOTae7tEVaAqLeFSnSod7/ zj5slSN87othKTCgnP9FYdtXRZ4CoycVwdb49uHWdz5TsB4U8XG9neot6H9tlvHVkKudd PqA1QaCr7v1XVLIMfR1LkYBpH3iEzPpYtDeFGluHQUzcb9JlZ2Mn6fkEP5+2NL+wH4P9y P4b1aoPEY20OmKUejp1RTbjeWpcZs9ZJaQBl0iQP7v7oMrYkwX7eRZJNqEagec+bwws4I IZciCft6EloIngWQ4bbZYcooaQMPcXS/EVE7/UW66g5/guhnFYE9972/0wy/Ubdzc6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DNFwSlW2; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id DA862606A31C; Sun, 29 Nov 2026 21:41:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DNFwSlW2 Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id E57DA1F00899; Sun, 29 Nov 2026 21:41:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795988483; bh=nY6zUm6Wj99LULzuy9o59+OtkWMuzjr6ZV01CGut4Eg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DNFwSlW2R8eLTTtBf86ZQcTHxefB2tYHw3CVfqYRZ3TUeAwaWzHgpZ5adKXNVkUXv krCGNXvm8yKUpYNUOVblr2V03OFMINI7CfIjH/ryEgTOA9KbOzDpbgDy8aCifawzIn TgbTuf9rdSfJqLnhN9lATJl37ic9tHUsp2LGo7pzMWf52xS22fOJgAzaI/L0tdK9hk lck+Iewy5zXdr1fINFeLkWMLtykEEQsoaZ1+x73qhnCi0gkJhIzOhWXZ6UimjeZKu0 Fd+5bbZKgAHs+Pji8SNoDWD2afzzy0P0jqvxeg+elFx2swKdy3RAFvFm6538PPws/U vKi28jM0NNG+A== From: Jarkko Sakkinen To: keyrings@vger.kernel.org Cc: Jarkko Sakkinen , linux-doc@vger.kernel.org, linux-security-module@vger.kernel.org, David Howells , Guicai Ma , James Morris , Jann Horn , Jonathan Corbet , Paul Moore , Randy Dunlap , "Serge E. Hallyn" , Shuah Khan , stable@vger.kernel.org Subject: [PATCH 1/2] KEYS: Return user session keyring on lookup Date: Sun, 29 Nov 2026 23:41:13 +0200 Message-ID: <20261129214115.3043489-2-jarkko@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261129214115.3043489-1-jarkko@kernel.org> References: <20261129214115.3043489-1-jarkko@kernel.org> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In lookup_user_key(), looking up KEY_SPEC_SESSION_KEYRING without KEY_LOOKUP_CREATE installs the user session keyring into the task's credentials when no session keyring is present. Mutating credentials on a read-only lookup leaves credentials cached by other subsystems, such as file->f_cred, out of sync with the task. Return the user session keyring directly without installing it, and check the resolved keyring in keyctl_session_to_parent(). Cc: stable@vger.kernel.org # v5.10+ Fixes: d84f4f992cbd ("CRED: Inaugurate COW credentials") Suggested-by: Jann Horn Closes: https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/ Reviewed-by: Guicai Ma Tested-by: Guicai Ma Link: https://lore.kernel.org/r/20261127121433.2400766-1-jarkko@kernel.org Signed-off-by: Jarkko Sakkinen --- Documentation/security/keys/core.rst | 6 ++-- security/keys/keyctl.c | 5 ++-- security/keys/process_keys.c | 44 +++++++--------------------- 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/Documentation/security/keys/core.rst b/Documentation/security/keys/core.rst index 37ed42382f16..e3a215e55f81 100644 --- a/Documentation/security/keys/core.rst +++ b/Documentation/security/keys/core.rst @@ -165,8 +165,10 @@ The key service provides a number of features besides keys: When a process changes its real UID, if it used to have no session key, it will be subscribed to the default session key for the new UID. - If a process attempts to access its session key when it doesn't have one, - it will be subscribed to the default for its current UID. + 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. * 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/keyctl.c b/security/keys/keyctl.c index d6b10fd67627..b11716f88ded 100644 --- a/security/keys/keyctl.c +++ b/security/keys/keyctl.c @@ -1682,8 +1682,7 @@ long keyctl_session_to_parent(void) * there's no point */ mycred = current_cred(); pcred = __task_cred(parent); - if (mycred == pcred || - mycred->session_keyring == pcred->session_keyring) { + if (cred->session_keyring == pcred->session_keyring) { ret = 0; goto unlock; } @@ -1701,7 +1700,7 @@ long keyctl_session_to_parent(void) /* the keyrings must have the same UID */ if ((pcred->session_keyring && !uid_eq(pcred->session_keyring->uid, mycred->euid)) || - !uid_eq(mycred->session_keyring->uid, mycred->euid)) + !uid_eq(cred->session_keyring->uid, mycred->euid)) goto unlock; /* cancel an already pending keyring replacement */ diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index 3af71c6322af..cdf03b8124d0 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -356,31 +356,6 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring) return 0; } -/* - * Install the given keyring as the session keyring of the current task, - * replacing the existing one if any. If the given keyring is NULL, then - * install a new anonymous session keyring. - * - * Return: 0 on success; -errno on failure. - */ -static int install_session_keyring(struct key *keyring) -{ - struct cred *new; - int ret; - - new = prepare_creds(); - if (!new) - return -ENOMEM; - - ret = install_session_keyring_to_cred(new, keyring); - if (ret < 0) { - abort_creds(new); - return ret; - } - - return commit_creds(new); -} - /* * Handle the fsuid changing. */ @@ -720,20 +695,21 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, case KEY_SPEC_SESSION_KEYRING: if (!ctx.cred->session_keyring) { - /* always install a session keyring upon access if one - * doesn't exist yet */ ret = look_up_user_keyrings(NULL, &user_session); if (ret < 0) goto error; - if (lflags & KEY_LOOKUP_CREATE) + + if (lflags & KEY_LOOKUP_CREATE) { + key_put(user_session); ret = join_session_keyring(NULL); - else - ret = install_session_keyring(user_session); + if (ret < 0) + goto error; + goto reget_creds; + } - key_put(user_session); - if (ret < 0) - goto error; - goto reget_creds; + key = user_session; + key_ref = make_key_ref(key, 1); + break; } else if (test_bit(KEY_FLAG_UID_KEYRING, &ctx.cred->session_keyring->flags) && lflags & KEY_LOOKUP_CREATE) { -- 2.47.3