5 * Acquisition of environmental noise (Unix-specific)
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
39 #include <sys/types.h>
50 #include <mLib/bits.h>
57 /*----- Magical numbers ---------------------------------------------------*/
59 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
60 #define MILLION 1000000 /* One million */
62 /*----- Noise source definition -------------------------------------------*/
64 const rand_source noise_source
= { noise_acquire
, noise_timer
};
66 /*----- Static variables --------------------------------------------------*/
68 /* --- Timer differences --- */
70 static unsigned long noise_last
; /* Last time recorded */
71 static unsigned long noise_diff
; /* Last first order difference */
73 /* --- Setuid program handling --- */
75 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
76 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
78 /*----- Main code ---------------------------------------------------------*/
80 /* --- @bitcount@ --- *
82 * Arguments: @unsigned long x@ = a word containing bits
84 * Returns: The number of bits set in the word.
87 static int bitcount(unsigned long x
)
89 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
90 1, 2, 2, 3, 2, 3, 3, 4 };
93 count
+= ctab
[x
& 0xfu
];
101 * Arguments: @rand_pool *r@ = pointer to randomness pool
102 * @struct timeval *tv@ = pointer to time block
104 * Returns: Nonzero if some randomness was contributed.
106 * Use: Low-level timer contributor.
109 static int timer(rand_pool
*r
, struct timeval
*tv
)
111 unsigned long x
, d
, dd
;
115 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
121 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
123 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
127 /* --- @noise_timer@ --- *
129 * Arguments: @rand_pool *r@ = pointer to a randomness pool
131 * Returns: Nonzero if some randomness was contributed.
133 * Use: Contributes the current time to the randomness pool.
134 * A guess at the number of useful bits contributed is made,
135 * based on first and second order bit differences. This isn't
136 * ever-so reliable, but it's better than nothing.
139 int noise_timer(rand_pool
*r
)
142 gettimeofday(&tv
, 0);
143 return (timer(r
, &tv
));
146 /* --- @noise_devrandom@ --- *
148 * Arguments: @rand_pool *r@ = pointer to a randomness pool
150 * Returns: Nonzero if some randomness was contributed.
152 * Use: Attempts to obtain some randomness from the system entropy
153 * pool. All bits from the device are assumed to be good.
156 int noise_devrandom(rand_pool
*r
)
159 octet buf
[RAND_POOLSZ
];
163 /* --- Be nice to other clients of the random device --- *
165 * Attempt to open the device nonblockingly. If that works, take up to
166 * one bufferful and then close again. If there's no data to be read,
167 * then that's tough and we go away again, on the grounds that the device
168 * needs to get some more entropy from somewhere.
171 if ((fd
= open("/dev/urandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
172 (fd
= open("/dev/arandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
173 (fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
174 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
175 rand_add(r
, buf
, len
, len
* 8);
185 /* --- @noise_setid@ --- *
187 * Arguments: @uid_t uid@ = uid to set
188 * @gid_t gid@ = gid to set
192 * Use: Sets the user and group ids to be used by @noise_filter@
193 * when running child processes. This is useful to avoid
194 * giving shell commands (even carefully written ones) undue
195 * privileges. This interface is Unix-specific
198 void noise_setid(uid_t uid
, gid_t gid
)
204 /* --- @noise_filter@ --- *
206 * Arguments: @rand_pool *r@ = pointer to a randomness pool
207 * @int good@ = number of good bits per 1024 bits
208 * @const char *c@ = shell command to run
210 * Returns: Nonzero if some randomness was contributed.
212 * Use: Attempts to execute a shell command, and dump it into the
213 * randomness pool. A very rough estimate of the number of
214 * good bits is made, based on the size of the command's output.
215 * This function calls @waitpid@, so be careful. Before execing
216 * the command, the process uid and gid are set to the values
217 * given to @noise_setid@, and an attempt is made to reset the
218 * list of supplementary groups. The environment passed to
219 * the command has been severly lobotimized. If the command
220 * fails to complete within a short time period, it is killed.
221 * Paranoid use of close-on-exec flags for file descriptors is
224 * This interface is Unix-specific.
227 int noise_filter(rand_pool
*r
, int good
, const char *c
)
234 const char *env
[] = {
235 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
239 /* --- Remember when this business started --- */
241 gettimeofday(&dead
, 0);
244 /* --- Create a pipe --- */
249 /* --- Fork a child off --- */
259 /* --- Handle the child end of the deal --- */
264 /* --- Set the pipe as standard output, close standard input --- */
266 close(0); close(1); close(2);
269 if (dup2(fd
[1], 1) < 0) _exit(127);
273 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
274 (f
= open("/dev/null", O_WRONLY
)) != 2)
277 /* --- Play games with uids --- */
279 if (noise_gid
!= NOISE_NOSETGID
) {
282 #ifdef HAVE_SETGROUPS
283 setgroups(1, &noise_gid
);
287 if (noise_uid
!= NOISE_NOSETUID
) {
292 /* --- Start the process up --- */
294 execle("/bin/sh", "-c", c
, (char *)0, env
);
298 /* --- Sort out my end of the deal --- */
302 /* --- Decide on the deadline --- */
304 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
306 /* --- Now read, and think --- */
309 struct timeval now
, diff
;
312 gettimeofday(&now
, 0);
314 if (TV_CMP(&now
, >, &dead
))
316 TV_SUB(&diff
, &dead
, &now
);
321 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
323 if (FD_ISSET(fd
[0], &rd
)) {
327 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
329 goodbits
= (sz
* good
) / 128;
330 rand_add(r
, buf
, sz
, goodbits
);
335 /* --- We've finished with it: kill the child process --- *
337 * This is morally questionable. On the other hand, I don't want to be
338 * held up in the @waitpid@ call if I can possibly help it. Maybe a
339 * double-fork is worth doing here.
350 /* --- @noise_freewheel@ --- *
352 * Arguments: @rand_pool *r@ = pointer to a randomness pool
354 * Returns: Nonzero if some randomness was contributed.
356 * Use: Runs a free counter for a short while as a desparate attempt
357 * to get randomness from somewhere. This is actually quite
363 static jmp_buf fwjmp
;
365 static void fwalarm(int sig
)
367 siglongjmp(fwjmp
, 1);
370 int noise_freewheel(rand_pool
*r
)
372 void (*sigal
)(int) = 0;
373 struct itimerval oitv
, itv
= { { 0, 0 }, { 0, 5000 } };
375 volatile uint32 fwcount
= 0;
377 if (!sigsetjmp(fwjmp
, 1)) {
378 if ((sigal
= signal(SIGALRM
, fwalarm
)) == SIG_ERR
)
380 if (setitimer(ITIMER_REAL
, &itv
, &oitv
))
386 STORE32(buf
, fwcount
);
387 rand_add(r
, buf
, sizeof(buf
), 16);
392 signal(SIGALRM
, sigal
);
393 TV_SUB(&oitv
.it_value
, &oitv
.it_value
, &itv
.it_value
);
394 setitimer(ITIMER_REAL
, &oitv
, 0);
400 int noise_freewheel(rand_pool
*r
)
407 /* --- @noise_enquire@ --- *
409 * Arguments: @rand_pool *r@ = pointer to a randomness pool
411 * Returns: Nonzero if some randomness was contributed.
413 * Use: Runs some shell commands to enquire about the prevailing
414 * environment. This can gather quite a lot of low-quality
418 int noise_enquire(rand_pool
*r
)
424 { "ps alxww || ps -elf", 16 },
426 { "ifconfig -a", 8 },
429 { "ls -align /tmp/.", 10 },
434 for (i
= 0; tab
[i
].cmd
; i
++)
435 noise_filter(r
, tab
[i
].rate
, tab
[i
].cmd
);
439 /* --- @noise_acquire@ --- *
441 * Arguments: @rand_pool *r@ = pointer to a randomness pool
445 * Use: Acquires some randomness from somewhere.
448 void noise_acquire(rand_pool
*r
)
451 for (i
= 0; i
< 8; i
++)
453 if (!noise_devrandom(r
)) {
455 for (i
= 0; i
< 8; i
++)
460 /*----- That's all, folks -------------------------------------------------*/