linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: Satya Tangirala <satyat@google.com>
Cc: Christoph Hellwig <hch@infradead.org>,
	linux-block@vger.kernel.org, linux-scsi@vger.kernel.org,
	linux-fscrypt@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-ext4@vger.kernel.org,
	Barani Muthukumaran <bmuthuku@qti.qualcomm.com>,
	Kuohong Wang <kuohong.wang@mediatek.com>,
	Kim Boojin <boojin.kim@samsung.com>
Subject: Re: [PATCH v10 02/12] block: Keyslot Manager for Inline Encryption
Date: Mon, 27 Apr 2020 19:57:08 -0700	[thread overview]
Message-ID: <20200428025708.GB251491@gmail.com> (raw)
In-Reply-To: <20200428024614.GA251491@gmail.com>

On Mon, Apr 27, 2020 at 07:46:14PM -0700, Eric Biggers wrote:
> On Tue, Apr 28, 2020 at 02:14:41AM +0000, Satya Tangirala wrote:
> > > > +int blk_ksm_evict_key(struct blk_keyslot_manager *ksm,
> > > > +		      const struct blk_crypto_key *key)
> > > > +{
> > > > +	struct blk_ksm_keyslot *slot;
> > > > +	int err = 0;
> > > > +
> > > > +	blk_ksm_hw_enter(ksm);
> > > > +	slot = blk_ksm_find_keyslot(ksm, key);
> > > > +	if (!slot)
> > > > +		goto out_unlock;
> > > > +
> > > > +	if (atomic_read(&slot->slot_refs) != 0) {
> > > > +		err = -EBUSY;
> > > > +		goto out_unlock;
> > > > +	}
> > > 
> > > This check looks racy.
> > Yes, this could in theory race with blk_ksm_put_slot (e.g. if it's
> > called while there's still IO in flight/IO that just finished) - But
> > it's currently only called by fscrypt when a key is being destroyed,
> > which only happens after all the inodes using that key are evicted, and
> > no data is in flight, so when this function is called, slot->slot_refs
> > will be 0. In particular, this function should only be called when the
> > key isn't being used for IO anymore. I'll add a WARN_ON_ONCE and also
> > make the assumption clearer. We could also instead make this wait for
> > the slot_refs to become 0 and then evict the key instead of just
> > returning -EBUSY as it does now, but I'm not sure if it's really what
> > we want to do/worth doing right now...
> 
> Note that we're holding down_write(&ksm->lock) here, which synchronizes with
> someone getting the keyslot (in particular, incrementing its refcount from 0)
> because that uses down_read(&ksm->lock).
> 
> So I don't think there's a race.  The behavior is just that if someone tries to
> evict a key that's still in-use, then we'll correctly fail to evict the key.
> 
> "Evicting a key that's still in-use" isn't supposed to happen, so printing a
> warning is a good idea.  But I think it needs to be pr_warn_once(), not
> WARN_ON_ONCE(), because WARN_ON_ONCE() is for kernel bugs only, not userspace
> bugs.  It's theoretically possible for userspace to cause the same key to be
> used multiple times on the same disk but via different blk_crypto_key's.  The
> keyslot manager will put these in the same keyslot, but there will be a separate
> eviction attempt for each blk_crypto_key.
> 
> For example, with fscrypt with -o inlinecrypt and blk-crypto-fallback, userspace
> could create an encrypted file using FSCRYPT_MODE_ADIANTUM and flags == 0, then
> get its encryption nonce and derive the file's encryption key.  Then in another
> directory, they could set FSCRYPT_MODE_ADIANTUM and flags ==
> FSCRYPT_POLICY_FLAG_DIRECT_KEY, and use the other file's encryption key as the
> *master* key.
> 
> That would be totally insane for userspace to do.  But it's possible, so we
> can't use WARN_ON_ONCE().
> 

Or maybe 'struct blk_ksm_keyslot' should contain a pointer to the
'struct blk_crypto_key' rather than a copy of it?  If we did that, then:

- Each duplicate blk_crypto_key would use its own keyslot and not interfere with
  any others.

- blk_crypto_evict_key() would be *required* to be called.

- It would be a kernel bug if blk_crypto_evict_key() were called with any
  pending I/O, so WARN_ON_ONCE() would be the right thing to do.

- The hash function used to find a key's keyslot would be
  hash_ptr(blk_crypto_key, ksm->log_slot_hashtable_size) instead of
  SipHash(key=perboot_key, data=raw_key).
  
I might be forgetting something; was there a reason we didn't do that?
It wouldn't be as robust against users forgetting to call
blk_crypto_evict_key(), but that would be a bug anyway.

- Eric

  reply	other threads:[~2020-04-28  2:57 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-08  3:56 [PATCH v10 00/12] Inline Encryption Support Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 01/12] Documentation: Document the blk-crypto framework Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 02/12] block: Keyslot Manager for Inline Encryption Satya Tangirala
2020-04-22  9:22   ` Christoph Hellwig
2020-04-28  2:14     ` Satya Tangirala
2020-04-28  2:46       ` Eric Biggers
2020-04-28  2:57         ` Eric Biggers [this message]
2020-04-28  5:19           ` Christoph Hellwig
2020-04-08  3:56 ` [PATCH v10 03/12] block: Inline encryption support for blk-mq Satya Tangirala
2020-04-22  9:35   ` Christoph Hellwig
2020-04-28  2:54     ` Satya Tangirala
2020-04-28  5:21       ` Christoph Hellwig
2020-04-08  3:56 ` [PATCH v10 04/12] block: Make blk-integrity preclude hardware inline encryption Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 05/12] block: blk-crypto-fallback for Inline Encryption Satya Tangirala
2020-04-22  9:37   ` Christoph Hellwig
2020-04-08  3:56 ` [PATCH v10 06/12] scsi: ufs: UFS driver v2.1 spec crypto additions Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 07/12] scsi: ufs: UFS crypto API Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 08/12] scsi: ufs: Add inline encryption support to UFS Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 09/12] fs: introduce SB_INLINECRYPT Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 10/12] fscrypt: add inline encryption support Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 11/12] f2fs: " Satya Tangirala
2020-04-08  3:56 ` [PATCH v10 12/12] ext4: " Satya Tangirala
2020-04-08  4:18 ` [PATCH v10 00/12] Inline Encryption Support Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200428025708.GB251491@gmail.com \
    --to=ebiggers@kernel.org \
    --cc=bmuthuku@qti.qualcomm.com \
    --cc=boojin.kim@samsung.com \
    --cc=hch@infradead.org \
    --cc=kuohong.wang@mediatek.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-fscrypt@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=satyat@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).