* [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
@ 2019-07-24 8:43 ` Jia-Ju Bai
0 siblings, 0 replies; 11+ messages in thread
From: Jia-Ju Bai @ 2019-07-24 8:43 UTC (permalink / raw)
To: rpeterso, agruenba; +Cc: cluster-devel, linux-kernel, Jia-Ju Bai
In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
cause a null-pointer dereference.
To fix this null-pointer dereference, NULL is returned when ip is NULL.
This bug is found by a static analysis tool STCheck written by us.
Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
fs/gfs2/super.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index 0acc5834f653..c07c3f4f8451 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -1728,8 +1728,9 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
memset(&ip->i_res, 0, sizeof(ip->i_res));
RB_CLEAR_NODE(&ip->i_res.rs_node);
ip->i_rahead = 0;
- }
- return &ip->i_inode;
+ return &ip->i_inode;
+ } else
+ return NULL;
}
static void gfs2_free_inode(struct inode *inode)
--
2.17.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
@ 2019-07-24 8:43 ` Jia-Ju Bai
0 siblings, 0 replies; 11+ messages in thread
From: Jia-Ju Bai @ 2019-07-24 8:43 UTC (permalink / raw)
To: cluster-devel.redhat.com
In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
cause a null-pointer dereference.
To fix this null-pointer dereference, NULL is returned when ip is NULL.
This bug is found by a static analysis tool STCheck written by us.
Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
fs/gfs2/super.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index 0acc5834f653..c07c3f4f8451 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -1728,8 +1728,9 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
memset(&ip->i_res, 0, sizeof(ip->i_res));
RB_CLEAR_NODE(&ip->i_res.rs_node);
ip->i_rahead = 0;
- }
- return &ip->i_inode;
+ return &ip->i_inode;
+ } else
+ return NULL;
}
static void gfs2_free_inode(struct inode *inode)
--
2.17.0
^ permalink raw reply related [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 8:43 ` [Cluster-devel] " Jia-Ju Bai
(?)
@ 2019-07-24 8:48 ` Steven Whitehouse
2019-07-24 10:02 ` Christoph Hellwig
-1 siblings, 1 reply; 11+ messages in thread
From: Steven Whitehouse @ 2019-07-24 8:48 UTC (permalink / raw)
To: cluster-devel.redhat.com
Hi,
On 24/07/2019 09:43, Jia-Ju Bai wrote:
> In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
> NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
> cause a null-pointer dereference.
>
> To fix this null-pointer dereference, NULL is returned when ip is NULL.
>
> This bug is found by a static analysis tool STCheck written by us.
The bug is in the tool I'm afraid. Since i_inode is the first element of
ip, there is no NULL dereference here. A pointer to ip->i_inode and a
pointer to ip are one and the same (bar the differing types) which is
the reason that we return &ip->i_inode rather than just ip,
Steve.
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
> fs/gfs2/super.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
> index 0acc5834f653..c07c3f4f8451 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1728,8 +1728,9 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
> memset(&ip->i_res, 0, sizeof(ip->i_res));
> RB_CLEAR_NODE(&ip->i_res.rs_node);
> ip->i_rahead = 0;
> - }
> - return &ip->i_inode;
> + return &ip->i_inode;
> + } else
> + return NULL;
> }
>
> static void gfs2_free_inode(struct inode *inode)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 8:43 ` [Cluster-devel] " Jia-Ju Bai
@ 2019-07-24 10:00 ` Christoph Hellwig
-1 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2019-07-24 10:00 UTC (permalink / raw)
To: Jia-Ju Bai; +Cc: rpeterso, agruenba, cluster-devel, linux-kernel
On Wed, Jul 24, 2019 at 04:43:03PM +0800, Jia-Ju Bai wrote:
> index 0acc5834f653..c07c3f4f8451 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1728,8 +1728,9 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
> memset(&ip->i_res, 0, sizeof(ip->i_res));
> RB_CLEAR_NODE(&ip->i_res.rs_node);
> ip->i_rahead = 0;
> - }
> - return &ip->i_inode;
> + return &ip->i_inode;
> + } else
> + return NULL;
> }
No need for an else after a return. You probably just want to
return early for the NULL case.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
@ 2019-07-24 10:00 ` Christoph Hellwig
0 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2019-07-24 10:00 UTC (permalink / raw)
To: cluster-devel.redhat.com
On Wed, Jul 24, 2019 at 04:43:03PM +0800, Jia-Ju Bai wrote:
> index 0acc5834f653..c07c3f4f8451 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -1728,8 +1728,9 @@ static struct inode *gfs2_alloc_inode(struct super_block *sb)
> memset(&ip->i_res, 0, sizeof(ip->i_res));
> RB_CLEAR_NODE(&ip->i_res.rs_node);
> ip->i_rahead = 0;
> - }
> - return &ip->i_inode;
> + return &ip->i_inode;
> + } else
> + return NULL;
> }
No need for an else after a return. You probably just want to
return early for the NULL case.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 8:48 ` Steven Whitehouse
@ 2019-07-24 10:02 ` Christoph Hellwig
2019-07-24 10:21 ` Edwin Török
2019-07-24 10:22 ` Steven Whitehouse
0 siblings, 2 replies; 11+ messages in thread
From: Christoph Hellwig @ 2019-07-24 10:02 UTC (permalink / raw)
To: cluster-devel.redhat.com
On Wed, Jul 24, 2019 at 09:48:38AM +0100, Steven Whitehouse wrote:
> Hi,
>
> On 24/07/2019 09:43, Jia-Ju Bai wrote:
> > In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
> > NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
> > cause a null-pointer dereference.
> >
> > To fix this null-pointer dereference, NULL is returned when ip is NULL.
> >
> > This bug is found by a static analysis tool STCheck written by us.
>
> The bug is in the tool I'm afraid. Since i_inode is the first element of ip,
> there is no NULL dereference here. A pointer to ip->i_inode and a pointer to
> ip are one and the same (bar the differing types) which is the reason that
> we return &ip->i_inode rather than just ip,
But that doesn't help if ip is NULL, as dereferencing a field in in
still remains invalid behavior.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 10:02 ` Christoph Hellwig
@ 2019-07-24 10:21 ` Edwin Török
2019-07-24 10:22 ` Steven Whitehouse
1 sibling, 0 replies; 11+ messages in thread
From: Edwin Török @ 2019-07-24 10:21 UTC (permalink / raw)
To: cluster-devel.redhat.com
On 24/07/2019 11:02, Christoph Hellwig wrote:
> On Wed, Jul 24, 2019 at 09:48:38AM +0100, Steven Whitehouse wrote:
>> Hi,
>>
>> On 24/07/2019 09:43, Jia-Ju Bai wrote:
>>> In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
>>> NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
>>> cause a null-pointer dereference.
>>>
>>> To fix this null-pointer dereference, NULL is returned when ip is NULL.
>>>
>>> This bug is found by a static analysis tool STCheck written by us.
>>
>> The bug is in the tool I'm afraid. Since i_inode is the first element of ip,
>> there is no NULL dereference here. A pointer to ip->i_inode and a pointer to
>> ip are one and the same (bar the differing types) which is the reason that
>> we return &ip->i_inode rather than just ip,
>
> But that doesn't help if ip is NULL, as dereferencing a field in in
> still remains invalid behavior.
>
According to C99 you may be right that it is undefined behaviour, that just happens to work correctly on current versions
of GCC, see [1].
Although as pointed out in [1] this undefined behaviour was relied upon to implement the offsetof macro
(nowadays that should be a compiler built-in, see include/linux/stddef.h).
I wish that cases like these would be more clearly defined in the C standard, at least as implementation defined behavior such that GCC
could then be explicit about what semantics it wants and document it here: https://gcc.gnu.org/onlinedocs/gcc/#toc-C-Implementation-Defined-Behavior
It is difficult to write future-proof code if the compiler can keep changing its mind about semantics of code that it has previously accepted.
[1] https://software.intel.com/en-us/blogs/2015/04/20/null-pointer-dereferencing-causes-undefined-behavior
Best regards,
--Edwin
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 10:02 ` Christoph Hellwig
2019-07-24 10:21 ` Edwin Török
@ 2019-07-24 10:22 ` Steven Whitehouse
2019-07-24 10:27 ` Christoph Hellwig
1 sibling, 1 reply; 11+ messages in thread
From: Steven Whitehouse @ 2019-07-24 10:22 UTC (permalink / raw)
To: cluster-devel.redhat.com
Hi,
On 24/07/2019 11:02, Christoph Hellwig wrote:
> On Wed, Jul 24, 2019 at 09:48:38AM +0100, Steven Whitehouse wrote:
>> Hi,
>>
>> On 24/07/2019 09:43, Jia-Ju Bai wrote:
>>> In gfs2_alloc_inode(), when kmem_cache_alloc() on line 1724 returns
>>> NULL, ip is assigned to NULL. In this case, "return &ip->i_inode" will
>>> cause a null-pointer dereference.
>>>
>>> To fix this null-pointer dereference, NULL is returned when ip is NULL.
>>>
>>> This bug is found by a static analysis tool STCheck written by us.
>> The bug is in the tool I'm afraid. Since i_inode is the first element of ip,
>> there is no NULL dereference here. A pointer to ip->i_inode and a pointer to
>> ip are one and the same (bar the differing types) which is the reason that
>> we return &ip->i_inode rather than just ip,
> But that doesn't help if ip is NULL, as dereferencing a field in in
> still remains invalid behavior.
>
>
We are not dereferencing it though really, we are taking the address of
the field... we could have written:
return (struct inode *)ip;
and it would have the same effect, so far as I can tell. I don't mind
changing it, if that is perhaps a clearer way to write the same thing,
rather than &ip->i_inode;
Steve.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 10:22 ` Steven Whitehouse
@ 2019-07-24 10:27 ` Christoph Hellwig
2019-07-24 10:55 ` Steven Whitehouse
0 siblings, 1 reply; 11+ messages in thread
From: Christoph Hellwig @ 2019-07-24 10:27 UTC (permalink / raw)
To: cluster-devel.redhat.com
On Wed, Jul 24, 2019 at 11:22:46AM +0100, Steven Whitehouse wrote:
> and it would have the same effect, so far as I can tell. I don't mind
> changing it, if that is perhaps a clearer way to write the same thing,
> rather than &ip->i_inode;
The cleanest thing is to not rely on any of that magic and write it
like all other file systems:
ip = kmem_cache_alloc
if (!ip)
retuturn NULL;
...
return &ip->i_inode;
Absolutely not point in trying to be clever here.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 10:27 ` Christoph Hellwig
@ 2019-07-24 10:55 ` Steven Whitehouse
2019-07-24 11:03 ` Andreas Gruenbacher
0 siblings, 1 reply; 11+ messages in thread
From: Steven Whitehouse @ 2019-07-24 10:55 UTC (permalink / raw)
To: cluster-devel.redhat.com
Hi,
On 24/07/2019 11:27, Christoph Hellwig wrote:
> On Wed, Jul 24, 2019 at 11:22:46AM +0100, Steven Whitehouse wrote:
>> and it would have the same effect, so far as I can tell. I don't mind
>> changing it, if that is perhaps a clearer way to write the same thing,
>> rather than &ip->i_inode;
> The cleanest thing is to not rely on any of that magic and write it
> like all other file systems:
>
> ip = kmem_cache_alloc
> if (!ip)
> retuturn NULL;
>
> ...
>
> return &ip->i_inode;
>
> Absolutely not point in trying to be clever here.
Yes, that works too,
Steve.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Cluster-devel] [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode()
2019-07-24 10:55 ` Steven Whitehouse
@ 2019-07-24 11:03 ` Andreas Gruenbacher
0 siblings, 0 replies; 11+ messages in thread
From: Andreas Gruenbacher @ 2019-07-24 11:03 UTC (permalink / raw)
To: cluster-devel.redhat.com
On Wed, 24 Jul 2019 at 12:55, Steven Whitehouse <swhiteho@redhat.com> wrote:
> On 24/07/2019 11:27, Christoph Hellwig wrote:
> > On Wed, Jul 24, 2019 at 11:22:46AM +0100, Steven Whitehouse wrote:
> >> and it would have the same effect, so far as I can tell. I don't mind
> >> changing it, if that is perhaps a clearer way to write the same thing,
> >> rather than &ip->i_inode;
> > The cleanest thing is to not rely on any of that magic and write it
> > like all other file systems:
> >
> > ip = kmem_cache_alloc
> > if (!ip)
> > retuturn NULL;
> >
> > ...
> >
> > return &ip->i_inode;
> >
> > Absolutely not point in trying to be clever here.
>
> Yes, that works too,
I'll change that ...
Andreas
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2019-07-24 11:03 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-24 8:43 [PATCH] fs: gfs2: Fix a null-pointer dereference in gfs2_alloc_inode() Jia-Ju Bai
2019-07-24 8:43 ` [Cluster-devel] " Jia-Ju Bai
2019-07-24 8:48 ` Steven Whitehouse
2019-07-24 10:02 ` Christoph Hellwig
2019-07-24 10:21 ` Edwin Török
2019-07-24 10:22 ` Steven Whitehouse
2019-07-24 10:27 ` Christoph Hellwig
2019-07-24 10:55 ` Steven Whitehouse
2019-07-24 11:03 ` Andreas Gruenbacher
2019-07-24 10:00 ` Christoph Hellwig
2019-07-24 10:00 ` [Cluster-devel] " Christoph Hellwig
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.