Skip to content

Commit ba6ef8a

Browse files
committed
Merge tag 'random-5.17-rc3-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random
Pull random number generator fixes from Jason Donenfeld: "For this week, we have: - A fix to make more frequent use of hwgenerator randomness, from Dominik. - More cleanups to the boot initialization sequence, from Dominik. - A fix for an old shortcoming with the ZAP ioctl, from me. - A workaround for a still unfixed Clang CFI/FullLTO compiler bug, from me. On one hand, it's a bummer to commit workarounds for experimental compiler features that have bugs. But on the other, I think this actually improves the code somewhat, independent of the bug. So a win-win" * tag 'random-5.17-rc3-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/crng/random: random: only call crng_finalize_init() for primary_crng random: access primary_pool directly rather than through pointer random: wake up /dev/random writers after zap random: continually use hwgenerator randomness lib/crypto: blake2s: avoid indirect calls to compression function for Clang CFI
2 parents ddb16b0 + 9d5505f commit ba6ef8a

File tree

6 files changed

+55
-40
lines changed

6 files changed

+55
-40
lines changed

arch/arm/crypto/blake2s-shash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
static int crypto_blake2s_update_arm(struct shash_desc *desc,
1414
const u8 *in, unsigned int inlen)
1515
{
16-
return crypto_blake2s_update(desc, in, inlen, blake2s_compress);
16+
return crypto_blake2s_update(desc, in, inlen, false);
1717
}
1818

1919
static int crypto_blake2s_final_arm(struct shash_desc *desc, u8 *out)
2020
{
21-
return crypto_blake2s_final(desc, out, blake2s_compress);
21+
return crypto_blake2s_final(desc, out, false);
2222
}
2323

2424
#define BLAKE2S_ALG(name, driver_name, digest_size) \

arch/x86/crypto/blake2s-shash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
static int crypto_blake2s_update_x86(struct shash_desc *desc,
1919
const u8 *in, unsigned int inlen)
2020
{
21-
return crypto_blake2s_update(desc, in, inlen, blake2s_compress);
21+
return crypto_blake2s_update(desc, in, inlen, false);
2222
}
2323

2424
static int crypto_blake2s_final_x86(struct shash_desc *desc, u8 *out)
2525
{
26-
return crypto_blake2s_final(desc, out, blake2s_compress);
26+
return crypto_blake2s_final(desc, out, false);
2727
}
2828

2929
#define BLAKE2S_ALG(name, driver_name, digest_size) \

crypto/blake2s_generic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
static int crypto_blake2s_update_generic(struct shash_desc *desc,
1616
const u8 *in, unsigned int inlen)
1717
{
18-
return crypto_blake2s_update(desc, in, inlen, blake2s_compress_generic);
18+
return crypto_blake2s_update(desc, in, inlen, true);
1919
}
2020

2121
static int crypto_blake2s_final_generic(struct shash_desc *desc, u8 *out)
2222
{
23-
return crypto_blake2s_final(desc, out, blake2s_compress_generic);
23+
return crypto_blake2s_final(desc, out, true);
2424
}
2525

2626
#define BLAKE2S_ALG(name, driver_name, digest_size) \

drivers/char/random.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ static bool crng_init_try_arch(struct crng_state *crng)
762762
return arch_init;
763763
}
764764

765-
static bool __init crng_init_try_arch_early(struct crng_state *crng)
765+
static bool __init crng_init_try_arch_early(void)
766766
{
767767
int i;
768768
bool arch_init = true;
@@ -774,7 +774,7 @@ static bool __init crng_init_try_arch_early(struct crng_state *crng)
774774
rv = random_get_entropy();
775775
arch_init = false;
776776
}
777-
crng->state[i] ^= rv;
777+
primary_crng.state[i] ^= rv;
778778
}
779779

780780
return arch_init;
@@ -788,22 +788,20 @@ static void crng_initialize_secondary(struct crng_state *crng)
788788
crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
789789
}
790790

791-
static void __init crng_initialize_primary(struct crng_state *crng)
791+
static void __init crng_initialize_primary(void)
792792
{
793-
_extract_entropy(&crng->state[4], sizeof(u32) * 12);
794-
if (crng_init_try_arch_early(crng) && trust_cpu && crng_init < 2) {
793+
_extract_entropy(&primary_crng.state[4], sizeof(u32) * 12);
794+
if (crng_init_try_arch_early() && trust_cpu && crng_init < 2) {
795795
invalidate_batched_entropy();
796796
numa_crng_init();
797797
crng_init = 2;
798798
pr_notice("crng init done (trusting CPU's manufacturer)\n");
799799
}
800-
crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
800+
primary_crng.init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
801801
}
802802

803-
static void crng_finalize_init(struct crng_state *crng)
803+
static void crng_finalize_init(void)
804804
{
805-
if (crng != &primary_crng || crng_init >= 2)
806-
return;
807805
if (!system_wq) {
808806
/* We can't call numa_crng_init until we have workqueues,
809807
* so mark this for processing later. */
@@ -814,6 +812,7 @@ static void crng_finalize_init(struct crng_state *crng)
814812
invalidate_batched_entropy();
815813
numa_crng_init();
816814
crng_init = 2;
815+
crng_need_final_init = false;
817816
process_random_ready_list();
818817
wake_up_interruptible(&crng_init_wait);
819818
kill_fasync(&fasync, SIGIO, POLL_IN);
@@ -980,7 +979,8 @@ static void crng_reseed(struct crng_state *crng, bool use_input_pool)
980979
memzero_explicit(&buf, sizeof(buf));
981980
WRITE_ONCE(crng->init_time, jiffies);
982981
spin_unlock_irqrestore(&crng->lock, flags);
983-
crng_finalize_init(crng);
982+
if (crng == &primary_crng && crng_init < 2)
983+
crng_finalize_init();
984984
}
985985

986986
static void _extract_crng(struct crng_state *crng, u8 out[CHACHA_BLOCK_SIZE])
@@ -1697,8 +1697,8 @@ int __init rand_initialize(void)
16971697
{
16981698
init_std_data();
16991699
if (crng_need_final_init)
1700-
crng_finalize_init(&primary_crng);
1701-
crng_initialize_primary(&primary_crng);
1700+
crng_finalize_init();
1701+
crng_initialize_primary();
17021702
crng_global_init_time = jiffies;
17031703
if (ratelimit_disable) {
17041704
urandom_warning.interval = 0;
@@ -1856,7 +1856,10 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
18561856
*/
18571857
if (!capable(CAP_SYS_ADMIN))
18581858
return -EPERM;
1859-
input_pool.entropy_count = 0;
1859+
if (xchg(&input_pool.entropy_count, 0) && random_write_wakeup_bits) {
1860+
wake_up_interruptible(&random_write_wait);
1861+
kill_fasync(&fasync, SIGIO, POLL_OUT);
1862+
}
18601863
return 0;
18611864
case RNDRESEEDCRNG:
18621865
if (!capable(CAP_SYS_ADMIN))
@@ -2205,13 +2208,15 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
22052208
return;
22062209
}
22072210

2208-
/* Suspend writing if we're above the trickle threshold.
2211+
/* Throttle writing if we're above the trickle threshold.
22092212
* We'll be woken up again once below random_write_wakeup_thresh,
2210-
* or when the calling thread is about to terminate.
2213+
* when the calling thread is about to terminate, or once
2214+
* CRNG_RESEED_INTERVAL has lapsed.
22112215
*/
2212-
wait_event_interruptible(random_write_wait,
2216+
wait_event_interruptible_timeout(random_write_wait,
22132217
!system_wq || kthread_should_stop() ||
2214-
POOL_ENTROPY_BITS() <= random_write_wakeup_bits);
2218+
POOL_ENTROPY_BITS() <= random_write_wakeup_bits,
2219+
CRNG_RESEED_INTERVAL);
22152220
mix_pool_bytes(buffer, count);
22162221
credit_entropy_bits(entropy);
22172222
}

include/crypto/internal/blake2s.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,54 @@ static inline void blake2s_set_lastblock(struct blake2s_state *state)
2424
state->f[0] = -1;
2525
}
2626

27-
typedef void (*blake2s_compress_t)(struct blake2s_state *state,
28-
const u8 *block, size_t nblocks, u32 inc);
29-
3027
/* Helper functions for BLAKE2s shared by the library and shash APIs */
3128

32-
static inline void __blake2s_update(struct blake2s_state *state,
33-
const u8 *in, size_t inlen,
34-
blake2s_compress_t compress)
29+
static __always_inline void
30+
__blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen,
31+
bool force_generic)
3532
{
3633
const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
3734

3835
if (unlikely(!inlen))
3936
return;
4037
if (inlen > fill) {
4138
memcpy(state->buf + state->buflen, in, fill);
42-
(*compress)(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
39+
if (force_generic)
40+
blake2s_compress_generic(state, state->buf, 1,
41+
BLAKE2S_BLOCK_SIZE);
42+
else
43+
blake2s_compress(state, state->buf, 1,
44+
BLAKE2S_BLOCK_SIZE);
4345
state->buflen = 0;
4446
in += fill;
4547
inlen -= fill;
4648
}
4749
if (inlen > BLAKE2S_BLOCK_SIZE) {
4850
const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
4951
/* Hash one less (full) block than strictly possible */
50-
(*compress)(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
52+
if (force_generic)
53+
blake2s_compress_generic(state, in, nblocks - 1,
54+
BLAKE2S_BLOCK_SIZE);
55+
else
56+
blake2s_compress(state, in, nblocks - 1,
57+
BLAKE2S_BLOCK_SIZE);
5158
in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
5259
inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
5360
}
5461
memcpy(state->buf + state->buflen, in, inlen);
5562
state->buflen += inlen;
5663
}
5764

58-
static inline void __blake2s_final(struct blake2s_state *state, u8 *out,
59-
blake2s_compress_t compress)
65+
static __always_inline void
66+
__blake2s_final(struct blake2s_state *state, u8 *out, bool force_generic)
6067
{
6168
blake2s_set_lastblock(state);
6269
memset(state->buf + state->buflen, 0,
6370
BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
64-
(*compress)(state, state->buf, 1, state->buflen);
71+
if (force_generic)
72+
blake2s_compress_generic(state, state->buf, 1, state->buflen);
73+
else
74+
blake2s_compress(state, state->buf, 1, state->buflen);
6575
cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
6676
memcpy(out, state->h, state->outlen);
6777
}
@@ -99,20 +109,20 @@ static inline int crypto_blake2s_init(struct shash_desc *desc)
99109

100110
static inline int crypto_blake2s_update(struct shash_desc *desc,
101111
const u8 *in, unsigned int inlen,
102-
blake2s_compress_t compress)
112+
bool force_generic)
103113
{
104114
struct blake2s_state *state = shash_desc_ctx(desc);
105115

106-
__blake2s_update(state, in, inlen, compress);
116+
__blake2s_update(state, in, inlen, force_generic);
107117
return 0;
108118
}
109119

110120
static inline int crypto_blake2s_final(struct shash_desc *desc, u8 *out,
111-
blake2s_compress_t compress)
121+
bool force_generic)
112122
{
113123
struct blake2s_state *state = shash_desc_ctx(desc);
114124

115-
__blake2s_final(state, out, compress);
125+
__blake2s_final(state, out, force_generic);
116126
return 0;
117127
}
118128

lib/crypto/blake2s.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen)
2020
{
21-
__blake2s_update(state, in, inlen, blake2s_compress);
21+
__blake2s_update(state, in, inlen, false);
2222
}
2323
EXPORT_SYMBOL(blake2s_update);
2424

2525
void blake2s_final(struct blake2s_state *state, u8 *out)
2626
{
2727
WARN_ON(IS_ENABLED(DEBUG) && !out);
28-
__blake2s_final(state, out, blake2s_compress);
28+
__blake2s_final(state, out, false);
2929
memzero_explicit(state, sizeof(*state));
3030
}
3131
EXPORT_SYMBOL(blake2s_final);

0 commit comments

Comments
 (0)