Skip to contentSkip to conversation

random: add and use memzero_explicit() for clearing data

PATCH Applied Daniel Borkmann 5 participants 11 messages Aug 25, 2014 – Dec 1, 2014

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

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

Reported-by [email protected]
Cc Hannes Frederic Sowa <[email protected]>
Cc Alexey Dobriyan <[email protected]>
---
 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
index c18d41d..8c86a95 100644
@@ -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
index d36977e..3b42b37 100644
@@ -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
index 992bf30..0f3a927 100644
@@ -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
#1 lore raw

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

#2 lore raw
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

Acked-by on v1 1/1
#3 lore raw
In reply to Daniel Borkmann
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 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. ]

Quoted 6 lines

Fixes kernel bugzilla: 82041

Reported-by [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

maintainer Applied
#4 lore raw
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 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. ]

Quoted 8 lines
Quoted 6 lines

Fixes kernel bugzilla: 82041

Reported-by [email protected]
Cc Hannes Frederic Sowa <[email protected]>
Cc Alexey Dobriyan <[email protected]>

Applied to the random tree, thanks.

- Ted

#5 lore raw
In reply to Daniel Borkmann
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 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

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

#6 lore raw
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 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. ]

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].

[1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.html

#7 lore raw
Quoted 4 lines · Daniel Borkmann

Well, BSD has helpers such as bzero_explicit() for such cases to work around this, which memzero_explicit() similarly does; see also [1].

[1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.html

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

#8 lore raw
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, which memzero_explicit() similarly does; see also [1].

[1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.html

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

#9 lore raw
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, which memzero_explicit() similarly does; see also [1].

   [1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.html

We 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).

#10 lore raw
In reply to Dan Carpenter
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, which memzero_explicit() similarly does; see also [1].

   [1] https://gcc.gnu.org/ml/gcc-help/2014-10/msg00059.html

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.

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.