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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/err.h> |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 17 | #include <keys/user-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/uaccess.h> |
| 19 | #include "internal.h" |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | .update = user_update, |
| 29 | .match = user_match, |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 30 | .revoke = user_revoke, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | .destroy = user_destroy, |
| 32 | .describe = user_describe, |
| 33 | .read = user_read, |
| 34 | }; |
| 35 | |
Michael Halcrow | 16c29b6 | 2005-06-23 22:00:58 -0700 | [diff] [blame] | 36 | EXPORT_SYMBOL_GPL(key_type_user); |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * instantiate a user defined key |
| 40 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 41 | int user_instantiate(struct key *key, const void *data, size_t datalen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 43 | struct user_key_payload *upayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | ret = -ENOMEM; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 55 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
| 56 | if (!upayload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | goto error; |
| 58 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 59 | /* attach the data */ |
| 60 | upayload->datalen = datalen; |
| 61 | memcpy(upayload->data, data, datalen); |
| 62 | rcu_assign_pointer(key->payload.data, upayload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | ret = 0; |
| 64 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 65 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 67 | } |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 68 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(user_instantiate); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 72 | * dispose of the old data from an updated user defined key |
| 73 | */ |
| 74 | static void user_update_rcu_disposal(struct rcu_head *rcu) |
| 75 | { |
| 76 | struct user_key_payload *upayload; |
| 77 | |
| 78 | upayload = container_of(rcu, struct user_key_payload, rcu); |
| 79 | |
| 80 | kfree(upayload); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 81 | } |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 82 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * update a user defined key |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 85 | * - the key's semaphore is write-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 87 | int user_update(struct key *key, const void *data, size_t datalen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 89 | struct user_key_payload *upayload, *zap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int ret; |
| 91 | |
| 92 | ret = -EINVAL; |
| 93 | if (datalen <= 0 || datalen > 32767 || !data) |
| 94 | goto error; |
| 95 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 96 | /* construct a replacement payload */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | ret = -ENOMEM; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 98 | upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL); |
| 99 | if (!upayload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | goto error; |
| 101 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 102 | upayload->datalen = datalen; |
| 103 | memcpy(upayload->data, data, datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | /* check the quota and attach the new data */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 106 | zap = upayload; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | ret = key_payload_reserve(key, datalen); |
| 109 | |
| 110 | if (ret == 0) { |
| 111 | /* attach the new data, displacing the old */ |
| 112 | zap = key->payload.data; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 113 | rcu_assign_pointer(key->payload.data, upayload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | key->expiry = 0; |
| 115 | } |
| 116 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 117 | call_rcu(&zap->rcu, user_update_rcu_disposal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 119 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 123 | EXPORT_SYMBOL_GPL(user_update); |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* |
| 126 | * match users on their name |
| 127 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 128 | int user_match(const struct key *key, const void *description) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | return strcmp(key->description, description) == 0; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(user_match); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 136 | * dispose of the links from a revoked keyring |
| 137 | * - called with the key sem write-locked |
| 138 | */ |
| 139 | void user_revoke(struct key *key) |
| 140 | { |
| 141 | struct user_key_payload *upayload = key->payload.data; |
| 142 | |
| 143 | /* clear the quota */ |
| 144 | key_payload_reserve(key, 0); |
| 145 | |
| 146 | if (upayload) { |
| 147 | rcu_assign_pointer(key->payload.data, NULL); |
| 148 | call_rcu(&upayload->rcu, user_update_rcu_disposal); |
| 149 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 150 | } |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 151 | |
| 152 | EXPORT_SYMBOL(user_revoke); |
| 153 | |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 154 | /* |
| 155 | * dispose of the data dangling from the corpse of a user key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 157 | void user_destroy(struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 159 | struct user_key_payload *upayload = key->payload.data; |
| 160 | |
| 161 | kfree(upayload); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(user_destroy); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 167 | * describe the user key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 169 | void user_describe(const struct key *key, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | seq_puts(m, key->description); |
| 172 | |
| 173 | seq_printf(m, ": %u", key->datalen); |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 176 | EXPORT_SYMBOL_GPL(user_describe); |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* |
| 179 | * read the key data |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 180 | * - the key's semaphore is read-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | */ |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 182 | 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] | 183 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 184 | struct user_key_payload *upayload; |
| 185 | long ret; |
| 186 | |
David Howells | 633e804 | 2011-03-07 15:05:51 +0000 | [diff] [blame] | 187 | upayload = rcu_dereference_key(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 188 | ret = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | /* we can return the data as is */ |
| 191 | if (buffer && buflen > 0) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 192 | if (buflen > upayload->datalen) |
| 193 | buflen = upayload->datalen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 195 | if (copy_to_user(buffer, upayload->data, buflen) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | ret = -EFAULT; |
| 197 | } |
| 198 | |
| 199 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 200 | } |
David Howells | 2aa349f | 2005-10-30 15:02:42 -0800 | [diff] [blame] | 201 | |
| 202 | EXPORT_SYMBOL_GPL(user_read); |