blob: 8171c90d4c9a793abdc86f4bc3ff470582f5a2a9 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsf36f8c72013-09-24 10:35:19 +01002/* General persistent per-UID keyrings register
3 *
4 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsf36f8c72013-09-24 10:35:19 +01006 */
7
8#include <linux/user_namespace.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01009#include <linux/cred.h>
10
David Howellsf36f8c72013-09-24 10:35:19 +010011#include "internal.h"
12
13unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
14
David Howells2e122562019-06-27 23:03:07 +010015static struct key_acl persistent_register_keyring_acl = {
16 .usage = REFCOUNT_INIT(1),
17 .nr_ace = 2,
18 .aces = {
19 KEY_POSSESSOR_ACE(KEY_ACE_SEARCH | KEY_ACE_WRITE),
20 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
21 }
22};
23
24static struct key_acl persistent_keyring_acl = {
25 .usage = REFCOUNT_INIT(1),
26 .nr_ace = 2,
27 .possessor_viewable = true,
28 .aces = {
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),
33 }
34};
35
David Howellsf36f8c72013-09-24 10:35:19 +010036/*
37 * Create the persistent keyring register for the current user namespace.
38 *
39 * Called with the namespace's sem locked for writing.
40 */
41static int key_create_persistent_register(struct user_namespace *ns)
42{
43 struct key *reg = keyring_alloc(".persistent_register",
44 KUIDT_INIT(0), KGIDT_INIT(0),
45 current_cred(),
David Howells2e122562019-06-27 23:03:07 +010046 &persistent_register_keyring_acl,
David Howells5ac7eac2016-04-06 16:14:24 +010047 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
David Howellsf36f8c72013-09-24 10:35:19 +010048 if (IS_ERR(reg))
49 return PTR_ERR(reg);
50
51 ns->persistent_keyring_register = reg;
52 return 0;
53}
54
55/*
56 * Create the persistent keyring for the specified user.
57 *
58 * Called with the namespace's sem locked for writing.
59 */
60static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
61 struct keyring_index_key *index_key)
62{
63 struct key *persistent;
64 key_ref_t reg_ref, persistent_ref;
65
66 if (!ns->persistent_keyring_register) {
67 long err = key_create_persistent_register(ns);
68 if (err < 0)
69 return ERR_PTR(err);
70 } else {
71 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
72 persistent_ref = find_key_to_update(reg_ref, index_key);
73 if (persistent_ref)
74 return persistent_ref;
75 }
76
77 persistent = keyring_alloc(index_key->description,
78 uid, INVALID_GID, current_cred(),
David Howells2e122562019-06-27 23:03:07 +010079 &persistent_keyring_acl,
David Howells5ac7eac2016-04-06 16:14:24 +010080 KEY_ALLOC_NOT_IN_QUOTA, NULL,
David Howellsf36f8c72013-09-24 10:35:19 +010081 ns->persistent_keyring_register);
82 if (IS_ERR(persistent))
83 return ERR_CAST(persistent);
84
85 return make_key_ref(persistent, true);
86}
87
88/*
89 * Get the persistent keyring for a specific UID and link it to the nominated
90 * keyring.
91 */
92static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
93 key_ref_t dest_ref)
94{
95 struct keyring_index_key index_key;
96 struct key *persistent;
97 key_ref_t reg_ref, persistent_ref;
98 char buf[32];
99 long ret;
100
101 /* Look in the register if it exists */
David Howells3b6e4de2019-06-26 21:02:32 +0100102 memset(&index_key, 0, sizeof(index_key));
David Howellsf36f8c72013-09-24 10:35:19 +0100103 index_key.type = &key_type_keyring;
104 index_key.description = buf;
105 index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
David Howellsf771fde2019-06-26 21:02:31 +0100106 key_set_index_key(&index_key);
David Howellsf36f8c72013-09-24 10:35:19 +0100107
108 if (ns->persistent_keyring_register) {
109 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
David Howells0f44e4d2019-06-26 21:02:32 +0100110 down_read(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100111 persistent_ref = find_key_to_update(reg_ref, &index_key);
David Howells0f44e4d2019-06-26 21:02:32 +0100112 up_read(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100113
114 if (persistent_ref)
115 goto found;
116 }
117
118 /* It wasn't in the register, so we'll need to create it. We might
119 * also need to create the register.
120 */
David Howells0f44e4d2019-06-26 21:02:32 +0100121 down_write(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100122 persistent_ref = key_create_persistent(ns, uid, &index_key);
David Howells0f44e4d2019-06-26 21:02:32 +0100123 up_write(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100124 if (!IS_ERR(persistent_ref))
125 goto found;
126
127 return PTR_ERR(persistent_ref);
128
129found:
David Howellsf5895942014-03-14 17:44:49 +0000130 ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
David Howellsf36f8c72013-09-24 10:35:19 +0100131 if (ret == 0) {
132 persistent = key_ref_to_ptr(persistent_ref);
133 ret = key_link(key_ref_to_ptr(dest_ref), persistent);
134 if (ret == 0) {
135 key_set_timeout(persistent, persistent_keyring_expiry);
David Howells965475a2016-06-14 10:29:44 +0100136 ret = persistent->serial;
David Howellsf36f8c72013-09-24 10:35:19 +0100137 }
138 }
139
140 key_ref_put(persistent_ref);
141 return ret;
142}
143
144/*
145 * Get the persistent keyring for a specific UID and link it to the nominated
146 * keyring.
147 */
148long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
149{
150 struct user_namespace *ns = current_user_ns();
151 key_ref_t dest_ref;
152 kuid_t uid;
153 long ret;
154
155 /* -1 indicates the current user */
156 if (_uid == (uid_t)-1) {
157 uid = current_uid();
158 } else {
159 uid = make_kuid(ns, _uid);
160 if (!uid_valid(uid))
161 return -EINVAL;
162
163 /* You can only see your own persistent cache if you're not
164 * sufficiently privileged.
165 */
David Howellsfbf8c53f2013-11-06 14:01:51 +0000166 if (!uid_eq(uid, current_uid()) &&
167 !uid_eq(uid, current_euid()) &&
David Howellsf36f8c72013-09-24 10:35:19 +0100168 !ns_capable(ns, CAP_SETUID))
169 return -EPERM;
170 }
171
172 /* There must be a destination keyring */
David Howellsf5895942014-03-14 17:44:49 +0000173 dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howellsf36f8c72013-09-24 10:35:19 +0100174 if (IS_ERR(dest_ref))
175 return PTR_ERR(dest_ref);
176 if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
177 ret = -ENOTDIR;
178 goto out_put_dest;
179 }
180
181 ret = key_get_persistent(ns, uid, dest_ref);
182
183out_put_dest:
184 key_ref_put(dest_ref);
185 return ret;
186}