Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Oct 2013 06:54:52 +0000 (UTC)
From:      Mark Murray <markm@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r256135 - in projects/random_number_generator/sys: dev/random kern sys
Message-ID:  <201310080654.r986sqBx018892@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <sys/cdefs.h>
@@ -46,6 +45,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/unistd.h>
 
 #include <machine/cpu.h>
+#include <machine/vmparam.h>
 
 #include <dev/random/randomdev.h>
 #include <dev/random/randomdev_soft.h>
@@ -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 <dev/random/rwfile.h>
 
 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;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201310080654.r986sqBx018892>