Re: [PATCH v43 01/15] Linux Random Number Generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>. 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;
}



[Index of Archives]     [Kernel]     [Gnu Classpath]     [Gnu Crypto]     [DM Crypt]     [Netfilter]     [Bugtraq]

  Powered by Linux