mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] KEYS: Fix ASN.1 indefinite length object parsing
@ 2016-05-12 11:11 David Howells
  2016-05-12 20:01 ` Linus Torvalds
  2016-05-12 22:12 ` David Howells
  0 siblings, 2 replies; 3+ messages in thread
From: David Howells @ 2016-05-12 11:11 UTC (permalink / raw)
  To: jmorris; +Cc: dhowells, torvalds, keyrings, linux-security-module, linux-kernel

Hi James,

Can you pass this patch on to Linus immediately please?

It is also tagged in my git tree here if you want to pull it:

  git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git tags/keys-fixes-20160512

Thanks,
David
---
KEYS: Fix ASN.1 indefinite length object parsing

This fixes CVE-2016-0758.

In the ASN.1 decoder, when the length field of an ASN.1 value is extracted,
it isn't validated against the remaining amount of data before being added
to the cursor.  With a sufficiently large size indicated, the check:

	datalen - dp < 2

may then fail due to integer overflow.

Fix this by checking the length indicated against the amount of remaining
data in both places a definite length is determined.

Whilst we're at it, make the following changes:

 (1) Check the maximum size of extended length does not exceed the capacity
     of the variable it's being stored in (len) rather than the type that
     variable is assumed to be (size_t).

 (2) Compare the EOC tag to the symbolic constant ASN1_EOC rather than the
     integer 0.

 (3) To reduce confusion, move the initialisation of len outside of:

	for (len = 0; n > 0; n--) {

     since it doesn't have anything to do with the loop counter n.

Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: David Woodhouse <David.Woodhouse@intel.com>
Acked-by: Peter Jones <pjones@redhat.com>
---
diff --git a/lib/asn1_decoder.c b/lib/asn1_decoder.c
index 2b3f46c049d4..554522934c44 100644
--- a/lib/asn1_decoder.c
+++ b/lib/asn1_decoder.c
@@ -74,7 +74,7 @@ next_tag:
 
 	/* Extract a tag from the data */
 	tag = data[dp++];
-	if (tag == 0) {
+	if (tag == ASN1_EOC) {
 		/* It appears to be an EOC. */
 		if (data[dp++] != 0)
 			goto invalid_eoc;
@@ -96,10 +96,8 @@ next_tag:
 
 	/* Extract the length */
 	len = data[dp++];
-	if (len <= 0x7f) {
-		dp += len;
-		goto next_tag;
-	}
+	if (len <= 0x7f)
+		goto check_length;
 
 	if (unlikely(len == ASN1_INDEFINITE_LENGTH)) {
 		/* Indefinite length */
@@ -110,14 +108,18 @@ next_tag:
 	}
 
 	n = len - 0x80;
-	if (unlikely(n > sizeof(size_t) - 1))
+	if (unlikely(n > sizeof(len) - 1))
 		goto length_too_long;
 	if (unlikely(n > datalen - dp))
 		goto data_overrun_error;
-	for (len = 0; n > 0; n--) {
+	len = 0;
+	for (; n > 0; n--) {
 		len <<= 8;
 		len |= data[dp++];
 	}
+check_length:
+	if (len > datalen - dp)
+		goto data_overrun_error;
 	dp += len;
 	goto next_tag;
 

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

* Re: [PATCH] KEYS: Fix ASN.1 indefinite length object parsing
  2016-05-12 11:11 [PATCH] KEYS: Fix ASN.1 indefinite length object parsing David Howells
@ 2016-05-12 20:01 ` Linus Torvalds
  2016-05-12 22:12 ` David Howells
  1 sibling, 0 replies; 3+ messages in thread
From: Linus Torvalds @ 2016-05-12 20:01 UTC (permalink / raw)
  To: David Howells; +Cc: James Morris, keyrings, LSM List, Linux Kernel Mailing List

On Thu, May 12, 2016 at 4:11 AM, David Howells <dhowells@redhat.com> wrote:
> Hi James,
>
> Can you pass this patch on to Linus immediately please?

I took it directly, since we're close to final release.

               Linus

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

* Re: [PATCH] KEYS: Fix ASN.1 indefinite length object parsing
  2016-05-12 11:11 [PATCH] KEYS: Fix ASN.1 indefinite length object parsing David Howells
  2016-05-12 20:01 ` Linus Torvalds
@ 2016-05-12 22:12 ` David Howells
  1 sibling, 0 replies; 3+ messages in thread
From: David Howells @ 2016-05-12 22:12 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: dhowells, James Morris, keyrings, LSM List, Linux Kernel Mailing List

Linus Torvalds <torvalds@linux-foundation.org> wrote:

> On Thu, May 12, 2016 at 4:11 AM, David Howells <dhowells@redhat.com> wrote:
> > Hi James,
> >
> > Can you pass this patch on to Linus immediately please?
> 
> I took it directly, since we're close to final release.

Thanks,
David

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

end of thread, other threads:[~2016-05-12 22:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-12 11:11 [PATCH] KEYS: Fix ASN.1 indefinite length object parsing David Howells
2016-05-12 20:01 ` Linus Torvalds
2016-05-12 22:12 ` David Howells

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®