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/sched.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
17 #include <linux/err.h>
18 #include <asm/uaccess.h>
21 static int user_instantiate(struct key *key, const void *data, size_t datalen);
22 static int user_duplicate(struct key *key, const struct key *source);
23 static int user_update(struct key *key, const void *data, size_t datalen);
24 static int user_match(const struct key *key, const void *criterion);
25 static void user_destroy(struct key *key);
26 static void user_describe(const struct key *user, struct seq_file *m);
27 static long user_read(const struct key *key,
28 char __user *buffer, size_t buflen);
31 * user defined keys take an arbitrary string as the description and an
32 * arbitrary blob of data as the payload
34 struct key_type key_type_user = {
36 .instantiate = user_instantiate,
37 .duplicate = user_duplicate,
38 .update = user_update,
40 .destroy = user_destroy,
41 .describe = user_describe,
45 struct user_key_payload {
46 struct rcu_head rcu; /* RCU destructor */
47 unsigned short datalen; /* length of this data */
48 char data[0]; /* actual data */
51 /*****************************************************************************/
53 * instantiate a user defined key
55 static int user_instantiate(struct key *key, const void *data, size_t datalen)
57 struct user_key_payload *upayload;
61 if (datalen <= 0 || datalen > 32767 || !data)
64 ret = key_payload_reserve(key, datalen);
69 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
74 upayload->datalen = datalen;
75 memcpy(upayload->data, data, datalen);
76 rcu_assign_pointer(key->payload.data, upayload);
82 } /* end user_instantiate() */
84 /*****************************************************************************/
86 * duplicate a user defined key
87 * - both keys' semaphores are locked against further modification
88 * - the new key cannot yet be accessed
90 static int user_duplicate(struct key *key, const struct key *source)
92 struct user_key_payload *upayload, *spayload;
95 /* just copy the payload */
97 upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
99 spayload = rcu_dereference(source->payload.data);
100 BUG_ON(source->datalen != spayload->datalen);
102 upayload->datalen = key->datalen = spayload->datalen;
103 memcpy(upayload->data, spayload->data, key->datalen);
105 key->payload.data = upayload;
111 } /* end user_duplicate() */
113 /*****************************************************************************/
115 * dispose of the old data from an updated user defined key
117 static void user_update_rcu_disposal(struct rcu_head *rcu)
119 struct user_key_payload *upayload;
121 upayload = container_of(rcu, struct user_key_payload, rcu);
125 } /* end user_update_rcu_disposal() */
127 /*****************************************************************************/
129 * update a user defined key
130 * - the key's semaphore is write-locked
132 static int user_update(struct key *key, const void *data, size_t datalen)
134 struct user_key_payload *upayload, *zap;
138 if (datalen <= 0 || datalen > 32767 || !data)
141 /* construct a replacement payload */
143 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
147 upayload->datalen = datalen;
148 memcpy(upayload->data, data, datalen);
150 /* check the quota and attach the new data */
153 ret = key_payload_reserve(key, datalen);
156 /* attach the new data, displacing the old */
157 zap = key->payload.data;
158 rcu_assign_pointer(key->payload.data, upayload);
162 call_rcu(&zap->rcu, user_update_rcu_disposal);
167 } /* end user_update() */
169 /*****************************************************************************/
171 * match users on their name
173 static int user_match(const struct key *key, const void *description)
175 return strcmp(key->description, description) == 0;
177 } /* end user_match() */
179 /*****************************************************************************/
181 * dispose of the data dangling from the corpse of a user
183 static void user_destroy(struct key *key)
185 struct user_key_payload *upayload = key->payload.data;
189 } /* end user_destroy() */
191 /*****************************************************************************/
193 * describe the user key
195 static void user_describe(const struct key *key, struct seq_file *m)
197 seq_puts(m, key->description);
199 seq_printf(m, ": %u", key->datalen);
201 } /* end user_describe() */
203 /*****************************************************************************/
206 * - the key's semaphore is read-locked
208 static long user_read(const struct key *key,
209 char __user *buffer, size_t buflen)
211 struct user_key_payload *upayload;
214 upayload = rcu_dereference(key->payload.data);
215 ret = upayload->datalen;
217 /* we can return the data as is */
218 if (buffer && buflen > 0) {
219 if (buflen > upayload->datalen)
220 buflen = upayload->datalen;
222 if (copy_to_user(buffer, upayload->data, buflen) != 0)
228 } /* end user_read() */