3 * $Id: noise.c,v 1.4 1999/12/10 23:25:15 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.4 1999/12/10 23:25:15 mdw
34 * Bug fix: remove old spurious fflush.
36 * Revision 1.3 1999/12/10 23:24:11 mdw
37 * Bug fix: flush buffers before forking.
39 * Revision 1.2 1999/11/11 00:59:08 mdw
40 * A bit of reformatting. Initialize the uid and gid correctly.
42 * Revision 1.1 1999/09/03 08:41:12 mdw
47 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
66 #include <mLib/bits.h>
73 /*----- Magical numbers ---------------------------------------------------*/
75 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
76 #define MILLION 1000000 /* One million */
78 /*----- Noise source definition -------------------------------------------*/
80 rand_source noise_source
= { noise_acquire
, noise_timer
};
82 /*----- Static variables --------------------------------------------------*/
84 /* --- Timer differences --- */
86 static unsigned long noise_last
; /* Last time recorded */
87 static unsigned long noise_diff
; /* Last first order difference */
89 /* --- Setuid program handling --- */
91 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
92 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @bitcount@ --- *
98 * Arguments: @unsigned long x@ = a word containing bits
100 * Returns: The number of bits set in the word.
103 static int bitcount(unsigned long x
)
105 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
106 1, 2, 2, 3, 2, 3, 3, 4 };
109 count
+= ctab
[x
& 0xfu
];
117 * Arguments: @rand_pool *r@ = pointer to randomness pool
118 * @struct timeval *tv@ = pointer to time block
120 * Returns: Nonzer if some randomness was contributed.
122 * Use: Low-level timer contributor.
125 static int timer(rand_pool
*r
, struct timeval
*tv
)
127 unsigned long x
, d
, dd
;
131 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
137 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
139 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
143 /* --- @noise_timer@ --- *
145 * Arguments: @rand_pool *r@ = pointer to a randomness pool
147 * Returns: Nonzero if some randomness was contributed.
149 * Use: Contributes the current time to the randomness pool.
150 * A guess at the number of useful bits contributed is made,
151 * based on first and second order bit differences. This isn't
152 * ever-so reliable, but it's better than nothing.
155 int noise_timer(rand_pool
*r
)
158 gettimeofday(&tv
, 0);
159 return (timer(r
, &tv
));
162 /* --- @noise_devrandom@ --- *
164 * Arguments: @rand_pool *r@ = pointer to a randomness pool
166 * Returns: Nonzero if some randomness was contributed.
168 * Use: Attempts to obtain some randomness from the system entropy
169 * pool. All bits from the device are assumed to be good.
172 int noise_devrandom(rand_pool
*r
)
175 octet buf
[RAND_POOLSZ
];
179 /* --- Be nice to other clients of the random device --- *
181 * Attempt to open the device nonblockingly. If that works, take up to
182 * one bufferful and then close again. If there's no data to be read,
183 * then that's tough and we go away again, on the grounds that the device
184 * needs to get some more entropy from somewhere.
187 if ((fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
188 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
189 rand_add(r
, buf
, len
, len
* 8);
199 /* --- @noise_setid@ --- *
201 * Arguments: @uid_t uid@ = uid to set
202 * @gid_t gid@ = gid to set
206 * Use: Sets the user and group ids to be used by @noise_filter@
207 * when running child processes. This is useful to avoid
208 * giving shell commands (even carefully written ones) undue
212 void noise_setid(uid_t uid
, gid_t gid
)
218 /* --- @noise_filter@ --- *
220 * Arguments: @rand_pool *r@ = pointer to a randomness pool
221 * @int good@ = number of good bits per 1024 bits
222 * @const char *c@ = shell command to run
224 * Returns: Nonzero if some randomness was contributed.
226 * Use: Attempts to execute a shell command, and dump it into the
227 * randomness pool. A very rough estimate of the number of
228 * good bits is made, based on the size of the command's output.
229 * This function calls @waitpid@, so be careful. Before execing
230 * the command, the process uid and gid are set to the values
231 * given to @noise_setid@, and an attempt is made to reset the
232 * list of supplementary groups. The environment passed to
233 * the command has been severly lobotimized. If the command
234 * fails to complete within a short time period, it is killed.
235 * Paranoid use of close-on-exec flags for file descriptors is
239 int noise_filter(rand_pool
*r
, int good
, const char *c
)
246 const char *env
[] = {
247 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
251 /* --- Remember when this business started --- */
253 gettimeofday(&dead
, 0);
256 /* --- Create a pipe --- */
261 /* --- Fork a child off --- */
271 /* --- Handle the child end of the deal --- */
276 /* --- Set the pipe as standard output, close standard input --- */
278 close(0); close(1); close(2);
281 if (dup2(fd
[1], 1) < 0) _exit(127);
285 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
286 (f
= open("/dev/null", O_WRONLY
)) != 2)
289 /* --- Play games with uids --- */
291 if (noise_gid
!= NOISE_NOSETGID
) {
294 #ifdef HAVE_SETGROUPS
295 setgroups(1, &noise_gid
);
299 if (noise_uid
!= NOISE_NOSETUID
) {
304 /* --- Start the process up --- */
306 execle("/bin/sh", "-c", c
, (char *)0, env
);
310 /* --- Sort out my end of the deal --- */
314 /* --- Decide on the deadline --- */
316 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
318 /* --- Now read, and think --- */
321 struct timeval now
, diff
;
324 gettimeofday(&now
, 0);
326 if (TV_CMP(&now
, >, &dead
))
328 TV_SUB(&diff
, &dead
, &now
);
333 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
335 if (FD_ISSET(fd
[0], &rd
)) {
339 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
341 goodbits
= (sz
* good
) / 128;
342 rand_add(r
, buf
, sz
, goodbits
);
347 /* --- We've finished with it: kill the child process --- *
349 * This is morally questionable. On the other hand, I don't want to be
350 * held up in the @waitpid@ call if I can possibly help it. Maybe a
351 * double-fork is worth doing here.
362 /* --- @noise_acquire@ --- *
364 * Arguments: @rand_pool *r@ = pointer to a randomness pool
368 * Use: Acquires some randomness from somewhere.
371 void noise_acquire(rand_pool
*r
)
373 if (!noise_devrandom(r
)) {
375 /* --- Output of `ps' --- *
377 * This is a hopefully cheap way of getting a bit of noise. I'm guessing
378 * the good bit ratio based on about 90 bytes per line of output, and
379 * each line containing maybe 12 bits worth of interesting data. (Some
380 * quick experiments with gzip seem to bear this idea out.) So, 12 good
381 * bits per 90 bytes of output gives slightly more than 17 good bits per
382 * 1024 bits of output. So I'll be a pessimist and say 16 bits.
386 (noise_filter(r
, 16, "ps alxww") ||
387 noise_filter(r
, 16, "ps -elf"));
389 /* --- Output of `netstat' --- *
391 * Another possibly cheap way of getting noise. My output has about
392 * 72 bytes per line of output. My wild guesses are telling me that
393 * there are probably only about four good bits per line (gzip tells
394 * me there's six, but I want to underestimate). Four bits per 72 bytes
395 * is 7 good bits per 1024 bits of output. Pessimist that I am, I'll
399 noise_filter(r
, 6, "netstat -n");
403 /*----- That's all, folks -------------------------------------------------*/