1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* General persistent per-UID keyrings register
4 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/user_namespace.h>
9 #include <linux/cred.h>
13 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
15 static struct key_acl persistent_register_keyring_acl = {
16 .usage = REFCOUNT_INIT(1),
19 KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
20 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
24 static struct key_acl persistent_keyring_acl = {
25 .usage = REFCOUNT_INIT(1),
27 .possessor_viewable = true,
29 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
30 KEY_ACE_SEARCH | KEY_ACE_LINK |
31 KEY_ACE_CLEAR | KEY_ACE_INVAL),
32 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
37 * Create the persistent keyring register for the current user namespace.
39 * Called with the namespace's sem locked for writing.
41 static int key_create_persistent_register(struct user_namespace *ns)
43 struct key *reg = keyring_alloc(".persistent_register",
44 KUIDT_INIT(0), KGIDT_INIT(0),
46 &persistent_register_keyring_acl,
47 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
51 ns->persistent_keyring_register = reg;
56 * Create the persistent keyring for the specified user.
58 * Called with the namespace's sem locked for writing.
60 static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
61 struct keyring_index_key *index_key)
63 struct key *persistent;
64 key_ref_t reg_ref, persistent_ref;
66 if (!ns->persistent_keyring_register) {
67 long err = key_create_persistent_register(ns);
71 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
72 persistent_ref = find_key_to_update(reg_ref, index_key);
74 return persistent_ref;
77 persistent = keyring_alloc(index_key->description,
78 uid, INVALID_GID, current_cred(),
79 &persistent_keyring_acl,
80 KEY_ALLOC_NOT_IN_QUOTA, NULL,
81 ns->persistent_keyring_register);
82 if (IS_ERR(persistent))
83 return ERR_CAST(persistent);
85 return make_key_ref(persistent, true);
89 * Get the persistent keyring for a specific UID and link it to the nominated
92 static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
95 struct keyring_index_key index_key;
96 struct key *persistent;
97 key_ref_t reg_ref, persistent_ref;
101 /* Look in the register if it exists */
102 memset(&index_key, 0, sizeof(index_key));
103 index_key.type = &key_type_keyring;
104 index_key.description = buf;
105 index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
106 key_set_index_key(&index_key);
108 if (ns->persistent_keyring_register) {
109 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
110 down_read(&ns->keyring_sem);
111 persistent_ref = find_key_to_update(reg_ref, &index_key);
112 up_read(&ns->keyring_sem);
118 /* It wasn't in the register, so we'll need to create it. We might
119 * also need to create the register.
121 down_write(&ns->keyring_sem);
122 persistent_ref = key_create_persistent(ns, uid, &index_key);
123 up_write(&ns->keyring_sem);
124 if (!IS_ERR(persistent_ref))
127 return PTR_ERR(persistent_ref);
130 ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
132 persistent = key_ref_to_ptr(persistent_ref);
133 ret = key_link(key_ref_to_ptr(dest_ref), persistent);
135 key_set_timeout(persistent, persistent_keyring_expiry);
136 ret = persistent->serial;
140 key_ref_put(persistent_ref);
145 * Get the persistent keyring for a specific UID and link it to the nominated
148 long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
150 struct user_namespace *ns = current_user_ns();
155 /* -1 indicates the current user */
156 if (_uid == (uid_t)-1) {
159 uid = make_kuid(ns, _uid);
163 /* You can only see your own persistent cache if you're not
164 * sufficiently privileged.
166 if (!uid_eq(uid, current_uid()) &&
167 !uid_eq(uid, current_euid()) &&
168 !ns_capable(ns, CAP_SETUID))
172 /* There must be a destination keyring */
173 dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
174 if (IS_ERR(dest_ref))
175 return PTR_ERR(dest_ref);
176 if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
181 ret = key_get_persistent(ns, uid, dest_ref);
184 key_ref_put(dest_ref);