Skip to contentSkip to conversation

KEYS: Miscellaneous fixes

PATCH In review David Howells 2 participants 9 messages Sep 2, 2014 – Sep 3, 2014

Subsystems (5)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

cover KEYS: Miscellaneous fixes

Hi James,

Here are some fixes to go upstream:

(1) Increase root's key quotas so that the DNS resolver doesn't run out of capacity. This is a bit of a stop-gap whilst I try to improve quota recycling, but that's proving to be a bit tricky.

(2) Fix the definition of the public_key subtype to give the name length.

(3) Set a prefix for printing from signature handling.

(4) Fix a use after free in assoc_array_gc().

(5) Relax a check in the PKCS#7 parser to allow for padding inserted after a PKCS#7 message.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=keys-fixes

Tagged with:

	keys-fixes-20140902

David
---
David Howells (4):
KEYS: Fix public_key asymmetric key subtype name
KEYS: Set pr_fmt() in asymmetric key signature handling
KEYS: Fix use-after-free in assoc_array_gc()
PEFILE: Relax the check on the length of the PKCS#7 cert

Steve Dickson (1):
KEYS: Increase root_maxkeys and root_maxbytes sizes

 crypto/asymmetric_keys/public_key.c    |    1 +
 crypto/asymmetric_keys/signature.c     |    1 +
 crypto/asymmetric_keys/verify_pefile.c |   49 ++++++++++++++++++++++----------
 lib/assoc_array.c                      |    2 +
 security/keys/key.c                    |    4 +--
 5 files changed, 38 insertions(+), 19 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 KEYS: Increase root_maxkeys and root_maxbytes sizes

From: Steve Dickson <stevedredhatcom>

Now that NFS client uses the kernel key ring facility to store the NFSv4 id/gid mappings, the defaults for root_maxkeys and root_maxbytes need to be substantially increased.

These values have been soak tested:

	https://bugzilla.redhat.com/show_bug.cgi?id=1033708#c73

---

 security/keys/key.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
security/keys/key.c +2 -2
index b90a68c4e2c4..6d0cad16f002 100644
@@ -27,8 +27,8 @@ DEFINE_SPINLOCK(key_serial_lock);
 struct rb_root	key_user_tree; /* tree of quota records indexed by UID */
 DEFINE_SPINLOCK(key_user_lock);
 
-unsigned int key_quota_root_maxkeys = 200;	/* root's key count quota */
-unsigned int key_quota_root_maxbytes = 20000;	/* root's key space quota */
+unsigned int key_quota_root_maxkeys = 1000000;	/* root's key count quota */
+unsigned int key_quota_root_maxbytes = 25000000; /* root's key space quota */
 unsigned int key_quota_maxkeys = 200;		/* general key count quota */
 unsigned int key_quota_maxbytes = 20000;	/* general key space quota */
 
author maintainer
#2 lore raw

PATCH 2/5 KEYS: Fix public_key asymmetric key subtype name

In reply to David Howells

The length of the name of an asymmetric key subtype must be stored in struct asymmetric_key_subtype::name_len so that it can be matched by a search for "<subkey_name>:<partial_fingerprint>". Fix the public_key subtype to have name_len set.

---

 crypto/asymmetric_keys/public_key.c |    1 +
 1 file changed, 1 insertion(+)
crypto/asymmetric_keys/public_key.c +1 -0
index 97eb001960b9..2f6e4fb1a1ea 100644
@@ -121,6 +121,7 @@ static int public_key_verify_signature_2(const struct key *key,
 struct asymmetric_key_subtype public_key_subtype = {
 	.owner			= THIS_MODULE,
 	.name			= "public_key",
+	.name_len		= sizeof("public_key") - 1,
 	.describe		= public_key_describe,
 	.destroy		= public_key_destroy,
 	.verify_signature	= public_key_verify_signature_2,
author maintainer
#3 lore raw

PATCH 3/5 KEYS: Set pr_fmt() in asymmetric key signature handling

In reply to David Howells

Printing in base signature handling should have a prefix, so set pr_fmt().

---

 crypto/asymmetric_keys/signature.c |    1 +
 1 file changed, 1 insertion(+)
crypto/asymmetric_keys/signature.c +1 -0
index 50b3f880b4ff..7525fd183574 100644
@@ -11,6 +11,7 @@
  * 2 of the Licence, or (at your option) any later version.
  */
 
+#define pr_fmt(fmt) "SIG: "fmt
 #include <keys/asymmetric-subtype.h>
 #include <linux/module.h>
 #include <linux/err.h>
author maintainer
#4 lore raw

PATCH 4/5 KEYS: Fix use-after-free in assoc_array_gc()

In reply to David Howells

An edit script should be considered inaccessible by a function once it has called assoc_array_apply_edit() or assoc_array_cancel_edit().

However, assoc_array_gc() is accessing the edit script just after the
gc_complete: label.

---

 lib/assoc_array.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/assoc_array.c +1 -1
index c0b1007011e1..ae146f0734eb 100644
@@ -1735,7 +1735,7 @@ ascend_old_tree:
 gc_complete:
 	edit->set[0].to = new_root;
 	assoc_array_apply_edit(edit);
-	edit->array->nr_leaves_on_tree = nr_leaves_on_tree;
+	array->nr_leaves_on_tree = nr_leaves_on_tree;
 	return 0;
 
 enomem:
author maintainer
#5 lore raw

PATCH 5/5 PEFILE: Relax the check on the length of the PKCS#7 cert

In reply to David Howells

Relax the check on the length of the PKCS#7 cert as it appears that the PE file wrapper size gets rounded up to the nearest 8.

The debugging output looks like this:

	PEFILE: ==> verify_pefile_signature()
	PEFILE: ==> pefile_parse_binary()
	PEFILE: checksum @ 110
	PEFILE: header size = 200
	PEFILE: cert = 968 @547be0 [68 09 00 00 00 02 02 00 30 82 09 56 ]
	PEFILE: sig wrapper = { 968, 200, 2 }
	PEFILE: Signature data not PKCS#7

The wrapper is the first 8 bytes of the hex dump inside []. This indicates a length of 0x968 bytes, including the wrapper header - so 0x960 bytes of payload.

The ASN.1 wrapper begins [ ... 30 82 09 56 ]. That indicates an object of size 0x956 - a four byte discrepency, presumably just padding for alignment purposes.

So we just check that the ASN.1 container is no bigger than the payload and reduce the recorded size appropriately.

Whilst we're at it, allow shorter PKCS#7 objects that manage to squeeze within 127 or 255 bytes. It's just about conceivable if no X.509 certs are included in the PKCS#7 message.

---

 crypto/asymmetric_keys/verify_pefile.c |   49 ++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 16 deletions(-)
crypto/asymmetric_keys/verify_pefile.c +33 -16
index 79175e6ea0b2..90122831b818 100644
@@ -128,6 +128,7 @@ static int pefile_strip_sig_wrapper(const void *pebuf,
 {
 	struct win_certificate wrapper;
 	const u8 *pkcs7;
+	unsigned len;
 
 	if (ctx->sig_len < sizeof(wrapper)) {
 		pr_debug("Signature wrapper too short\n");
@@ -154,33 +155,49 @@ static int pefile_strip_sig_wrapper(const void *pebuf,
 		return -ENOTSUPP;
 	}
 
-	/* Looks like actual pkcs signature length is in wrapper->length.
-	 * size obtained from data dir entries lists the total size of
-	 * certificate table which is also aligned to octawrod boundary.
-	 *
-	 * So set signature length field appropriately.
+	/* It looks like the pkcs signature length in wrapper->length and the
+	 * size obtained from the data dir entries, which lists the total size
+	 * of certificate table, are both aligned to an octaword boundary, so
+	 * we may have to deal with some padding.
 	 */
 	ctx->sig_len = wrapper.length;
 	ctx->sig_offset += sizeof(wrapper);
 	ctx->sig_len -= sizeof(wrapper);
-	if (ctx->sig_len == 0) {
+	if (ctx->sig_len < 4) {
 		pr_debug("Signature data missing\n");
 		return -EKEYREJECTED;
 	}
 
-	/* What's left should a PKCS#7 cert */
+	/* What's left should be a PKCS#7 cert */
 	pkcs7 = pebuf + ctx->sig_offset;
-	if (pkcs7[0] == (ASN1_CONS_BIT | ASN1_SEQ)) {
-		if (pkcs7[1] == 0x82 &&
-		    pkcs7[2] == (((ctx->sig_len - 4) >> 8) & 0xff) &&
-		    pkcs7[3] ==  ((ctx->sig_len - 4)       & 0xff))
-			return 0;
-		if (pkcs7[1] == 0x80)
-			return 0;
-		if (pkcs7[1] > 0x82)
-			return -EMSGSIZE;
+	if (pkcs7[0] != (ASN1_CONS_BIT | ASN1_SEQ))
+		goto not_pkcs7;
+	
+	switch (pkcs7[1]) {
+	case 0 ... 0x7f:
+		len = pkcs7[1] + 2;
+		goto check_len;
+	case ASN1_INDEFINITE_LENGTH:
+		return 0;
+	case 0x81:
+		len = pkcs7[2] + 3;
+		goto check_len;
+	case 0x82:
+		len = ((pkcs7[2] << 8) | pkcs7[3]) + 4;
+		goto check_len;
+	case 0x83 ... 0xff:
+		return -EMSGSIZE;
+	default:
+		goto not_pkcs7;
 	}
 
+check_len:
+	if (len <= ctx->sig_len) {
+		/* There may be padding */
+		ctx->sig_len = len;
+		return 0;
+	}
+not_pkcs7:
 	pr_debug("Signature data not PKCS#7\n");
 	return -ELIBBAD;
 }
maintainer
#6 lore raw
In reply to David Howells
Quoted 4 lines · David Howells

Hi James,

Here are some fixes to go upstream:

Which of these need to go into current Linus?

#4 looks like it should.

Quoted 41 lines

(1) Increase root's key quotas so that the DNS resolver doesn't run out of capacity. This is a bit of a stop-gap whilst I try to improve quota recycling, but that's proving to be a bit tricky.

(2) Fix the definition of the public_key subtype to give the name length.

(3) Set a prefix for printing from signature handling.

(4) Fix a use after free in assoc_array_gc().

(5) Relax a check in the PKCS#7 parser to allow for padding inserted after a PKCS#7 message.

They can be found here also:

http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=keys-fixes

Tagged with:

	keys-fixes-20140902

David
---
David Howells (4):
KEYS: Fix public_key asymmetric key subtype name
KEYS: Set pr_fmt() in asymmetric key signature handling
KEYS: Fix use-after-free in assoc_array_gc()
PEFILE: Relax the check on the length of the PKCS#7 cert

Steve Dickson (1):
KEYS: Increase root_maxkeys and root_maxbytes sizes

 crypto/asymmetric_keys/public_key.c    |    1 +
 crypto/asymmetric_keys/signature.c     |    1 +
 crypto/asymmetric_keys/verify_pefile.c |   49 ++++++++++++++++++++++----------
 lib/assoc_array.c                      |    2 +
 security/keys/key.c                    |    4 +--
 5 files changed, 38 insertions(+), 19 deletions(-)
Signature · 2 lines
James Morris
<[email protected]>
author maintainer
#7 lore raw
Quoted 3 lines · James Morris

Which of these need to go into current Linus?

#4 looks like it should.

All of them, preferably, though I'm okay with #3 being deferred.

David

maintainer
#8 lore raw
Quoted 7 lines · David Howells
Quoted 3 lines · James Morris

Which of these need to go into current Linus?

#4 looks like it should.

All of them, preferably, though I'm okay with #3 being deferred.

Ok, I applied #3 to next.

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