Module Name:    src
Committed By:   martin
Date:           Wed Oct  9 13:04:17 UTC 2024

Modified Files:
        src/sys/dev [netbsd-10]: random.c
        src/sys/kern [netbsd-10]: kern_entropy.c
        src/sys/sys [netbsd-10]: entropy.h

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #937):

        sys/dev/random.c: revision 1.11
        sys/kern/kern_entropy.c: revision 1.67
        sys/kern/kern_entropy.c: revision 1.68
        sys/kern/kern_entropy.c: revision 1.69
        sys/sys/entropy.h: revision 1.5

entropy(9): New function entropy_consolidate_sig.

This is the same as entropy_consolidate, but it returns EINTR if
interrupted by a signal and 0 otherwise.  (entropy_consolidate can
already be interrupted by a signal -- it just doesn't tell you if it
was.)

Eventually these will be merged into a single entropy_consolidate
that returns the error code, but adding a new symbol first makes it
safe for pullup-10.
PR kern/58646: /dev/random, kern.entropy.*: signal bugs

/dev/random: Fix two signal bugs.
1. If a long write to /dev/random is interrupted by a signal, it may
   proceed to sleep on the entropy source lock instead of returning
   promptly.
   => Don't try to consolidate entropy if we've already been
      interrupted by a signal.
2. If a write to /dev/random is interrupted by a signal while
   sleeping on the entropy source lock, it may fail to report EINTR.
   => Pass through EINTR from entropy consolidation via new
      entropy_consolidate_sig function.
PR kern/58646: /dev/random, kern.entropy.*: signal bugs

kern.entropy.consolidate, ioctl(RNDCTL): Fail with EINTR on signal.

This can happen if another thread is currently running consolidation
and has the entropy source lock held.  Use the new function
entropy_consolidate_sig to get at EINTR.
PR kern/58646: /dev/random, kern.entropy.*: signal bugs

kern.entropy.gather: Fail with EINTR on signal.

Just don't throw away the error code we already have!
PR kern/58646: /dev/random, kern.entropy.*: signal bugs


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.10.4.1 src/sys/dev/random.c
cvs rdiff -u -r1.57.4.4 -r1.57.4.5 src/sys/kern/kern_entropy.c
cvs rdiff -u -r1.4 -r1.4.20.1 src/sys/sys/entropy.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/random.c
diff -u src/sys/dev/random.c:1.10 src/sys/dev/random.c:1.10.4.1
--- src/sys/dev/random.c:1.10	Tue Dec 28 13:22:43 2021
+++ src/sys/dev/random.c	Wed Oct  9 13:04:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: random.c,v 1.10 2021/12/28 13:22:43 riastradh Exp $	*/
+/*	$NetBSD: random.c,v 1.10.4.1 2024/10/09 13:04:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: random.c,v 1.10 2021/12/28 13:22:43 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: random.c,v 1.10.4.1 2024/10/09 13:04:16 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -298,8 +298,8 @@ random_write(dev_t dev, struct uio *uio,
 	kmem_free(buf, RANDOM_BUFSIZE);
 
 	/* If we added anything, consolidate entropy now.  */
-	if (any)
-		entropy_consolidate();
+	if (any && error == 0)
+		error = entropy_consolidate_sig();
 
 	return error;
 }

Index: src/sys/kern/kern_entropy.c
diff -u src/sys/kern/kern_entropy.c:1.57.4.4 src/sys/kern/kern_entropy.c:1.57.4.5
--- src/sys/kern/kern_entropy.c:1.57.4.4	Fri Aug 11 14:35:25 2023
+++ src/sys/kern/kern_entropy.c	Wed Oct  9 13:04:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_entropy.c,v 1.57.4.4 2023/08/11 14:35:25 martin Exp $	*/
+/*	$NetBSD: kern_entropy.c,v 1.57.4.5 2024/10/09 13:04:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -77,7 +77,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.57.4.4 2023/08/11 14:35:25 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_entropy.c,v 1.57.4.5 2024/10/09 13:04:16 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -1354,7 +1354,21 @@ entropy_notify(void)
 /*
  * entropy_consolidate()
  *
- *	Trigger entropy consolidation and wait for it to complete.
+ *	Trigger entropy consolidation and wait for it to complete, or
+ *	return early if interrupted by a signal.
+ */
+void
+entropy_consolidate(void)
+{
+
+	(void)entropy_consolidate_sig();
+}
+
+/*
+ * entropy_consolidate_sig()
+ *
+ *	Trigger entropy consolidation and wait for it to complete, or
+ *	return EINTR if interrupted by a signal.
  *
  *	This should be used sparingly, not periodically -- requiring
  *	conscious intervention by the operator or a clear policy
@@ -1362,8 +1376,8 @@ entropy_notify(void)
  *	when enough entropy has been gathered into per-CPU pools to
  *	transition to full entropy.
  */
-void
-entropy_consolidate(void)
+int
+entropy_consolidate_sig(void)
 {
 	uint64_t ticket;
 	int error;
@@ -1381,6 +1395,8 @@ entropy_consolidate(void)
 			break;
 	}
 	mutex_exit(&E->lock);
+
+	return error;
 }
 
 /*
@@ -1404,7 +1420,7 @@ sysctl_entropy_consolidate(SYSCTLFN_ARGS
 	if (error || newp == NULL)
 		return error;
 	if (arg)
-		entropy_consolidate();
+		error = entropy_consolidate_sig();
 
 	return error;
 }
@@ -1434,7 +1450,7 @@ sysctl_entropy_gather(SYSCTLFN_ARGS)
 		mutex_exit(&E->lock);
 	}
 
-	return 0;
+	return error;
 }
 
 /*
@@ -2777,7 +2793,7 @@ entropy_ioctl(unsigned long cmd, void *d
 		/* Enter the data and consolidate entropy.  */
 		rnd_add_data(&seed_rndsource, rdata->data, rdata->len,
 		    entropybits);
-		entropy_consolidate();
+		error = entropy_consolidate_sig();
 		break;
 	}
 	default:

Index: src/sys/sys/entropy.h
diff -u src/sys/sys/entropy.h:1.4 src/sys/sys/entropy.h:1.4.20.1
--- src/sys/sys/entropy.h:1.4	Fri Aug 14 00:53:16 2020
+++ src/sys/sys/entropy.h	Wed Oct  9 13:04:17 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: entropy.h,v 1.4 2020/08/14 00:53:16 riastradh Exp $	*/
+/*	$NetBSD: entropy.h,v 1.4.20.1 2024/10/09 13:04:17 martin Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -50,6 +50,7 @@ struct knote;
 
 void	entropy_bootrequest(void);
 void	entropy_consolidate(void);
+int	entropy_consolidate_sig(void);
 unsigned entropy_epoch(void);
 bool	entropy_ready(void);
 int	entropy_extract(void *, size_t, int);

Reply via email to