linux/security/keys
David Howells 21279cfa10 KEYS: get_instantiation_keyring() should inc the keyring refcount in all cases
The destination keyring specified to request_key() and co. is made available to
the process that instantiates the key (the slave process started by
/sbin/request-key typically).  This is passed in the request_key_auth struct as
the dest_keyring member.

keyctl_instantiate_key and keyctl_negate_key() call get_instantiation_keyring()
to get the keyring to attach the newly constructed key to at the end of
instantiation.  This may be given a specific keyring into which a link will be
made later, or it may be asked to find the keyring passed to request_key().  In
the former case, it returns a keyring with the refcount incremented by
lookup_user_key(); in the latter case, it returns the keyring from the
request_key_auth struct - and does _not_ increment the refcount.

The latter case will eventually result in an oops when the keyring prematurely
runs out of references and gets destroyed.  The effect may take some time to
show up as the key is destroyed lazily.

To fix this, the keyring returned by get_instantiation_keyring() must always
have its refcount incremented, no matter where it comes from.

This can be tested by setting /etc/request-key.conf to:

#OP	TYPE	DESCRIPTION	CALLOUT INFO	PROGRAM ARG1 ARG2 ARG3 ...
#======	=======	===============	===============	===============================
create  *	test:*		*		|/bin/false %u %g %d %{user:_display}
negate	*	*		*		/bin/keyctl negate %k 10 @u

and then doing:

	keyctl add user _display aaaaaaaa @u
        while keyctl request2 user test:x test:x @u &&
        keyctl list @u;
        do
                keyctl request2 user test:x test:x @u;
                sleep 31;
                keyctl list @u;
        done

which will oops eventually.  Changing the negate line to have @u rather than
%S at the end is important as that forces the latter case by passing a special
keyring ID rather than an actual keyring ID.

Reported-by: Alexander Zangerl <az@bond.edu.au>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Alexander Zangerl <az@bond.edu.au>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2009-10-15 15:19:58 -07:00
..
Makefile KEYS: Add garbage collection for dead, revoked and expired keys. [try #6] 2009-09-02 21:29:11 +10:00
compat.c KEYS: Add a keyctl to install a process's session keyring on its parent [try #6] 2009-09-02 21:29:22 +10:00
gc.c KEYS: Have the garbage collector set its timer for live expired keys 2009-09-23 11:03:47 -07:00
internal.h KEYS: Add a keyctl to install a process's session keyring on its parent [try #6] 2009-09-02 21:29:22 +10:00
key.c KEYS: Fix garbage collector 2009-09-15 09:11:02 +10:00
keyctl.c KEYS: get_instantiation_keyring() should inc the keyring refcount in all cases 2009-10-15 15:19:58 -07:00
keyring.c KEYS: Fix garbage collector 2009-09-15 09:11:02 +10:00
permission.c keys: consider user namespace in key_permission 2009-02-27 12:35:09 +11:00
proc.c KEYS: Do some whitespace cleanups [try #6] 2009-09-02 21:29:16 +10:00
process_keys.c KEYS: Add a keyctl to install a process's session keyring on its parent [try #6] 2009-09-02 21:29:22 +10:00
request_key.c keys: Handle there being no fallback destination keyring for request_key() 2009-04-09 10:41:19 -07:00
request_key_auth.c CRED: Inaugurate COW credentials 2008-11-14 10:39:23 +11:00
sysctl.c KEYS: Add garbage collection for dead, revoked and expired keys. [try #6] 2009-09-02 21:29:11 +10:00
user_defined.c [PATCH] remove many unneeded #includes of sched.h 2007-02-14 08:09:54 -08:00