Skip to content

Commit d9297fb

Browse files
ebiggerspopcornmix
authored andcommitted
KEYS: prevent creating a different user's keyrings
commit 237bbd2 upstream. It was possible for an unprivileged user to create the user and user session keyrings for another user. For example: sudo -u '#3000' sh -c 'keyctl add keyring _uid.4000 "" @U keyctl add keyring _uid_ses.4000 "" @U sleep 15' & sleep 1 sudo -u '#4000' keyctl describe @U sudo -u '#4000' keyctl describe @us This is problematic because these "fake" keyrings won't have the right permissions. In particular, the user who created them first will own them and will have full access to them via the possessor permissions, which can be used to compromise the security of a user's keys: -4: alswrv-----v------------ 3000 0 keyring: _uid.4000 -5: alswrv-----v------------ 3000 0 keyring: _uid_ses.4000 Fix it by marking user and user session keyrings with a flag KEY_FLAG_UID_KEYRING. Then, when searching for a user or user session keyring by name, skip all keyrings that don't have the flag set. Fixes: 69664cf ("keys: don't generate user and user session keyrings unless they're accessed") Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cadb3d4 commit d9297fb

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

include/linux/key.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ struct key {
176176
#define KEY_FLAG_BUILTIN 8 /* set if key is built in to the kernel */
177177
#define KEY_FLAG_ROOT_CAN_INVAL 9 /* set if key can be invalidated by root without permission */
178178
#define KEY_FLAG_KEEP 10 /* set if key should not be removed */
179+
#define KEY_FLAG_UID_KEYRING 11 /* set if key is a user or user session keyring */
179180

180181
/* the key type and key description string
181182
* - the desc is used to match a key against search criteria
@@ -235,6 +236,7 @@ extern struct key *key_alloc(struct key_type *type,
235236
#define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
236237
#define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */
237238
#define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */
239+
#define KEY_ALLOC_UID_KEYRING 0x0010 /* allocating a user or user session keyring */
238240

239241
extern void key_revoke(struct key *key);
240242
extern void key_invalidate(struct key *key);

security/keys/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
137137
extern key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx);
138138
extern key_ref_t search_process_keyrings(struct keyring_search_context *ctx);
139139

140-
extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
140+
extern struct key *find_keyring_by_name(const char *name, bool uid_keyring);
141141

142142
extern int install_user_keyrings(void);
143143
extern int install_thread_keyring_to_cred(struct cred *);

security/keys/key.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ struct key *key_alloc(struct key_type *type, const char *desc,
301301
key->flags |= 1 << KEY_FLAG_IN_QUOTA;
302302
if (flags & KEY_ALLOC_BUILT_IN)
303303
key->flags |= 1 << KEY_FLAG_BUILTIN;
304+
if (flags & KEY_ALLOC_UID_KEYRING)
305+
key->flags |= 1 << KEY_FLAG_UID_KEYRING;
304306

305307
#ifdef KEY_DEBUGGING
306308
key->magic = KEY_DEBUG_MAGIC;

security/keys/keyring.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -985,15 +985,15 @@ key_ref_t find_key_to_update(key_ref_t keyring_ref,
985985
/*
986986
* Find a keyring with the specified name.
987987
*
988-
* All named keyrings in the current user namespace are searched, provided they
989-
* grant Search permission directly to the caller (unless this check is
990-
* skipped). Keyrings whose usage points have reached zero or who have been
991-
* revoked are skipped.
988+
* Only keyrings that have nonzero refcount, are not revoked, and are owned by a
989+
* user in the current user namespace are considered. If @uid_keyring is %true,
990+
* the keyring additionally must have been allocated as a user or user session
991+
* keyring; otherwise, it must grant Search permission directly to the caller.
992992
*
993993
* Returns a pointer to the keyring with the keyring's refcount having being
994994
* incremented on success. -ENOKEY is returned if a key could not be found.
995995
*/
996-
struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
996+
struct key *find_keyring_by_name(const char *name, bool uid_keyring)
997997
{
998998
struct key *keyring;
999999
int bucket;
@@ -1021,10 +1021,15 @@ struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
10211021
if (strcmp(keyring->description, name) != 0)
10221022
continue;
10231023

1024-
if (!skip_perm_check &&
1025-
key_permission(make_key_ref(keyring, 0),
1026-
KEY_NEED_SEARCH) < 0)
1027-
continue;
1024+
if (uid_keyring) {
1025+
if (!test_bit(KEY_FLAG_UID_KEYRING,
1026+
&keyring->flags))
1027+
continue;
1028+
} else {
1029+
if (key_permission(make_key_ref(keyring, 0),
1030+
KEY_NEED_SEARCH) < 0)
1031+
continue;
1032+
}
10281033

10291034
/* we've got a match but we might end up racing with
10301035
* key_cleanup() if the keyring is currently 'dead'

security/keys/process_keys.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ int install_user_keyrings(void)
7676
if (IS_ERR(uid_keyring)) {
7777
uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
7878
cred, user_keyring_perm,
79-
KEY_ALLOC_IN_QUOTA,
79+
KEY_ALLOC_UID_KEYRING |
80+
KEY_ALLOC_IN_QUOTA,
8081
NULL, NULL);
8182
if (IS_ERR(uid_keyring)) {
8283
ret = PTR_ERR(uid_keyring);
@@ -93,7 +94,8 @@ int install_user_keyrings(void)
9394
session_keyring =
9495
keyring_alloc(buf, user->uid, INVALID_GID,
9596
cred, user_keyring_perm,
96-
KEY_ALLOC_IN_QUOTA,
97+
KEY_ALLOC_UID_KEYRING |
98+
KEY_ALLOC_IN_QUOTA,
9799
NULL, NULL);
98100
if (IS_ERR(session_keyring)) {
99101
ret = PTR_ERR(session_keyring);

0 commit comments

Comments
 (0)