VYPR
Unrated severityNVD Advisory· Published May 27, 2026· Updated May 27, 2026

CVE-2026-45949

CVE-2026-45949

Description

In the Linux kernel, the following vulnerability has been resolved:

hwrng: core - use RCU and work_struct to fix race condition

Currently, hwrng_fill is not cleared until the hwrng_fillfn() thread exits. Since hwrng_unregister() reads hwrng_fill outside the rng_mutex lock, a concurrent hwrng_unregister() may call kthread_stop() again on the same task.

Additionally, if hwrng_unregister() is called immediately after hwrng_register(), the stopped thread may have never been executed. Thus, hwrng_fill remains dirty even after hwrng_unregister() returns. In this case, subsequent calls to hwrng_register() will fail to start new threads, and hwrng_unregister() will call kthread_stop() on the same freed task. In both cases, a use-after-free occurs:

refcount_t: addition on 0; use-after-free. WARNING: ... at lib/refcount.c:25 refcount_warn_saturate+0xec/0x1c0 Call Trace: kthread_stop+0x181/0x360 hwrng_unregister+0x288/0x380 virtrng_remove+0xe3/0x200

This patch fixes the race by protecting the global hwrng_fill pointer inside the rng_mutex lock, so that hwrng_fillfn() thread is stopped only once, and calls to kthread_run() and kthread_stop() are serialized with the lock held.

To avoid deadlock in hwrng_fillfn() while being stopped with the lock held, we convert current_rng to RCU, so that get_current_rng() can read current_rng without holding the lock. To remove the lock from put_rng(), we also delay the actual cleanup into a work_struct.

Since get_current_rng() no longer returns ERR_PTR values, the IS_ERR() checks are removed from its callers.

With hwrng_fill protected by the rng_mutex lock, hwrng_fillfn() can no longer clear hwrng_fill itself. Therefore, if hwrng_fillfn() returns directly after current_rng is dropped, kthread_stop() would be called on a freed task_struct later. To fix this, hwrng_fillfn() calls schedule() now to keep the task alive until being stopped. The kthread_stop() call is also moved from hwrng_unregister() to drop_current_rng(), ensuring kthread_stop() is called on all possible paths where current_rng becomes NULL, so that the thread would not wait forever.

AI Insight

LLM-synthesized narrative grounded in this CVE's description and references.

A race condition in the Linux kernel's hwrng core, due to unsynchronized access to the kthread task, can result in use-after-free vulnerability.

Vulnerability

A race condition exists in the Linux kernel's hardware random number generator (hwrng) core, prior to the fix introduced in the commit at the provided stable kernel tree link [1]. The issue arises because the global hwrng_fill pointer, which references a kernel thread (hwrng_fillfn()), is read outside the rng_mutex lock in hwrng_unregister(). This lack of synchronization allows a concurrent hwrng_unregister() to call kthread_stop() on the same task. Additionally, if hwrng_unregister() is called immediately after hwrng_register(), the thread may never have executed, leaving hwrng_fill dirty even after hwrng_unregister() returns. This causes subsequent hwrng_register() calls to fail to start new threads, and later hwrng_unregister() calls to trigger kthread_stop() on a freed task, resulting in a use-after-free vulnerability.

Exploitation

To exploit this vulnerability, an attacker would need to be able to trigger a specific sequence of calls to hwrng_register() and hwrng_unregister() in the Linux kernel. The race window is tight and requires concurrent or precisely timed operations, typically from a local user who can load and unload kernel modules, or from a system command that interacts with the hwrng subsystem. No special privileges beyond the ability to register/unregister hwrng devices are required; this can be achieved by loading/unloading a driver, such as virtrng, as shown in the provided call trace [1]. The exploitation sequence involves racing the unregistration of an hwrng device against the creation or cleanup of the associated kernel thread, leading to a use-after-free condition when kthread_stop() is called on an already freed task_struct.

Impact

Upon successful exploitation, an attacker can trigger a use-after-free condition on a kernel task structure. This can result in a denial of service (system crash due to kernel panic) or potentially allow privilege escalation if the freed memory is reallocated and controlled by the attacker, leading to arbitrary code execution at the kernel level. The warning message "refcount_t: addition on 0; use-after-free" and the call trace in the description indicate immediate system instability. The scope of the compromise includes accessing kernel memory and manipulating kernel structures, thus compromising system integrity and confidentiality.

Mitigation

The vulnerability is fixed in the Linux kernel by the commit referenced in the provided stable kernel tree link [1]. The fix protects the global hwrng_fill pointer inside the rng_mutex lock, serializes calls to kthread_run() and kthread_stop(), converts current_rng to RCU to avoid deadlock, and moves cleanup into a work_struct. Users should apply this kernel patch or update to a kernel version containing the fix. As of the publication date (2026-05-27), no workaround is available other than applying the patch. The CVE is not listed on the CISA Known Exploited Vulnerabilities (KEV) catalog based on the provided information.

AI Insight generated on May 27, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.

Affected products

1

Patches

8
dcf416eb88ea

hwrng: core - use RCU and work_struct to fix race condition

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.gitLianjie WangJan 29, 2026Fixed in 6.18.14via kernel-cna
4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 56d888bebe0c08..036de7294bbdab 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 56d888bebe0c08..036de7294bbdab 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
ad38f2cdfef9

hwrng: core - use RCU and work_struct to fix race condition

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.gitLianjie WangJan 29, 2026Fixed in 6.19.4via kernel-cna
4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
cc2f39d6ac48

hwrng: core - use RCU and work_struct to fix race condition

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.gitLianjie WangJan 29, 2026Fixed in 7.0via kernel-cna
4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
d5b7730f0699

hwrng: core - use RCU and work_struct to fix race condition

https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.gitLianjie WangJan 29, 2026Fixed in 6.12.75via kernel-cna
4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 7be3d504d8c014..c6e913497e1df9 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -206,10 +244,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -296,7 +330,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -314,7 +348,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -364,8 +400,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -409,8 +443,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -425,6 +457,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -441,12 +474,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -482,8 +516,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -511,14 +557,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -533,6 +578,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -540,16 +586,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -562,14 +611,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -577,17 +629,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -675,7 +717,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 7be3d504d8c014..c6e913497e1df9 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -206,10 +244,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -296,7 +330,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -314,7 +348,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -364,8 +400,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -409,8 +443,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -425,6 +457,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -441,12 +474,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -482,8 +516,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -511,14 +557,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -533,6 +578,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -540,16 +586,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -562,14 +611,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -577,17 +629,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -675,7 +717,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
dcf416eb88ea

hwrng: core - use RCU and work_struct to fix race condition

4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 56d888bebe0c08..036de7294bbdab 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 56d888bebe0c08..036de7294bbdab 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
cc2f39d6ac48

hwrng: core - use RCU and work_struct to fix race condition

4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
d5b7730f0699

hwrng: core - use RCU and work_struct to fix race condition

4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 7be3d504d8c014..c6e913497e1df9 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -206,10 +244,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -296,7 +330,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -314,7 +348,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -364,8 +400,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -409,8 +443,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -425,6 +457,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -441,12 +474,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -482,8 +516,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -511,14 +557,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -533,6 +578,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -540,16 +586,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -562,14 +611,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -577,17 +629,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -675,7 +717,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 7be3d504d8c014..c6e913497e1df9 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -206,10 +244,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -296,7 +330,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -314,7 +348,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -364,8 +400,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -409,8 +443,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -425,6 +457,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -441,12 +474,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -482,8 +516,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -511,14 +557,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -533,6 +578,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -540,16 +586,19 @@ 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);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -562,14 +611,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -577,17 +629,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -675,7 +717,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
ad38f2cdfef9

hwrng: core - use RCU and work_struct to fix race condition

4 files changed · +214 128
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • drivers/char/hw_random/core.c+105 63 modified
    diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
    index 96d7fe41b373d5..aba92d777f7260 100644
    --- a/drivers/char/hw_random/core.c
    +++ b/drivers/char/hw_random/core.c
    @@ -20,23 +20,25 @@
     #include <linux/miscdevice.h>
     #include <linux/module.h>
     #include <linux/random.h>
    +#include <linux/rcupdate.h>
     #include <linux/sched.h>
     #include <linux/sched/signal.h>
     #include <linux/slab.h>
     #include <linux/string.h>
     #include <linux/uaccess.h>
    +#include <linux/workqueue.h>
     
     #define RNG_MODULE_NAME		"hw_random"
     
     #define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
     
    -static struct hwrng *current_rng;
    +static struct hwrng __rcu *current_rng;
     /* the current rng has been explicitly chosen by user via sysfs */
     static int cur_rng_set_by_user;
     static struct task_struct *hwrng_fill;
     /* list of registered rngs */
     static LIST_HEAD(rng_list);
    -/* Protects rng_list and current_rng */
    +/* Protects rng_list, hwrng_fill and updating on current_rng */
     static DEFINE_MUTEX(rng_mutex);
     /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
     static DEFINE_MUTEX(reading_mutex);
    @@ -64,18 +66,39 @@ static size_t rng_buffer_size(void)
     	return RNG_BUFFER_SIZE;
     }
     
    -static inline void cleanup_rng(struct kref *kref)
    +static void cleanup_rng_work(struct work_struct *work)
     {
    -	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +	struct hwrng *rng = container_of(work, struct hwrng, cleanup_work);
    +
    +	/*
    +	 * Hold rng_mutex here so we serialize in case they set_current_rng
    +	 * on rng again immediately.
    +	 */
    +	mutex_lock(&rng_mutex);
    +
    +	/* Skip if rng has been reinitialized. */
    +	if (kref_read(&rng->ref)) {
    +		mutex_unlock(&rng_mutex);
    +		return;
    +	}
     
     	if (rng->cleanup)
     		rng->cleanup(rng);
     
     	complete(&rng->cleanup_done);
    +	mutex_unlock(&rng_mutex);
    +}
    +
    +static inline void cleanup_rng(struct kref *kref)
    +{
    +	struct hwrng *rng = container_of(kref, struct hwrng, ref);
    +
    +	schedule_work(&rng->cleanup_work);
     }
     
     static int set_current_rng(struct hwrng *rng)
     {
    +	struct hwrng *old_rng;
     	int err;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -84,8 +107,14 @@ static int set_current_rng(struct hwrng *rng)
     	if (err)
     		return err;
     
    -	drop_current_rng();
    -	current_rng = rng;
    +	old_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	rcu_assign_pointer(current_rng, rng);
    +
    +	if (old_rng) {
    +		synchronize_rcu();
    +		kref_put(&old_rng->ref, cleanup_rng);
    +	}
     
     	/* if necessary, start hwrng thread */
     	if (!hwrng_fill) {
    @@ -101,47 +130,56 @@ static int set_current_rng(struct hwrng *rng)
     
     static void drop_current_rng(void)
     {
    -	BUG_ON(!mutex_is_locked(&rng_mutex));
    -	if (!current_rng)
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (!rng)
     		return;
     
    +	RCU_INIT_POINTER(current_rng, NULL);
    +	synchronize_rcu();
    +
    +	if (hwrng_fill) {
    +		kthread_stop(hwrng_fill);
    +		hwrng_fill = NULL;
    +	}
    +
     	/* decrease last reference for triggering the cleanup */
    -	kref_put(&current_rng->ref, cleanup_rng);
    -	current_rng = NULL;
    +	kref_put(&rng->ref, cleanup_rng);
     }
     
    -/* Returns ERR_PTR(), NULL or refcounted hwrng */
    +/* Returns NULL or refcounted hwrng */
     static struct hwrng *get_current_rng_nolock(void)
     {
    -	if (current_rng)
    -		kref_get(&current_rng->ref);
    +	struct hwrng *rng;
    +
    +	rng = rcu_dereference_protected(current_rng,
    +					lockdep_is_held(&rng_mutex));
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	return current_rng;
    +	return rng;
     }
     
     static struct hwrng *get_current_rng(void)
     {
     	struct hwrng *rng;
     
    -	if (mutex_lock_interruptible(&rng_mutex))
    -		return ERR_PTR(-ERESTARTSYS);
    +	rcu_read_lock();
    +	rng = rcu_dereference(current_rng);
    +	if (rng)
    +		kref_get(&rng->ref);
     
    -	rng = get_current_rng_nolock();
    +	rcu_read_unlock();
     
    -	mutex_unlock(&rng_mutex);
     	return rng;
     }
     
     static void put_rng(struct hwrng *rng)
     {
    -	/*
    -	 * Hold rng_mutex here so we serialize in case they set_current_rng
    -	 * on rng again immediately.
    -	 */
    -	mutex_lock(&rng_mutex);
     	if (rng)
     		kref_put(&rng->ref, cleanup_rng);
    -	mutex_unlock(&rng_mutex);
     }
     
     static int hwrng_init(struct hwrng *rng)
    @@ -213,10 +251,6 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
     
     	while (size) {
     		rng = get_current_rng();
    -		if (IS_ERR(rng)) {
    -			err = PTR_ERR(rng);
    -			goto out;
    -		}
     		if (!rng) {
     			err = -ENODEV;
     			goto out;
    @@ -303,7 +337,7 @@ static struct miscdevice rng_miscdev = {
     
     static int enable_best_rng(void)
     {
    -	struct hwrng *rng, *new_rng = NULL;
    +	struct hwrng *rng, *cur_rng, *new_rng = NULL;
     	int ret = -ENODEV;
     
     	BUG_ON(!mutex_is_locked(&rng_mutex));
    @@ -321,7 +355,9 @@ static int enable_best_rng(void)
     			new_rng = rng;
     	}
     
    -	ret = ((new_rng == current_rng) ? 0 : set_current_rng(new_rng));
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	ret = ((new_rng == cur_rng) ? 0 : set_current_rng(new_rng));
     	if (!ret)
     		cur_rng_set_by_user = 0;
     
    @@ -371,8 +407,6 @@ static ssize_t rng_current_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	ret = sysfs_emit(buf, "%s\n", rng ? rng->name : "none");
     	put_rng(rng);
    @@ -416,8 +450,6 @@ static ssize_t rng_quality_show(struct device *dev,
     	struct hwrng *rng;
     
     	rng = get_current_rng();
    -	if (IS_ERR(rng))
    -		return PTR_ERR(rng);
     
     	if (!rng) /* no need to put_rng */
     		return -ENODEV;
    @@ -432,6 +464,7 @@ static ssize_t rng_quality_store(struct device *dev,
     				 struct device_attribute *attr,
     				 const char *buf, size_t len)
     {
    +	struct hwrng *rng;
     	u16 quality;
     	int ret = -EINVAL;
     
    @@ -448,12 +481,13 @@ static ssize_t rng_quality_store(struct device *dev,
     		goto out;
     	}
     
    -	if (!current_rng) {
    +	rng = rcu_dereference_protected(current_rng, lockdep_is_held(&rng_mutex));
    +	if (!rng) {
     		ret = -ENODEV;
     		goto out;
     	}
     
    -	current_rng->quality = quality;
    +	rng->quality = quality;
     	current_quality = quality; /* obsolete */
     
     	/* the best available RNG may have changed */
    @@ -489,8 +523,20 @@ static int hwrng_fillfn(void *unused)
     		struct hwrng *rng;
     
     		rng = get_current_rng();
    -		if (IS_ERR(rng) || !rng)
    +		if (!rng) {
    +			/*
    +			 * Keep the task_struct alive until kthread_stop()
    +			 * is called to avoid UAF in drop_current_rng().
    +			 */
    +			while (!kthread_should_stop()) {
    +				set_current_state(TASK_INTERRUPTIBLE);
    +				if (!kthread_should_stop())
    +					schedule();
    +			}
    +			set_current_state(TASK_RUNNING);
     			break;
    +		}
    +
     		mutex_lock(&reading_mutex);
     		rc = rng_get_data(rng, rng_fillbuf,
     				  rng_buffer_size(), 1);
    @@ -518,14 +564,13 @@ 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)
     {
     	int err = -EINVAL;
    -	struct hwrng *tmp;
    +	struct hwrng *cur_rng, *tmp;
     
     	if (!rng->name || (!rng->data_read && !rng->read))
     		goto out;
    @@ -540,6 +585,7 @@ int hwrng_register(struct hwrng *rng)
     	}
     	list_add_tail(&rng->list, &rng_list);
     
    +	INIT_WORK(&rng->cleanup_work, cleanup_rng_work);
     	init_completion(&rng->cleanup_done);
     	complete(&rng->cleanup_done);
     	init_completion(&rng->dying);
    @@ -547,16 +593,19 @@ int hwrng_register(struct hwrng *rng)
     	/* Adjust quality field to always have a proper value */
     	rng->quality = min3(default_quality, 1024, rng->quality ?: 1024);
     
    -	if (!cur_rng_set_by_user &&
    -	    (!current_rng || rng->quality > current_rng->quality)) {
    -		/*
    -		 * Set new rng as current as the new rng source
    -		 * provides better entropy quality and was not
    -		 * chosen by userspace.
    -		 */
    -		err = set_current_rng(rng);
    -		if (err)
    -			goto out_unlock;
    +	if (!cur_rng_set_by_user) {
    +		cur_rng = rcu_dereference_protected(current_rng,
    +						    lockdep_is_held(&rng_mutex));
    +		if (!cur_rng || rng->quality > cur_rng->quality) {
    +			/*
    +			 * Set new rng as current as the new rng source
    +			 * provides better entropy quality and was not
    +			 * chosen by userspace.
    +			 */
    +			err = set_current_rng(rng);
    +			if (err)
    +				goto out_unlock;
    +		}
     	}
     	mutex_unlock(&rng_mutex);
     	return 0;
    @@ -569,14 +618,17 @@ EXPORT_SYMBOL_GPL(hwrng_register);
     
     void hwrng_unregister(struct hwrng *rng)
     {
    -	struct hwrng *new_rng;
    +	struct hwrng *cur_rng;
     	int err;
     
     	mutex_lock(&rng_mutex);
     
     	list_del(&rng->list);
     	complete_all(&rng->dying);
    -	if (current_rng == rng) {
    +
    +	cur_rng = rcu_dereference_protected(current_rng,
    +					    lockdep_is_held(&rng_mutex));
    +	if (cur_rng == rng) {
     		err = enable_best_rng();
     		if (err) {
     			drop_current_rng();
    @@ -584,17 +636,7 @@ 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);
    -
    -	if (new_rng)
    -		put_rng(new_rng);
    -
    +	mutex_unlock(&rng_mutex);
     	wait_for_completion(&rng->cleanup_done);
     }
     EXPORT_SYMBOL_GPL(hwrng_unregister);
    @@ -682,7 +724,7 @@ static int __init hwrng_modinit(void)
     static void __exit hwrng_modexit(void)
     {
     	mutex_lock(&rng_mutex);
    -	BUG_ON(current_rng);
    +	WARN_ON(rcu_access_pointer(current_rng));
     	kfree(rng_buffer);
     	kfree(rng_fillbuf);
     	mutex_unlock(&rng_mutex);
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    
  • include/linux/hw_random.h+2 1 modified
    diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h
    index b424555753b11f..b77bc55a4cf356 100644
    --- a/include/linux/hw_random.h
    +++ b/include/linux/hw_random.h
    @@ -15,6 +15,7 @@
     #include <linux/completion.h>
     #include <linux/kref.h>
     #include <linux/types.h>
    +#include <linux/workqueue_types.h>
     
     /**
      * struct hwrng - Hardware Random Number Generator driver
    @@ -48,6 +49,7 @@ struct hwrng {
     	/* internal. */
     	struct list_head list;
     	struct kref ref;
    +	struct work_struct cleanup_work;
     	struct completion cleanup_done;
     	struct completion dying;
     };
    -- 
    cgit 1.3-korg
    
    
    

Vulnerability mechanics

Root cause

"Missing synchronization on the global hwrng_fill pointer allows concurrent hwrng_unregister() calls to call kthread_stop() multiple times on the same task, causing a use-after-free."

Attack vector

An attacker triggers the bug by causing concurrent `hwrng_unregister()` calls on the same hardware RNG device, or by calling `hwrng_unregister()` immediately after `hwrng_register()` so the fill thread never executes. Because `hwrng_unregister()` read `hwrng_fill` outside `rng_mutex`, a second concurrent call could call `kthread_stop()` again on the same task, leading to a use-after-free. The kernel warning shows the crash path via `kthread_stop` called from `hwrng_unregister` during `virtrng_remove` [patch_id=2661067].

Affected code

The vulnerability is in `drivers/char/hw_random/core.c` [patch_id=2661067]. The global `hwrng_fill` pointer was not cleared until the `hwrng_fillfn()` thread exited, and `hwrng_unregister()` read `hwrng_fill` outside the `rng_mutex` lock. The `current_rng` pointer was also accessed without proper synchronization, allowing concurrent `hwrng_unregister()` calls to call `kthread_stop()` multiple times on the same task.

What the fix does

The patch protects the global `hwrng_fill` pointer inside `rng_mutex` so that `kthread_stop()` and `kthread_run()` are serialized. It converts `current_rng` to an RCU-protected pointer (`struct hwrng __rcu *current_rng`) so that `get_current_rng()` can read it without holding the mutex, avoiding deadlock when `hwrng_fillfn()` is stopped. The actual cleanup is deferred into a `work_struct` (`cleanup_work`) to remove the lock from `put_rng()`. The `kthread_stop()` call is moved from `hwrng_unregister()` into `drop_current_rng()`, ensuring it is called on all paths where `current_rng` becomes NULL. The `hwrng_fillfn()` now calls `schedule()` to keep the task alive until `kthread_stop()` is called, preventing use-after-free on the task_struct [patch_id=2661067][patch_id=2661073].

Preconditions

  • inputAttacker must be able to trigger concurrent hwrng_unregister() calls on the same device, or call hwrng_unregister() immediately after hwrng_register()
  • configThe race requires the hwrng_fill thread to not have exited before the second unregister call reads hwrng_fill

Generated on May 27, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.

References

4

News mentions

0

No linked articles in our index yet.