Module Name: src Committed By: riastradh Date: Wed May 24 20:22:23 UTC 2023
Modified Files: src/sys/kern: kern_entropy.c Log Message: entropy(9): Avoid race between rnd_add_data and ioctl(RNDCTL). XXX pullup-10 To generate a diff of this commit: cvs rdiff -u -r1.60 -r1.61 src/sys/kern/kern_entropy.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/kern_entropy.c diff -u src/sys/kern/kern_entropy.c:1.60 src/sys/kern/kern_entropy.c:1.61 --- src/sys/kern/kern_entropy.c:1.60 Wed May 24 20:22:12 2023 +++ src/sys/kern/kern_entropy.c Wed May 24 20:22:23 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_entropy.c,v 1.60 2023/05/24 20:22:12 riastradh Exp $ */ +/* $NetBSD: kern_entropy.c,v 1.61 2023/05/24 20:22:23 riastradh Exp $ */ /*- * Copyright (c) 2019 The NetBSD Foundation, Inc. @@ -75,7 +75,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.60 2023/05/24 20:22:12 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.61 2023/05/24 20:22:23 riastradh Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -1910,6 +1910,14 @@ rnd_add_data(struct krndsource *rs, cons return; } + /* + * Hold up the reset xcall before it zeroes the entropy counts + * on this CPU or globally. Otherwise, we might leave some + * nonzero entropy attributed to an untrusted source in the + * event of a race with a change to flags. + */ + kpreempt_disable(); + /* Load a snapshot of the flags. Ioctl may change them under us. */ flags = atomic_load_relaxed(&rs->flags); @@ -1922,7 +1930,7 @@ rnd_add_data(struct krndsource *rs, cons if (!atomic_load_relaxed(&entropy_collection) || ISSET(flags, RND_FLAG_NO_COLLECT) || !ISSET(flags, RND_FLAG_COLLECT_VALUE|RND_FLAG_COLLECT_TIME)) - return; + goto out; /* If asked, ignore the estimate. */ if (ISSET(flags, RND_FLAG_NO_ESTIMATE)) @@ -1939,6 +1947,9 @@ rnd_add_data(struct krndsource *rs, cons rnd_add_data_1(rs, &extra, sizeof extra, 0, RND_FLAG_COLLECT_TIME); } + +out: /* Allow concurrent changes to flags to finish. */ + kpreempt_enable(); } static unsigned