Skip to contentSkip to conversation

hwrng: fix khwrng lifecycle

RFC PATCH In review Marek Vasut 3 participants 7 messages Oct 24, 2024 – Feb 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 hwrng: fix khwrng lifecycle

The khwrng can terminate also if the rng is removed, and in this case it doesn't synchronize with kthread_should_stop(), but it directly sets hwrng_fill to NULL. If this happens after the NULL check but before kthread_stop() is called, we'll have a NULL pointer dereference.

Keep the hwrng_fill thread around and only park it when unused, i.e. when there is no RNG available. Unpark it when RNG becomes available. This way, we are sure to never trigger the NULL pointer dereference.

---
This is a follow-up on first part of V2 of work by Luca Dariz:
https://lore.kernel.org/all/AM6PR06MB5400DAFE0551F1D468B728FBAB889@AM6PR06MB5400.eurprd06.prod.outlook.com/
---
Cc: Dominik Brodowski <[email protected]>
Cc: Harald Freudenberger <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Li Zhijian <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Olivia Mackall <[email protected]>
Cc: [email protected]

---
 drivers/char/hw_random/core.c | 71 ++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 26 deletions(-)
drivers/char/hw_random/core.c +45 -26
index 018316f546215..5be26f4e9d975 100644
@@ -87,15 +87,6 @@ static int set_current_rng(struct hwrng *rng)
 	drop_current_rng();
 	current_rng = rng;
 
-	/* if necessary, start hwrng thread */
-	if (!hwrng_fill) {
-		hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
-		if (IS_ERR(hwrng_fill)) {
-			pr_err("hwrng_fill thread creation failed\n");
-			hwrng_fill = NULL;
-		}
-	}
-
 	return 0;
 }
 
@@ -484,10 +475,17 @@ static int hwrng_fillfn(void *unused)
 	while (!kthread_should_stop()) {
 		unsigned short quality;
 		struct hwrng *rng;
+		if (kthread_should_park()) {
+			kthread_parkme();
+			continue;
+		}
 
 		rng = get_current_rng();
-		if (IS_ERR(rng) || !rng)
-			break;
+		if (IS_ERR(rng) || !rng) {
+			schedule();
+			continue;
+		}
+
 		mutex_lock(&reading_mutex);
 		rc = rng_get_data(rng, rng_fillbuf,
 				  rng_buffer_size(), 1);
@@ -515,12 +513,12 @@ static int hwrng_fillfn(void *unused)
 		add_hwgenerator_randomness((void *)rng_fillbuf, rc,
 					   entropy >> 10, true);
 	}
-	hwrng_fill = NULL;
 	return 0;
 }
 
 int hwrng_register(struct hwrng *rng)
 {
+	struct hwrng *old_rng;
 	int err = -EINVAL;
 	struct hwrng *tmp;
 
@@ -544,6 +542,7 @@ int hwrng_register(struct hwrng *rng)
 	/* Adjust quality field to always have a proper value */
 	rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024);
 
+	old_rng = current_rng;
 	if (!current_rng ||
 	    (!cur_rng_set_by_user && rng->quality > current_rng->quality)) {
 		/*
@@ -556,6 +555,10 @@ int hwrng_register(struct hwrng *rng)
 			goto out_unlock;
 	}
 	mutex_unlock(&rng_mutex);
+
+	if (!old_rng && current_rng)
+		kthread_unpark(hwrng_fill);
+
 	return 0;
 out_unlock:
 	mutex_unlock(&rng_mutex);
@@ -582,15 +585,12 @@ void hwrng_unregister(struct hwrng *rng)
 	}
 
 	new_rng = get_current_rng_nolock();
-	if (list_empty(&rng_list)) {
-		mutex_unlock(&rng_mutex);
-		if (hwrng_fill)
-			kthread_stop(hwrng_fill);
-	} else
-		mutex_unlock(&rng_mutex);
+	mutex_unlock(&rng_mutex);
 
 	if (new_rng)
 		put_rng(new_rng);
+	else
+		kthread_park(hwrng_fill);
 
 	wait_for_completion(&rng->cleanup_done);
 }
@@ -654,7 +654,7 @@ EXPORT_SYMBOL_GPL(hwrng_yield);
 
 static int __init hwrng_modinit(void)
 {
-	int ret;
+	int ret = -ENOMEM;
 
 	/* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
 	rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
@@ -662,22 +662,41 @@ static int __init hwrng_modinit(void)
 		return -ENOMEM;
 
 	rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
-	if (!rng_fillbuf) {
-		kfree(rng_buffer);
-		return -ENOMEM;
-	}
+	if (!rng_fillbuf)
+		goto err_fillbuf;
 
 	ret = misc_register(&rng_miscdev);
-	if (ret) {
-		kfree(rng_fillbuf);
-		kfree(rng_buffer);
+	if (ret)
+		goto err_miscdev;
+
+	hwrng_fill = kthread_create(hwrng_fillfn, NULL, "hwrng");
+	if (IS_ERR(hwrng_fill)) {
+		ret = PTR_ERR(hwrng_fill);
+		pr_err("hwrng_fill thread creation failed (%d)\n", ret);
+		goto err_kthread;
 	}
 
+	ret = kthread_park(hwrng_fill);
+	if (ret)
+		goto err_park;
+
+	return ret;
+
+err_park:
+	kthread_stop(hwrng_fill);
+err_kthread:
+	misc_deregister(&rng_miscdev);
+err_miscdev:
+	kfree(rng_buffer);
+err_fillbuf:
+	kfree(rng_buffer);
 	return ret;
 }
 
 static void __exit hwrng_modexit(void)
 {
+	kthread_stop(hwrng_fill);
+
 	mutex_lock(&rng_mutex);
 	BUG_ON(current_rng);
 	kfree(rng_buffer);
Signature · 1 line
2.45.2
#1 lore raw

RFC PATCH 2/2 hwrng: fix khwrng registration

If we have a register/unregister too fast, it can happen that the kthread is not yet started when kthread_stop is called, and this seems to leave a corrupted or uninitialized kthread struct. This is detected by the WARN_ON at kernel/kthread.c:75 and later causes a page domain fault.

Wait for the kthread to start the same way as drivers/base/devtmpfs.c does wait for kdevtmpfs thread to start using setup_done completion.

---
This is a follow-up on second part of V2 of work by Luca Dariz:
https://lore.kernel.org/all/AM6PR06MB5400DAFE0551F1D468B728FBAB889@AM6PR06MB5400.eurprd06.prod.outlook.com/
---
Cc: Dominik Brodowski <[email protected]>
Cc: Harald Freudenberger <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: Li Zhijian <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Olivia Mackall <[email protected]>
Cc: [email protected]

---
 drivers/char/hw_random/core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/char/hw_random/core.c +5 -1
index 5be26f4e9d975..bb1f4ba602b1d 100644
@@ -55,6 +55,7 @@ MODULE_PARM_DESC(default_quality,
 static void drop_current_rng(void);
 static int hwrng_init(struct hwrng *rng);
 static int hwrng_fillfn(void *unused);
+static DECLARE_COMPLETION(setup_done);
 
 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
 			       int wait);
@@ -472,6 +473,7 @@ static int hwrng_fillfn(void *unused)
 	size_t entropy, entropy_credit = 0; /* in 1/1024 of a bit */
 	long rc;
 
+	complete(&setup_done);
 	while (!kthread_should_stop()) {
 		unsigned short quality;
 		struct hwrng *rng;
@@ -669,13 +671,15 @@ static int __init hwrng_modinit(void)
 	if (ret)
 		goto err_miscdev;
 
-	hwrng_fill = kthread_create(hwrng_fillfn, NULL, "hwrng");
+	hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
 	if (IS_ERR(hwrng_fill)) {
 		ret = PTR_ERR(hwrng_fill);
 		pr_err("hwrng_fill thread creation failed (%d)\n", ret);
 		goto err_kthread;
 	}
 
+	wait_for_completion(&setup_done);
+
 	ret = kthread_park(hwrng_fill);
 	if (ret)
 		goto err_park;
Signature · 1 line
2.45.2
#2 lore raw
In reply to Marek Vasut

Hi Marek,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Marek-Vasut/hwrng-fix-khwrng-registration/20241025-003448
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20241024163121.246420-1-marex%40denx.de
patch subject: [PATCH 1/2] [RFC] hwrng: fix khwrng lifecycle
config: i386-randconfig-141-20241027 (https://download.01.org/0day-ci/archive/20241027/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/char/hw_random/core.c:685 hwrng_modinit() error: double free of 'rng_buffer'

vim +/rng_buffer +685 drivers/char/hw_random/core.c

ac3a497f13e42a Herbert Xu                 2014-12-23  648  static int __init hwrng_modinit(void)
ac3a497f13e42a Herbert Xu                 2014-12-23  649  {
78dd6dd163821d Marek Vasut                2024-10-24  650  	int ret = -ENOMEM;
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  651  
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  652  	/* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  653  	rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  654  	if (!rng_buffer)
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  655  		return -ENOMEM;
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  656  
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  657  	rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
78dd6dd163821d Marek Vasut                2024-10-24  658  	if (!rng_fillbuf)
78dd6dd163821d Marek Vasut                2024-10-24  659  		goto err_fillbuf;
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  660  
df1e9791998a92 Masahiro Yamada            2024-07-06  661  	ret = misc_register(&rng_miscdev);
78dd6dd163821d Marek Vasut                2024-10-24  662  	if (ret)
78dd6dd163821d Marek Vasut                2024-10-24  663  		goto err_miscdev;
78dd6dd163821d Marek Vasut                2024-10-24  664  
78dd6dd163821d Marek Vasut                2024-10-24  665  	hwrng_fill = kthread_create(hwrng_fillfn, NULL, "hwrng");
78dd6dd163821d Marek Vasut                2024-10-24  666  	if (IS_ERR(hwrng_fill)) {
78dd6dd163821d Marek Vasut                2024-10-24  667  		ret = PTR_ERR(hwrng_fill);
78dd6dd163821d Marek Vasut                2024-10-24  668  		pr_err("hwrng_fill thread creation failed (%d)\n", ret);
78dd6dd163821d Marek Vasut                2024-10-24  669  		goto err_kthread;
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  670  	}
58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  671  
78dd6dd163821d Marek Vasut                2024-10-24  672  	ret = kthread_park(hwrng_fill);
78dd6dd163821d Marek Vasut                2024-10-24  673  	if (ret)
78dd6dd163821d Marek Vasut                2024-10-24  674  		goto err_park;
78dd6dd163821d Marek Vasut                2024-10-24  675  
78dd6dd163821d Marek Vasut                2024-10-24  676  	return ret;
78dd6dd163821d Marek Vasut                2024-10-24  677  
78dd6dd163821d Marek Vasut                2024-10-24  678  err_park:
78dd6dd163821d Marek Vasut                2024-10-24  679  	kthread_stop(hwrng_fill);
78dd6dd163821d Marek Vasut                2024-10-24  680  err_kthread:
78dd6dd163821d Marek Vasut                2024-10-24  681  	misc_deregister(&rng_miscdev);
78dd6dd163821d Marek Vasut                2024-10-24  682  err_miscdev:
78dd6dd163821d Marek Vasut                2024-10-24  683  	kfree(rng_buffer);
                                                                      ^^^^^^^^^^
78dd6dd163821d Marek Vasut                2024-10-24  684  err_fillbuf:
78dd6dd163821d Marek Vasut                2024-10-24 @685  	kfree(rng_buffer);
                                                                      ^^^^^^^^^^

Double free.

58b022acac8dd4 PrasannaKumar Muralidharan 2016-09-07  686  	return ret;
ac3a497f13e42a Herbert Xu                 2014-12-23  687  }
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
Quoted 1 line · Dan Carpenter

Hi Marek,

Hi,

Quoted 12 lines

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Marek-Vasut/hwrng-fix-khwrng-registration/20241025-003448
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20241024163121.246420-1-marex%40denx.de
patch subject: [PATCH 1/2] [RFC] hwrng: fix khwrng lifecycle
config: i386-randconfig-141-20241027 (https://download.01.org/0day-ci/archive/20241027/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
Quoted 8 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

smatch warnings:
drivers/char/hw_random/core.c:685 hwrng_modinit() error: double free of 'rng_buffer'

Indeed, one of them should be rng_fillbuf instead of rng_buffer.

Fixed for V2, thank you.

maintainer
#4 lore raw
In reply to Marek Vasut
Quoted 7 lines · Marek Vasut
drivers/char/hw_random/core.c +0 -2
@@ -582,15 +585,12 @@ void hwrng_unregister(struct hwrng *rng)
 	}
 
 	new_rng = get_current_rng_nolock();
-	if (list_empty(&rng_list)) {
-		mutex_unlock(&rng_mutex);

drivers/char/hw_random/core.c void hwrng_unregister(struct hwrng *rng) · line 593

Quoted 10 lines
drivers/char/hw_random/core.c +3 -4
-		if (hwrng_fill)
-			kthread_stop(hwrng_fill);
-	} else
-		mutex_unlock(&rng_mutex);
+	mutex_unlock(&rng_mutex);
 
 	if (new_rng)
 		put_rng(new_rng);
+	else
+		kthread_park(hwrng_fill);

The kthread_park should be moved back into the locked region of rng_mute). The kthread_stop was moved out because it could dead-lock waiting on the kthread that's also taking the same lock. This is no longer an issue with kthread_park since it simply sets a flag.

Having it outside of the locked region is potentially dangerous since a pair of hwrng_unregister and hwrng_register could be re-ordered resulting in the kthread being parked forever.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw
Quoted 25 lines · Herbert Xu
Quoted 7 lines · Marek Vasut
drivers/char/hw_random/core.c +0 -2
@@ -582,15 +585,12 @@ void hwrng_unregister(struct hwrng *rng)
  	}
 
  	new_rng = get_current_rng_nolock();
-	if (list_empty(&rng_list)) {
-		mutex_unlock(&rng_mutex);

drivers/char/hw_random/core.c void hwrng_unregister(struct hwrng *rng) · line 593

Quoted 10 lines
drivers/char/hw_random/core.c +3 -4
-		if (hwrng_fill)
-			kthread_stop(hwrng_fill);
-	} else
-		mutex_unlock(&rng_mutex);
+	mutex_unlock(&rng_mutex);
 
  	if (new_rng)
  		put_rng(new_rng);
+	else
+		kthread_park(hwrng_fill);

The kthread_park should be moved back into the locked region of rng_mute). The kthread_stop was moved out because it could dead-lock waiting on the kthread that's also taking the same lock. This is no longer an issue with kthread_park since it simply sets a flag.

Quoted 3 lines

Having it outside of the locked region is potentially dangerous since a pair of hwrng_unregister and hwrng_register could be re-ordered resulting in the kthread being parked forever.

Sorry for the late reply.

I'm afraid this problem is still present, since kthread_park() synchronously waits for the kthread to call kthread_parkme(), see kernel/kthread.c :

655 int kthread_park(struct task_struct *k)
656 {
...
665 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
666 if (k != current) {
667 wake_up_process(k);
668 /*
669 * Wait for __kthread_parkme() to complete(), this
means we
670 * _will_ have TASK_PARKED and are about to call
schedule().
671 */
672 wait_for_completion(&kthread->parked);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This part .

The hwrng_fillfn() may call put_rng() which locks rng_mutex() for a short time, and if kthread_park() is called before hwrng_fillfn() calls put_rng() within a section protected by rng_mutex too, put_rng() could never claim rng_mutex and hwrng_fillfn() can never reach kthread_parkme() call, causing a deadlock.

maintainer
#6 lore raw
Quoted 3 lines · Marek Vasut

I'm afraid this problem is still present, since kthread_park() synchronously waits for the kthread to call kthread_parkme(), see kernel/kthread.c :

Fair enough. But we still need a solution to the potential reordering between kthread_park and kthread_unpark. Any ideas?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt