mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: alexious@zju.edu.cn
To: "Chuck Lever" <chuck.lever@oracle.com>
Cc: "Trond Myklebust" <trond.myklebust@hammerspace.com>,
	 "Anna Schumaker" <anna@kernel.org>,
	 "Jeff Layton" <jlayton@kernel.org>, "Neil Brown" <neilb@suse.de>,
	 "Olga Kornievskaia" <kolga@netapp.com>,
	 "Dai Ngo" <Dai.Ngo@oracle.com>, "Tom Talpey" <tom@talpey.com>,
	 "David S. Miller" <davem@davemloft.net>,
	 "Eric Dumazet" <edumazet@google.com>,
	 "Jakub Kicinski" <kuba@kernel.org>,
	 "Paolo Abeni" <pabeni@redhat.com>,
	"Simo Sorce" <simo@redhat.com>,
	 "Steve Dickson" <steved@redhat.com>,
	 "Kevin Coffman" <kwc@citi.umich.edu>,
	linux-nfs@vger.kernel.org,  netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
Date: Tue, 26 Dec 2023 17:01:05 +0800 (GMT+08:00)	[thread overview]
Message-ID: <45874bb1.58022.18ca55b2eab.Coremail.alexious@zju.edu.cn> (raw)
In-Reply-To: <ZYhivG2MxmtePvo3@tissot.1015granger.net>

> On Sun, Dec 24, 2023 at 04:20:33PM +0800, Zhipeng Lu wrote:
> > The ctx->mech_used.data allocated by kmemdup is not freed in neither
> > gss_import_v2_context nor it only caller radeon_driver_open_kms.
> > Thus, this patch reform the last call of gss_import_v2_context to the
> > gss_krb5_import_ctx_v2, preventing the memleak while keepping the return
> > formation.
> > 
> > Fixes: 47d848077629 ("gss_krb5: handle new context format from gssd")
> > Signed-off-by: Zhipeng Lu <alexious@zju.edu.cn>
> > ---
> >  net/sunrpc/auth_gss/gss_krb5_mech.c | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> > 
> > diff --git a/net/sunrpc/auth_gss/gss_krb5_mech.c b/net/sunrpc/auth_gss/gss_krb5_mech.c
> > index e31cfdf7eadc..1e54bd63e3f0 100644
> > --- a/net/sunrpc/auth_gss/gss_krb5_mech.c
> > +++ b/net/sunrpc/auth_gss/gss_krb5_mech.c
> > @@ -398,6 +398,7 @@ gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx,
> >  	u64 seq_send64;
> >  	int keylen;
> >  	u32 time32;
> > +	int ret;
> >  
> >  	p = simple_get_bytes(p, end, &ctx->flags, sizeof(ctx->flags));
> >  	if (IS_ERR(p))
> > @@ -450,8 +451,14 @@ gss_import_v2_context(const void *p, const void *end, struct krb5_ctx *ctx,
> >  	}
> >  	ctx->mech_used.len = gss_kerberos_mech.gm_oid.len;
> >  
> > -	return gss_krb5_import_ctx_v2(ctx, gfp_mask);
> > +	ret = gss_krb5_import_ctx_v2(ctx, gfp_mask);
> > +	if (ret) {
> > +		p = ERR_PTR(ret);
> > +		goto out_free;
> > +	};
> >  
> > +out_free:
> > +	kfree(ctx->mech_used.data);
> 
> If the caller's error flow does not invoke
> gss_krb5_delete_sec_context(), then I would expect more than just
> mech_used.data would be leaked. What if, instead, you changed
> gss_krb5_import_sec_context() like this (untested):
> 
> 471         ret = gss_import_v2_context(p, end, ctx, gfp_mask);
> 472         memzero_explicit(&ctx->Ksess, sizeof(ctx->Ksess));
> 473         if (ret) {      
>    -                kfree(ctx);                      
>    +                gss_krb5_delete_sec_context(ctx);
> 475                 return ret;
> 476         }    
> 
> Obviously you would need to add a forward declaration of
> gss_krb5_import_sec_context() to make this compile. The question
> is whether gss_krb5_delete_sec_context() will deal with a partially-
> initialized @ctx.

Since the ctx is allocated just in gss_krb5_import_sec_context, 
together with that all of gss_krb5_import_sec_context, gss_import_v2_context(with this patch)
and gss_krb5_import_ctx_v2 are allocation-free balanced. It seems that we don't need to 
release anything else by invoking gss_krb5_delete_sec_context.

If I miss something leaked, please let me know.


> 
> How did you find this leak, and what kind of testing was done to
> confirm the fix is safe?

I found this memleak by static analysis. 
The safety issue can't be solved by automatic tools as far as I know.
So I check patches manuelly before sending patches.

> >  out_err:
> >  	return PTR_ERR(p);
> >  }
> > -- 
> > 2.34.1
> > 
> 
> -- 
> Chuck Lever

  reply	other threads:[~2023-12-26  9:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-24  8:20 Zhipeng Lu
2023-12-24 16:56 ` Chuck Lever
2023-12-26  9:01   ` alexious [this message]
2023-12-27 15:36     ` Chuck Lever
2023-12-24 21:30 ` Simon Horman

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=45874bb1.58022.18ca55b2eab.Coremail.alexious@zju.edu.cn \
    --to=alexious@zju.edu.cn \
    --cc=Dai.Ngo@oracle.com \
    --cc=anna@kernel.org \
    --cc=chuck.lever@oracle.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=jlayton@kernel.org \
    --cc=kolga@netapp.com \
    --cc=kuba@kernel.org \
    --cc=kwc@citi.umich.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=neilb@suse.de \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=simo@redhat.com \
    --cc=steved@redhat.com \
    --cc=tom@talpey.com \
    --cc=trond.myklebust@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

all inboxes | Powered by JetHome®