From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793827211; b=FskPknaV3Td6b/2WtPNoMubYJlq0YCAjFGurNoz/DacoEtkBdg6S1xQhlaCrVCwI07Mc Usz0krKDmsj3g2ZeX/pzUZCdD0OKzW/SSrvpqGOaWc/tQYqnUBoVDRGnDsm6hmO8T/k/1 0ZgN0xJacDBjoX4FH/zvLU8AwryLnjmQqvvTP8OP5hJc+0aslRcuxJVxx/IXfVJReOUvK T9U43/GqOV4y6xbT5XMoko3XmDy0MWPCpg/zHeYsgfTDe52xpQS0FSMKkcGrIcBxQoby7 WVgzgPbW6Fu5/fsUX043NYFCHX5ewiMcOqVFfbJhbl3Fbk8HDTRP9WANN+8ebyvE5zA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793827211; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=a7vpgE73VaX27NTeZH0q2vZm5fQioMjA1FYspohCs78=; b=NDVbC/Q4C4i4QSxgoYD59+uDHFdIjnWKKuEhZtifpkOAuqy9bnRhOtsb1F1eGBcvRBSd yVUPr9/oYifxCtmdvEPtq0ncV216E5bdoo6bER4oawU03n8lFGmDXz0YxaO8dNRMpwx77 NO8on9iF8ccp16vlOTc0FJpceopRj0ZOfBuEddQ20/a1nBQKASDWdNmup5PYv9okUjRI6 +gdAYB2mLl1m30duSeUb41zeAhW/citeiesPloSU24XoqhVdKNwZPFzkTILrlfn7k6Pcl M2I/OdpNl49VJd4RhDGV1X/289ZXYyRpi1UgF75fu4CqTgcCqfmiamiaryWqv/8JanQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="VD/sSf3W"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A4DE606B39A; Wed, 4 Nov 2026 21:20:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=VD/sSf3W Received: by smtp.kernel.org (Postfix) with ESMTPSA id BBDBA1F00919; Wed, 4 Nov 2026 21:20:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1793827211; bh=a7vpgE73VaX27NTeZH0q2vZm5fQioMjA1FYspohCs78=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VD/sSf3W8efSuiT4qXXDHEBH7UBBkXmd43R++kiEVCn4PtHONv58MuXWu/IHN8PGl vt+/bd+Gm+UT0ryyu5L3V2iMFikcFyTjdJqX4eb56fDWFseycTAtTRVgjWaooUjWvd 3XqqASjA9xHjbD+cnmFTGm5qYxDtbBEvb9kfydJA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Danish Khateeb , Rodolfo Giometti Subject: [PATCH 7.3 259/547] pps: generators: dont use the drivers info after unregister Date: Wed, 4 Nov 2026 16:07:23 -0500 Message-ID: <20261104211045.543914359@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261104211038.406938024@linuxfoundation.org> References: <20261104211038.406938024@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Danish Khateeb commit 4e8063cae337cb7328e6856235cf4de5d165516e upstream. A file that was opened before pps_gen_unregister_source() keeps pps_gen, and with it the pointer to the driver's pps_gen_source_info. PPS_GEN_SETENABLE and PPS_GEN_USESYSTEMCLOCK keep using that pointer after the driver has gone: - pps_gen_tio allocates its info with devm_kzalloc(), so after an unbind both ioctls read freed memory, and SETENABLE calls the driver's enable() with its freed private data. - pps_gen-dummy does not set info->owner, so it can be unloaded while the file is open, and the ioctls then read the unloaded module's data and call its code. TIO can't probe in a VM, as it needs ART. A test driver that registers its info in devm memory like TIO does, unbound while /dev/pps-gen0 is open, gives: BUG: KASAN: slab-use-after-free in pps_gen_cdev_ioctl+0x4c1/0x580 Read of size 1 at addr ffff88800e647f38 by task ppsgen64/145 ... Freed by task 146: kfree+0x25a/0x6d0 release_nodes+0xd1/0x140 devres_release_all+0x10e/0x1a0 device_unbind_cleanup+0x71/0x250 device_release_driver_internal+0x41b/0x570 unbind_store+0xd9/0x100 and unloading pps_gen-dummy while its file is open oopses: BUG: unable to handle page fault for address: ffffffffa0203040 Oops: Oops: 0000 [#1] SMP KASAN NOPTI RIP: 0010:pps_gen_cdev_ioctl+0x372/0x580 Protect info in the ioctl handler with a mutex, and clear it under the mutex on unregister, so that unregister waits for the ioctls using it and later ones fail with -ENODEV. PPS_KC_BIND does the same for a removed PPS device since commit 3649f9a6b897 ("pps: don't allow PPS_KC_BIND on removed devices"). The sysfs attributes need nothing new: they are removed, and their callbacks drained, before info is cleared. Fixes: 86b525bed275 ("drivers pps: add PPS generators support") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Danish Khateeb Acked-by: Rodolfo Giometti Link: https://patch.msgid.link/20260929124937.51114-3-danishkhateeb03@gmail.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/pps/generators/pps_gen.c | 31 ++++++++++++++++++++++++++----- include/linux/pps_gen_kernel.h | 2 ++ 2 files changed, 28 insertions(+), 5 deletions(-) --- a/drivers/pps/generators/pps_gen.c +++ b/drivers/pps/generators/pps_gen.c @@ -69,17 +69,28 @@ static long pps_gen_cdev_ioctl(struct fi if (ret) return -EFAULT; - ret = pps_gen->info->enable(pps_gen, status); - if (ret) - return ret; - pps_gen->enabled = status; + scoped_guard(mutex, &pps_gen->info_lock) { + if (!pps_gen->info) + return -ENODEV; + + ret = pps_gen->info->enable(pps_gen, status); + if (ret) + return ret; + pps_gen->enabled = status; + } break; case PPS_GEN_USESYSTEMCLOCK: dev_dbg(&pps_gen->dev, "PPS_GEN_USESYSTEMCLOCK\n"); - ret = put_user(pps_gen->info->use_system_clock, uiuarg); + scoped_guard(mutex, &pps_gen->info_lock) { + if (!pps_gen->info) + return -ENODEV; + status = pps_gen->info->use_system_clock; + } + + ret = put_user(status, uiuarg); if (ret) return -EFAULT; @@ -212,6 +223,15 @@ static void pps_gen_unregister_cdev(stru { pr_debug("unregistering pps-gen%d\n", pps_gen->id); cdev_device_del(&pps_gen->cdev, &pps_gen->dev); + + /* + * An open file keeps pps_gen around, but the driver may free info as + * soon as we return. The sysfs files are gone now, so wait for the + * ioctls using info and make later ones fail. + */ + scoped_guard(mutex, &pps_gen->info_lock) + pps_gen->info = NULL; + put_device(&pps_gen->dev); } @@ -243,6 +263,7 @@ struct pps_gen_device *pps_gen_register_ pps_gen->info = info; pps_gen->enabled = false; + mutex_init(&pps_gen->info_lock); init_waitqueue_head(&pps_gen->queue); spin_lock_init(&pps_gen->lock); --- a/include/linux/pps_gen_kernel.h +++ b/include/linux/pps_gen_kernel.h @@ -11,6 +11,7 @@ #include #include #include +#include /* * Global defines @@ -44,6 +45,7 @@ struct pps_gen_source_info { /* The main struct */ struct pps_gen_device { const struct pps_gen_source_info *info; /* PSS generator info */ + struct mutex info_lock; /* protects info */ bool enabled; /* PSS generator status */ unsigned int event;