2024-03-29 12:24:59

by Artem Chernyshev

[permalink] [raw]
Subject: [PATCH] fs/jffs2: Fix NULL deref in jffs2_scan_dirty_space

As was mentioned in 2ebf09c2, it is possible to get oops,
when marking space dirty in scan, but no previous node exists

It still can be in jffs2_link_node_ref() via deref jeb->last_node.
Since all jffs2_scan_dirty_space() callers check value of a
function, we can return error code safely.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Artem Chernyshev <[email protected]>
---
fs/jffs2/nodelist.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/jffs2/nodelist.c b/fs/jffs2/nodelist.c
index b86c78d178c6..6bebf1d64000 100644
--- a/fs/jffs2/nodelist.c
+++ b/fs/jffs2/nodelist.c
@@ -669,8 +669,11 @@ int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
size, jeb->free_size, jeb->wasted_size);
BUG();
}
+ if (!jeb->last_node) {
+ return -EINVAL;
+ }
/* REF_EMPTY_NODE is !obsolete, so that works OK */
- if (jeb->last_node && ref_obsolete(jeb->last_node)) {
+ if (ref_obsolete(jeb->last_node) {
#ifdef TEST_TOTLEN
jeb->last_node->__totlen += size;
#endif
--
2.37.3



2024-05-12 20:48:58

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH] fs/jffs2: Fix NULL deref in jffs2_scan_dirty_space

----- Ursprüngliche Mail -----
> Von: "Artem Chernyshev" <[email protected]>
> An: "David Woodhouse" <[email protected]>, "richard" <[email protected]>
> CC: "Artem Chernyshev" <[email protected]>, "linux-mtd" <[email protected]>, "linux-kernel"
> <[email protected]>, [email protected]
> Gesendet: Freitag, 29. März 2024 13:16:29
> Betreff: [PATCH] fs/jffs2: Fix NULL deref in jffs2_scan_dirty_space

> As was mentioned in 2ebf09c2, it is possible to get oops,
> when marking space dirty in scan, but no previous node exists
>
> It still can be in jffs2_link_node_ref() via deref jeb->last_node.
> Since all jffs2_scan_dirty_space() callers check value of a
> function, we can return error code safely.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Signed-off-by: Artem Chernyshev <[email protected]>
> ---
> fs/jffs2/nodelist.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/fs/jffs2/nodelist.c b/fs/jffs2/nodelist.c
> index b86c78d178c6..6bebf1d64000 100644
> --- a/fs/jffs2/nodelist.c
> +++ b/fs/jffs2/nodelist.c
> @@ -669,8 +669,11 @@ int jffs2_scan_dirty_space(struct jffs2_sb_info *c, struct
> jffs2_eraseblock *jeb
> size, jeb->free_size, jeb->wasted_size);
> BUG();
> }
> + if (!jeb->last_node) {
> + return -EINVAL;
> + }

Did you test this change? I'm not so sure whether returning -EINVAL is
correct.

Thanks,
//richard