blob: 6518ac9e5deb492f21d28c9a2e455f8f21174ef2 [file] [log] [blame]
Neeraj Sonic692cb92018-04-18 17:20:22 +05301/*
2 * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14/*
15 * Per-File-Key (PFK).
16 *
17 * This driver is responsible for overall management of various
18 * Per File Encryption variants that work on top of or as part of different
19 * file systems.
20 *
21 * The driver has the following purpose :
22 * 1) Define priorities between PFE's if more than one is enabled
23 * 2) Extract key information from inode
24 * 3) Load and manage various keys in ICE HW engine
25 * 4) It should be invoked from various layers in FS/BLOCK/STORAGE DRIVER
26 * that need to take decision on HW encryption management of the data
27 * Some examples:
28 * BLOCK LAYER: when it takes decision on whether 2 chunks can be united
29 * to one encryption / decryption request sent to the HW
30 *
31 * UFS DRIVER: when it need to configure ICE HW with a particular key slot
32 * to be used for encryption / decryption
33 *
34 * PFE variants can differ on particular way of storing the cryptographic info
35 * inside inode, actions to be taken upon file operations, etc., but the common
36 * properties are described above
37 *
38 */
39
40
41/* Uncomment the line below to enable debug messages */
42/* #define DEBUG 1 */
43#define pr_fmt(fmt) "pfk [%s]: " fmt, __func__
44
45#include <linux/module.h>
46#include <linux/fs.h>
47#include <linux/errno.h>
48#include <linux/printk.h>
49#include <linux/bio.h>
50#include <linux/security.h>
Neeraj Soni36c65122018-04-18 21:04:46 +053051#include <crypto/algapi.h>
Neeraj Sonic692cb92018-04-18 17:20:22 +053052#include <crypto/ice.h>
53
54#include <linux/pfk.h>
55
56#include "pfk_kc.h"
57#include "objsec.h"
58#include "pfk_ice.h"
59#include "pfk_ext4.h"
Neeraj Soni36c65122018-04-18 21:04:46 +053060#include "pfk_f2fs.h"
Neeraj Sonic692cb92018-04-18 17:20:22 +053061#include "pfk_internal.h"
Neeraj Soni36c65122018-04-18 21:04:46 +053062//#include "ext4.h"
Neeraj Sonic692cb92018-04-18 17:20:22 +053063
64static bool pfk_ready;
65
66
67/* might be replaced by a table when more than one cipher is supported */
68#define PFK_SUPPORTED_KEY_SIZE 32
69#define PFK_SUPPORTED_SALT_SIZE 32
70
71/* Various PFE types and function tables to support each one of them */
Neeraj Soni36c65122018-04-18 21:04:46 +053072enum pfe_type {EXT4_CRYPT_PFE, F2FS_CRYPT_PFE, INVALID_PFE};
Neeraj Sonic692cb92018-04-18 17:20:22 +053073
74typedef int (*pfk_parse_inode_type)(const struct bio *bio,
75 const struct inode *inode,
76 struct pfk_key_info *key_info,
77 enum ice_cryto_algo_mode *algo,
Neeraj Soniefb33112018-08-17 20:39:35 +053078 bool *is_pfe,
79 unsigned int *data_unit,
80 const char *storage_type);
Neeraj Sonic692cb92018-04-18 17:20:22 +053081
82typedef bool (*pfk_allow_merge_bio_type)(const struct bio *bio1,
83 const struct bio *bio2, const struct inode *inode1,
84 const struct inode *inode2);
85
86static const pfk_parse_inode_type pfk_parse_inode_ftable[] = {
87 /* EXT4_CRYPT_PFE */ &pfk_ext4_parse_inode,
Neeraj Soni36c65122018-04-18 21:04:46 +053088 /* F2FS_CRYPT_PFE */ &pfk_f2fs_parse_inode,
Neeraj Sonic692cb92018-04-18 17:20:22 +053089};
90
91static const pfk_allow_merge_bio_type pfk_allow_merge_bio_ftable[] = {
92 /* EXT4_CRYPT_PFE */ &pfk_ext4_allow_merge_bio,
Neeraj Soni36c65122018-04-18 21:04:46 +053093 /* F2FS_CRYPT_PFE */ &pfk_f2fs_allow_merge_bio,
Neeraj Sonic692cb92018-04-18 17:20:22 +053094};
95
96static void __exit pfk_exit(void)
97{
98 pfk_ready = false;
99 pfk_ext4_deinit();
Neeraj Soni36c65122018-04-18 21:04:46 +0530100 pfk_f2fs_deinit();
Neeraj Sonic692cb92018-04-18 17:20:22 +0530101 pfk_kc_deinit();
102}
103
104static int __init pfk_init(void)
105{
106
107 int ret = 0;
108
109 ret = pfk_ext4_init();
110 if (ret != 0)
111 goto fail;
112
Neeraj Soni36c65122018-04-18 21:04:46 +0530113 ret = pfk_f2fs_init();
114 if (ret != 0)
115 goto fail;
116
Neeraj Sonic692cb92018-04-18 17:20:22 +0530117 ret = pfk_kc_init();
118 if (ret != 0) {
119 pr_err("could init pfk key cache, error %d\n", ret);
120 pfk_ext4_deinit();
Neeraj Soni36c65122018-04-18 21:04:46 +0530121 pfk_f2fs_deinit();
Neeraj Sonic692cb92018-04-18 17:20:22 +0530122 goto fail;
123 }
124
125 pfk_ready = true;
126 pr_info("Driver initialized successfully\n");
127
128 return 0;
129
130fail:
131 pr_err("Failed to init driver\n");
132 return -ENODEV;
133}
134
135/*
136 * If more than one type is supported simultaneously, this function will also
137 * set the priority between them
138 */
139static enum pfe_type pfk_get_pfe_type(const struct inode *inode)
140{
141 if (!inode)
142 return INVALID_PFE;
143
144 if (pfk_is_ext4_type(inode))
145 return EXT4_CRYPT_PFE;
146
Neeraj Soni36c65122018-04-18 21:04:46 +0530147 if (pfk_is_f2fs_type(inode))
148 return F2FS_CRYPT_PFE;
149
Neeraj Sonic692cb92018-04-18 17:20:22 +0530150 return INVALID_PFE;
151}
152
153/**
154 * inode_to_filename() - get the filename from inode pointer.
155 * @inode: inode pointer
156 *
157 * it is used for debug prints.
158 *
159 * Return: filename string or "unknown".
160 */
161char *inode_to_filename(const struct inode *inode)
162{
163 struct dentry *dentry = NULL;
164 char *filename = NULL;
165
Neeraj Soni36c65122018-04-18 21:04:46 +0530166 if (!inode)
167 return "NULL";
168
Neeraj Sonic692cb92018-04-18 17:20:22 +0530169 if (hlist_empty(&inode->i_dentry))
170 return "unknown";
171
172 dentry = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
173 filename = dentry->d_iname;
174
175 return filename;
176}
177
178/**
179 * pfk_is_ready() - driver is initialized and ready.
180 *
181 * Return: true if the driver is ready.
182 */
183static inline bool pfk_is_ready(void)
184{
185 return pfk_ready;
186}
187
188/**
189 * pfk_bio_get_inode() - get the inode from a bio.
190 * @bio: Pointer to BIO structure.
191 *
192 * Walk the bio struct links to get the inode.
193 * Please note, that in general bio may consist of several pages from
194 * several files, but in our case we always assume that all pages come
195 * from the same file, since our logic ensures it. That is why we only
196 * walk through the first page to look for inode.
197 *
198 * Return: pointer to the inode struct if successful, or NULL otherwise.
199 *
200 */
201static struct inode *pfk_bio_get_inode(const struct bio *bio)
202{
Neeraj Sonic692cb92018-04-18 17:20:22 +0530203 if (!bio)
204 return NULL;
Neeraj Soni36c65122018-04-18 21:04:46 +0530205 if (!bio_has_data((struct bio *)bio))
206 return NULL;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530207 if (!bio->bi_io_vec)
208 return NULL;
209 if (!bio->bi_io_vec->bv_page)
210 return NULL;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530211
212 if (PageAnon(bio->bi_io_vec->bv_page)) {
213 struct inode *inode;
214
Neeraj Soni36c65122018-04-18 21:04:46 +0530215 /* Using direct-io (O_DIRECT) without page cache */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530216 inode = dio_bio_get_inode((struct bio *)bio);
217 pr_debug("inode on direct-io, inode = 0x%pK.\n", inode);
218
219 return inode;
220 }
221
Neeraj Soni36c65122018-04-18 21:04:46 +0530222 if (!page_mapping(bio->bi_io_vec->bv_page))
Neeraj Sonic692cb92018-04-18 17:20:22 +0530223 return NULL;
224
Jaegeuk Kim1158db32018-03-08 10:54:26 -0800225 return page_mapping(bio->bi_io_vec->bv_page)->host;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530226}
227
228/**
229 * pfk_key_size_to_key_type() - translate key size to key size enum
230 * @key_size: key size in bytes
231 * @key_size_type: pointer to store the output enum (can be null)
232 *
233 * return 0 in case of success, error otherwise (i.e not supported key size)
234 */
235int pfk_key_size_to_key_type(size_t key_size,
236 enum ice_crpto_key_size *key_size_type)
237{
238 /*
239 * currently only 32 bit key size is supported
240 * in the future, table with supported key sizes might
241 * be introduced
242 */
243
244 if (key_size != PFK_SUPPORTED_KEY_SIZE) {
245 pr_err("not supported key size %zu\n", key_size);
246 return -EINVAL;
247 }
248
249 if (key_size_type)
250 *key_size_type = ICE_CRYPTO_KEY_SIZE_256;
251
252 return 0;
253}
254
255/*
256 * Retrieves filesystem type from inode's superblock
257 */
258bool pfe_is_inode_filesystem_type(const struct inode *inode,
259 const char *fs_type)
260{
261 if (!inode || !fs_type)
262 return false;
263
264 if (!inode->i_sb)
265 return false;
266
267 if (!inode->i_sb->s_type)
268 return false;
269
270 return (strcmp(inode->i_sb->s_type->name, fs_type) == 0);
271}
272
Neeraj Soni36c65122018-04-18 21:04:46 +0530273/**
274 * pfk_get_key_for_bio() - get the encryption key to be used for a bio
275 *
276 * @bio: pointer to the BIO
277 * @key_info: pointer to the key information which will be filled in
278 * @algo_mode: optional pointer to the algorithm identifier which will be set
279 * @is_pfe: will be set to false if the BIO should be left unencrypted
280 *
281 * Return: 0 if a key is being used, otherwise a -errno value
282 */
283static int pfk_get_key_for_bio(const struct bio *bio,
284 struct pfk_key_info *key_info,
285 enum ice_cryto_algo_mode *algo_mode,
Neeraj Soniefb33112018-08-17 20:39:35 +0530286 bool *is_pfe, unsigned int *data_unit)
Neeraj Soni36c65122018-04-18 21:04:46 +0530287{
288 const struct inode *inode;
289 enum pfe_type which_pfe;
Neeraj Soniefb33112018-08-17 20:39:35 +0530290 char *s_type = NULL;
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700291 const struct blk_encryption_key *key = NULL;
Neeraj Soni36c65122018-04-18 21:04:46 +0530292
293 inode = pfk_bio_get_inode(bio);
294 which_pfe = pfk_get_pfe_type(inode);
Neeraj Soniefb33112018-08-17 20:39:35 +0530295 s_type = (char *)pfk_kc_get_storage_type();
Neeraj Soni36c65122018-04-18 21:04:46 +0530296
297 if (which_pfe != INVALID_PFE) {
298 /* Encrypted file; override ->bi_crypt_key */
299 pr_debug("parsing inode %lu with PFE type %d\n",
300 inode->i_ino, which_pfe);
301 return (*(pfk_parse_inode_ftable[which_pfe]))
Neeraj Soniefb33112018-08-17 20:39:35 +0530302 (bio, inode, key_info, algo_mode, is_pfe,
303 data_unit, (const char *)s_type);
Neeraj Soni36c65122018-04-18 21:04:46 +0530304 }
305
306 /*
307 * bio is not for an encrypted file. Use ->bi_crypt_key if it was set.
308 * Otherwise, don't encrypt/decrypt the bio.
309 */
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700310#ifdef CONFIG_DM_DEFAULT_KEY
Neeraj Soni36c65122018-04-18 21:04:46 +0530311 key = bio->bi_crypt_key;
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700312#endif
Neeraj Soni36c65122018-04-18 21:04:46 +0530313 if (!key) {
314 *is_pfe = false;
315 return -EINVAL;
316 }
317
318 /* Note: the "salt" is really just the second half of the XTS key. */
319 BUILD_BUG_ON(sizeof(key->raw) !=
320 PFK_SUPPORTED_KEY_SIZE + PFK_SUPPORTED_SALT_SIZE);
321 key_info->key = &key->raw[0];
322 key_info->key_size = PFK_SUPPORTED_KEY_SIZE;
323 key_info->salt = &key->raw[PFK_SUPPORTED_KEY_SIZE];
324 key_info->salt_size = PFK_SUPPORTED_SALT_SIZE;
325 if (algo_mode)
326 *algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
327 return 0;
328}
329
Neeraj Sonic692cb92018-04-18 17:20:22 +0530330
331/**
332 * pfk_load_key_start() - loads PFE encryption key to the ICE
333 * Can also be invoked from non
334 * PFE context, in this case it
335 * is not relevant and is_pfe
336 * flag is set to false
337 *
338 * @bio: Pointer to the BIO structure
339 * @ice_setting: Pointer to ice setting structure that will be filled with
340 * ice configuration values, including the index to which the key was loaded
341 * @is_pfe: will be false if inode is not relevant to PFE, in such a case
342 * it should be treated as non PFE by the block layer
343 *
344 * Returns the index where the key is stored in encryption hw and additional
345 * information that will be used later for configuration of the encryption hw.
346 *
347 * Must be followed by pfk_load_key_end when key is no longer used by ice
348 *
349 */
350int pfk_load_key_start(const struct bio *bio,
351 struct ice_crypto_setting *ice_setting, bool *is_pfe,
Neeraj Sonie3af8d52018-09-05 14:32:49 +0530352 bool async, int ice_rev)
Neeraj Sonic692cb92018-04-18 17:20:22 +0530353{
354 int ret = 0;
355 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
356 enum ice_cryto_algo_mode algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
357 enum ice_crpto_key_size key_size_type = 0;
Neeraj Soniefb33112018-08-17 20:39:35 +0530358 unsigned int data_unit = 1 << ICE_CRYPTO_DATA_UNIT_512_B;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530359 u32 key_index = 0;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530360
361 if (!is_pfe) {
362 pr_err("is_pfe is NULL\n");
363 return -EINVAL;
364 }
365
366 /*
367 * only a few errors below can indicate that
368 * this function was not invoked within PFE context,
369 * otherwise we will consider it PFE
370 */
371 *is_pfe = true;
372
373 if (!pfk_is_ready())
374 return -ENODEV;
375
376 if (!ice_setting) {
377 pr_err("ice setting is NULL\n");
378 return -EINVAL;
379 }
380
Neeraj Soniefb33112018-08-17 20:39:35 +0530381 ret = pfk_get_key_for_bio(bio, &key_info, &algo_mode, is_pfe,
382 &data_unit);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530383
Neeraj Sonic692cb92018-04-18 17:20:22 +0530384 if (ret != 0)
385 return ret;
386
387 ret = pfk_key_size_to_key_type(key_info.key_size, &key_size_type);
388 if (ret != 0)
389 return ret;
390
391 ret = pfk_kc_load_key_start(key_info.key, key_info.key_size,
Neeraj Soniefb33112018-08-17 20:39:35 +0530392 key_info.salt, key_info.salt_size, &key_index, async,
Neeraj Sonie3af8d52018-09-05 14:32:49 +0530393 data_unit, ice_rev);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530394 if (ret) {
395 if (ret != -EBUSY && ret != -EAGAIN)
396 pr_err("start: could not load key into pfk key cache, error %d\n",
397 ret);
398
399 return ret;
400 }
401
402 ice_setting->key_size = key_size_type;
403 ice_setting->algo_mode = algo_mode;
404 /* hardcoded for now */
405 ice_setting->key_mode = ICE_CRYPTO_USE_LUT_SW_KEY;
406 ice_setting->key_index = key_index;
407
408 pr_debug("loaded key for file %s key_index %d\n",
Neeraj Soni36c65122018-04-18 21:04:46 +0530409 inode_to_filename(pfk_bio_get_inode(bio)), key_index);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530410
411 return 0;
412}
413
414/**
415 * pfk_load_key_end() - marks the PFE key as no longer used by ICE
416 * Can also be invoked from non
417 * PFE context, in this case it is not
418 * relevant and is_pfe flag is
419 * set to false
420 *
421 * @bio: Pointer to the BIO structure
422 * @is_pfe: Pointer to is_pfe flag, which will be true if function was invoked
423 * from PFE context
424 */
425int pfk_load_key_end(const struct bio *bio, bool *is_pfe)
426{
427 int ret = 0;
428 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
Neeraj Sonic692cb92018-04-18 17:20:22 +0530429
430 if (!is_pfe) {
431 pr_err("is_pfe is NULL\n");
432 return -EINVAL;
433 }
434
435 /* only a few errors below can indicate that
436 * this function was not invoked within PFE context,
437 * otherwise we will consider it PFE
438 */
439 *is_pfe = true;
440
441 if (!pfk_is_ready())
442 return -ENODEV;
443
Neeraj Soniefb33112018-08-17 20:39:35 +0530444 ret = pfk_get_key_for_bio(bio, &key_info, NULL, is_pfe, NULL);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530445 if (ret != 0)
446 return ret;
447
448 pfk_kc_load_key_end(key_info.key, key_info.key_size,
449 key_info.salt, key_info.salt_size);
450
451 pr_debug("finished using key for file %s\n",
Neeraj Soni36c65122018-04-18 21:04:46 +0530452 inode_to_filename(pfk_bio_get_inode(bio)));
Neeraj Sonic692cb92018-04-18 17:20:22 +0530453
454 return 0;
455}
456
457/**
458 * pfk_allow_merge_bio() - Check if 2 BIOs can be merged.
459 * @bio1: Pointer to first BIO structure.
460 * @bio2: Pointer to second BIO structure.
461 *
462 * Prevent merging of BIOs from encrypted and non-encrypted
463 * files, or files encrypted with different key.
464 * Also prevent non encrypted and encrypted data from the same file
465 * to be merged (ecryptfs header if stored inside file should be non
466 * encrypted)
467 * This API is called by the file system block layer.
468 *
469 * Return: true if the BIOs allowed to be merged, false
470 * otherwise.
471 */
472bool pfk_allow_merge_bio(const struct bio *bio1, const struct bio *bio2)
473{
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700474 const struct blk_encryption_key *key1 = NULL;
475 const struct blk_encryption_key *key2 = NULL;
Neeraj Soni36c65122018-04-18 21:04:46 +0530476 const struct inode *inode1;
477 const struct inode *inode2;
478 enum pfe_type which_pfe1;
479 enum pfe_type which_pfe2;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530480
Jaegeuk Kim3bd012f2018-04-20 19:26:09 -0700481#ifdef CONFIG_DM_DEFAULT_KEY
482 key1 = bio1->bi_crypt_key;
483 key2 = bio2->bi_crypt_key;
484#endif
485
Neeraj Sonic692cb92018-04-18 17:20:22 +0530486 if (!pfk_is_ready())
487 return false;
488
489 if (!bio1 || !bio2)
490 return false;
491
492 if (bio1 == bio2)
493 return true;
494
Neeraj Soni36c65122018-04-18 21:04:46 +0530495 key1 = bio1->bi_crypt_key;
496 key2 = bio2->bi_crypt_key;
497
Neeraj Sonic692cb92018-04-18 17:20:22 +0530498 inode1 = pfk_bio_get_inode(bio1);
499 inode2 = pfk_bio_get_inode(bio2);
500
Neeraj Sonic692cb92018-04-18 17:20:22 +0530501 which_pfe1 = pfk_get_pfe_type(inode1);
502 which_pfe2 = pfk_get_pfe_type(inode2);
503
Neeraj Soni36c65122018-04-18 21:04:46 +0530504 /*
505 * If one bio is for an encrypted file and the other is for a different
506 * type of encrypted file or for blocks that are not part of an
507 * encrypted file, do not merge.
508 */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530509 if (which_pfe1 != which_pfe2)
510 return false;
511
Neeraj Soni36c65122018-04-18 21:04:46 +0530512 if (which_pfe1 != INVALID_PFE) {
513 /* Both bios are for the same type of encrypted file. */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530514 return (*(pfk_allow_merge_bio_ftable[which_pfe1]))(bio1, bio2,
515 inode1, inode2);
Neeraj Soni36c65122018-04-18 21:04:46 +0530516 }
517
518 /*
519 * Neither bio is for an encrypted file. Merge only if the default keys
520 * are the same (or both are NULL).
521 */
522 return key1 == key2 ||
523 (key1 && key2 &&
524 !crypto_memneq(key1->raw, key2->raw, sizeof(key1->raw)));
Neeraj Sonic692cb92018-04-18 17:20:22 +0530525}
Neeraj Soni36c65122018-04-18 21:04:46 +0530526
Neeraj Sonic692cb92018-04-18 17:20:22 +0530527/**
528 * Flush key table on storage core reset. During core reset key configuration
529 * is lost in ICE. We need to flash the cache, so that the keys will be
530 * reconfigured again for every subsequent transaction
531 */
532void pfk_clear_on_reset(void)
533{
534 if (!pfk_is_ready())
535 return;
536
537 pfk_kc_clear_on_reset();
538}
539
540module_init(pfk_init);
541module_exit(pfk_exit);
542
543MODULE_LICENSE("GPL v2");
544MODULE_DESCRIPTION("Per-File-Key driver");