random: add and use memzero_explicit() for clearing data
Subsystems (4)
PATCH 1/1 random: add and use memzero_explicit() for clearing data
zatimend has reported that in his environment (3.16/gcc4.8.3/corei7) memset() calls which clear out sensitive data in extract_{buf,entropy, entropy_user}() in random driver are being optimized away by gcc.
Add a helper memzero_explicit() (similarly as explicit_bzero() variants) that can be used in such cases where a variable with sensitive data is being cleared out in the end. Other use cases might also be in crypto code. [ I have put this into lib/string.c though, as it's always built-in and doesn't need any dependencies then. ]
Fixes kernel bugzilla: 82041
--- drivers/char/random.c | 8 ++++---- include/linux/string.h | 5 +++-- lib/string.c | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-)
drivers/char/random.c +4 -4
@@ -1106,7 +1106,7 @@ static void extract_buf(struct entropy_store *r, __u8 *out)__mix_pool_bytes (r, hash.w,sizeof (hash.w));spin_unlock_irqrestore (&r->lock, flags); -memset (workspace,0 ,sizeof (workspace)); +memzero_explicit (workspace,sizeof (workspace));/* * In case the hash function has some recognizable output
@@ -1118,7 +1118,7 @@ static void extract_buf(struct entropy_store *r, __u8 *out) hash.w[2 ] ^=rol32 (hash.w[2 ],16 );memcpy (out, &hash,EXTRACT_SIZE ); -memset (&hash,0 ,sizeof (hash)); +memzero_explicit (&hash,sizeof (hash)); }/*
@@ -1175,7 +1175,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf, }/* Wipe data just returned from memory */ -memset (tmp,0 ,sizeof (tmp)); +memzero_explicit (tmp,sizeof (tmp));return ret; }
@@ -1218,7 +1218,7 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf, }/* Wipe data just returned from memory */ -memset (tmp,0 ,sizeof (tmp)); +memzero_explicit (tmp,sizeof (tmp));return ret; }
include/linux/string.h +3 -2
@@ -132,7 +132,7 @@ int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);#endif extern ssize_t memory_read_from_buffer (void *to,size_t count,loff_t *ppos, -const void *from,size_t available); +const void *from,size_t available);/** * strstarts - does @str start with @prefix?
@@ -144,7 +144,8 @@ static inline bool strstarts(const char *str, const char *prefix)return strncmp (str, prefix,strlen (prefix)) ==0 ; } -extern size_t memweight (const void *ptr,size_t bytes); +size_t memweight (const void *ptr,size_t bytes); +void memzero_explicit (void *s,size_t count);/** * kbasename - return the last part of a pathname.
lib/string.c +16 -0
@@ -604,6 +604,22 @@ 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. +* +* 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); +OPTIMIZER_HIDE_VAR (s); +} +EXPORT_SYMBOL (memzero_explicit); +#ifndef __HAVE_ARCH_MEMCPY/** * memcpy - Copy one area of memory to another
Signature · 1 line
1.7.11.7
lib/string.c void *memset(void *s, int c, size_t count) · line 620
Quoted 5 lines · Daniel Borkmann
lib/string.c +5
-0+void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +OPTIMIZER_HIDE_VAR (s); +}
BSD seems to name it explicit_bzero().
Alexey
Quoted 8 lines · Alexey Dobriyan
lib/string.c void *memset(void *s, int c, size_t count) · line 620
Quoted 5 lines · Daniel Borkmann
lib/string.c +5
-0+void memzero_explicit (void *s,size_t count) +{ +memset (s,0 , count); +OPTIMIZER_HIDE_VAR (s); +}BSD seems to name it
explicit_bzero().
Sure, that's what I wrote in the commit log. But we don't have bzero(), and I think the name is more clear and conform to mem*() API.
Best,
Daniel
Quoted 10 lines · Daniel Borkmann
zatimend has reported that in his environment (3.16/gcc4.8.3/corei7)
memset()calls which clear out sensitive data in extract_{buf,entropy, entropy_user}() in random driver are being optimized away by gcc.Add a helper
memzero_explicit()(similarly asexplicit_bzero()variants) that can be used in such cases where a variable with sensitive data is being cleared out in the end. Other use cases might also be in crypto code. [ I have put this intolib/string.cthough, as it's always built-in and doesn't need any dependencies then. ]
Quoted 6 lines
Fixes kernel bugzilla: 82041
Reported-by [email protected]Signed-off-by Daniel Borkmann <[email protected]>Cc Hannes Frederic Sowa <[email protected]>Cc Alexey Dobriyan <[email protected]>
In case this pattern of important function calls getting optimized away emerges more often we could also go with a wrapper which forces the execution of the function, like:
include/linux/compiler.h +7 -0
@@ -181,6 +181,13 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);#define OPTIMIZER_HIDE_VAR (var) barrier()#endif +#ifndef OPTIMIZER_FORCE_CALL +#define OPTIMIZER_FORCE_CALL(func, args...) ({ \ + typeof(func) * volatile __func = (func); \ + __func(args); \ + }) +#endif +/* Not-quite-unique ID. */ #ifndef __UNIQUE_ID# define __UNIQUE_ID (prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
Thanks,
Hannes
Quoted 11 lines · Hannes Frederic Sowa
Quoted 10 lines · Daniel Borkmann
zatimend has reported that in his environment (3.16/gcc4.8.3/corei7)
memset()calls which clear out sensitive data in extract_{buf,entropy, entropy_user}() in random driver are being optimized away by gcc.Add a helper
memzero_explicit()(similarly asexplicit_bzero()variants) that can be used in such cases where a variable with sensitive data is being cleared out in the end. Other use cases might also be in crypto code. [ I have put this intolib/string.cthough, as it's always built-in and doesn't need any dependencies then. ]
Quoted 8 lines
Quoted 6 lines
Fixes kernel bugzilla: 82041
Reported-by [email protected]Signed-off-by Daniel Borkmann <[email protected]>Cc Hannes Frederic Sowa <[email protected]>Cc Alexey Dobriyan <[email protected]>
Applied to the random tree, thanks.
- Ted
Quoted 4 lines · Daniel Borkmann
zatimend has reported that in his environment (3.16/gcc4.8.3/corei7)
memset()calls which clear out sensitive data in extract_{buf,entropy, entropy_user}() in random driver are being optimized away by gcc.
Quoted 8 lines
Add a helper
memzero_explicit()(similarly asexplicit_bzero()variants) that can be used in such cases where a variable with sensitive data is being cleared out in the end. Other use cases might also be in crypto code. [ I have put this intolib/string.cthough, as it's always built-in and doesn't need any dependencies then. ]Fixes kernel bugzilla: 82041
What???
That's not ok. We totally rely on memset to work. Every single memset that I have added was absolutely necessary. This should be fixed so that memset works instead of adding a work around for specific drivers.
regards,
dan carpnter
Quoted 11 lines · Dan Carpenter
Quoted 10 lines · Daniel Borkmann
zatimend has reported that in his environment (3.16/gcc4.8.3/corei7)
memset()calls which clear out sensitive data in extract_{buf,entropy, entropy_user}() in random driver are being optimized away by gcc.Add a helper
memzero_explicit()(similarly asexplicit_bzero()variants) that can be used in such cases where a variable with sensitive data is being cleared out in the end. Other use cases might also be in crypto code. [ I have put this intolib/string.cthough, as it's always built-in and doesn't need any dependencies then. ]
Quoted 8 lines
Quoted 1 line
Fixes kernel bugzilla: 82041
What???
That's not ok. We totally rely on memset to work. Every single memset that I have added was absolutely necessary. This should be fixed so that memset works instead of adding a work around for specific drivers.
Well, BSD has helpers such as bzero_explicit() for such cases to work around this, which memzero_explicit() similarly does; see also [1].
Quoted 4 lines · Daniel Borkmann
Well, BSD has helpers such as
bzero_explicit()for such cases to work around this, whichmemzero_explicit()similarly does; see also [1].
We should make memset() a define and call a custom function internally.
Otherwise there are thousands of calls to memset() which we would need to audit. We could do some of this automatically but it's going to be a mess.
regards,
dan carpenter
Quoted 8 lines · Dan Carpenter
Quoted 4 lines · Daniel Borkmann
Well, BSD has helpers such as
bzero_explicit()for such cases to work around this, whichmemzero_explicit()similarly does; see also [1].We should make
memset()a define and call a custom function internally.
Or we could specify -fno-builtin-memset like the GCC people suggest.
regards,
dan carpenter
Quoted 10 lines · Dan Carpenter
Quoted 7 lines · Dan Carpenter
Quoted 4 lines · Daniel Borkmann
Well, BSD has helpers such as
bzero_explicit()for such cases to work around this, whichmemzero_explicit()similarly does; see also [1].[1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.htmlWe should make
memset()a define and call a custom function internally.Or we could specify -fno-builtin-memset like the GCC people suggest.
If there's a better, reliable way, I'm all for it. To me, memzero_explicit() seemed like the least intrusive variant doing the job, I'm unsure if globally setting -fno-builtin-memset will come with unforeseen side-effects on some archs (besides performance-wise).
Quoted 11 lines · Dan Carpenter
Quoted 4 lines · Daniel Borkmann
Well, BSD has helpers such as
bzero_explicit()for such cases to work around this, whichmemzero_explicit()similarly does; see also [1].[1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.htmlWe should make
memset()a define and call a custom function internally.Otherwise there are thousands of calls to
memset()which we would need to audit. We could do some of this automatically but it's going to be a mess.
Sort of, yeah; for now random driver and crypto subsystem, which I consider the main candidates, have been converted and with Julia's latest patch set from today (via coccinelle) also remaining arch-specific crypto drivers.