Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE9963C13E0; Sat, 17 Oct 2026 18:01:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792260073; cv=none; b=uxvGOEXdyYz2/gi/nE8WmEZyXlfrmXaCXbEW4Q4bSqPkpDTLfsVWdPC3jF8qopfEgAwVjGdLl1t9ytgjDdfghuVO9RMqCfsYscazt2RJkz4NG3gAaYmK6h05Ko6fXoD/habBvHP5o5DyAA5ZzriWiiyEwoNouybPZJzsjVMyTZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792260073; c=relaxed/simple; bh=AT0P7n4+xtTdhZJnbHDrBK58DW50kcl68CWhoubYChY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sVtbWlnOip1RA8+jRKZZIl68vBDaKBaZjjsTdQPSDQDnDImoZkb+EgmXSH9YvkpevVCQLaGjHG/TH+MMbZO1l9nct2VSXvkFYRUp7xNsmqcvbxIoRks/RCriGjMIVZ01CXDshyAMKSpYncG2ebqNCTal4qsc5ELd74Ili5LoGTg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=X9X+a9ke; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="X9X+a9ke" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ECC11F000C4; Sat, 17 Oct 2026 18:01:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1792260072; bh=DR0uKVnjbXXiUZK4DhF21id3nNDoLEGinlM36trRMBc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=X9X+a9kezuALrawOb3V7futez/Vkp9YkuOM4PT+kxOP4oBFgJ8Z1Xw3xiSQEf6Leg Mt1FzD+HXo/lgyy+BzL795/D5raI97knYrco20JHLFsQarse6eP38ugJz9XyaoHkxq ZR18jO5vZpOGC9JbCjnoi2uaNAyzZ3N5A7pnINHk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Aaron Tomlin , Petr Pavlu , Sasha Levin Subject: [PATCH 6.6 1425/1674] module/dups: Fix use-after-free in kmod_dup_req lifetime handling Date: Sat, 17 Oct 2026 18:01:37 +0200 Message-ID: <20261017160507.755435451@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261017160431.119607952@linuxfoundation.org> References: <20261017160431.119607952@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Petr Pavlu [ Upstream commit 5eecb11b543f9f417bcf0dea239ff99c6af65dbd ] The kmod dups code uses RCU to ensure that a kmod_dup_req instance is freed only after it is no longer referenced. When releasing an instance, the kmod_dup_request_delete() function removes the kmod_dup_req from the dup_kmod_reqs list, waits via synchronize_rcu() and finally frees it. However, this doesn't work correctly because parallel users referencing the instance in kmod_dup_request_exists_wait() don't enter an RCU read-side critical section. This can result in a use-after-free. The kmod_dup_request_exists_wait() function may need to hold a valid reference to a kmod_dup_req instance across a blocking wait until the corresponding modprobe command completes. This makes it unsuitable for RCU. Fix the issue by changing the lifecycle management of kmod_dup_req to use reference counting. Fixes: 8660484ed1cf ("module: add debugging auto-load duplicate module support") Reviewed-by: Aaron Tomlin Signed-off-by: Petr Pavlu Signed-off-by: Sasha Levin --- kernel/module/dups.c | 66 ++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/kernel/module/dups.c b/kernel/module/dups.c index f3d7ea1e96d82..019545cef7e4a 100644 --- a/kernel/module/dups.c +++ b/kernel/module/dups.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "internal.h" @@ -39,13 +40,12 @@ static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE); module_param(enable_dups_trace, bool_enable_only, 0644); -/* - * Protects dup_kmod_reqs list, adds / removals with RCU. - */ +/* A mutex-protected list of active kmod requests. */ static DEFINE_MUTEX(kmod_dup_mutex); static LIST_HEAD(dup_kmod_reqs); struct kmod_dup_req { + refcount_t refcount; struct list_head list; char name[MODULE_NAME_LEN]; struct completion first_req_done; @@ -54,12 +54,24 @@ struct kmod_dup_req { int dup_ret; }; +static void get_kmod_req(struct kmod_dup_req *kmod_req) +{ + refcount_inc(&kmod_req->refcount); +} + +static void put_kmod_req(struct kmod_dup_req *kmod_req) +{ + if (refcount_dec_and_test(&kmod_req->refcount)) + kfree(kmod_req); +} + static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name) { struct kmod_dup_req *kmod_req; - list_for_each_entry_rcu(kmod_req, &dup_kmod_reqs, list, - lockdep_is_held(&kmod_dup_mutex)) { + lockdep_assert_held(&kmod_dup_mutex); + + list_for_each_entry(kmod_req, &dup_kmod_reqs, list) { if (strlen(kmod_req->name) == strlen(module_name) && !memcmp(kmod_req->name, module_name, strlen(module_name))) { return kmod_req; @@ -88,10 +100,10 @@ static void kmod_dup_request_delete(struct work_struct *work) * just returning 0. */ mutex_lock(&kmod_dup_mutex); - list_del_rcu(&kmod_req->list); - synchronize_rcu(); + list_del(&kmod_req->list); mutex_unlock(&kmod_dup_mutex); - kfree(kmod_req); + + put_kmod_req(kmod_req); } static void kmod_dup_request_complete(struct work_struct *work) @@ -100,6 +112,9 @@ static void kmod_dup_request_complete(struct work_struct *work) kmod_req = container_of(work, struct kmod_dup_req, complete_work); + if (completion_done(&kmod_req->first_req_done)) + goto out; + /* * This will ensure that the kernel will let all the waiters get * informed its time to check the return value. It's time to @@ -115,6 +130,9 @@ static void kmod_dup_request_complete(struct work_struct *work) * possible abuses of vmalloc() incurred by finit_module() thrashing. */ queue_delayed_work(system_wq, &kmod_req->delete_work, 60 * HZ); + +out: + put_kmod_req(kmod_req); } bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) @@ -130,6 +148,7 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) if (!new_kmod_req) return false; + refcount_set(&new_kmod_req->refcount, 1); memcpy(new_kmod_req->name, module_name, strlen(module_name)); INIT_WORK(&new_kmod_req->complete_work, kmod_dup_request_complete); INIT_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete); @@ -162,10 +181,12 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) * keep tab on duplicates later. */ pr_debug("New request_module() for %s\n", module_name); - list_add_rcu(&new_kmod_req->list, &dup_kmod_reqs); + list_add(&new_kmod_req->list, &dup_kmod_reqs); mutex_unlock(&kmod_dup_mutex); return false; } + + get_kmod_req(kmod_req); mutex_unlock(&kmod_dup_mutex); /* We are dealing with a duplicate request now */ @@ -195,7 +216,7 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) * calls bail out right away. */ *dup_ret = 0; - return true; + goto out; } /* @@ -210,12 +231,14 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) TASK_UNINTERRUPTIBLE | TASK_KILLABLE); if (ret) { *dup_ret = ret; - return true; + goto out; } /* Now the duplicate request has the same exact return value as the first request */ *dup_ret = kmod_req->dup_ret; +out: + put_kmod_req(kmod_req); return true; } @@ -225,12 +248,23 @@ void kmod_dup_request_announce(char *module_name, int ret) mutex_lock(&kmod_dup_mutex); + /* + * Look for a kmod_dup_req previously added in + * kmod_dup_request_exists_wait(). Note that a request_module_nowait() + * without its own kmod_dup_req entry can announce a result of + * a concurrent request_module() call. + */ kmod_req = kmod_dup_request_lookup(module_name); - if (!kmod_req) - goto out; + if (!kmod_req || completion_done(&kmod_req->first_req_done)) { + mutex_unlock(&kmod_dup_mutex); + return; + } kmod_req->dup_ret = ret; + get_kmod_req(kmod_req); + mutex_unlock(&kmod_dup_mutex); + /* * If we complete() here we may allow duplicate threads * to continue before the first one that submitted the @@ -241,8 +275,6 @@ void kmod_dup_request_announce(char *module_name, int ret) * There is no rush. But we also don't want to hold the * caller up forever or introduce any boot delays. */ - queue_work(system_wq, &kmod_req->complete_work); - -out: - mutex_unlock(&kmod_dup_mutex); + if (!queue_work(system_wq, &kmod_req->complete_work)) + put_kmod_req(kmod_req); } -- 2.53.0