summaryrefslogtreecommitdiff
path: root/trans/random.c
blob: b68354e1e2adcd8b631e737f691266ab2ea199f6 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/* random.c - A single-file translator providing random data
   Copyright (C) 1998, 1999, 2001, 2017 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <argp.h>
#include <argz.h>
#include <assert-backtrace.h>
#include <error.h>
#include <fcntl.h>
#include <gcrypt.h>
#include <hurd/paths.h>
#include <hurd/startup.h>
#include <hurd/trivfs.h>
#include <mach/gnumach.h>
#include <mach/vm_cache_statistics.h>
#include <mach/vm_param.h>
#include <mach/vm_statistics.h>
#include <mach_debug/mach_debug_types.h>
#include <maptime.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <version.h>

#include "mach_debug_U.h"



char *trivfs_server_name = "random";

/* Referred by glibc to avoid using /dev/random for malloc() initialization */
char *__trivfs_server_name = "random";

/* Entropy pool.  We use one of the SHAKE algorithms from the Keccak
   family.  Being a sponge construction, it allows the extraction of
   arbitrary amounts of pseudorandom data.  */
static gcry_md_hd_t pool;
enum gcry_md_algos hash_algo = GCRY_MD_SHAKE128;

/* Protected by this lock.  */
static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;

/* A map of the Mach time device.  Used for quick stirring.  */
volatile struct mapped_time_value *mtime;

static void
pool_initialize (void)
{
  error_t err;
  gcry_error_t cerr;

  if (! gcry_check_version ("1.8.0"))
    error (1, 0, "libgcrypt version mismatch\n");

  cerr = gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  if (cerr)
    error (1, 0, "Finalizing gcrypt failed: %s",
	   gcry_strerror (cerr));

  cerr = gcry_md_open (&pool, hash_algo, GCRY_MD_FLAG_SECURE);
  if (cerr)
    error (1, 0, "Initializing hash failed: %s",
	   gcry_strerror (cerr));

  err = maptime_map (1, NULL, &mtime);
  if (err)
    err = maptime_map (0, NULL, &mtime);
  if (err)
    error (1, err, "Failed to map time device");
}

/* Mix data into the pool.  */
static void
pool_add_entropy (const void *buffer, size_t length)
{
  pthread_mutex_lock (&pool_lock);
  gcry_md_write (pool, buffer, length);
  pthread_mutex_unlock (&pool_lock);
}

/* Extract data from the pool.  */
static error_t
pool_randomize (void *buffer, size_t length)
{
  gcry_error_t cerr;
  pthread_mutex_lock (&pool_lock);

  /* Quickly stir the the time device into the pool.  Do not even
     bother with synchronization.  */
  gcry_md_write (pool, (void *) mtime, sizeof *mtime);

  cerr = gcry_md_extract (pool, hash_algo, buffer, length);
  pthread_mutex_unlock (&pool_lock);
  return cerr ? EIO : 0;
}



/* Name of file to use as seed.  */
static char *seed_file;

/* Size of the seed file.  */
size_t seed_size = 600;

static error_t
update_random_seed_file (void)
{
  error_t err;
  int fd;
  void *map;

  if (seed_file == NULL)
    return 0;

  fd = open (seed_file, O_RDWR|O_CREAT, 0600);
  if (fd < 0)
    return errno;

  if (ftruncate (fd, seed_size))
    {
      err = errno;
      goto out;
    }

  map = mmap (NULL, seed_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    {
      err = errno;
      goto out;
    }

  err = pool_randomize (map, seed_size);
  munmap (map, seed_size);

 out:
  close (fd);
  return err;
}

static error_t
read_random_seed_file (void)
{
  error_t err;
  int fd;
  struct stat s;
  void *map;

  if (seed_file == NULL)
    return 0;

  fd = open (seed_file, O_RDWR);
  if (fd < 0)
    return errno;

  if (fstat (fd, &s))
    {
      err = errno;
      goto out;
    }

  /* XXX should check file permissions.  */

  map = mmap (NULL, s.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    {
      err = errno;
      goto out;
    }

  pool_add_entropy (map, s.st_size);
  /* Immediately update it, to minimize the chance that the same state
     is read twice.  */
  pool_randomize (map, s.st_size);
  munmap (map, s.st_size);

 out:
  close (fd);
  return err;
}



static void
gather_slab_info (void)
{
  error_t err;
  cache_info_array_t cache_info;
  mach_msg_type_number_t cache_info_count;

  cache_info = NULL;
  cache_info_count = 0;

  err = host_slab_info (mach_host_self(), &cache_info, &cache_info_count);
  if (err)
    return;

  pool_add_entropy (cache_info, cache_info_count * sizeof *cache_info);

  vm_deallocate (mach_task_self (),
		 (vm_address_t) cache_info,
		 cache_info_count * sizeof *cache_info);
}

static void
gather_vm_statistics (void)
{
  error_t err;
  struct vm_statistics vmstats;

  err = vm_statistics (mach_task_self (), &vmstats);
  if (err)
    return;

  pool_add_entropy (&vmstats, sizeof vmstats);
}

static void
gather_vm_cache_statistics (void)
{
  error_t err;
  struct vm_cache_statistics cache_stats;

  err = vm_cache_statistics (mach_task_self (), &cache_stats);
  if (err)
    return;

  pool_add_entropy (&cache_stats, sizeof cache_stats);
}

static void *
gather_thread (void *args)
{
  while (1)
    {
      gather_slab_info ();
      gather_vm_statistics ();
      gather_vm_cache_statistics ();
      usleep (
        (useconds_t) (1000000. * (1.
                                  + (float) random () / (float) RAND_MAX)));
    }

  assert_backtrace (! "reached");
}

error_t
start_gather_thread (void)
{
  error_t err;
  pthread_t thread;

  err = pthread_create (&thread, NULL, gather_thread, NULL);
  if (err)
    return err;

  err = pthread_detach (thread);
  return err;
}



const char *argp_program_version = STANDARD_HURD_VERSION (random);

/* Our control port.  */
struct trivfs_control *fsys;

/* Trivfs hooks. */
int trivfs_fstype = FSTYPE_MISC;
int trivfs_fsid = 0;

int trivfs_allow_open = O_READ | O_WRITE;

int trivfs_support_read = 1;
int trivfs_support_write = 1;
int trivfs_support_exec = 0;

void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
  /* Mark the node as a read-only plain file. */
  st->st_mode &= ~((unsigned) S_IFMT);
  st->st_mode |= (S_IFCHR);
  st->st_size = 0;
}

error_t
trivfs_goaway (struct trivfs_control *cntl, int flags)
{
  error_t err;
  err = update_random_seed_file ();
  if (err)
    error (0, err, "Warning: Failed to save random seed to %s", seed_file);
  exit (0);
}

/* Read data from an IO object.  If offset is -1, read from the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount desired to be read is in AMOUNT.  */
kern_return_t
trivfs_S_io_read (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  data_t *data, mach_msg_type_number_t *data_len,
		  off_t offs, vm_size_t amount)
{
  error_t err;
  void *buf = NULL;
  size_t length = 0;

  if (! cred)
    return EOPNOTSUPP;
  else if (! (cred->po->openmodes & O_READ))
    return EBADF;

  if (amount > 0)
    {
      /* Possibly allocate a new buffer. */
      if (*data_len < amount)
	{
	  *data = mmap (0, amount, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
	  if (*data == MAP_FAILED)
	    {
              err = errno;
              goto errout;
	    }

	  /* Keep track of our map in case of errors.  */
	  buf = *data, length = amount;

	  /* Update DATA_LEN to reflect the new buffers size.  */
	  *data_len = amount;
	}

      err = pool_randomize (*data, amount);
      if (err)
        goto errout;

    }

  *data_len = amount;
  trivfs_set_atime (fsys);
  return 0;

 errout:
  if (buf)
    munmap (buf, length);
  return err;
}

/* Write data to an IO object.  If offset is -1, write at the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount successfully written is returned in amount.  A
   given user should not have more than one outstanding io_write on an
   object at a time; servers implement congestion control by delaying
   responses to io_write.  Servers may drop data (returning ENOBUFS)
   if they receive more than one write when not prepared for it.  */
kern_return_t
trivfs_S_io_write (struct trivfs_protid *cred,
                   mach_port_t reply,
                   mach_msg_type_name_t replytype,
                   const_data_t data,
                   mach_msg_type_number_t datalen,
                   off_t offset,
                   vm_size_t *amount)
{
  /* Deny access if they have bad credentials. */
  if (! cred)
    return EOPNOTSUPP;
  else if (! (cred->po->openmodes & O_WRITE))
    return EBADF;

  pool_add_entropy (data, datalen);
  *amount = datalen;
  trivfs_set_mtime (fsys);
  return 0;
}

/* Tell how much data can be read from the object without blocking for
   a "long time" (this should be the same meaning of "long time" used
   by the nonblocking flag.  */
kern_return_t
trivfs_S_io_readable (struct trivfs_protid *cred,
                      mach_port_t reply, mach_msg_type_name_t replytype,
                      mach_msg_type_number_t *amount)
{
  /* Deny access if they have bad credentials. */
  if (! cred)
    return EOPNOTSUPP;
  else if (! (cred->po->openmodes & O_READ))
    return EBADF;

  /* We allow an infinite amount of data to be extracted.  We need to
     return something here, so just go with the page size.  */
  *amount = PAGE_SIZE;
  return 0;
}

/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
   Block until one of the indicated types of i/o can be done "quickly", and
   return the types that are then available.  ID_TAG is returned as passed; it
   is just for the convenience of the user in matching up reply messages with
   specific requests sent.  */
error_t
trivfs_S_io_select (struct trivfs_protid *cred,
                    mach_port_t reply,
                    mach_msg_type_name_t reply_type,
                    int *type)
{
  if (!cred)
    return EOPNOTSUPP;

  /* We only deal with SELECT_READ and SELECT_WRITE here.  */
  if (*type & ~(SELECT_READ | SELECT_WRITE))
    return EINVAL;

  /* We allow an infinite amount of data to be extracted and stored.
     Just return success.  */
  return 0;
}


/* Change current read/write offset */
kern_return_t
trivfs_S_io_seek (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  loff_t offs, int whence, loff_t *new_offs)
{
  if (! cred)
    return EOPNOTSUPP;

  /* Not seekable.  */
  return ESPIPE;
}

/* Change the size of the file.  If the size increases, new blocks are
   zero-filled.  After successful return, it is safe to reference mapped
   areas of the file up to NEW_SIZE.  */
kern_return_t
trivfs_S_file_set_size (struct trivfs_protid *cred,
                        mach_port_t reply, mach_msg_type_name_t reply_type,
                        loff_t size)
{
  if (!cred)
    return EOPNOTSUPP;

  return size == 0 ? 0 : EINVAL;
}

/* These four routines modify the O_APPEND, O_ASYNC, O_FSYNC, and
   O_NONBLOCK bits for the IO object. In addition, io_get_openmodes
   will tell you which of O_READ, O_WRITE, and O_EXEC the object can
   be used for.  The O_ASYNC bit affects icky async I/O; good async
   I/O is done through io_async which is orthogonal to these calls. */
kern_return_t
trivfs_S_io_set_all_openmodes(struct trivfs_protid *cred,
                              mach_port_t reply,
                              mach_msg_type_name_t reply_type,
                              int mode)
{
  if (!cred)
    return EOPNOTSUPP;

  return 0;
}

kern_return_t
trivfs_S_io_set_some_openmodes (struct trivfs_protid *cred,
                                mach_port_t reply,
                                mach_msg_type_name_t reply_type,
                                int bits)
{
  if (!cred)
    return EOPNOTSUPP;

  return 0;
}

kern_return_t
trivfs_S_io_get_owner (struct trivfs_protid *cred,
                       mach_port_t reply,
                       mach_msg_type_name_t reply_type,
                       pid_t *owner)
{
  if (!cred)
    return EOPNOTSUPP;

  *owner = 0;
  return 0;
}

kern_return_t
trivfs_S_io_mod_owner (struct trivfs_protid *cred,
                       mach_port_t reply, mach_msg_type_name_t reply_type,
                       pid_t owner)
{
  if (!cred)
    return EOPNOTSUPP;

  return EINVAL;
}

/* Return objects mapping the data underlying this memory object.  If
   the object can be read then memobjrd will be provided; if the
   object can be written then memobjwr will be provided.  For objects
   where read data and write data are the same, these objects will be
   equal, otherwise they will be disjoint.  Servers are permitted to
   implement io_map but not io_map_cntl.  Some objects do not provide
   mapping; they will set none of the ports and return an error.  Such
   objects can still be accessed by io_read and io_write.  */
kern_return_t
trivfs_S_io_map(struct trivfs_protid *cred,
                       mach_port_t reply, mach_msg_type_name_t reply_type,
                mach_port_t *rdobj,
                mach_msg_type_name_t *rdtype,
                mach_port_t *wrobj,
                mach_msg_type_name_t *wrtype)
{
  if (!cred)
    return EOPNOTSUPP;

  return EINVAL;
}


int
random_demuxer (mach_msg_header_t *inp,
                mach_msg_header_t *outp)
{
  extern int startup_notify_server (mach_msg_header_t *, mach_msg_header_t *);

  return (trivfs_demuxer (inp, outp)
	  || startup_notify_server (inp, outp));
}


/* Options processing.  We accept the same options on the command line
   and from fsys_set_options.  */

static const struct argp_option options[] =
{
  {"fast",	'f', 0,	0, "(ignored)"},
  {"secure",    's', 0, 0, "(ignored)"},
  {"seed-file", 'S', "FILE", 0, "Use FILE to remember the seed"},
  {0}
};

static error_t
parse_opt (int opt, char *arg, struct argp_state *state)
{
  switch (opt)
    {
    default:
      return ARGP_ERR_UNKNOWN;
    case ARGP_KEY_INIT:
    case ARGP_KEY_SUCCESS:
    case ARGP_KEY_ERROR:
      break;

    case 'f':
    case 's':
      /* Ignored.  */
      break;

    case 'S':
      seed_file = strdup (arg);
      break;
    }
  return 0;
}

/* This will be called from libtrivfs to help construct the answer
   to an fsys_get_options RPC.  */
error_t
trivfs_append_args (struct trivfs_control *fsys,
		    char **argz, size_t *argz_len)
{
  error_t err = 0;
  char *opt;

  if (seed_file)
    {
      if (asprintf (&opt, "--seed-file=%s", seed_file) < 0)
	err = ENOMEM;
      else
	{
	  err = argz_add (argz, argz_len, opt);
	  free (opt);
	}
    }

  return err;
}

static struct argp random_argp =
{ options, parse_opt, 0,
  "A translator providing random output." };

/* Setting this variable makes libtrivfs use our argp to
   parse options passed in an fsys_set_options RPC.  */
struct argp *trivfs_runtime_argp = &random_argp;

struct port_class *shutdown_notify_class;

/* The system is going down; destroy all the extant port rights.  That
   will cause net channels and such to close promptly.  */
error_t
S_startup_dosync (mach_port_t handle)
{
  error_t err;
  struct port_info *inpi = ports_lookup_port (fsys->pi.bucket, handle,
					      shutdown_notify_class);

  if (!inpi)
    return EOPNOTSUPP;

  err = update_random_seed_file ();
  if (err)
    error (0, err, "Warning: Failed to save random seed to %s", seed_file);
  return 0;
}

void
sigterm_handler (int signo)
{
  error_t err;
  err = update_random_seed_file ();
  if (err)
    error (0, err, "Warning: Failed to save random seed to %s", seed_file);
  signal (SIGTERM, SIG_DFL);
  raise (SIGTERM);
}

static error_t
arrange_shutdown_notification (void)
{
  error_t err;
  mach_port_t initport, notify;
  struct port_info *pi;

  shutdown_notify_class = ports_create_class (0, 0);

  if (signal (SIGTERM, sigterm_handler) == SIG_ERR)
    return errno;

  /* Arrange to get notified when the system goes down,
     but if we fail for some reason, just silently give up.  No big deal. */

  err = ports_create_port (shutdown_notify_class, fsys->pi.bucket,
			   sizeof (struct port_info), &pi);
  if (err)
    return err;

  initport = file_name_lookup (_SERVERS_STARTUP, 0, 0);
  if (! MACH_PORT_VALID (initport))
    return errno;

  notify = ports_get_send_right (pi);
  ports_port_deref (pi);
  err = startup_request_notification (initport, notify,
				      MACH_MSG_TYPE_MAKE_SEND,
				      program_invocation_short_name);

  mach_port_deallocate (mach_task_self (), notify);
  mach_port_deallocate (mach_task_self (), initport);
  return err;
}

int
main (int argc, char **argv)
{
  error_t err;
  unsigned int seed;
  mach_port_t bootstrap;

  /* We use the same argp for options available at startup
     as for options we'll accept in an fsys_set_options RPC.  */
  argp_parse (&random_argp, argc, argv, 0, 0, 0);

  pool_initialize ();

  err = read_random_seed_file ();
  if (err)
    error (0, err, "Warning: Failed to read random seed file %s", seed_file);

  /* Initialize the libcs PRNG.  */
  pool_randomize (&seed, sizeof seed);
  srandom (seed);

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap == MACH_PORT_NULL)
    error (1, 0, "Must be started as a translator");

  /* Reply to our parent */
  err = trivfs_startup (bootstrap, 0, 0, 0, 0, 0, &fsys);
  mach_port_deallocate (mach_task_self (), bootstrap);
  if (err)
    error (3, err, "trivfs_startup");

  err = arrange_shutdown_notification ();
  if (err)
    error (0, err, "Warning: Cannot request shutdown notification");

  err = start_gather_thread ();
  if (err)
    error (1, err, "Starting gather thread failed");

  /* Launch. */
  ports_manage_port_operations_multithread (fsys->pi.bucket, random_demuxer,
					    10 * 1000, /* idle thread */
					    10 * 60 * 1000, /* idle server */
					    0);
  return 0;
}