big_key.c 8.8 KB
Newer Older
1 2
/* Large capacity key type
 *
3
 * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 5 6 7 8 9 10 11 12
 * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

13
#define pr_fmt(fmt) "big_key: "fmt
14 15 16 17 18
#include <linux/init.h>
#include <linux/seq_file.h>
#include <linux/file.h>
#include <linux/shmem_fs.h>
#include <linux/err.h>
19
#include <linux/scatterlist.h>
20
#include <linux/random.h>
21 22
#include <keys/user-type.h>
#include <keys/big_key-type.h>
23
#include <crypto/aead.h>
24

25 26 27 28 29 30 31 32 33 34
/*
 * Layout of key payload words.
 */
enum {
	big_key_data,
	big_key_path,
	big_key_path_2nd_part,
	big_key_len,
};

35 36 37 38 39 40 41 42
/*
 * Crypto operation with big_key data
 */
enum big_key_op {
	BIG_KEY_ENC,
	BIG_KEY_DEC,
};

43 44 45 46 47 48 49
/*
 * If the data is under this limit, there's no point creating a shm file to
 * hold it as the permanently resident metadata for the shmem fs will be at
 * least as large as the data.
 */
#define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry))

50 51 52
/*
 * Key size for big_key data encryption
 */
53 54 55 56 57 58
#define ENC_KEY_SIZE 32

/*
 * Authentication tag length
 */
#define ENC_AUTHTAG_SIZE 16
59

60 61 62 63 64 65
/*
 * big_key defined keys take an arbitrary string as the description and an
 * arbitrary blob of data as the payload
 */
struct key_type key_type_big_key = {
	.name			= "big_key",
66 67 68
	.preparse		= big_key_preparse,
	.free_preparse		= big_key_free_preparse,
	.instantiate		= generic_key_instantiate,
69 70 71 72
	.revoke			= big_key_revoke,
	.destroy		= big_key_destroy,
	.describe		= big_key_describe,
	.read			= big_key_read,
73
	/* no ->update(); don't add it without changing big_key_crypt() nonce */
74 75
};

76
/*
77
 * Crypto names for big_key data authenticated encryption
78
 */
79
static const char big_key_alg_name[] = "gcm(aes)";
80 81

/*
82
 * Crypto algorithms for big_key data authenticated encryption
83
 */
84
static struct crypto_aead *big_key_aead;
85 86

/*
87
 * Since changing the key affects the entire object, we need a mutex.
88
 */
89
static DEFINE_MUTEX(big_key_aead_lock);
90 91 92 93 94 95

/*
 * Encrypt/decrypt big_key data
 */
static int big_key_crypt(enum big_key_op op, u8 *data, size_t datalen, u8 *key)
{
96
	int ret;
97
	struct scatterlist sgio;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	struct aead_request *aead_req;
	/* We always use a zero nonce. The reason we can get away with this is
	 * because we're using a different randomly generated key for every
	 * different encryption. Notably, too, key_type_big_key doesn't define
	 * an .update function, so there's no chance we'll wind up reusing the
	 * key to encrypt updated data. Simply put: one key, one encryption.
	 */
	u8 zero_nonce[crypto_aead_ivsize(big_key_aead)];

	aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL);
	if (!aead_req)
		return -ENOMEM;

	memset(zero_nonce, 0, sizeof(zero_nonce));
	sg_init_one(&sgio, data, datalen + (op == BIG_KEY_ENC ? ENC_AUTHTAG_SIZE : 0));
	aead_request_set_crypt(aead_req, &sgio, &sgio, datalen, zero_nonce);
	aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
	aead_request_set_ad(aead_req, 0);

	mutex_lock(&big_key_aead_lock);
	if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) {
119 120 121 122
		ret = -EAGAIN;
		goto error;
	}
	if (op == BIG_KEY_ENC)
123
		ret = crypto_aead_encrypt(aead_req);
124
	else
125
		ret = crypto_aead_decrypt(aead_req);
126
error:
127 128
	mutex_unlock(&big_key_aead_lock);
	aead_request_free(aead_req);
129 130 131
	return ret;
}

132
/*
133
 * Preparse a big key
134
 */
135
int big_key_preparse(struct key_preparsed_payload *prep)
136
{
137
	struct path *path = (struct path *)&prep->payload.data[big_key_path];
138
	struct file *file;
139 140
	u8 *enckey;
	u8 *data = NULL;
141 142 143 144 145 146 147 148 149
	ssize_t written;
	size_t datalen = prep->datalen;
	int ret;

	ret = -EINVAL;
	if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data)
		goto error;

	/* Set an arbitrary quota */
150
	prep->quotalen = 16;
151

152
	prep->payload.data[big_key_len] = (void *)(unsigned long)datalen;
153 154 155 156 157

	if (datalen > BIG_KEY_FILE_THRESHOLD) {
		/* Create a shmem file to store the data in.  This will permit the data
		 * to be swapped out if needed.
		 *
158
		 * File content is stored encrypted with randomly generated key.
159
		 */
160
		size_t enclen = datalen + ENC_AUTHTAG_SIZE;
161
		loff_t pos = 0;
162 163 164 165 166 167 168 169 170 171 172 173

		data = kmalloc(enclen, GFP_KERNEL);
		if (!data)
			return -ENOMEM;
		memcpy(data, prep->data, datalen);

		/* generate random key */
		enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL);
		if (!enckey) {
			ret = -ENOMEM;
			goto error;
		}
174 175
		ret = get_random_bytes_wait(enckey, ENC_KEY_SIZE);
		if (unlikely(ret))
176 177 178
			goto err_enckey;

		/* encrypt aligned data */
179
		ret = big_key_crypt(BIG_KEY_ENC, data, datalen, enckey);
180 181 182 183 184
		if (ret)
			goto err_enckey;

		/* save aligned data to file */
		file = shmem_kernel_file_setup("", enclen, 0);
185 186
		if (IS_ERR(file)) {
			ret = PTR_ERR(file);
187
			goto err_enckey;
188
		}
189

190
		written = kernel_write(file, data, enclen, &pos);
191
		if (written != enclen) {
192
			ret = written;
193 194 195 196 197 198 199 200
			if (written >= 0)
				ret = -ENOMEM;
			goto err_fput;
		}

		/* Pin the mount and dentry to the key so that we can open it again
		 * later
		 */
201
		prep->payload.data[big_key_data] = enckey;
202 203 204
		*path = file->f_path;
		path_get(path);
		fput(file);
205
		kzfree(data);
206 207 208
	} else {
		/* Just store the data in a buffer */
		void *data = kmalloc(datalen, GFP_KERNEL);
209

210 211
		if (!data)
			return -ENOMEM;
212

213 214
		prep->payload.data[big_key_data] = data;
		memcpy(data, prep->data, prep->datalen);
215 216 217 218 219
	}
	return 0;

err_fput:
	fput(file);
220
err_enckey:
221
	kzfree(enckey);
222
error:
223
	kzfree(data);
224 225 226
	return ret;
}

227 228 229 230 231 232
/*
 * Clear preparsement.
 */
void big_key_free_preparse(struct key_preparsed_payload *prep)
{
	if (prep->datalen > BIG_KEY_FILE_THRESHOLD) {
233
		struct path *path = (struct path *)&prep->payload.data[big_key_path];
234

235 236
		path_put(path);
	}
237
	kzfree(prep->payload.data[big_key_data]);
238 239
}

240 241 242 243 244 245
/*
 * dispose of the links from a revoked keyring
 * - called with the key sem write-locked
 */
void big_key_revoke(struct key *key)
{
246
	struct path *path = (struct path *)&key->payload.data[big_key_path];
247 248 249

	/* clear the quota */
	key_payload_reserve(key, 0);
250
	if (key_is_positive(key) &&
251
	    (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD)
252 253 254 255 256 257 258 259
		vfs_truncate(path, 0);
}

/*
 * dispose of the data dangling from the corpse of a big_key key
 */
void big_key_destroy(struct key *key)
{
260 261
	size_t datalen = (size_t)key->payload.data[big_key_len];

262
	if (datalen > BIG_KEY_FILE_THRESHOLD) {
263
		struct path *path = (struct path *)&key->payload.data[big_key_path];
264

265 266 267 268
		path_put(path);
		path->mnt = NULL;
		path->dentry = NULL;
	}
269
	kzfree(key->payload.data[big_key_data]);
270
	key->payload.data[big_key_data] = NULL;
271 272 273 274 275 276 277
}

/*
 * describe the big_key key
 */
void big_key_describe(const struct key *key, struct seq_file *m)
{
278
	size_t datalen = (size_t)key->payload.data[big_key_len];
279 280 281

	seq_puts(m, key->description);

282
	if (key_is_positive(key))
283
		seq_printf(m, ": %zu [%s]",
284 285 286 287 288 289 290 291 292 293
			   datalen,
			   datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff");
}

/*
 * read the key data
 * - the key's semaphore is read-locked
 */
long big_key_read(const struct key *key, char __user *buffer, size_t buflen)
{
294
	size_t datalen = (size_t)key->payload.data[big_key_len];
295 296 297 298 299 300
	long ret;

	if (!buffer || buflen < datalen)
		return datalen;

	if (datalen > BIG_KEY_FILE_THRESHOLD) {
301
		struct path *path = (struct path *)&key->payload.data[big_key_path];
302
		struct file *file;
303 304
		u8 *data;
		u8 *enckey = (u8 *)key->payload.data[big_key_data];
305
		size_t enclen = datalen + ENC_AUTHTAG_SIZE;
306
		loff_t pos = 0;
307 308 309 310

		data = kmalloc(enclen, GFP_KERNEL);
		if (!data)
			return -ENOMEM;
311 312

		file = dentry_open(path, O_RDONLY, current_cred());
313 314 315 316
		if (IS_ERR(file)) {
			ret = PTR_ERR(file);
			goto error;
		}
317

318
		/* read file to kernel and decrypt */
319
		ret = kernel_read(file, data, enclen, &pos);
320
		if (ret >= 0 && ret != enclen) {
321
			ret = -EIO;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
			goto err_fput;
		}

		ret = big_key_crypt(BIG_KEY_DEC, data, enclen, enckey);
		if (ret)
			goto err_fput;

		ret = datalen;

		/* copy decrypted data to user */
		if (copy_to_user(buffer, data, datalen) != 0)
			ret = -EFAULT;

err_fput:
		fput(file);
error:
338
		kzfree(data);
339 340
	} else {
		ret = datalen;
341 342
		if (copy_to_user(buffer, key->payload.data[big_key_data],
				 datalen) != 0)
343 344 345 346 347 348
			ret = -EFAULT;
	}

	return ret;
}

349 350 351
/*
 * Register key type
 */
352 353
static int __init big_key_init(void)
{
354
	int ret;
355 356

	/* init block cipher */
357 358 359
	big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC);
	if (IS_ERR(big_key_aead)) {
		ret = PTR_ERR(big_key_aead);
360
		pr_err("Can't alloc crypto: %d\n", ret);
361 362 363 364 365 366
		return ret;
	}
	ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE);
	if (ret < 0) {
		pr_err("Can't set crypto auth tag len: %d\n", ret);
		goto free_aead;
367 368 369 370 371
	}

	ret = register_key_type(&key_type_big_key);
	if (ret < 0) {
		pr_err("Can't register type: %d\n", ret);
372
		goto free_aead;
373 374 375 376
	}

	return 0;

377 378
free_aead:
	crypto_free_aead(big_key_aead);
379 380 381
	return ret;
}

382
late_initcall(big_key_init);