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 ------------------------------------------------------*/
38 #include <sys/types.h>
49 #include <mLib/bits.h>
50 #include <mLib/mdup.h>
58 /*----- Magical numbers ---------------------------------------------------*/
60 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
61 #define MILLION 1000000 /* One million */
63 /*----- Noise source definition -------------------------------------------*/
65 const rand_source noise_source
= { noise_acquire
, noise_timer
};
67 /*----- Static variables --------------------------------------------------*/
69 /* --- Timer differences --- */
71 static unsigned long noise_last
; /* Last time recorded */
72 static unsigned long noise_diff
; /* Last first order difference */
74 /* --- Setuid program handling --- */
76 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
77 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @bitcount@ --- *
83 * Arguments: @unsigned long x@ = a word containing bits
85 * Returns: The number of bits set in the word.
88 static int bitcount(unsigned long x
)
90 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
91 1, 2, 2, 3, 2, 3, 3, 4 };
94 count
+= ctab
[x
& 0xfu
];
102 * Arguments: @rand_pool *r@ = pointer to randomness pool
103 * @struct timeval *tv@ = pointer to time block
105 * Returns: Nonzero if some randomness was contributed.
107 * Use: Low-level timer contributor.
110 static int timer(rand_pool
*r
, struct timeval
*tv
)
112 unsigned long x
, d
, dd
;
116 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
123 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
125 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
129 /* --- @noise_timer@ --- *
131 * Arguments: @rand_pool *r@ = pointer to a randomness pool
133 * Returns: Nonzero if some randomness was contributed.
135 * Use: Contributes the current time to the randomness pool.
136 * A guess at the number of useful bits contributed is made,
137 * based on first and second order bit differences. This isn't
138 * ever-so reliable, but it's better than nothing.
141 int noise_timer(rand_pool
*r
)
144 gettimeofday(&tv
, 0);
145 return (timer(r
, &tv
));
148 /* --- @noise_devrandom@ --- *
150 * Arguments: @rand_pool *r@ = pointer to a randomness pool
152 * Returns: Nonzero if some randomness was contributed.
154 * Use: Attempts to obtain some randomness from the system entropy
155 * pool. All bits from the device are assumed to be good.
158 int noise_devrandom(rand_pool
*r
)
161 octet buf
[RAND_POOLSZ
];
166 /* --- Be nice to other clients of the random device --- *
168 * Attempt to open the device nonblockingly. If that works, take up to
169 * one bufferful and then close again. If there's no data to be read,
170 * then that's tough and we go away again, on the grounds that the device
171 * needs to get some more entropy from somewhere.
174 if ((fd
= open("/dev/urandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
175 (fd
= open("/dev/arandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
176 (fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
177 while (n
< sizeof(buf
)) {
178 if ((len
= read(fd
, buf
+ n
, sizeof(buf
) - n
)) <= 0) break;
181 rand_add(r
, buf
, n
, n
* 8);
183 if (n
== sizeof(buf
)) ret
= 1;
190 /* --- @noise_setid@ --- *
192 * Arguments: @uid_t uid@ = uid to set
193 * @gid_t gid@ = gid to set
197 * Use: Sets the user and group ids to be used by @noise_filter@
198 * when running child processes. This is useful to avoid
199 * giving shell commands (even carefully written ones) undue
200 * privileges. This interface is Unix-specific
203 void noise_setid(uid_t uid
, gid_t gid
)
209 /* --- @noise_filter@ --- *
211 * Arguments: @rand_pool *r@ = pointer to a randomness pool
212 * @int good@ = number of good bits per 1024 bits
213 * @const char *c@ = shell command to run
215 * Returns: Nonzero if some randomness was contributed.
217 * Use: Attempts to execute a shell command, and dump it into the
218 * randomness pool. A very rough estimate of the number of
219 * good bits is made, based on the size of the command's output.
220 * This function calls @waitpid@, so be careful. Before execing
221 * the command, the process uid and gid are set to the values
222 * given to @noise_setid@, and an attempt is made to reset the
223 * list of supplementary groups. The environment passed to
224 * the command has been severly lobotimized. If the command
225 * fails to complete within a short time period, it is killed.
226 * Paranoid use of close-on-exec flags for file descriptors is
229 * This interface is Unix-specific.
240 static void kid_read(int fd
, unsigned mode
, void *p
)
242 struct noisekid
*nk
= p
;
247 if ((sz
= read(fd
, nk
->buf
, sizeof(nk
->buf
))) <= 0)
250 goodbits
= (sz
* nk
->good
) / 128;
251 rand_add(nk
->r
, nk
->buf
, sz
, goodbits
);
256 static void kid_dead(struct timeval
*tv
, void *p
)
257 { struct noisekid
*nk
= p
; nk
->donep
= 1; }
259 int noise_filter(rand_pool
*r
, int good
, const char *c
)
265 struct noisekid nk
= { 0 };
269 const char *env
[] = {
270 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
274 /* --- Remember when this business started --- */
276 gettimeofday(&dead
, 0);
279 /* --- Create a pipe --- */
284 /* --- Fork a child off --- */
294 /* --- Handle the child end of the deal --- */
300 /* --- Set the pipe as standard output, close standard input --- */
302 if ((f
= open("/dev/null", O_RDONLY
)) < 0) _exit(127);
303 mfd
[i
].cur
= f
; mfd
[i
].want
= 0; i
++;
304 mfd
[i
].cur
= fd
[1]; mfd
[i
].want
= 1; i
++;
305 mfd
[i
].cur
= f
; mfd
[i
].want
= 2; i
++;
306 if (mdup(mfd
, i
)) _exit(127);
308 /* --- Play games with uids --- */
310 if (noise_gid
!= NOISE_NOSETGID
) {
313 #ifdef HAVE_SETGROUPS
314 setgroups(1, &noise_gid
);
318 if (noise_uid
!= NOISE_NOSETUID
) {
323 /* --- Start the process up --- */
325 execle("/bin/sh", "sh", "-c", c
, (char *)0, env
);
329 /* --- Sort out my end of the deal --- */
333 nk
.r
= r
; nk
.good
= good
;
334 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
335 sel_initfile(&sel
, &sf
, fd
[0], SEL_READ
, kid_read
, &nk
);
337 sel_addtimer(&sel
, &st
, &dead
, kid_dead
, &nk
);
338 while (!nk
.donep
&& !sel_select(&sel
));
340 /* --- We've finished with it: kill the child process --- *
342 * This is morally questionable. On the other hand, I don't want to be
343 * held up in the @waitpid@ call if I can possibly help it. Maybe a
344 * double-fork is worth doing here.
355 /* --- @noise_freewheel@ --- *
357 * Arguments: @rand_pool *r@ = pointer to a randomness pool
359 * Returns: Nonzero if some randomness was contributed.
361 * Use: Runs a free counter for a short while as a desparate attempt
362 * to get randomness from somewhere. This is actually quite
368 static jmp_buf fwjmp
;
370 static void fwalarm(int sig
)
372 siglongjmp(fwjmp
, 1);
375 int noise_freewheel(rand_pool
*r
)
377 void (*sigal
)(int) = 0;
378 struct itimerval oitv
, itv
= { { 0, 0 }, { 0, 5000 } };
380 volatile uint32 fwcount
= 0;
382 if (!sigsetjmp(fwjmp
, 1)) {
383 if ((sigal
= signal(SIGALRM
, fwalarm
)) == SIG_ERR
)
385 if (setitimer(ITIMER_REAL
, &itv
, &oitv
))
391 STORE32(buf
, fwcount
);
392 rand_add(r
, buf
, sizeof(buf
), 16);
397 signal(SIGALRM
, sigal
);
398 if (oitv
.it_value
.tv_sec
|| oitv
.it_value
.tv_usec
)
399 TV_SUB(&oitv
.it_value
, &oitv
.it_value
, &itv
.it_value
);
400 setitimer(ITIMER_REAL
, &oitv
, 0);
406 int noise_freewheel(rand_pool
*r
)
413 /* --- @noise_enquire@ --- *
415 * Arguments: @rand_pool *r@ = pointer to a randomness pool
417 * Returns: Nonzero if some randomness was contributed.
419 * Use: Runs some shell commands to enquire about the prevailing
420 * environment. This can gather quite a lot of low-quality
424 int noise_enquire(rand_pool
*r
)
430 { "ps alxww || ps -elf", 16 },
432 { "ifconfig -a", 8 },
435 { "ls -align /tmp/.", 10 },
440 for (i
= 0; tab
[i
].cmd
; i
++)
441 noise_filter(r
, tab
[i
].rate
, tab
[i
].cmd
);
445 /* --- @noise_acquire@ --- *
447 * Arguments: @rand_pool *r@ = pointer to a randomness pool
451 * Use: Acquires some randomness from somewhere.
454 void noise_acquire(rand_pool
*r
)
457 for (i
= 0; i
< 8; i
++)
459 if (!noise_devrandom(r
) || getenv("CATACOMB_FORCE_ESOTERIC_SOURCES")) {
461 for (i
= 0; i
< 8; i
++)
466 /*----- That's all, folks -------------------------------------------------*/