xref: /linux/security/keys/persistent.c (revision a58946c158a040068e7c94dc1d58bbd273258068)
1 /* General persistent per-UID keyrings register
2  *
3  * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 
12 #include <linux/user_namespace.h>
13 #include <linux/cred.h>
14 
15 #include "internal.h"
16 
17 unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
18 
19 /*
20  * Create the persistent keyring register for the current user namespace.
21  *
22  * Called with the namespace's sem locked for writing.
23  */
24 static int key_create_persistent_register(struct user_namespace *ns)
25 {
26 	struct key *reg = keyring_alloc(".persistent_register",
27 					KUIDT_INIT(0), KGIDT_INIT(0),
28 					current_cred(),
29 					((KEY_POS_ALL & ~KEY_POS_SETATTR) |
30 					 KEY_USR_VIEW | KEY_USR_READ),
31 					KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
32 	if (IS_ERR(reg))
33 		return PTR_ERR(reg);
34 
35 	ns->persistent_keyring_register = reg;
36 	return 0;
37 }
38 
39 /*
40  * Create the persistent keyring for the specified user.
41  *
42  * Called with the namespace's sem locked for writing.
43  */
44 static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
45 				       struct keyring_index_key *index_key)
46 {
47 	struct key *persistent;
48 	key_ref_t reg_ref, persistent_ref;
49 
50 	if (!ns->persistent_keyring_register) {
51 		long err = key_create_persistent_register(ns);
52 		if (err < 0)
53 			return ERR_PTR(err);
54 	} else {
55 		reg_ref = make_key_ref(ns->persistent_keyring_register, true);
56 		persistent_ref = find_key_to_update(reg_ref, index_key);
57 		if (persistent_ref)
58 			return persistent_ref;
59 	}
60 
61 	persistent = keyring_alloc(index_key->description,
62 				   uid, INVALID_GID, current_cred(),
63 				   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
64 				    KEY_USR_VIEW | KEY_USR_READ),
65 				   KEY_ALLOC_NOT_IN_QUOTA, NULL,
66 				   ns->persistent_keyring_register);
67 	if (IS_ERR(persistent))
68 		return ERR_CAST(persistent);
69 
70 	return make_key_ref(persistent, true);
71 }
72 
73 /*
74  * Get the persistent keyring for a specific UID and link it to the nominated
75  * keyring.
76  */
77 static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
78 			       key_ref_t dest_ref)
79 {
80 	struct keyring_index_key index_key;
81 	struct key *persistent;
82 	key_ref_t reg_ref, persistent_ref;
83 	char buf[32];
84 	long ret;
85 
86 	/* Look in the register if it exists */
87 	memset(&index_key, 0, sizeof(index_key));
88 	index_key.type = &key_type_keyring;
89 	index_key.description = buf;
90 	index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
91 	key_set_index_key(&index_key);
92 
93 	if (ns->persistent_keyring_register) {
94 		reg_ref = make_key_ref(ns->persistent_keyring_register, true);
95 		down_read(&ns->keyring_sem);
96 		persistent_ref = find_key_to_update(reg_ref, &index_key);
97 		up_read(&ns->keyring_sem);
98 
99 		if (persistent_ref)
100 			goto found;
101 	}
102 
103 	/* It wasn't in the register, so we'll need to create it.  We might
104 	 * also need to create the register.
105 	 */
106 	down_write(&ns->keyring_sem);
107 	persistent_ref = key_create_persistent(ns, uid, &index_key);
108 	up_write(&ns->keyring_sem);
109 	if (!IS_ERR(persistent_ref))
110 		goto found;
111 
112 	return PTR_ERR(persistent_ref);
113 
114 found:
115 	ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
116 	if (ret == 0) {
117 		persistent = key_ref_to_ptr(persistent_ref);
118 		ret = key_link(key_ref_to_ptr(dest_ref), persistent);
119 		if (ret == 0) {
120 			key_set_timeout(persistent, persistent_keyring_expiry);
121 			ret = persistent->serial;
122 		}
123 	}
124 
125 	key_ref_put(persistent_ref);
126 	return ret;
127 }
128 
129 /*
130  * Get the persistent keyring for a specific UID and link it to the nominated
131  * keyring.
132  */
133 long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
134 {
135 	struct user_namespace *ns = current_user_ns();
136 	key_ref_t dest_ref;
137 	kuid_t uid;
138 	long ret;
139 
140 	/* -1 indicates the current user */
141 	if (_uid == (uid_t)-1) {
142 		uid = current_uid();
143 	} else {
144 		uid = make_kuid(ns, _uid);
145 		if (!uid_valid(uid))
146 			return -EINVAL;
147 
148 		/* You can only see your own persistent cache if you're not
149 		 * sufficiently privileged.
150 		 */
151 		if (!uid_eq(uid, current_uid()) &&
152 		    !uid_eq(uid, current_euid()) &&
153 		    !ns_capable(ns, CAP_SETUID))
154 			return -EPERM;
155 	}
156 
157 	/* There must be a destination keyring */
158 	dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
159 	if (IS_ERR(dest_ref))
160 		return PTR_ERR(dest_ref);
161 	if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
162 		ret = -ENOTDIR;
163 		goto out_put_dest;
164 	}
165 
166 	ret = key_get_persistent(ns, uid, dest_ref);
167 
168 out_put_dest:
169 	key_ref_put(dest_ref);
170 	return ret;
171 }
172