Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. From 936f4e3a97bea275d23cb7b2ad2cf50640675a7b Mon Sep 17 00:00:00 2001
  2. From: Neil Shepperd <nshepperd@gmail.com>
  3. Date: Tue, 1 Sep 2015 14:22:47 +1000
  4. Subject: [PATCH] Implement BSD-style /dev/[u]random behaviour
  5.  
  6. ---
  7. drivers/char/random.c | 32 +++++++++++---------------------
  8. 1 file changed, 11 insertions(+), 21 deletions(-)
  9.  
  10. diff --git a/drivers/char/random.c b/drivers/char/random.c
  11. index d0da5d8..cebed43 100644
  12. --- a/drivers/char/random.c
  13. +++ b/drivers/char/random.c
  14. @@ -281,6 +281,7 @@
  15. #define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))
  16. #define SEC_XFER_SIZE 512
  17. #define EXTRACT_SIZE 10
  18. +#define MINIMUM_ENTROPY_BITS 256
  19.  
  20. #define DEBUG_RANDOM_BOOT 0
  21.  
  22. @@ -468,6 +469,7 @@ static struct entropy_store blocking_pool = {
  23. static struct entropy_store nonblocking_pool = {
  24. .poolinfo = &poolinfo_table[1],
  25. .name = "nonblocking",
  26. + .limit = 1,
  27. .pull = &input_pool,
  28. .lock = __SPIN_LOCK_UNLOCKED(nonblocking_pool.lock),
  29. .pool = nonblocking_pool_data,
  30. @@ -1036,22 +1038,20 @@ static void push_to_pool(struct work_struct *work)
  31. static size_t account(struct entropy_store *r, size_t nbytes, int min,
  32. int reserved)
  33. {
  34. - int entropy_count, orig;
  35. - size_t ibytes, nfrac;
  36. + int entropy_count;
  37. + size_t ibytes;
  38.  
  39. BUG_ON(r->entropy_count > r->poolinfo->poolfracbits);
  40.  
  41. /* Can we pull enough? */
  42. -retry:
  43. - entropy_count = orig = ACCESS_ONCE(r->entropy_count);
  44. + entropy_count = ACCESS_ONCE(r->entropy_count);
  45. ibytes = nbytes;
  46. - /* If limited, never pull more than available */
  47. + /* If limited, block until we have MINIMUM_ENTROPY_BITS bits in the pool */
  48. if (r->limit) {
  49. - int have_bytes = entropy_count >> (ENTROPY_SHIFT + 3);
  50. -
  51. - if ((have_bytes -= reserved) < 0)
  52. - have_bytes = 0;
  53. - ibytes = min_t(size_t, ibytes, have_bytes);
  54. + int have_bits = entropy_count >> ENTROPY_SHIFT;
  55. + if (have_bits < MINIMUM_ENTROPY_BITS) {
  56. + ibytes = 0;
  57. + }
  58. }
  59. if (ibytes < min)
  60. ibytes = 0;
  61. @@ -1060,18 +1060,8 @@ retry:
  62. pr_warn("random: negative entropy count: pool %s count %d\n",
  63. r->name, entropy_count);
  64. WARN_ON(1);
  65. - entropy_count = 0;
  66. }
  67. - nfrac = ibytes << (ENTROPY_SHIFT + 3);
  68. - if ((size_t) entropy_count > nfrac)
  69. - entropy_count -= nfrac;
  70. - else
  71. - entropy_count = 0;
  72. -
  73. - if (cmpxchg(&r->entropy_count, orig, entropy_count) != orig)
  74. - goto retry;
  75.  
  76. - trace_debit_entropy(r->name, 8 * ibytes);
  77. if (ibytes &&
  78. (r->entropy_count >> ENTROPY_SHIFT) < random_write_wakeup_bits) {
  79. wake_up_interruptible(&random_write_wait);
  80. @@ -1343,7 +1333,7 @@ void get_random_bytes_arch(void *buf, int nbytes)
  81.  
  82. if (!arch_get_random_long(&v))
  83. break;
  84. -
  85. +
  86. memcpy(p, &v, chunk);
  87. p += chunk;
  88. nbytes -= chunk;
  89. --
  90. 2.5.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement