blob: 97af230aa4b22b3667e7098558bdff933a7b5bb4 [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
15/*
16 * Create the persistent keyring register for the current user namespace.
17 *
18 * Called with the namespace's sem locked for writing.
19 */
20static int key_create_persistent_register(struct user_namespace *ns)
21{
22 struct key *reg = keyring_alloc(".persistent_register",
23 KUIDT_INIT(0), KGIDT_INIT(0),
24 current_cred(),
Linus Torvalds028db3e2019-07-10 18:43:43 -070025 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
26 KEY_USR_VIEW | KEY_USR_READ),
David Howells5ac7eac2016-04-06 16:14:24 +010027 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
David Howellsf36f8c72013-09-24 10:35:19 +010028 if (IS_ERR(reg))
29 return PTR_ERR(reg);
30
31 ns->persistent_keyring_register = reg;
32 return 0;
33}
34
35/*
36 * Create the persistent keyring for the specified user.
37 *
38 * Called with the namespace's sem locked for writing.
39 */
40static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
41 struct keyring_index_key *index_key)
42{
43 struct key *persistent;
44 key_ref_t reg_ref, persistent_ref;
45
46 if (!ns->persistent_keyring_register) {
47 long err = key_create_persistent_register(ns);
48 if (err < 0)
49 return ERR_PTR(err);
50 } else {
51 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
52 persistent_ref = find_key_to_update(reg_ref, index_key);
53 if (persistent_ref)
54 return persistent_ref;
55 }
56
57 persistent = keyring_alloc(index_key->description,
58 uid, INVALID_GID, current_cred(),
Linus Torvalds028db3e2019-07-10 18:43:43 -070059 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
60 KEY_USR_VIEW | KEY_USR_READ),
David Howells5ac7eac2016-04-06 16:14:24 +010061 KEY_ALLOC_NOT_IN_QUOTA, NULL,
David Howellsf36f8c72013-09-24 10:35:19 +010062 ns->persistent_keyring_register);
63 if (IS_ERR(persistent))
64 return ERR_CAST(persistent);
65
66 return make_key_ref(persistent, true);
67}
68
69/*
70 * Get the persistent keyring for a specific UID and link it to the nominated
71 * keyring.
72 */
73static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
74 key_ref_t dest_ref)
75{
76 struct keyring_index_key index_key;
77 struct key *persistent;
78 key_ref_t reg_ref, persistent_ref;
79 char buf[32];
80 long ret;
81
82 /* Look in the register if it exists */
David Howells3b6e4de2019-06-26 21:02:32 +010083 memset(&index_key, 0, sizeof(index_key));
David Howellsf36f8c72013-09-24 10:35:19 +010084 index_key.type = &key_type_keyring;
85 index_key.description = buf;
86 index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
David Howellsf771fde2019-06-26 21:02:31 +010087 key_set_index_key(&index_key);
David Howellsf36f8c72013-09-24 10:35:19 +010088
89 if (ns->persistent_keyring_register) {
90 reg_ref = make_key_ref(ns->persistent_keyring_register, true);
David Howells0f44e4d2019-06-26 21:02:32 +010091 down_read(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +010092 persistent_ref = find_key_to_update(reg_ref, &index_key);
David Howells0f44e4d2019-06-26 21:02:32 +010093 up_read(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +010094
95 if (persistent_ref)
96 goto found;
97 }
98
99 /* It wasn't in the register, so we'll need to create it. We might
100 * also need to create the register.
101 */
David Howells0f44e4d2019-06-26 21:02:32 +0100102 down_write(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100103 persistent_ref = key_create_persistent(ns, uid, &index_key);
David Howells0f44e4d2019-06-26 21:02:32 +0100104 up_write(&ns->keyring_sem);
David Howellsf36f8c72013-09-24 10:35:19 +0100105 if (!IS_ERR(persistent_ref))
106 goto found;
107
108 return PTR_ERR(persistent_ref);
109
110found:
David Howellsf5895942014-03-14 17:44:49 +0000111 ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
David Howellsf36f8c72013-09-24 10:35:19 +0100112 if (ret == 0) {
113 persistent = key_ref_to_ptr(persistent_ref);
114 ret = key_link(key_ref_to_ptr(dest_ref), persistent);
115 if (ret == 0) {
116 key_set_timeout(persistent, persistent_keyring_expiry);
David Howells965475a2016-06-14 10:29:44 +0100117 ret = persistent->serial;
David Howellsf36f8c72013-09-24 10:35:19 +0100118 }
119 }
120
121 key_ref_put(persistent_ref);
122 return ret;
123}
124
125/*
126 * Get the persistent keyring for a specific UID and link it to the nominated
127 * keyring.
128 */
129long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
130{
131 struct user_namespace *ns = current_user_ns();
132 key_ref_t dest_ref;
133 kuid_t uid;
134 long ret;
135
136 /* -1 indicates the current user */
137 if (_uid == (uid_t)-1) {
138 uid = current_uid();
139 } else {
140 uid = make_kuid(ns, _uid);
141 if (!uid_valid(uid))
142 return -EINVAL;
143
144 /* You can only see your own persistent cache if you're not
145 * sufficiently privileged.
146 */
David Howellsfbf8c53f2013-11-06 14:01:51 +0000147 if (!uid_eq(uid, current_uid()) &&
148 !uid_eq(uid, current_euid()) &&
David Howellsf36f8c72013-09-24 10:35:19 +0100149 !ns_capable(ns, CAP_SETUID))
150 return -EPERM;
151 }
152
153 /* There must be a destination keyring */
David Howellsf5895942014-03-14 17:44:49 +0000154 dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
David Howellsf36f8c72013-09-24 10:35:19 +0100155 if (IS_ERR(dest_ref))
156 return PTR_ERR(dest_ref);
157 if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
158 ret = -ENOTDIR;
159 goto out_put_dest;
160 }
161
162 ret = key_get_persistent(ns, uid, dest_ref);
163
164out_put_dest:
165 key_ref_put(dest_ref);
166 return ret;
167}