aa45e03b822ea9f17cce1b35ecaae05f48aab0fe
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>
56 /*----- Magical numbers ---------------------------------------------------*/
58 #define NOISE_KIDLIFE 100000 /* @noise_filter@ child lifetime */
59 #define MILLION 1000000 /* One million */
61 /*----- Noise source definition -------------------------------------------*/
63 const rand_source noise_source
= { noise_acquire
, noise_timer
};
65 /*----- Static variables --------------------------------------------------*/
67 /* --- Timer differences --- */
69 static unsigned long noise_last
; /* Last time recorded */
70 static unsigned long noise_diff
; /* Last first order difference */
72 /* --- Setuid program handling --- */
74 static uid_t noise_uid
= NOISE_NOSETUID
; /* Uid to set to spawn processes */
75 static gid_t noise_gid
= NOISE_NOSETGID
; /* Gid to set to spawn processes */
77 /*----- Main code ---------------------------------------------------------*/
79 /* --- @bitcount@ --- *
81 * Arguments: @unsigned long x@ = a word containing bits
83 * Returns: The number of bits set in the word.
86 static int bitcount(unsigned long x
)
88 char ctab
[] = { 0, 1, 1, 2, 1, 2, 2, 3,
89 1, 2, 2, 3, 2, 3, 3, 4 };
92 count
+= ctab
[x
& 0xfu
];
100 * Arguments: @rand_pool *r@ = pointer to randomness pool
101 * @struct timeval *tv@ = pointer to time block
103 * Returns: Nonzero if some randomness was contributed.
105 * Use: Low-level timer contributor.
108 static int timer(rand_pool
*r
, struct timeval
*tv
)
110 unsigned long x
, d
, dd
;
114 x
= tv
->tv_usec
+ MILLION
* tv
->tv_sec
;
121 rand_add(r
, tv
, sizeof(*tv
), de
<= dde ? de
: dde
);
123 BURN(tv
); x
= d
= dd
= de
= dde
= 0;
127 /* --- @noise_timer@ --- *
129 * Arguments: @rand_pool *r@ = pointer to a randomness pool
131 * Returns: Nonzero if some randomness was contributed.
133 * Use: Contributes the current time to the randomness pool.
134 * A guess at the number of useful bits contributed is made,
135 * based on first and second order bit differences. This isn't
136 * ever-so reliable, but it's better than nothing.
139 int noise_timer(rand_pool
*r
)
142 gettimeofday(&tv
, 0);
143 return (timer(r
, &tv
));
146 /* --- @noise_devrandom@ --- *
148 * Arguments: @rand_pool *r@ = pointer to a randomness pool
150 * Returns: Nonzero if some randomness was contributed.
152 * Use: Attempts to obtain some randomness from the system entropy
153 * pool. All bits from the device are assumed to be good.
156 int noise_devrandom(rand_pool
*r
)
159 octet buf
[RAND_POOLSZ
];
164 /* --- Be nice to other clients of the random device --- *
166 * Attempt to open the device nonblockingly. If that works, take up to
167 * one bufferful and then close again. If there's no data to be read,
168 * then that's tough and we go away again, on the grounds that the device
169 * needs to get some more entropy from somewhere.
172 if ((fd
= open("/dev/urandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
173 (fd
= open("/dev/arandom", O_RDONLY
| O_NONBLOCK
)) >= 0 ||
174 (fd
= open("/dev/random", O_RDONLY
| O_NONBLOCK
)) >= 0) {
175 while (n
< sizeof(buf
)) {
176 if ((len
= read(fd
, buf
+ n
, sizeof(buf
) - n
)) <= 0) break;
179 rand_add(r
, buf
, n
, n
* 8);
181 if (n
== sizeof(buf
)) ret
= 1;
188 /* --- @noise_setid@ --- *
190 * Arguments: @uid_t uid@ = uid to set
191 * @gid_t gid@ = gid to set
195 * Use: Sets the user and group ids to be used by @noise_filter@
196 * when running child processes. This is useful to avoid
197 * giving shell commands (even carefully written ones) undue
198 * privileges. This interface is Unix-specific
201 void noise_setid(uid_t uid
, gid_t gid
)
207 /* --- @noise_filter@ --- *
209 * Arguments: @rand_pool *r@ = pointer to a randomness pool
210 * @int good@ = number of good bits per 1024 bits
211 * @const char *c@ = shell command to run
213 * Returns: Nonzero if some randomness was contributed.
215 * Use: Attempts to execute a shell command, and dump it into the
216 * randomness pool. A very rough estimate of the number of
217 * good bits is made, based on the size of the command's output.
218 * This function calls @waitpid@, so be careful. Before execing
219 * the command, the process uid and gid are set to the values
220 * given to @noise_setid@, and an attempt is made to reset the
221 * list of supplementary groups. The environment passed to
222 * the command has been severly lobotimized. If the command
223 * fails to complete within a short time period, it is killed.
224 * Paranoid use of close-on-exec flags for file descriptors is
227 * This interface is Unix-specific.
230 int noise_filter(rand_pool
*r
, int good
, const char *c
)
237 const char *env
[] = {
238 "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc",
242 /* --- Remember when this business started --- */
244 gettimeofday(&dead
, 0);
247 /* --- Create a pipe --- */
252 /* --- Fork a child off --- */
262 /* --- Handle the child end of the deal --- */
267 /* --- Set the pipe as standard output, close standard input --- */
269 close(0); close(1); close(2);
272 if (dup2(fd
[1], 1) < 0) _exit(127);
276 if ((f
= open("/dev/null", O_RDONLY
)) != 0 ||
277 (f
= open("/dev/null", O_WRONLY
)) != 2)
280 /* --- Play games with uids --- */
282 if (noise_gid
!= NOISE_NOSETGID
) {
285 #ifdef HAVE_SETGROUPS
286 setgroups(1, &noise_gid
);
290 if (noise_uid
!= NOISE_NOSETUID
) {
295 /* --- Start the process up --- */
297 execle("/bin/sh", "sh", "-c", c
, (char *)0, env
);
301 /* --- Sort out my end of the deal --- */
305 /* --- Decide on the deadline --- */
307 TV_ADDL(&dead
, &dead
, 0, NOISE_KIDLIFE
);
309 /* --- Now read, and think --- */
312 struct timeval now
, diff
;
315 gettimeofday(&now
, 0);
317 if (TV_CMP(&now
, >, &dead
))
319 TV_SUB(&diff
, &dead
, &now
);
324 if (select(fd
[0] + 1, &rd
, 0, 0, &diff
) < 0)
326 if (FD_ISSET(fd
[0], &rd
)) {
330 if ((sz
= read(fd
[0], buf
, sizeof(buf
))) <= 0)
332 goodbits
= (sz
* good
) / 128;
333 rand_add(r
, buf
, sz
, goodbits
);
338 /* --- We've finished with it: kill the child process --- *
340 * This is morally questionable. On the other hand, I don't want to be
341 * held up in the @waitpid@ call if I can possibly help it. Maybe a
342 * double-fork is worth doing here.
353 /* --- @noise_freewheel@ --- *
355 * Arguments: @rand_pool *r@ = pointer to a randomness pool
357 * Returns: Nonzero if some randomness was contributed.
359 * Use: Runs a free counter for a short while as a desparate attempt
360 * to get randomness from somewhere. This is actually quite
366 static jmp_buf fwjmp
;
368 static void fwalarm(int sig
)
370 siglongjmp(fwjmp
, 1);
373 int noise_freewheel(rand_pool
*r
)
375 void (*sigal
)(int) = 0;
376 struct itimerval oitv
, itv
= { { 0, 0 }, { 0, 5000 } };
378 volatile uint32 fwcount
= 0;
380 if (!sigsetjmp(fwjmp
, 1)) {
381 if ((sigal
= signal(SIGALRM
, fwalarm
)) == SIG_ERR
)
383 if (setitimer(ITIMER_REAL
, &itv
, &oitv
))
389 STORE32(buf
, fwcount
);
390 rand_add(r
, buf
, sizeof(buf
), 16);
395 signal(SIGALRM
, sigal
);
396 if (oitv
.it_value
.tv_sec
|| oitv
.it_value
.tv_usec
)
397 TV_SUB(&oitv
.it_value
, &oitv
.it_value
, &itv
.it_value
);
398 setitimer(ITIMER_REAL
, &oitv
, 0);
404 int noise_freewheel(rand_pool
*r
)
411 /* --- @noise_enquire@ --- *
413 * Arguments: @rand_pool *r@ = pointer to a randomness pool
415 * Returns: Nonzero if some randomness was contributed.
417 * Use: Runs some shell commands to enquire about the prevailing
418 * environment. This can gather quite a lot of low-quality
422 int noise_enquire(rand_pool
*r
)
428 { "ps alxww || ps -elf", 16 },
430 { "ifconfig -a", 8 },
433 { "ls -align /tmp/.", 10 },
438 for (i
= 0; tab
[i
].cmd
; i
++)
439 noise_filter(r
, tab
[i
].rate
, tab
[i
].cmd
);
443 /* --- @noise_acquire@ --- *
445 * Arguments: @rand_pool *r@ = pointer to a randomness pool
449 * Use: Acquires some randomness from somewhere.
452 void noise_acquire(rand_pool
*r
)
455 for (i
= 0; i
< 8; i
++)
457 if (!noise_devrandom(r
) || getenv("CATACOMB_FORCE_ESOTERIC_SOURCES")) {
459 for (i
= 0; i
< 8; i
++)
464 /*----- That's all, folks -------------------------------------------------*/