xref: /linux/security/keys/user_defined.c (revision b43ab901d671e3e3cad425ea5e9a3c74e266dcdd)
1 /* user_defined.c: user defined key type
2  *
3  * Copyright (C) 2004 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 License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/seq_file.h>
16 #include <linux/err.h>
17 #include <keys/user-type.h>
18 #include <asm/uaccess.h>
19 #include "internal.h"
20 
21 /*
22  * user defined keys take an arbitrary string as the description and an
23  * arbitrary blob of data as the payload
24  */
25 struct key_type key_type_user = {
26 	.name		= "user",
27 	.instantiate	= user_instantiate,
28 	.update		= user_update,
29 	.match		= user_match,
30 	.revoke		= user_revoke,
31 	.destroy	= user_destroy,
32 	.describe	= user_describe,
33 	.read		= user_read,
34 };
35 
36 EXPORT_SYMBOL_GPL(key_type_user);
37 
38 /*
39  * instantiate a user defined key
40  */
41 int user_instantiate(struct key *key, const void *data, size_t datalen)
42 {
43 	struct user_key_payload *upayload;
44 	int ret;
45 
46 	ret = -EINVAL;
47 	if (datalen <= 0 || datalen > 32767 || !data)
48 		goto error;
49 
50 	ret = key_payload_reserve(key, datalen);
51 	if (ret < 0)
52 		goto error;
53 
54 	ret = -ENOMEM;
55 	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
56 	if (!upayload)
57 		goto error;
58 
59 	/* attach the data */
60 	upayload->datalen = datalen;
61 	memcpy(upayload->data, data, datalen);
62 	rcu_assign_keypointer(key, upayload);
63 	ret = 0;
64 
65 error:
66 	return ret;
67 }
68 
69 EXPORT_SYMBOL_GPL(user_instantiate);
70 
71 /*
72  * update a user defined key
73  * - the key's semaphore is write-locked
74  */
75 int user_update(struct key *key, const void *data, size_t datalen)
76 {
77 	struct user_key_payload *upayload, *zap;
78 	int ret;
79 
80 	ret = -EINVAL;
81 	if (datalen <= 0 || datalen > 32767 || !data)
82 		goto error;
83 
84 	/* construct a replacement payload */
85 	ret = -ENOMEM;
86 	upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
87 	if (!upayload)
88 		goto error;
89 
90 	upayload->datalen = datalen;
91 	memcpy(upayload->data, data, datalen);
92 
93 	/* check the quota and attach the new data */
94 	zap = upayload;
95 
96 	ret = key_payload_reserve(key, datalen);
97 
98 	if (ret == 0) {
99 		/* attach the new data, displacing the old */
100 		zap = key->payload.data;
101 		rcu_assign_keypointer(key, upayload);
102 		key->expiry = 0;
103 	}
104 
105 	if (zap)
106 		kfree_rcu(zap, rcu);
107 
108 error:
109 	return ret;
110 }
111 
112 EXPORT_SYMBOL_GPL(user_update);
113 
114 /*
115  * match users on their name
116  */
117 int user_match(const struct key *key, const void *description)
118 {
119 	return strcmp(key->description, description) == 0;
120 }
121 
122 EXPORT_SYMBOL_GPL(user_match);
123 
124 /*
125  * dispose of the links from a revoked keyring
126  * - called with the key sem write-locked
127  */
128 void user_revoke(struct key *key)
129 {
130 	struct user_key_payload *upayload = key->payload.data;
131 
132 	/* clear the quota */
133 	key_payload_reserve(key, 0);
134 
135 	if (upayload) {
136 		rcu_assign_keypointer(key, NULL);
137 		kfree_rcu(upayload, rcu);
138 	}
139 }
140 
141 EXPORT_SYMBOL(user_revoke);
142 
143 /*
144  * dispose of the data dangling from the corpse of a user key
145  */
146 void user_destroy(struct key *key)
147 {
148 	struct user_key_payload *upayload = key->payload.data;
149 
150 	kfree(upayload);
151 }
152 
153 EXPORT_SYMBOL_GPL(user_destroy);
154 
155 /*
156  * describe the user key
157  */
158 void user_describe(const struct key *key, struct seq_file *m)
159 {
160 	seq_puts(m, key->description);
161 	if (key_is_instantiated(key))
162 		seq_printf(m, ": %u", key->datalen);
163 }
164 
165 EXPORT_SYMBOL_GPL(user_describe);
166 
167 /*
168  * read the key data
169  * - the key's semaphore is read-locked
170  */
171 long user_read(const struct key *key, char __user *buffer, size_t buflen)
172 {
173 	struct user_key_payload *upayload;
174 	long ret;
175 
176 	upayload = rcu_dereference_key(key);
177 	ret = upayload->datalen;
178 
179 	/* we can return the data as is */
180 	if (buffer && buflen > 0) {
181 		if (buflen > upayload->datalen)
182 			buflen = upayload->datalen;
183 
184 		if (copy_to_user(buffer, upayload->data, buflen) != 0)
185 			ret = -EFAULT;
186 	}
187 
188 	return ret;
189 }
190 
191 EXPORT_SYMBOL_GPL(user_read);
192