Skip to contentSkip to conversation

dm-crypt: zero key before freeing it

PATCH In review Stefan Rompf 7 participants 12 messages Jan 4, 2006 – Jan 24, 2006 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm-crypt: zero key before freeing it

Hi Andrew,

dm-crypt does not clear struct crypt_config before freeing it. Thus, information on the key could leak f.e. to a swsusp image even after the encrypted device has been removed. The attached patch against 2.6.14 / 2.6.15 fixes it.

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.old +2 -0
@@ -694,6 +694,7 @@ bad3:
 bad2:
 	crypto_free_tfm(tfm);
 bad1:
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
@@ -710,6 +711,7 @@ static void crypt_dtr(struct dm_target *
 		cc->iv_gen_ops->dtr(cc);
 	crypto_free_tfm(cc->tfm);
 	dm_put_device(ti, cc->dev);
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 }
 
#1 lore raw
Quoted 6 lines · Stefan Rompf

Hi Andrew,

dm-crypt does not clear struct crypt_config before freeing it. Thus, information on the key could leak f.e. to a swsusp image even after the encrypted device has been removed. The attached patch against 2.6.14 / 2.6.15 fixes it.

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

author Acked-by Clemens Fruhwirth
#2 lore raw

Am Mittwoch 04 Januar 2006 21:09 schrieb Arjan van de Ven:

Quoted 3 lines

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

Valid objection, here is an update (and see, I'm running 2.6.15 now ;-)

Stefan

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.orig +5 -0
@@ -690,6 +690,8 @@
 bad2:
 	crypto_free_tfm(tfm);
 bad1:
+	/* Must zero key material before freeing */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
@@ -706,6 +708,9 @@
 		cc->iv_gen_ops->dtr(cc);
 	crypto_free_tfm(cc->tfm);
 	dm_put_device(ti, cc->dev);
+
+	/* Must zero key material before freeing */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 }
 
#3 lore raw
Quoted 7 lines · Stefan Rompf

Am Mittwoch 04 Januar 2006 21:09 schrieb Arjan van de Ven:

Quoted 3 lines

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

Valid objection, here is an update (and see, I'm running 2.6.15 now ;-)

A reason "why" would be more helpful that a "what".

Quoted 25 lines
drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.orig +5 -1
@@ -690,6 +690,8 @@
 bad2:
 	crypto_free_tfm(tfm);
 bad1:
+	/* Must zero key material before freeing */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
@@ -706,6 +708,9 @@
 		cc->iv_gen_ops->dtr(cc);
 	crypto_free_tfm(cc->tfm);
 	dm_put_device(ti, cc->dev);
+
+	/* Must zero key material before freeing */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 }
 
-
Signature · 1 line
~Randy
#4 lore raw
Quoted 10 lines · Randy.Dunlap
Quoted 7 lines · Stefan Rompf

Am Mittwoch 04 Januar 2006 21:09 schrieb Arjan van de Ven:

Quoted 3 lines

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

Valid objection, here is an update (and see, I'm running 2.6.15 now ;-)

A reason "why" would be more helpful that a "what".

"prevent information leak"

This is still a "what", but at least not a "how".

Jörn

Signature · 4 lines
Linux is more the core point of a concept that surrounds "open source"
which, in turn, is based on a false concept. This concept is that
people actually want to look at source code.
-- Rob Enderle
#5 lore raw
Quoted 8 lines · Jörn Engel
Quoted 7 lines · Randy.Dunlap
Quoted 6 lines · Stefan Rompf

Am Mittwoch 04 Januar 2006 21:09 schrieb Arjan van de Ven:

Quoted 3 lines

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Valid objection, here is an update (and see, I'm running 2.6.15 now ;-)

A reason "why" would be more helpful that a "what".

"prevent information leak"

This is still a "what", but at least not a "how".

OK, that's a much better changelog entry or source code comment... if it could be put in one of those places.

Thanks.

Signature · 1 line
~Randy
#6 lore raw
Quoted 12 lines · Randy.Dunlap
Quoted 10 lines · J?rn Engel
Quoted 9 lines · Randy.Dunlap
Quoted 7 lines · Stefan Rompf

Am Mittwoch 04 Januar 2006 21:09 schrieb Arjan van de Ven:

Quoted 3 lines

since a memset right before a free is a very unusual code pattern in the kernel it may well be worth putting a short comment around it to prevent someone later removing it as "optimization"

Valid objection, here is an update (and see, I'm running 2.6.15 now ;-)

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

A reason "why" would be more helpful that a "what".

"prevent information leak"

This is still a "what", but at least not a "how".

OK, that's a much better changelog entry or source code comment... if it could be put in one of those places.

Yes, Stefan, care to redo this with an updated changelog command?

thanks,

greg k-h

#7 lore raw

PATCH 1/1 dm-crypt: Zero key material before free to avoid information leak

Am Mittwoch 04 Januar 2006 22:15 schrieb Greg KH:

Quoted 1 line

Yes, Stefan, care to redo this with an updated changelog command?

dm-crypt should clear struct crypt_config before freeing it to avoid information leak f.e. to a swsusp image.

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.orig +5 -0
@@ -690,6 +690,8 @@
 bad2:
 	crypto_free_tfm(tfm);
 bad1:
+	/* Zero key material before free to avoid information leak */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
@@ -706,6 +708,9 @@
 		cc->iv_gen_ops->dtr(cc);
 	crypto_free_tfm(cc->tfm);
 	dm_put_device(ti, cc->dev);
+
+	/* Zero key material before free to avoid information leak */
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 }
 
#8 lore raw
In reply to Stefan Rompf
Quoted 19 lines

Hi Andrew,

dm-crypt does not clear struct crypt_config before freeing it. Thus, information on the key could leak f.e. to a swsusp image even after the encrypted device has been removed. The attached patch against 2.6.14 / 2.6.15 fixes it.

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.old +0 -0
@@ -694,6 +694,7 @@ bad3:

bad2:

	crypto_free_tfm(tfm);

bad1:

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.old +1 -0
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Quoted 3 lines
	kfree(cc);
	return -EINVAL;
}

There is a small problem with this patch.
If the 'goto bad1' branch is taken, then 'cc->key_size' will not be defined.
I think you need the following patch on top.

(Is that "sizeof(u8)" *really* necessary?? :-)

NeilBrown

### Diffstat output

 ./drivers/md/dm-crypt.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c~current~ +1 -1
diff ./drivers/md/dm-crypt.c~current~ ./drivers/md/dm-crypt.c
@@ -691,7 +691,7 @@ bad2:
 	crypto_free_tfm(tfm);
 bad1:
 	/* Must zero key material before freeing */
-	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
+	memset(cc, 0, sizeof(*cc) + key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
#9 lore raw

Am Dienstag 24 Januar 2006 05:49 schrieb Neil Brown:

Quoted 7 lines
Quoted 7 lines

--- linux-2.6.14.4/drivers/md/dm-crypt.c.old 2005-12-16 18:27:05.000000000
+0100 +++ linux-2.6.14.4/drivers/md/dm-crypt.c 2005-12-28
12:49:13.000000000 +0100 @@ -694,6 +694,7 @@ bad3:
bad2:

	crypto_free_tfm(tfm);

bad1:

(quoted diff) +1 -0
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Quoted 8 lines
Quoted 3 lines
	kfree(cc);
	return -EINVAL;
}

There is a small problem with this patch.
If the 'goto bad1' branch is taken, then 'cc->key_size' will not be defined.
I think you need the following patch on top.

Why? This is from today's git, just before the first goto bad1

559 cc->key_size = key_size;
560 if ((!key_size && strcmp(argv[1], "-") != 0) ||
561 (key_size && crypt_decode_key(cc->key, argv[1], key_size) <
0)) {
562 ti->error = PFX "Error decoding key";
563 goto bad1;
564 }

Stefan

#10 lore raw
Quoted 28 lines · On Tuesday January 24, [email protected] wrote:

Am Dienstag 24 Januar 2006 05:49 schrieb Neil Brown:

Quoted 7 lines
Quoted 7 lines

--- linux-2.6.14.4/drivers/md/dm-crypt.c.old 2005-12-16 18:27:05.000000000
+0100 +++ linux-2.6.14.4/drivers/md/dm-crypt.c 2005-12-28
12:49:13.000000000 +0100 @@ -694,6 +694,7 @@ bad3:
bad2:

	crypto_free_tfm(tfm);

bad1:

(quoted diff) +1 -0
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Quoted 8 lines
Quoted 3 lines
	kfree(cc);
	return -EINVAL;
}

There is a small problem with this patch.
If the 'goto bad1' branch is taken, then 'cc->key_size' will not be defined.
I think you need the following patch on top.

Why? This is from today's git, just before the first goto bad1

559 cc->key_size = key_size;
560 if ((!key_size && strcmp(argv[1], "-") != 0) ||
561 (key_size && crypt_decode_key(cc->key, argv[1], key_size) <
0)) {
562 ti->error = PFX "Error decoding key";
563 goto bad1;
564 }

Quoted 1 line

Stefan

Ahhh.... sorry, 'bout that. You are right. I was looking at an older kernel and assumed that bit of code hadn't been re-arrange... My bad. Pardon the noise.

NeilBrown

#11 lore raw
In reply to Stefan Rompf

Once the page is placed on the free list, doesn't that prevent it from being swapped out? swsusp doesn't bother saving free pages and before the pages can be recycled, the kernel zeros them right?

Quoted 29 lines · Stefan Rompf

Hi Andrew,

dm-crypt does not clear struct crypt_config before freeing it. Thus, information on the key could leak f.e. to a swsusp image even after the encrypted device has been removed. The attached patch against 2.6.14 / 2.6.15 fixes it.

drivers/md/dm-crypt.c renamed from drivers/md/dm-crypt.c.old +2 -1
@@ -694,6 +694,7 @@ bad3:
 bad2:
 	crypto_free_tfm(tfm);
 bad1:
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 	return -EINVAL;
 }
@@ -710,6 +711,7 @@ static void crypt_dtr(struct dm_target *
 		cc->iv_gen_ops->dtr(cc);
 	crypto_free_tfm(cc->tfm);
 	dm_put_device(ti, cc->dev);
+	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
 	kfree(cc);
 }
 
 
-