Skip to content

Commit 14d4cc0

Browse files
committed
Merge tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random
Pull /dev/random updates from Ted Ts'o: "This adds a memzero_explicit() call which is guaranteed not to be optimized away by GCC. This is important when we are wiping cryptographically sensitive material" * tag 'random_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/random: crypto: memzero_explicit - make sure to clear out sensitive data random: add and use memzero_explicit() for clearing data
2 parents 1c45d9a + 7185ad2 commit 14d4cc0

10 files changed

Lines changed: 36 additions & 19 deletions

File tree

‎crypto/cts.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ static int cts_cbc_decrypt(struct crypto_cts_ctx *ctx,
202202
/* 5. Append the tail (BB - Ln) bytes of Xn (tmp) to Cn to create En */
203203
memcpy(s + bsize + lastn, tmp + lastn, bsize - lastn);
204204
/* 6. Decrypt En to create Pn-1 */
205-
memset(iv, 0, sizeof(iv));
205+
memzero_explicit(iv, sizeof(iv));
206+
206207
sg_set_buf(&sgsrc[0], s + bsize, bsize);
207208
sg_set_buf(&sgdst[0], d, bsize);
208209
err = crypto_blkcipher_decrypt_iv(&lcldesc, sgdst, sgsrc, bsize);

‎crypto/sha1_generic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
6464
src = data + done;
6565
} while (done + SHA1_BLOCK_SIZE <= len);
6666

67-
memset(temp, 0, sizeof(temp));
67+
memzero_explicit(temp, sizeof(temp));
6868
partial = 0;
6969
}
7070
memcpy(sctx->buffer + partial, src, len - done);

‎crypto/sha256_generic.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ static void sha256_transform(u32 *state, const u8 *input)
211211

212212
/* clear any sensitive info... */
213213
a = b = c = d = e = f = g = h = t1 = t2 = 0;
214-
memset(W, 0, 64 * sizeof(u32));
214+
memzero_explicit(W, 64 * sizeof(u32));
215215
}
216216

217-
218217
static int sha224_init(struct shash_desc *desc)
219218
{
220219
struct sha256_state *sctx = shash_desc_ctx(desc);
@@ -317,7 +316,7 @@ static int sha224_final(struct shash_desc *desc, u8 *hash)
317316
sha256_final(desc, D);
318317

319318
memcpy(hash, D, SHA224_DIGEST_SIZE);
320-
memset(D, 0, SHA256_DIGEST_SIZE);
319+
memzero_explicit(D, SHA256_DIGEST_SIZE);
321320

322321
return 0;
323322
}

‎crypto/sha512_generic.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static int sha384_final(struct shash_desc *desc, u8 *hash)
239239
sha512_final(desc, D);
240240

241241
memcpy(hash, D, 48);
242-
memset(D, 0, 64);
242+
memzero_explicit(D, 64);
243243

244244
return 0;
245245
}

‎crypto/tgr192.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ static int tgr160_final(struct shash_desc *desc, u8 * out)
612612

613613
tgr192_final(desc, D);
614614
memcpy(out, D, TGR160_DIGEST_SIZE);
615-
memset(D, 0, TGR192_DIGEST_SIZE);
615+
memzero_explicit(D, TGR192_DIGEST_SIZE);
616616

617617
return 0;
618618
}
@@ -623,7 +623,7 @@ static int tgr128_final(struct shash_desc *desc, u8 * out)
623623

624624
tgr192_final(desc, D);
625625
memcpy(out, D, TGR128_DIGEST_SIZE);
626-
memset(D, 0, TGR192_DIGEST_SIZE);
626+
memzero_explicit(D, TGR192_DIGEST_SIZE);
627627

628628
return 0;
629629
}

‎crypto/vmac.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static int vmac_final(struct shash_desc *pdesc, u8 *out)
613613
}
614614
mac = vmac(ctx->partial, ctx->partial_size, nonce, NULL, ctx);
615615
memcpy(out, &mac, sizeof(vmac_t));
616-
memset(&mac, 0, sizeof(vmac_t));
616+
memzero_explicit(&mac, sizeof(vmac_t));
617617
memset(&ctx->__vmac_ctx, 0, sizeof(struct vmac_ctx));
618618
ctx->partial_size = 0;
619619
return 0;

‎crypto/wp512.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,8 @@ static int wp384_final(struct shash_desc *desc, u8 *out)
11021102
u8 D[64];
11031103

11041104
wp512_final(desc, D);
1105-
memcpy (out, D, WP384_DIGEST_SIZE);
1106-
memset (D, 0, WP512_DIGEST_SIZE);
1105+
memcpy(out, D, WP384_DIGEST_SIZE);
1106+
memzero_explicit(D, WP512_DIGEST_SIZE);
11071107

11081108
return 0;
11091109
}
@@ -1113,8 +1113,8 @@ static int wp256_final(struct shash_desc *desc, u8 *out)
11131113
u8 D[64];
11141114

11151115
wp512_final(desc, D);
1116-
memcpy (out, D, WP256_DIGEST_SIZE);
1117-
memset (D, 0, WP512_DIGEST_SIZE);
1116+
memcpy(out, D, WP256_DIGEST_SIZE);
1117+
memzero_explicit(D, WP512_DIGEST_SIZE);
11181118

11191119
return 0;
11201120
}

‎drivers/char/random.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
11061106
__mix_pool_bytes(r, hash.w, sizeof(hash.w));
11071107
spin_unlock_irqrestore(&r->lock, flags);
11081108

1109-
memset(workspace, 0, sizeof(workspace));
1109+
memzero_explicit(workspace, sizeof(workspace));
11101110

11111111
/*
11121112
* In case the hash function has some recognizable output
@@ -1118,7 +1118,7 @@ static void extract_buf(struct entropy_store *r, __u8 *out)
11181118
hash.w[2] ^= rol32(hash.w[2], 16);
11191119

11201120
memcpy(out, &hash, EXTRACT_SIZE);
1121-
memset(&hash, 0, sizeof(hash));
1121+
memzero_explicit(&hash, sizeof(hash));
11221122
}
11231123

11241124
/*
@@ -1175,7 +1175,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
11751175
}
11761176

11771177
/* Wipe data just returned from memory */
1178-
memset(tmp, 0, sizeof(tmp));
1178+
memzero_explicit(tmp, sizeof(tmp));
11791179

11801180
return ret;
11811181
}
@@ -1218,7 +1218,7 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
12181218
}
12191219

12201220
/* Wipe data just returned from memory */
1221-
memset(tmp, 0, sizeof(tmp));
1221+
memzero_explicit(tmp, sizeof(tmp));
12221222

12231223
return ret;
12241224
}

‎include/linux/string.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) __printf(3, 4);
132132
#endif
133133

134134
extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
135-
const void *from, size_t available);
135+
const void *from, size_t available);
136136

137137
/**
138138
* strstarts - does @str start with @prefix?
@@ -144,7 +144,8 @@ static inline bool strstarts(const char *str, const char *prefix)
144144
return strncmp(str, prefix, strlen(prefix)) == 0;
145145
}
146146

147-
extern size_t memweight(const void *ptr, size_t bytes);
147+
size_t memweight(const void *ptr, size_t bytes);
148+
void memzero_explicit(void *s, size_t count);
148149

149150
/**
150151
* kbasename - return the last part of a pathname.

‎lib/string.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,22 @@ void *memset(void *s, int c, size_t count)
598598
EXPORT_SYMBOL(memset);
599599
#endif
600600

601+
/**
602+
* memzero_explicit - Fill a region of memory (e.g. sensitive
603+
* keying data) with 0s.
604+
* @s: Pointer to the start of the area.
605+
* @count: The size of the area.
606+
*
607+
* memzero_explicit() doesn't need an arch-specific version as
608+
* it just invokes the one of memset() implicitly.
609+
*/
610+
void memzero_explicit(void *s, size_t count)
611+
{
612+
memset(s, 0, count);
613+
OPTIMIZER_HIDE_VAR(s);
614+
}
615+
EXPORT_SYMBOL(memzero_explicit);
616+
601617
#ifndef __HAVE_ARCH_MEMCPY
602618
/**
603619
* memcpy - Copy one area of memory to another

0 commit comments

Comments
 (0)