blob: 218b2830b632240b4628b83007218ed6f7c8f4ee [file] [log] [blame]
Zhen Kongee7bdc62019-03-14 10:55:19 -07001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
4 */
5
6/*
7 * Per-File-Key (PFK).
8 *
9 * This driver is responsible for overall management of various
10 * Per File Encryption variants that work on top of or as part of different
11 * file systems.
12 *
13 * The driver has the following purpose :
14 * 1) Define priorities between PFE's if more than one is enabled
15 * 2) Extract key information from inode
16 * 3) Load and manage various keys in ICE HW engine
17 * 4) It should be invoked from various layers in FS/BLOCK/STORAGE DRIVER
18 * that need to take decision on HW encryption management of the data
19 * Some examples:
20 * BLOCK LAYER: when it takes decision on whether 2 chunks can be united
21 * to one encryption / decryption request sent to the HW
22 *
23 * UFS DRIVER: when it need to configure ICE HW with a particular key slot
24 * to be used for encryption / decryption
25 *
26 * PFE variants can differ on particular way of storing the cryptographic info
27 * inside inode, actions to be taken upon file operations, etc., but the common
28 * properties are described above
29 *
30 */
31
32#define pr_fmt(fmt) "pfk [%s]: " fmt, __func__
33
34#include <linux/module.h>
35#include <linux/fs.h>
36#include <linux/errno.h>
37#include <linux/printk.h>
38#include <linux/bio.h>
39#include <linux/security.h>
40#include <crypto/algapi.h>
41#include <crypto/ice.h>
42
43#include <linux/pfk.h>
44
45#include "pfk_kc.h"
46#include "objsec.h"
47#include "pfk_ice.h"
48#include "pfk_ext4.h"
49#include "pfk_f2fs.h"
50#include "pfk_internal.h"
51
52static bool pfk_ready;
53
54
55/* might be replaced by a table when more than one cipher is supported */
56#define PFK_SUPPORTED_KEY_SIZE 32
57#define PFK_SUPPORTED_SALT_SIZE 32
58
59/* Various PFE types and function tables to support each one of them */
60enum pfe_type {EXT4_CRYPT_PFE, F2FS_CRYPT_PFE, INVALID_PFE};
61
62typedef int (*pfk_parse_inode_type)(const struct bio *bio,
63 const struct inode *inode,
64 struct pfk_key_info *key_info,
65 enum ice_cryto_algo_mode *algo,
66 bool *is_pfe);
67
68typedef bool (*pfk_allow_merge_bio_type)(const struct bio *bio1,
69 const struct bio *bio2, const struct inode *inode1,
70 const struct inode *inode2);
71
72static const pfk_parse_inode_type pfk_parse_inode_ftable[] = {
73 &pfk_ext4_parse_inode, /* EXT4_CRYPT_PFE */
74 &pfk_f2fs_parse_inode, /* F2FS_CRYPT_PFE */
75};
76
77static const pfk_allow_merge_bio_type pfk_allow_merge_bio_ftable[] = {
78 &pfk_ext4_allow_merge_bio, /* EXT4_CRYPT_PFE */
79 &pfk_f2fs_allow_merge_bio, /* F2FS_CRYPT_PFE */
80};
81
82static void __exit pfk_exit(void)
83{
84 pfk_ready = false;
85 pfk_ext4_deinit();
86 pfk_f2fs_deinit();
87 pfk_kc_deinit();
88}
89
90static int __init pfk_init(void)
91{
92 int ret = 0;
93
94 ret = pfk_ext4_init();
95 if (ret != 0)
96 goto fail;
97
98 ret = pfk_f2fs_init();
99 if (ret != 0)
100 goto fail;
101
102 ret = pfk_kc_init();
103 if (ret != 0) {
104 pr_err("could init pfk key cache, error %d\n", ret);
105 pfk_ext4_deinit();
106 pfk_f2fs_deinit();
107 goto fail;
108 }
109
110 pfk_ready = true;
111 pr_debug("Driver initialized successfully\n");
112
113 return 0;
114
115fail:
116 pr_err("Failed to init driver\n");
117 return -ENODEV;
118}
119
120/*
121 * If more than one type is supported simultaneously, this function will also
122 * set the priority between them
123 */
124static enum pfe_type pfk_get_pfe_type(const struct inode *inode)
125{
126 if (!inode)
127 return INVALID_PFE;
128
129 if (pfk_is_ext4_type(inode))
130 return EXT4_CRYPT_PFE;
131
132 if (pfk_is_f2fs_type(inode))
133 return F2FS_CRYPT_PFE;
134
135 return INVALID_PFE;
136}
137
138/**
139 * inode_to_filename() - get the filename from inode pointer.
140 * @inode: inode pointer
141 *
142 * it is used for debug prints.
143 *
144 * Return: filename string or "unknown".
145 */
146char *inode_to_filename(const struct inode *inode)
147{
148 struct dentry *dentry = NULL;
149 char *filename = NULL;
150
151 if (!inode)
152 return "NULL";
153
154 if (hlist_empty(&inode->i_dentry))
155 return "unknown";
156
157 dentry = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
158 filename = dentry->d_iname;
159
160 return filename;
161}
162
163/**
164 * pfk_is_ready() - driver is initialized and ready.
165 *
166 * Return: true if the driver is ready.
167 */
168static inline bool pfk_is_ready(void)
169{
170 return pfk_ready;
171}
172
173/**
174 * pfk_bio_get_inode() - get the inode from a bio.
175 * @bio: Pointer to BIO structure.
176 *
177 * Walk the bio struct links to get the inode.
178 * Please note, that in general bio may consist of several pages from
179 * several files, but in our case we always assume that all pages come
180 * from the same file, since our logic ensures it. That is why we only
181 * walk through the first page to look for inode.
182 *
183 * Return: pointer to the inode struct if successful, or NULL otherwise.
184 *
185 */
186static struct inode *pfk_bio_get_inode(const struct bio *bio)
187{
188 if (!bio)
189 return NULL;
190 if (!bio_has_data((struct bio *)bio))
191 return NULL;
192 if (!bio->bi_io_vec)
193 return NULL;
194 if (!bio->bi_io_vec->bv_page)
195 return NULL;
196
197 if (PageAnon(bio->bi_io_vec->bv_page)) {
198 struct inode *inode;
199
200 /* Using direct-io (O_DIRECT) without page cache */
201 inode = dio_bio_get_inode((struct bio *)bio);
202 pr_debug("inode on direct-io, inode = 0x%pK.\n", inode);
203
204 return inode;
205 }
206
207 if (!page_mapping(bio->bi_io_vec->bv_page))
208 return NULL;
209
210 return page_mapping(bio->bi_io_vec->bv_page)->host;
211}
212
213/**
214 * pfk_key_size_to_key_type() - translate key size to key size enum
215 * @key_size: key size in bytes
216 * @key_size_type: pointer to store the output enum (can be null)
217 *
218 * return 0 in case of success, error otherwise (i.e not supported key size)
219 */
220int pfk_key_size_to_key_type(size_t key_size,
221 enum ice_crpto_key_size *key_size_type)
222{
223 /*
224 * currently only 32 bit key size is supported
225 * in the future, table with supported key sizes might
226 * be introduced
227 */
228
229 if (key_size != PFK_SUPPORTED_KEY_SIZE) {
230 pr_err("not supported key size %zu\n", key_size);
231 return -EINVAL;
232 }
233
234 if (key_size_type)
235 *key_size_type = ICE_CRYPTO_KEY_SIZE_256;
236
237 return 0;
238}
239
240/*
241 * Retrieves filesystem type from inode's superblock
242 */
243bool pfe_is_inode_filesystem_type(const struct inode *inode,
244 const char *fs_type)
245{
246 if (!inode || !fs_type)
247 return false;
248
249 if (!inode->i_sb)
250 return false;
251
252 if (!inode->i_sb->s_type)
253 return false;
254
255 return (strcmp(inode->i_sb->s_type->name, fs_type) == 0);
256}
257
258/**
259 * pfk_get_key_for_bio() - get the encryption key to be used for a bio
260 *
261 * @bio: pointer to the BIO
262 * @key_info: pointer to the key information which will be filled in
263 * @algo_mode: optional pointer to the algorithm identifier which will be set
264 * @is_pfe: will be set to false if the BIO should be left unencrypted
265 *
266 * Return: 0 if a key is being used, otherwise a -errno value
267 */
268static int pfk_get_key_for_bio(const struct bio *bio,
269 struct pfk_key_info *key_info,
270 enum ice_cryto_algo_mode *algo_mode,
271 bool *is_pfe, unsigned int *data_unit)
272{
273 const struct inode *inode;
274 enum pfe_type which_pfe;
275 const struct blk_encryption_key *key;
276 char *s_type = NULL;
277
278 inode = pfk_bio_get_inode(bio);
279 which_pfe = pfk_get_pfe_type(inode);
280 s_type = (char *)pfk_kc_get_storage_type();
281
282 /*
283 * Update dun based on storage type.
284 * 512 byte dun - For ext4 emmc
285 * 4K dun - For ext4 ufs, f2fs ufs and f2fs emmc
286 */
287
288 if (data_unit) {
289 if (!bio_dun(bio) && !memcmp(s_type, "sdcc", strlen("sdcc")))
290 *data_unit = 1 << ICE_CRYPTO_DATA_UNIT_512_B;
291 else
292 *data_unit = 1 << ICE_CRYPTO_DATA_UNIT_4_KB;
293 }
294
295 if (which_pfe != INVALID_PFE) {
296 /* Encrypted file; override ->bi_crypt_key */
297 pr_debug("parsing inode %lu with PFE type %d\n",
298 inode->i_ino, which_pfe);
299 return (*(pfk_parse_inode_ftable[which_pfe]))
300 (bio, inode, key_info, algo_mode, is_pfe);
301 }
302
303 /*
304 * bio is not for an encrypted file. Use ->bi_crypt_key if it was set.
305 * Otherwise, don't encrypt/decrypt the bio.
306 */
307 key = bio->bi_crypt_key;
308 if (!key) {
309 *is_pfe = false;
310 return -EINVAL;
311 }
312
313 /* Note: the "salt" is really just the second half of the XTS key. */
314 BUILD_BUG_ON(sizeof(key->raw) !=
315 PFK_SUPPORTED_KEY_SIZE + PFK_SUPPORTED_SALT_SIZE);
316 key_info->key = &key->raw[0];
317 key_info->key_size = PFK_SUPPORTED_KEY_SIZE;
318 key_info->salt = &key->raw[PFK_SUPPORTED_KEY_SIZE];
319 key_info->salt_size = PFK_SUPPORTED_SALT_SIZE;
320 if (algo_mode)
321 *algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
322 return 0;
323}
324
325/**
326 * pfk_load_key_start() - loads PFE encryption key to the ICE
327 * Can also be invoked from non
328 * PFE context, in this case it
329 * is not relevant and is_pfe
330 * flag is set to false
331 *
332 * @bio: Pointer to the BIO structure
333 * @ice_setting: Pointer to ice setting structure that will be filled with
334 * ice configuration values, including the index to which the key was loaded
335 * @is_pfe: will be false if inode is not relevant to PFE, in such a case
336 * it should be treated as non PFE by the block layer
337 *
338 * Returns the index where the key is stored in encryption hw and additional
339 * information that will be used later for configuration of the encryption hw.
340 *
341 * Must be followed by pfk_load_key_end when key is no longer used by ice
342 *
343 */
344int pfk_load_key_start(const struct bio *bio,
345 struct ice_crypto_setting *ice_setting, bool *is_pfe,
346 bool async)
347{
348 int ret = 0;
349 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
350 enum ice_cryto_algo_mode algo_mode = ICE_CRYPTO_ALGO_MODE_AES_XTS;
351 enum ice_crpto_key_size key_size_type = 0;
352 unsigned int data_unit = 1 << ICE_CRYPTO_DATA_UNIT_512_B;
353 u32 key_index = 0;
354
355 if (!is_pfe) {
356 pr_err("is_pfe is NULL\n");
357 return -EINVAL;
358 }
359
360 /*
361 * only a few errors below can indicate that
362 * this function was not invoked within PFE context,
363 * otherwise we will consider it PFE
364 */
365 *is_pfe = true;
366
367 if (!pfk_is_ready())
368 return -ENODEV;
369
370 if (!ice_setting) {
371 pr_err("ice setting is NULL\n");
372 return -EINVAL;
373 }
374
375 ret = pfk_get_key_for_bio(bio, &key_info, &algo_mode, is_pfe,
376 &data_unit);
377
378 if (ret != 0)
379 return ret;
380
381 ret = pfk_key_size_to_key_type(key_info.key_size, &key_size_type);
382 if (ret != 0)
383 return ret;
384
385 ret = pfk_kc_load_key_start(key_info.key, key_info.key_size,
386 key_info.salt, key_info.salt_size, &key_index, async,
387 data_unit);
388 if (ret) {
389 if (ret != -EBUSY && ret != -EAGAIN)
390 pr_err("start: could not load key into pfk key cache, error %d\n",
391 ret);
392
393 return ret;
394 }
395
396 ice_setting->key_size = key_size_type;
397 ice_setting->algo_mode = algo_mode;
398 /* hardcoded for now */
399 ice_setting->key_mode = ICE_CRYPTO_USE_LUT_SW_KEY;
400 ice_setting->key_index = key_index;
401
402 pr_debug("loaded key for file %s key_index %d\n",
403 inode_to_filename(pfk_bio_get_inode(bio)), key_index);
404
405 return 0;
406}
407
408/**
409 * pfk_load_key_end() - marks the PFE key as no longer used by ICE
410 * Can also be invoked from non
411 * PFE context, in this case it is not
412 * relevant and is_pfe flag is
413 * set to false
414 *
415 * @bio: Pointer to the BIO structure
416 * @is_pfe: Pointer to is_pfe flag, which will be true if function was invoked
417 * from PFE context
418 */
419int pfk_load_key_end(const struct bio *bio, bool *is_pfe)
420{
421 int ret = 0;
422 struct pfk_key_info key_info = {NULL, NULL, 0, 0};
423
424 if (!is_pfe) {
425 pr_err("is_pfe is NULL\n");
426 return -EINVAL;
427 }
428
429 /* only a few errors below can indicate that
430 * this function was not invoked within PFE context,
431 * otherwise we will consider it PFE
432 */
433 *is_pfe = true;
434
435 if (!pfk_is_ready())
436 return -ENODEV;
437
438 ret = pfk_get_key_for_bio(bio, &key_info, NULL, is_pfe, NULL);
439 if (ret != 0)
440 return ret;
441
442 pfk_kc_load_key_end(key_info.key, key_info.key_size,
443 key_info.salt, key_info.salt_size);
444
445 pr_debug("finished using key for file %s\n",
446 inode_to_filename(pfk_bio_get_inode(bio)));
447
448 return 0;
449}
450
451/**
452 * pfk_allow_merge_bio() - Check if 2 BIOs can be merged.
453 * @bio1: Pointer to first BIO structure.
454 * @bio2: Pointer to second BIO structure.
455 *
456 * Prevent merging of BIOs from encrypted and non-encrypted
457 * files, or files encrypted with different key.
458 * Also prevent non encrypted and encrypted data from the same file
459 * to be merged (ecryptfs header if stored inside file should be non
460 * encrypted)
461 * This API is called by the file system block layer.
462 *
463 * Return: true if the BIOs allowed to be merged, false
464 * otherwise.
465 */
466bool pfk_allow_merge_bio(const struct bio *bio1, const struct bio *bio2)
467{
468 const struct blk_encryption_key *key1;
469 const struct blk_encryption_key *key2;
470 const struct inode *inode1;
471 const struct inode *inode2;
472 enum pfe_type which_pfe1;
473 enum pfe_type which_pfe2;
474
475 if (!pfk_is_ready())
476 return false;
477
478 if (!bio1 || !bio2)
479 return false;
480
481 if (bio1 == bio2)
482 return true;
483
484 key1 = bio1->bi_crypt_key;
485 key2 = bio2->bi_crypt_key;
486
487 inode1 = pfk_bio_get_inode(bio1);
488 inode2 = pfk_bio_get_inode(bio2);
489
490 which_pfe1 = pfk_get_pfe_type(inode1);
491 which_pfe2 = pfk_get_pfe_type(inode2);
492
493 /*
494 * If one bio is for an encrypted file and the other is for a different
495 * type of encrypted file or for blocks that are not part of an
496 * encrypted file, do not merge.
497 */
498 if (which_pfe1 != which_pfe2)
499 return false;
500
501 if (which_pfe1 != INVALID_PFE) {
502 /* Both bios are for the same type of encrypted file. */
503 return (*(pfk_allow_merge_bio_ftable[which_pfe1]))(bio1, bio2,
504 inode1, inode2);
505 }
506
507 /*
508 * Neither bio is for an encrypted file. Merge only if the default keys
509 * are the same (or both are NULL).
510 */
511 return key1 == key2 ||
512 (key1 && key2 &&
513 !crypto_memneq(key1->raw, key2->raw, sizeof(key1->raw)));
514}
515
516int pfk_fbe_clear_key(const unsigned char *key, size_t key_size,
517 const unsigned char *salt, size_t salt_size)
518{
519 int ret = -EINVAL;
520
521 if (!key || !salt)
522 return ret;
523
524 ret = pfk_kc_remove_key_with_salt(key, key_size, salt, salt_size);
525 if (ret)
526 pr_err("Clear key error: ret value %d\n", ret);
527 return ret;
528}
529
530/**
531 * Flush key table on storage core reset. During core reset key configuration
532 * is lost in ICE. We need to flash the cache, so that the keys will be
533 * reconfigured again for every subsequent transaction
534 */
535void pfk_clear_on_reset(void)
536{
537 if (!pfk_is_ready())
538 return;
539
540 pfk_kc_clear_on_reset();
541}
542
543module_init(pfk_init);
544module_exit(pfk_exit);
545
546MODULE_LICENSE("GPL v2");
547MODULE_DESCRIPTION("Per-File-Key driver");