Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/sched.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/seq_file.h> |
| 17 | #include <linux/err.h> |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 18 | #include <keys/user-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> |
| 20 | #include "internal.h" |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | /* |
| 23 | * user defined keys take an arbitrary string as the description and an |
| 24 | * arbitrary blob of data as the payload |
| 25 | */ |
| 26 | struct key_type key_type_user = { |
| 27 | .name = "user", |
| 28 | .instantiate = user_instantiate, |
| 29 | .duplicate = user_duplicate, |
| 30 | .update = user_update, |
| 31 | .match = user_match, |
| 32 | .destroy = user_destroy, |
| 33 | .describe = user_describe, |
| 34 | .read = user_read, |
| 35 | }; |
| 36 | |
Michael Halcrow | 16c29b6 | 2005-06-23 22:00:58 -0700 | [diff] [blame] | 37 | EXPORT_SYMBOL_GPL(key_type_user); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /*****************************************************************************/ |
| 40 | /* |
| 41 | * instantiate a user defined key |
| 42 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 43 | int user_instantiate(struct key *key, const void *data, size_t datalen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 45 | struct user_key_payload *upayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | ret = -EINVAL; |
| 49 | if (datalen <= 0 || datalen > 32767 || !data) |
| 50 | goto error; |
| 51 | |
| 52 | ret = key_payload_reserve(key, datalen); |
| 53 | if (ret < 0) |
| 54 | goto error; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | ret = -ENOMEM; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 57 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
| 58 | if (!upayload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | goto error; |
| 60 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 61 | /* attach the data */ |
| 62 | upayload->datalen = datalen; |
| 63 | memcpy(upayload->data, data, datalen); |
| 64 | rcu_assign_pointer(key->payload.data, upayload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | ret = 0; |
| 66 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 67 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return ret; |
| 69 | |
| 70 | } /* end user_instantiate() */ |
| 71 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 72 | EXPORT_SYMBOL_GPL(user_instantiate); |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /*****************************************************************************/ |
| 75 | /* |
| 76 | * duplicate a user defined key |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 77 | * - both keys' semaphores are locked against further modification |
| 78 | * - the new key cannot yet be accessed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 80 | int user_duplicate(struct key *key, const struct key *source) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 82 | struct user_key_payload *upayload, *spayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int ret; |
| 84 | |
| 85 | /* just copy the payload */ |
| 86 | ret = -ENOMEM; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 87 | upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL); |
| 88 | if (upayload) { |
| 89 | spayload = rcu_dereference(source->payload.data); |
| 90 | BUG_ON(source->datalen != spayload->datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 92 | upayload->datalen = key->datalen = spayload->datalen; |
| 93 | memcpy(upayload->data, spayload->data, key->datalen); |
| 94 | |
| 95 | key->payload.data = upayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | ret = 0; |
| 97 | } |
| 98 | |
| 99 | return ret; |
| 100 | |
| 101 | } /* end user_duplicate() */ |
| 102 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 103 | EXPORT_SYMBOL_GPL(user_duplicate); |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /*****************************************************************************/ |
| 106 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 107 | * dispose of the old data from an updated user defined key |
| 108 | */ |
| 109 | static void user_update_rcu_disposal(struct rcu_head *rcu) |
| 110 | { |
| 111 | struct user_key_payload *upayload; |
| 112 | |
| 113 | upayload = container_of(rcu, struct user_key_payload, rcu); |
| 114 | |
| 115 | kfree(upayload); |
| 116 | |
| 117 | } /* end user_update_rcu_disposal() */ |
| 118 | |
| 119 | /*****************************************************************************/ |
| 120 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | * update a user defined key |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 122 | * - the key's semaphore is write-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 124 | int user_update(struct key *key, const void *data, size_t datalen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 126 | struct user_key_payload *upayload, *zap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | int ret; |
| 128 | |
| 129 | ret = -EINVAL; |
| 130 | if (datalen <= 0 || datalen > 32767 || !data) |
| 131 | goto error; |
| 132 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 133 | /* construct a replacement payload */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | ret = -ENOMEM; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 135 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
| 136 | if (!upayload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | goto error; |
| 138 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 139 | upayload->datalen = datalen; |
| 140 | memcpy(upayload->data, data, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* check the quota and attach the new data */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 143 | zap = upayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | ret = key_payload_reserve(key, datalen); |
| 146 | |
| 147 | if (ret == 0) { |
| 148 | /* attach the new data, displacing the old */ |
| 149 | zap = key->payload.data; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 150 | rcu_assign_pointer(key->payload.data, upayload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | key->expiry = 0; |
| 152 | } |
| 153 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 154 | call_rcu(&zap->rcu, user_update_rcu_disposal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 156 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return ret; |
| 158 | |
| 159 | } /* end user_update() */ |
| 160 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 161 | EXPORT_SYMBOL_GPL(user_update); |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /*****************************************************************************/ |
| 164 | /* |
| 165 | * match users on their name |
| 166 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 167 | int user_match(const struct key *key, const void *description) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | return strcmp(key->description, description) == 0; |
| 170 | |
| 171 | } /* end user_match() */ |
| 172 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 173 | EXPORT_SYMBOL_GPL(user_match); |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /*****************************************************************************/ |
| 176 | /* |
| 177 | * dispose of the data dangling from the corpse of a user |
| 178 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 179 | void user_destroy(struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 181 | struct user_key_payload *upayload = key->payload.data; |
| 182 | |
| 183 | kfree(upayload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | } /* end user_destroy() */ |
| 186 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 187 | EXPORT_SYMBOL_GPL(user_destroy); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /*****************************************************************************/ |
| 190 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 191 | * describe the user key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 193 | void user_describe(const struct key *key, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | seq_puts(m, key->description); |
| 196 | |
| 197 | seq_printf(m, ": %u", key->datalen); |
| 198 | |
| 199 | } /* end user_describe() */ |
| 200 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 201 | EXPORT_SYMBOL_GPL(user_describe); |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /*****************************************************************************/ |
| 204 | /* |
| 205 | * read the key data |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 206 | * - the key's semaphore is read-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 208 | long user_read(const struct key *key, char __user *buffer, size_t buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 210 | struct user_key_payload *upayload; |
| 211 | long ret; |
| 212 | |
| 213 | upayload = rcu_dereference(key->payload.data); |
| 214 | ret = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | /* we can return the data as is */ |
| 217 | if (buffer && buflen > 0) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 218 | if (buflen > upayload->datalen) |
| 219 | buflen = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 221 | if (copy_to_user(buffer, upayload->data, buflen) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | ret = -EFAULT; |
| 223 | } |
| 224 | |
| 225 | return ret; |
| 226 | |
| 227 | } /* end user_read() */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame^] | 228 | |
| 229 | EXPORT_SYMBOL_GPL(user_read); |