ext2: handle NULL fc->source in ext2_msg_fc()
Subsystems (2)
PATCH 1/1 ext2: handle NULL fc->source in ext2_msg_fc()
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.
--- fs/ext2/super.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
fs/ext2/super.c +7 -2
@@ -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); }
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 16 lines
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->sourceas NULL during option parsing.In
ext2_msg_fc(), whenfc->purpose!=FS_CONTEXT_FOR_RECONFIGURE,strrchr(fc->source, '/')is called unconditionally without checking iffc->sourceis 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/0x1e3Check
fc->sourcebefore callingstrrchr(), and omit the "(%s)" device prefix whens_idis NULL or empty (e.g., when source is unset, empty, or ends with '/').
Quoted 8 lines
Tested locally in QEMU (
x86_64, KASAN+UBSAN) using fsopen()/fsconfig() with NULL, empty, trailing-slash, relative, and full-path sources.Reported-by [email protected]Fixes 98f18c76c2c3 ("ext2: create ext2_msg_fc for use during parsing")Signed-off-by Arbab Haider <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 16 lines · Arbab Haider
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->sourceas NULL during option parsing.In
ext2_msg_fc(), whenfc->purpose!=FS_CONTEXT_FOR_RECONFIGURE,strrchr(fc->source, '/')is called unconditionally without checking iffc->sourceis 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/0x1e3Check
fc->sourcebefore callingstrrchr(), and omit the "(%s)" device prefix whens_idis NULL or empty (e.g., when source is unset, empty, or ends with '/').
Quoted 8 lines
Tested locally in QEMU (
x86_64, KASAN+UBSAN) using fsopen()/fsconfig() with NULL, empty, trailing-slash, relative, and full-path sources.Reported-by [email protected]Fixes 98f18c76c2c3 ("ext2: create ext2_msg_fc for use during parsing")Signed-off-by Arbab Haider <[email protected]>
Thanks but I've already merged a similar fix on Friday to my tree.
Honza
Quoted 39 lines
--- fs/ext2/super.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)fs/ext2/super.c +7
-2@@ -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); }Signature · 1 line
2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR