[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YdynXjhhuQfbYuSb@zx2c4.com>
Date: Mon, 10 Jan 2022 22:38:38 +0100
From: "Jason A. Donenfeld" <Jason@...c4.com>
To: Marcelo Henrique Cerri <marcelo.cerri@...onical.com>,
Simo Sorce <simo@...hat.com>
Cc: Theodore Ts'o <tytso@....edu>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Jeffrey Walton <noloader@...il.com>,
Stephan Mueller <smueller@...onox.de>,
Linux Crypto Mailing List <linux-crypto@...r.kernel.org>,
Willy Tarreau <w@....eu>, Nicolai Stange <nstange@...e.de>,
LKML <linux-kernel@...r.kernel.org>,
Arnd Bergmann <arnd@...db.de>,
"Eric W. Biederman" <ebiederm@...ssion.com>,
"Alexander E. Patrakov" <patrakov@...il.com>,
"Ahmed S. Darwish" <darwish.07@...il.com>,
Matthew Garrett <mjg59@...f.ucam.org>,
Vito Caputo <vcaputo@...garu.com>,
Andreas Dilger <adilger.kernel@...ger.ca>,
Jan Kara <jack@...e.cz>, Ray Strode <rstrode@...hat.com>,
William Jon McCann <mccann@....edu>,
zhangjs <zachary@...shancloud.com>,
Andy Lutomirski <luto@...nel.org>,
Florian Weimer <fweimer@...hat.com>,
Lennart Poettering <mzxreary@...inter.de>,
Peter Matthias <matthias.peter@....bund.de>,
Neil Horman <nhorman@...hat.com>,
Randy Dunlap <rdunlap@...radead.org>,
Julia Lawall <julia.lawall@...ia.fr>,
Dan Carpenter <dan.carpenter@...cle.com>,
Andy Lavr <andy.lavr@...il.com>,
Petr Tesarik <ptesarik@...e.cz>,
John Haxby <john.haxby@...cle.com>,
Alexander Lobakin <alobakin@...lbox.org>,
Jirka Hladky <jhladky@...hat.com>,
Eric Biggers <ebiggers@...nel.org>
Subject: Re: [PATCH v43 01/15] Linux Random Number Generator
Just in case you were curious...
On Mon, Jan 10, 2022 at 07:44:23PM +0100, Jason A. Donenfeld wrote:
> (b) can be accomplished in userspace by just (i) disabling getrandom()
> (making it return ENOSYS), and then (ii) replacing the /dev/urandom
> path with a CUSE device or similar.
>
> I suppose (b.i) might be able to be done with some bpf seccomp cgroup
> situation. Or, if that's problematic, somebody could propose a
> "disable getrandom(2)" cmdline option. That doesn't seem very hard.
> And (b.ii) could use combined inputs from /dev/urandom and whatever
> FIPSy userspace jitter entropy daemon you have.
The below took all of 5 minutes to write. Should be easy to tweak this
for whatever flavors required.
====
/* Copyright (C) 2022 Jason A. Donenfeld <Jason@...c4.com>. All Rights Reserved.
*
* Usage:
* # gcc -O2 jrandom.c `pkg-config fuse3 --cflags --libs` -o jrandom
* # ./jrandom
* # chmod 666 /dev/jrandom
* # ln -sf jrandom /dev/urandom
* # ln -sf jrandom /dev/random
*/
#define FUSE_USE_VERSION 31
#include <cuse_lowlevel.h>
#include <fuse_opt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
static int rng;
static void fipsrng_open(fuse_req_t req, struct fuse_file_info *fi)
{
fuse_reply_open(req, fi);
}
static void fipsrng_read(fuse_req_t req, size_t size, off_t off,
struct fuse_file_info *fi)
{
char random[128];
ssize_t ret_bytes;
if (size > sizeof(random))
size = sizeof(random);
ret_bytes = read(rng, random, size);
if (ret_bytes < 0)
fuse_reply_err(req, errno);
else
fuse_reply_buf(req, random, ret_bytes);
}
static void fipsrng_write(fuse_req_t req, const char *buf, size_t size,
off_t off, struct fuse_file_info *fi)
{
/* Swallow it, we don't care. */
fuse_reply_write(req, size);
}
static void fipsrng_ioctl(fuse_req_t req, int cmd, void *arg,
struct fuse_file_info *fi, unsigned flags,
const void *in_buf, size_t in_bufsz, size_t out_bufsz)
{
/* TODO: implement the various RNG ioctls */
fuse_reply_err(req, ENOSYS);
}
static const struct cuse_lowlevel_ops fipsrng_clop = {
.open = fipsrng_open,
.read = fipsrng_read,
.write = fipsrng_write,
.ioctl = fipsrng_ioctl,
};
int main(int argc, char **argv)
{
static const struct sockaddr_alg sa = {
.salg_family = AF_ALG,
.salg_type = "rng",
.salg_name = "jitterentropy_rng"
};
static const char *dev_info_argv[] = { "DEVNAME=jrandom" };
static const struct cuse_info ci = {
.dev_info_argc = 1,
.dev_info_argv = dev_info_argv,
.flags = CUSE_UNRESTRICTED_IOCTL
};
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
int ret = 1, afalg;
if (fuse_opt_parse(&args, NULL, NULL, NULL)) {
fprintf(stderr, "failed to parse options\n");
goto out;
}
afalg = socket(AF_ALG, SOCK_SEQPACKET, 0);
if (afalg < 0) {
perror("socket(AF_ALG)");
goto out;
}
if (bind(afalg, (const struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("bind(\"rng\", \"jitterentropy_rng\")");
goto out;
}
rng = accept(afalg, NULL, 0);
if (rng < 0) {
perror("accept()");
goto out;
}
ret = cuse_lowlevel_main(args.argc, args.argv, &ci, &fipsrng_clop, NULL);
out:
fuse_opt_free_args(&args);
return ret;
}
Powered by blists - more mailing lists