stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks
@ 2017-10-24 19:30 Eric Biggers
  2017-10-24 19:30 ` [PATCH 2/3] f2fs crypto: add missing locking for keyring_key access Eric Biggers
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Biggers @ 2017-10-24 19:30 UTC (permalink / raw)
  To: stable; +Cc: Theodore Ts'o, Jaegeuk Kim, David Howells, Eric Biggers

From: Jaegeuk Kim <jaegeuk@kernel.org>

commit 66aa3e1274fcf887e9d6501a68163270fc7718e7 upstream.  Please apply
to 4.4-stable.

This patch adopts:
	ext4 crypto: replace some BUG_ON()'s with error checks

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/f2fs/crypto.c       |  1 -
 fs/f2fs/crypto_fname.c |  2 --
 fs/f2fs/crypto_key.c   | 15 ++++++++++++---
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/fs/f2fs/crypto.c b/fs/f2fs/crypto.c
index 4a62ef14e932..d879c6c846b7 100644
--- a/fs/f2fs/crypto.c
+++ b/fs/f2fs/crypto.c
@@ -362,7 +362,6 @@ static int f2fs_page_crypto(struct f2fs_crypto_ctx *ctx,
 	else
 		res = crypto_ablkcipher_encrypt(req);
 	if (res == -EINPROGRESS || res == -EBUSY) {
-		BUG_ON(req->base.data != &ecr);
 		wait_for_completion(&ecr.completion);
 		res = ecr.res;
 	}
diff --git a/fs/f2fs/crypto_fname.c b/fs/f2fs/crypto_fname.c
index 38349ed5ea51..0fce444dd5ae 100644
--- a/fs/f2fs/crypto_fname.c
+++ b/fs/f2fs/crypto_fname.c
@@ -124,7 +124,6 @@ static int f2fs_fname_encrypt(struct inode *inode,
 	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
 	res = crypto_ablkcipher_encrypt(req);
 	if (res == -EINPROGRESS || res == -EBUSY) {
-		BUG_ON(req->base.data != &ecr);
 		wait_for_completion(&ecr.completion);
 		res = ecr.res;
 	}
@@ -180,7 +179,6 @@ static int f2fs_fname_decrypt(struct inode *inode,
 	ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
 	res = crypto_ablkcipher_decrypt(req);
 	if (res == -EINPROGRESS || res == -EBUSY) {
-		BUG_ON(req->base.data != &ecr);
 		wait_for_completion(&ecr.completion);
 		res = ecr.res;
 	}
diff --git a/fs/f2fs/crypto_key.c b/fs/f2fs/crypto_key.c
index 18595d7a0efc..81c87f7a3251 100644
--- a/fs/f2fs/crypto_key.c
+++ b/fs/f2fs/crypto_key.c
@@ -75,7 +75,6 @@ static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE],
 					F2FS_AES_256_XTS_KEY_SIZE, NULL);
 	res = crypto_ablkcipher_encrypt(req);
 	if (res == -EINPROGRESS || res == -EBUSY) {
-		BUG_ON(req->base.data != &ecr);
 		wait_for_completion(&ecr.completion);
 		res = ecr.res;
 	}
@@ -189,7 +188,11 @@ int f2fs_get_encryption_info(struct inode *inode)
 		keyring_key = NULL;
 		goto out;
 	}
-	BUG_ON(keyring_key->type != &key_type_logon);
+	if (keyring_key->type != &key_type_logon) {
+		printk_once(KERN_WARNING "f2fs: key type must be logon\n");
+		res = -ENOKEY;
+		goto out;
+	}
 	ukp = user_key_payload(keyring_key);
 	if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
 		res = -EINVAL;
@@ -198,7 +201,13 @@ int f2fs_get_encryption_info(struct inode *inode)
 	master_key = (struct f2fs_encryption_key *)ukp->data;
 	BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
 				F2FS_KEY_DERIVATION_NONCE_SIZE);
-	BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
+	if (master_key->size != F2FS_AES_256_XTS_KEY_SIZE) {
+		printk_once(KERN_WARNING
+				"f2fs: key size incorrect: %d\n",
+				master_key->size);
+		res = -ENOKEY;
+		goto out;
+	}
 	res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
 				  raw_key);
 	if (res)
-- 
2.15.0.rc0.271.g36b669edcc-goog

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

* [PATCH 2/3] f2fs crypto: add missing locking for keyring_key access
  2017-10-24 19:30 [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Eric Biggers
@ 2017-10-24 19:30 ` Eric Biggers
  2017-10-24 19:30 ` [PATCH 3/3] fscrypt: fix dereference of NULL user_key_payload Eric Biggers
  2017-10-25  9:39 ` [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2017-10-24 19:30 UTC (permalink / raw)
  To: stable; +Cc: Theodore Ts'o, Jaegeuk Kim, David Howells, Eric Biggers

From: Jaegeuk Kim <jaegeuk@kernel.org>

commit 745e8490b1e960ad79859dd8ba6a0b5a8d3d994e upstream.  Please apply
to 4.4-stable.

This patch adopts:
	ext4 crypto: add missing locking for keyring_key access

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/f2fs/crypto_key.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/fs/f2fs/crypto_key.c b/fs/f2fs/crypto_key.c
index 81c87f7a3251..ae49be377b60 100644
--- a/fs/f2fs/crypto_key.c
+++ b/fs/f2fs/crypto_key.c
@@ -193,9 +193,11 @@ int f2fs_get_encryption_info(struct inode *inode)
 		res = -ENOKEY;
 		goto out;
 	}
+	down_read(&keyring_key->sem);
 	ukp = user_key_payload(keyring_key);
 	if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
 		res = -EINVAL;
+		up_read(&keyring_key->sem);
 		goto out;
 	}
 	master_key = (struct f2fs_encryption_key *)ukp->data;
@@ -206,10 +208,12 @@ int f2fs_get_encryption_info(struct inode *inode)
 				"f2fs: key size incorrect: %d\n",
 				master_key->size);
 		res = -ENOKEY;
+		up_read(&keyring_key->sem);
 		goto out;
 	}
 	res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
 				  raw_key);
+	up_read(&keyring_key->sem);
 	if (res)
 		goto out;
 
-- 
2.15.0.rc0.271.g36b669edcc-goog

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

* [PATCH 3/3] fscrypt: fix dereference of NULL user_key_payload
  2017-10-24 19:30 [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Eric Biggers
  2017-10-24 19:30 ` [PATCH 2/3] f2fs crypto: add missing locking for keyring_key access Eric Biggers
@ 2017-10-24 19:30 ` Eric Biggers
  2017-10-25  9:39 ` [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2017-10-24 19:30 UTC (permalink / raw)
  To: stable; +Cc: Theodore Ts'o, Jaegeuk Kim, David Howells, Eric Biggers

From: Eric Biggers <ebiggers@google.com>

commit d60b5b7854c3d135b869f74fb93eaf63cbb1991a upstream.  Please apply
to 4.4-stable.

When an fscrypt-encrypted file is opened, we request the file's master
key from the keyrings service as a logon key, then access its payload.
However, a revoked key has a NULL payload, and we failed to check for
this.  request_key() *does* skip revoked keys, but there is still a
window where the key can be revoked before we acquire its semaphore.

Fix it by checking for a NULL payload, treating it like a key which was
already revoked at the time it was requested.

Fixes: 88bd6ccdcdd6 ("ext4 crypto: add encryption key management facilities")
Reviewed-by: James Morris <james.l.morris@oracle.com>
Cc: <stable@vger.kernel.org>    [v4.1+]
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
---
 fs/ext4/crypto_key.c | 6 ++++++
 fs/f2fs/crypto_key.c | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
index 505f8afde57c..9a1bc638abce 100644
--- a/fs/ext4/crypto_key.c
+++ b/fs/ext4/crypto_key.c
@@ -204,6 +204,12 @@ int ext4_get_encryption_info(struct inode *inode)
 	}
 	down_read(&keyring_key->sem);
 	ukp = user_key_payload(keyring_key);
+	if (!ukp) {
+		/* key was revoked before we acquired its semaphore */
+		res = -EKEYREVOKED;
+		up_read(&keyring_key->sem);
+		goto out;
+	}
 	if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
 		res = -EINVAL;
 		up_read(&keyring_key->sem);
diff --git a/fs/f2fs/crypto_key.c b/fs/f2fs/crypto_key.c
index ae49be377b60..7e62889a1d3d 100644
--- a/fs/f2fs/crypto_key.c
+++ b/fs/f2fs/crypto_key.c
@@ -195,6 +195,12 @@ int f2fs_get_encryption_info(struct inode *inode)
 	}
 	down_read(&keyring_key->sem);
 	ukp = user_key_payload(keyring_key);
+	if (!ukp) {
+		/* key was revoked before we acquired its semaphore */
+		res = -EKEYREVOKED;
+		up_read(&keyring_key->sem);
+		goto out;
+	}
 	if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
 		res = -EINVAL;
 		up_read(&keyring_key->sem);
-- 
2.15.0.rc0.271.g36b669edcc-goog

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

* Re: [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks
  2017-10-24 19:30 [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Eric Biggers
  2017-10-24 19:30 ` [PATCH 2/3] f2fs crypto: add missing locking for keyring_key access Eric Biggers
  2017-10-24 19:30 ` [PATCH 3/3] fscrypt: fix dereference of NULL user_key_payload Eric Biggers
@ 2017-10-25  9:39 ` Greg KH
  2 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2017-10-25  9:39 UTC (permalink / raw)
  To: Eric Biggers
  Cc: stable, Theodore Ts'o, Jaegeuk Kim, David Howells,
	Eric Biggers

On Tue, Oct 24, 2017 at 12:30:01PM -0700, Eric Biggers wrote:
> From: Jaegeuk Kim <jaegeuk@kernel.org>
> 
> commit 66aa3e1274fcf887e9d6501a68163270fc7718e7 upstream.  Please apply
> to 4.4-stable.

Thanks for all 3 of these patches, now queued up.

greg k-h

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

end of thread, other threads:[~2017-10-25  9:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-24 19:30 [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Eric Biggers
2017-10-24 19:30 ` [PATCH 2/3] f2fs crypto: add missing locking for keyring_key access Eric Biggers
2017-10-24 19:30 ` [PATCH 3/3] fscrypt: fix dereference of NULL user_key_payload Eric Biggers
2017-10-25  9:39 ` [PATCH 1/3] f2fs crypto: replace some BUG_ON()'s with error checks Greg KH

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).