0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) "big_key: "fmt
0010 #include <linux/init.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/file.h>
0013 #include <linux/shmem_fs.h>
0014 #include <linux/err.h>
0015 #include <linux/random.h>
0016 #include <keys/user-type.h>
0017 #include <keys/big_key-type.h>
0018 #include <crypto/chacha20poly1305.h>
0019
0020
0021
0022
0023 struct big_key_payload {
0024 u8 *data;
0025 struct path path;
0026 size_t length;
0027 };
0028 #define to_big_key_payload(payload) \
0029 (struct big_key_payload *)((payload).data)
0030
0031
0032
0033
0034
0035
0036 #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))
0037
0038
0039
0040
0041
0042 struct key_type key_type_big_key = {
0043 .name = "big_key",
0044 .preparse = big_key_preparse,
0045 .free_preparse = big_key_free_preparse,
0046 .instantiate = generic_key_instantiate,
0047 .revoke = big_key_revoke,
0048 .destroy = big_key_destroy,
0049 .describe = big_key_describe,
0050 .read = big_key_read,
0051 .update = big_key_update,
0052 };
0053
0054
0055
0056
0057 int big_key_preparse(struct key_preparsed_payload *prep)
0058 {
0059 struct big_key_payload *payload = to_big_key_payload(prep->payload);
0060 struct file *file;
0061 u8 *buf, *enckey;
0062 ssize_t written;
0063 size_t datalen = prep->datalen;
0064 size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
0065 int ret;
0066
0067 BUILD_BUG_ON(sizeof(*payload) != sizeof(prep->payload.data));
0068
0069 if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
0070 return -EINVAL;
0071
0072
0073 prep->quotalen = 16;
0074
0075 payload->length = datalen;
0076
0077 if (datalen > BIG_KEY_FILE_THRESHOLD) {
0078
0079
0080
0081
0082
0083
0084
0085 loff_t pos = 0;
0086
0087 buf = kvmalloc(enclen, GFP_KERNEL);
0088 if (!buf)
0089 return -ENOMEM;
0090
0091
0092 enckey = kmalloc(CHACHA20POLY1305_KEY_SIZE, GFP_KERNEL);
0093 if (!enckey) {
0094 ret = -ENOMEM;
0095 goto error;
0096 }
0097 ret = get_random_bytes_wait(enckey, CHACHA20POLY1305_KEY_SIZE);
0098 if (unlikely(ret))
0099 goto err_enckey;
0100
0101
0102 chacha20poly1305_encrypt(buf, prep->data, datalen, NULL, 0,
0103 0, enckey);
0104
0105
0106 file = shmem_kernel_file_setup("", enclen, 0);
0107 if (IS_ERR(file)) {
0108 ret = PTR_ERR(file);
0109 goto err_enckey;
0110 }
0111
0112 written = kernel_write(file, buf, enclen, &pos);
0113 if (written != enclen) {
0114 ret = written;
0115 if (written >= 0)
0116 ret = -EIO;
0117 goto err_fput;
0118 }
0119
0120
0121
0122
0123 payload->data = enckey;
0124 payload->path = file->f_path;
0125 path_get(&payload->path);
0126 fput(file);
0127 kvfree_sensitive(buf, enclen);
0128 } else {
0129
0130 void *data = kmalloc(datalen, GFP_KERNEL);
0131
0132 if (!data)
0133 return -ENOMEM;
0134
0135 payload->data = data;
0136 memcpy(data, prep->data, prep->datalen);
0137 }
0138 return 0;
0139
0140 err_fput:
0141 fput(file);
0142 err_enckey:
0143 kfree_sensitive(enckey);
0144 error:
0145 kvfree_sensitive(buf, enclen);
0146 return ret;
0147 }
0148
0149
0150
0151
0152 void big_key_free_preparse(struct key_preparsed_payload *prep)
0153 {
0154 struct big_key_payload *payload = to_big_key_payload(prep->payload);
0155
0156 if (prep->datalen > BIG_KEY_FILE_THRESHOLD)
0157 path_put(&payload->path);
0158 kfree_sensitive(payload->data);
0159 }
0160
0161
0162
0163
0164
0165 void big_key_revoke(struct key *key)
0166 {
0167 struct big_key_payload *payload = to_big_key_payload(key->payload);
0168
0169
0170 key_payload_reserve(key, 0);
0171 if (key_is_positive(key) && payload->length > BIG_KEY_FILE_THRESHOLD)
0172 vfs_truncate(&payload->path, 0);
0173 }
0174
0175
0176
0177
0178 void big_key_destroy(struct key *key)
0179 {
0180 struct big_key_payload *payload = to_big_key_payload(key->payload);
0181
0182 if (payload->length > BIG_KEY_FILE_THRESHOLD) {
0183 path_put(&payload->path);
0184 payload->path.mnt = NULL;
0185 payload->path.dentry = NULL;
0186 }
0187 kfree_sensitive(payload->data);
0188 payload->data = NULL;
0189 }
0190
0191
0192
0193
0194 int big_key_update(struct key *key, struct key_preparsed_payload *prep)
0195 {
0196 int ret;
0197
0198 ret = key_payload_reserve(key, prep->datalen);
0199 if (ret < 0)
0200 return ret;
0201
0202 if (key_is_positive(key))
0203 big_key_destroy(key);
0204
0205 return generic_key_instantiate(key, prep);
0206 }
0207
0208
0209
0210
0211 void big_key_describe(const struct key *key, struct seq_file *m)
0212 {
0213 struct big_key_payload *payload = to_big_key_payload(key->payload);
0214
0215 seq_puts(m, key->description);
0216
0217 if (key_is_positive(key))
0218 seq_printf(m, ": %zu [%s]",
0219 payload->length,
0220 payload->length > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
0221 }
0222
0223
0224
0225
0226
0227 long big_key_read(const struct key *key, char *buffer, size_t buflen)
0228 {
0229 struct big_key_payload *payload = to_big_key_payload(key->payload);
0230 size_t datalen = payload->length;
0231 long ret;
0232
0233 if (!buffer || buflen < datalen)
0234 return datalen;
0235
0236 if (datalen > BIG_KEY_FILE_THRESHOLD) {
0237 struct file *file;
0238 u8 *buf, *enckey = payload->data;
0239 size_t enclen = datalen + CHACHA20POLY1305_AUTHTAG_SIZE;
0240 loff_t pos = 0;
0241
0242 buf = kvmalloc(enclen, GFP_KERNEL);
0243 if (!buf)
0244 return -ENOMEM;
0245
0246 file = dentry_open(&payload->path, O_RDONLY, current_cred());
0247 if (IS_ERR(file)) {
0248 ret = PTR_ERR(file);
0249 goto error;
0250 }
0251
0252
0253 ret = kernel_read(file, buf, enclen, &pos);
0254 if (ret != enclen) {
0255 if (ret >= 0)
0256 ret = -EIO;
0257 goto err_fput;
0258 }
0259
0260 ret = chacha20poly1305_decrypt(buf, buf, enclen, NULL, 0, 0,
0261 enckey) ? 0 : -EBADMSG;
0262 if (unlikely(ret))
0263 goto err_fput;
0264
0265 ret = datalen;
0266
0267
0268 memcpy(buffer, buf, datalen);
0269
0270 err_fput:
0271 fput(file);
0272 error:
0273 kvfree_sensitive(buf, enclen);
0274 } else {
0275 ret = datalen;
0276 memcpy(buffer, payload->data, datalen);
0277 }
0278
0279 return ret;
0280 }
0281
0282
0283
0284
0285 static int __init big_key_init(void)
0286 {
0287 return register_key_type(&key_type_big_key);
0288 }
0289
0290 late_initcall(big_key_init);