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