[PATCH] ext2: handle NULL fc->source in ext2_msg_fc()

Arbab Haider posted 1 patch 1 day, 17 hours ago
fs/ext2/super.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] ext2: handle NULL fc->source in ext2_msg_fc()
Posted by Arbab Haider 1 day, 17 hours ago
With the new mount API (fsopen()/fsconfig()), userspace can pass mount
options such as "nobh", "dax", or "reservation" before setting the
"source" parameter, leaving fc->source as NULL during option parsing.

In ext2_msg_fc(), when fc->purpose != FS_CONTEXT_FOR_RECONFIGURE,
strrchr(fc->source, '/') is called unconditionally without checking if
fc->source is non-NULL, causing a kernel NULL pointer dereference:

  KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
  RIP: 0010:strrchr+0x24/0x80
  ext2_msg_fc+0x157/0x1e3

Check fc->source before calling strrchr(), and omit the "(%s)" device
prefix when s_id is NULL or empty (e.g., when source is unset, empty,
or ends with '/').

Tested locally in QEMU (x86_64, KASAN+UBSAN) using fsopen()/fsconfig()
with NULL, empty, trailing-slash, relative, and full-path sources.

Reported-by: syzbot+1d23ad199a3bfd6f9149@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=1d23ad199a3bfd6f9149
Fixes: 98f18c76c2c3 ("ext2: create ext2_msg_fc for use during parsing")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext2/super.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/fs/ext2/super.c b/fs/ext2/super.c
index 54a6c26d0..c99367652 100644
--- a/fs/ext2/super.c
+++ b/fs/ext2/super.c
@@ -89,20 +89,25 @@ static void ext2_msg_fc(struct fs_context *fc, const char *prefix,
 
 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
 		s_id = fc->root->d_sb->s_id;
-	} else {
+	} else if (fc->source) {
 		/* get last path component of source */
 		s_id = strrchr(fc->source, '/');
 		if (s_id)
 			s_id++;
 		else
 			s_id = fc->source;
+	} else {
+		s_id = NULL;
 	}
 	va_start(args, fmt);
 
 	vaf.fmt = fmt;
 	vaf.va = &args;
 
-	printk("%sEXT2-fs (%s): %pV\n", prefix, s_id, &vaf);
+	if (s_id && *s_id)
+		printk("%sEXT2-fs (%s): %pV\n", prefix, s_id, &vaf);
+	else
+		printk("%sEXT2-fs: %pV\n", prefix, &vaf);
 
 	va_end(args);
 }
-- 
2.53.0
Re: [PATCH] ext2: handle NULL fc->source in ext2_msg_fc()
Posted by Jan Kara 1 day ago
On Sun 22-11-26 23:23:31, Arbab Haider wrote:
> With the new mount API (fsopen()/fsconfig()), userspace can pass mount
> options such as "nobh", "dax", or "reservation" before setting the
> "source" parameter, leaving fc->source as NULL during option parsing.
> 
> In ext2_msg_fc(), when fc->purpose != FS_CONTEXT_FOR_RECONFIGURE,
> strrchr(fc->source, '/') is called unconditionally without checking if
> fc->source is non-NULL, causing a kernel NULL pointer dereference:
> 
>   KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007]
>   RIP: 0010:strrchr+0x24/0x80
>   ext2_msg_fc+0x157/0x1e3
> 
> Check fc->source before calling strrchr(), and omit the "(%s)" device
> prefix when s_id is NULL or empty (e.g., when source is unset, empty,
> or ends with '/').
> 
> Tested locally in QEMU (x86_64, KASAN+UBSAN) using fsopen()/fsconfig()
> with NULL, empty, trailing-slash, relative, and full-path sources.
> 
> Reported-by: syzbot+1d23ad199a3bfd6f9149@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=1d23ad199a3bfd6f9149
> Fixes: 98f18c76c2c3 ("ext2: create ext2_msg_fc for use during parsing")
> Cc: stable@vger.kernel.org
> Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>

Thanks but I've already merged a similar fix on Friday to my tree.

								Honza

> ---
>  fs/ext2/super.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/fs/ext2/super.c b/fs/ext2/super.c
> index 54a6c26d0..c99367652 100644
> --- a/fs/ext2/super.c
> +++ b/fs/ext2/super.c
> @@ -89,20 +89,25 @@ static void ext2_msg_fc(struct fs_context *fc, const char *prefix,
>  
>  	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
>  		s_id = fc->root->d_sb->s_id;
> -	} else {
> +	} else if (fc->source) {
>  		/* get last path component of source */
>  		s_id = strrchr(fc->source, '/');
>  		if (s_id)
>  			s_id++;
>  		else
>  			s_id = fc->source;
> +	} else {
> +		s_id = NULL;
>  	}
>  	va_start(args, fmt);
>  
>  	vaf.fmt = fmt;
>  	vaf.va = &args;
>  
> -	printk("%sEXT2-fs (%s): %pV\n", prefix, s_id, &vaf);
> +	if (s_id && *s_id)
> +		printk("%sEXT2-fs (%s): %pV\n", prefix, s_id, &vaf);
> +	else
> +		printk("%sEXT2-fs: %pV\n", prefix, &vaf);
>  
>  	va_end(args);
>  }
> -- 
> 2.53.0
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR