From owner-svn-src-projects@FreeBSD.ORG Tue Oct 8 06:54:53 2013 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id E8EF1529; Tue, 8 Oct 2013 06:54:53 +0000 (UTC) (envelope-from markm@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id C74FD282B; Tue, 8 Oct 2013 06:54:53 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r986sr3Q018897; Tue, 8 Oct 2013 06:54:53 GMT (envelope-from markm@svn.freebsd.org) Received: (from markm@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r986sqBx018892; Tue, 8 Oct 2013 06:54:52 GMT (envelope-from markm@svn.freebsd.org) Message-Id: <201310080654.r986sqBx018892@svn.freebsd.org> From: Mark Murray Date: Tue, 8 Oct 2013 06:54:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r256135 - in projects/random_number_generator/sys: dev/random kern sys X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Oct 2013 06:54:54 -0000 Author: markm Date: Tue Oct 8 06:54:52 2013 New Revision: 256135 URL: http://svnweb.freebsd.org/changeset/base/256135 Log: Debugging. My attempt at EVENTHANDLER(multiuser) was a failure; use EVENTHANDLER(mountroot) instead. This means we can't count on /var being present, so something will need to be done about harvesting /var/db/entropy/... . Some policy now needs to be sorted out, and a pre-sync cache needs to be written, but apart from that we are now ready to go. Over to review. Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c projects/random_number_generator/sys/dev/random/rwfile.c projects/random_number_generator/sys/dev/random/rwfile.h projects/random_number_generator/sys/kern/init_main.c projects/random_number_generator/sys/sys/eventhandler.h Modified: projects/random_number_generator/sys/dev/random/random_harvestq.c ============================================================================== --- projects/random_number_generator/sys/dev/random/random_harvestq.c Tue Oct 8 04:52:40 2013 (r256134) +++ projects/random_number_generator/sys/dev/random/random_harvestq.c Tue Oct 8 06:54:52 2013 (r256135) @@ -25,7 +25,6 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $FreeBSD$ */ #include @@ -46,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include @@ -80,21 +80,18 @@ static struct proc *random_kthread_proc; static const char *entropy_files[] = { "/entropy", - "/var/db/entropy", - "/boot/entropy", /* Yeah, Yeah. I know this is loaded by - * loader(8), but not always, and it doesn't - * hurt to do this again. - */ NULL }; /* Deal with entropy cached externally if this is present. + * Lots of policy may eventually arrive in this function. + * Called after / is mounted. */ static void random_harvestq_cache(void *arg __unused) { const char **entropy_file; - uint8_t *keyfile, *data; + uint8_t *keyfile, *data, *zbuf; size_t size, i; int error; @@ -104,21 +101,34 @@ random_harvestq_cache(void *arg __unused data = preload_fetch_addr(keyfile); size = preload_fetch_size(keyfile); if (data != NULL && size != 0) { - for (i = 0U; i < size; i += 16) - random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); + for (i = 0; i < size; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); printf("random: read %zu bytes from preloaded cache\n", size); bzero(data, size); } else printf("random: no preloaded entropy cache available\n"); } + + /* Read and attempt to overwrite the entropy cache files. + * If the file exists, can be read and then overwritten,i + * then use it. Ignore it otherwise, but print out what is + * going on. + */ data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); + zbuf = __DECONST(void *, zero_region); for (entropy_file = entropy_files; *entropy_file; entropy_file++) { - error = randomdev_read_file(*entropy_file, data); + error = randomdev_read_file(*entropy_file, data, PAGE_SIZE); if (error == 0) { - for (i = 0U; i < PAGE_SIZE; i += 16) - random_harvestq_internal(get_cyclecount(), data + i, 16, (16*8)/4, RANDOM_CACHED); - printf("random: read %d bytes from '%s'\n", PAGE_SIZE, *entropy_file); + printf("random: entropy cache '%s' provides %d bytes\n", *entropy_file, PAGE_SIZE); + error = randomdev_write_file(*entropy_file, zbuf, PAGE_SIZE); + if (error == 0) { + printf("random: entropy cache '%s' contents used and successfully overwritten\n", *entropy_file); + for (i = 0; i < PAGE_SIZE; i += 16) + random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED); + } + else + printf("random: entropy cache '%s' not overwritten and therefore not used; error = %d\n", *entropy_file, error); } else printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error); @@ -126,7 +136,7 @@ random_harvestq_cache(void *arg __unused bzero(data, PAGE_SIZE); free(data, M_ENTROPY); } -EVENTHANDLER_DEFINE(multiuser, random_harvestq_cache, NULL, 0); +EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0); static void random_kthread(void *arg) Modified: projects/random_number_generator/sys/dev/random/rwfile.c ============================================================================== --- projects/random_number_generator/sys/dev/random/rwfile.c Tue Oct 8 04:52:40 2013 (r256134) +++ projects/random_number_generator/sys/dev/random/rwfile.c Tue Oct 8 06:54:52 2013 (r256135) @@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$"); #include int -randomdev_read_file(const char *filename, void *buf) +randomdev_read_file(const char *filename, void *buf, size_t length) { struct nameidata nd; struct thread* td = curthread; @@ -55,8 +55,7 @@ randomdev_read_file(const char *filename if (nd.ni_vp->v_type != VREG) error = ENOEXEC; else - error = vn_rdwr(UIO_READ, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); - + error = vn_rdwr(UIO_READ, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); VOP_UNLOCK(nd.ni_vp, 0); vn_close(nd.ni_vp, FREAD, td->td_ucred, td); } @@ -65,7 +64,7 @@ randomdev_read_file(const char *filename } int -randomdev_write_file(const char *filename, void *buf) +randomdev_write_file(const char *filename, void *buf, size_t length) { struct nameidata nd; struct thread* td = curthread; @@ -81,7 +80,7 @@ randomdev_write_file(const char *filenam if (nd.ni_vp->v_type != VREG) error = ENOEXEC; else - error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, PAGE_SIZE, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); + error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td); VOP_UNLOCK(nd.ni_vp, 0); vn_close(nd.ni_vp, FREAD, td->td_ucred, td); Modified: projects/random_number_generator/sys/dev/random/rwfile.h ============================================================================== --- projects/random_number_generator/sys/dev/random/rwfile.h Tue Oct 8 04:52:40 2013 (r256134) +++ projects/random_number_generator/sys/dev/random/rwfile.h Tue Oct 8 06:54:52 2013 (r256135) @@ -26,5 +26,5 @@ * $FreeBSD$ */ -int randomdev_read_file(const char *filename, void *buf); -int randomdev_write_file(const char *filename, void *buf); +int randomdev_read_file(const char *filename, void *buf, size_t); +int randomdev_write_file(const char *filename, void *buf, size_t); Modified: projects/random_number_generator/sys/kern/init_main.c ============================================================================== --- projects/random_number_generator/sys/kern/init_main.c Tue Oct 8 04:52:40 2013 (r256134) +++ projects/random_number_generator/sys/kern/init_main.c Tue Oct 8 06:54:52 2013 (r256135) @@ -847,8 +847,6 @@ kick_init(const void *udata __unused) { struct thread *td; - EVENTHANDLER_INVOKE(multiuser); - td = FIRST_THREAD_IN_PROC(initproc); thread_lock(td); TD_SET_CAN_RUN(td); Modified: projects/random_number_generator/sys/sys/eventhandler.h ============================================================================== --- projects/random_number_generator/sys/sys/eventhandler.h Tue Oct 8 04:52:40 2013 (r256134) +++ projects/random_number_generator/sys/sys/eventhandler.h Tue Oct 8 06:54:52 2013 (r256135) @@ -192,10 +192,6 @@ EVENTHANDLER_DECLARE(vm_lowmem, vm_lowme typedef void (*mountroot_handler_t)(void *); EVENTHANDLER_DECLARE(mountroot, mountroot_handler_t); -/* Going multiuser (starting pid 1) event */ -typedef void (*multiuser_handler_t)(void *); -EVENTHANDLER_DECLARE(multiuser, multiuser_handler_t); - /* File system mount events */ struct mount; struct vnode;