Skip to contentSkip to conversation

KEYS: Fix race with concurrent install_user_keyrings()

PATCH In review David Howells 5 participants 8 messages Mar 6, 2013 – Mar 12, 2013

Subsystems (2)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH 1/1 KEYS: Fix race with concurrent install_user_keyrings()

This fixes CVE-2013-1792.

There is a race in install_user_keyrings() that can cause a NULL pointer dereference when called concurrently for the same user if the uid and uid-session keyrings are not yet created. It might be possible for an unprivileged user to trigger this by calling keyctl() from userspace in parallel immediately after logging in.

Assume that we have two threads both executing lookup_user_key(), both looking for KEY_SPEC_USER_SESSION_KEYRING.

	THREAD A			THREAD B
	===============================	===============================
					==>call install_user_keyrings();
	if (!cred->user->session_keyring)
	==>call install_user_keyrings()
					...
					user->uid_keyring = uid_keyring;
	if (user->uid_keyring)
		return 0;
	<==
	key = cred->user->session_keyring [== NULL]
					user->session_keyring = session_keyring;
	atomic_inc(&key->usage); [oops]

At the point thread A dereferences cred->user->session_keyring, thread B hasn't updated user->session_keyring yet, but thread A assumes it is populated because install_user_keyrings() returned ok.

The race window is really small but can be exploited if, for example, thread B is interrupted or preempted after initializing uid_keyring, but before doing setting session_keyring.

This couldn't be reproduced on a stock kernel. However, after placing systemtap probe on 'user->session_keyring = session_keyring;' that introduced some delay, the kernel could be crashed reliably.

Fix this by checking both pointers before deciding whether to return. Alternatively, the test could be done away with entirely as it is checked inside the mutex - but since the mutex is global, that may not be the best way.

---

 security/keys/process_keys.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
security/keys/process_keys.c +1 -1
index 58dfe08..c5ec083 100644
@@ -57,7 +57,7 @@ int install_user_keyrings(void)
 
 	kenter("%p{%u}", user, uid);
 
-	if (user->uid_keyring) {
+	if (user->uid_keyring && user->session_keyring) {
 		kleave(" = 0 [exist]");
 		return 0;
 	}
#3 lore raw
Quoted 10 lines · On Mon, 11 Mar 2013 14:02:11 -0700 Greg KH <[email protected]> wrote:
Quoted 6 lines · James Morris
Quoted 2 lines · David Howells

What happened to this patch? I don't see it in Linus's tree, James, did you pick it up?

James has acked it. I have it queued for processing so it isn't lost. It has no cc:stable's in it, but David always forgets that ;)

#4 lore raw
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 11 Mar 2013 14:02:11 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · James Morris
Quoted 2 lines · David Howells
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What happened to this patch? I don't see it in Linus's tree, James, did you pick it up?

James has acked it. I have it queued for processing so it isn't lost.

Ok, I'll keep a lookout for it.

Quoted 1 line

It has no cc:stable's in it, but David always forgets that ;)

Yeah, I'm used to that :)

#5 lore raw
In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 11 Mar 2013 14:02:11 -0700 Greg KH <[email protected]> wrote:
Quoted 5 lines · James Morris
Quoted 2 lines · David Howells
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

What happened to this patch? I don't see it in Linus's tree, James, did you pick it up?

James has acked it. I have it queued for processing so it isn't lost. It has no cc:stable's in it, but David always forgets that ;)

I usually expect that if the subsystem maintainer is involved, he's the one to pick it up.

If James' ack was "Linus, please take this directly, I'm busy and have nothing else pending", then I really would prefer to hear that exact thing. Otherwise I'm just going to ignore the patch "safe" in the knowledge that clearly the subsystem maintainer is aware of it.

Linus

author maintainer
#6 lore raw
In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

James has acked it. I have it queued for processing so it isn't lost. It has no cc:stable's in it, but David always forgets that ;)

Hmmm... I did try and resend it there on the 7th. My mail client records that it did so, but I don't see it in the list archive and I didn't get any bounce notifications:-/

David