1 /* user_defined.c: user defined key type
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
22 * user defined keys take an arbitrary string as the description and an
23 * arbitrary blob of data as the payload
25 struct key_type key_type_user
= {
27 .instantiate
= user_instantiate
,
28 .update
= user_update
,
30 .revoke
= user_revoke
,
31 .destroy
= user_destroy
,
32 .describe
= user_describe
,
36 EXPORT_SYMBOL_GPL(key_type_user
);
39 * instantiate a user defined key
41 int user_instantiate(struct key
*key
, const void *data
, size_t datalen
)
43 struct user_key_payload
*upayload
;
47 if (datalen
<= 0 || datalen
> 32767 || !data
)
50 ret
= key_payload_reserve(key
, datalen
);
55 upayload
= kmalloc(sizeof(*upayload
) + datalen
, GFP_KERNEL
);
60 upayload
->datalen
= datalen
;
61 memcpy(upayload
->data
, data
, datalen
);
62 rcu_assign_pointer(key
->payload
.data
, upayload
);
69 EXPORT_SYMBOL_GPL(user_instantiate
);
72 * update a user defined key
73 * - the key's semaphore is write-locked
75 int user_update(struct key
*key
, const void *data
, size_t datalen
)
77 struct user_key_payload
*upayload
, *zap
;
81 if (datalen
<= 0 || datalen
> 32767 || !data
)
84 /* construct a replacement payload */
86 upayload
= kmalloc(sizeof(*upayload
) + datalen
, GFP_KERNEL
);
90 upayload
->datalen
= datalen
;
91 memcpy(upayload
->data
, data
, datalen
);
93 /* check the quota and attach the new data */
96 ret
= key_payload_reserve(key
, datalen
);
99 /* attach the new data, displacing the old */
100 zap
= key
->payload
.data
;
101 rcu_assign_pointer(key
->payload
.data
, upayload
);
111 EXPORT_SYMBOL_GPL(user_update
);
114 * match users on their name
116 int user_match(const struct key
*key
, const void *description
)
118 return strcmp(key
->description
, description
) == 0;
121 EXPORT_SYMBOL_GPL(user_match
);
124 * dispose of the links from a revoked keyring
125 * - called with the key sem write-locked
127 void user_revoke(struct key
*key
)
129 struct user_key_payload
*upayload
= key
->payload
.data
;
131 /* clear the quota */
132 key_payload_reserve(key
, 0);
135 rcu_assign_pointer(key
->payload
.data
, NULL
);
136 kfree_rcu(upayload
, rcu
);
140 EXPORT_SYMBOL(user_revoke
);
143 * dispose of the data dangling from the corpse of a user key
145 void user_destroy(struct key
*key
)
147 struct user_key_payload
*upayload
= key
->payload
.data
;
152 EXPORT_SYMBOL_GPL(user_destroy
);
155 * describe the user key
157 void user_describe(const struct key
*key
, struct seq_file
*m
)
159 seq_puts(m
, key
->description
);
160 if (key_is_instantiated(key
))
161 seq_printf(m
, ": %u", key
->datalen
);
164 EXPORT_SYMBOL_GPL(user_describe
);
168 * - the key's semaphore is read-locked
170 long user_read(const struct key
*key
, char __user
*buffer
, size_t buflen
)
172 struct user_key_payload
*upayload
;
175 upayload
= rcu_dereference_key(key
);
176 ret
= upayload
->datalen
;
178 /* we can return the data as is */
179 if (buffer
&& buflen
> 0) {
180 if (buflen
> upayload
->datalen
)
181 buflen
= upayload
->datalen
;
183 if (copy_to_user(buffer
, upayload
->data
, buflen
) != 0)
190 EXPORT_SYMBOL_GPL(user_read
);