Skip to contentSkip to conversation

kzfree() v2

PATCH In review Johannes Weiner 10 participants 26 messages Feb 17, 2009 – Feb 23, 2009

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

cover kzfree() v2

This series introduces kzfree() and converts callsites which do memset() + kzfree() explicitely.

The caller intention is to ensure that security-sensitive data are cleared from slab objects before they are passed back to the allocator.

This also removes the last modular ksize() user (crypto/api.c) again by converting it to kzfree() which figures out the length of the memory region to zero internally.

I left out drivers/w1/w1{,_int}.c and dropped the conversion of drivers/atm/mpoa_caches.c in this iteration as I think they don't strictly need the zeroeing and the memsetting should probably be removed [ added Chas Williams and Evgeniy Polyakov to Cc ].

v2:
- EXPORT_SYMBOL(kzfree), thanks linker
- remove superfluous NULL checks, thanks Pekka
- mention `security' in the description

Hannes

 arch/s390/crypto/prng.c             |    3 +--
 crypto/api.c                        |    5 +----
 drivers/md/dm-crypt.c               |    6 ++----
 drivers/s390/crypto/zcrypt_pcixcc.c |    3 +--
 drivers/usb/host/hwa-hc.c           |    3 +--
 drivers/usb/wusbcore/cbaf.c         |    3 +--
 fs/cifs/connect.c                   |    6 +-----
 fs/cifs/misc.c                      |   10 ++--------
 fs/ecryptfs/keystore.c              |    3 +--
 fs/ecryptfs/messaging.c             |    3 +--
 include/linux/slab.h                |    1 +
 mm/util.c                           |   20 ++++++++++++++++++++
 12 files changed, 33 insertions(+), 33 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 1/7 slab: introduce kzfree()

kzfree() is a wrapper for kfree() that additionally zeroes the underlying memory before releasing it to the slab allocator.

Currently there is code which memset()s the memory region of an object before releasing it back to the slab allocator to make sure security-sensitive data are really zeroed out after use.

These callsites can then just use kzfree() which saves some code, makes users greppable and allows for a stupid destructor that isn't necessarily aware of the actual object size.

Cc Matt Mackall <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Nick Piggin <[email protected]>
---
 include/linux/slab.h |    1 +
 mm/util.c            |   20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)
include/linux/slab.h +1 -0
@@ -127,6 +127,7 @@ int kmem_ptr_validate(struct kmem_cache 
 void * __must_check __krealloc(const void *, size_t, gfp_t);
 void * __must_check krealloc(const void *, size_t, gfp_t);
 void kfree(const void *);
+void kzfree(const void *);
 size_t ksize(const void *);
 
 /*
mm/util.c +20 -0
@@ -129,6 +129,26 @@ void *krealloc(const void *p, size_t new
 }
 EXPORT_SYMBOL(krealloc);
 
+/**
+ * kzfree - like kfree but zero memory
+ * @p: object to free memory of
+ *
+ * The memory of the object @p points to is zeroed before freed.
+ * If @p is %NULL, kzfree() does nothing.
+ */
+void kzfree(const void *p)
+{
+	size_t ks;
+	void *mem = (void *)p;
+
+	if (unlikely(ZERO_OR_NULL_PTR(mem)))
+		return;
+	ks = ksize(mem);
+	memset(mem, 0, ks);
+	kfree(mem);
+}
+EXPORT_SYMBOL(kzfree);
+
 /*
  * strndup_user - duplicate an existing string from user space
  * @s: The string to duplicate

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [patch 1/7] slab: introduce kzfree()

In reply to Johannes Weiner
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

David

Signature · 3 lines
David Vrabel, Senior Software Engineer, Drivers
CSR, Churchill House, Cambridge Business Park,  Tel: +44 (0)1223 692562
Cowley Road, Cambridge, CB4 0WZ                 http://www.csr.com/
#4 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 4 lines · David Vrabel
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 8 lines · David Vrabel
Quoted 4 lines
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

I don't think so. It's debetable thing.

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

#6 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 7 lines · David Vrabel
Quoted 7 lines
Quoted 4 lines
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

Quoted 4 lines · KOSAKI Motohiro

I don't think so. It's debetable thing.

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [patch 1/7] slab: introduce kzfree()

2009/2/19 Pekka Enberg <[email protected]>:

Quoted 9 lines · David Vrabel
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

Quoted 8 lines
Quoted 4 lines · KOSAKI Motohiro

I don't think so. It's debetable thing.

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

Sure.
ok, I don't oppse this :)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [patch 1/7] slab: introduce kzfree()

In reply to Pekka Enberg
Quoted 9 lines · Pekka Enberg
Quoted 7 lines · David Vrabel
Quoted 7 lines
Quoted 4 lines
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

Quoted 8 lines
Quoted 4 lines · KOSAKI Motohiro

I don't think so. It's debetable thing.

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

There may be more important things for us to worry about, but I do strongly agree with KOSAKI-san on this.

kzfree() already differs from kfree() by a "z": that "z" says please zero the buffer pointed to; "const" says it won't modify the buffer pointed to. What sense does kzfree(const void *) make? Why is keeping the declarations the same apart from the "z" desirable?

By all means refuse to add kzfree(), but please don't add it with const.

I can see that the "const" in kfree(const void *) is debatable [looks to see how userspace free() is defined: without a const], I can see that it might be nice to have some "goesaway" attribute for such pointers instead; but I don't see how you can argue for kzalloc(const void *).

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 27 lines · Hugh Dickins
Quoted 9 lines · Pekka Enberg
Quoted 7 lines · David Vrabel
Quoted 7 lines
Quoted 4 lines
Quoted 1 line · Johannes Weiner

+void kzfree(const void *p)

Shouldn't this be void * since it writes to the memory?

No. kfree() writes to the memory as well to update freelists, poisoning and such so kzfree() is not at all different from it.

Quoted 8 lines
Quoted 4 lines · KOSAKI Motohiro

I don't think so. It's debetable thing.

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

There may be more important things for us to worry about, but I do strongly agree with KOSAKI-san on this.

kzfree() already differs from kfree() by a "z": that "z" says please zero the buffer pointed to; "const" says it won't modify the buffer pointed to. What sense does kzfree(const void *) make? Why is keeping the declarations the same apart from the "z" desirable?

Quoted 7 lines

By all means refuse to add kzfree(), but please don't add it with const.

I can see that the "const" in kfree(const void *) is debatable [looks to see how userspace free() is defined: without a const], I can see that it might be nice to have some "goesaway" attribute for such pointers instead; but I don't see how you can argue for kzalloc(const void *).

This is what Linus said last time this came up:

http://lkml.org/lkml/2008/1/16/227

Signature · 8 lines
http://selenic.com : development and support for Mercurial and Linux


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 18 lines · Matt Mackall
Quoted 17 lines · Hugh Dickins
Quoted 7 lines · Pekka Enberg
Quoted 3 lines · KOSAKI Motohiro

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

There may be more important things for us to worry about, but I do strongly agree with KOSAKI-san on this.

kzfree() already differs from kfree() by a "z": that "z" says please zero the buffer pointed to; "const" says it won't modify the buffer pointed to. What sense does kzfree(const void *) make? Why is keeping the declarations the same apart from the "z" desirable?

Quoted 7 lines
Quoted 7 lines

By all means refuse to add kzfree(), but please don't add it with const.

I can see that the "const" in kfree(const void *) is debatable [looks to see how userspace free() is defined: without a const], I can see that it might be nice to have some "goesaway" attribute for such pointers instead; but I don't see how you can argue for kzalloc(const void *).

    ^^^^^^^^^^^^^^^^^^^^^

(Of course I meant to say "kzfree(const void *)" there.)

Quoted 4 lines

This is what Linus said last time this came up:

http://lkml.org/lkml/2008/1/16/227

Thanks for that, I remember it now.

Okay, that's some justification for kfree(const void *).

But I fail to see it as a justification for kzfree(const void *):
if someone has "const char *string = kmalloc(size)" and then wants that string zeroed before it is freed, then I think it's quite right to cast out the const when calling kzfree().

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 34 lines · Hugh Dickins
Quoted 18 lines · Matt Mackall
Quoted 17 lines · Hugh Dickins
Quoted 7 lines · Pekka Enberg
Quoted 3 lines · KOSAKI Motohiro

poisonig is transparent feature from caller.
but the caller of kzfree() know to fill memory and it should know.

Debatable, sure, but doesn't seem like a big enough reason to make kzfree() differ from kfree().

There may be more important things for us to worry about, but I do strongly agree with KOSAKI-san on this.

kzfree() already differs from kfree() by a "z": that "z" says please zero the buffer pointed to; "const" says it won't modify the buffer pointed to. What sense does kzfree(const void *) make? Why is keeping the declarations the same apart from the "z" desirable?

Quoted 7 lines
Quoted 7 lines

By all means refuse to add kzfree(), but please don't add it with const.

I can see that the "const" in kfree(const void *) is debatable [looks to see how userspace free() is defined: without a const], I can see that it might be nice to have some "goesaway" attribute for such pointers instead; but I don't see how you can argue for kzalloc(const void *).

    ^^^^^^^^^^^^^^^^^^^^^

(Of course I meant to say "kzfree(const void *)" there.)

Quoted 4 lines

This is what Linus said last time this came up:

http://lkml.org/lkml/2008/1/16/227

Quoted 8 lines

Thanks for that, I remember it now.

Okay, that's some justification for kfree(const void *).

But I fail to see it as a justification for kzfree(const void *):
if someone has "const char *string = kmalloc(size)" and then wants that string zeroed before it is freed, then I think it's quite right to cast out the const when calling kzfree().

You could argue that the pointer passed to kzfree() points to an abstract slab object and kzfree() uses this to find the memory of that object which it then zeroes. The translation of course is a no-op as the object pointer and the memory pointer coincide.

It depends on how transparent you want to make kzfree() for the caller. Is it 'zero out and then free the object' or is it 'free the object, but note that it contains security-sensitive data, so make sure that it never gets into the hands of somebody else'?

No strong opinion from me, though, I can not say which one feels better. I made it intuitively const, so I guess I would lean to the more opaque version of the function.

Quoted 1 line

Hugh

Hannes

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw

Re: [patch 1/7] slab: introduce kzfree()

In reply to Hugh Dickins

Hi Hugh.

Quoted 8 lines · Hugh Dickins

Thanks for that, I remember it now.

Okay, that's some justification for kfree(const void *).

But I fail to see it as a justification for kzfree(const void *):
if someone has "const char *string = kmalloc(size)" and then wants that string zeroed before it is freed, then I think it's quite right to cast out the const when calling kzfree().

Quite frankly, I fail to see how kzfree() is fundamentally different from kfree(). I don't see kzfree() as a memset() + kfree() but rather as a kfree() "and make sure no one sees my data". So the zeroing happens _after_ you've invalidated the pointer with kzfree() so there's no "zeroing of buffer going on". So the way I see it, Linus' argument for having const for kfree() applies to kzfree().

That said, if you guys think it's a merge blocker, by all means remove the const. I just want few less open-coded ksize() users, that's all.

Pekka

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 7 lines · Pekka Enberg
Quoted 5 lines · Hugh Dickins

But I fail to see it as a justification for kzfree(const void *):
if someone has "const char *string = kmalloc(size)" and then wants that string zeroed before it is freed, then I think it's quite right to cast out the const when calling kzfree().

Quoted 5 lines

Quite frankly, I fail to see how kzfree() is fundamentally different from kfree(). I don't see kzfree() as a memset() + kfree() but rather as a kfree() "and make sure no one sees my data". So the zeroing happens _after_ you've invalidated the pointer with kzfree() so there's no "zeroing of buffer going on".

Well, that would be one way of picturing it, yes. Imagine the "z" as for "zap" rather than "zero", and the mechanism as opaque as Hannes suggests.

Quoted 5 lines

So the way I see it, Linus' argument for having const for kfree() applies to kzfree().

That said, if you guys think it's a merge blocker, by all means remove the const. I just want few less open-coded ksize() users, that's all.

I wouldn't call it a merge blocker, no; though I still think it makes far more sense without the "const" there.

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 12 lines · On Friday 20 February 2009 07:36:48 Hugh Dickins wrote:
Quoted 5 lines · Pekka Enberg

Quite frankly, I fail to see how kzfree() is fundamentally different from kfree(). I don't see kzfree() as a memset() + kfree() but rather as a kfree() "and make sure no one sees my data". So the zeroing happens _after_ you've invalidated the pointer with kzfree() so there's no "zeroing of buffer going on".

Well, that would be one way of picturing it, yes. Imagine the "z" as for "zap" rather than "zero", and the mechanism as opaque as Hannes suggests.

Quoted 8 lines
Quoted 5 lines

So the way I see it, Linus' argument for having const for kfree() applies to kzfree().

That said, if you guys think it's a merge blocker, by all means remove the const. I just want few less open-coded ksize() users, that's all.

I wouldn't call it a merge blocker, no; though I still think it makes far more sense without the "const" there.

Well, the buffer is only non-modified in the case of one of the allocators (SLAB). All others overwrite some of the data region with their own metadata.

I think it is OK to use const, though. Because k(z)free has the knowledge that the data will not be touched by the caller any longer.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#15 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 8 lines · Nick Piggin

Well, the buffer is only non-modified in the case of one of the allocators (SLAB). All others overwrite some of the data region with their own metadata.

I think it is OK to use const, though. Because k(z)free has the knowledge that the data will not be touched by the caller any longer.

Sorry, you're not adding anything new to the thread here.

Yes, the caller is surrendering the buffer, so we can get away with calling the argument const; and Linus argues that's helpful in the case of kfree (to allow passing a const pointer without having to cast it).

My contention is that kzfree(const void *ptr) is nonsensical because it says please zero this buffer without modifying it.

But the change has gone in, I seem to be the only one still bothered by it, and I've conceded that the "z" might stand for zap rather than zero.

So it may be saying please hide the contents of this buffer, rather than please zero it. And then it can be argued that the modification is an implementation detail which happens (like other housekeeping internal to the sl?b allocator) only after the original buffer has been freed.

Philosophy.

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [patch 1/7] slab: introduce kzfree()

Quoted 9 lines · On Tuesday 24 February 2009 01:51:05 Hugh Dickins wrote:
Quoted 7 lines · Nick Piggin

Well, the buffer is only non-modified in the case of one of the allocators (SLAB). All others overwrite some of the data region with their own metadata.

I think it is OK to use const, though. Because k(z)free has the knowledge that the data will not be touched by the caller any longer.

Quoted 6 lines

Sorry, you're not adding anything new to the thread here.

Yes, the caller is surrendering the buffer, so we can get away with calling the argument const; and Linus argues that's helpful in the case of kfree (to allow passing a const pointer without having to cast it).

(Yes, not that I agree his argument is strong enough to be able to call libc's definition wrong)

Quoted 7 lines

My contention is that kzfree(const void *ptr) is nonsensical because it says please zero this buffer without modifying it.

But the change has gone in, I seem to be the only one still bothered by it, and I've conceded that the "z" might stand for zap rather than zero.

Quoted 7 lines

So it may be saying please hide the contents of this buffer, rather than please zero it. And then it can be argued that the modification is an implementation detail which happens (like other housekeeping internal to the sl?b allocator) only after the original buffer has been freed.

Philosophy.

Hmm, well it better if kzfree is defined to zap rather than zero anyway. zap is a better definition because it theoretically allows the implementation to do something else (poision it with some other value; mark it as zapped and don't reallocate it without zeroing it; etc). And also it doesn't imply that the caller still cares about what it actually gets filled with.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#17 lore raw

Re: [patch 1/7] slab: introduce kzfree()

In reply to Hugh Dickins
Quoted 12 lines · On Mon, 23 Feb 2009 14:51:05 +0000 (GMT) Hugh Dickins <[email protected]> wrote:
Quoted 8 lines · Nick Piggin

Well, the buffer is only non-modified in the case of one of the allocators (SLAB). All others overwrite some of the data region with their own metadata.

I think it is OK to use const, though. Because k(z)free has the knowledge that the data will not be touched by the caller any longer.

Sorry, you're not adding anything new to the thread here.

Quoted 7 lines

Yes, the caller is surrendering the buffer, so we can get away with calling the argument const; and Linus argues that's helpful in the case of kfree (to allow passing a const pointer without having to cast it).

My contention is that kzfree(const void *ptr) is nonsensical because it says please zero this buffer without modifying it.

yup. The intent of kzfree() is explicitly, overtly, deliberately to modify the passed memory before freeing it. Marking it const is dopey.

But the const marker is potentially useful to some caller. An arguably misdesigned caller.

Quoted 3 lines

But the change has gone in, I seem to be the only one still bothered by it, and I've conceded that the "z" might stand for zap rather than zero.

Yeah. But it's a very small bother.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw

PATCH 2/7 crypto: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

---
 crypto/api.c |    5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
crypto/api.c +1 -4
@@ -569,20 +569,17 @@ EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
 {
 	struct crypto_alg *alg;
-	int size;
 
 	if (unlikely(!mem))
 		return;
 
 	alg = tfm->__crt_alg;
-	size = ksize(mem);
 
 	if (!tfm->exit && alg->cra_exit)
 		alg->cra_exit(tfm);
 	crypto_exit_ops(tfm);
 	crypto_mod_put(alg);
-	memset(mem, 0, size);
-	kfree(mem);
+	kzfree(mem);
 }
 EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer Acked-by on v1 2/7
#19 lore raw

Re: [patch 2/7] crypto: use kzfree()

Quoted 5 lines · Johannes Weiner

Use kzfree() instead of memset() + kfree().

Thanks,

Signature · 10 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#20 lore raw

PATCH 3/7 s390: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

Cc Martin Schwidefsky <[email protected]>
Cc Heiko Carstens <[email protected]>
---
 arch/s390/crypto/prng.c             |    3 +--
 drivers/s390/crypto/zcrypt_pcixcc.c |    3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/s390/crypto/prng.c +1 -2
@@ -201,8 +201,7 @@ out_free:
 static void __exit prng_exit(void)
 {
 	/* wipe me */
-	memset(p->buf, 0, prng_chunk_size);
-	kfree(p->buf);
+	kzfree(p->buf);
 	kfree(p);
 
 	misc_deregister(&prng_dev);
drivers/s390/crypto/zcrypt_pcixcc.c +1 -2
@@ -781,8 +781,7 @@ static long zcrypt_pcixcc_send_cprb(stru
 		/* Signal pending. */
 		ap_cancel_message(zdev->ap_dev, &ap_msg);
 out_free:
-	memset(ap_msg.message, 0x0, ap_msg.length);
-	kfree(ap_msg.message);
+	kzfree(ap_msg.message);
 	return rc;
 }
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw

PATCH 4/7 md: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

---
 drivers/md/dm-crypt.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/md/dm-crypt.c +2 -4
@@ -1137,8 +1137,7 @@ bad_ivmode:
 	crypto_free_ablkcipher(tfm);
 bad_cipher:
 	/* Must zero key material before freeing */
-	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
-	kfree(cc);
+	kzfree(cc);
 	return -EINVAL;
 }
 
@@ -1164,8 +1163,7 @@ static void crypt_dtr(struct dm_target *
 	dm_put_device(ti, cc->dev);
 
 	/* Must zero key material before freeing */
-	memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
-	kfree(cc);
+	kzfree(cc);
 }
 
 static int crypt_map(struct dm_target *ti, struct bio *bio,

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

PATCH 5/7 usb: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

---
 drivers/usb/host/hwa-hc.c   |    3 +--
 drivers/usb/wusbcore/cbaf.c |    3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
drivers/usb/host/hwa-hc.c +1 -2
@@ -464,8 +464,7 @@ static int __hwahc_dev_set_key(struct wu
 			port_idx << 8 | iface_no,
 			keyd, keyd_len, 1000 /* FIXME: arbitrary */);
 
-	memset(keyd, 0, sizeof(*keyd));	/* clear keys etc. */
-	kfree(keyd);
+	kzfree(keyd); /* clear keys etc. */
 	return result;
 }
 
drivers/usb/wusbcore/cbaf.c +1 -2
@@ -638,8 +638,7 @@ static void cbaf_disconnect(struct usb_i
 	usb_put_intf(iface);
 	kfree(cbaf->buffer);
 	/* paranoia: clean up crypto keys */
-	memset(cbaf, 0, sizeof(*cbaf));
-	kfree(cbaf);
+	kzfree(cbaf);
 }
 
 static struct usb_device_id cbaf_id_table[] = {

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by on v1 5/7
#23 lore raw

Re: [patch 5/7] usb: use kzfree()

This is in the WUSB code so:

David

Signature · 9 lines
David Vrabel, Senior Software Engineer, Drivers
CSR, Churchill House, Cambridge Business Park,  Tel: +44 (0)1223 692562
Cowley Road, Cambridge, CB4 0WZ                 http://www.csr.com/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#24 lore raw

PATCH 6/7 cifs: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

---
 fs/cifs/connect.c |    6 +-----
 fs/cifs/misc.c    |   10 ++--------
 2 files changed, 3 insertions(+), 13 deletions(-)
fs/cifs/connect.c +1 -5
@@ -2433,11 +2433,7 @@ mount_fail_check:
 out:
 	/* zero out password before freeing */
 	if (volume_info) {
-		if (volume_info->password != NULL) {
-			memset(volume_info->password, 0,
-				strlen(volume_info->password));
-			kfree(volume_info->password);
-		}
+		kzfree(volume_info->password);
 		kfree(volume_info->UNC);
 		kfree(volume_info->prepath);
 		kfree(volume_info);
fs/cifs/misc.c +2 -8
@@ -97,10 +97,7 @@ sesInfoFree(struct cifsSesInfo *buf_to_f
 	kfree(buf_to_free->serverOS);
 	kfree(buf_to_free->serverDomain);
 	kfree(buf_to_free->serverNOS);
-	if (buf_to_free->password) {
-		memset(buf_to_free->password, 0, strlen(buf_to_free->password));
-		kfree(buf_to_free->password);
-	}
+	kzfree(buf_to_free->password);
 	kfree(buf_to_free->domainName);
 	kfree(buf_to_free);
 }
@@ -132,10 +129,7 @@ tconInfoFree(struct cifsTconInfo *buf_to
 	}
 	atomic_dec(&tconInfoAllocCount);
 	kfree(buf_to_free->nativeFileSystem);
-	if (buf_to_free->password) {
-		memset(buf_to_free->password, 0, strlen(buf_to_free->password));
-		kfree(buf_to_free->password);
-	}
+	kzfree(buf_to_free->password);
 	kfree(buf_to_free);
 }
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#25 lore raw

PATCH 7/7 ecryptfs: use kzfree()

In reply to Johannes Weiner

Use kzfree() instead of memset() + kfree().

---
 fs/ecryptfs/keystore.c  |    3 +--
 fs/ecryptfs/messaging.c |    3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)
fs/ecryptfs/keystore.c +1 -2
@@ -740,8 +740,7 @@ ecryptfs_write_tag_70_packet(char *dest,
 out_release_free_unlock:
 	crypto_free_hash(s->hash_desc.tfm);
 out_free_unlock:
-	memset(s->block_aligned_filename, 0, s->block_aligned_filename_size);
-	kfree(s->block_aligned_filename);
+	kzfree(s->block_aligned_filename);
 out_unlock:
 	mutex_unlock(s->tfm_mutex);
 out:
fs/ecryptfs/messaging.c +1 -2
@@ -291,8 +291,7 @@ int ecryptfs_exorcise_daemon(struct ecry
 	if (daemon->user_ns)
 		put_user_ns(daemon->user_ns);
 	mutex_unlock(&daemon->mux);
-	memset(daemon, 0, sizeof(*daemon));
-	kfree(daemon);
+	kzfree(daemon);
 out:
 	return rc;
 }

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>