diff options
author | Theodore Ts'o <tytso@mit.edu> | 2016-12-12 21:50:28 -0500 |
---|---|---|
committer | Theodore Ts'o <tytso@mit.edu> | 2016-12-12 21:50:28 -0500 |
commit | a551d7c8deefb6d9fb45a1de03a617dd064e0419 (patch) | |
tree | c58f83ef3edd83d55f8f574cfe7ba023e029090b | |
parent | 0cb80b4847553582830a59da2c022c37a1f4a119 (diff) | |
parent | 6a34e4d2be07255cc59e6ccaf820669cfd7f815c (diff) |
Merge branch 'fscrypt' into dev
-rw-r--r-- | MAINTAINERS | 1 | ||||
-rw-r--r-- | fs/crypto/Kconfig | 2 | ||||
-rw-r--r-- | fs/crypto/crypto.c | 138 | ||||
-rw-r--r-- | fs/crypto/fname.c | 4 | ||||
-rw-r--r-- | fs/crypto/fscrypt_private.h | 93 | ||||
-rw-r--r-- | fs/crypto/keyinfo.c | 8 | ||||
-rw-r--r-- | fs/crypto/policy.c | 36 | ||||
-rw-r--r-- | fs/ext4/ext4.h | 4 | ||||
-rw-r--r-- | fs/ext4/inode.c | 1 | ||||
-rw-r--r-- | fs/ext4/ioctl.c | 32 | ||||
-rw-r--r-- | fs/f2fs/data.c | 1 | ||||
-rw-r--r-- | fs/f2fs/f2fs.h | 4 | ||||
-rw-r--r-- | fs/f2fs/file.c | 19 | ||||
-rw-r--r-- | include/linux/fscrypto.h | 106 | ||||
-rw-r--r-- | include/uapi/linux/fs.h | 14 |
15 files changed, 242 insertions, 221 deletions
diff --git a/MAINTAINERS b/MAINTAINERS index 411e3b87b8c2..0774714c4e67 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
@@ -5138,6 +5138,7 @@ F: include/linux/fscache*.h | |||
5138 | FS-CRYPTO: FILE SYSTEM LEVEL ENCRYPTION SUPPORT | 5138 | FS-CRYPTO: FILE SYSTEM LEVEL ENCRYPTION SUPPORT |
5139 | M: Theodore Y. Ts'o <tytso@mit.edu> | 5139 | M: Theodore Y. Ts'o <tytso@mit.edu> |
5140 | M: Jaegeuk Kim <jaegeuk@kernel.org> | 5140 | M: Jaegeuk Kim <jaegeuk@kernel.org> |
5141 | L: linux-fsdevel@vger.kernel.org | ||
5141 | S: Supported | 5142 | S: Supported |
5142 | F: fs/crypto/ | 5143 | F: fs/crypto/ |
5143 | F: include/linux/fscrypto.h | 5144 | F: include/linux/fscrypto.h |
diff --git a/fs/crypto/Kconfig b/fs/crypto/Kconfig index 92348faf9865..f514978f6688 100644 --- a/fs/crypto/Kconfig +++ b/fs/crypto/Kconfig | |||
@@ -8,9 +8,7 @@ config FS_ENCRYPTION | |||
8 | select CRYPTO_XTS | 8 | select CRYPTO_XTS |
9 | select CRYPTO_CTS | 9 | select CRYPTO_CTS |
10 | select CRYPTO_CTR | 10 | select CRYPTO_CTR |
11 | select CRYPTO_SHA256 | ||
12 | select KEYS | 11 | select KEYS |
13 | select ENCRYPTED_KEYS | ||
14 | help | 12 | help |
15 | Enable encryption of files and directories. This | 13 | Enable encryption of files and directories. This |
16 | feature is similar to ecryptfs, but it is more memory | 14 | feature is similar to ecryptfs, but it is more memory |
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index b6029785714c..ac8e4f6a3773 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c | |||
@@ -27,7 +27,7 @@ | |||
27 | #include <linux/bio.h> | 27 | #include <linux/bio.h> |
28 | #include <linux/dcache.h> | 28 | #include <linux/dcache.h> |
29 | #include <linux/namei.h> | 29 | #include <linux/namei.h> |
30 | #include <linux/fscrypto.h> | 30 | #include "fscrypt_private.h" |
31 | 31 | ||
32 | static unsigned int num_prealloc_crypto_pages = 32; | 32 | static unsigned int num_prealloc_crypto_pages = 32; |
33 | static unsigned int num_prealloc_crypto_ctxs = 128; | 33 | static unsigned int num_prealloc_crypto_ctxs = 128; |
@@ -63,7 +63,7 @@ void fscrypt_release_ctx(struct fscrypt_ctx *ctx) | |||
63 | { | 63 | { |
64 | unsigned long flags; | 64 | unsigned long flags; |
65 | 65 | ||
66 | if (ctx->flags & FS_WRITE_PATH_FL && ctx->w.bounce_page) { | 66 | if (ctx->flags & FS_CTX_HAS_BOUNCE_BUFFER_FL && ctx->w.bounce_page) { |
67 | mempool_free(ctx->w.bounce_page, fscrypt_bounce_page_pool); | 67 | mempool_free(ctx->w.bounce_page, fscrypt_bounce_page_pool); |
68 | ctx->w.bounce_page = NULL; | 68 | ctx->w.bounce_page = NULL; |
69 | } | 69 | } |
@@ -121,7 +121,7 @@ struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode, gfp_t gfp_flags) | |||
121 | } else { | 121 | } else { |
122 | ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL; | 122 | ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
123 | } | 123 | } |
124 | ctx->flags &= ~FS_WRITE_PATH_FL; | 124 | ctx->flags &= ~FS_CTX_HAS_BOUNCE_BUFFER_FL; |
125 | return ctx; | 125 | return ctx; |
126 | } | 126 | } |
127 | EXPORT_SYMBOL(fscrypt_get_ctx); | 127 | EXPORT_SYMBOL(fscrypt_get_ctx); |
@@ -147,9 +147,9 @@ typedef enum { | |||
147 | } fscrypt_direction_t; | 147 | } fscrypt_direction_t; |
148 | 148 | ||
149 | static int do_page_crypto(const struct inode *inode, | 149 | static int do_page_crypto(const struct inode *inode, |
150 | fscrypt_direction_t rw, pgoff_t index, | 150 | fscrypt_direction_t rw, u64 lblk_num, |
151 | struct page *src_page, struct page *dest_page, | 151 | struct page *src_page, struct page *dest_page, |
152 | unsigned int src_len, unsigned int src_offset, | 152 | unsigned int len, unsigned int offs, |
153 | gfp_t gfp_flags) | 153 | gfp_t gfp_flags) |
154 | { | 154 | { |
155 | struct { | 155 | struct { |
@@ -163,6 +163,8 @@ static int do_page_crypto(const struct inode *inode, | |||
163 | struct crypto_skcipher *tfm = ci->ci_ctfm; | 163 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
164 | int res = 0; | 164 | int res = 0; |
165 | 165 | ||
166 | BUG_ON(len == 0); | ||
167 | |||
166 | req = skcipher_request_alloc(tfm, gfp_flags); | 168 | req = skcipher_request_alloc(tfm, gfp_flags); |
167 | if (!req) { | 169 | if (!req) { |
168 | printk_ratelimited(KERN_ERR | 170 | printk_ratelimited(KERN_ERR |
@@ -176,14 +178,14 @@ static int do_page_crypto(const struct inode *inode, | |||
176 | page_crypt_complete, &ecr); | 178 | page_crypt_complete, &ecr); |
177 | 179 | ||
178 | BUILD_BUG_ON(sizeof(xts_tweak) != FS_XTS_TWEAK_SIZE); | 180 | BUILD_BUG_ON(sizeof(xts_tweak) != FS_XTS_TWEAK_SIZE); |
179 | xts_tweak.index = cpu_to_le64(index); | 181 | xts_tweak.index = cpu_to_le64(lblk_num); |
180 | memset(xts_tweak.padding, 0, sizeof(xts_tweak.padding)); | 182 | memset(xts_tweak.padding, 0, sizeof(xts_tweak.padding)); |
181 | 183 | ||
182 | sg_init_table(&dst, 1); | 184 | sg_init_table(&dst, 1); |
183 | sg_set_page(&dst, dest_page, src_len, src_offset); | 185 | sg_set_page(&dst, dest_page, len, offs); |
184 | sg_init_table(&src, 1); | 186 | sg_init_table(&src, 1); |
185 | sg_set_page(&src, src_page, src_len, src_offset); | 187 | sg_set_page(&src, src_page, len, offs); |
186 | skcipher_request_set_crypt(req, &src, &dst, src_len, &xts_tweak); | 188 | skcipher_request_set_crypt(req, &src, &dst, len, &xts_tweak); |
187 | if (rw == FS_DECRYPT) | 189 | if (rw == FS_DECRYPT) |
188 | res = crypto_skcipher_decrypt(req); | 190 | res = crypto_skcipher_decrypt(req); |
189 | else | 191 | else |
@@ -208,69 +210,87 @@ static struct page *alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags) | |||
208 | ctx->w.bounce_page = mempool_alloc(fscrypt_bounce_page_pool, gfp_flags); | 210 | ctx->w.bounce_page = mempool_alloc(fscrypt_bounce_page_pool, gfp_flags); |
209 | if (ctx->w.bounce_page == NULL) | 211 | if (ctx->w.bounce_page == NULL) |
210 | return ERR_PTR(-ENOMEM); | 212 | return ERR_PTR(-ENOMEM); |
211 | ctx->flags |= FS_WRITE_PATH_FL; | 213 | ctx->flags |= FS_CTX_HAS_BOUNCE_BUFFER_FL; |
212 | return ctx->w.bounce_page; | 214 | return ctx->w.bounce_page; |
213 | } | 215 | } |
214 | 216 | ||
215 | /** | 217 | /** |
216 | * fscypt_encrypt_page() - Encrypts a page | 218 | * fscypt_encrypt_page() - Encrypts a page |
217 | * @inode: The inode for which the encryption should take place | 219 | * @inode: The inode for which the encryption should take place |
218 | * @plaintext_page: The page to encrypt. Must be locked. | 220 | * @page: The page to encrypt. Must be locked for bounce-page |
219 | * @plaintext_len: Length of plaintext within page | 221 | * encryption. |
220 | * @plaintext_offset: Offset of plaintext within page | 222 | * @len: Length of data to encrypt in @page and encrypted |
221 | * @index: Index for encryption. This is mainly the page index, but | 223 | * data in returned page. |
222 | * but might be different for multiple calls on same page. | 224 | * @offs: Offset of data within @page and returned |
223 | * @gfp_flags: The gfp flag for memory allocation | 225 | * page holding encrypted data. |
226 | * @lblk_num: Logical block number. This must be unique for multiple | ||
227 | * calls with same inode, except when overwriting | ||
228 | * previously written data. | ||
229 | * @gfp_flags: The gfp flag for memory allocation | ||
224 | * | 230 | * |
225 | * Encrypts plaintext_page using the ctx encryption context. If | 231 | * Encrypts @page using the ctx encryption context. Performs encryption |
226 | * the filesystem supports it, encryption is performed in-place, otherwise a | 232 | * either in-place or into a newly allocated bounce page. |
227 | * new ciphertext_page is allocated and returned. | 233 | * Called on the page write path. |
228 | * | 234 | * |
229 | * Called on the page write path. The caller must call | 235 | * Bounce page allocation is the default. |
236 | * In this case, the contents of @page are encrypted and stored in an | ||
237 | * allocated bounce page. @page has to be locked and the caller must call | ||
230 | * fscrypt_restore_control_page() on the returned ciphertext page to | 238 | * fscrypt_restore_control_page() on the returned ciphertext page to |
231 | * release the bounce buffer and the encryption context. | 239 | * release the bounce buffer and the encryption context. |
232 | * | 240 | * |
233 | * Return: An allocated page with the encrypted content on success. Else, an | 241 | * In-place encryption is used by setting the FS_CFLG_OWN_PAGES flag in |
242 | * fscrypt_operations. Here, the input-page is returned with its content | ||
243 | * encrypted. | ||
244 | * | ||
245 | * Return: A page with the encrypted content on success. Else, an | ||
234 | * error value or NULL. | 246 | * error value or NULL. |
235 | */ | 247 | */ |
236 | struct page *fscrypt_encrypt_page(const struct inode *inode, | 248 | struct page *fscrypt_encrypt_page(const struct inode *inode, |
237 | struct page *plaintext_page, | 249 | struct page *page, |
238 | unsigned int plaintext_len, | 250 | unsigned int len, |
239 | unsigned int plaintext_offset, | 251 | unsigned int offs, |
240 | pgoff_t index, gfp_t gfp_flags) | 252 | u64 lblk_num, gfp_t gfp_flags) |
241 | 253 | ||
242 | { | 254 | { |
243 | struct fscrypt_ctx *ctx; | 255 | struct fscrypt_ctx *ctx; |
244 | struct page *ciphertext_page = plaintext_page; | 256 | struct page *ciphertext_page = page; |
245 | int err; | 257 | int err; |
246 | 258 | ||
247 | BUG_ON(plaintext_len % FS_CRYPTO_BLOCK_SIZE != 0); | 259 | BUG_ON(len % FS_CRYPTO_BLOCK_SIZE != 0); |
260 | |||
261 | if (inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES) { | ||
262 | /* with inplace-encryption we just encrypt the page */ | ||
263 | err = do_page_crypto(inode, FS_ENCRYPT, lblk_num, | ||
264 | page, ciphertext_page, | ||
265 | len, offs, gfp_flags); | ||
266 | if (err) | ||
267 | return ERR_PTR(err); | ||
268 | |||
269 | return ciphertext_page; | ||
270 | } | ||
271 | |||
272 | BUG_ON(!PageLocked(page)); | ||
248 | 273 | ||
249 | ctx = fscrypt_get_ctx(inode, gfp_flags); | 274 | ctx = fscrypt_get_ctx(inode, gfp_flags); |
250 | if (IS_ERR(ctx)) | 275 | if (IS_ERR(ctx)) |
251 | return (struct page *)ctx; | 276 | return (struct page *)ctx; |
252 | 277 | ||
253 | if (!(inode->i_sb->s_cop->flags & FS_CFLG_INPLACE_ENCRYPTION)) { | 278 | /* The encryption operation will require a bounce page. */ |
254 | /* The encryption operation will require a bounce page. */ | 279 | ciphertext_page = alloc_bounce_page(ctx, gfp_flags); |
255 | ciphertext_page = alloc_bounce_page(ctx, gfp_flags); | 280 | if (IS_ERR(ciphertext_page)) |
256 | if (IS_ERR(ciphertext_page)) | 281 | goto errout; |
257 | goto errout; | ||
258 | } | ||
259 | 282 | ||
260 | ctx->w.control_page = plaintext_page; | 283 | ctx->w.control_page = page; |
261 | err = do_page_crypto(inode, FS_ENCRYPT, index, | 284 | err = do_page_crypto(inode, FS_ENCRYPT, lblk_num, |
262 | plaintext_page, ciphertext_page, | 285 | page, ciphertext_page, |
263 | plaintext_len, plaintext_offset, | 286 | len, offs, gfp_flags); |
264 | gfp_flags); | ||
265 | if (err) { | 287 | if (err) { |
266 | ciphertext_page = ERR_PTR(err); | 288 | ciphertext_page = ERR_PTR(err); |
267 | goto errout; | 289 | goto errout; |
268 | } | 290 | } |
269 | if (!(inode->i_sb->s_cop->flags & FS_CFLG_INPLACE_ENCRYPTION)) { | 291 | SetPagePrivate(ciphertext_page); |
270 | SetPagePrivate(ciphertext_page); | 292 | set_page_private(ciphertext_page, (unsigned long)ctx); |
271 | set_page_private(ciphertext_page, (unsigned long)ctx); | 293 | lock_page(ciphertext_page); |
272 | lock_page(ciphertext_page); | ||
273 | } | ||
274 | return ciphertext_page; | 294 | return ciphertext_page; |
275 | 295 | ||
276 | errout: | 296 | errout: |
@@ -281,11 +301,12 @@ EXPORT_SYMBOL(fscrypt_encrypt_page); | |||
281 | 301 | ||
282 | /** | 302 | /** |
283 | * fscrypt_decrypt_page() - Decrypts a page in-place | 303 | * fscrypt_decrypt_page() - Decrypts a page in-place |
284 | * @inode: Encrypted inode to decrypt. | 304 | * @inode: The corresponding inode for the page to decrypt. |
285 | * @page: The page to decrypt. Must be locked. | 305 | * @page: The page to decrypt. Must be locked in case |
286 | * @len: Number of bytes in @page to be decrypted. | 306 | * it is a writeback page (FS_CFLG_OWN_PAGES unset). |
287 | * @offs: Start of data in @page. | 307 | * @len: Number of bytes in @page to be decrypted. |
288 | * @index: Index for encryption. | 308 | * @offs: Start of data in @page. |
309 | * @lblk_num: Logical block number. | ||
289 | * | 310 | * |
290 | * Decrypts page in-place using the ctx encryption context. | 311 | * Decrypts page in-place using the ctx encryption context. |
291 | * | 312 | * |
@@ -294,10 +315,13 @@ EXPORT_SYMBOL(fscrypt_encrypt_page); | |||
294 | * Return: Zero on success, non-zero otherwise. | 315 | * Return: Zero on success, non-zero otherwise. |
295 | */ | 316 | */ |
296 | int fscrypt_decrypt_page(const struct inode *inode, struct page *page, | 317 | int fscrypt_decrypt_page(const struct inode *inode, struct page *page, |
297 | unsigned int len, unsigned int offs, pgoff_t index) | 318 | unsigned int len, unsigned int offs, u64 lblk_num) |
298 | { | 319 | { |
299 | return do_page_crypto(inode, FS_DECRYPT, page->index, page, page, len, offs, | 320 | if (!(inode->i_sb->s_cop->flags & FS_CFLG_OWN_PAGES)) |
300 | GFP_NOFS); | 321 | BUG_ON(!PageLocked(page)); |
322 | |||
323 | return do_page_crypto(inode, FS_DECRYPT, lblk_num, page, page, len, | ||
324 | offs, GFP_NOFS); | ||
301 | } | 325 | } |
302 | EXPORT_SYMBOL(fscrypt_decrypt_page); | 326 | EXPORT_SYMBOL(fscrypt_decrypt_page); |
303 | 327 | ||
@@ -501,17 +525,22 @@ static void fscrypt_destroy(void) | |||
501 | 525 | ||
502 | /** | 526 | /** |
503 | * fscrypt_initialize() - allocate major buffers for fs encryption. | 527 | * fscrypt_initialize() - allocate major buffers for fs encryption. |
528 | * @cop_flags: fscrypt operations flags | ||
504 | * | 529 | * |
505 | * We only call this when we start accessing encrypted files, since it | 530 | * We only call this when we start accessing encrypted files, since it |
506 | * results in memory getting allocated that wouldn't otherwise be used. | 531 | * results in memory getting allocated that wouldn't otherwise be used. |
507 | * | 532 | * |
508 | * Return: Zero on success, non-zero otherwise. | 533 | * Return: Zero on success, non-zero otherwise. |
509 | */ | 534 | */ |
510 | int fscrypt_initialize(void) | 535 | int fscrypt_initialize(unsigned int cop_flags) |
511 | { | 536 | { |
512 | int i, res = -ENOMEM; | 537 | int i, res = -ENOMEM; |
513 | 538 | ||
514 | if (fscrypt_bounce_page_pool) | 539 | /* |
540 | * No need to allocate a bounce page pool if there already is one or | ||
541 | * this FS won't use it. | ||
542 | */ | ||
543 | if (cop_flags & FS_CFLG_OWN_PAGES || fscrypt_bounce_page_pool) | ||
515 | return 0; | 544 | return 0; |
516 | 545 | ||
517 | mutex_lock(&fscrypt_init_mutex); | 546 | mutex_lock(&fscrypt_init_mutex); |
@@ -540,7 +569,6 @@ fail: | |||
540 | mutex_unlock(&fscrypt_init_mutex); | 569 | mutex_unlock(&fscrypt_init_mutex); |
541 | return res; | 570 | return res; |
542 | } | 571 | } |
543 | EXPORT_SYMBOL(fscrypt_initialize); | ||
544 | 572 | ||
545 | /** | 573 | /** |
546 | * fscrypt_init() - Set up for fs encryption. | 574 | * fscrypt_init() - Set up for fs encryption. |
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c index 6b45d9caeeb0..56ad9d195f18 100644 --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c | |||
@@ -12,7 +12,7 @@ | |||
12 | 12 | ||
13 | #include <linux/scatterlist.h> | 13 | #include <linux/scatterlist.h> |
14 | #include <linux/ratelimit.h> | 14 | #include <linux/ratelimit.h> |
15 | #include <linux/fscrypto.h> | 15 | #include "fscrypt_private.h" |
16 | 16 | ||
17 | /** | 17 | /** |
18 | * fname_crypt_complete() - completion callback for filename crypto | 18 | * fname_crypt_complete() - completion callback for filename crypto |
@@ -350,7 +350,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, | |||
350 | fname->disk_name.len = iname->len; | 350 | fname->disk_name.len = iname->len; |
351 | return 0; | 351 | return 0; |
352 | } | 352 | } |
353 | ret = get_crypt_info(dir); | 353 | ret = fscrypt_get_crypt_info(dir); |
354 | if (ret && ret != -EOPNOTSUPP) | 354 | if (ret && ret != -EOPNOTSUPP) |
355 | return ret; | 355 | return ret; |
356 | 356 | ||
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h new file mode 100644 index 000000000000..aeab032d7d35 --- /dev/null +++ b/fs/crypto/fscrypt_private.h | |||
@@ -0,0 +1,93 @@ | |||
1 | /* | ||
2 | * fscrypt_private.h | ||
3 | * | ||
4 | * Copyright (C) 2015, Google, Inc. | ||
5 | * | ||
6 | * This contains encryption key functions. | ||
7 | * | ||
8 | * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015. | ||
9 | */ | ||
10 | |||
11 | #ifndef _FSCRYPT_PRIVATE_H | ||
12 | #define _FSCRYPT_PRIVATE_H | ||
13 | |||
14 | #include <linux/fscrypto.h> | ||
15 | |||
16 | #define FS_FNAME_CRYPTO_DIGEST_SIZE 32 | ||
17 | |||
18 | /* Encryption parameters */ | ||
19 | #define FS_XTS_TWEAK_SIZE 16 | ||
20 | #define FS_AES_128_ECB_KEY_SIZE 16 | ||
21 | #define FS_AES_256_GCM_KEY_SIZE 32 | ||
22 | #define FS_AES_256_CBC_KEY_SIZE 32 | ||
23 | #define FS_AES_256_CTS_KEY_SIZE 32 | ||
24 | #define FS_AES_256_XTS_KEY_SIZE 64 | ||
25 | #define FS_MAX_KEY_SIZE 64 | ||
26 | |||
27 | #define FS_KEY_DESC_PREFIX "fscrypt:" | ||
28 | #define FS_KEY_DESC_PREFIX_SIZE 8 | ||
29 | |||
30 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | ||
31 | |||
32 | /** | ||
33 | * Encryption context for inode | ||
34 | * | ||
35 | * Protector format: | ||
36 | * 1 byte: Protector format (1 = this version) | ||
37 | * 1 byte: File contents encryption mode | ||
38 | * 1 byte: File names encryption mode | ||
39 | * 1 byte: Flags | ||
40 | * 8 bytes: Master Key descriptor | ||
41 | * 16 bytes: Encryption Key derivation nonce | ||
42 | */ | ||
43 | struct fscrypt_context { | ||
44 | u8 format; | ||
45 | u8 contents_encryption_mode; | ||
46 | u8 filenames_encryption_mode; | ||
47 | u8 flags; | ||
48 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
49 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
50 | } __packed; | ||
51 | |||
52 | #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1 | ||
53 | |||
54 | /* This is passed in from userspace into the kernel keyring */ | ||
55 | struct fscrypt_key { | ||
56 | u32 mode; | ||
57 | u8 raw[FS_MAX_KEY_SIZE]; | ||
58 | u32 size; | ||
59 | } __packed; | ||
60 | |||
61 | /* | ||
62 | * A pointer to this structure is stored in the file system's in-core | ||
63 | * representation of an inode. | ||
64 | */ | ||
65 | struct fscrypt_info { | ||
66 | u8 ci_data_mode; | ||
67 | u8 ci_filename_mode; | ||
68 | u8 ci_flags; | ||
69 | struct crypto_skcipher *ci_ctfm; | ||
70 | struct key *ci_keyring_key; | ||
71 | u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE]; | ||
72 | }; | ||
73 | |||
74 | #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 | ||
75 | #define FS_CTX_HAS_BOUNCE_BUFFER_FL 0x00000002 | ||
76 | |||
77 | struct fscrypt_completion_result { | ||
78 | struct completion completion; | ||
79 | int res; | ||
80 | }; | ||
81 | |||
82 | #define DECLARE_FS_COMPLETION_RESULT(ecr) \ | ||
83 | struct fscrypt_completion_result ecr = { \ | ||
84 | COMPLETION_INITIALIZER((ecr).completion), 0 } | ||
85 | |||
86 | |||
87 | /* crypto.c */ | ||
88 | int fscrypt_initialize(unsigned int cop_flags); | ||
89 | |||
90 | /* keyinfo.c */ | ||
91 | extern int fscrypt_get_crypt_info(struct inode *); | ||
92 | |||
93 | #endif /* _FSCRYPT_PRIVATE_H */ | ||
diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 67fb6d8876d0..6eeea1dcba41 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c | |||
@@ -10,7 +10,7 @@ | |||
10 | 10 | ||
11 | #include <keys/user-type.h> | 11 | #include <keys/user-type.h> |
12 | #include <linux/scatterlist.h> | 12 | #include <linux/scatterlist.h> |
13 | #include <linux/fscrypto.h> | 13 | #include "fscrypt_private.h" |
14 | 14 | ||
15 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) | 15 | static void derive_crypt_complete(struct crypto_async_request *req, int rc) |
16 | { | 16 | { |
@@ -178,7 +178,7 @@ static void put_crypt_info(struct fscrypt_info *ci) | |||
178 | kmem_cache_free(fscrypt_info_cachep, ci); | 178 | kmem_cache_free(fscrypt_info_cachep, ci); |
179 | } | 179 | } |
180 | 180 | ||
181 | int get_crypt_info(struct inode *inode) | 181 | int fscrypt_get_crypt_info(struct inode *inode) |
182 | { | 182 | { |
183 | struct fscrypt_info *crypt_info; | 183 | struct fscrypt_info *crypt_info; |
184 | struct fscrypt_context ctx; | 184 | struct fscrypt_context ctx; |
@@ -188,7 +188,7 @@ int get_crypt_info(struct inode *inode) | |||
188 | u8 *raw_key = NULL; | 188 | u8 *raw_key = NULL; |
189 | int res; | 189 | int res; |
190 | 190 | ||
191 | res = fscrypt_initialize(); | 191 | res = fscrypt_initialize(inode->i_sb->s_cop->flags); |
192 | if (res) | 192 | if (res) |
193 | return res; | 193 | return res; |
194 | 194 | ||
@@ -327,7 +327,7 @@ int fscrypt_get_encryption_info(struct inode *inode) | |||
327 | (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) | | 327 | (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) | |
328 | (1 << KEY_FLAG_REVOKED) | | 328 | (1 << KEY_FLAG_REVOKED) | |
329 | (1 << KEY_FLAG_DEAD))))) | 329 | (1 << KEY_FLAG_DEAD))))) |
330 | return get_crypt_info(inode); | 330 | return fscrypt_get_crypt_info(inode); |
331 | return 0; | 331 | return 0; |
332 | } | 332 | } |
333 | EXPORT_SYMBOL(fscrypt_get_encryption_info); | 333 | EXPORT_SYMBOL(fscrypt_get_encryption_info); |
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index 6865663aac69..6ed7c2eebeec 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c | |||
@@ -10,8 +10,8 @@ | |||
10 | 10 | ||
11 | #include <linux/random.h> | 11 | #include <linux/random.h> |
12 | #include <linux/string.h> | 12 | #include <linux/string.h> |
13 | #include <linux/fscrypto.h> | ||
14 | #include <linux/mount.h> | 13 | #include <linux/mount.h> |
14 | #include "fscrypt_private.h" | ||
15 | 15 | ||
16 | static int inode_has_encryption_context(struct inode *inode) | 16 | static int inode_has_encryption_context(struct inode *inode) |
17 | { | 17 | { |
@@ -93,16 +93,19 @@ static int create_encryption_context_from_policy(struct inode *inode, | |||
93 | return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL); | 93 | return inode->i_sb->s_cop->set_context(inode, &ctx, sizeof(ctx), NULL); |
94 | } | 94 | } |
95 | 95 | ||
96 | int fscrypt_process_policy(struct file *filp, | 96 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
97 | const struct fscrypt_policy *policy) | ||
98 | { | 97 | { |
98 | struct fscrypt_policy policy; | ||
99 | struct inode *inode = file_inode(filp); | 99 | struct inode *inode = file_inode(filp); |
100 | int ret; | 100 | int ret; |
101 | 101 | ||
102 | if (copy_from_user(&policy, arg, sizeof(policy))) | ||
103 | return -EFAULT; | ||
104 | |||
102 | if (!inode_owner_or_capable(inode)) | 105 | if (!inode_owner_or_capable(inode)) |
103 | return -EACCES; | 106 | return -EACCES; |
104 | 107 | ||
105 | if (policy->version != 0) | 108 | if (policy.version != 0) |
106 | return -EINVAL; | 109 | return -EINVAL; |
107 | 110 | ||
108 | ret = mnt_want_write_file(filp); | 111 | ret = mnt_want_write_file(filp); |
@@ -120,9 +123,9 @@ int fscrypt_process_policy(struct file *filp, | |||
120 | ret = -ENOTEMPTY; | 123 | ret = -ENOTEMPTY; |
121 | else | 124 | else |
122 | ret = create_encryption_context_from_policy(inode, | 125 | ret = create_encryption_context_from_policy(inode, |
123 | policy); | 126 | &policy); |
124 | } else if (!is_encryption_context_consistent_with_policy(inode, | 127 | } else if (!is_encryption_context_consistent_with_policy(inode, |
125 | policy)) { | 128 | &policy)) { |
126 | printk(KERN_WARNING | 129 | printk(KERN_WARNING |
127 | "%s: Policy inconsistent with encryption context\n", | 130 | "%s: Policy inconsistent with encryption context\n", |
128 | __func__); | 131 | __func__); |
@@ -134,11 +137,13 @@ int fscrypt_process_policy(struct file *filp, | |||
134 | mnt_drop_write_file(filp); | 137 | mnt_drop_write_file(filp); |
135 | return ret; | 138 | return ret; |
136 | } | 139 | } |
137 | EXPORT_SYMBOL(fscrypt_process_policy); | 140 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
138 | 141 | ||
139 | int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy) | 142 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
140 | { | 143 | { |
144 | struct inode *inode = file_inode(filp); | ||
141 | struct fscrypt_context ctx; | 145 | struct fscrypt_context ctx; |
146 | struct fscrypt_policy policy; | ||
142 | int res; | 147 | int res; |
143 | 148 | ||
144 | if (!inode->i_sb->s_cop->get_context || | 149 | if (!inode->i_sb->s_cop->get_context || |
@@ -151,15 +156,18 @@ int fscrypt_get_policy(struct inode *inode, struct fscrypt_policy *policy) | |||
151 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) | 156 | if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) |
152 | return -EINVAL; | 157 | return -EINVAL; |
153 | 158 | ||
154 | policy->version = 0; | 159 | policy.version = 0; |
155 | policy->contents_encryption_mode = ctx.contents_encryption_mode; | 160 | policy.contents_encryption_mode = ctx.contents_encryption_mode; |
156 | policy->filenames_encryption_mode = ctx.filenames_encryption_mode; | 161 | policy.filenames_encryption_mode = ctx.filenames_encryption_mode; |
157 | policy->flags = ctx.flags; | 162 | policy.flags = ctx.flags; |
158 | memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor, | 163 | memcpy(policy.master_key_descriptor, ctx.master_key_descriptor, |
159 | FS_KEY_DESCRIPTOR_SIZE); | 164 | FS_KEY_DESCRIPTOR_SIZE); |
165 | |||
166 | if (copy_to_user(arg, &policy, sizeof(policy))) | ||
167 | return -EFAULT; | ||
160 | return 0; | 168 | return 0; |
161 | } | 169 | } |
162 | EXPORT_SYMBOL(fscrypt_get_policy); | 170 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
163 | 171 | ||
164 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) | 172 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
165 | { | 173 | { |
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 7446d390d051..2163c1e69f2a 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h | |||
@@ -2329,8 +2329,8 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) { } | |||
2329 | #define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page | 2329 | #define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page |
2330 | #define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page | 2330 | #define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page |
2331 | #define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range | 2331 | #define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range |
2332 | #define fscrypt_process_policy fscrypt_notsupp_process_policy | 2332 | #define fscrypt_ioctl_set_policy fscrypt_notsupp_ioctl_set_policy |
2333 | #define fscrypt_get_policy fscrypt_notsupp_get_policy | 2333 | #define fscrypt_ioctl_get_policy fscrypt_notsupp_ioctl_get_policy |
2334 | #define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context | 2334 | #define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context |
2335 | #define fscrypt_inherit_context fscrypt_notsupp_inherit_context | 2335 | #define fscrypt_inherit_context fscrypt_notsupp_inherit_context |
2336 | #define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info | 2336 | #define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info |
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index f9f892212308..72d593fa690d 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c | |||
@@ -3851,7 +3851,6 @@ static int __ext4_block_zero_page_range(handle_t *handle, | |||
3851 | /* We expect the key to be set. */ | 3851 | /* We expect the key to be set. */ |
3852 | BUG_ON(!fscrypt_has_encryption_key(inode)); | 3852 | BUG_ON(!fscrypt_has_encryption_key(inode)); |
3853 | BUG_ON(blocksize != PAGE_SIZE); | 3853 | BUG_ON(blocksize != PAGE_SIZE); |
3854 | BUG_ON(!PageLocked(page)); | ||
3855 | WARN_ON_ONCE(fscrypt_decrypt_page(page->mapping->host, | 3854 | WARN_ON_ONCE(fscrypt_decrypt_page(page->mapping->host, |
3856 | page, PAGE_SIZE, 0, page->index)); | 3855 | page, PAGE_SIZE, 0, page->index)); |
3857 | } | 3856 | } |
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index fcc9510a819c..49fd1371bfa2 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c | |||
@@ -787,22 +787,12 @@ resizefs_out: | |||
787 | } | 787 | } |
788 | case EXT4_IOC_PRECACHE_EXTENTS: | 788 | case EXT4_IOC_PRECACHE_EXTENTS: |
789 | return ext4_ext_precache(inode); | 789 | return ext4_ext_precache(inode); |
790 | case EXT4_IOC_SET_ENCRYPTION_POLICY: { | ||
791 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | ||
792 | struct fscrypt_policy policy; | ||
793 | 790 | ||
791 | case EXT4_IOC_SET_ENCRYPTION_POLICY: | ||
794 | if (!ext4_has_feature_encrypt(sb)) | 792 | if (!ext4_has_feature_encrypt(sb)) |
795 | return -EOPNOTSUPP; | 793 | return -EOPNOTSUPP; |
794 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); | ||
796 | 795 | ||
797 | if (copy_from_user(&policy, | ||
798 | (struct fscrypt_policy __user *)arg, | ||
799 | sizeof(policy))) | ||
800 | return -EFAULT; | ||
801 | return fscrypt_process_policy(filp, &policy); | ||
802 | #else | ||
803 | return -EOPNOTSUPP; | ||
804 | #endif | ||
805 | } | ||
806 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { | 796 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { |
807 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | 797 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
808 | int err, err2; | 798 | int err, err2; |
@@ -843,23 +833,9 @@ resizefs_out: | |||
843 | return -EOPNOTSUPP; | 833 | return -EOPNOTSUPP; |
844 | #endif | 834 | #endif |
845 | } | 835 | } |
846 | case EXT4_IOC_GET_ENCRYPTION_POLICY: { | 836 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
847 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | 837 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
848 | struct fscrypt_policy policy; | ||
849 | int err = 0; | ||
850 | 838 | ||
851 | if (!ext4_encrypted_inode(inode)) | ||
852 | return -ENOENT; | ||
853 | err = fscrypt_get_policy(inode, &policy); | ||
854 | if (err) | ||
855 | return err; | ||
856 | if (copy_to_user((void __user *)arg, &policy, sizeof(policy))) | ||
857 | return -EFAULT; | ||
858 | return 0; | ||
859 | #else | ||
860 | return -EOPNOTSUPP; | ||
861 | #endif | ||
862 | } | ||
863 | case EXT4_IOC_FSGETXATTR: | 839 | case EXT4_IOC_FSGETXATTR: |
864 | { | 840 | { |
865 | struct fsxattr fa; | 841 | struct fsxattr fa; |
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 435590c4b341..9f0ba90b92e4 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c | |||
@@ -1194,7 +1194,6 @@ int do_write_data_page(struct f2fs_io_info *fio) | |||
1194 | f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), | 1194 | f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), |
1195 | fio->old_blkaddr); | 1195 | fio->old_blkaddr); |
1196 | retry_encrypt: | 1196 | retry_encrypt: |
1197 | BUG_ON(!PageLocked(fio->page)); | ||
1198 | fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, | 1197 | fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, |
1199 | PAGE_SIZE, 0, | 1198 | PAGE_SIZE, 0, |
1200 | fio->page->index, | 1199 | fio->page->index, |
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 9e8de18a168a..8e94b7bda42b 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h | |||
@@ -2453,8 +2453,8 @@ static inline bool f2fs_may_encrypt(struct inode *inode) | |||
2453 | #define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page | 2453 | #define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page |
2454 | #define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page | 2454 | #define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page |
2455 | #define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range | 2455 | #define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range |
2456 | #define fscrypt_process_policy fscrypt_notsupp_process_policy | 2456 | #define fscrypt_ioctl_set_policy fscrypt_notsupp_ioctl_set_policy |
2457 | #define fscrypt_get_policy fscrypt_notsupp_get_policy | 2457 | #define fscrypt_ioctl_get_policy fscrypt_notsupp_ioctl_get_policy |
2458 | #define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context | 2458 | #define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context |
2459 | #define fscrypt_inherit_context fscrypt_notsupp_inherit_context | 2459 | #define fscrypt_inherit_context fscrypt_notsupp_inherit_context |
2460 | #define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info | 2460 | #define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info |
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index c7865073cd26..f0c83f74557d 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c | |||
@@ -1752,31 +1752,16 @@ static bool uuid_is_nonzero(__u8 u[16]) | |||
1752 | 1752 | ||
1753 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) | 1753 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg) |
1754 | { | 1754 | { |
1755 | struct fscrypt_policy policy; | ||
1756 | struct inode *inode = file_inode(filp); | 1755 | struct inode *inode = file_inode(filp); |
1757 | 1756 | ||
1758 | if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg, | ||
1759 | sizeof(policy))) | ||
1760 | return -EFAULT; | ||
1761 | |||
1762 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | 1757 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
1763 | 1758 | ||
1764 | return fscrypt_process_policy(filp, &policy); | 1759 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
1765 | } | 1760 | } |
1766 | 1761 | ||
1767 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) | 1762 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg) |
1768 | { | 1763 | { |
1769 | struct fscrypt_policy policy; | 1764 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
1770 | struct inode *inode = file_inode(filp); | ||
1771 | int err; | ||
1772 | |||
1773 | err = fscrypt_get_policy(inode, &policy); | ||
1774 | if (err) | ||
1775 | return err; | ||
1776 | |||
1777 | if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy))) | ||
1778 | return -EFAULT; | ||
1779 | return 0; | ||
1780 | } | 1765 | } |
1781 | 1766 | ||
1782 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) | 1767 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg) |
diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h index 98c71e973a96..c074b670aa99 100644 --- a/include/linux/fscrypto.h +++ b/include/linux/fscrypto.h | |||
@@ -18,73 +18,9 @@ | |||
18 | #include <crypto/skcipher.h> | 18 | #include <crypto/skcipher.h> |
19 | #include <uapi/linux/fs.h> | 19 | #include <uapi/linux/fs.h> |
20 | 20 | ||
21 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | 21 | #define FS_CRYPTO_BLOCK_SIZE 16 |
22 | #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1 | ||
23 | |||
24 | #define FS_POLICY_FLAGS_PAD_4 0x00 | ||
25 | #define FS_POLICY_FLAGS_PAD_8 0x01 | ||
26 | #define FS_POLICY_FLAGS_PAD_16 0x02 | ||
27 | #define FS_POLICY_FLAGS_PAD_32 0x03 | ||
28 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 | ||
29 | #define FS_POLICY_FLAGS_VALID 0x03 | ||
30 | |||
31 | /* Encryption algorithms */ | ||
32 | #define FS_ENCRYPTION_MODE_INVALID 0 | ||
33 | #define FS_ENCRYPTION_MODE_AES_256_XTS 1 | ||
34 | #define FS_ENCRYPTION_MODE_AES_256_GCM 2 | ||
35 | #define FS_ENCRYPTION_MODE_AES_256_CBC 3 | ||
36 | #define FS_ENCRYPTION_MODE_AES_256_CTS 4 | ||
37 | |||
38 | /** | ||
39 | * Encryption context for inode | ||
40 | * | ||
41 | * Protector format: | ||
42 | * 1 byte: Protector format (1 = this version) | ||
43 | * 1 byte: File contents encryption mode | ||
44 | * 1 byte: File names encryption mode | ||
45 | * 1 byte: Flags | ||
46 | * 8 bytes: Master Key descriptor | ||
47 | * 16 bytes: Encryption Key derivation nonce | ||
48 | */ | ||
49 | struct fscrypt_context { | ||
50 | u8 format; | ||
51 | u8 contents_encryption_mode; | ||
52 | u8 filenames_encryption_mode; | ||
53 | u8 flags; | ||
54 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
55 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
56 | } __packed; | ||
57 | |||
58 | /* Encryption parameters */ | ||
59 | #define FS_XTS_TWEAK_SIZE 16 | ||
60 | #define FS_AES_128_ECB_KEY_SIZE 16 | ||
61 | #define FS_AES_256_GCM_KEY_SIZE 32 | ||
62 | #define FS_AES_256_CBC_KEY_SIZE 32 | ||
63 | #define FS_AES_256_CTS_KEY_SIZE 32 | ||
64 | #define FS_AES_256_XTS_KEY_SIZE 64 | ||
65 | #define FS_MAX_KEY_SIZE 64 | ||
66 | |||
67 | #define FS_KEY_DESC_PREFIX "fscrypt:" | ||
68 | #define FS_KEY_DESC_PREFIX_SIZE 8 | ||
69 | |||
70 | /* This is passed in from userspace into the kernel keyring */ | ||
71 | struct fscrypt_key { | ||
72 | u32 mode; | ||
73 | u8 raw[FS_MAX_KEY_SIZE]; | ||
74 | u32 size; | ||
75 | } __packed; | ||
76 | |||
77 | struct fscrypt_info { | ||
78 | u8 ci_data_mode; | ||
79 | u8 ci_filename_mode; | ||
80 | u8 ci_flags; | ||
81 | struct crypto_skcipher *ci_ctfm; | ||
82 | struct key *ci_keyring_key; | ||
83 | u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE]; | ||
84 | }; | ||
85 | 22 | ||
86 | #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 | 23 | struct fscrypt_info; |
87 | #define FS_WRITE_PATH_FL 0x00000002 | ||
88 | 24 | ||
89 | struct fscrypt_ctx { | 25 | struct fscrypt_ctx { |
90 | union { | 26 | union { |
@@ -102,19 +38,6 @@ struct fscrypt_ctx { | |||
102 | u8 mode; /* Encryption mode for tfm */ | 38 | u8 mode; /* Encryption mode for tfm */ |
103 | }; | 39 | }; |
104 | 40 | ||
105 | struct fscrypt_completion_result { | ||
106 | struct completion completion; | ||
107 | int res; | ||
108 | }; | ||
109 | |||
110 | #define DECLARE_FS_COMPLETION_RESULT(ecr) \ | ||
111 | struct fscrypt_completion_result ecr = { \ | ||
112 | COMPLETION_INITIALIZER((ecr).completion), 0 } | ||
113 | |||
114 | #define FS_FNAME_NUM_SCATTER_ENTRIES 4 | ||
115 | #define FS_CRYPTO_BLOCK_SIZE 16 | ||
116 | #define FS_FNAME_CRYPTO_DIGEST_SIZE 32 | ||
117 | |||
118 | /** | 41 | /** |
119 | * For encrypted symlinks, the ciphertext length is stored at the beginning | 42 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
120 | * of the string in little-endian format. | 43 | * of the string in little-endian format. |
@@ -156,7 +79,7 @@ struct fscrypt_name { | |||
156 | /* | 79 | /* |
157 | * fscrypt superblock flags | 80 | * fscrypt superblock flags |
158 | */ | 81 | */ |
159 | #define FS_CFLG_INPLACE_ENCRYPTION (1U << 1) | 82 | #define FS_CFLG_OWN_PAGES (1U << 1) |
160 | 83 | ||
161 | /* | 84 | /* |
162 | * crypto opertions for filesystems | 85 | * crypto opertions for filesystems |
@@ -244,28 +167,25 @@ static inline void fscrypt_set_d_op(struct dentry *dentry) | |||
244 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) | 167 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
245 | /* crypto.c */ | 168 | /* crypto.c */ |
246 | extern struct kmem_cache *fscrypt_info_cachep; | 169 | extern struct kmem_cache *fscrypt_info_cachep; |
247 | int fscrypt_initialize(void); | ||
248 | |||
249 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); | 170 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); |
250 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); | 171 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); |
251 | extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *, | 172 | extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *, |
252 | unsigned int, unsigned int, | 173 | unsigned int, unsigned int, |
253 | pgoff_t, gfp_t); | 174 | u64, gfp_t); |
254 | extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int, | 175 | extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int, |
255 | unsigned int, pgoff_t); | 176 | unsigned int, u64); |
256 | extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *); | 177 | extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *); |
257 | extern void fscrypt_pullback_bio_page(struct page **, bool); | 178 | extern void fscrypt_pullback_bio_page(struct page **, bool); |
258 | extern void fscrypt_restore_control_page(struct page *); | 179 | extern void fscrypt_restore_control_page(struct page *); |
259 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, | 180 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, |
260 | unsigned int); | 181 | unsigned int); |
261 | /* policy.c */ | 182 | /* policy.c */ |
262 | extern int fscrypt_process_policy(struct file *, const struct fscrypt_policy *); | 183 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); |
263 | extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *); | 184 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); |
264 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); | 185 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
265 | extern int fscrypt_inherit_context(struct inode *, struct inode *, | 186 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
266 | void *, bool); | 187 | void *, bool); |
267 | /* keyinfo.c */ | 188 | /* keyinfo.c */ |
268 | extern int get_crypt_info(struct inode *); | ||
269 | extern int fscrypt_get_encryption_info(struct inode *); | 189 | extern int fscrypt_get_encryption_info(struct inode *); |
270 | extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *); | 190 | extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *); |
271 | 191 | ||
@@ -299,14 +219,14 @@ static inline struct page *fscrypt_notsupp_encrypt_page(const struct inode *i, | |||
299 | struct page *p, | 219 | struct page *p, |
300 | unsigned int len, | 220 | unsigned int len, |
301 | unsigned int offs, | 221 | unsigned int offs, |
302 | pgoff_t index, gfp_t f) | 222 | u64 lblk_num, gfp_t f) |
303 | { | 223 | { |
304 | return ERR_PTR(-EOPNOTSUPP); | 224 | return ERR_PTR(-EOPNOTSUPP); |
305 | } | 225 | } |
306 | 226 | ||
307 | static inline int fscrypt_notsupp_decrypt_page(const struct inode *i, struct page *p, | 227 | static inline int fscrypt_notsupp_decrypt_page(const struct inode *i, struct page *p, |
308 | unsigned int len, unsigned int offs, | 228 | unsigned int len, unsigned int offs, |
309 | pgoff_t index) | 229 | u64 lblk_num) |
310 | { | 230 | { |
311 | return -EOPNOTSUPP; | 231 | return -EOPNOTSUPP; |
312 | } | 232 | } |
@@ -334,14 +254,14 @@ static inline int fscrypt_notsupp_zeroout_range(const struct inode *i, pgoff_t p | |||
334 | } | 254 | } |
335 | 255 | ||
336 | /* policy.c */ | 256 | /* policy.c */ |
337 | static inline int fscrypt_notsupp_process_policy(struct file *f, | 257 | static inline int fscrypt_notsupp_ioctl_set_policy(struct file *f, |
338 | const struct fscrypt_policy *p) | 258 | const void __user *arg) |
339 | { | 259 | { |
340 | return -EOPNOTSUPP; | 260 | return -EOPNOTSUPP; |
341 | } | 261 | } |
342 | 262 | ||
343 | static inline int fscrypt_notsupp_get_policy(struct inode *i, | 263 | static inline int fscrypt_notsupp_ioctl_get_policy(struct file *f, |
344 | struct fscrypt_policy *p) | 264 | void __user *arg) |
345 | { | 265 | { |
346 | return -EOPNOTSUPP; | 266 | return -EOPNOTSUPP; |
347 | } | 267 | } |
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index acb2b6152ba0..0496d37abe28 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h | |||
@@ -254,6 +254,20 @@ struct fsxattr { | |||
254 | /* Policy provided via an ioctl on the topmost directory */ | 254 | /* Policy provided via an ioctl on the topmost directory */ |
255 | #define FS_KEY_DESCRIPTOR_SIZE 8 | 255 | #define FS_KEY_DESCRIPTOR_SIZE 8 |
256 | 256 | ||
257 | #define FS_POLICY_FLAGS_PAD_4 0x00 | ||
258 | #define FS_POLICY_FLAGS_PAD_8 0x01 | ||
259 | #define FS_POLICY_FLAGS_PAD_16 0x02 | ||
260 | #define FS_POLICY_FLAGS_PAD_32 0x03 | ||
261 | #define FS_POLICY_FLAGS_PAD_MASK 0x03 | ||
262 | #define FS_POLICY_FLAGS_VALID 0x03 | ||
263 | |||
264 | /* Encryption algorithms */ | ||
265 | #define FS_ENCRYPTION_MODE_INVALID 0 | ||
266 | #define FS_ENCRYPTION_MODE_AES_256_XTS 1 | ||
267 | #define FS_ENCRYPTION_MODE_AES_256_GCM 2 | ||
268 | #define FS_ENCRYPTION_MODE_AES_256_CBC 3 | ||
269 | #define FS_ENCRYPTION_MODE_AES_256_CTS 4 | ||
270 | |||
257 | struct fscrypt_policy { | 271 | struct fscrypt_policy { |
258 | __u8 version; | 272 | __u8 version; |
259 | __u8 contents_encryption_mode; | 273 | __u8 contents_encryption_mode; |