Date: Wed, 1 Apr 2015 13:36:28 +0200 From: Mateusz Guzik <mjg@freebsd.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r280955 - in head/sys: modules/notrandom dev/notrandom Message-ID: <20150401113628.GA16649@dft-labs.eu>
next in thread | raw e-mail | index | archive | help
Author: mjg Date: Wed Apr 1 13:37:00 2015 New Revision: 280955 URL: https://svnweb.freebsd.org/changeset/base/280955 Log: Add /dev/notrandom notrandom provides fast and reliable not random numbers. This was added in an effort to increase feature-compatiblity with Solaris 10. See http://www.brendangregg.com/Specials/notrandom.c for Solaris implementation. Reviewed-by: Bruce Schneier (ok, not really) MFC after: 1 week Added: head/sys/modules/notrandom/Makefile head/sys/dev/notrandom/notrandom.c Added: sys/dev/notrandom/notrandom.c =================================================================== --- head/sys/dev/notrandom/notrandom.c (revision 0) +++ head/sys/dev/notrandom/notrandom.c (working copy) @@ -0,0 +1,126 @@ +/*- + * Copyright (c) 2015 Mateusz Guzik + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/conf.h> +#include <sys/uio.h> +#include <sys/kernel.h> +#include <sys/malloc.h> +#include <sys/module.h> +#include <sys/filio.h> + +static struct cdev *notrandom_dev; + +static d_ioctl_t notrandom_ioctl; +static d_read_t notrandom_read; + +static struct cdevsw notrandom_cdevsw = { + .d_version = D_VERSION, + .d_read = notrandom_read, + .d_ioctl = notrandom_ioctl, + .d_name = "notrandom", + .d_flags = D_MMAP_ANON, +}; + +static char notrandom_buf[1024]; + +static int +notrandom_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused, + int flags __unused, struct thread *td) +{ + int error = 0; + + switch (cmd) { + case FIONBIO: + break; + case FIOASYNC: + if (*(int *)data != 0) + error = EINVAL; + break; + default: + error = ENOIOCTL; + } + return (error); +} + + +/* ARGSUSED */ +static int +notrandom_read(struct cdev *dev __unused, struct uio *uio, int flags __unused) +{ + ssize_t len; + int error = 0; + + while (uio->uio_resid > 0 && error == 0) { + len = uio->uio_resid; + if (len > sizeof(notrandom_buf)) + len = sizeof(notrandom_buf); + error = uiomove(notrandom_buf, len, uio); + } + + return (error); +} + +/* ARGSUSED */ +static int +notrandom_modevent(module_t mod __unused, int type, void *data __unused) +{ + int error = 0; + + switch(type) { + case MOD_LOAD: + memset(notrandom_buf, 7, sizeof(notrandom_buf)); + notrandom_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, + ¬random_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0666, + "notrandom"); + break; + + case MOD_UNLOAD: + destroy_dev(notrandom_dev); + /* + * Trash notrandom region so that the content cannot be + * retrieved. Better safe than sorry. + */ + memset(notrandom_buf, 123, sizeof(notrandom_buf)); + break; + + case MOD_SHUTDOWN: + break; + + default: + error = EOPNOTSUPP; + } + + return (error); +} + +DEV_MODULE(notrandom, notrandom_modevent, NULL); +MODULE_VERSION(notrandom, 1); Property changes on: head/sys/dev/notrandom/notrandom.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: sys/modules/notrandom/Makefile =================================================================== --- head/sys/modules/notrandom/Makefile (revision 0) +++ head/sys/modules/notrandom/Makefile (working copy) @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../dev/notrandom + +KMOD= notrandom +SRCS= notrandom.c + +.include <bsd.kmod.mk> Property changes on: head/sys/modules/notrandom/Makefile ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +FreeBSD=%H \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20150401113628.GA16649>