]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ext4: avoid potential buffer over-read in parse_apply_sb_mount_options()
authorTheodore Ts'o <tytso@mit.edu>
Tue, 21 Oct 2025 17:04:01 +0000 (13:04 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Oct 2025 13:04:41 +0000 (14:04 +0100)
[ Upstream commit 8ecb790ea8c3fc69e77bace57f14cf0d7c177bd8 ]

Unlike other strings in the ext4 superblock, we rely on tune2fs to
make sure s_mount_opts is NUL terminated.  Harden
parse_apply_sb_mount_options() by treating s_mount_opts as a potential
__nonstring.

Cc: stable@vger.kernel.org
Fixes: 8b67f04ab9de ("ext4: Add mount options in superblock")
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Message-ID: <20250916-tune2fs-v2-1-d594dc7486f0@mit.edu>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
[ added sizeof() third argument to strscpy_pad() ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext4/super.c

index ab99197ee22ed97d65cad6622d4ad8e6cb1682b9..b563c2e59227f2c9f82a9592545079caa859990b 100644 (file)
@@ -2415,7 +2415,7 @@ static int parse_apply_sb_mount_options(struct super_block *sb,
                                        struct ext4_fs_context *m_ctx)
 {
        struct ext4_sb_info *sbi = EXT4_SB(sb);
-       char *s_mount_opts = NULL;
+       char s_mount_opts[65];
        struct ext4_fs_context *s_ctx = NULL;
        struct fs_context *fc = NULL;
        int ret = -ENOMEM;
@@ -2423,15 +2423,11 @@ static int parse_apply_sb_mount_options(struct super_block *sb,
        if (!sbi->s_es->s_mount_opts[0])
                return 0;
 
-       s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
-                               sizeof(sbi->s_es->s_mount_opts),
-                               GFP_KERNEL);
-       if (!s_mount_opts)
-               return ret;
+       strscpy_pad(s_mount_opts, sbi->s_es->s_mount_opts, sizeof(s_mount_opts));
 
        fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
        if (!fc)
-               goto out_free;
+               return -ENOMEM;
 
        s_ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
        if (!s_ctx)
@@ -2463,11 +2459,8 @@ parse_failed:
        ret = 0;
 
 out_free:
-       if (fc) {
-               ext4_fc_free(fc);
-               kfree(fc);
-       }
-       kfree(s_mount_opts);
+       ext4_fc_free(fc);
+       kfree(fc);
        return ret;
 }