From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpcmd0641.aruba.it (smtpcmd0641.aruba.it [62.149.156.41]) (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 2BDB32741B6 for ; Thu, 4 Jun 2026 07:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.149.156.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780559604; cv=none; b=I4z4DhZBHr3ji2rMjY2E1peDAD6qmfbX3esuIGlvnEdQA592vbae2kHcXpoUfKIXoJqETxubHTaS6dmTLkuWbozm/l2Y84gWsMNL/8EDE641aQEW9bHYPESpom9l4bvaD+6vUPvInlwDja57aw/nCDurqAd4UFcYo6yb+6cepZw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780559604; c=relaxed/simple; bh=EUTWqJTIAJCMTJgpX9ME24DQ7rrMQaaXXdmiNoC4FBI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=PjOFfKZL43rucfiKgwzGeeMRqGiiEBFDr6FRmkOvgPetx+tFWPzRIZZcJRPTCW6LFgaGxbHtkjbhrS9+lwUIBOc0IesZnz7u8VIkSBKbceZhVmhTrz0W+eCcLI/PKWq29FXdCV582fKwNsjuOXqIyVNdxnnH/X68z/N5aUpKqBI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=enneenne.com; spf=pass smtp.mailfrom=enneenne.com; dkim=pass (2048-bit key) header.d=aruba.it header.i=@aruba.it header.b=DaelV3AL; arc=none smtp.client-ip=62.149.156.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=enneenne.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=enneenne.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=aruba.it header.i=@aruba.it header.b="DaelV3AL" Received: from [172.18.100.99] ([109.238.20.116]) by Aruba SMTP with ESMTPSA id V2qAwqXbmdarQV2qAwcVU6; Thu, 04 Jun 2026 09:50:10 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=aruba.it; s=a1; t=1780559410; bh=EUTWqJTIAJCMTJgpX9ME24DQ7rrMQaaXXdmiNoC4FBI=; h=Date:MIME-Version:Subject:To:From:Content-Type; b=DaelV3ALvEDmqGs1z5HxUQtnxOWMD/JWmfsGhARk6i+uPLS39i03YkdPx0a6PYjEJ jU6CiWP829StnLZGeg/RrEVz+1odOsq4XuT1yy/OAui6ZQurTKVN3LuUR+mn9rbNnE vErmaKiI3v9uLtOCew1QBGEixcY6qWrUFdWs7rXbT2EmMblhTOfBX/iqk2UvNc4OJz DyiWG2fA9wGDgHKbT8Dj6SZptPjJyHB4EvZxrrE/L5hfSNPqwULDAtys4dT8SDlfCC EqACrTSMa95TzkgcLExTnZYDWUg2iGPCLHvzgqKnFnhlcF3GmcoY4fm4rGfq2PKiTP T1Q/ylXcklbOA== Message-ID: <0ddb957d-3a20-4b92-b77f-dfc4b8cb3dca@enneenne.com> Date: Thu, 4 Jun 2026 09:50:10 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] pps: Don't allow PPS_KC_BIND on removed devices Content-Language: en-US To: Calvin Owens , linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman References: <672778c177ac9b6fdcb445e35c97ac4ca7d1149f.1780506611.git.calvin@wbinvd.org> From: Rodolfo Giometti In-Reply-To: <672778c177ac9b6fdcb445e35c97ac4ca7d1149f.1780506611.git.calvin@wbinvd.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4xfLcuqSmeRlBLi5wLGfaJR05O2Ys0yyK6eAUpO/xCNzKMBlnvznVt+diE3ykY2/D0xXoq7BnEBK07ci1aADjuKyvldFWuD8puwgCAcrehdtfP8NKE8oX4 L+BXKU28IrS3XPPO1FSUm1TIh+3ERHApvnijGePZ2SVj4v4Kt+IlNY1u2vvXaVluvRE1CMGd9jDMSIjvcmKbuKCgequ15vQlwfhNOh8fIbW9yOFpbTki174W M2aZCaA9gHlz1lHZroREMdnP0k5umJDbjXk1Yv2XUYc= On 03/06/2026 19:25, Calvin Owens wrote: > If userspace holds its file descriptor open, it can call PPS_KC_BIND on > a device which has been unplugged, leaving pps_kc_hardpps_dev as a > dangling pointer after close(). > > After that sequence, PPS_KC_BIND is broken until the system is rebooted, > because the pointer comparison in pps_kc_bind() can never be true. > > calling pps_ktimer_init+0x0/0x1000 [pps_ktimer] @ 1081 > initcall pps_ktimer_init+0x0/0x1000 [pps_ktimer] returned 0 after 811 usecs > pps pps0: bound kernel consumer: edge=0x1 > pps pps0: unbound kernel consumer on device removal > pps pps0: bound kernel consumer: edge=0x1 > calling pps_ktimer_init+0x0/0x1000 [pps_ktimer] @ 1085 > initcall pps_ktimer_init+0x0/0x1000 [pps_ktimer] returned 0 after 340 usecs > pps pps0: another kernel consumer is already bound > > Here is a short reproducer, which uses rmmod of the pps-ktimer testcase > to simulate a device being unplugged: > > #include > #include > #include > #include > #include > #include > #include > #include > > int main(void) > { > while (1) { > int fd; > > if (system("insmod ./pps-ktimer.ko")) > err(1, "insmod failed"); > > fd = open("/dev/pps0", O_RDWR); > if (fd == -1) > err(1, "open failed"); > > struct pps_bind_args args = { > .tsformat = PPS_TSFMT_TSPEC, > .edge = PPS_CAPTUREASSERT, > .consumer = PPS_KC_HARDPPS, > }; > > if (ioctl(fd, PPS_KC_BIND, &args)) > err(1, "first PPS_KC_BIND failed"); > > if (system("rmmod pps-ktimer")) > err(1, "rmmod failed"); > > if (ioctl(fd, PPS_KC_BIND, &args)) { > if (errno != ENODEV) > err(1, "second PPS_KC_BIND failed"); > else > puts("Got ENODEV, kernel is patched"); > } > > close(fd); > } > } > > Fix this by setting a flag when the device is unplugged, returning > -ENODEV from PPS_KC_BIND if the flag is set. > > For userspace to encounter this new behavior, it must do something which > breaks the interface today, so this fix shouldn't cause any observable > behavior change for working programs. > > Reported-by: Sashiko > Closes: https://sashiko.dev/#/patchset/cover.1779733602.git.calvin%40wbinvd.org?part=1 > Signed-off-by: Calvin Owens Acked-by: Rodolfo Giometti > --- > Changes in v2: > * Drop guard() changes so irqs are unmasked for printk() calls [Rodolfo] > > v1: https://lore.kernel.org/lkml/c9bfee54cbabbbfc8da7aa3d3893cf61d3ebf3b2.1780108479.git.calvin@wbinvd.org/ > > drivers/pps/kc.c | 10 ++++++++++ > include/linux/pps_kernel.h | 1 + > 2 files changed, 11 insertions(+) > > diff --git a/drivers/pps/kc.c b/drivers/pps/kc.c > index fbd23295afd7..4f8fffa7edd6 100644 > --- a/drivers/pps/kc.c > +++ b/drivers/pps/kc.c > @@ -38,6 +38,14 @@ int pps_kc_bind(struct pps_device *pps, struct pps_bind_args *bind_args) > /* Check if another consumer is already bound */ > spin_lock_irq(&pps_kc_hardpps_lock); > > + /* > + * Don't allow PPS_KC_BIND on a removed device. > + */ > + if (pps->kc_removed) { > + spin_unlock_irq(&pps_kc_hardpps_lock); > + return -ENODEV; > + } > + > if (bind_args->edge == 0) > if (pps_kc_hardpps_dev == pps) { > pps_kc_hardpps_mode = 0; > @@ -79,6 +87,8 @@ int pps_kc_bind(struct pps_device *pps, struct pps_bind_args *bind_args) > void pps_kc_remove(struct pps_device *pps) > { > spin_lock_irq(&pps_kc_hardpps_lock); > + > + pps->kc_removed = true; > if (pps == pps_kc_hardpps_dev) { > pps_kc_hardpps_mode = 0; > pps_kc_hardpps_dev = NULL; > diff --git a/include/linux/pps_kernel.h b/include/linux/pps_kernel.h > index aab0aebb529e..3c2979f8a5ac 100644 > --- a/include/linux/pps_kernel.h > +++ b/include/linux/pps_kernel.h > @@ -60,6 +60,7 @@ struct pps_device { > struct device dev; > struct fasync_struct *async_queue; /* fasync method */ > spinlock_t lock; > + bool kc_removed; > }; > > /* -- GNU/Linux Solutions e-mail: giometti@enneenne.com Linux Device Driver giometti@linux.it Embedded Systems phone: +39 349 2432127 UNIX programming