From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 CF4ED26B0B3 for ; Fri, 16 Jan 2026 14:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768574364; cv=none; b=Rb1EVlTBjeAi+JhYMzi7FHOvMNRIipechZF+4l4t5VvubDyA/3VEW41f+kziHR+n+5cr+aFUClxWfb6mpqCurFyBin2Kbj+y3FLjoB46RBxHfckobqUFVbC56aEzBBPoxO6rhH1WDc8x0zOlKsymIfrUGiOXZykO6Q4W5XgpwfU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768574364; c=relaxed/simple; bh=veX66scoB9hefb5+2W3G1YzoT2BEgFv0GhGpgsi4C3A=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jaiOKqNHSgkrS7SGpBmfanHFGHDArG7UE5T9CKukNHH/xbFpBjLifsC7dh1Y5YjeR9lnkNJtl525eQ7f+0eQdDP+4yQggzbmiq0d3MGMfwjyOdZeNjhOmg4WFBomfSmStsAVADtrkFvx77t1njG+LSqYUohBxv9sJK5921gvUEE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=MBz79W9N; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="MBz79W9N" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9BB62C116C6; Fri, 16 Jan 2026 14:39:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1768574364; bh=veX66scoB9hefb5+2W3G1YzoT2BEgFv0GhGpgsi4C3A=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MBz79W9NXc2SzDUxJcWl0MoinypRNDo0FXHIAduNTTn52Dp2vScDm6yMbbwbTI9D+ 7f73xbXK6nzEgX6O7GQqtuMrjqH4ODfUJP/9dlhJXrKlRjeHDkP3mn1SBqUKmBi5vX br073P7f1hWans954Ald8idDIUH23TSsGRpeuaVc= Date: Fri, 16 Jan 2026 15:39:21 +0100 From: Greg KH To: linan666@huaweicloud.com Cc: arnd@arndb.de, linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, wanghai38@huawei.com Subject: Re: [PATCH v2] char: lp: Fix NULL pointer dereference of cad Message-ID: <2026011644-glimpse-eatery-c556@gregkh> References: <20260108024155.3800149-1-linan666@huaweicloud.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=us-ascii Content-Disposition: inline In-Reply-To: <20260108024155.3800149-1-linan666@huaweicloud.com> On Thu, Jan 08, 2026 at 10:41:55AM +0800, linan666@huaweicloud.com wrote: > From: Li Nan > > NULL pointer dereference occurs when accessing 'port->physport->cad' > as below: > > KASAN: null-ptr-deref in range [0x00000000000003f0-0x00000000000003f7] > RIP: 0010:parport_wait_peripheral+0x130/0x4b0 > Call Trace: > parport_ieee1284_write_compat+0x306/0xb70 > ? __pfx_parport_ieee1284_write_compat+0x10/0x10 > parport_write+0x1d6/0x660 > lp_write+0x43e/0xbc0 > ? __pfx_lp_write+0x10/0x10 > vfs_write+0x21c/0x960 > ksys_write+0x12e/0x260 > ? __pfx_ksys_write+0x10/0x10 > ? __audit_syscall_entry+0x39e/0x510 > do_syscall_64+0x59/0x110 > entry_SYSCALL_64_after_hwframe+0x78/0xe2 > > The root cause is other processes may set 'port->cad' to NULL during > lp_write() operations. Process flow: > > T1 T2 > lp_write > lock port_mutex * > lp_claim_parport_or_block > parport_claim > port->cad = dev; > parport_write > parport_ieee1284_write_compat > lp_do_ioctl > lp_reset > lp_release_parport > parport_release > port->cad = NULL; > parport_wait_peripheral > port->physport->cad->timeout > | > NULL > > Fix this issue by adding 'port_mutex' protection. Like read/write and > ioctl LPGETSTATUS, use this lock to protect port access and modification > to prevent concurrency problems. > > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Signed-off-by: Li Nan > --- > v2: Use mutex_lock instead of mutex_lock_interruptible in lp_release(). > > drivers/char/lp.c | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/drivers/char/lp.c b/drivers/char/lp.c > index 24417a00dfe9..82f2405b4502 100644 > --- a/drivers/char/lp.c > +++ b/drivers/char/lp.c > @@ -520,9 +520,14 @@ static int lp_open(struct inode *inode, struct file *file) > should most likely only ever be used by the tunelp application. */ > if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) { > int status; > + if (mutex_lock_interruptible(&lp_table[minor].port_mutex)) { > + ret = -EINTR; > + goto out; > + } > lp_claim_parport_or_block(&lp_table[minor]); > status = r_str(minor); > lp_release_parport(&lp_table[minor]); > + mutex_unlock(&lp_table[minor].port_mutex); > if (status & LP_POUTPA) { > printk(KERN_INFO "lp%d out of paper\n", minor); > LP_F(minor) &= ~LP_BUSY; > @@ -547,6 +552,10 @@ static int lp_open(struct inode *inode, struct file *file) > goto out; > } > /* Determine if the peripheral supports ECP mode */ > + if (mutex_lock_interruptible(&lp_table[minor].port_mutex)) { > + ret = -EINTR; > + goto out; > + } > lp_claim_parport_or_block(&lp_table[minor]); > if ((lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) && > !parport_negotiate(lp_table[minor].dev->port, > @@ -559,6 +568,7 @@ static int lp_open(struct inode *inode, struct file *file) > /* Leave peripheral in compatibility mode */ > parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); > lp_release_parport(&lp_table[minor]); > + mutex_unlock(&lp_table[minor].port_mutex); > lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; > out: > mutex_unlock(&lp_mutex); > @@ -569,10 +579,13 @@ static int lp_release(struct inode *inode, struct file *file) > { > unsigned int minor = iminor(inode); > > + /* ->release should never fail. Use uninterruptible mutex variant */ > + mutex_lock(&lp_table[minor].port_mutex); But could stall for a very long time, right? That's not a good idea if possible. And how was this tested? thanks, greg k-h