aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-04-14 21:22:42 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-04-14 21:22:42 -0400
commitdfe70581c1b7a7625baa6ba26f8876d337845a23 (patch)
tree67b1c5a5a37e4bbd44a77d3a1c3aa28d5e028dfc
parent16382ed978cb40713684cfa0f25dc255a58d0c59 (diff)
parent03a8bb0e53d9562276045bdfcf2b5de2e4cff5a1 (diff)
Merge tag 'for-linus-4.6-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs
Pull f2fs/fscrypto fixes from Jaegeuk Kim: "In addition to f2fs/fscrypto fixes, I've added one patch which prevents RCU mode lookup in d_revalidate, as Al mentioned. These patches fix f2fs and fscrypto based on -rc3 bug fixes in ext4 crypto, which have not yet been fully propagated as follows. - use of dget_parent and file_dentry to avoid crashes - disallow RCU-mode lookup in d_invalidate - disallow -ENOMEM in the core data encryption path" * tag 'for-linus-4.6-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs: ext4/fscrypto: avoid RCU lookup in d_revalidate fscrypto: don't let data integrity writebacks fail with ENOMEM f2fs: use dget_parent and file_dentry in f2fs_file_open fscrypto: use dget_parent() in fscrypt_d_revalidate()
-rw-r--r--fs/crypto/crypto.c51
-rw-r--r--fs/ext4/crypto.c4
-rw-r--r--fs/f2fs/data.c16
-rw-r--r--fs/f2fs/file.c10
-rw-r--r--include/linux/fscrypto.h9
5 files changed, 61 insertions, 29 deletions
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c
index 7f5804537d30..2fc8c43ce531 100644
--- a/fs/crypto/crypto.c
+++ b/fs/crypto/crypto.c
@@ -26,6 +26,7 @@
26#include <linux/ratelimit.h> 26#include <linux/ratelimit.h>
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/fscrypto.h> 30#include <linux/fscrypto.h>
30#include <linux/ecryptfs.h> 31#include <linux/ecryptfs.h>
31 32
@@ -81,13 +82,14 @@ EXPORT_SYMBOL(fscrypt_release_ctx);
81/** 82/**
82 * fscrypt_get_ctx() - Gets an encryption context 83 * fscrypt_get_ctx() - Gets an encryption context
83 * @inode: The inode for which we are doing the crypto 84 * @inode: The inode for which we are doing the crypto
85 * @gfp_flags: The gfp flag for memory allocation
84 * 86 *
85 * Allocates and initializes an encryption context. 87 * Allocates and initializes an encryption context.
86 * 88 *
87 * Return: An allocated and initialized encryption context on success; error 89 * Return: An allocated and initialized encryption context on success; error
88 * value or NULL otherwise. 90 * value or NULL otherwise.
89 */ 91 */
90struct fscrypt_ctx *fscrypt_get_ctx(struct inode *inode) 92struct fscrypt_ctx *fscrypt_get_ctx(struct inode *inode, gfp_t gfp_flags)
91{ 93{
92 struct fscrypt_ctx *ctx = NULL; 94 struct fscrypt_ctx *ctx = NULL;
93 struct fscrypt_info *ci = inode->i_crypt_info; 95 struct fscrypt_info *ci = inode->i_crypt_info;
@@ -113,7 +115,7 @@ struct fscrypt_ctx *fscrypt_get_ctx(struct inode *inode)
113 list_del(&ctx->free_list); 115 list_del(&ctx->free_list);
114 spin_unlock_irqrestore(&fscrypt_ctx_lock, flags); 116 spin_unlock_irqrestore(&fscrypt_ctx_lock, flags);
115 if (!ctx) { 117 if (!ctx) {
116 ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, GFP_NOFS); 118 ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, gfp_flags);
117 if (!ctx) 119 if (!ctx)
118 return ERR_PTR(-ENOMEM); 120 return ERR_PTR(-ENOMEM);
119 ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL; 121 ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
@@ -147,7 +149,8 @@ typedef enum {
147 149
148static int do_page_crypto(struct inode *inode, 150static int do_page_crypto(struct inode *inode,
149 fscrypt_direction_t rw, pgoff_t index, 151 fscrypt_direction_t rw, pgoff_t index,
150 struct page *src_page, struct page *dest_page) 152 struct page *src_page, struct page *dest_page,
153 gfp_t gfp_flags)
151{ 154{
152 u8 xts_tweak[FS_XTS_TWEAK_SIZE]; 155 u8 xts_tweak[FS_XTS_TWEAK_SIZE];
153 struct skcipher_request *req = NULL; 156 struct skcipher_request *req = NULL;
@@ -157,7 +160,7 @@ static int do_page_crypto(struct inode *inode,
157 struct crypto_skcipher *tfm = ci->ci_ctfm; 160 struct crypto_skcipher *tfm = ci->ci_ctfm;
158 int res = 0; 161 int res = 0;
159 162
160 req = skcipher_request_alloc(tfm, GFP_NOFS); 163 req = skcipher_request_alloc(tfm, gfp_flags);
161 if (!req) { 164 if (!req) {
162 printk_ratelimited(KERN_ERR 165 printk_ratelimited(KERN_ERR
163 "%s: crypto_request_alloc() failed\n", 166 "%s: crypto_request_alloc() failed\n",
@@ -199,10 +202,9 @@ static int do_page_crypto(struct inode *inode,
199 return 0; 202 return 0;
200} 203}
201 204
202static struct page *alloc_bounce_page(struct fscrypt_ctx *ctx) 205static struct page *alloc_bounce_page(struct fscrypt_ctx *ctx, gfp_t gfp_flags)
203{ 206{
204 ctx->w.bounce_page = mempool_alloc(fscrypt_bounce_page_pool, 207 ctx->w.bounce_page = mempool_alloc(fscrypt_bounce_page_pool, gfp_flags);
205 GFP_NOWAIT);
206 if (ctx->w.bounce_page == NULL) 208 if (ctx->w.bounce_page == NULL)
207 return ERR_PTR(-ENOMEM); 209 return ERR_PTR(-ENOMEM);
208 ctx->flags |= FS_WRITE_PATH_FL; 210 ctx->flags |= FS_WRITE_PATH_FL;
@@ -213,6 +215,7 @@ static struct page *alloc_bounce_page(struct fscrypt_ctx *ctx)
213 * fscypt_encrypt_page() - Encrypts a page 215 * fscypt_encrypt_page() - Encrypts a page
214 * @inode: The inode for which the encryption should take place 216 * @inode: The inode for which the encryption should take place
215 * @plaintext_page: The page to encrypt. Must be locked. 217 * @plaintext_page: The page to encrypt. Must be locked.
218 * @gfp_flags: The gfp flag for memory allocation
216 * 219 *
217 * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx 220 * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
218 * encryption context. 221 * encryption context.
@@ -225,7 +228,7 @@ static struct page *alloc_bounce_page(struct fscrypt_ctx *ctx)
225 * error value or NULL. 228 * error value or NULL.
226 */ 229 */
227struct page *fscrypt_encrypt_page(struct inode *inode, 230struct page *fscrypt_encrypt_page(struct inode *inode,
228 struct page *plaintext_page) 231 struct page *plaintext_page, gfp_t gfp_flags)
229{ 232{
230 struct fscrypt_ctx *ctx; 233 struct fscrypt_ctx *ctx;
231 struct page *ciphertext_page = NULL; 234 struct page *ciphertext_page = NULL;
@@ -233,18 +236,19 @@ struct page *fscrypt_encrypt_page(struct inode *inode,
233 236
234 BUG_ON(!PageLocked(plaintext_page)); 237 BUG_ON(!PageLocked(plaintext_page));
235 238
236 ctx = fscrypt_get_ctx(inode); 239 ctx = fscrypt_get_ctx(inode, gfp_flags);
237 if (IS_ERR(ctx)) 240 if (IS_ERR(ctx))
238 return (struct page *)ctx; 241 return (struct page *)ctx;
239 242
240 /* The encryption operation will require a bounce page. */ 243 /* The encryption operation will require a bounce page. */
241 ciphertext_page = alloc_bounce_page(ctx); 244 ciphertext_page = alloc_bounce_page(ctx, gfp_flags);
242 if (IS_ERR(ciphertext_page)) 245 if (IS_ERR(ciphertext_page))
243 goto errout; 246 goto errout;
244 247
245 ctx->w.control_page = plaintext_page; 248 ctx->w.control_page = plaintext_page;
246 err = do_page_crypto(inode, FS_ENCRYPT, plaintext_page->index, 249 err = do_page_crypto(inode, FS_ENCRYPT, plaintext_page->index,
247 plaintext_page, ciphertext_page); 250 plaintext_page, ciphertext_page,
251 gfp_flags);
248 if (err) { 252 if (err) {
249 ciphertext_page = ERR_PTR(err); 253 ciphertext_page = ERR_PTR(err);
250 goto errout; 254 goto errout;
@@ -275,7 +279,7 @@ int fscrypt_decrypt_page(struct page *page)
275 BUG_ON(!PageLocked(page)); 279 BUG_ON(!PageLocked(page));
276 280
277 return do_page_crypto(page->mapping->host, 281 return do_page_crypto(page->mapping->host,
278 FS_DECRYPT, page->index, page, page); 282 FS_DECRYPT, page->index, page, page, GFP_NOFS);
279} 283}
280EXPORT_SYMBOL(fscrypt_decrypt_page); 284EXPORT_SYMBOL(fscrypt_decrypt_page);
281 285
@@ -289,11 +293,11 @@ int fscrypt_zeroout_range(struct inode *inode, pgoff_t lblk,
289 293
290 BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE); 294 BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
291 295
292 ctx = fscrypt_get_ctx(inode); 296 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
293 if (IS_ERR(ctx)) 297 if (IS_ERR(ctx))
294 return PTR_ERR(ctx); 298 return PTR_ERR(ctx);
295 299
296 ciphertext_page = alloc_bounce_page(ctx); 300 ciphertext_page = alloc_bounce_page(ctx, GFP_NOWAIT);
297 if (IS_ERR(ciphertext_page)) { 301 if (IS_ERR(ciphertext_page)) {
298 err = PTR_ERR(ciphertext_page); 302 err = PTR_ERR(ciphertext_page);
299 goto errout; 303 goto errout;
@@ -301,11 +305,12 @@ int fscrypt_zeroout_range(struct inode *inode, pgoff_t lblk,
301 305
302 while (len--) { 306 while (len--) {
303 err = do_page_crypto(inode, FS_ENCRYPT, lblk, 307 err = do_page_crypto(inode, FS_ENCRYPT, lblk,
304 ZERO_PAGE(0), ciphertext_page); 308 ZERO_PAGE(0), ciphertext_page,
309 GFP_NOFS);
305 if (err) 310 if (err)
306 goto errout; 311 goto errout;
307 312
308 bio = bio_alloc(GFP_KERNEL, 1); 313 bio = bio_alloc(GFP_NOWAIT, 1);
309 if (!bio) { 314 if (!bio) {
310 err = -ENOMEM; 315 err = -ENOMEM;
311 goto errout; 316 goto errout;
@@ -345,13 +350,20 @@ EXPORT_SYMBOL(fscrypt_zeroout_range);
345 */ 350 */
346static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) 351static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
347{ 352{
348 struct inode *dir = d_inode(dentry->d_parent); 353 struct dentry *dir;
349 struct fscrypt_info *ci = dir->i_crypt_info; 354 struct fscrypt_info *ci;
350 int dir_has_key, cached_with_key; 355 int dir_has_key, cached_with_key;
351 356
352 if (!dir->i_sb->s_cop->is_encrypted(dir)) 357 if (flags & LOOKUP_RCU)
358 return -ECHILD;
359
360 dir = dget_parent(dentry);
361 if (!d_inode(dir)->i_sb->s_cop->is_encrypted(d_inode(dir))) {
362 dput(dir);
353 return 0; 363 return 0;
364 }
354 365
366 ci = d_inode(dir)->i_crypt_info;
355 if (ci && ci->ci_keyring_key && 367 if (ci && ci->ci_keyring_key &&
356 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) | 368 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
357 (1 << KEY_FLAG_REVOKED) | 369 (1 << KEY_FLAG_REVOKED) |
@@ -363,6 +375,7 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
363 cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY; 375 cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY;
364 spin_unlock(&dentry->d_lock); 376 spin_unlock(&dentry->d_lock);
365 dir_has_key = (ci != NULL); 377 dir_has_key = (ci != NULL);
378 dput(dir);
366 379
367 /* 380 /*
368 * If the dentry was cached without the key, and it is a 381 * If the dentry was cached without the key, and it is a
diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
index db9ae6e18154..6a6c27373b54 100644
--- a/fs/ext4/crypto.c
+++ b/fs/ext4/crypto.c
@@ -32,6 +32,7 @@
32#include <linux/random.h> 32#include <linux/random.h>
33#include <linux/scatterlist.h> 33#include <linux/scatterlist.h>
34#include <linux/spinlock_types.h> 34#include <linux/spinlock_types.h>
35#include <linux/namei.h>
35 36
36#include "ext4_extents.h" 37#include "ext4_extents.h"
37#include "xattr.h" 38#include "xattr.h"
@@ -482,6 +483,9 @@ static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
482 struct ext4_crypt_info *ci; 483 struct ext4_crypt_info *ci;
483 int dir_has_key, cached_with_key; 484 int dir_has_key, cached_with_key;
484 485
486 if (flags & LOOKUP_RCU)
487 return -ECHILD;
488
485 dir = dget_parent(dentry); 489 dir = dget_parent(dentry);
486 if (!ext4_encrypted_inode(d_inode(dir))) { 490 if (!ext4_encrypted_inode(d_inode(dir))) {
487 dput(dir); 491 dput(dir);
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 53fec0872e60..5dafb9cef12e 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -992,7 +992,7 @@ submit_and_realloc:
992 if (f2fs_encrypted_inode(inode) && 992 if (f2fs_encrypted_inode(inode) &&
993 S_ISREG(inode->i_mode)) { 993 S_ISREG(inode->i_mode)) {
994 994
995 ctx = fscrypt_get_ctx(inode); 995 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
996 if (IS_ERR(ctx)) 996 if (IS_ERR(ctx))
997 goto set_error_page; 997 goto set_error_page;
998 998
@@ -1092,14 +1092,24 @@ int do_write_data_page(struct f2fs_io_info *fio)
1092 } 1092 }
1093 1093
1094 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { 1094 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1095 gfp_t gfp_flags = GFP_NOFS;
1095 1096
1096 /* wait for GCed encrypted page writeback */ 1097 /* wait for GCed encrypted page writeback */
1097 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), 1098 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
1098 fio->old_blkaddr); 1099 fio->old_blkaddr);
1099 1100retry_encrypt:
1100 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page); 1101 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1102 gfp_flags);
1101 if (IS_ERR(fio->encrypted_page)) { 1103 if (IS_ERR(fio->encrypted_page)) {
1102 err = PTR_ERR(fio->encrypted_page); 1104 err = PTR_ERR(fio->encrypted_page);
1105 if (err == -ENOMEM) {
1106 /* flush pending ios and wait for a while */
1107 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1108 congestion_wait(BLK_RW_ASYNC, HZ/50);
1109 gfp_flags |= __GFP_NOFAIL;
1110 err = 0;
1111 goto retry_encrypt;
1112 }
1103 goto out_writepage; 1113 goto out_writepage;
1104 } 1114 }
1105 } 1115 }
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 443e07705c2a..90d1157a09f9 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -441,7 +441,7 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
441static int f2fs_file_open(struct inode *inode, struct file *filp) 441static int f2fs_file_open(struct inode *inode, struct file *filp)
442{ 442{
443 int ret = generic_file_open(inode, filp); 443 int ret = generic_file_open(inode, filp);
444 struct inode *dir = filp->f_path.dentry->d_parent->d_inode; 444 struct dentry *dir;
445 445
446 if (!ret && f2fs_encrypted_inode(inode)) { 446 if (!ret && f2fs_encrypted_inode(inode)) {
447 ret = fscrypt_get_encryption_info(inode); 447 ret = fscrypt_get_encryption_info(inode);
@@ -450,9 +450,13 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)
450 if (!fscrypt_has_encryption_key(inode)) 450 if (!fscrypt_has_encryption_key(inode))
451 return -ENOKEY; 451 return -ENOKEY;
452 } 452 }
453 if (f2fs_encrypted_inode(dir) && 453 dir = dget_parent(file_dentry(filp));
454 !fscrypt_has_permitted_context(dir, inode)) 454 if (f2fs_encrypted_inode(d_inode(dir)) &&
455 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
456 dput(dir);
455 return -EPERM; 457 return -EPERM;
458 }
459 dput(dir);
456 return ret; 460 return ret;
457} 461}
458 462
diff --git a/include/linux/fscrypto.h b/include/linux/fscrypto.h
index cd91f75de49b..6027f6bbb061 100644
--- a/include/linux/fscrypto.h
+++ b/include/linux/fscrypto.h
@@ -263,9 +263,9 @@ static inline void fscrypt_set_d_op(struct dentry *dentry)
263extern struct kmem_cache *fscrypt_info_cachep; 263extern struct kmem_cache *fscrypt_info_cachep;
264int fscrypt_initialize(void); 264int fscrypt_initialize(void);
265 265
266extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *); 266extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *, gfp_t);
267extern void fscrypt_release_ctx(struct fscrypt_ctx *); 267extern void fscrypt_release_ctx(struct fscrypt_ctx *);
268extern struct page *fscrypt_encrypt_page(struct inode *, struct page *); 268extern struct page *fscrypt_encrypt_page(struct inode *, struct page *, gfp_t);
269extern int fscrypt_decrypt_page(struct page *); 269extern int fscrypt_decrypt_page(struct page *);
270extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *); 270extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
271extern void fscrypt_pullback_bio_page(struct page **, bool); 271extern void fscrypt_pullback_bio_page(struct page **, bool);
@@ -299,7 +299,8 @@ extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
299#endif 299#endif
300 300
301/* crypto.c */ 301/* crypto.c */
302static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i) 302static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i,
303 gfp_t f)
303{ 304{
304 return ERR_PTR(-EOPNOTSUPP); 305 return ERR_PTR(-EOPNOTSUPP);
305} 306}
@@ -310,7 +311,7 @@ static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
310} 311}
311 312
312static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i, 313static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
313 struct page *p) 314 struct page *p, gfp_t f)
314{ 315{
315 return ERR_PTR(-EOPNOTSUPP); 316 return ERR_PTR(-EOPNOTSUPP);
316} 317}