]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - security/keys/user_defined.c
Linux 3.2-rc2
[mirror_ubuntu-eoan-kernel.git] / security / keys / user_defined.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
14#include <linux/slab.h>
15#include <linux/seq_file.h>
16#include <linux/err.h>
2aa349f6 17#include <keys/user-type.h>
1da177e4
LT
18#include <asm/uaccess.h>
19#include "internal.h"
20
1da177e4
LT
21/*
22 * user defined keys take an arbitrary string as the description and an
23 * arbitrary blob of data as the payload
24 */
25struct key_type key_type_user = {
26 .name = "user",
27 .instantiate = user_instantiate,
1da177e4
LT
28 .update = user_update,
29 .match = user_match,
31204ed9 30 .revoke = user_revoke,
1da177e4
LT
31 .destroy = user_destroy,
32 .describe = user_describe,
33 .read = user_read,
34};
35
16c29b67
MH
36EXPORT_SYMBOL_GPL(key_type_user);
37
1da177e4
LT
38/*
39 * instantiate a user defined key
40 */
2aa349f6 41int user_instantiate(struct key *key, const void *data, size_t datalen)
1da177e4 42{
76d8aeab 43 struct user_key_payload *upayload;
1da177e4
LT
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
1da177e4 54 ret = -ENOMEM;
76d8aeab
DH
55 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
56 if (!upayload)
1da177e4
LT
57 goto error;
58
76d8aeab
DH
59 /* attach the data */
60 upayload->datalen = datalen;
61 memcpy(upayload->data, data, datalen);
62 rcu_assign_pointer(key->payload.data, upayload);
1da177e4
LT
63 ret = 0;
64
2aa349f6 65error:
1da177e4 66 return ret;
a8b17ed0 67}
31204ed9 68
2aa349f6
DH
69EXPORT_SYMBOL_GPL(user_instantiate);
70
1da177e4
LT
71/*
72 * update a user defined key
76d8aeab 73 * - the key's semaphore is write-locked
1da177e4 74 */
2aa349f6 75int user_update(struct key *key, const void *data, size_t datalen)
1da177e4 76{
76d8aeab 77 struct user_key_payload *upayload, *zap;
1da177e4
LT
78 int ret;
79
80 ret = -EINVAL;
81 if (datalen <= 0 || datalen > 32767 || !data)
82 goto error;
83
76d8aeab 84 /* construct a replacement payload */
1da177e4 85 ret = -ENOMEM;
76d8aeab
DH
86 upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
87 if (!upayload)
1da177e4
LT
88 goto error;
89
76d8aeab
DH
90 upayload->datalen = datalen;
91 memcpy(upayload->data, data, datalen);
1da177e4
LT
92
93 /* check the quota and attach the new data */
76d8aeab 94 zap = upayload;
1da177e4
LT
95
96 ret = key_payload_reserve(key, datalen);
97
98 if (ret == 0) {
99 /* attach the new data, displacing the old */
100 zap = key->payload.data;
76d8aeab 101 rcu_assign_pointer(key->payload.data, upayload);
1da177e4
LT
102 key->expiry = 0;
103 }
104
3acb458c 105 kfree_rcu(zap, rcu);
1da177e4 106
2aa349f6 107error:
1da177e4 108 return ret;
a8b17ed0 109}
1da177e4 110
2aa349f6
DH
111EXPORT_SYMBOL_GPL(user_update);
112
1da177e4
LT
113/*
114 * match users on their name
115 */
2aa349f6 116int user_match(const struct key *key, const void *description)
1da177e4
LT
117{
118 return strcmp(key->description, description) == 0;
a8b17ed0 119}
1da177e4 120
2aa349f6
DH
121EXPORT_SYMBOL_GPL(user_match);
122
1da177e4 123/*
31204ed9
DH
124 * dispose of the links from a revoked keyring
125 * - called with the key sem write-locked
126 */
127void user_revoke(struct key *key)
128{
129 struct user_key_payload *upayload = key->payload.data;
130
131 /* clear the quota */
132 key_payload_reserve(key, 0);
133
134 if (upayload) {
135 rcu_assign_pointer(key->payload.data, NULL);
3acb458c 136 kfree_rcu(upayload, rcu);
31204ed9 137 }
a8b17ed0 138}
31204ed9
DH
139
140EXPORT_SYMBOL(user_revoke);
141
31204ed9
DH
142/*
143 * dispose of the data dangling from the corpse of a user key
1da177e4 144 */
2aa349f6 145void user_destroy(struct key *key)
1da177e4 146{
76d8aeab
DH
147 struct user_key_payload *upayload = key->payload.data;
148
149 kfree(upayload);
a8b17ed0 150}
1da177e4 151
2aa349f6
DH
152EXPORT_SYMBOL_GPL(user_destroy);
153
1da177e4 154/*
76d8aeab 155 * describe the user key
1da177e4 156 */
2aa349f6 157void user_describe(const struct key *key, struct seq_file *m)
1da177e4
LT
158{
159 seq_puts(m, key->description);
78b7280c
DH
160 if (key_is_instantiated(key))
161 seq_printf(m, ": %u", key->datalen);
a8b17ed0 162}
1da177e4 163
2aa349f6
DH
164EXPORT_SYMBOL_GPL(user_describe);
165
1da177e4
LT
166/*
167 * read the key data
76d8aeab 168 * - the key's semaphore is read-locked
1da177e4 169 */
2aa349f6 170long user_read(const struct key *key, char __user *buffer, size_t buflen)
1da177e4 171{
76d8aeab
DH
172 struct user_key_payload *upayload;
173 long ret;
174
633e804e 175 upayload = rcu_dereference_key(key);
76d8aeab 176 ret = upayload->datalen;
1da177e4
LT
177
178 /* we can return the data as is */
179 if (buffer && buflen > 0) {
76d8aeab
DH
180 if (buflen > upayload->datalen)
181 buflen = upayload->datalen;
1da177e4 182
76d8aeab 183 if (copy_to_user(buffer, upayload->data, buflen) != 0)
1da177e4
LT
184 ret = -EFAULT;
185 }
186
187 return ret;
a8b17ed0 188}
2aa349f6
DH
189
190EXPORT_SYMBOL_GPL(user_read);