blob: 0cdfd306417fe478c041901f3e5f69e3eee6188a [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,
Neeraj Soniefb33112018-08-17 20:39:35 +053079 const char *storage_type);
Neeraj Sonic692cb92018-04-18 17:20:22 +053080
81typedef bool (*pfk_allow_merge_bio_type)(const struct bio *bio1,
82 const struct bio *bio2, const struct inode *inode1,
83 const struct inode *inode2);
84
85static const pfk_parse_inode_type pfk_parse_inode_ftable[] = {
86 /* EXT4_CRYPT_PFE */ &pfk_ext4_parse_inode,
Neeraj Soni36c65122018-04-18 21:04:46 +053087 /* F2FS_CRYPT_PFE */ &pfk_f2fs_parse_inode,
Neeraj Sonic692cb92018-04-18 17:20:22 +053088};
89
90static const pfk_allow_merge_bio_type pfk_allow_merge_bio_ftable[] = {
91 /* EXT4_CRYPT_PFE */ &pfk_ext4_allow_merge_bio,
Neeraj Soni36c65122018-04-18 21:04:46 +053092 /* F2FS_CRYPT_PFE */ &pfk_f2fs_allow_merge_bio,
Neeraj Sonic692cb92018-04-18 17:20:22 +053093};
94
95static void __exit pfk_exit(void)
96{
97 pfk_ready = false;
98 pfk_ext4_deinit();
Neeraj Soni36c65122018-04-18 21:04:46 +053099 pfk_f2fs_deinit();
Neeraj Sonic692cb92018-04-18 17:20:22 +0530100 pfk_kc_deinit();
101}
102
103static int __init pfk_init(void)
104{
105
106 int ret = 0;
107
108 ret = pfk_ext4_init();
109 if (ret != 0)
110 goto fail;
111
Neeraj Soni36c65122018-04-18 21:04:46 +0530112 ret = pfk_f2fs_init();
113 if (ret != 0)
114 goto fail;
115
Neeraj Sonic692cb92018-04-18 17:20:22 +0530116 ret = pfk_kc_init();
117 if (ret != 0) {
118 pr_err("could init pfk key cache, error %d\n", ret);
119 pfk_ext4_deinit();
Neeraj Soni36c65122018-04-18 21:04:46 +0530120 pfk_f2fs_deinit();
Neeraj Sonic692cb92018-04-18 17:20:22 +0530121 goto fail;
122 }
123
124 pfk_ready = true;
125 pr_info("Driver initialized successfully\n");
126
127 return 0;
128
129fail:
130 pr_err("Failed to init driver\n");
131 return -ENODEV;
132}
133
134/*
135 * If more than one type is supported simultaneously, this function will also
136 * set the priority between them
137 */
138static enum pfe_type pfk_get_pfe_type(const struct inode *inode)
139{
140 if (!inode)
141 return INVALID_PFE;
142
143 if (pfk_is_ext4_type(inode))
144 return EXT4_CRYPT_PFE;
145
Neeraj Soni36c65122018-04-18 21:04:46 +0530146 if (pfk_is_f2fs_type(inode))
147 return F2FS_CRYPT_PFE;
148
Neeraj Sonic692cb92018-04-18 17:20:22 +0530149 return INVALID_PFE;
150}
151
152/**
153 * inode_to_filename() - get the filename from inode pointer.
154 * @inode: inode pointer
155 *
156 * it is used for debug prints.
157 *
158 * Return: filename string or "unknown".
159 */
160char *inode_to_filename(const struct inode *inode)
161{
162 struct dentry *dentry = NULL;
163 char *filename = NULL;
164
Neeraj Soni36c65122018-04-18 21:04:46 +0530165 if (!inode)
166 return "NULL";
167
Neeraj Sonic692cb92018-04-18 17:20:22 +0530168 if (hlist_empty(&inode->i_dentry))
169 return "unknown";
170
171 dentry = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
172 filename = dentry->d_iname;
173
174 return filename;
175}
176
177/**
178 * pfk_is_ready() - driver is initialized and ready.
179 *
180 * Return: true if the driver is ready.
181 */
182static inline bool pfk_is_ready(void)
183{
184 return pfk_ready;
185}
186
187/**
188 * pfk_bio_get_inode() - get the inode from a bio.
189 * @bio: Pointer to BIO structure.
190 *
191 * Walk the bio struct links to get the inode.
192 * Please note, that in general bio may consist of several pages from
193 * several files, but in our case we always assume that all pages come
194 * from the same file, since our logic ensures it. That is why we only
195 * walk through the first page to look for inode.
196 *
197 * Return: pointer to the inode struct if successful, or NULL otherwise.
198 *
199 */
200static struct inode *pfk_bio_get_inode(const struct bio *bio)
201{
Neeraj Sonic692cb92018-04-18 17:20:22 +0530202 if (!bio)
203 return NULL;
Neeraj Soni36c65122018-04-18 21:04:46 +0530204 if (!bio_has_data((struct bio *)bio))
205 return NULL;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530206 if (!bio->bi_io_vec)
207 return NULL;
208 if (!bio->bi_io_vec->bv_page)
209 return NULL;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530210
211 if (PageAnon(bio->bi_io_vec->bv_page)) {
212 struct inode *inode;
213
Neeraj Soni36c65122018-04-18 21:04:46 +0530214 /* Using direct-io (O_DIRECT) without page cache */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530215 inode = dio_bio_get_inode((struct bio *)bio);
216 pr_debug("inode on direct-io, inode = 0x%pK.\n", inode);
217
218 return inode;
219 }
220
Neeraj Soni36c65122018-04-18 21:04:46 +0530221 if (!page_mapping(bio->bi_io_vec->bv_page))
Neeraj Sonic692cb92018-04-18 17:20:22 +0530222 return NULL;
223
Jaegeuk Kim1158db32018-03-08 10:54:26 -0800224 return page_mapping(bio->bi_io_vec->bv_page)->host;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530225}
226
227/**
228 * pfk_key_size_to_key_type() - translate key size to key size enum
229 * @key_size: key size in bytes
230 * @key_size_type: pointer to store the output enum (can be null)
231 *
232 * return 0 in case of success, error otherwise (i.e not supported key size)
233 */
234int pfk_key_size_to_key_type(size_t key_size,
235 enum ice_crpto_key_size *key_size_type)
236{
237 /*
238 * currently only 32 bit key size is supported
239 * in the future, table with supported key sizes might
240 * be introduced
241 */
242
243 if (key_size != PFK_SUPPORTED_KEY_SIZE) {
244 pr_err("not supported key size %zu\n", key_size);
245 return -EINVAL;
246 }
247
248 if (key_size_type)
249 *key_size_type = ICE_CRYPTO_KEY_SIZE_256;
250
251 return 0;
252}
253
254/*
255 * Retrieves filesystem type from inode's superblock
256 */
257bool pfe_is_inode_filesystem_type(const struct inode *inode,
258 const char *fs_type)
259{
260 if (!inode || !fs_type)
261 return false;
262
263 if (!inode->i_sb)
264 return false;
265
266 if (!inode->i_sb->s_type)
267 return false;
268
269 return (strcmp(inode->i_sb->s_type->name, fs_type) == 0);
270}
271
Neeraj Soni36c65122018-04-18 21:04:46 +0530272/**
273 * pfk_get_key_for_bio() - get the encryption key to be used for a bio
274 *
275 * @bio: pointer to the BIO
276 * @key_info: pointer to the key information which will be filled in
277 * @algo_mode: optional pointer to the algorithm identifier which will be set
278 * @is_pfe: will be set to false if the BIO should be left unencrypted
279 *
280 * Return: 0 if a key is being used, otherwise a -errno value
281 */
282static int pfk_get_key_for_bio(const struct bio *bio,
283 struct pfk_key_info *key_info,
284 enum ice_cryto_algo_mode *algo_mode,
Neeraj Soniefb33112018-08-17 20:39:35 +0530285 bool *is_pfe, unsigned int *data_unit)
Neeraj Soni36c65122018-04-18 21:04:46 +0530286{
287 const struct inode *inode;
288 enum pfe_type which_pfe;
289 const struct blk_encryption_key *key;
Neeraj Soniefb33112018-08-17 20:39:35 +0530290 char *s_type = NULL;
Neeraj Soni36c65122018-04-18 21:04:46 +0530291
292 inode = pfk_bio_get_inode(bio);
293 which_pfe = pfk_get_pfe_type(inode);
Neeraj Soniefb33112018-08-17 20:39:35 +0530294 s_type = (char *)pfk_kc_get_storage_type();
Neeraj Soni36c65122018-04-18 21:04:46 +0530295
Neeraj Soni75e59c92018-10-22 20:07:50 +0530296 if (data_unit && (bio_dun(bio) ||
297 !memcmp(s_type, "ufs", strlen("ufs"))))
298 *data_unit = 1 << ICE_CRYPTO_DATA_UNIT_4_KB;
299
Neeraj Soni36c65122018-04-18 21:04:46 +0530300 if (which_pfe != INVALID_PFE) {
301 /* Encrypted file; override ->bi_crypt_key */
302 pr_debug("parsing inode %lu with PFE type %d\n",
303 inode->i_ino, which_pfe);
304 return (*(pfk_parse_inode_ftable[which_pfe]))
Neeraj Soniefb33112018-08-17 20:39:35 +0530305 (bio, inode, key_info, algo_mode, is_pfe,
Neeraj Soni75e59c92018-10-22 20:07:50 +0530306 (const char *)s_type);
Neeraj Soni36c65122018-04-18 21:04:46 +0530307 }
308
309 /*
310 * bio is not for an encrypted file. Use ->bi_crypt_key if it was set.
311 * Otherwise, don't encrypt/decrypt the bio.
312 */
313 key = bio->bi_crypt_key;
314 if (!key) {
315 *is_pfe = false;
316 return -EINVAL;
317 }
318
319 /* Note: the "salt" is really just the second half of the XTS key. */
320 BUILD_BUG_ON(sizeof(key->raw) !=
321 PFK_SUPPORTED_KEY_SIZE + PFK_SUPPORTED_SALT_SIZE);
322 key_info->key = &key->raw[0];
323 key_info->key_size = PFK_SUPPORTED_KEY_SIZE;
324 key_info->salt = &key->raw[PFK_SUPPORTED_KEY_SIZE];
325 key_info->salt_size = PFK_SUPPORTED_SALT_SIZE;
326 if (algo_mode)
327 *algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
328 return 0;
329}
330
Neeraj Sonic692cb92018-04-18 17:20:22 +0530331
332/**
333 * pfk_load_key_start() - loads PFE encryption key to the ICE
334 * Can also be invoked from non
335 * PFE context, in this case it
336 * is not relevant and is_pfe
337 * flag is set to false
338 *
339 * @bio: Pointer to the BIO structure
340 * @ice_setting: Pointer to ice setting structure that will be filled with
341 * ice configuration values, including the index to which the key was loaded
342 * @is_pfe: will be false if inode is not relevant to PFE, in such a case
343 * it should be treated as non PFE by the block layer
344 *
345 * Returns the index where the key is stored in encryption hw and additional
346 * information that will be used later for configuration of the encryption hw.
347 *
348 * Must be followed by pfk_load_key_end when key is no longer used by ice
349 *
350 */
351int pfk_load_key_start(const struct bio *bio,
352 struct ice_crypto_setting *ice_setting, bool *is_pfe,
Neeraj Sonie3af8d52018-09-05 14:32:49 +0530353 bool async, int ice_rev)
Neeraj Sonic692cb92018-04-18 17:20:22 +0530354{
355 int ret = 0;
356 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
357 enum ice_cryto_algo_mode algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
358 enum ice_crpto_key_size key_size_type = 0;
Neeraj Soniefb33112018-08-17 20:39:35 +0530359 unsigned int data_unit = 1 << ICE_CRYPTO_DATA_UNIT_512_B;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530360 u32 key_index = 0;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530361
362 if (!is_pfe) {
363 pr_err("is_pfe is NULL\n");
364 return -EINVAL;
365 }
366
367 /*
368 * only a few errors below can indicate that
369 * this function was not invoked within PFE context,
370 * otherwise we will consider it PFE
371 */
372 *is_pfe = true;
373
374 if (!pfk_is_ready())
375 return -ENODEV;
376
377 if (!ice_setting) {
378 pr_err("ice setting is NULL\n");
379 return -EINVAL;
380 }
381
Neeraj Soniefb33112018-08-17 20:39:35 +0530382 ret = pfk_get_key_for_bio(bio, &key_info, &algo_mode, is_pfe,
383 &data_unit);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530384
Neeraj Sonic692cb92018-04-18 17:20:22 +0530385 if (ret != 0)
386 return ret;
387
388 ret = pfk_key_size_to_key_type(key_info.key_size, &key_size_type);
389 if (ret != 0)
390 return ret;
391
392 ret = pfk_kc_load_key_start(key_info.key, key_info.key_size,
Neeraj Soniefb33112018-08-17 20:39:35 +0530393 key_info.salt, key_info.salt_size, &key_index, async,
Neeraj Sonie3af8d52018-09-05 14:32:49 +0530394 data_unit, ice_rev);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530395 if (ret) {
396 if (ret != -EBUSY && ret != -EAGAIN)
397 pr_err("start: could not load key into pfk key cache, error %d\n",
398 ret);
399
400 return ret;
401 }
402
403 ice_setting->key_size = key_size_type;
404 ice_setting->algo_mode = algo_mode;
405 /* hardcoded for now */
406 ice_setting->key_mode = ICE_CRYPTO_USE_LUT_SW_KEY;
407 ice_setting->key_index = key_index;
408
409 pr_debug("loaded key for file %s key_index %d\n",
Neeraj Soni36c65122018-04-18 21:04:46 +0530410 inode_to_filename(pfk_bio_get_inode(bio)), key_index);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530411
412 return 0;
413}
414
415/**
416 * pfk_load_key_end() - marks the PFE key as no longer used by ICE
417 * Can also be invoked from non
418 * PFE context, in this case it is not
419 * relevant and is_pfe flag is
420 * set to false
421 *
422 * @bio: Pointer to the BIO structure
423 * @is_pfe: Pointer to is_pfe flag, which will be true if function was invoked
424 * from PFE context
425 */
426int pfk_load_key_end(const struct bio *bio, bool *is_pfe)
427{
428 int ret = 0;
429 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
Neeraj Sonic692cb92018-04-18 17:20:22 +0530430
431 if (!is_pfe) {
432 pr_err("is_pfe is NULL\n");
433 return -EINVAL;
434 }
435
436 /* only a few errors below can indicate that
437 * this function was not invoked within PFE context,
438 * otherwise we will consider it PFE
439 */
440 *is_pfe = true;
441
442 if (!pfk_is_ready())
443 return -ENODEV;
444
Neeraj Soniefb33112018-08-17 20:39:35 +0530445 ret = pfk_get_key_for_bio(bio, &key_info, NULL, is_pfe, NULL);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530446 if (ret != 0)
447 return ret;
448
449 pfk_kc_load_key_end(key_info.key, key_info.key_size,
450 key_info.salt, key_info.salt_size);
451
452 pr_debug("finished using key for file %s\n",
Neeraj Soni36c65122018-04-18 21:04:46 +0530453 inode_to_filename(pfk_bio_get_inode(bio)));
Neeraj Sonic692cb92018-04-18 17:20:22 +0530454
455 return 0;
456}
457
458/**
459 * pfk_allow_merge_bio() - Check if 2 BIOs can be merged.
460 * @bio1: Pointer to first BIO structure.
461 * @bio2: Pointer to second BIO structure.
462 *
463 * Prevent merging of BIOs from encrypted and non-encrypted
464 * files, or files encrypted with different key.
465 * Also prevent non encrypted and encrypted data from the same file
466 * to be merged (ecryptfs header if stored inside file should be non
467 * encrypted)
468 * This API is called by the file system block layer.
469 *
470 * Return: true if the BIOs allowed to be merged, false
471 * otherwise.
472 */
473bool pfk_allow_merge_bio(const struct bio *bio1, const struct bio *bio2)
474{
Neeraj Soni36c65122018-04-18 21:04:46 +0530475 const struct blk_encryption_key *key1;
476 const struct blk_encryption_key *key2;
477 const struct inode *inode1;
478 const struct inode *inode2;
479 enum pfe_type which_pfe1;
480 enum pfe_type which_pfe2;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530481
482 if (!pfk_is_ready())
483 return false;
484
485 if (!bio1 || !bio2)
486 return false;
487
488 if (bio1 == bio2)
489 return true;
490
Neeraj Soni36c65122018-04-18 21:04:46 +0530491 key1 = bio1->bi_crypt_key;
492 key2 = bio2->bi_crypt_key;
493
Neeraj Sonic692cb92018-04-18 17:20:22 +0530494 inode1 = pfk_bio_get_inode(bio1);
495 inode2 = pfk_bio_get_inode(bio2);
496
Neeraj Sonic692cb92018-04-18 17:20:22 +0530497 which_pfe1 = pfk_get_pfe_type(inode1);
498 which_pfe2 = pfk_get_pfe_type(inode2);
499
Neeraj Soni36c65122018-04-18 21:04:46 +0530500 /*
501 * If one bio is for an encrypted file and the other is for a different
502 * type of encrypted file or for blocks that are not part of an
503 * encrypted file, do not merge.
504 */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530505 if (which_pfe1 != which_pfe2)
506 return false;
507
Neeraj Soni36c65122018-04-18 21:04:46 +0530508 if (which_pfe1 != INVALID_PFE) {
509 /* Both bios are for the same type of encrypted file. */
Neeraj Sonic692cb92018-04-18 17:20:22 +0530510 return (*(pfk_allow_merge_bio_ftable[which_pfe1]))(bio1, bio2,
511 inode1, inode2);
Neeraj Soni36c65122018-04-18 21:04:46 +0530512 }
513
514 /*
515 * Neither bio is for an encrypted file. Merge only if the default keys
516 * are the same (or both are NULL).
517 */
518 return key1 == key2 ||
519 (key1 && key2 &&
520 !crypto_memneq(key1->raw, key2->raw, sizeof(key1->raw)));
Neeraj Sonic692cb92018-04-18 17:20:22 +0530521}
Neeraj Soni36c65122018-04-18 21:04:46 +0530522
Neeraj Sonic692cb92018-04-18 17:20:22 +0530523/**
524 * Flush key table on storage core reset. During core reset key configuration
525 * is lost in ICE. We need to flash the cache, so that the keys will be
526 * reconfigured again for every subsequent transaction
527 */
528void pfk_clear_on_reset(void)
529{
530 if (!pfk_is_ready())
531 return;
532
533 pfk_kc_clear_on_reset();
534}
535
536module_init(pfk_init);
537module_exit(pfk_exit);
538
539MODULE_LICENSE("GPL v2");
540MODULE_DESCRIPTION("Per-File-Key driver");