* [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
@ 2023-12-24 8:20 Zhipeng Lu
2023-12-24 16:56 ` Chuck Lever
2023-12-24 21:30 ` Simon Horman
0 siblings, 2 replies; 5+ messages in thread
From: Zhipeng Lu @ 2023-12-24 8:20 UTC (permalink / raw)
To: alexious
Cc: Trond Myklebust, Anna Schumaker, Chuck Lever, Jeff Layton,
Neil Brown, Olga Kornievskaia, Dai Ngo, Tom Talpey,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simo Sorce, Steve Dickson, Kevin Coffman, linux-nfs, netdev,
linux-kernel
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);
out_err:
return PTR_ERR(p);
}
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
2023-12-24 8:20 [PATCH] SUNRPC: fix a memleak in gss_import_v2_context Zhipeng Lu
@ 2023-12-24 16:56 ` Chuck Lever
2023-12-26 9:01 ` alexious
2023-12-24 21:30 ` Simon Horman
1 sibling, 1 reply; 5+ messages in thread
From: Chuck Lever @ 2023-12-24 16:56 UTC (permalink / raw)
To: Zhipeng Lu
Cc: Trond Myklebust, Anna Schumaker, Jeff Layton, Neil Brown,
Olga Kornievskaia, Dai Ngo, Tom Talpey, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simo Sorce,
Steve Dickson, Kevin Coffman, linux-nfs, netdev, linux-kernel
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.
How did you find this leak, and what kind of testing was done to
confirm the fix is safe?
> out_err:
> return PTR_ERR(p);
> }
> --
> 2.34.1
>
--
Chuck Lever
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
2023-12-24 8:20 [PATCH] SUNRPC: fix a memleak in gss_import_v2_context Zhipeng Lu
2023-12-24 16:56 ` Chuck Lever
@ 2023-12-24 21:30 ` Simon Horman
1 sibling, 0 replies; 5+ messages in thread
From: Simon Horman @ 2023-12-24 21:30 UTC (permalink / raw)
To: Zhipeng Lu
Cc: Trond Myklebust, Anna Schumaker, Chuck Lever, Jeff Layton,
Neil Brown, Olga Kornievskaia, Dai Ngo, Tom Talpey,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simo Sorce, Steve Dickson, Kevin Coffman, linux-nfs, netdev,
linux-kernel
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;
> + };
Hi Zhipeng Lu,
I think you need to handle the non-error case here:
return 0;
>
> +out_free:
> + kfree(ctx->mech_used.data);
> out_err:
> return PTR_ERR(p);
> }
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
2023-12-24 16:56 ` Chuck Lever
@ 2023-12-26 9:01 ` alexious
2023-12-27 15:36 ` Chuck Lever
0 siblings, 1 reply; 5+ messages in thread
From: alexious @ 2023-12-26 9:01 UTC (permalink / raw)
To: Chuck Lever
Cc: Trond Myklebust, Anna Schumaker, Jeff Layton, Neil Brown,
Olga Kornievskaia, Dai Ngo, Tom Talpey, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simo Sorce,
Steve Dickson, Kevin Coffman, linux-nfs, netdev, linux-kernel
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] SUNRPC: fix a memleak in gss_import_v2_context
2023-12-26 9:01 ` alexious
@ 2023-12-27 15:36 ` Chuck Lever
0 siblings, 0 replies; 5+ messages in thread
From: Chuck Lever @ 2023-12-27 15:36 UTC (permalink / raw)
To: alexious
Cc: Trond Myklebust, Anna Schumaker, Jeff Layton, Neil Brown,
Olga Kornievskaia, Dai Ngo, Tom Talpey, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simo Sorce,
Steve Dickson, Kevin Coffman, linux-nfs, netdev, linux-kernel
On Tue, Dec 26, 2023 at 05:01:05PM +0800, alexious@zju.edu.cn wrote:
> > 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.
I see, if gss_krb5_import_ctx_v2() fails, it releases the ciphers
and hashes via out_free. So no leak there.
A nicer approach would be to handle that clean up in
gss_krb5_import_sec_context(): less code duplication.
But you're right, it's not broken today.
> > 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.
Can you give some details about how you check the patches?
--
Chuck Lever
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-12-27 15:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-24 8:20 [PATCH] SUNRPC: fix a memleak in gss_import_v2_context Zhipeng Lu
2023-12-24 16:56 ` Chuck Lever
2023-12-26 9:01 ` alexious
2023-12-27 15:36 ` Chuck Lever
2023-12-24 21:30 ` Simon Horman
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).