x86/boot: Provide memzero_explicit
Subsystems (7)
PATCH v2 1/1 x86/boot: Provide memzero_explicit
The purgatory code now uses the shared lib/crypto/sha256.c sha256 implementation. This needs memzero_explicit, implement this.
---
Changes in v2:
- Add barrier_data() call after the memset, making the function really explicit. Using barrier_data() works fine in the purgatory (build) environment.
--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/boot/compressed/string.c +6 -0
@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +} +void *memmove (void *dest,const void *src,size_t n) {unsigned char *d = dest;
Signature · 1 line
2.23.0
Quoted 20 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/boot/compressed/string.c +0
-0
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 57
Quoted 8 lines
arch/x86/boot/compressed/string.c +5
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}
So the barrier_data() is only there to keep LTO from optimizing out the seemingly unused function?
Is there no canonical way to do that, instead of a seemingly obscure barrier_data() call - which would require a comment at minimum.
We do have $(DISABLE_LTO), not sure whether it's applicable here though.
Thanks,
Ingo
Hi,
Quoted 26 lines · Ingo Molnar
Quoted 23 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)arch/x86/boot/compressed/string.c +0
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; }
Quoted 8 lines
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 57
Quoted 5 lines
arch/x86/boot/compressed/string.c +5
-0+void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}So the
barrier_data()is only there to keep LTO from optimizing out the seemingly unused function?
I believe that Stephan Mueller (who suggested adding the barrier) was also worried about people using this as an example for other "explicit" functions which actually might get inlined.
This is not so much about protecting against LTO as it is against protecting against inlining, which in this case boils down to the same thing. Also this change makes the arch/x86/boot/compressed/string.c and lib/string.c versions identical which seems like a good thing to me (except for the code duplication part of it).
But I agree a comment would be good, how about:
void memzero_explicit(void *s, size_t count)
{
memset(s, 0, count);
/* Avoid the memset getting optimized away if we ever get inlined */
barrier_data(s);
}?
Regards,
Hans
Quoted 47 lines · Hans de Goede
Hi,
Quoted 18 lines · Ingo Molnar
Quoted 15 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)Quoted 15 lines
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 56
Quoted 12 lines
arch/x86/boot/compressed/string.c +5
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}So the
barrier_data()is only there to keep LTO from optimizing out the seemingly unused function?I believe that Stephan Mueller (who suggested adding the barrier) was also worried about people using this as an example for other "explicit" functions which actually might get inlined.
This is not so much about protecting against LTO as it is against protecting against inlining, which in this case boils down to the same thing. Also this change makes the
arch/x86/boot/compressed/string.candlib/string.cversions identical which seems like a good thing to me (except for the code duplication part of it).
Quoted 8 lines
But I agree a comment would be good, how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Avoid the memset getting optimized away if we ever get inlined */ barrier_data(s); }
Well, the standard construct for preventing inlining would be 'noinline', right? Any reason that wouldn't work?
Thanks,
Ingo
Hi,
Quoted 59 lines · Ingo Molnar
Quoted 47 lines · Hans de Goede
Hi,
Quoted 18 lines · Ingo Molnar
Quoted 15 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)Quoted 15 lines
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 56
Quoted 12 lines
arch/x86/boot/compressed/string.c +5
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}So the
barrier_data()is only there to keep LTO from optimizing out the seemingly unused function?I believe that Stephan Mueller (who suggested adding the barrier) was also worried about people using this as an example for other "explicit" functions which actually might get inlined.
This is not so much about protecting against LTO as it is against protecting against inlining, which in this case boils down to the same thing. Also this change makes the
arch/x86/boot/compressed/string.candlib/string.cversions identical which seems like a good thing to me (except for the code duplication part of it).Quoted 8 lines
But I agree a comment would be good, how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Avoid the memset getting optimized away if we ever get inlined */ barrier_data(s); }
Quoted 2 lines
Well, the standard construct for preventing inlining would be 'noinline', right? Any reason that wouldn't work?
Good question. I guess the worry is that modern compilers are getting more aggressive with optimizing and then even if not inlined if the function gets compiled in the same scope, then the compiler might still notice it is only every writing to the memory passed in; and then optimize it away of the write happens to memory which lifetime ends immediately afterwards. I mean removing the call is not inlining, so compiler developers might decide that that is still fine to do.
IMHO with trickycode like this is is best to just use the proven version from lib/string.c
I guess I made the comment to specific though, so how about:
void memzero_explicit(void *s, size_t count)
{
memset(s, 0, count);
/* Tell the compiler to never remove / optimize away the memset */
barrier_data(s);
}Regards,
Hans
Quoted 76 lines · Hans de Goede
Hi,
Quoted 59 lines · Ingo Molnar
Quoted 47 lines · Hans de Goede
Hi,
Quoted 18 lines · Ingo Molnar
Quoted 15 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)Quoted 15 lines
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 56
Quoted 12 lines
arch/x86/boot/compressed/string.c +5
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}So the
barrier_data()is only there to keep LTO from optimizing out the seemingly unused function?I believe that Stephan Mueller (who suggested adding the barrier) was also worried about people using this as an example for other "explicit" functions which actually might get inlined.
This is not so much about protecting against LTO as it is against protecting against inlining, which in this case boils down to the same thing. Also this change makes the
arch/x86/boot/compressed/string.candlib/string.cversions identical which seems like a good thing to me (except for the code duplication part of it).Quoted 8 lines
But I agree a comment would be good, how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Avoid the memset getting optimized away if we ever get inlined */ barrier_data(s); }Quoted 2 lines
Well, the standard construct for preventing inlining would be 'noinline', right? Any reason that wouldn't work?
Good question. I guess the worry is that modern compilers are getting more aggressive with optimizing and then even if not inlined if the function gets compiled in the same scope, then the compiler might still notice it is only every writing to the memory passed in; and then optimize it away of the write happens to memory which lifetime ends immediately afterwards. I mean removing the call is not inlining, so compiler developers might decide that that is still fine to do.
IMHO with trickycode like this is is best to just use the proven version from
lib/string.c
Quoted 8 lines
I guess I made the comment to specific though, so how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Tell the compiler to never remove / optimize away the memset */ barrier_data(s); }
Ok, I guess this will work.
Thanks,
Ingo
Quoted 88 lines · Ingo Molnar
Quoted 76 lines · Hans de Goede
Hi,
Quoted 59 lines · Ingo Molnar
Quoted 47 lines · Hans de Goede
Hi,
Quoted 18 lines · Ingo Molnar
Quoted 15 lines · Hans de Goede
The purgatory code now uses the shared
lib/crypto/sha256.csha256 implementation. This needsmemzero_explicit, implement this.Reported-by Arvind Sankar <[email protected]>Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Signed-off-by Hans de Goede <[email protected]>---
Changes in v2:
- Addbarrier_data()call after the memset, making the function really explicit. Usingbarrier_data()works fine in the purgatory (build) environment.--- arch/x86/boot/compressed/string.c | 6 ++++++ 1 file changed, 6 insertions(+)Quoted 15 lines
arch/x86/boot/compressed/string.c void *memset(void *s, int c, size_t n) · line 56
Quoted 12 lines
arch/x86/boot/compressed/string.c +5
-0@@ -50,6 +50,12 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}So the
barrier_data()is only there to keep LTO from optimizing out the seemingly unused function?I believe that Stephan Mueller (who suggested adding the barrier) was also worried about people using this as an example for other "explicit" functions which actually might get inlined.
This is not so much about protecting against LTO as it is against protecting against inlining, which in this case boils down to the same thing. Also this change makes the
arch/x86/boot/compressed/string.candlib/string.cversions identical which seems like a good thing to me (except for the code duplication part of it).Quoted 8 lines
But I agree a comment would be good, how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Avoid the memset getting optimized away if we ever get inlined */ barrier_data(s); }Quoted 2 lines
Well, the standard construct for preventing inlining would be 'noinline', right? Any reason that wouldn't work?
Good question. I guess the worry is that modern compilers are getting more aggressive with optimizing and then even if not inlined if the function gets compiled in the same scope, then the compiler might still notice it is only every writing to the memory passed in; and then optimize it away of the write happens to memory which lifetime ends immediately afterwards. I mean removing the call is not inlining, so compiler developers might decide that that is still fine to do.
IMHO with trickycode like this is is best to just use the proven version from
lib/string.cQuoted 8 lines
I guess I made the comment to specific though, so how about:
void
memzero_explicit(void *s, size_t count){ memset(s, 0, count); /* Tell the compiler to never remove / optimize away the memset */ barrier_data(s); }
Quoted 5 lines
Ok, I guess this will work.
Thanks,
Ingo
With the barrier in there, is there any reason to *not* inline the function? barrier_data() is an asm statement that tells the compiler that the asm uses the memory that was set to zero, thus preventing it from removing the memset even if nothing else uses that memory later. A more detailed comment is there in compiler-gcc.h. I can't see why it wouldn't work even if it were inlined.
If the function can indeed be inlined, we could just make the common implementation a macro and avoid duplicating it? As mentioned in another mail, we otherwise will likely need another duplicate implementation for arch/s390/purgatory as well.
Quoted 11 lines · Arvind Sankar
With the barrier in there, is there any reason to *not* inline the function?
barrier_data()is an asm statement that tells the compiler that the asm uses the memory that was set to zero, thus preventing it from removing the memset even if nothing else uses that memory later. A more detailed comment is there incompiler-gcc.h. I can't see why it wouldn't work even if it were inlined.If the function can indeed be inlined, we could just make the common implementation a macro and avoid duplicating it? As mentioned in another mail, we otherwise will likely need another duplicate implementation for
arch/s390/purgatoryas well.
I suspect macro would be justified in this case. Mind sending a v3 patch to demonstrate how it would all look like?
I'll zap v2 if the macro solution looks better.
Thanks,
Ingo
Quoted 15 lines · Ingo Molnar
Quoted 11 lines · Arvind Sankar
With the barrier in there, is there any reason to *not* inline the function?
barrier_data()is an asm statement that tells the compiler that the asm uses the memory that was set to zero, thus preventing it from removing the memset even if nothing else uses that memory later. A more detailed comment is there incompiler-gcc.h. I can't see why it wouldn't work even if it were inlined.If the function can indeed be inlined, we could just make the common implementation a macro and avoid duplicating it? As mentioned in another mail, we otherwise will likely need another duplicate implementation for
arch/s390/purgatoryas well.
Quoted 8 lines
I suspect macro would be justified in this case. Mind sending a v3 patch to demonstrate how it would all look like?
I'll zap v2 if the macro solution looks better.
Thanks,
Ingo
Patch attached to turn memzero_explicit into inline function.
From 25834b8040eff72478489be0bd8a2ff549af7f94 Mon Sep 17 00:00:00 2001
From: Arvind Sankar <[email protected]>
Date: Mon, 7 Oct 2019 14:34:24 -0400
Subject: [PATCH] lib/string: make memzero_explicit inline instead of external
With the use of the barrier implied by barrier_data(), there is no need for memzero_explicit to be extern. Making it inline saves the overhead of a function call, and allows the code to be reused in arch/*/purgatory without having to duplicate the implementation.
--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)
include/linux/string.h +20 -1
@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname.
lib/string.c +0 -21
@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/ -void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit); -#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t
Signature · 1 line
2.21.0
Hi,
Quoted 19 lines · Arvind Sankar
Quoted 18 lines · Ingo Molnar
Quoted 11 lines · Arvind Sankar
With the barrier in there, is there any reason to *not* inline the function?
barrier_data()is an asm statement that tells the compiler that the asm uses the memory that was set to zero, thus preventing it from removing the memset even if nothing else uses that memory later. A more detailed comment is there incompiler-gcc.h. I can't see why it wouldn't work even if it were inlined.If the function can indeed be inlined, we could just make the common implementation a macro and avoid duplicating it? As mentioned in another mail, we otherwise will likely need another duplicate implementation for
arch/s390/purgatoryas well.I suspect macro would be justified in this case. Mind sending a v3 patch to demonstrate how it would all look like?
Quoted 7 lines
Quoted 5 lines
I'll zap v2 if the macro solution looks better.
Thanks,
Ingo
Patch attached to turn
memzero_explicitinto inline function.
Hehe, I had prepared and have just tested the exact same patch (only the commit msg was different).
I've just booted a kernel build with that patch and that works fine (as expected).
So your patch is:
Since this is a bit of a core change though, I think it is best if you send it to the linux-kernel list (with my tags from above added) as is normally done for kernel patches. Then others, who may not be following this thread, will get a chance to give feedback on it.
Regards,
Hans
PATCH 1/1 lib/string: make memzero_explicit inline instead of external
With the use of the barrier implied by barrier_data(), there is no need for memzero_explicit to be extern. Making it inline saves the overhead of a function call, and allows the code to be reused in arch/*/purgatory without having to duplicate the implementation.
--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)
include/linux/string.h +20 -1
@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname.
lib/string.c +0 -21
@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/ -void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit); -#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t
Signature · 1 line
2.21.0
[tip: x86/urgent] lib/string: Make memzero_explicit() inline instead of external
The following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: bec500777089b3c96c53681fc0aa6fee59711d4a Gitweb: https://git.kernel.org/tip/bec500777089b3c96c53681fc0aa6fee59711d4a Author: Arvind Sankar <[email protected]> AuthorDate: Mon, 07 Oct 2019 18:00:02 -04:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 08 Oct 2019 13:27:05 +02:00
lib/string: Make memzero_explicit() inline instead of external
With the use of the barrier implied by barrier_data(), there is no need for memzero_explicit() to be extern. Making it inline saves the overhead of a function call, and allows the code to be reused in arch/*/purgatory without having to duplicate the implementation.
--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)
include/linux/string.h +20 -1
@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname.
lib/string.c +0 -21
@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/ -void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit); -#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t
[tip: x86/urgent] lib/string: Make memzero_explicit() inline instead of external
In reply to Arvind SankarThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: bec500777089b3c96c53681fc0aa6fee59711d4a Gitweb: https://git.kernel.org/tip/bec500777089b3c96c53681fc0aa6fee59711d4a Author: Arvind Sankar <[email protected]> AuthorDate: Mon, 07 Oct 2019 18:00:02 -04:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 08 Oct 2019 13:27:05 +02:00
lib/string: Make memzero_explicit() inline instead of external
With the use of the barrier implied by barrier_data(), there is no need for memzero_explicit() to be extern. Making it inline saves the overhead of a function call, and allows the code to be reused in arch/*/purgatory without having to duplicate the implementation.
--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)
include/linux/string.h +20 -1
@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname.
lib/string.c +0 -21
@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/ -void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit); -#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t
Re: [PATCH] lib/string: make memzero_explicit inline instead of external
In reply to Arvind SankarQuoted 69 lines · Arvind Sankar
With the use of the barrier implied by
barrier_data(), there is no need formemzero_explicitto be extern. Making it inline saves the overhead of a function call, and allows the code to be reused inarch/*/purgatorywithout having to duplicate the implementation.Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Reviewed-by Hans de Goede <[email protected]>Tested-by Hans de Goede <[email protected]>Signed-off-by Arvind Sankar <[email protected]>--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)include/linux/string.h +20
-1@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname. lib/string.c +0
-14@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/
lib/string.c void *memset(void *s, int c, size_t count) · line 753
Quoted 10 lines
lib/string.c +0
-7-void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit); -#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t
Thanks for the fix! Ccing kexec list since the problem is kexec/kdump related. People can try it when they see same issue.
Dave
Dave Young
Tested-byRe: [PATCH] lib/string: make memzero_explicit inline instead of external
Quoted 75 lines · Dave Young
Quoted 74 lines · Arvind Sankar
With the use of the barrier implied by
barrier_data(), there is no need formemzero_explicitto be extern. Making it inline saves the overhead of a function call, and allows the code to be reused inarch/*/purgatorywithout having to duplicate the implementation.Fixes 906a4bb97f5d ("crypto: sha256 - Use get/put_unaligned_be32 to get input, memzero_explicit")Reviewed-by Hans de Goede <[email protected]>Tested-by Hans de Goede <[email protected]>Signed-off-by Arvind Sankar <[email protected]>--- include/linux/string.h | 21 ++++++++++++++++++++- lib/string.c | 21 --------------------- 2 files changed, 20 insertions(+), 22 deletions(-)include/linux/string.h +20
-1@@ -227,7 +227,26 @@ static inline bool strstarts(const char *str, const char *prefix) }size_t memweight (const void *ptr,size_t bytes); -void memzero_explicit (void *s,size_t count); + +/** +* memzero_explicit - Fill a region of memory (e.g. sensitive +* keying data) with 0s. +* @s: Pointer to the start of the area. +* @count: The size of the area. +* +* Note: usually using memset() is just fine (!), but in cases +* where clearing out _local_ data at the end of a scope is +* necessary, memzero_explicit() should be used instead in +* order to prevent the compiler from optimising away zeroing. +* +* memzero_explicit() doesn't need an arch-specific version as +* it just invokes the one of memset() implicitly. +*/ +static inline void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +barrier_data (s); +}/** * kbasename - return the last part of a pathname. lib/string.c +0
-20@@ -748,27 +748,6 @@ void *memset(void *s, int c, size_t count)EXPORT_SYMBOL (memset);#endif -/** -* memzero_explicit - Fill a region of memory (e.g. sensitive -* keying data) with 0s. -* @s: Pointer to the start of the area. -* @count: The size of the area. -* -* Note: usually using memset() is just fine (!), but in cases -* where clearing out _local_ data at the end of a scope is -* necessary, memzero_explicit() should be used instead in -* order to prevent the compiler from optimising away zeroing. -* -* memzero_explicit() doesn't need an arch-specific version as -* it just invokes the one of memset() implicitly. -*/ -void memzero_explicit (void *s,size_t count) -{ -memset (s,0 , count); -barrier_data (s); -} -EXPORT_SYMBOL (memzero_explicit);
Quoted 9 lines
lib/string.c void *memset(void *s, int c, size_t count) · line 753
Quoted 5 lines
lib/string.c +0
-1-#ifndef __HAVE_ARCH_MEMSET16/** * memset16() - Fill a memory area with a uint16_t Thanks for the fix! Ccing kexec list since the problem is kexec/kdump related. People can try it when they see same issue.
Also:
Thanks
Dave
[tip: x86/urgent] x86/boot: Provide memzero_explicit()
In reply to Hans de GoedeThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: ee008a19f1c72c37ffa54326a592035dddb66fd6 Gitweb: https://git.kernel.org/tip/ee008a19f1c72c37ffa54326a592035dddb66fd6 Author: Hans de Goede <[email protected]> AuthorDate: Mon, 07 Oct 2019 15:47:24 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 07 Oct 2019 16:47:35 +02:00
x86/boot: Provide memzero_explicit()
The purgatory code now uses the shared lib/crypto/sha256.c sha256 implementation. This needs memzero_explicit(), implement this.
We also have barrier_data() call after the memset, making sure neither the compiler nor the linker optimizes out this seemingly unused function.
--- arch/x86/boot/compressed/string.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/boot/compressed/string.c +10 -0
@@ -50,6 +50,16 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +/* +* Make sure this function never gets inlined and +* the memset() never gets optimized away: +*/ +barrier_data (s); +} +void *memmove (void *dest,const void *src,size_t n) {unsigned char *d = dest;
[tip: x86/urgent] x86/boot: Provide memzero_explicit()
In reply to Hans de GoedeThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: ee008a19f1c72c37ffa54326a592035dddb66fd6 Gitweb: https://git.kernel.org/tip/ee008a19f1c72c37ffa54326a592035dddb66fd6 Author: Hans de Goede <[email protected]> AuthorDate: Mon, 07 Oct 2019 15:47:24 +02:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 07 Oct 2019 16:47:35 +02:00
x86/boot: Provide memzero_explicit()
The purgatory code now uses the shared lib/crypto/sha256.c sha256 implementation. This needs memzero_explicit(), implement this.
We also have barrier_data() call after the memset, making sure neither the compiler nor the linker optimizes out this seemingly unused function.
--- arch/x86/boot/compressed/string.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/x86/boot/compressed/string.c +10 -0
@@ -50,6 +50,16 @@ void *memset(void *s, int c, size_t n)return s; } +void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +/* +* Make sure this function never gets inlined and +* the memset() never gets optimized away: +*/ +barrier_data (s); +} +void *memmove (void *dest,const void *src,size_t n) {unsigned char *d = dest;