* Re: PROBLEM: Pointer dereference error may occur in "alloc_init_deleg" function
[not found] <49ad6b84.57cc.18b1de7572b.Coremail.huangsicong@iie.ac.cn>
@ 2023-10-11 12:16 ` Jeff Layton
2023-10-12 8:34 ` [PATCH v1] NFSD: clean up alloc_init_deleg() 黄思聪
0 siblings, 1 reply; 4+ messages in thread
From: Jeff Layton @ 2023-10-11 12:16 UTC (permalink / raw)
To: 黄思聪, chuck.lever
Cc: neilb, kolga, Dai.Ngo, tom, linux-nfs
On Wed, 2023-10-11 at 16:43 +0800, 黄思聪 wrote:
> Pointer dereference error may occur in "alloc_init_deleg" function.
>
> The "alloc_init_deleg" function located in "fs/nfsd/nfs4state.c" may occur a pointer dereference error when it calls the function "nfs4_alloc_stid" located in the same kernel file. The "nfs4_alloc_stid" function will call the "kmem_cache_zalloc" function to allocate enough memory for storing the "stid" variable. If there are significant memory fragmentation issues, insufficient free memory blocks, or internal errors in the allocation function, the "kmem_cache_zalloc" function will return NULL. Then the "nfs4_alloc_stid" function will return NULL to the "alloc_init_deleg" function. Finally, the "alloc_init_deleg" function will execute the following instructions.
> dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> if (dp == NULL)
> goto out_dec;
> dp->dl_stid.sc_stateid.si_generation = 1;
>
> The "delegstateid" function is defined as below:
> static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
> {
> return container_of(s, struct nfs4_delegation, dl_stid);
> }
>
> When the parameter "struct nfs4_stid *s" is NULL, the function will return a strange value which is a negative number. The value will be interpreted as a very large number. Then the variable "dp" in the "alloc_init_deleg" function will get the value, and it will pass the following "if" conditional statements. In the last, the variable "dp" will be dereferenced, and it will cause an error.
>
> My experimental kernel version is "LINUX 6.1", and this problem exists in all the version from "LINUX v3.2-rc1" to "LINUX v6.6-rc5".
(I don't think there are security implications here, so I'm cc'ing the
mailing list and making this public.)
Well spotted! Ordinarily you'd be correct, but dl_stid is the first
field in the struct, so the container_of will just return the same
value that you pass in.
Still, this is not something we ought to rely on going forward. Would
you care to make a patch to clean this up and make that a bit less
subtle?
Thanks!
--
Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v1] NFSD: clean up alloc_init_deleg()
2023-10-11 12:16 ` PROBLEM: Pointer dereference error may occur in "alloc_init_deleg" function Jeff Layton
@ 2023-10-12 8:34 ` 黄思聪
2023-10-12 12:26 ` Chuck Lever III
2023-10-12 12:44 ` Jeff Layton
0 siblings, 2 replies; 4+ messages in thread
From: 黄思聪 @ 2023-10-12 8:34 UTC (permalink / raw)
To: Jeff Layton; +Cc: chuck.lever, neilb, kolga, Dai.Ngo, tom, linux-nfs
> On Wed, 2023-10-11 at 16:43 +0800, 黄思聪 wrote:
> > Pointer dereference error may occur in "alloc_init_deleg" function.
> >
> > The "alloc_init_deleg" function located in "fs/nfsd/nfs4state.c" may occur a pointer dereference error when it calls the function "nfs4_alloc_stid" located in the same kernel file. The "nfs4_alloc_stid" function will call the "kmem_cache_zalloc" function to allocate enough memory for storing the "stid" variable. If there are significant memory fragmentation issues, insufficient free memory blocks, or internal errors in the allocation function, the "kmem_cache_zalloc" function will return NULL. Then the "nfs4_alloc_stid" function will return NULL to the "alloc_init_deleg" function. Finally, the "alloc_init_deleg" function will execute the following instructions.
> > dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> > if (dp == NULL)
> > goto out_dec;
> > dp->dl_stid.sc_stateid.si_generation = 1;
> >
> > The "delegstateid" function is defined as below:
> > static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
> > {
> > return container_of(s, struct nfs4_delegation, dl_stid);
> > }
> >
> > When the parameter "struct nfs4_stid *s" is NULL, the function will return a strange value which is a negative number. The value will be interpreted as a very large number. Then the variable "dp" in the "alloc_init_deleg" function will get the value, and it will pass the following "if" conditional statements. In the last, the variable "dp" will be dereferenced, and it will cause an error.
> >
> > My experimental kernel version is "LINUX 6.1", and this problem exists in all the version from "LINUX v3.2-rc1" to "LINUX v6.6-rc5".
>
>
> (I don't think there are security implications here, so I'm cc'ing the
> mailing list and making this public.)
>
> Well spotted! Ordinarily you'd be correct, but dl_stid is the first
> field in the struct, so the container_of will just return the same
> value that you pass in.
>
> Still, this is not something we ought to rely on going forward. Would
> you care to make a patch to clean this up and make that a bit less
> subtle?
>
> Thanks!
> --
> Jeff Layton <jlayton@kernel.org>
Thank you for your feedback! Indeed, you are correct! Next time I will check it twice before reporting a problem.
My patch is below:
Modify the conditional statement for null pointer check in the function
'alloc_init_deleg' to make this function more robust and clear. Otherwise,
this function may have potential pointer dereference problem in the future,
when modifying or expanding the nfs4_delegation structure.
Signed-off-by: Sicong Huang <huangsicong@iie.ac.cn>
---
fs/nfsd/nfs4state.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index b1118050ff52..516b8bd6cb53 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -1160,6 +1160,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
struct nfs4_clnt_odstate *odstate, u32 dl_type)
{
struct nfs4_delegation *dp;
+ struct nfs4_stid *stid;
long n;
dprintk("NFSD alloc_init_deleg\n");
@@ -1168,9 +1169,10 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
goto out_dec;
if (delegation_blocked(&fp->fi_fhandle))
goto out_dec;
- dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
- if (dp == NULL)
+ stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
+ if (stid == NULL)
goto out_dec;
+ dp = delegstateid(stid);
/*
* delegation seqid's are never incremented. The 4.1 special
--
2.34.1
Best Regards,
Sicong Huang
</huangsicong@iie.ac.cn></jlayton@kernel.org>
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v1] NFSD: clean up alloc_init_deleg()
2023-10-12 8:34 ` [PATCH v1] NFSD: clean up alloc_init_deleg() 黄思聪
@ 2023-10-12 12:26 ` Chuck Lever III
2023-10-12 12:44 ` Jeff Layton
1 sibling, 0 replies; 4+ messages in thread
From: Chuck Lever III @ 2023-10-12 12:26 UTC (permalink / raw)
To: 黄思聪
Cc: Jeff Layton, Neil Brown, Olga Kornievskaia, Dai Ngo, Tom Talpey,
Linux NFS Mailing List
> On Oct 12, 2023, at 4:34 AM, 黄思聪 <huangsicong@iie.ac.cn> wrote:
>
>
> > On Wed, 2023-10-11 at 16:43 +0800, 黄思聪 wrote:
> > > Pointer dereference error may occur in "alloc_init_deleg" function.
> > >
> > > The "alloc_init_deleg" function located in "fs/nfsd/nfs4state.c" may occur a pointer dereference error when it calls the function "nfs4_alloc_stid" located in the same kernel file. The "nfs4_alloc_stid" function will call the "kmem_cache_zalloc" function to allocate enough memory for storing the "stid" variable. If there are significant memory fragmentation issues, insufficient free memory blocks, or internal errors in the allocation function, the "kmem_cache_zalloc" function will return NULL. Then the "nfs4_alloc_stid" function will return NULL to the "alloc_init_deleg" function. Finally, the "alloc_init_deleg" function will execute the following instructions.
> > > dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> > > if (dp == NULL)
> > > goto out_dec;
> > > dp->dl_stid.sc_stateid.si_generation = 1;
> > >
> > > The "delegstateid" function is defined as below:
> > > static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
> > > {
> > > return container_of(s, struct nfs4_delegation, dl_stid);
> > > }
> > >
> > > When the parameter "struct nfs4_stid *s" is NULL, the function will return a strange value which is a negative number. The value will be interpreted as a very large number. Then the variable "dp" in the "alloc_init_deleg" function will get the value, and it will pass the following "if" conditional statements. In the last, the variable "dp" will be dereferenced, and it will cause an error.
> > >
> > > My experimental kernel version is "LINUX 6.1", and this problem exists in all the version from "LINUX v3.2-rc1" to "LINUX v6.6-rc5".
> >
> >
> > (I don't think there are security implications here, so I'm cc'ing the
> > mailing list and making this public.)
> >
> > Well spotted! Ordinarily you'd be correct, but dl_stid is the first
> > field in the struct, so the container_of will just return the same
> > value that you pass in.
> >
> > Still, this is not something we ought to rely on going forward. Would
> > you care to make a patch to clean this up and make that a bit less
> > subtle?
> >
> > Thanks!
> > --
> > Jeff Layton <jlayton@kernel.org>
>
>
> Thank you for your feedback! Indeed, you are correct! Next time I will check it twice before reporting a problem.
>
> My patch is below:
>
> Modify the conditional statement for null pointer check in the function
> 'alloc_init_deleg' to make this function more robust and clear. Otherwise,
> this function may have potential pointer dereference problem in the future,
> when modifying or expanding the nfs4_delegation structure.
>
> Signed-off-by: Sicong Huang <huangsicong@iie.ac.cn>
> ---
> fs/nfsd/nfs4state.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
> index b1118050ff52..516b8bd6cb53 100644
> --- a/fs/nfsd/nfs4state.c
> +++ b/fs/nfsd/nfs4state.c
> @@ -1160,6 +1160,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
> struct nfs4_clnt_odstate *odstate, u32 dl_type)
> {
> struct nfs4_delegation *dp;
> + struct nfs4_stid *stid;
> long n;
>
> dprintk("NFSD alloc_init_deleg\n");
> @@ -1168,9 +1169,10 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
> goto out_dec;
> if (delegation_blocked(&fp->fi_fhandle))
Thanks for your report and the fix.
Some part of the MTA/MUA chain has mangled this email; see
above where '&' is now & and '>' is now > . I had
to apply this patch by hand. It's now pushed out to
nfsd-next, so please check it over.
> goto out_dec;
> - dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> - if (dp == NULL)
> + stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
> + if (stid == NULL)
> goto out_dec;
> + dp = delegstateid(stid);
>
> /*
> * delegation seqid's are never incremented. The 4.1 special
> --
> 2.34.1
>
> Best Regards,
> Sicong Huang
> </huangsicong@iie.ac.cn></jlayton@kernel.org>
--
Chuck Lever
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] NFSD: clean up alloc_init_deleg()
2023-10-12 8:34 ` [PATCH v1] NFSD: clean up alloc_init_deleg() 黄思聪
2023-10-12 12:26 ` Chuck Lever III
@ 2023-10-12 12:44 ` Jeff Layton
1 sibling, 0 replies; 4+ messages in thread
From: Jeff Layton @ 2023-10-12 12:44 UTC (permalink / raw)
To: 黄思聪
Cc: chuck.lever, neilb, kolga, Dai.Ngo, tom, linux-nfs
On Thu, 2023-10-12 at 16:34 +0800, 黄思聪 wrote:
> > On Wed, 2023-10-11 at 16:43 +0800, 黄思聪 wrote:
> > > Pointer dereference error may occur in "alloc_init_deleg" function.
> > >
> > > The "alloc_init_deleg" function located in "fs/nfsd/nfs4state.c" may occur a pointer dereference error when it calls the function "nfs4_alloc_stid" located in the same kernel file. The "nfs4_alloc_stid" function will call the "kmem_cache_zalloc" function to allocate enough memory for storing the "stid" variable. If there are significant memory fragmentation issues, insufficient free memory blocks, or internal errors in the allocation function, the "kmem_cache_zalloc" function will return NULL. Then the "nfs4_alloc_stid" function will return NULL to the "alloc_init_deleg" function. Finally, the "alloc_init_deleg" function will execute the following instructions.
> > > dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> > > if (dp == NULL)
> > > goto out_dec;
> > > dp->dl_stid.sc_stateid.si_generation = 1;
> > >
> > > The "delegstateid" function is defined as below:
> > > static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
> > > {
> > > return container_of(s, struct nfs4_delegation, dl_stid);
> > > }
> > >
> > > When the parameter "struct nfs4_stid *s" is NULL, the function will return a strange value which is a negative number. The value will be interpreted as a very large number. Then the variable "dp" in the "alloc_init_deleg" function will get the value, and it will pass the following "if" conditional statements. In the last, the variable "dp" will be dereferenced, and it will cause an error.
> > >
> > > My experimental kernel version is "LINUX 6.1", and this problem exists in all the version from "LINUX v3.2-rc1" to "LINUX v6.6-rc5".
> >
> >
> > (I don't think there are security implications here, so I'm cc'ing the
> > mailing list and making this public.)
> >
> > Well spotted! Ordinarily you'd be correct, but dl_stid is the first
> > field in the struct, so the container_of will just return the same
> > value that you pass in.
> >
> > Still, this is not something we ought to rely on going forward. Would
> > you care to make a patch to clean this up and make that a bit less
> > subtle?
> >
> > Thanks!
> > --
> > Jeff Layton <jlayton@kernel.org>
>
>
> Thank you for your feedback! Indeed, you are correct! Next time I will check it twice before reporting a problem.
>
> My patch is below:
>
> Modify the conditional statement for null pointer check in the function
> 'alloc_init_deleg' to make this function more robust and clear. Otherwise,
> this function may have potential pointer dereference problem in the future,
> when modifying or expanding the nfs4_delegation structure.
>
> Signed-off-by: Sicong Huang <huangsicong@iie.ac.cn>
> ---
> fs/nfsd/nfs4state.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
> index b1118050ff52..516b8bd6cb53 100644
> --- a/fs/nfsd/nfs4state.c
> +++ b/fs/nfsd/nfs4state.c
> @@ -1160,6 +1160,7 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
> struct nfs4_clnt_odstate *odstate, u32 dl_type)
> {
> struct nfs4_delegation *dp;
> + struct nfs4_stid *stid;
> long n;
>
> dprintk("NFSD alloc_init_deleg\n");
> @@ -1168,9 +1169,10 @@ alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
> goto out_dec;
> if (delegation_blocked(&fp->fi_fhandle))
> goto out_dec;
> - dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
> - if (dp == NULL)
> + stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
> + if (stid == NULL)
> goto out_dec;
> + dp = delegstateid(stid);
>
> /*
> * delegation seqid's are never incremented. The 4.1 special
Reviewed-by: Jeff Layton <jlayton@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-10-12 12:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <49ad6b84.57cc.18b1de7572b.Coremail.huangsicong@iie.ac.cn>
2023-10-11 12:16 ` PROBLEM: Pointer dereference error may occur in "alloc_init_deleg" function Jeff Layton
2023-10-12 8:34 ` [PATCH v1] NFSD: clean up alloc_init_deleg() 黄思聪
2023-10-12 12:26 ` Chuck Lever III
2023-10-12 12:44 ` Jeff Layton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox