3 * $Id: noise.c,v 1.2 1999/11/11 00:59:08 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.2 1999/11/11 00:59:08 mdw
34 * A bit of reformatting. Initialize the uid and gid correctly.
36 * Revision 1.1 1999/09/03 08:41:12 mdw
41 /*----- Header files ------------------------------------------------------*/
49 #include <sys/types.h>
60 #include <mLib/bits.h>
67 /*----- Magical numbers ---------------------------------------------------*/
69 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
70 #define MILLION 1000000 /* One million */
72 /*----- Noise source definition -------------------------------------------*/
74 rand_source noise_source
= { noise_acquire
, noise_timer
};
76 /*----- Static variables --------------------------------------------------*/
78 /* --- Timer differences --- */
80 static unsigned long noise_last
; /* Last time recorded */
81 static unsigned long noise_diff
; /* Last first order difference */
83 /* --- Setuid program handling --- */
85 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
86 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
88 /*----- Main code ---------------------------------------------------------*/
90 /* --- @bitcount@ --- *
92 * Arguments: @unsigned long x@ = a word containing bits
94 * Returns: The number of bits set in the word.
97 static int bitcount(unsigned long x
)
99 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
100 1, 2, 2, 3, 2, 3, 3, 4 };
103 count
+= ctab
[x
& 0xfu
];
111 * Arguments: @rand_pool *r@ = pointer to randomness pool
112 * @struct timeval *tv@ = pointer to time block
114 * Returns: Nonzer if some randomness was contributed.
116 * Use: Low-level timer contributor.
119 static int timer(rand_pool
*r
, struct timeval
*tv
)
121 unsigned long x
, d
, dd
;
125 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
131 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
133 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
137 /* --- @noise_timer@ --- *
139 * Arguments: @rand_pool *r@ = pointer to a randomness pool
141 * Returns: Nonzero if some randomness was contributed.
143 * Use: Contributes the current time to the randomness pool.
144 * A guess at the number of useful bits contributed is made,
145 * based on first and second order bit differences. This isn't
146 * ever-so reliable, but it's better than nothing.
149 int noise_timer(rand_pool
*r
)
152 gettimeofday(&tv
, 0);
153 return (timer(r
, &tv
));
156 /* --- @noise_devrandom@ --- *
158 * Arguments: @rand_pool *r@ = pointer to a randomness pool
160 * Returns: Nonzero if some randomness was contributed.
162 * Use: Attempts to obtain some randomness from the system entropy
163 * pool. All bits from the device are assumed to be good.
166 int noise_devrandom(rand_pool
*r
)
169 octet buf
[RAND_POOLSZ
];
173 /* --- Be nice to other clients of the random device --- *
175 * Attempt to open the device nonblockingly. If that works, take up to
176 * one bufferful and then close again. If there's no data to be read,
177 * then that's tough and we go away again, on the grounds that the device
178 * needs to get some more entropy from somewhere.
181 if ((fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
182 if ((len
= read(fd
, buf
, sizeof(buf
))) > 0) {
183 rand_add(r
, buf
, len
, len
* 8);
193 /* --- @noise_setid@ --- *
195 * Arguments: @uid_t uid@ = uid to set
196 * @gid_t gid@ = gid to set
200 * Use: Sets the user and group ids to be used by @noise_filter@
201 * when running child processes. This is useful to avoid
202 * giving shell commands (even carefully written ones) undue
206 void noise_setid(uid_t uid
, gid_t gid
)
212 /* --- @noise_filter@ --- *
214 * Arguments: @rand_pool *r@ = pointer to a randomness pool
215 * @int good@ = number of good bits per 1024 bits
216 * @const char *c@ = shell command to run
218 * Returns: Nonzero if some randomness was contributed.
220 * Use: Attempts to execute a shell command, and dump it into the
221 * randomness pool. A very rough estimate of the number of
222 * good bits is made, based on the size of the command's output.
223 * This function calls @waitpid@, so be careful. Before execing
224 * the command, the process uid and gid are set to the values
225 * given to @noise_setid@, and an attempt is made to reset the
226 * list of supplementary groups. The environment passed to
227 * the command has been severly lobotimized. If the command
228 * fails to complete within a short time period, it is killed.
229 * Paranoid use of close-on-exec flags for file descriptors is
233 int noise_filter(rand_pool
*r
, int good
, const char *c
)
240 const char *env
[] = {
241 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
245 /* --- Remember when this business started --- */
247 gettimeofday(&dead
, 0);
250 /* --- Create a pipe --- */
255 /* --- Fork a child off --- */
264 /* --- Handle the child end of the deal --- */
270 /* --- Set the pipe as standard output, close standard input --- */
272 close(0); close(1); close(2);
275 if (dup2(fd
[1], 1) < 0) _exit(127);
279 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
280 (f
= open("/dev/null", O_WRONLY
)) != 2)
283 /* --- Play games with uids --- */
285 if (noise_gid
!= NOISE_NOSETGID
) {
288 #ifdef HAVE_SETGROUPS
289 setgroups(1, &noise_gid
);
293 if (noise_uid
!= NOISE_NOSETUID
) {
298 /* --- Start the process up --- */
300 execle("/bin/sh", "-c", c
, (char *)0, env
);
304 /* --- Sort out my end of the deal --- */
308 /* --- Decide on the deadline --- */
310 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
312 /* --- Now read, and think --- */
315 struct timeval now
, diff
;
318 gettimeofday(&now
, 0);
320 if (TV_CMP(&now
, >, &dead
))
322 TV_SUB(&diff
, &dead
, &now
);
327 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
329 if (FD_ISSET(fd
[0], &rd
)) {
333 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
335 goodbits
= (sz
* good
) / 128;
336 rand_add(r
, buf
, sz
, goodbits
);
341 /* --- We've finished with it: kill the child process --- *
343 * This is morally questionable. On the other hand, I don't want to be
344 * held up in the @waitpid@ call if I can possibly help it. Maybe a
345 * double-fork is worth doing here.
356 /* --- @noise_acquire@ --- *
358 * Arguments: @rand_pool *r@ = pointer to a randomness pool
362 * Use: Acquires some randomness from somewhere.
365 void noise_acquire(rand_pool
*r
)
367 if (!noise_devrandom(r
)) {
369 /* --- Output of `ps' --- *
371 * This is a hopefully cheap way of getting a bit of noise. I'm guessing
372 * the good bit ratio based on about 90 bytes per line of output, and
373 * each line containing maybe 12 bits worth of interesting data. (Some
374 * quick experiments with gzip seem to bear this idea out.) So, 12 good
375 * bits per 90 bytes of output gives slightly more than 17 good bits per
376 * 1024 bits of output. So I'll be a pessimist and say 16 bits.
380 (noise_filter(r
, 16, "ps alxww") ||
381 noise_filter(r
, 16, "ps -elf"));
383 /* --- Output of `netstat' --- *
385 * Another possibly cheap way of getting noise. My output has about
386 * 72 bytes per line of output. My wild guesses are telling me that
387 * there are probably only about four good bits per line (gzip tells
388 * me there's six, but I want to underestimate). Four bits per 72 bytes
389 * is 7 good bits per 1024 bits of output. Pessimist that I am, I'll
393 noise_filter(r
, 6, "netstat -n");
397 /*----- That's all, folks -------------------------------------------------*/