From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Prashanth K <quic_prashk@quicinc.com>
Cc: Alan Stern <stern@rowland.harvard.edu>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Xiu Jianfeng <xiujianfeng@huawei.com>,
Pratham Pratap <quic_ppratap@quicinc.com>,
Jack Pham <quic_jackp@quicinc.com>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: u_serial: Add null pointer check in gserial_resume
Date: Thu, 9 Feb 2023 08:01:58 +0100 [thread overview]
Message-ID: <Y+SaZrDmaqB0U2QA@kroah.com> (raw)
In-Reply-To: <542ee8a6-598c-ca17-6d75-5eca2b34133a@quicinc.com>
On Thu, Feb 09, 2023 at 10:31:50AM +0530, Prashanth K wrote:
>
>
> On 09-02-23 01:51 am, Alan Stern wrote:
> > On Wed, Feb 08, 2023 at 09:15:54PM +0530, Prashanth K wrote:
> > >
> > >
> > > On 08-02-23 08:24 pm, Greg Kroah-Hartman wrote:
> > > > On Wed, Feb 08, 2023 at 07:24:47PM +0530, Prashanth K wrote:
> > > > > Consider a case where gserial_disconnect has already cleared
> > > > > gser->ioport. And if a wakeup interrupt triggers afterwards,
> > > > > gserial_resume gets called, which will lead to accessing of
> > > > > gserial->port and thus causing null pointer dereference.Add
> > > > > a null pointer check to prevent this.
> > > > >
> > > > > Fixes: aba3a8d01d62 (" usb: gadget: u_serial: add suspend resume callbacks")
> > > >
> > > > Nit, and our tools will complain, no " " before the "usb:" string here,
> > > > right?
> > > >
> > > Will fix it in next patch.
> > > >
> > > >
> > > > > Signed-off-by: Prashanth K <quic_prashk@quicinc.com>
> > > > > ---
> > > > > drivers/usb/gadget/function/u_serial.c | 3 +++
> > > > > 1 file changed, 3 insertions(+)
> > > > >
> > > > > diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
> > > > > index 840626e..98be2b8 100644
> > > > > --- a/drivers/usb/gadget/function/u_serial.c
> > > > > +++ b/drivers/usb/gadget/function/u_serial.c
> > > > > @@ -1428,6 +1428,9 @@ void gserial_resume(struct gserial *gser)
> > > > > struct gs_port *port = gser->ioport;
> > > > > unsigned long flags;
> > > > > + if (!port)
> > > > > + return;
> > > > > +
> > > >
> > > > What prevents port from going to NULL right after this check?
> > > In our case we got a null pointer de-reference while performing USB
> > > compliance tests, as the gser->port was null. Because in gserial_resume,
> > > spinlock_irq_save(&port->port_lock) accesses a null-pointer as port was
> > > already marked null by gserial_disconnect.
> > >
> > > And after gserial_resume acquires the spinlock, gserial_disconnect cant mark
> > > it null until the spinlock is released. We need to check if the port->lock
> > > is valid before accessing it, otherwise it can lead to the above mentioned
> > > scenario
> >
> > What happens if gserial_disconnect sets gser->port to NULL immediately
> > after your new check occurs, but before
> > spinlock_irq_save(&port->port_lock) gets called?
> >
> > You may need to add a static spinlock to prevent this from happening.
> >
> > Alan Stern
> In that case i guess we have to make port_lock a global variable and take it
> out of gs_port structure.
>
> + static DEFINE_SPINLOCK(port_lock);
>
> struct gs_port {
> struct tty_port port;
> - spinlock_t port_lock;
>
> This will require us to change all the spinlock(port->port_lock) used in
> u_serial.c, what do you suggest?
Yes, that would be the correct thing to do.
next prev parent reply other threads:[~2023-02-09 7:02 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-08 13:54 [PATCH] usb: gadget: u_serial: Add null pointer check in gserial_resume Prashanth K
2023-02-08 14:54 ` Greg Kroah-Hartman
2023-02-08 15:45 ` Prashanth K
2023-02-08 20:21 ` Alan Stern
2023-02-09 5:01 ` Prashanth K
2023-02-09 7:01 ` Greg Kroah-Hartman [this message]
2023-02-09 7:03 ` Prashanth K
2023-02-09 14:07 ` Prashanth K
2023-02-09 15:09 ` Alan Stern
2023-02-09 15:43 ` Prashanth K
2023-02-09 16:03 ` Alan Stern
2023-02-09 18:27 ` Prashanth K
2023-02-09 21:05 ` Alan Stern
2023-02-10 6:22 ` Prashanth K
2023-02-10 6:56 ` Prashanth K
2023-02-10 15:47 ` Alan Stern
2023-02-11 6:25 ` Prashanth K
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y+SaZrDmaqB0U2QA@kroah.com \
--to=gregkh@linuxfoundation.org \
--cc=christophe.jaillet@wanadoo.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=quic_jackp@quicinc.com \
--cc=quic_ppratap@quicinc.com \
--cc=quic_prashk@quicinc.com \
--cc=stern@rowland.harvard.edu \
--cc=xiujianfeng@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox