blob: 6d3cd4c43fa0ce9cf246941056903c89fae0a38c [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) - EXT4
16 *
17 * This driver is used for working with EXT4 crypt extension
18 *
19 * The key information is stored in node by EXT4 when file is first opened
20 * and will be later accessed by Block Device Driver to actually load the key
21 * to encryption hw.
22 *
23 * PFK exposes API's for loading and removing keys from encryption hw
24 * and also API to determine whether 2 adjacent blocks can be agregated by
25 * Block Layer in one request to encryption hw.
26 *
27 */
28
29
30/* Uncomment the line below to enable debug messages */
31/* #define DEBUG 1 */
32#define pr_fmt(fmt) "pfk_ext4 [%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
Neeraj Soni36c65122018-04-18 21:04:46 +053039#include "fscrypt_ice.h"
Neeraj Sonic692cb92018-04-18 17:20:22 +053040#include "pfk_ext4.h"
Neeraj Soni36c65122018-04-18 21:04:46 +053041//#include "ext4_ice.h"
Neeraj Sonic692cb92018-04-18 17:20:22 +053042
43static bool pfk_ext4_ready;
44
45/*
46 * pfk_ext4_deinit() - Deinit function, should be invoked by upper PFK layer
47 */
48void pfk_ext4_deinit(void)
49{
50 pfk_ext4_ready = false;
51}
52
53/*
54 * pfk_ecryptfs_init() - Init function, should be invoked by upper PFK layer
55 */
56int __init pfk_ext4_init(void)
57{
58 pfk_ext4_ready = true;
59 pr_info("PFK EXT4 inited successfully\n");
60
61 return 0;
62}
63
64/**
65 * pfk_ecryptfs_is_ready() - driver is initialized and ready.
66 *
67 * Return: true if the driver is ready.
68 */
69static inline bool pfk_ext4_is_ready(void)
70{
71 return pfk_ext4_ready;
72}
73
74/**
75 * pfk_ext4_dump_inode() - dumps all interesting info about inode to the screen
76 *
77 *
78 */
79/*
80 * static void pfk_ext4_dump_inode(const struct inode* inode)
81 * {
82 * struct ext4_crypt_info *ci = ext4_encryption_info((struct inode*)inode);
83 *
84 * pr_debug("dumping inode with address 0x%p\n", inode);
85 * pr_debug("S_ISREG is %d\n", S_ISREG(inode->i_mode));
86 * pr_debug("EXT4_INODE_ENCRYPT flag is %d\n",
87 * ext4_test_inode_flag((struct inode*)inode, EXT4_INODE_ENCRYPT));
88 * if (ci) {
89 * pr_debug("crypt_info address 0x%p\n", ci);
90 * pr_debug("ci->ci_data_mode %d\n", ci->ci_data_mode);
91 * } else {
92 * pr_debug("crypt_info is NULL\n");
93 * }
94 * }
95 */
96
97/**
98 * pfk_is_ext4_type() - return true if inode belongs to ICE EXT4 PFE
99 * @inode: inode pointer
100 */
101bool pfk_is_ext4_type(const struct inode *inode)
102{
103 if (!pfe_is_inode_filesystem_type(inode, "ext4"))
104 return false;
105
Neeraj Soni36c65122018-04-18 21:04:46 +0530106 return fscrypt_should_be_processed_by_ice(inode);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530107}
108
109/**
110 * pfk_ext4_parse_cipher() - parse cipher from inode to enum
111 * @inode: inode
112 * @algo: pointer to store the output enum (can be null)
113 *
114 * return 0 in case of success, error otherwise (i.e not supported cipher)
115 */
116static int pfk_ext4_parse_cipher(const struct inode *inode,
117 enum ice_cryto_algo_mode *algo)
118{
119 /*
120 * currently only AES XTS algo is supported
121 * in the future, table with supported ciphers might
122 * be introduced
123 */
124
125 if (!inode)
126 return -EINVAL;
127
Neeraj Soni36c65122018-04-18 21:04:46 +0530128 if (!fscrypt_is_aes_xts_cipher(inode)) {
Neeraj Sonic692cb92018-04-18 17:20:22 +0530129 pr_err("ext4 alghoritm is not supported by pfk\n");
130 return -EINVAL;
131 }
132
133 if (algo)
134 *algo = ICE_CRYPTO_ALGO_MODE_AES_XTS;
135
136 return 0;
137}
138
139
140int pfk_ext4_parse_inode(const struct bio *bio,
141 const struct inode *inode,
142 struct pfk_key_info *key_info,
143 enum ice_cryto_algo_mode *algo,
Neeraj Soniefb33112018-08-17 20:39:35 +0530144 bool *is_pfe,
Neeraj Soniefb33112018-08-17 20:39:35 +0530145 const char *storage_type)
Neeraj Sonic692cb92018-04-18 17:20:22 +0530146{
147 int ret = 0;
148
149 if (!is_pfe)
150 return -EINVAL;
151
152 /*
153 * only a few errors below can indicate that
154 * this function was not invoked within PFE context,
155 * otherwise we will consider it PFE
156 */
157 *is_pfe = true;
158
159 if (!pfk_ext4_is_ready())
160 return -ENODEV;
161
162 if (!inode)
163 return -EINVAL;
164
165 if (!key_info)
166 return -EINVAL;
167
Neeraj Soni36c65122018-04-18 21:04:46 +0530168 key_info->key = fscrypt_get_ice_encryption_key(inode);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530169 if (!key_info->key) {
170 pr_err("could not parse key from ext4\n");
171 return -EINVAL;
172 }
173
Neeraj Soni36c65122018-04-18 21:04:46 +0530174 key_info->key_size = fscrypt_get_ice_encryption_key_size(inode);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530175 if (!key_info->key_size) {
176 pr_err("could not parse key size from ext4\n");
177 return -EINVAL;
178 }
179
Neeraj Soni36c65122018-04-18 21:04:46 +0530180 key_info->salt = fscrypt_get_ice_encryption_salt(inode);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530181 if (!key_info->salt) {
182 pr_err("could not parse salt from ext4\n");
183 return -EINVAL;
184 }
185
Neeraj Soni36c65122018-04-18 21:04:46 +0530186 key_info->salt_size = fscrypt_get_ice_encryption_salt_size(inode);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530187 if (!key_info->salt_size) {
188 pr_err("could not parse salt size from ext4\n");
189 return -EINVAL;
190 }
191
192 ret = pfk_ext4_parse_cipher(inode, algo);
193 if (ret != 0) {
194 pr_err("not supported cipher\n");
195 return ret;
196 }
197
198 return 0;
199}
200
201bool pfk_ext4_allow_merge_bio(const struct bio *bio1,
202 const struct bio *bio2, const struct inode *inode1,
203 const struct inode *inode2)
204{
205 /* if there is no ext4 pfk, don't disallow merging blocks */
206 if (!pfk_ext4_is_ready())
207 return true;
208
209 if (!inode1 || !inode2)
210 return false;
211
Neeraj Soni36c65122018-04-18 21:04:46 +0530212 return fscrypt_is_ice_encryption_info_equal(inode1, inode2);
Neeraj Sonic692cb92018-04-18 17:20:22 +0530213}