Skip to content

Commit 9a56c2d

Browse files
committed
userns: Convert security/keys to the new userns infrastructure
- Replace key_user ->user_ns equality checks with kuid_has_mapping checks. - Use from_kuid to generate key descriptions - Use kuid_t and kgid_t and the associated helpers instead of uid_t and gid_t - Avoid potential problems with file descriptor passing by displaying keys in the user namespace of the opener of key status proc files. Cc: linux-security-module@vger.kernel.org Cc: keyrings@linux-nfs.org Cc: David Howells <dhowells@redhat.com> Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent 5fce5e0 commit 9a56c2d

10 files changed

Lines changed: 84 additions & 88 deletions

File tree

‎include/linux/key.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/atomic.h>
2525

2626
#ifdef __KERNEL__
27+
#include <linux/uidgid.h>
2728

2829
/* key handle serial number */
2930
typedef int32_t key_serial_t;
@@ -137,8 +138,8 @@ struct key {
137138
time_t revoked_at; /* time at which key was revoked */
138139
};
139140
time_t last_used_at; /* last time used for LRU keyring discard */
140-
uid_t uid;
141-
gid_t gid;
141+
kuid_t uid;
142+
kgid_t gid;
142143
key_perm_t perm; /* access permissions */
143144
unsigned short quotalen; /* length added to quota */
144145
unsigned short datalen; /* payload data length
@@ -193,7 +194,7 @@ struct key {
193194

194195
extern struct key *key_alloc(struct key_type *type,
195196
const char *desc,
196-
uid_t uid, gid_t gid,
197+
kuid_t uid, kgid_t gid,
197198
const struct cred *cred,
198199
key_perm_t perm,
199200
unsigned long flags);
@@ -262,7 +263,7 @@ extern int key_link(struct key *keyring,
262263
extern int key_unlink(struct key *keyring,
263264
struct key *key);
264265

265-
extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
266+
extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
266267
const struct cred *cred,
267268
unsigned long flags,
268269
struct key *dest);

‎init/Kconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,6 @@ config UIDGID_CONVERTED
927927
# Features
928928
depends on IMA = n
929929
depends on EVM = n
930-
depends on KEYS = n
931930
depends on AUDIT = n
932931
depends on AUDITSYSCALL = n
933932
depends on TASKSTATS = n

‎security/keys/internal.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ struct key_user {
5252
atomic_t usage; /* for accessing qnkeys & qnbytes */
5353
atomic_t nkeys; /* number of keys */
5454
atomic_t nikeys; /* number of instantiated keys */
55-
uid_t uid;
56-
struct user_namespace *user_ns;
55+
kuid_t uid;
5756
int qnkeys; /* number of keys allocated to this user */
5857
int qnbytes; /* number of bytes allocated to this user */
5958
};
@@ -62,8 +61,7 @@ extern struct rb_root key_user_tree;
6261
extern spinlock_t key_user_lock;
6362
extern struct key_user root_key_user;
6463

65-
extern struct key_user *key_user_lookup(uid_t uid,
66-
struct user_namespace *user_ns);
64+
extern struct key_user *key_user_lookup(kuid_t uid);
6765
extern void key_user_put(struct key_user *user);
6866

6967
/*

‎security/keys/key.c‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <linux/workqueue.h>
1919
#include <linux/random.h>
2020
#include <linux/err.h>
21-
#include <linux/user_namespace.h>
2221
#include "internal.h"
2322

2423
struct kmem_cache *key_jar;
@@ -52,7 +51,7 @@ void __key_check(const struct key *key)
5251
* Get the key quota record for a user, allocating a new record if one doesn't
5352
* already exist.
5453
*/
55-
struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
54+
struct key_user *key_user_lookup(kuid_t uid)
5655
{
5756
struct key_user *candidate = NULL, *user;
5857
struct rb_node *parent = NULL;
@@ -67,13 +66,9 @@ struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
6766
parent = *p;
6867
user = rb_entry(parent, struct key_user, node);
6968

70-
if (uid < user->uid)
69+
if (uid_lt(uid, user->uid))
7170
p = &(*p)->rb_left;
72-
else if (uid > user->uid)
73-
p = &(*p)->rb_right;
74-
else if (user_ns < user->user_ns)
75-
p = &(*p)->rb_left;
76-
else if (user_ns > user->user_ns)
71+
else if (uid_gt(uid, user->uid))
7772
p = &(*p)->rb_right;
7873
else
7974
goto found;
@@ -102,7 +97,6 @@ struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
10297
atomic_set(&candidate->nkeys, 0);
10398
atomic_set(&candidate->nikeys, 0);
10499
candidate->uid = uid;
105-
candidate->user_ns = get_user_ns(user_ns);
106100
candidate->qnkeys = 0;
107101
candidate->qnbytes = 0;
108102
spin_lock_init(&candidate->lock);
@@ -131,7 +125,6 @@ void key_user_put(struct key_user *user)
131125
if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
132126
rb_erase(&user->node, &key_user_tree);
133127
spin_unlock(&key_user_lock);
134-
put_user_ns(user->user_ns);
135128

136129
kfree(user);
137130
}
@@ -229,7 +222,7 @@ static inline void key_alloc_serial(struct key *key)
229222
* key_alloc() calls don't race with module unloading.
230223
*/
231224
struct key *key_alloc(struct key_type *type, const char *desc,
232-
uid_t uid, gid_t gid, const struct cred *cred,
225+
kuid_t uid, kgid_t gid, const struct cred *cred,
233226
key_perm_t perm, unsigned long flags)
234227
{
235228
struct key_user *user = NULL;
@@ -253,16 +246,16 @@ struct key *key_alloc(struct key_type *type, const char *desc,
253246
quotalen = desclen + type->def_datalen;
254247

255248
/* get hold of the key tracking for this user */
256-
user = key_user_lookup(uid, cred->user_ns);
249+
user = key_user_lookup(uid);
257250
if (!user)
258251
goto no_memory_1;
259252

260253
/* check that the user's quota permits allocation of another key and
261254
* its description */
262255
if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
263-
unsigned maxkeys = (uid == 0) ?
256+
unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
264257
key_quota_root_maxkeys : key_quota_maxkeys;
265-
unsigned maxbytes = (uid == 0) ?
258+
unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
266259
key_quota_root_maxbytes : key_quota_maxbytes;
267260

268261
spin_lock(&user->lock);
@@ -380,7 +373,7 @@ int key_payload_reserve(struct key *key, size_t datalen)
380373

381374
/* contemplate the quota adjustment */
382375
if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
383-
unsigned maxbytes = (key->user->uid == 0) ?
376+
unsigned maxbytes = uid_eq(key->user->uid, GLOBAL_ROOT_UID) ?
384377
key_quota_root_maxbytes : key_quota_maxbytes;
385378

386379
spin_lock(&key->user->lock);

‎security/keys/keyctl.c‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,8 @@ long keyctl_describe_key(key_serial_t keyid,
569569
ret = snprintf(tmpbuf, PAGE_SIZE - 1,
570570
"%s;%d;%d;%08x;%s",
571571
key->type->name,
572-
key->uid,
573-
key->gid,
572+
from_kuid_munged(current_user_ns(), key->uid),
573+
from_kgid_munged(current_user_ns(), key->gid),
574574
key->perm,
575575
key->description ?: "");
576576

@@ -766,15 +766,25 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
766766
*
767767
* If successful, 0 will be returned.
768768
*/
769-
long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
769+
long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
770770
{
771771
struct key_user *newowner, *zapowner = NULL;
772772
struct key *key;
773773
key_ref_t key_ref;
774774
long ret;
775+
kuid_t uid;
776+
kgid_t gid;
777+
778+
uid = make_kuid(current_user_ns(), user);
779+
gid = make_kgid(current_user_ns(), group);
780+
ret = -EINVAL;
781+
if ((user != (uid_t) -1) && !uid_valid(uid))
782+
goto error;
783+
if ((group != (gid_t) -1) && !gid_valid(gid))
784+
goto error;
775785

776786
ret = 0;
777-
if (uid == (uid_t) -1 && gid == (gid_t) -1)
787+
if (user == (uid_t) -1 && group == (gid_t) -1)
778788
goto error;
779789

780790
key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
@@ -792,27 +802,27 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
792802

793803
if (!capable(CAP_SYS_ADMIN)) {
794804
/* only the sysadmin can chown a key to some other UID */
795-
if (uid != (uid_t) -1 && key->uid != uid)
805+
if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
796806
goto error_put;
797807

798808
/* only the sysadmin can set the key's GID to a group other
799809
* than one of those that the current process subscribes to */
800-
if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
810+
if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
801811
goto error_put;
802812
}
803813

804814
/* change the UID */
805-
if (uid != (uid_t) -1 && uid != key->uid) {
815+
if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
806816
ret = -ENOMEM;
807-
newowner = key_user_lookup(uid, current_user_ns());
817+
newowner = key_user_lookup(uid);
808818
if (!newowner)
809819
goto error_put;
810820

811821
/* transfer the quota burden to the new user */
812822
if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
813-
unsigned maxkeys = (uid == 0) ?
823+
unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
814824
key_quota_root_maxkeys : key_quota_maxkeys;
815-
unsigned maxbytes = (uid == 0) ?
825+
unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
816826
key_quota_root_maxbytes : key_quota_maxbytes;
817827

818828
spin_lock(&newowner->lock);
@@ -846,7 +856,7 @@ long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
846856
}
847857

848858
/* change the GID */
849-
if (gid != (gid_t) -1)
859+
if (group != (gid_t) -1)
850860
key->gid = gid;
851861

852862
ret = 0;
@@ -897,7 +907,7 @@ long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
897907
down_write(&key->sem);
898908

899909
/* if we're not the sysadmin, we can only change a key that we own */
900-
if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
910+
if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
901911
key->perm = perm;
902912
ret = 0;
903913
}
@@ -1507,18 +1517,18 @@ long keyctl_session_to_parent(void)
15071517

15081518
/* the parent must have the same effective ownership and mustn't be
15091519
* SUID/SGID */
1510-
if (pcred->uid != mycred->euid ||
1511-
pcred->euid != mycred->euid ||
1512-
pcred->suid != mycred->euid ||
1513-
pcred->gid != mycred->egid ||
1514-
pcred->egid != mycred->egid ||
1515-
pcred->sgid != mycred->egid)
1520+
if (!uid_eq(pcred->uid, mycred->euid) ||
1521+
!uid_eq(pcred->euid, mycred->euid) ||
1522+
!uid_eq(pcred->suid, mycred->euid) ||
1523+
!gid_eq(pcred->gid, mycred->egid) ||
1524+
!gid_eq(pcred->egid, mycred->egid) ||
1525+
!gid_eq(pcred->sgid, mycred->egid))
15161526
goto unlock;
15171527

15181528
/* the keyrings must have the same UID */
15191529
if ((pcred->tgcred->session_keyring &&
1520-
pcred->tgcred->session_keyring->uid != mycred->euid) ||
1521-
mycred->tgcred->session_keyring->uid != mycred->euid)
1530+
!uid_eq(pcred->tgcred->session_keyring->uid, mycred->euid)) ||
1531+
!uid_eq(mycred->tgcred->session_keyring->uid, mycred->euid))
15221532
goto unlock;
15231533

15241534
/* cancel an already pending keyring replacement */

‎security/keys/keyring.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static long keyring_read(const struct key *keyring,
256256
/*
257257
* Allocate a keyring and link into the destination keyring.
258258
*/
259-
struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
259+
struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
260260
const struct cred *cred, unsigned long flags,
261261
struct key *dest)
262262
{
@@ -612,7 +612,7 @@ struct key *find_keyring_by_name(const char *name, bool skip_perm_check)
612612
&keyring_name_hash[bucket],
613613
type_data.link
614614
) {
615-
if (keyring->user->user_ns != current_user_ns())
615+
if (!kuid_has_mapping(current_user_ns(), keyring->user->uid))
616616
continue;
617617

618618
if (test_bit(KEY_FLAG_REVOKED, &keyring->flags))

‎security/keys/permission.c‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,27 @@ int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
3636

3737
key = key_ref_to_ptr(key_ref);
3838

39-
if (key->user->user_ns != cred->user_ns)
40-
goto use_other_perms;
41-
4239
/* use the second 8-bits of permissions for keys the caller owns */
43-
if (key->uid == cred->fsuid) {
40+
if (uid_eq(key->uid, cred->fsuid)) {
4441
kperm = key->perm >> 16;
4542
goto use_these_perms;
4643
}
4744

4845
/* use the third 8-bits of permissions for keys the caller has a group
4946
* membership in common with */
50-
if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
51-
if (key->gid == cred->fsgid) {
47+
if (gid_valid(key->gid) && key->perm & KEY_GRP_ALL) {
48+
if (gid_eq(key->gid, cred->fsgid)) {
5249
kperm = key->perm >> 8;
5350
goto use_these_perms;
5451
}
5552

56-
ret = groups_search(cred->group_info,
57-
make_kgid(current_user_ns(), key->gid));
53+
ret = groups_search(cred->group_info, key->gid);
5854
if (ret) {
5955
kperm = key->perm >> 8;
6056
goto use_these_perms;
6157
}
6258
}
6359

64-
use_other_perms:
65-
6660
/* otherwise use the least-significant 8-bits */
6761
kperm = key->perm;
6862

0 commit comments

Comments
 (0)