From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D31A3353EDF for ; Thu, 23 Jul 2026 08:26:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784795210; cv=none; b=j0byu7OrtHLcccUxmSiY2kXFh431odjh3wCbT2XGQDAeyB9kaW62IKpIRpTSezXQGCcJh3XB1b28NWmXOm3MogWumk7awvXM0DsvqMEQ/jDZYKFVdTsU+0qq7kHsv7Y3vnQeFlwg6Ubhg2aREew7lILsZej0R2g/f6ivADNqQEw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784795210; c=relaxed/simple; bh=49xIm8JVVxA1IO8RnKUTcUahhuvMP7ZhANoSnO5slgk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lPGKFMz1tiHfuALB47o2jNw4BAHUf72IXBGsSDFbLDRzGD/Ts5bFAC6FOxFGPCsVGWOSdI/PrDCrEFjWVoWnpihwPO9QbzcYSbtg2NZIIX+nJBD7BtUKK1DfnsCcYag4KFudfJtU+Umh4OBsympTl+EKAc6T3mZNG8at+LyFLgo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=DKFxVLJW; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="DKFxVLJW" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4954f5e8020so1553035e9.2 for ; Thu, 23 Jul 2026 01:26:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1784795203; x=1785400003; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ejMHszX4krSpU2meGRfcTBBnbHvAJ6f3bfAgNnoNb/s=; b=DKFxVLJWHa4Zxp0ys2bXvnrD/jpB8tTc98fsGB2pnuma1BDu7lD7Rmnn6F1Ns7Mu/x 6/h6KFiGbzUPSIK+wNaGn19hAInNIpubk0kSIW1Dg8Bw7kPABEfeIUuq4s3A5XAJqwd6 dvf2Oa1/v0VbgZMnwg5dJD/VHzR5u+nTJnFQy1CERGzhKs3KgCYtDCUB6+OnRfaZsorZ 3jDJXNqk50dYAjVeTEJLsHWR2/qKrHaFYO5cJgYZxBmtFajFtzipc4iUaecQU+bqWT8D uzNey9WVaFtWMALwa5R+biIvmYbk/ME0Rl+k96nqf2BZ/ItFYzDg8/rR6OVPf2VYT94U 1V1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784795203; x=1785400003; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ejMHszX4krSpU2meGRfcTBBnbHvAJ6f3bfAgNnoNb/s=; b=ph3OM//MOaht4P3q2n2GVHhL0Ct/jui47RJK6Vf0IQq4mbk0ydMIN6t8js//FCHGT6 cRYnSvHrWOi83l+IKHnuwl2GOejJ+xlLv/6NeJO8XTymcUxJyimUCKonJ4EUXpHr8FXy lHSa5KY3HG71kqBY9hsmcCJsiuXKLf5bsXNPAY56j9dF91KixY+A4XPF/6aKXC4w9Fvf 1+Aecbs426/AygEBMmj1Kyhg7ULaiaCu2qvxCijPXgW6h+56esJG7M7Bpx/s7w/lAKSW cZFVqfIaFwbvO4DOSGgqv89C7/D5ifSByeMCwGaZ/pt5wGDUDcBtj3lr67HIl4XLX8k9 M4cw== X-Forwarded-Encrypted: i=1; AHgh+RqrVvL3K9V9thf7pXR5tib4G/fG/bhVWXOEal0IlHblRLgV7Z+Ua0hJ+hgKepdCPToXVTSpQD8GpbvvJkg=@vger.kernel.org X-Gm-Message-State: AOJu0YwZxVS3xdy2aUv9mFRT/5P4D1Q9pwAtutNHx2yV095fAzHtfrRo ZarUNOU3hd+Zwptjy3ky0jHmFzq4pAwO6/fWEjrbwBg4filvg4gmJ4IuD2tlq3iOguY= X-Gm-Gg: AR+sD123sY0/yd6PaM9SZfIUw0SXmzjSCxBCCDJjIBiDsiNY6jdpbi7T4M2EbZLNJkX q2YC9BqIb5QeMSnW+G6nlhYfBla7aAgzeNyKDrwL4Qr5XQa7Tx/viHDn4TmSd9EARnfn5tL5G5m FcHDPP1Prwm+NICl5Ayh24FzFLQ/jPEjTpUQ03vw4dRuzcoTLBw9CHYNaXfcufM4UwCNm1pABmm EIH+H0w8ANqCh04r50AJnOiY6QSsqRLhQTgFaXGQRi+iE5aZHy1B/UwgZP3i/Sxdemoh6vVKQve Je33jBrzWG26cmWi1S27Ow81vD2AE5AYoJhhuCs0iPF6RvBDEKwEvEgU0vChRb/xAcus6Z30nS9 4xCsvesQKg1rwCdJMqOq8zCv474BVnFme/6rztB9W/XFykL6oGlJijr8mT5VydBFjnaXhJsiMrA == X-Received: by 2002:a05:600c:a0a:b0:495:4dea:f7f5 with SMTP id 5b1f17b1804b1-49573ce968emr25120595e9.29.1784795202251; Thu, 23 Jul 2026 01:26:42 -0700 (PDT) Received: from zovi.suse.cz ([2001:af0:8000:1409:193:86:92:181]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4956a4f9a1csm124831645e9.5.2026.07.23.01.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jul 2026 01:26:41 -0700 (PDT) From: Petr Pavlu To: Luis Chamberlain , Petr Pavlu , Daniel Gomez , Sami Tolvanen Cc: Aaron Tomlin , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/6] module/dups: Fix use-after-free in kmod_dup_req lifetime handling Date: Thu, 23 Jul 2026 10:25:52 +0200 Message-ID: <20260723-module-stats-rcu-v2-2-bd582261fae3@suse.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260723-module-stats-rcu-v2-0-bd582261fae3@suse.com> References: <20260723-module-stats-rcu-v2-0-bd582261fae3@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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") Signed-off-by: Petr Pavlu --- kernel/module/dups.c | 56 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/kernel/module/dups.c b/kernel/module/dups.c index b063bc423aa8..99661236490a 100644 --- a/kernel/module/dups.c +++ b/kernel/module/dups.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "internal.h" @@ -38,13 +39,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; @@ -52,12 +52,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; @@ -86,10 +98,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); } bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) @@ -105,6 +117,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_DELAYED_WORK(&new_kmod_req->delete_work, kmod_dup_request_delete); init_completion(&new_kmod_req->first_req_done); @@ -136,10 +149,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 */ @@ -169,7 +184,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; } /* @@ -184,12 +199,14 @@ bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret) 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; } @@ -199,15 +216,25 @@ 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; /* Inform all duplicate waiters to check the return value. */ complete_all(&kmod_req->first_req_done); + mutex_unlock(&kmod_dup_mutex); + /* * Now that we have allowed prior request_module() calls to go on * with life, let's schedule deleting this entry. We don't have @@ -216,7 +243,4 @@ void kmod_dup_request_announce(char *module_name, int ret) * possible abuses of vmalloc() incurred by finit_module() thrashing. */ queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ); - -out: - mutex_unlock(&kmod_dup_mutex); } -- 2.54.0