All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction
@ 2020-08-24  6:17 ` Eric Biggers
  0 siblings, 0 replies; 46+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

This series fixes some deadlocks which are theoretically possible due to
fscrypt_get_encryption_info() being GFP_NOFS-unsafe, and thus not safe
to be called from within an ext4 transaction or under f2fs_lock_op().

The problem is solved by new helper functions which allow setting up the
key for new inodes earlier.  Patch 1 adds these helper functions.  Also
see that patch for a more detailed description of this problem.

Patches 2-6 then convert ext4, f2fs, and ubifs to use these new helpers.

Patch 7-8 then clean up a few things afterwards.

Coincidentally, this also solves some of the ordering problems that
ceph fscrypt support will have.  For more details about this, see the
discussion on Jeff Layton's RFC patchset for ceph fscrypt support
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
However, fscrypt_prepare_new_inode() still requires that the new
'struct inode' exist already, so it might not be enough for ceph yet.

This patchset applies to v5.9-rc2.

Eric Biggers (8):
  fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  ext4: factor out ext4_xattr_credits_for_new_inode()
  ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
  ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  fscrypt: remove fscrypt_inherit_context()
  fscrypt: stop pretending that key setup is nofs-safe

 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/hooks.c           |  10 +-
 fs/crypto/inline_crypt.c    |   7 +-
 fs/crypto/keysetup.c        | 190 ++++++++++++++++++++++++++++--------
 fs/crypto/keysetup_v1.c     |   8 +-
 fs/crypto/policy.c          |  64 +++++++-----
 fs/ext4/ialloc.c            | 118 +++++++++++-----------
 fs/f2fs/dir.c               |   2 +-
 fs/f2fs/f2fs.h              |  16 ---
 fs/f2fs/namei.c             |   7 +-
 fs/ubifs/dir.c              |  26 ++---
 include/linux/fscrypt.h     |  18 +++-
 12 files changed, 293 insertions(+), 176 deletions(-)

-- 
2.28.0


^ permalink raw reply	[flat|nested] 46+ messages in thread
* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 20:01 kernel test robot
  0 siblings, 0 replies; 46+ messages in thread
From: kernel test robot @ 2020-08-24 20:01 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 8242 bytes --]

CC: kbuild-all(a)lists.01.org
In-Reply-To: <20200824061712.195654-2-ebiggers@kernel.org>
References: <20200824061712.195654-2-ebiggers@kernel.org>
TO: Eric Biggers <ebiggers@kernel.org>

Hi Eric,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on f2fs/dev-test]
[also build test WARNING on linus/master v5.9-rc2 next-20200824]
[cannot apply to ext4/dev rw-ubifs/next rw-ubifs/fixes]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Eric-Biggers/fscrypt-avoid-GFP_NOFS-unsafe-key-setup-during-transaction/20200824-141936
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git dev-test
:::::: branch date: 14 hours ago
:::::: commit date: 14 hours ago
config: mips-randconfig-m031-20200824 (attached as .config)
compiler: mipsel-linux-gcc (GCC) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
fs/crypto/keysetup.c:490 fscrypt_setup_encryption_info() warn: passing a valid pointer to 'PTR_ERR'

# https://github.com/0day-ci/linux/commit/e83d6a405416ac7c2a5836a3b027837ceb4a3255
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Eric-Biggers/fscrypt-avoid-GFP_NOFS-unsafe-key-setup-during-transaction/20200824-141936
git checkout e83d6a405416ac7c2a5836a3b027837ceb4a3255
vim +/PTR_ERR +490 fs/crypto/keysetup.c

8094c3ceb21ad9 fs/crypto/keyinfo.c  Eric Biggers 2019-01-06  469  
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  470  static int
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  471  fscrypt_setup_encryption_info(struct inode *inode,
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  472  			      const union fscrypt_policy *policy,
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  473  			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
26bf3dc7e25b81 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-19  474  {
0b81d077907269 fs/crypto/keyinfo.c  Jaegeuk Kim  2015-05-15  475  	struct fscrypt_info *crypt_info;
e1cc40e5d42acb fs/crypto/keyinfo.c  Eric Biggers 2018-05-18  476  	struct fscrypt_mode *mode;
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  477  	struct key *master_key = NULL;
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  478  	int res;
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  479  
8094c3ceb21ad9 fs/crypto/keyinfo.c  Eric Biggers 2019-01-06  480  	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  481  	if (!crypt_info)
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  482  		return -ENOMEM;
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  483  
59dc6a8e1f534c fs/crypto/keyinfo.c  Eric Biggers 2019-08-04  484  	crypt_info->ci_inode = inode;
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  485  	crypt_info->ci_policy = *policy;
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  486  	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
640778fbc97b36 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-12  487  
5dae460c2292db fs/crypto/keysetup.c Eric Biggers 2019-08-04  488  	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
e1cc40e5d42acb fs/crypto/keyinfo.c  Eric Biggers 2018-05-18  489  	if (IS_ERR(mode)) {
e1cc40e5d42acb fs/crypto/keyinfo.c  Eric Biggers 2018-05-18 @490  		res = PTR_ERR(mode);
26bf3dc7e25b81 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-19  491  		goto out;
e1cc40e5d42acb fs/crypto/keyinfo.c  Eric Biggers 2018-05-18  492  	}
8094c3ceb21ad9 fs/crypto/keyinfo.c  Eric Biggers 2019-01-06  493  	WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
8094c3ceb21ad9 fs/crypto/keyinfo.c  Eric Biggers 2019-01-06  494  	crypt_info->ci_mode = mode;
8f39850dffa9cb fs/crypto/keyinfo.c  Eric Biggers 2016-09-15  495  
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  496  	res = setup_file_encryption_key(crypt_info, &master_key);
26bf3dc7e25b81 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-19  497  	if (res)
26bf3dc7e25b81 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-19  498  		goto out;
26bf3dc7e25b81 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-05-19  499  
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  500  	/*
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  501  	 * Multiple tasks may race to set ->i_crypt_info, so use
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  502  	 * cmpxchg_release().  This pairs with the smp_load_acquire() in
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  503  	 * fscrypt_get_info().  I.e., here we publish ->i_crypt_info with a
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  504  	 * RELEASE barrier so that other tasks can ACQUIRE it.
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  505  	 */
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  506  	if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  507  		/*
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  508  		 * We won the race and set ->i_crypt_info to our crypt_info.
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  509  		 * Now link it into the master key's inode list.
ab673b987488c4 fs/crypto/keysetup.c Eric Biggers 2020-07-21  510  		 */
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  511  		if (master_key) {
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  512  			struct fscrypt_master_key *mk =
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  513  				master_key->payload.data[0];
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  514  
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  515  			refcount_inc(&mk->mk_refcount);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  516  			crypt_info->ci_master_key = key_get(master_key);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  517  			spin_lock(&mk->mk_decrypted_inodes_lock);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  518  			list_add(&crypt_info->ci_master_key_link,
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  519  				 &mk->mk_decrypted_inodes);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  520  			spin_unlock(&mk->mk_decrypted_inodes_lock);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  521  		}
1b53cf9815bb47 fs/crypto/keyinfo.c  Eric Biggers 2017-02-21  522  		crypt_info = NULL;
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  523  	}
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  524  	res = 0;
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  525  out:
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  526  	if (master_key) {
23c688b54016ee fs/crypto/keysetup.c Eric Biggers 2019-08-04  527  		struct fscrypt_master_key *mk = master_key->payload.data[0];
23c688b54016ee fs/crypto/keysetup.c Eric Biggers 2019-08-04  528  
23c688b54016ee fs/crypto/keysetup.c Eric Biggers 2019-08-04  529  		up_read(&mk->mk_secret_sem);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  530  		key_put(master_key);
b1c0ec3599f42a fs/crypto/keysetup.c Eric Biggers 2019-08-04  531  	}
0b81d077907269 fs/crypto/keyinfo.c  Jaegeuk Kim  2015-05-15  532  	if (res == -ENOKEY)
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  533  		res = 0;
0b81d077907269 fs/crypto/keyinfo.c  Jaegeuk Kim  2015-05-15  534  	put_crypt_info(crypt_info);
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  535  	return res;
0adda907f23df2 fs/f2fs/crypto_key.c Jaegeuk Kim  2015-04-21  536  }
e83d6a405416ac fs/crypto/keysetup.c Eric Biggers 2020-08-23  537  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 28439 bytes --]

^ permalink raw reply	[flat|nested] 46+ messages in thread

end of thread, other threads:[~2020-08-24 20:49 UTC | newest]

Thread overview: 46+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-24  6:17 [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction Eric Biggers
2020-08-24  6:17 ` Eric Biggers
2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24 16:48   ` Jeff Layton
2020-08-24 16:48     ` Jeff Layton
2020-08-24 16:48     ` [f2fs-dev] " Jeff Layton
2020-08-24 18:21     ` Eric Biggers
2020-08-24 18:21       ` Eric Biggers
2020-08-24 18:21       ` [f2fs-dev] " Eric Biggers
2020-08-24 18:47       ` Jeff Layton
2020-08-24 18:47         ` Jeff Layton
2020-08-24 18:47         ` [f2fs-dev] " Jeff Layton
2020-08-24 19:02         ` Eric Biggers
2020-08-24 19:02           ` Eric Biggers
2020-08-24 19:02           ` [f2fs-dev] " Eric Biggers
2020-08-24 19:42           ` Jeff Layton
2020-08-24 19:42             ` Jeff Layton
2020-08-24 19:42             ` [f2fs-dev] " Jeff Layton
2020-08-24 20:49             ` Eric Biggers
2020-08-24 20:49               ` Eric Biggers
2020-08-24 20:49               ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 2/8] ext4: factor out ext4_xattr_credits_for_new_inode() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 3/8] ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 4/8] ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 5/8] f2fs: " Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 6/8] ubifs: " Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 7/8] fscrypt: remove fscrypt_inherit_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 8/8] fscrypt: stop pretending that key setup is nofs-safe Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24 20:01 [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context() kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.