3 * $Id: noise.c,v 1.3 1999/12/10 23:24:11 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/12/10 23:24:11 mdw
34 * Bug fix: flush buffers before forking.
36 * Revision 1.2 1999/11/11 00:59:08 mdw
37 * A bit of reformatting. Initialize the uid and gid correctly.
39 * Revision 1.1 1999/09/03 08:41:12 mdw
44 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
63 #include <mLib/bits.h>
70 /*----- Magical numbers ---------------------------------------------------*/
72 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
73 #define MILLION 1000000 /* One million */
75 /*----- Noise source definition -------------------------------------------*/
77 rand_source noise_source
= { noise_acquire
, noise_timer
};
79 /*----- Static variables --------------------------------------------------*/
81 /* --- Timer differences --- */
83 static unsigned long noise_last
; /* Last time recorded */
84 static unsigned long noise_diff
; /* Last first order difference */
86 /* --- Setuid program handling --- */
88 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
89 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
91 /*----- Main code ---------------------------------------------------------*/
93 /* --- @bitcount@ --- *
95 * Arguments: @unsigned long x@ = a word containing bits
97 * Returns: The number of bits set in the word.
100 static int bitcount(unsigned long x
)
102 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
103 1, 2, 2, 3, 2, 3, 3, 4 };
106 count
+= ctab
[x
& 0xfu
];
114 * Arguments: @rand_pool *r@ = pointer to randomness pool
115 * @struct timeval *tv@ = pointer to time block
117 * Returns: Nonzer if some randomness was contributed.
119 * Use: Low-level timer contributor.
122 static int timer(rand_pool
*r
, struct timeval
*tv
)
124 unsigned long x
, d
, dd
;
128 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
134 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
136 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
140 /* --- @noise_timer@ --- *
142 * Arguments: @rand_pool *r@ = pointer to a randomness pool
144 * Returns: Nonzero if some randomness was contributed.
146 * Use: Contributes the current time to the randomness pool.
147 * A guess at the number of useful bits contributed is made,
148 * based on first and second order bit differences. This isn't
149 * ever-so reliable, but it's better than nothing.
152 int noise_timer(rand_pool
*r
)
155 gettimeofday(&tv
, 0);
156 return (timer(r
, &tv
));
159 /* --- @noise_devrandom@ --- *
161 * Arguments: @rand_pool *r@ = pointer to a randomness pool
163 * Returns: Nonzero if some randomness was contributed.
165 * Use: Attempts to obtain some randomness from the system entropy
166 * pool. All bits from the device are assumed to be good.
169 int noise_devrandom(rand_pool
*r
)
172 octet buf
[RAND_POOLSZ
];
176 /* --- Be nice to other clients of the random device --- *
178 * Attempt to open the device nonblockingly. If that works, take up to
179 * one bufferful and then close again. If there's no data to be read,
180 * then that's tough and we go away again, on the grounds that the device
181 * needs to get some more entropy from somewhere.
184 if ((fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
185 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
186 rand_add(r
, buf
, len
, len
* 8);
196 /* --- @noise_setid@ --- *
198 * Arguments: @uid_t uid@ = uid to set
199 * @gid_t gid@ = gid to set
203 * Use: Sets the user and group ids to be used by @noise_filter@
204 * when running child processes. This is useful to avoid
205 * giving shell commands (even carefully written ones) undue
209 void noise_setid(uid_t uid
, gid_t gid
)
215 /* --- @noise_filter@ --- *
217 * Arguments: @rand_pool *r@ = pointer to a randomness pool
218 * @int good@ = number of good bits per 1024 bits
219 * @const char *c@ = shell command to run
221 * Returns: Nonzero if some randomness was contributed.
223 * Use: Attempts to execute a shell command, and dump it into the
224 * randomness pool. A very rough estimate of the number of
225 * good bits is made, based on the size of the command's output.
226 * This function calls @waitpid@, so be careful. Before execing
227 * the command, the process uid and gid are set to the values
228 * given to @noise_setid@, and an attempt is made to reset the
229 * list of supplementary groups. The environment passed to
230 * the command has been severly lobotimized. If the command
231 * fails to complete within a short time period, it is killed.
232 * Paranoid use of close-on-exec flags for file descriptors is
236 int noise_filter(rand_pool
*r
, int good
, const char *c
)
243 const char *env
[] = {
244 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
248 /* --- Remember when this business started --- */
250 gettimeofday(&dead
, 0);
253 /* --- Create a pipe --- */
258 /* --- Fork a child off --- */
268 /* --- Handle the child end of the deal --- */
274 /* --- Set the pipe as standard output, close standard input --- */
276 close(0); close(1); close(2);
279 if (dup2(fd
[1], 1) < 0) _exit(127);
283 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
284 (f
= open("/dev/null", O_WRONLY
)) != 2)
287 /* --- Play games with uids --- */
289 if (noise_gid
!= NOISE_NOSETGID
) {
292 #ifdef HAVE_SETGROUPS
293 setgroups(1, &noise_gid
);
297 if (noise_uid
!= NOISE_NOSETUID
) {
302 /* --- Start the process up --- */
304 execle("/bin/sh", "-c", c
, (char *)0, env
);
308 /* --- Sort out my end of the deal --- */
312 /* --- Decide on the deadline --- */
314 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
316 /* --- Now read, and think --- */
319 struct timeval now
, diff
;
322 gettimeofday(&now
, 0);
324 if (TV_CMP(&now
, >, &dead
))
326 TV_SUB(&diff
, &dead
, &now
);
331 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
333 if (FD_ISSET(fd
[0], &rd
)) {
337 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
339 goodbits
= (sz
* good
) / 128;
340 rand_add(r
, buf
, sz
, goodbits
);
345 /* --- We've finished with it: kill the child process --- *
347 * This is morally questionable. On the other hand, I don't want to be
348 * held up in the @waitpid@ call if I can possibly help it. Maybe a
349 * double-fork is worth doing here.
360 /* --- @noise_acquire@ --- *
362 * Arguments: @rand_pool *r@ = pointer to a randomness pool
366 * Use: Acquires some randomness from somewhere.
369 void noise_acquire(rand_pool
*r
)
371 if (!noise_devrandom(r
)) {
373 /* --- Output of `ps' --- *
375 * This is a hopefully cheap way of getting a bit of noise. I'm guessing
376 * the good bit ratio based on about 90 bytes per line of output, and
377 * each line containing maybe 12 bits worth of interesting data. (Some
378 * quick experiments with gzip seem to bear this idea out.) So, 12 good
379 * bits per 90 bytes of output gives slightly more than 17 good bits per
380 * 1024 bits of output. So I'll be a pessimist and say 16 bits.
384 (noise_filter(r
, 16, "ps alxww") ||
385 noise_filter(r
, 16, "ps -elf"));
387 /* --- Output of `netstat' --- *
389 * Another possibly cheap way of getting noise. My output has about
390 * 72 bytes per line of output. My wild guesses are telling me that
391 * there are probably only about four good bits per line (gzip tells
392 * me there's six, but I want to underestimate). Four bits per 72 bytes
393 * is 7 good bits per 1024 bits of output. Pessimist that I am, I'll
397 noise_filter(r
, 6, "netstat -n");
401 /*----- That's all, folks -------------------------------------------------*/