1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233#include <linux/utsname.h>
234#include <linux/module.h>
235#include <linux/kernel.h>
236#include <linux/major.h>
237#include <linux/string.h>
238#include <linux/fcntl.h>
239#include <linux/slab.h>
240#include <linux/random.h>
241#include <linux/poll.h>
242#include <linux/init.h>
243#include <linux/fs.h>
244#include <linux/genhd.h>
245#include <linux/interrupt.h>
246#include <linux/mm.h>
247#include <linux/spinlock.h>
248#include <linux/percpu.h>
249#include <linux/cryptohash.h>
250#include <linux/fips.h>
251
252#ifdef CONFIG_GENERIC_HARDIRQS
253# include <linux/irq.h>
254#endif
255
256#include <asm/processor.h>
257#include <asm/uaccess.h>
258#include <asm/irq.h>
259#include <asm/io.h>
260
261
262
263
264#define INPUT_POOL_WORDS 128
265#define OUTPUT_POOL_WORDS 32
266#define SEC_XFER_SIZE 512
267#define EXTRACT_SIZE 10
268
269
270
271
272
273static int random_read_wakeup_thresh = 64;
274
275
276
277
278
279
280static int random_write_wakeup_thresh = 128;
281
282
283
284
285
286
287static int trickle_thresh __read_mostly = INPUT_POOL_WORDS * 28;
288
289static DEFINE_PER_CPU(int, trickle_count);
290
291
292
293
294
295
296
297
298
299static struct poolinfo {
300 int poolwords;
301 int tap1, tap2, tap3, tap4, tap5;
302} poolinfo_table[] = {
303
304 { 128, 103, 76, 51, 25, 1 },
305
306 { 32, 26, 20, 14, 7, 1 },
307#if 0
308
309 { 2048, 1638, 1231, 819, 411, 1 },
310
311
312 { 1024, 817, 615, 412, 204, 1 },
313
314
315 { 1024, 819, 616, 410, 207, 2 },
316
317
318 { 512, 411, 308, 208, 104, 1 },
319
320
321 { 512, 409, 307, 206, 102, 2 },
322
323 { 512, 409, 309, 205, 103, 2 },
324
325
326 { 256, 205, 155, 101, 52, 1 },
327
328
329 { 128, 103, 78, 51, 27, 2 },
330
331
332 { 64, 52, 39, 26, 14, 1 },
333#endif
334};
335
336#define POOLBITS poolwords*32
337#define POOLBYTES poolwords*4
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
386static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
387static struct fasync_struct *fasync;
388
389#if 0
390static int debug;
391module_param(debug, bool, 0644);
392#define DEBUG_ENT(fmt, arg...) do { \
393 if (debug) \
394 printk(KERN_DEBUG "random %04d %04d %04d: " \
395 fmt,\
396 input_pool.entropy_count,\
397 blocking_pool.entropy_count,\
398 nonblocking_pool.entropy_count,\
399 ## arg); } while (0)
400#else
401#define DEBUG_ENT(fmt, arg...) do {} while (0)
402#endif
403
404
405
406
407
408
409
410
411struct entropy_store;
412struct entropy_store {
413
414 struct poolinfo *poolinfo;
415 __u32 *pool;
416 const char *name;
417 struct entropy_store *pull;
418 int limit;
419
420
421 spinlock_t lock;
422 unsigned add_ptr;
423 int entropy_count;
424 int input_rotate;
425 __u8 last_data[EXTRACT_SIZE];
426};
427
428static __u32 input_pool_data[INPUT_POOL_WORDS];
429static __u32 blocking_pool_data[OUTPUT_POOL_WORDS];
430static __u32 nonblocking_pool_data[OUTPUT_POOL_WORDS];
431
432static struct entropy_store input_pool = {
433 .poolinfo = &poolinfo_table[0],
434 .name = "input",
435 .limit = 1,
436 .lock = __SPIN_LOCK_UNLOCKED(&input_pool.lock),
437 .pool = input_pool_data
438};
439
440static struct entropy_store blocking_pool = {
441 .poolinfo = &poolinfo_table[1],
442 .name = "blocking",
443 .limit = 1,
444 .pull = &input_pool,
445 .lock = __SPIN_LOCK_UNLOCKED(&blocking_pool.lock),
446 .pool = blocking_pool_data
447};
448
449static struct entropy_store nonblocking_pool = {
450 .poolinfo = &poolinfo_table[1],
451 .name = "nonblocking",
452 .pull = &input_pool,
453 .lock = __SPIN_LOCK_UNLOCKED(&nonblocking_pool.lock),
454 .pool = nonblocking_pool_data
455};
456
457
458
459
460
461
462
463
464
465
466
467static void mix_pool_bytes_extract(struct entropy_store *r, const void *in,
468 int nbytes, __u8 out[64])
469{
470 static __u32 const twist_table[8] = {
471 0x00000000, 0x3b6e20c8, 0x76dc4190, 0x4db26158,
472 0xedb88320, 0xd6d6a3e8, 0x9b64c2b0, 0xa00ae278 };
473 unsigned long i, j, tap1, tap2, tap3, tap4, tap5;
474 int input_rotate;
475 int wordmask = r->poolinfo->poolwords - 1;
476 const char *bytes = in;
477 __u32 w;
478 unsigned long flags;
479
480
481 tap1 = r->poolinfo->tap1;
482 tap2 = r->poolinfo->tap2;
483 tap3 = r->poolinfo->tap3;
484 tap4 = r->poolinfo->tap4;
485 tap5 = r->poolinfo->tap5;
486
487 spin_lock_irqsave(&r->lock, flags);
488 input_rotate = r->input_rotate;
489 i = r->add_ptr;
490
491
492 while (nbytes--) {
493 w = rol32(*bytes++, input_rotate & 31);
494 i = (i - 1) & wordmask;
495
496
497 w ^= r->pool[i];
498 w ^= r->pool[(i + tap1) & wordmask];
499 w ^= r->pool[(i + tap2) & wordmask];
500 w ^= r->pool[(i + tap3) & wordmask];
501 w ^= r->pool[(i + tap4) & wordmask];
502 w ^= r->pool[(i + tap5) & wordmask];
503
504
505 r->pool[i] = (w >> 3) ^ twist_table[w & 7];
506
507
508
509
510
511
512
513 input_rotate += i ? 7 : 14;
514 }
515
516 r->input_rotate = input_rotate;
517 r->add_ptr = i;
518
519 if (out)
520 for (j = 0; j < 16; j++)
521 ((__u32 *)out)[j] = r->pool[(i - j) & wordmask];
522
523 spin_unlock_irqrestore(&r->lock, flags);
524}
525
526static void mix_pool_bytes(struct entropy_store *r, const void *in, int bytes)
527{
528 mix_pool_bytes_extract(r, in, bytes, NULL);
529}
530
531
532
533
534static void credit_entropy_bits(struct entropy_store *r, int nbits)
535{
536 unsigned long flags;
537 int entropy_count;
538
539 if (!nbits)
540 return;
541
542 spin_lock_irqsave(&r->lock, flags);
543
544 DEBUG_ENT("added %d entropy credits to %s\n", nbits, r->name);
545 entropy_count = r->entropy_count;
546 entropy_count += nbits;
547 if (entropy_count < 0) {
548 DEBUG_ENT("negative entropy/overflow\n");
549 entropy_count = 0;
550 } else if (entropy_count > r->poolinfo->POOLBITS)
551 entropy_count = r->poolinfo->POOLBITS;
552 r->entropy_count = entropy_count;
553
554
555 if (r == &input_pool && entropy_count >= random_read_wakeup_thresh) {
556 wake_up_interruptible(&random_read_wait);
557 kill_fasync(&fasync, SIGIO, POLL_IN);
558 }
559 spin_unlock_irqrestore(&r->lock, flags);
560}
561
562
563
564
565
566
567
568
569struct timer_rand_state {
570 cycles_t last_time;
571 long last_delta, last_delta2;
572 unsigned dont_count_entropy:1;
573};
574
575#ifndef CONFIG_GENERIC_HARDIRQS
576
577static struct timer_rand_state *irq_timer_state[NR_IRQS];
578
579static struct timer_rand_state *get_timer_rand_state(unsigned int irq)
580{
581 return irq_timer_state[irq];
582}
583
584static void set_timer_rand_state(unsigned int irq,
585 struct timer_rand_state *state)
586{
587 irq_timer_state[irq] = state;
588}
589
590#else
591
592static struct timer_rand_state *get_timer_rand_state(unsigned int irq)
593{
594 struct irq_desc *desc;
595
596 desc = irq_to_desc(irq);
597
598 return desc->timer_rand_state;
599}
600
601static void set_timer_rand_state(unsigned int irq,
602 struct timer_rand_state *state)
603{
604 struct irq_desc *desc;
605
606 desc = irq_to_desc(irq);
607
608 desc->timer_rand_state = state;
609}
610#endif
611
612static struct timer_rand_state input_timer_state;
613
614
615
616
617
618
619
620
621
622
623
624static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
625{
626 struct {
627 cycles_t cycles;
628 long jiffies;
629 unsigned num;
630 } sample;
631 long delta, delta2, delta3;
632
633 preempt_disable();
634
635 if (input_pool.entropy_count > trickle_thresh &&
636 ((__this_cpu_inc_return(trickle_count) - 1) & 0xfff))
637 goto out;
638
639 sample.jiffies = jiffies;
640 sample.cycles = get_cycles();
641 sample.num = num;
642 mix_pool_bytes(&input_pool, &sample, sizeof(sample));
643
644
645
646
647
648
649
650 if (!state->dont_count_entropy) {
651 delta = sample.jiffies - state->last_time;
652 state->last_time = sample.jiffies;
653
654 delta2 = delta - state->last_delta;
655 state->last_delta = delta;
656
657 delta3 = delta2 - state->last_delta2;
658 state->last_delta2 = delta2;
659
660 if (delta < 0)
661 delta = -delta;
662 if (delta2 < 0)
663 delta2 = -delta2;
664 if (delta3 < 0)
665 delta3 = -delta3;
666 if (delta > delta2)
667 delta = delta2;
668 if (delta > delta3)
669 delta = delta3;
670
671
672
673
674
675
676 credit_entropy_bits(&input_pool,
677 min_t(int, fls(delta>>1), 11));
678 }
679out:
680 preempt_enable();
681}
682
683void add_input_randomness(unsigned int type, unsigned int code,
684 unsigned int value)
685{
686 static unsigned char last_value;
687
688
689 if (value == last_value)
690 return;
691
692 DEBUG_ENT("input event\n");
693 last_value = value;
694 add_timer_randomness(&input_timer_state,
695 (type << 4) ^ code ^ (code >> 4) ^ value);
696}
697EXPORT_SYMBOL_GPL(add_input_randomness);
698
699void add_interrupt_randomness(int irq)
700{
701 struct timer_rand_state *state;
702
703 state = get_timer_rand_state(irq);
704
705 if (state == NULL)
706 return;
707
708 DEBUG_ENT("irq event %d\n", irq);
709 add_timer_randomness(state, 0x100 + irq);
710}
711
712#ifdef CONFIG_BLOCK
713void add_disk_randomness(struct gendisk *disk)
714{
715 if (!disk || !disk->random)
716 return;
717
718 DEBUG_ENT("disk event %d:%d\n",
719 MAJOR(disk_devt(disk)), MINOR(disk_devt(disk)));
720
721 add_timer_randomness(disk->random, 0x100 + disk_devt(disk));
722}
723#endif
724
725
726
727
728
729
730
731static ssize_t extract_entropy(struct entropy_store *r, void *buf,
732 size_t nbytes, int min, int rsvd);
733
734
735
736
737
738
739static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
740{
741 __u32 tmp[OUTPUT_POOL_WORDS];
742
743 if (r->pull && r->entropy_count < nbytes * 8 &&
744 r->entropy_count < r->poolinfo->POOLBITS) {
745
746 int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4;
747 int bytes = nbytes;
748
749
750 bytes = max_t(int, bytes, random_read_wakeup_thresh / 8);
751
752 bytes = min_t(int, bytes, sizeof(tmp));
753
754 DEBUG_ENT("going to reseed %s with %d bits "
755 "(%d of %d requested)\n",
756 r->name, bytes * 8, nbytes * 8, r->entropy_count);
757
758 bytes = extract_entropy(r->pull, tmp, bytes,
759 random_read_wakeup_thresh / 8, rsvd);
760 mix_pool_bytes(r, tmp, bytes);
761 credit_entropy_bits(r, bytes*8);
762 }
763}
764
765
766
767
768
769
770
771
772
773
774
775
776
777static size_t account(struct entropy_store *r, size_t nbytes, int min,
778 int reserved)
779{
780 unsigned long flags;
781
782
783 spin_lock_irqsave(&r->lock, flags);
784
785 BUG_ON(r->entropy_count > r->poolinfo->POOLBITS);
786 DEBUG_ENT("trying to extract %d bits from %s\n",
787 nbytes * 8, r->name);
788
789
790 if (r->entropy_count / 8 < min + reserved) {
791 nbytes = 0;
792 } else {
793
794 if (r->limit && nbytes + reserved >= r->entropy_count / 8)
795 nbytes = r->entropy_count/8 - reserved;
796
797 if (r->entropy_count / 8 >= nbytes + reserved)
798 r->entropy_count -= nbytes*8;
799 else
800 r->entropy_count = reserved;
801
802 if (r->entropy_count < random_write_wakeup_thresh) {
803 wake_up_interruptible(&random_write_wait);
804 kill_fasync(&fasync, SIGIO, POLL_OUT);
805 }
806 }
807
808 DEBUG_ENT("debiting %d entropy credits from %s%s\n",
809 nbytes * 8, r->name, r->limit ? "" : " (unlimited)");
810
811 spin_unlock_irqrestore(&r->lock, flags);
812
813 return nbytes;
814}
815
816static void extract_buf(struct entropy_store *r, __u8 *out)
817{
818 int i;
819 __u32 hash[5], workspace[SHA_WORKSPACE_WORDS];
820 __u8 extract[64];
821
822
823 sha_init(hash);
824 for (i = 0; i < r->poolinfo->poolwords; i += 16)
825 sha_transform(hash, (__u8 *)(r->pool + i), workspace);
826
827
828
829
830
831
832
833
834
835
836 mix_pool_bytes_extract(r, hash, sizeof(hash), extract);
837
838
839
840
841
842 sha_transform(hash, extract, workspace);
843 memset(extract, 0, sizeof(extract));
844 memset(workspace, 0, sizeof(workspace));
845
846
847
848
849
850
851 hash[0] ^= hash[3];
852 hash[1] ^= hash[4];
853 hash[2] ^= rol32(hash[2], 16);
854 memcpy(out, hash, EXTRACT_SIZE);
855 memset(hash, 0, sizeof(hash));
856}
857
858static ssize_t extract_entropy(struct entropy_store *r, void *buf,
859 size_t nbytes, int min, int reserved)
860{
861 ssize_t ret = 0, i;
862 __u8 tmp[EXTRACT_SIZE];
863 unsigned long flags;
864
865 xfer_secondary_pool(r, nbytes);
866 nbytes = account(r, nbytes, min, reserved);
867
868 while (nbytes) {
869 extract_buf(r, tmp);
870
871 if (fips_enabled) {
872 spin_lock_irqsave(&r->lock, flags);
873 if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
874 panic("Hardware RNG duplicated output!\n");
875 memcpy(r->last_data, tmp, EXTRACT_SIZE);
876 spin_unlock_irqrestore(&r->lock, flags);
877 }
878 i = min_t(int, nbytes, EXTRACT_SIZE);
879 memcpy(buf, tmp, i);
880 nbytes -= i;
881 buf += i;
882 ret += i;
883 }
884
885
886 memset(tmp, 0, sizeof(tmp));
887
888 return ret;
889}
890
891static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
892 size_t nbytes)
893{
894 ssize_t ret = 0, i;
895 __u8 tmp[EXTRACT_SIZE];
896
897 xfer_secondary_pool(r, nbytes);
898 nbytes = account(r, nbytes, 0, 0);
899
900 while (nbytes) {
901 if (need_resched()) {
902 if (signal_pending(current)) {
903 if (ret == 0)
904 ret = -ERESTARTSYS;
905 break;
906 }
907 schedule();
908 }
909
910 extract_buf(r, tmp);
911 i = min_t(int, nbytes, EXTRACT_SIZE);
912 if (copy_to_user(buf, tmp, i)) {
913 ret = -EFAULT;
914 break;
915 }
916
917 nbytes -= i;
918 buf += i;
919 ret += i;
920 }
921
922
923 memset(tmp, 0, sizeof(tmp));
924
925 return ret;
926}
927
928
929
930
931
932
933void get_random_bytes(void *buf, int nbytes)
934{
935 char *p = buf;
936
937 while (nbytes) {
938 unsigned long v;
939 int chunk = min(nbytes, (int)sizeof(unsigned long));
940
941 if (!arch_get_random_long(&v))
942 break;
943
944 memcpy(p, &v, chunk);
945 p += chunk;
946 nbytes -= chunk;
947 }
948
949 extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
950}
951EXPORT_SYMBOL(get_random_bytes);
952
953
954
955
956
957
958
959
960
961
962static void init_std_data(struct entropy_store *r)
963{
964 ktime_t now;
965 unsigned long flags;
966
967 spin_lock_irqsave(&r->lock, flags);
968 r->entropy_count = 0;
969 spin_unlock_irqrestore(&r->lock, flags);
970
971 now = ktime_get_real();
972 mix_pool_bytes(r, &now, sizeof(now));
973 mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
974}
975
976static int rand_initialize(void)
977{
978 init_std_data(&input_pool);
979 init_std_data(&blocking_pool);
980 init_std_data(&nonblocking_pool);
981 return 0;
982}
983module_init(rand_initialize);
984
985void rand_initialize_irq(int irq)
986{
987 struct timer_rand_state *state;
988
989 state = get_timer_rand_state(irq);
990
991 if (state)
992 return;
993
994
995
996
997
998 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
999 if (state)
1000 set_timer_rand_state(irq, state);
1001}
1002
1003#ifdef CONFIG_BLOCK
1004void rand_initialize_disk(struct gendisk *disk)
1005{
1006 struct timer_rand_state *state;
1007
1008
1009
1010
1011
1012 state = kzalloc(sizeof(struct timer_rand_state), GFP_KERNEL);
1013 if (state)
1014 disk->random = state;
1015}
1016#endif
1017
1018static ssize_t
1019random_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1020{
1021 ssize_t n, retval = 0, count = 0;
1022
1023 if (nbytes == 0)
1024 return 0;
1025
1026 while (nbytes > 0) {
1027 n = nbytes;
1028 if (n > SEC_XFER_SIZE)
1029 n = SEC_XFER_SIZE;
1030
1031 DEBUG_ENT("reading %d bits\n", n*8);
1032
1033 n = extract_entropy_user(&blocking_pool, buf, n);
1034
1035 DEBUG_ENT("read got %d bits (%d still needed)\n",
1036 n*8, (nbytes-n)*8);
1037
1038 if (n == 0) {
1039 if (file->f_flags & O_NONBLOCK) {
1040 retval = -EAGAIN;
1041 break;
1042 }
1043
1044 DEBUG_ENT("sleeping?\n");
1045
1046 wait_event_interruptible(random_read_wait,
1047 input_pool.entropy_count >=
1048 random_read_wakeup_thresh);
1049
1050 DEBUG_ENT("awake\n");
1051
1052 if (signal_pending(current)) {
1053 retval = -ERESTARTSYS;
1054 break;
1055 }
1056
1057 continue;
1058 }
1059
1060 if (n < 0) {
1061 retval = n;
1062 break;
1063 }
1064 count += n;
1065 buf += n;
1066 nbytes -= n;
1067 break;
1068
1069 }
1070
1071 return (count ? count : retval);
1072}
1073
1074static ssize_t
1075urandom_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
1076{
1077 return extract_entropy_user(&nonblocking_pool, buf, nbytes);
1078}
1079
1080static unsigned int
1081random_poll(struct file *file, poll_table * wait)
1082{
1083 unsigned int mask;
1084
1085 poll_wait(file, &random_read_wait, wait);
1086 poll_wait(file, &random_write_wait, wait);
1087 mask = 0;
1088 if (input_pool.entropy_count >= random_read_wakeup_thresh)
1089 mask |= POLLIN | POLLRDNORM;
1090 if (input_pool.entropy_count < random_write_wakeup_thresh)
1091 mask |= POLLOUT | POLLWRNORM;
1092 return mask;
1093}
1094
1095static int
1096write_pool(struct entropy_store *r, const char __user *buffer, size_t count)
1097{
1098 size_t bytes;
1099 __u32 buf[16];
1100 const char __user *p = buffer;
1101
1102 while (count > 0) {
1103 bytes = min(count, sizeof(buf));
1104 if (copy_from_user(&buf, p, bytes))
1105 return -EFAULT;
1106
1107 count -= bytes;
1108 p += bytes;
1109
1110 mix_pool_bytes(r, buf, bytes);
1111 cond_resched();
1112 }
1113
1114 return 0;
1115}
1116
1117static ssize_t random_write(struct file *file, const char __user *buffer,
1118 size_t count, loff_t *ppos)
1119{
1120 size_t ret;
1121
1122 ret = write_pool(&blocking_pool, buffer, count);
1123 if (ret)
1124 return ret;
1125 ret = write_pool(&nonblocking_pool, buffer, count);
1126 if (ret)
1127 return ret;
1128
1129 return (ssize_t)count;
1130}
1131
1132static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
1133{
1134 int size, ent_count;
1135 int __user *p = (int __user *)arg;
1136 int retval;
1137
1138 switch (cmd) {
1139 case RNDGETENTCNT:
1140
1141 if (put_user(input_pool.entropy_count, p))
1142 return -EFAULT;
1143 return 0;
1144 case RNDADDTOENTCNT:
1145 if (!capable(CAP_SYS_ADMIN))
1146 return -EPERM;
1147 if (get_user(ent_count, p))
1148 return -EFAULT;
1149 credit_entropy_bits(&input_pool, ent_count);
1150 return 0;
1151 case RNDADDENTROPY:
1152 if (!capable(CAP_SYS_ADMIN))
1153 return -EPERM;
1154 if (get_user(ent_count, p++))
1155 return -EFAULT;
1156 if (ent_count < 0)
1157 return -EINVAL;
1158 if (get_user(size, p++))
1159 return -EFAULT;
1160 retval = write_pool(&input_pool, (const char __user *)p,
1161 size);
1162 if (retval < 0)
1163 return retval;
1164 credit_entropy_bits(&input_pool, ent_count);
1165 return 0;
1166 case RNDZAPENTCNT:
1167 case RNDCLEARPOOL:
1168
1169 if (!capable(CAP_SYS_ADMIN))
1170 return -EPERM;
1171 rand_initialize();
1172 return 0;
1173 default:
1174 return -EINVAL;
1175 }
1176}
1177
1178static int random_fasync(int fd, struct file *filp, int on)
1179{
1180 return fasync_helper(fd, filp, on, &fasync);
1181}
1182
1183const struct file_operations random_fops = {
1184 .read = random_read,
1185 .write = random_write,
1186 .poll = random_poll,
1187 .unlocked_ioctl = random_ioctl,
1188 .fasync = random_fasync,
1189 .llseek = noop_llseek,
1190};
1191
1192const struct file_operations urandom_fops = {
1193 .read = urandom_read,
1194 .write = random_write,
1195 .unlocked_ioctl = random_ioctl,
1196 .fasync = random_fasync,
1197 .llseek = noop_llseek,
1198};
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210void generate_random_uuid(unsigned char uuid_out[16])
1211{
1212 get_random_bytes(uuid_out, 16);
1213
1214 uuid_out[6] = (uuid_out[6] & 0x0F) | 0x40;
1215
1216 uuid_out[8] = (uuid_out[8] & 0x3F) | 0x80;
1217}
1218EXPORT_SYMBOL(generate_random_uuid);
1219
1220
1221
1222
1223
1224
1225
1226#ifdef CONFIG_SYSCTL
1227
1228#include <linux/sysctl.h>
1229
1230static int min_read_thresh = 8, min_write_thresh;
1231static int max_read_thresh = INPUT_POOL_WORDS * 32;
1232static int max_write_thresh = INPUT_POOL_WORDS * 32;
1233static char sysctl_bootid[16];
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244static int proc_do_uuid(ctl_table *table, int write,
1245 void __user *buffer, size_t *lenp, loff_t *ppos)
1246{
1247 ctl_table fake_table;
1248 unsigned char buf[64], tmp_uuid[16], *uuid;
1249
1250 uuid = table->data;
1251 if (!uuid) {
1252 uuid = tmp_uuid;
1253 uuid[8] = 0;
1254 }
1255 if (uuid[8] == 0)
1256 generate_random_uuid(uuid);
1257
1258 sprintf(buf, "%pU", uuid);
1259
1260 fake_table.data = buf;
1261 fake_table.maxlen = sizeof(buf);
1262
1263 return proc_dostring(&fake_table, write, buffer, lenp, ppos);
1264}
1265
1266static int sysctl_poolsize = INPUT_POOL_WORDS * 32;
1267ctl_table random_table[] = {
1268 {
1269 .procname = "poolsize",
1270 .data = &sysctl_poolsize,
1271 .maxlen = sizeof(int),
1272 .mode = 0444,
1273 .proc_handler = proc_dointvec,
1274 },
1275 {
1276 .procname = "entropy_avail",
1277 .maxlen = sizeof(int),
1278 .mode = 0444,
1279 .proc_handler = proc_dointvec,
1280 .data = &input_pool.entropy_count,
1281 },
1282 {
1283 .procname = "read_wakeup_threshold",
1284 .data = &random_read_wakeup_thresh,
1285 .maxlen = sizeof(int),
1286 .mode = 0644,
1287 .proc_handler = proc_dointvec_minmax,
1288 .extra1 = &min_read_thresh,
1289 .extra2 = &max_read_thresh,
1290 },
1291 {
1292 .procname = "write_wakeup_threshold",
1293 .data = &random_write_wakeup_thresh,
1294 .maxlen = sizeof(int),
1295 .mode = 0644,
1296 .proc_handler = proc_dointvec_minmax,
1297 .extra1 = &min_write_thresh,
1298 .extra2 = &max_write_thresh,
1299 },
1300 {
1301 .procname = "boot_id",
1302 .data = &sysctl_bootid,
1303 .maxlen = 16,
1304 .mode = 0444,
1305 .proc_handler = proc_do_uuid,
1306 },
1307 {
1308 .procname = "uuid",
1309 .maxlen = 16,
1310 .mode = 0444,
1311 .proc_handler = proc_do_uuid,
1312 },
1313 { }
1314};
1315#endif
1316
1317static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
1318
1319static int __init random_int_secret_init(void)
1320{
1321 get_random_bytes(random_int_secret, sizeof(random_int_secret));
1322 return 0;
1323}
1324late_initcall(random_int_secret_init);
1325
1326
1327
1328
1329
1330
1331
1332DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);
1333unsigned int get_random_int(void)
1334{
1335 __u32 *hash;
1336 unsigned int ret;
1337
1338 if (arch_get_random_int(&ret))
1339 return ret;
1340
1341 hash = get_cpu_var(get_random_int_hash);
1342
1343 hash[0] += current->pid + jiffies + get_cycles();
1344 md5_transform(hash, random_int_secret);
1345 ret = hash[0];
1346 put_cpu_var(get_random_int_hash);
1347
1348 return ret;
1349}
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360unsigned long
1361randomize_range(unsigned long start, unsigned long end, unsigned long len)
1362{
1363 unsigned long range = end - len - start;
1364
1365 if (end <= start + len)
1366 return 0;
1367 return PAGE_ALIGN(get_random_int() % range + start);
1368}
1369