2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-03 16:15:27 +00:00

No need to destroy filesources -- they will clean up when the source is destroyed

This commit is contained in:
Michael Graff
2000-06-15 18:15:26 +00:00
parent 06ed5a0d46
commit c3581e2bcc
7 changed files with 135 additions and 104 deletions

View File

@@ -35,8 +35,6 @@
extern int verbose; extern int verbose;
extern const char *program; extern const char *program;
static isc_entropysource_t *filesource = NULL;
void void
fatal(const char *format, ...) { fatal(const char *format, ...) {
va_list args; va_list args;
@@ -171,17 +169,16 @@ setup_logging(int verbose, isc_mem_t *mctx, isc_log_t **logp) {
void void
setup_entropy(isc_mem_t *mctx, const char *randomfile, isc_entropy_t **ectx) { setup_entropy(isc_mem_t *mctx, const char *randomfile, isc_entropy_t **ectx) {
isc_result_t result; isc_result_t result;
result = isc_entropy_create(mctx, ectx); result = isc_entropy_create(mctx, ectx);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
fatal("could not create entropy object"); fatal("could not create entropy object");
if (randomfile != NULL) { if (randomfile != NULL) {
result = isc_entropy_createfilesource(*ectx, randomfile, 0, result = isc_entropy_createfilesource(*ectx, randomfile, 0);
&filesource);
if (result == ISC_R_SUCCESS) if (result == ISC_R_SUCCESS)
return; return;
} }
result = isc_entropy_createfilesource(*ectx, "/dev/random", 0, result = isc_entropy_createfilesource(*ectx, "/dev/random", 0);
&filesource);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
fatal("No randomfile specified, and /dev/random not present."); fatal("No randomfile specified, and /dev/random not present.");
return; return;
@@ -189,7 +186,5 @@ setup_entropy(isc_mem_t *mctx, const char *randomfile, isc_entropy_t **ectx) {
void void
cleanup_entropy(isc_entropy_t **ectx) { cleanup_entropy(isc_entropy_t **ectx) {
if (filesource != NULL)
isc_entropy_destroysource(&filesource);
isc_entropy_detach(ectx); isc_entropy_detach(ectx);
} }

View File

@@ -229,7 +229,6 @@ int
main(void) { main(void) {
isc_mem_t *mctx = NULL; isc_mem_t *mctx = NULL;
isc_entropy_t *ectx = NULL; isc_entropy_t *ectx = NULL;
isc_entropysource_t *devrandom = NULL, *randfile = NULL;
isc_buffer_t b; isc_buffer_t b;
dns_fixedname_t fname; dns_fixedname_t fname;
dns_name_t *name; dns_name_t *name;
@@ -242,10 +241,8 @@ main(void) {
dns_result_register(); dns_result_register();
isc_entropy_create(mctx, &ectx); isc_entropy_create(mctx, &ectx);
isc_entropy_createfilesource(ectx, "/dev/random", 0, isc_entropy_createfilesource(ectx, "/dev/random", 0);
&devrandom); isc_entropy_createfilesource(ectx, "randomfile", 0);
isc_entropy_createfilesource(ectx, "randomfile", 0,
&randfile);
dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING|ISC_ENTROPY_GOODONLY); dst_lib_init(mctx, ectx, ISC_ENTROPY_BLOCKING|ISC_ENTROPY_GOODONLY);
dns_fixedname_init(&fname); dns_fixedname_init(&fname);
@@ -270,10 +267,6 @@ main(void) {
generate(DST_ALG_HMACMD5, mctx); generate(DST_ALG_HMACMD5, mctx);
dst_lib_destroy(); dst_lib_destroy();
if (devrandom != NULL)
isc_entropy_destroysource(&devrandom);
if (randfile != NULL)
isc_entropy_destroysource(&randfile);
isc_entropy_detach(&ectx); isc_entropy_detach(&ectx);
isc_mem_put(mctx, current, 256); isc_mem_put(mctx, current, 256);

View File

@@ -338,8 +338,6 @@ static void
t1(void) { t1(void) {
isc_mem_t *mctx; isc_mem_t *mctx;
isc_entropy_t *ectx; isc_entropy_t *ectx;
isc_entropysource_t *devrandom;
isc_entropysource_t *randfile;
int nfails; int nfails;
int nprobs; int nprobs;
int result; int result;
@@ -368,12 +366,8 @@ t1(void) {
t_result(T_UNRESOLVED); t_result(T_UNRESOLVED);
return; return;
} }
devrandom = NULL; isc_entropy_createfilesource(ectx, "/dev/random", 0);
isc_entropy_createfilesource(ectx, "/dev/random", 0, result = isc_entropy_createfilesource(ectx, "randomfile", 0);
&devrandom);
randfile = NULL;
result = isc_entropy_createfilesource(ectx, "randomfile", 0,
&randfile);
if (isc_result != ISC_R_SUCCESS) { if (isc_result != ISC_R_SUCCESS) {
t_info("isc_entropy_create failed %d\n", t_info("isc_entropy_create failed %d\n",
isc_result_totext(isc_result)); isc_result_totext(isc_result));
@@ -426,9 +420,6 @@ t1(void) {
dst_lib_destroy(); dst_lib_destroy();
if (devrandom != NULL)
isc_entropy_destroysource(&devrandom);
isc_entropy_destroysource(&randfile);
isc_entropy_detach(&ectx); isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx); isc_mem_destroy(&mctx);
@@ -813,8 +804,6 @@ t2_vfy(char **av) {
int nprobs; int nprobs;
isc_mem_t *mctx; isc_mem_t *mctx;
isc_entropy_t *ectx; isc_entropy_t *ectx;
isc_entropysource_t *devrandom;
isc_entropysource_t *randfile;
isc_result_t isc_result; isc_result_t isc_result;
int result; int result;
@@ -851,12 +840,8 @@ t2_vfy(char **av) {
isc_result_totext(isc_result)); isc_result_totext(isc_result));
return(T_UNRESOLVED); return(T_UNRESOLVED);
} }
devrandom = NULL; isc_entropy_createfilesource(ectx, "/dev/random", 0);
isc_entropy_createfilesource(ectx, "/dev/random", 0, result = isc_entropy_createfilesource(ectx, "randomfile", 0);
&devrandom);
randfile = NULL;
result = isc_entropy_createfilesource(ectx, "randomfile", 0,
&randfile);
if (isc_result != ISC_R_SUCCESS) { if (isc_result != ISC_R_SUCCESS) {
t_info("isc_entropy_create failed %d\n", t_info("isc_entropy_create failed %d\n",
isc_result_totext(isc_result)); isc_result_totext(isc_result));
@@ -878,9 +863,6 @@ t2_vfy(char **av) {
dst_lib_destroy(); dst_lib_destroy();
if (devrandom != NULL)
isc_entropy_destroysource(&devrandom);
isc_entropy_destroysource(&randfile);
isc_entropy_detach(&ectx); isc_entropy_detach(&ectx);
isc_mem_destroy(&mctx); isc_mem_destroy(&mctx);

View File

@@ -53,7 +53,6 @@ main(int argc, char **argv) {
isc_mem_t *mctx; isc_mem_t *mctx;
unsigned char buffer[512]; unsigned char buffer[512];
isc_entropy_t *ent; isc_entropy_t *ent;
isc_entropysource_t *devrandom;
unsigned int returned; unsigned int returned;
unsigned int flags; unsigned int flags;
isc_result_t result; isc_result_t result;
@@ -72,17 +71,15 @@ main(int argc, char **argv) {
isc_entropy_stats(ent, stderr); isc_entropy_stats(ent, stderr);
#if 1 #if 1
devrandom = NULL;
flags = 0; flags = 0;
CHECK("isc_entropy_createfilesource()", CHECK("isc_entropy_createfilesource() 1",
isc_entropy_createfilesource(ent, "/dev/random", isc_entropy_createfilesource(ent, "/dev/random", flags));
flags, &devrandom)); CHECK("isc_entropy_createfilesource() 2",
isc_entropy_createfilesource(ent, "/dev/random", flags));
#else #else
devrandom = NULL;
flags = 0; flags = 0;
CHECK("isc_entropy_createfilesource()", CHECK("isc_entropy_createfilesource() 3",
isc_entropy_createfilesource(ent, "/tmp/foo", isc_entropy_createfilesource(ent, "/tmp/foo", flags));
flags, &devrandom));
#endif #endif
fprintf(stderr, fprintf(stderr,
@@ -100,7 +97,7 @@ main(int argc, char **argv) {
any: any:
isc_entropy_stats(ent, stderr); isc_entropy_stats(ent, stderr);
CHECK("isc_entropy_getdata()", CHECK("isc_entropy_getdata() pseudorandom",
isc_entropy_getdata(ent, buffer, 128, NULL, 0)); isc_entropy_getdata(ent, buffer, 128, NULL, 0));
hex_dump("pseudorandom data", buffer, 128); hex_dump("pseudorandom data", buffer, 128);
@@ -129,10 +126,10 @@ main(int argc, char **argv) {
isc_entropy_detach(&entcopy3); isc_entropy_detach(&entcopy3);
} }
isc_entropy_destroysource(&devrandom);
isc_entropy_detach(&ent); isc_entropy_detach(&ent);
isc_mem_stats(mctx, stderr); isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx); isc_mem_destroy(&mctx);
return (0); return (0);
} }

View File

@@ -359,7 +359,6 @@ main(int argc, char *argv[]) {
struct in_addr inaddr; struct in_addr inaddr;
dns_fixedname_t fname; dns_fixedname_t fname;
dns_name_t *name; dns_name_t *name;
isc_entropysource_t *devrandom;
isc_buffer_t b; isc_buffer_t b;
isc_result_t result; isc_result_t result;
@@ -373,10 +372,8 @@ main(int argc, char *argv[]) {
ectx = NULL; ectx = NULL;
RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS); RUNTIME_CHECK(isc_entropy_create(mctx, &ectx) == ISC_R_SUCCESS);
devrandom = NULL; result = isc_entropy_createfilesource(ectx, "/dev/random", 0);
result = isc_entropy_createfilesource(ectx, "/dev/random", 0, if (result != ISC_R_SUCCESS) {
&devrandom);
if (devrandom == NULL) {
fprintf(stderr, fprintf(stderr,
"%s only runs when /dev/random is available.\n", "%s only runs when /dev/random is available.\n",
argv[0]); argv[0]);
@@ -476,7 +473,6 @@ main(int argc, char *argv[]) {
isc_log_destroy(&log); isc_log_destroy(&log);
dst_lib_destroy(); dst_lib_destroy();
isc_entropy_destroysource(&devrandom);
isc_entropy_detach(&ectx); isc_entropy_detach(&ectx);
if (verbose) if (verbose)

View File

@@ -124,8 +124,7 @@ isc_entropy_detach(isc_entropy_t **entp);
isc_result_t isc_result_t
isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname, isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname,
unsigned int flags, unsigned int flags);
isc_entropysource_t **sourcep);
/* /*
* Create a new entropy source from a file. * Create a new entropy source from a file.
* *

View File

@@ -83,6 +83,8 @@ struct isc_entropy {
unsigned int refcnt; unsigned int refcnt;
isc_uint32_t initialized; isc_uint32_t initialized;
isc_entropypool_t pool; isc_entropypool_t pool;
unsigned int nsources;
isc_entropysource_t *nextsource;
ISC_LIST(isc_entropysource_t) sources; ISC_LIST(isc_entropysource_t) sources;
}; };
@@ -163,22 +165,22 @@ subtract_entropy(isc_entropy_t *ent, isc_uint32_t entropy) {
* very large. * very large.
*/ */
static inline void static inline void
add_pseudo(isc_entropy_t *ent, isc_uint32_t entropy) { add_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
/* clamp input. Yes, this must be done. */ /* clamp input. Yes, this must be done. */
entropy = ISC_MIN(entropy, RND_POOLBITS * 8); pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
/* Add in the entropy we already have. */ /* Add in the pseudo we already have. */
entropy += ent->pool.pseudo; pseudo += ent->pool.pseudo;
/* Clamp. */ /* Clamp. */
ent->pool.pseudo = ISC_MIN(entropy, RND_POOLBITS * 8); ent->pool.pseudo = ISC_MIN(pseudo, RND_POOLBITS * 8);
} }
/* /*
* Decrement the amount of entropy the pool has. * Decrement the amount of pseudo the pool has.
*/ */
static inline void static inline void
subtract_pseudo(isc_entropy_t *ent, isc_uint32_t entropy) { subtract_pseudo(isc_entropy_t *ent, isc_uint32_t pseudo) {
entropy = ISC_MIN(entropy, ent->pool.pseudo); pseudo = ISC_MIN(pseudo, ent->pool.pseudo);
ent->pool.pseudo -= entropy; ent->pool.pseudo -= pseudo;
} }
/* /*
@@ -320,6 +322,7 @@ static void
fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) { fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) {
isc_uint32_t added; isc_uint32_t added;
isc_uint32_t remaining; isc_uint32_t remaining;
unsigned int nsource;
isc_entropysource_t *source; isc_entropysource_t *source;
REQUIRE(VALID_ENTROPY(ent)); REQUIRE(VALID_ENTROPY(ent));
@@ -372,7 +375,7 @@ fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) {
/* /*
* But wait! If we're not yet initialized, we need at least * But wait! If we're not yet initialized, we need at least
* THRESHOLD_BITS * THRESHOLD_BITS
* bytes of randomness. * of randomness.
*/ */
if (ent->initialized < THRESHOLD_BITS) if (ent->initialized < THRESHOLD_BITS)
needed = ISC_MAX(needed, THRESHOLD_BITS - ent->initialized); needed = ISC_MAX(needed, THRESHOLD_BITS - ent->initialized);
@@ -387,11 +390,19 @@ fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) {
added = 0; added = 0;
remaining = needed; remaining = needed;
if (ent->nextsource == NULL) {
ent->nextsource = ISC_LIST_HEAD(ent->sources);
if (ent->nextsource == NULL)
return;
}
source = ent->nextsource;
again: again:
source = ISC_LIST_HEAD(ent->sources); for (nsource = 0 ; nsource < ent->nsources ; nsource++) {
while (source != NULL && remaining > 0) {
isc_uint32_t got; isc_uint32_t got;
if (remaining == 0)
break;
got = 0; got = 0;
if (source->type == ENTROPY_SOURCETYPE_FILE) if (source->type == ENTROPY_SOURCETYPE_FILE)
@@ -404,11 +415,14 @@ fillpool(isc_entropy_t *ent, unsigned int needed, isc_boolean_t blocking) {
else else
remaining = 0; remaining = 0;
source = ISC_LIST_NEXT(source, link);
if (source == NULL)
source = ISC_LIST_HEAD(ent->sources);
if (added >= needed) if (added >= needed)
break; break;
source = ISC_LIST_NEXT(source, link);
} }
ent->nextsource = source;
if (blocking && remaining != 0) { if (blocking && remaining != 0) {
int fds; int fds;
@@ -652,6 +666,8 @@ isc_entropy_create(isc_mem_t *mctx, isc_entropy_t **entp) {
* From here down, no failures will/can occur. * From here down, no failures will/can occur.
*/ */
ISC_LIST_INIT(ent->sources); ISC_LIST_INIT(ent->sources);
ent->nextsource = NULL;
ent->nsources = 0;
ent->mctx = NULL; ent->mctx = NULL;
isc_mem_attach(mctx, &ent->mctx); isc_mem_attach(mctx, &ent->mctx);
ent->refcnt = 1; ent->refcnt = 1;
@@ -669,12 +685,63 @@ isc_entropy_create(isc_mem_t *mctx, isc_entropy_t **entp) {
return (ret); return (ret);
} }
/*
* Requires "ent" be locked.
*/
static void
destroysource(isc_entropysource_t **sourcep) {
isc_entropysource_t *source;
isc_entropy_t *ent;
void *ptr;
int fd;
source = *sourcep;
*sourcep = NULL;
ent = source->ent;
ISC_LIST_UNLINK(ent->sources, source, link);
ent->nextsource = NULL;
REQUIRE(ent->nsources > 0);
ent->nsources--;
switch (source->type) {
case ENTROPY_SOURCETYPE_FILE:
fd = source->sources.file.fd;
if (fd >= 0)
close(fd);
break;
case ENTROPY_SOURCETYPE_SAMPLE:
ptr = source->sources.sample.samples;
if (ptr != NULL)
isc_mem_put(ent->mctx, ptr, RND_EVENTQSIZE * 4);
ptr = source->sources.sample.extra;
if (ptr != NULL)
isc_mem_put(ent->mctx, ptr, RND_EVENTQSIZE * 4);
break;
}
memset(source, 0, sizeof(isc_entropysource_t));
isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
}
static inline isc_boolean_t static inline isc_boolean_t
destroy_check(isc_entropy_t *ent) { destroy_check(isc_entropy_t *ent) {
isc_entropysource_t *source;
if (ent->refcnt > 0) if (ent->refcnt > 0)
return (ISC_FALSE); return (ISC_FALSE);
if (!ISC_LIST_EMPTY(ent->sources))
return (ISC_FALSE); source = ISC_LIST_HEAD(ent->sources);
while (source != NULL) {
switch (source->type) {
case ENTROPY_SOURCETYPE_FILE:
break;
default:
return (ISC_FALSE);
}
source = ISC_LIST_NEXT(source, link);
}
return (ISC_TRUE); return (ISC_TRUE);
} }
@@ -682,18 +749,41 @@ destroy_check(isc_entropy_t *ent) {
static void static void
destroy(isc_entropy_t **entp) { destroy(isc_entropy_t **entp) {
isc_entropy_t *ent; isc_entropy_t *ent;
isc_entropysource_t *source;
isc_mem_t *mctx; isc_mem_t *mctx;
REQUIRE(entp != NULL && *entp != NULL); REQUIRE(entp != NULL && *entp != NULL);
ent = *entp; ent = *entp;
*entp = NULL; *entp = NULL;
LOCK(&ent->lock);
REQUIRE(ent->refcnt == 0); REQUIRE(ent->refcnt == 0);
/*
* Here, detach non-sample sources.
*/
source = ISC_LIST_HEAD(ent->sources);
while (source != NULL) {
switch(source->type) {
case ENTROPY_SOURCETYPE_FILE:
destroysource(&source);
break;
}
source = ISC_LIST_HEAD(ent->sources);
}
/*
* If there are other types of sources, we've found a bug.
*/
REQUIRE(ISC_LIST_EMPTY(ent->sources)); REQUIRE(ISC_LIST_EMPTY(ent->sources));
mctx = ent->mctx; mctx = ent->mctx;
isc_entropypool_invalidate(&ent->pool); isc_entropypool_invalidate(&ent->pool);
UNLOCK(&ent->lock);
isc_mutex_destroy(&ent->lock); isc_mutex_destroy(&ent->lock);
memset(ent, 0, sizeof(isc_entropy_t)); memset(ent, 0, sizeof(isc_entropy_t));
@@ -726,8 +816,7 @@ make_nonblock(int fd) {
isc_result_t isc_result_t
isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname, isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname,
unsigned int flags, unsigned int flags)
isc_entropysource_t **sourcep)
{ {
int fd; int fd;
isc_result_t ret; isc_result_t ret;
@@ -735,7 +824,6 @@ isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname,
REQUIRE(VALID_ENTROPY(ent)); REQUIRE(VALID_ENTROPY(ent));
REQUIRE(fname != NULL); REQUIRE(fname != NULL);
REQUIRE(sourcep != NULL && *sourcep == NULL);
LOCK(&ent->lock); LOCK(&ent->lock);
@@ -777,8 +865,7 @@ isc_entropy_createfilesource(isc_entropy_t *ent, const char *fname,
* Hook it into the entropy system. * Hook it into the entropy system.
*/ */
ISC_LIST_APPEND(ent->sources, source, link); ISC_LIST_APPEND(ent->sources, source, link);
ent->nsources++;
*sourcep = source;
UNLOCK(&ent->lock); UNLOCK(&ent->lock);
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
@@ -798,8 +885,6 @@ void
isc_entropy_destroysource(isc_entropysource_t **sourcep) { isc_entropy_destroysource(isc_entropysource_t **sourcep) {
isc_entropysource_t *source; isc_entropysource_t *source;
isc_entropy_t *ent; isc_entropy_t *ent;
void *ptr;
int fd;
isc_boolean_t killit; isc_boolean_t killit;
REQUIRE(sourcep != NULL); REQUIRE(sourcep != NULL);
@@ -813,27 +898,7 @@ isc_entropy_destroysource(isc_entropysource_t **sourcep) {
LOCK(&ent->lock); LOCK(&ent->lock);
ISC_LIST_UNLINK(ent->sources, source, link); destroysource(&source);
switch (source->type) {
case ENTROPY_SOURCETYPE_FILE:
fd = source->sources.file.fd;
if (fd >= 0)
close(fd);
break;
case ENTROPY_SOURCETYPE_SAMPLE:
ptr = source->sources.sample.samples;
if (ptr != NULL)
isc_mem_put(ent->mctx, ptr, RND_EVENTQSIZE * 4);
ptr = source->sources.sample.extra;
if (ptr != NULL)
isc_mem_put(ent->mctx, ptr, RND_EVENTQSIZE * 4);
break;
}
memset(source, 0, sizeof(isc_entropysource_t));
isc_mem_put(ent->mctx, source, sizeof(isc_entropysource_t));
killit = destroy_check(ent); killit = destroy_check(ent);
@@ -852,6 +917,8 @@ isc_entropy_createsamplesource(isc_entropy_t *ent,
LOCK(&ent->lock); LOCK(&ent->lock);
ent->nsources++;
UNLOCK(&ent->lock); UNLOCK(&ent->lock);
return (ISC_R_NOTIMPLEMENTED); return (ISC_R_NOTIMPLEMENTED);
@@ -888,10 +955,12 @@ isc_entropy_stats(isc_entropy_t *ent, FILE *out) {
fprintf(out, fprintf(out,
"Entropy pool %p: refcnt %u" "Entropy pool %p: refcnt %u"
" cursor %u, rotate %u entropy %u pseudo %u\n", " cursor %u, rotate %u entropy %u pseudo %u nsources %u"
" nextsource %p initialized %u\n",
ent, ent->refcnt, ent, ent->refcnt,
ent->pool.cursor, ent->pool.rotate, ent->pool.cursor, ent->pool.rotate,
ent->pool.entropy, ent->pool.pseudo); ent->pool.entropy, ent->pool.pseudo,
ent->nsources, ent->nextsource, ent->initialized);
} }
void void