linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xiaomeng Tong <xiam0nd.tong@gmail.com>
To: trondmy@hammerspace.com
Cc: anna@kernel.org, bhalevy@panasas.com, bharrosh@panasas.com,
	linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org,
	stable@vger.kernel.org, xiam0nd.tong@gmail.com
Subject: Re: [PATCH] nfs: callback_proc: fix an incorrect NULL check on list iterator
Date: Mon, 28 Mar 2022 09:43:14 +0800	[thread overview]
Message-ID: <20220328014314.18987-1-xiam0nd.tong@gmail.com> (raw)
In-Reply-To: <436766b6fb5f3ec513629d4fa0888b77c65cfa16.camel@hammerspace.com>

On Sun, 27 Mar 2022 15:20:42 +0000, Trond Myklebust wrote:
> On Sun, 2022-03-27 at 16:02 +0800, Xiaomeng Tong wrote:
> > The bug is here:
> >         if (!server ||
> >         server->pnfs_curr_ld->id != dev->cbd_layout_type) {
> > 
> > The list iterator value 'server' will *always* be set and non-NULL
> > by list_for_each_entry_rcu, so it is incorrect to assume that the
> > iterator value will be NULL if the list is empty or no element is
> > found (In fact, it will be a bogus pointer to an invalid struct
> > object containing the HEAD, which is used for above check at next
> > outer loop). Otherwise it may bypass the check in theory (iif
> > server->pnfs_curr_ld->id == dev->cbd_layout_type, 'server' now is
> > a bogus pointer) and lead to invalid memory access passing the check.
> > 
> > To fix the bug, use a new variable 'iter' as the list iterator,
> > while use the original variable 'server' as a dedicated pointer to
> > point to the found element.
> > 
> > Cc: stable@vger.kernel.org
> > Fixes: 1be5683b03a76 ("pnfs: CB_NOTIFY_DEVICEID")
> > Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
> > ---
> >  fs/nfs/callback_proc.c | 9 +++++----
> >  1 file changed, 5 insertions(+), 4 deletions(-)
> > 
> > diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c
> > index c343666d9a42..84779785dc8d 100644
> > --- a/fs/nfs/callback_proc.c
> > +++ b/fs/nfs/callback_proc.c
> > @@ -361,7 +361,7 @@ __be32 nfs4_callback_devicenotify(void *argp,
> > void *resp,
> >         uint32_t i;
> >         __be32 res = 0;
> >         struct nfs_client *clp = cps->clp;
> > -       struct nfs_server *server = NULL;
> > +       struct nfs_server *server = NULL, *iter;
> >  
> >         if (!clp) {
> >                 res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
> > @@ -374,10 +374,11 @@ __be32 nfs4_callback_devicenotify(void *argp,
> > void *resp,
> >                 if (!server ||
> >                     server->pnfs_curr_ld->id != dev->cbd_layout_type)
> > {
> >                         rcu_read_lock();
> > -                       list_for_each_entry_rcu(server, &clp-
> > >cl_superblocks, client_link)
> > -                               if (server->pnfs_curr_ld &&
> > -                                   server->pnfs_curr_ld->id == dev-
> > >cbd_layout_type) {
> > +                       list_for_each_entry_rcu(iter, &clp-
> > >cl_superblocks, client_link)
> > +                               if (iter->pnfs_curr_ld &&
> > +                                   iter->pnfs_curr_ld->id == dev-
> > >cbd_layout_type) {
> >                                         rcu_read_unlock();
> > +                                       server = iter;
> 
> Hmm... We're not holding any locks on the super block for 'iter' here,
> so nothing is preventing it from going away while we're.
> 

ok, i am not a 'rcu lock' expert, i will make it hold the rcu_read_lock()
if necessary.

> Given that we really only want a pointer to the struct
> pnfs_layoutdriver_type anyway, why not just convert the code to save a
> pointer to that (and do it while holding the rcu_read_lock())?
> 

Maybe it's not that simple. If you only save a pointer to that and still
use 'server' as the list iterator of list_for_each_entry_rcu, there could
be problem.

I.e., if no element found in list_for_each_entry_rcu in the first outer
'for' loop, and now 'server' is a bogus pointer to an invalid struct, and
continue to go into the second outer 'for' loop, and the check below will
lead to invalid memory access (server->pnfs_curr_ld->id), even can potentialy
be bypassed with crafted data to make the condition false and mistakely run
nfs4_delete_deviceid(server->pnfs_curr_ld, clp, &dev->cbd_dev_id); with bogus
'server'.

if (!server ||
    server->pnfs_curr_ld->id != dev->cbd_layout_type) {

> The struct pnfs_layoutdriver is always expected to be a statically
> allocated structure, so it won't go away as long as the pNFS driver
> module remains loaded.
>

--
Xiaomeng Tong 

  reply	other threads:[~2022-03-28  1:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-27  8:02 [PATCH] nfs: callback_proc: fix an incorrect NULL check on list iterator Xiaomeng Tong
2022-03-27 15:20 ` Trond Myklebust
2022-03-28  1:43   ` Xiaomeng Tong [this message]
2022-03-28 13:24     ` Trond Myklebust
2022-03-28 13:43       ` Greg KH
2022-03-29  4:00       ` Xiaomeng Tong

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=20220328014314.18987-1-xiam0nd.tong@gmail.com \
    --to=xiam0nd.tong@gmail.com \
    --cc=anna@kernel.org \
    --cc=bhalevy@panasas.com \
    --cc=bharrosh@panasas.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=trondmy@hammerspace.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;
as well as URLs for NNTP newsgroup(s).