Skip to contentSkip to conversation

Fix dereferencing payload of revoked keys

PATCH Superseded Eric Biggers 2 participants 15 messages Sep 28, 2017 – Oct 3, 2017

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix dereferencing payload of revoked keys

From: Eric Biggers <[email protected]>

This series fixes the various users of the keyrings service that access a "user" or "logon" key's payload without first checking whether the payload pointer is NULL, or calling key_validate() while holding the key semaphore. Without one of these two checks, a NULL pointer dereference will occur if the key has been revoked concurrently. Usually this is pretty easy to reproduce (in most of the cases even as an unprivileged user), although it may be unlikely to happen by accident.

Patch 6 also fixes the lack of key length validation in ecryptfs.

These fixes probably will need to be split up between a few different maintainers, but initially I'm sending the full series so that people can see the full context of the fixes.

Eric Biggers (7):
KEYS: encrypted: fix dereference of NULL user_key_payload
FS-Cache: fix dereference of NULL user_key_payload lib/digsig: fix dereference of NULL user_key_payload
fscrypt: fix dereference of NULL user_key_payload
ecryptfs: fix dereference of NULL user_key_payload
ecryptfs: fix out-of-bounds read of key payload
ecryptfs: move key payload accessor functions into keystore.c

 fs/crypto/keyinfo.c                      |  5 +++
 fs/ecryptfs/ecryptfs_kernel.h            | 44 -------------------
 fs/ecryptfs/keystore.c                   | 73 +++++++++++++++++++++++++++++++-
 fs/fscache/object-list.c                 |  7 +++
 lib/digsig.c                             |  6 +++
 security/keys/encrypted-keys/encrypted.c |  7 +++
 6 files changed, 97 insertions(+), 45 deletions(-)
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
#1 lore raw

PATCH 1/7 KEYS: encrypted: fix dereference of NULL user_key_payload

From: Eric Biggers <[email protected]>

A key of type "encrypted" references a "master key" which is used to encrypt and decrypt the encrypted key's payload. However, when we accessed the master key's payload, we failed to handle the case where the master key has been revoked, which sets the payload pointer to NULL. Note that 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.

This was an issue for master keys of type "user" only. Master keys can also be of type "trusted", but those cannot be revoked.

Fixes 7e70cb497850 ("keys: add new key-type encrypted")
Cc <[email protected]> [v2.6.38+]
---
 security/keys/encrypted-keys/encrypted.c | 7 +++++++
 1 file changed, 7 insertions(+)
security/keys/encrypted-keys/encrypted.c +7 -0
index f54b92868bc3..d92cbf9687c3 100644
@@ -309,6 +309,13 @@ static struct key *request_user_key(const char *master_desc, const u8 **master_k
 
 	down_read(&ukey->sem);
 	upayload = user_key_payload_locked(ukey);
+	if (!upayload) {
+		/* key was revoked before we acquired its semaphore */
+		up_read(&ukey->sem);
+		key_put(ukey);
+		ukey = ERR_PTR(-EKEYREVOKED);
+		goto error;
+	}
 	*master_key = upayload->data;
 	*master_keylen = upayload->datalen;
 error:
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] KEYS: encrypted: fix dereference of NULL user_key_payload

Quoted 12 lines · Eric Biggers

From: Eric Biggers <[email protected]>

A key of type "encrypted" references a "master key" which is used to encrypt and decrypt the encrypted key's payload. However, when we accessed the master key's payload, we failed to handle the case where the master key has been revoked, which sets the payload pointer to NULL. Note that 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.

Quoted 6 lines

This was an issue for master keys of type "user" only. Master keys can also be of type "trusted", but those cannot be revoked.

Fixes 7e70cb497850 ("keys: add new key-type encrypted")
Cc <[email protected]> [v2.6.38+]
Signature · 2 lines
James Morris
<[email protected]>
#3 lore raw

PATCH 2/7 FS-Cache: fix dereference of NULL user_key_payload

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

When the file /proc/fs/fscache/objects (available with CONFIG_FSCACHE_OBJECT_LIST=y) is opened, we request a user key with description "fscache:objlist", 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 access its payload.

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

Fixes 4fbf4291aa15 ("FS-Cache: Allow the current state of all objects to be dumped")
Cc <[email protected]> [v2.6.32+]
---
 fs/fscache/object-list.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/fscache/object-list.c +7 -0
index b5ab06fabc60..0438d4cd91ef 100644
@@ -331,6 +331,13 @@ static void fscache_objlist_config(struct fscache_objlist_data *data)
 	rcu_read_lock();
 
 	confkey = user_key_payload_rcu(key);
+	if (!confkey) {
+		/* key was revoked */
+		rcu_read_unlock();
+		key_put(key);
+		goto no_config;
+	}
+
 	buf = confkey->data;
 
 	for (len = confkey->datalen - 1; len >= 0; len--) {
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] FS-Cache: fix dereference of NULL user_key_payload

Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

When the file /proc/fs/fscache/objects (available with CONFIG_FSCACHE_OBJECT_LIST=y) is opened, we request a user key with description "fscache:objlist", 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 access its payload.

Quoted 6 lines

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

Fixes 4fbf4291aa15 ("FS-Cache: Allow the current state of all objects to be dumped")
Cc <[email protected]> [v2.6.32+]
Signature · 2 lines
James Morris
<[email protected]>
#5 lore raw

PATCH 3/7 lib/digsig: fix dereference of NULL user_key_payload

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

digsig_verify() requests a user key, then accesses 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 051dbb918c7f ("crypto: digital signature verification support")
Cc <[email protected]> [v3.3+]
---
 lib/digsig.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/digsig.c +6 -0
index 03d7c63837ae..6ba6fcd92dd1 100644
@@ -87,6 +87,12 @@ static int digsig_verify_rsa(struct key *key,
 	down_read(&key->sem);
 	ukp = user_key_payload_locked(key);
 
+	if (!ukp) {
+		/* key was revoked before we acquired its semaphore */
+		err = -EKEYREVOKED;
+		goto err1;
+	}
+
 	if (ukp->datalen < sizeof(*pkh))
 		goto err1;
 
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 3/7
#6 lore raw

Re: [PATCH 3/7] lib/digsig: fix dereference of NULL user_key_payload

Quoted 7 lines · Eric Biggers

From: Eric Biggers <[email protected]>

digsig_verify() requests a user key, then accesses 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.

Quoted 6 lines

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

Fixes 051dbb918c7f ("crypto: digital signature verification support")
Cc <[email protected]> [v3.3+]
Signature · 2 lines
James Morris
<[email protected]>
#7 lore raw

PATCH 4/7 fscrypt: fix dereference of NULL user_key_payload

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

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")
Cc <[email protected]> [v4.1+]
---
 fs/crypto/keyinfo.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/crypto/keyinfo.c +5 -0
index 018c588c7ac3..8e704d12a1cf 100644
@@ -109,6 +109,11 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
 		goto out;
 	}
 	ukp = user_key_payload_locked(keyring_key);
+	if (!ukp) {
+		/* key was revoked before we acquired its semaphore */
+		res = -EKEYREVOKED;
+		goto out;
+	}
 	if (ukp->datalen != sizeof(struct fscrypt_key)) {
 		res = -EINVAL;
 		goto out;
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 4/7
#8 lore raw

Re: [PATCH 4/7] fscrypt: fix dereference of NULL user_key_payload

Quoted 8 lines · Eric Biggers

From: Eric Biggers <[email protected]>

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.

Quoted 6 lines

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")
Cc <[email protected]> [v4.1+]
Signature · 2 lines
James Morris
<[email protected]>
#9 lore raw

PATCH 5/7 ecryptfs: fix dereference of NULL user_key_payload

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

In eCryptfs, we failed to verify that the authentication token keys are not revoked before dereferencing their payloads, which is problematic because the payload of a revoked key is NULL. request_key() *does* skip revoked keys, but there is still a window where the key can be revoked before we acquire the key semaphore.

Fix it by updating ecryptfs_get_key_payload_data() to return -EKEYREVOKED if the key payload is NULL. For completeness we check this for "encrypted" keys as well as "user" keys, although encrypted keys cannot be revoked currently.

Alternatively we could use key_validate(), but since we'll also need to fix ecryptfs_get_key_payload_data() to validate the payload length, it seems appropriate to just check the payload pointer.

Fixes 237fead61998 ("[PATCH] ecryptfs: fs/Makefile and fs/Kconfig")
Cc <[email protected]> [v2.6.19+]
---
 fs/ecryptfs/ecryptfs_kernel.h | 24 +++++++++++++++++-------
 fs/ecryptfs/keystore.c        |  9 ++++++++-
 2 files changed, 25 insertions(+), 8 deletions(-)
fs/ecryptfs/ecryptfs_kernel.h +17 -7
index 9c351bf757b2..3fbc0ff79699 100644
@@ -84,11 +84,16 @@ struct ecryptfs_page_crypt_context {
 static inline struct ecryptfs_auth_tok *
 ecryptfs_get_encrypted_key_payload_data(struct key *key)
 {
-	if (key->type = &key_type_encrypted)
-		return (struct ecryptfs_auth_tok *)
-			(&((struct encrypted_key_payload *)key->payload.data[0])->payload_data);
-	else
+	struct encrypted_key_payload *payload;
+
+	if (key->type != &key_type_encrypted)
 		return NULL;
+
+	payload = key->payload.data[0];
+	if (!payload)
+		return ERR_PTR(-EKEYREVOKED);
+
+	return (struct ecryptfs_auth_tok *)payload->payload_data;
 }
 
 static inline struct key *ecryptfs_get_encrypted_key(char *sig)
@@ -114,12 +119,17 @@ static inline struct ecryptfs_auth_tok *
 ecryptfs_get_key_payload_data(struct key *key)
 {
 	struct ecryptfs_auth_tok *auth_tok;
+	struct user_key_payload *ukp;
 
 	auth_tok = ecryptfs_get_encrypted_key_payload_data(key);
-	if (!auth_tok)
-		return (struct ecryptfs_auth_tok *)user_key_payload_locked(key)->data;
-	else
+	if (auth_tok)
 		return auth_tok;
+
+	ukp = user_key_payload_locked(key);
+	if (!ukp)
+		return ERR_PTR(-EKEYREVOKED);
+
+	return (struct ecryptfs_auth_tok *)ukp->data;
 }
 
 #define ECRYPTFS_MAX_KEYSET_SIZE 1024
fs/ecryptfs/keystore.c +8 -1
index 3cf1546dca82..fa218cd64f74 100644
@@ -459,7 +459,8 @@ static int ecryptfs_verify_version(u16 version)
  * @auth_tok_key: key containing the authentication token
  * @auth_tok: authentication token
  *
- * Returns zero on valid auth tok; -EINVAL otherwise
+ * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or
+ * -EKEYREVOKED if the key was revoked before we acquired its semaphore.
  */
 static int
 ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
@@ -468,6 +469,12 @@ ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
 	int rc = 0;
 
 	(*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
+	if (IS_ERR(*auth_tok)) {
+		rc = PTR_ERR(*auth_tok);
+		*auth_tok = NULL;
+		goto out;
+	}
+
 	if (ecryptfs_verify_version((*auth_tok)->version)) {
 		printk(KERN_ERR "Data structure version mismatch. Userspace "
 		       "tools must match eCryptfs kernel module with major "
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 5/7
#10 lore raw

Re: [PATCH 5/7] ecryptfs: fix dereference of NULL user_key_payload

Quoted 13 lines · Eric Biggers

From: Eric Biggers <[email protected]>

In eCryptfs, we failed to verify that the authentication token keys are not revoked before dereferencing their payloads, which is problematic because the payload of a revoked key is NULL. request_key() *does* skip revoked keys, but there is still a window where the key can be revoked before we acquire the key semaphore.

Fix it by updating ecryptfs_get_key_payload_data() to return -EKEYREVOKED if the key payload is NULL. For completeness we check this for "encrypted" keys as well as "user" keys, although encrypted keys cannot be revoked currently.

Quoted 7 lines

Alternatively we could use key_validate(), but since we'll also need to fix ecryptfs_get_key_payload_data() to validate the payload length, it seems appropriate to just check the payload pointer.

Fixes 237fead61998 ("[PATCH] ecryptfs: fs/Makefile and fs/Kconfig")
Cc <[email protected]> [v2.6.19+]

(A further cleanup might add some inline accessor functions for key data, but it's not necessary now).

Signature · 2 lines
James Morris
<[email protected]>
#11 lore raw

PATCH 6/7 ecryptfs: fix out-of-bounds read of key payload

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

eCryptfs blindly casts the user-supplied key payload to a 'struct ecryptfs_auth_tok' without validating that the payload does, in fact, have the size of a 'struct ecryptfs_auth_tok'. Fix it.

Fixes 237fead61998 ("[PATCH] ecryptfs: fs/Makefile and fs/Kconfig")
Cc <[email protected]> [v2.6.19+]
---
 fs/ecryptfs/ecryptfs_kernel.h | 6 ++++++
 fs/ecryptfs/keystore.c        | 4 ++++
 2 files changed, 10 insertions(+)
fs/ecryptfs/ecryptfs_kernel.h +6 -0
index 3fbc0ff79699..945844d5f0ef 100644
@@ -93,6 +93,9 @@ ecryptfs_get_encrypted_key_payload_data(struct key *key)
 	if (!payload)
 		return ERR_PTR(-EKEYREVOKED);
 
+	if (payload->payload_datalen != sizeof(struct ecryptfs_auth_tok))
+		return ERR_PTR(-EINVAL);
+
 	return (struct ecryptfs_auth_tok *)payload->payload_data;
 }
 
@@ -129,6 +132,9 @@ ecryptfs_get_key_payload_data(struct key *key)
 	if (!ukp)
 		return ERR_PTR(-EKEYREVOKED);
 
+	if (ukp->datalen != sizeof(struct ecryptfs_auth_tok))
+		return ERR_PTR(-EINVAL);
+
 	return (struct ecryptfs_auth_tok *)ukp->data;
 }
 
fs/ecryptfs/keystore.c +4 -0
index fa218cd64f74..95e20ab67df3 100644
@@ -471,6 +471,10 @@ ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
 	(*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
 	if (IS_ERR(*auth_tok)) {
 		rc = PTR_ERR(*auth_tok);
+		if (rc == -EINVAL) {
+			ecryptfs_printk(KERN_ERR,
+					"Authentication token payload has wrong length\n");
+		}
 		*auth_tok = NULL;
 		goto out;
 	}
Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 6/7
#12 lore raw

Re: [PATCH 6/7] ecryptfs: fix out-of-bounds read of key payload

Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

eCryptfs blindly casts the user-supplied key payload to a 'struct ecryptfs_auth_tok' without validating that the payload does, in fact, have the size of a 'struct ecryptfs_auth_tok'. Fix it.

Fixes 237fead61998 ("[PATCH] ecryptfs: fs/Makefile and fs/Kconfig")
Cc <[email protected]> [v2.6.19+]
Signature · 2 lines
James Morris
<[email protected]>
#13 lore raw

PATCH 7/7 ecryptfs: move key payload accessor functions into keystore.c

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

As experience has shown, accessing the 'struct key' payload is very error-prone, since we need to hold the key semaphore and properly validate everything. Fortunately eCryptfs only does it from one place, in ecryptfs_verify_auth_tok_from_key() in keystore.c. Therefore, move the payload accessor functions like ecryptfs_get_key_payload_data() out of ecryptfs_kernel.h and into keystore.c so that people might be less tempted to use them directly.

---
 fs/ecryptfs/ecryptfs_kernel.h | 60 -------------------------------------------
 fs/ecryptfs/keystore.c        | 60 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 60 deletions(-)
fs/ecryptfs/ecryptfs_kernel.h +0 -60
index 945844d5f0ef..f2e339a6f9e9 100644
@@ -29,8 +29,6 @@
 #define ECRYPTFS_KERNEL_H
 
 #include <crypto/skcipher.h>
-#include <keys/user-type.h>
-#include <keys/encrypted-type.h>
 #include <linux/fs.h>
 #include <linux/fs_stack.h>
 #include <linux/namei.h>
@@ -80,64 +78,6 @@ struct ecryptfs_page_crypt_context {
 	} param;
 };
 
-#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE)
-static inline struct ecryptfs_auth_tok *
-ecryptfs_get_encrypted_key_payload_data(struct key *key)
-{
-	struct encrypted_key_payload *payload;
-
-	if (key->type != &key_type_encrypted)
-		return NULL;
-
-	payload = key->payload.data[0];
-	if (!payload)
-		return ERR_PTR(-EKEYREVOKED);
-
-	if (payload->payload_datalen != sizeof(struct ecryptfs_auth_tok))
-		return ERR_PTR(-EINVAL);
-
-	return (struct ecryptfs_auth_tok *)payload->payload_data;
-}
-
-static inline struct key *ecryptfs_get_encrypted_key(char *sig)
-{
-	return request_key(&key_type_encrypted, sig, NULL);
-}
-
-#else
-static inline struct ecryptfs_auth_tok *
-ecryptfs_get_encrypted_key_payload_data(struct key *key)
-{
-	return NULL;
-}
-
-static inline struct key *ecryptfs_get_encrypted_key(char *sig)
-{
-	return ERR_PTR(-ENOKEY);
-}
-
-#endif /* CONFIG_ENCRYPTED_KEYS */
-
-static inline struct ecryptfs_auth_tok *
-ecryptfs_get_key_payload_data(struct key *key)
-{
-	struct ecryptfs_auth_tok *auth_tok;
-	struct user_key_payload *ukp;
-
-	auth_tok = ecryptfs_get_encrypted_key_payload_data(key);
-	if (auth_tok)
-		return auth_tok;
-
-	ukp = user_key_payload_locked(key);
-	if (!ukp)
-		return ERR_PTR(-EKEYREVOKED);
-
-	if (ukp->datalen != sizeof(struct ecryptfs_auth_tok))
-		return ERR_PTR(-EINVAL);
-
-	return (struct ecryptfs_auth_tok *)ukp->data;
-}
-
 #define ECRYPTFS_MAX_KEYSET_SIZE 1024
 #define ECRYPTFS_MAX_CIPHER_NAME_SIZE 31
 #define ECRYPTFS_MAX_NUM_ENC_KEYS 64
fs/ecryptfs/keystore.c +60 -0

72 lines not on this page · raw message

Signature · 1 line
2.14.2.822.g60be5d43e6-goog
maintainer Reviewed-by on v1 7/7
#14 lore raw

Re: [PATCH 7/7] ecryptfs: move key payload accessor functions into keystore.c

Quoted 10 lines · Eric Biggers

From: Eric Biggers <[email protected]>

As experience has shown, accessing the 'struct key' payload is very error-prone, since we need to hold the key semaphore and properly validate everything. Fortunately eCryptfs only does it from one place, in ecryptfs_verify_auth_tok_from_key() in keystore.c. Therefore, move the payload accessor functions like ecryptfs_get_key_payload_data() out of ecryptfs_kernel.h and into keystore.c so that people might be less tempted to use them directly.

Quoted 5 lines
---
 fs/ecryptfs/ecryptfs_kernel.h | 60 -------------------------------------------
 fs/ecryptfs/keystore.c        | 60 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 60 deletions(-)

Ok, I should have started at this patch :)

Signature · 2 lines
James Morris
<[email protected]>