3 * Acquisition of environmental noise (Unix-specific)
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
48 #include <mLib/bits.h>
55 /*----- Magical numbers ---------------------------------------------------*/
57 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
58 #define MILLION 1000000 /* One million */
60 /*----- Noise source definition -------------------------------------------*/
62 const rand_source noise_source
= { noise_acquire
, noise_timer
};
64 /*----- Static variables --------------------------------------------------*/
66 /* --- Timer differences --- */
68 static unsigned long noise_last
; /* Last time recorded */
69 static unsigned long noise_diff
; /* Last first order difference */
71 /* --- Setuid program handling --- */
73 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
74 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @bitcount@ --- *
80 * Arguments: @unsigned long x@ = a word containing bits
82 * Returns: The number of bits set in the word.
85 static int bitcount(unsigned long x
)
87 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
88 1, 2, 2, 3, 2, 3, 3, 4 };
91 count
+= ctab
[x
& 0xfu
];
99 * Arguments: @rand_pool *r@ = pointer to randomness pool
100 * @struct timeval *tv@ = pointer to time block
102 * Returns: Nonzero if some randomness was contributed.
104 * Use: Low-level timer contributor.
107 static int timer(rand_pool
*r
, struct timeval
*tv
)
109 unsigned long x
, d
, dd
;
113 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
119 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
121 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
125 /* --- @noise_timer@ --- *
127 * Arguments: @rand_pool *r@ = pointer to a randomness pool
129 * Returns: Nonzero if some randomness was contributed.
131 * Use: Contributes the current time to the randomness pool.
132 * A guess at the number of useful bits contributed is made,
133 * based on first and second order bit differences. This isn't
134 * ever-so reliable, but it's better than nothing.
137 int noise_timer(rand_pool
*r
)
140 gettimeofday(&tv
, 0);
141 return (timer(r
, &tv
));
144 /* --- @noise_devrandom@ --- *
146 * Arguments: @rand_pool *r@ = pointer to a randomness pool
148 * Returns: Nonzero if some randomness was contributed.
150 * Use: Attempts to obtain some randomness from the system entropy
151 * pool. All bits from the device are assumed to be good.
154 int noise_devrandom(rand_pool
*r
)
157 octet buf
[RAND_POOLSZ
];
161 /* --- Be nice to other clients of the random device --- *
163 * Attempt to open the device nonblockingly. If that works, take up to
164 * one bufferful and then close again. If there's no data to be read,
165 * then that's tough and we go away again, on the grounds that the device
166 * needs to get some more entropy from somewhere.
169 if ((fd
= open("/dev/urandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
170 (fd
= open("/dev/arandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
171 (fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
172 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
173 rand_add(r
, buf
, len
, len
* 8);
183 /* --- @noise_setid@ --- *
185 * Arguments: @uid_t uid@ = uid to set
186 * @gid_t gid@ = gid to set
190 * Use: Sets the user and group ids to be used by @noise_filter@
191 * when running child processes. This is useful to avoid
192 * giving shell commands (even carefully written ones) undue
193 * privileges. This interface is Unix-specific
196 void noise_setid(uid_t uid
, gid_t gid
)
202 /* --- @noise_filter@ --- *
204 * Arguments: @rand_pool *r@ = pointer to a randomness pool
205 * @int good@ = number of good bits per 1024 bits
206 * @const char *c@ = shell command to run
208 * Returns: Nonzero if some randomness was contributed.
210 * Use: Attempts to execute a shell command, and dump it into the
211 * randomness pool. A very rough estimate of the number of
212 * good bits is made, based on the size of the command's output.
213 * This function calls @waitpid@, so be careful. Before execing
214 * the command, the process uid and gid are set to the values
215 * given to @noise_setid@, and an attempt is made to reset the
216 * list of supplementary groups. The environment passed to
217 * the command has been severly lobotimized. If the command
218 * fails to complete within a short time period, it is killed.
219 * Paranoid use of close-on-exec flags for file descriptors is
222 * This interface is Unix-specific.
225 int noise_filter(rand_pool
*r
, int good
, const char *c
)
232 const char *env
[] = {
233 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
237 /* --- Remember when this business started --- */
239 gettimeofday(&dead
, 0);
242 /* --- Create a pipe --- */
247 /* --- Fork a child off --- */
257 /* --- Handle the child end of the deal --- */
262 /* --- Set the pipe as standard output, close standard input --- */
264 close(0); close(1); close(2);
267 if (dup2(fd
[1], 1) < 0) _exit(127);
271 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
272 (f
= open("/dev/null", O_WRONLY
)) != 2)
275 /* --- Play games with uids --- */
277 if (noise_gid
!= NOISE_NOSETGID
) {
280 #ifdef HAVE_SETGROUPS
281 setgroups(1, &noise_gid
);
285 if (noise_uid
!= NOISE_NOSETUID
) {
290 /* --- Start the process up --- */
292 execle("/bin/sh", "-c", c
, (char *)0, env
);
296 /* --- Sort out my end of the deal --- */
300 /* --- Decide on the deadline --- */
302 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
304 /* --- Now read, and think --- */
307 struct timeval now
, diff
;
310 gettimeofday(&now
, 0);
312 if (TV_CMP(&now
, >, &dead
))
314 TV_SUB(&diff
, &dead
, &now
);
319 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
321 if (FD_ISSET(fd
[0], &rd
)) {
325 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
327 goodbits
= (sz
* good
) / 128;
328 rand_add(r
, buf
, sz
, goodbits
);
333 /* --- We've finished with it: kill the child process --- *
335 * This is morally questionable. On the other hand, I don't want to be
336 * held up in the @waitpid@ call if I can possibly help it. Maybe a
337 * double-fork is worth doing here.
348 /* --- @noise_freewheel@ --- *
350 * Arguments: @rand_pool *r@ = pointer to a randomness pool
352 * Returns: Nonzero if some randomness was contributed.
354 * Use: Runs a free counter for a short while as a desparate attempt
355 * to get randomness from somewhere. This is actually quite
361 static jmp_buf fwjmp
;
363 static void fwalarm(int sig
)
365 siglongjmp(fwjmp
, 1);
368 int noise_freewheel(rand_pool
*r
)
370 void (*sigal
)(int) = 0;
371 struct itimerval oitv
, itv
= { { 0, 0 }, { 0, 5000 } };
373 volatile uint32 fwcount
= 0;
375 if (!sigsetjmp(fwjmp
, 1)) {
376 if ((sigal
= signal(SIGALRM
, fwalarm
)) == SIG_ERR
)
378 if (setitimer(ITIMER_REAL
, &itv
, &oitv
))
384 STORE32(buf
, fwcount
);
385 rand_add(r
, buf
, sizeof(buf
), 16);
390 signal(SIGALRM
, sigal
);
391 if (oitv
.it_value
.tv_sec
|| oitv
.it_value
.tv_usec
)
392 TV_SUB(&oitv
.it_value
, &oitv
.it_value
, &itv
.it_value
);
393 setitimer(ITIMER_REAL
, &oitv
, 0);
399 int noise_freewheel(rand_pool
*r
)
406 /* --- @noise_enquire@ --- *
408 * Arguments: @rand_pool *r@ = pointer to a randomness pool
410 * Returns: Nonzero if some randomness was contributed.
412 * Use: Runs some shell commands to enquire about the prevailing
413 * environment. This can gather quite a lot of low-quality
417 int noise_enquire(rand_pool
*r
)
423 { "ps alxww || ps -elf", 16 },
425 { "ifconfig -a", 8 },
428 { "ls -align /tmp/.", 10 },
433 for (i
= 0; tab
[i
].cmd
; i
++)
434 noise_filter(r
, tab
[i
].rate
, tab
[i
].cmd
);
438 /* --- @noise_acquire@ --- *
440 * Arguments: @rand_pool *r@ = pointer to a randomness pool
444 * Use: Acquires some randomness from somewhere.
447 void noise_acquire(rand_pool
*r
)
450 for (i
= 0; i
< 8; i
++)
452 if (!noise_devrandom(r
)) {
454 for (i
= 0; i
< 8; i
++)
459 /*----- That's all, folks -------------------------------------------------*/