Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 042A8C433EF for ; Mon, 10 Jan 2022 21:38:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244307AbiAJViu (ORCPT ); Mon, 10 Jan 2022 16:38:50 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:55404 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244498AbiAJViu (ORCPT ); Mon, 10 Jan 2022 16:38:50 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A052861470; Mon, 10 Jan 2022 21:38:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C4ECC36AEF; Mon, 10 Jan 2022 21:38:46 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="nGSXNiOY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1641850724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/ZvI34k5qopJ7LeVmPLQAXBYV6MHZ7fIrRCNdpRkt04=; b=nGSXNiOYP3xPcCFVhh+vex4wGgo61Z13xqOTxD+n+OXf3aRgZzDoKF11qBhecKxQrZ90RU CZxOYW+4nY2Wi61BstCVT9bIka0dL5Wd6/9dh4fxPW8vscFIfc+q5VoPaq6wJ8moj75fcQ u7F+z/UiWjBmqZXHGC+mDsNLGm1zMB8= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 9a7e174a (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Mon, 10 Jan 2022 21:38:44 +0000 (UTC) Date: Mon, 10 Jan 2022 22:38:38 +0100 From: "Jason A. Donenfeld" To: Marcelo Henrique Cerri , Simo Sorce Cc: Theodore Ts'o , Greg Kroah-Hartman , Jeffrey Walton , Stephan Mueller , Linux Crypto Mailing List , Willy Tarreau , Nicolai Stange , LKML , Arnd Bergmann , "Eric W. Biederman" , "Alexander E. Patrakov" , "Ahmed S. Darwish" , Matthew Garrett , Vito Caputo , Andreas Dilger , Jan Kara , Ray Strode , William Jon McCann , zhangjs , Andy Lutomirski , Florian Weimer , Lennart Poettering , Peter Matthias , Neil Horman , Randy Dunlap , Julia Lawall , Dan Carpenter , Andy Lavr , Petr Tesarik , John Haxby , Alexander Lobakin , Jirka Hladky , Eric Biggers Subject: Re: [PATCH v43 01/15] Linux Random Number Generator Message-ID: References: <20211210014337.xmin2lu5rhhe3b3t@valinor> <20220110132349.siplwka7yhe2tmwc@valinor> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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 . 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 #include #include #include #include #include #include #include #include #include #include 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; }