1

nilfs2: protect references to superblock parameters exposed in sysfs

The superblock buffers of nilfs2 can not only be overwritten at runtime
for modifications/repairs, but they are also regularly swapped, replaced
during resizing, and even abandoned when degrading to one side due to
backing device issues.  So, accessing them requires mutual exclusion using
the reader/writer semaphore "nilfs->ns_sem".

Some sysfs attribute show methods read this superblock buffer without the
necessary mutual exclusion, which can cause problems with pointer
dereferencing and memory access, so fix it.

Link: https://lkml.kernel.org/r/20240811100320.9913-1-konishi.ryusuke@gmail.com
Fixes: da7141fb78 ("nilfs2: add /sys/fs/nilfs2/<device> group")
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Ryusuke Konishi 2024-08-11 19:03:20 +09:00 committed by Andrew Morton
parent 4828d207dc
commit 6834082589

View File

@ -836,9 +836,15 @@ ssize_t nilfs_dev_revision_show(struct nilfs_dev_attr *attr,
struct the_nilfs *nilfs,
char *buf)
{
struct nilfs_super_block **sbp = nilfs->ns_sbp;
u32 major = le32_to_cpu(sbp[0]->s_rev_level);
u16 minor = le16_to_cpu(sbp[0]->s_minor_rev_level);
struct nilfs_super_block *raw_sb;
u32 major;
u16 minor;
down_read(&nilfs->ns_sem);
raw_sb = nilfs->ns_sbp[0];
major = le32_to_cpu(raw_sb->s_rev_level);
minor = le16_to_cpu(raw_sb->s_minor_rev_level);
up_read(&nilfs->ns_sem);
return sysfs_emit(buf, "%d.%d\n", major, minor);
}
@ -856,8 +862,13 @@ ssize_t nilfs_dev_device_size_show(struct nilfs_dev_attr *attr,
struct the_nilfs *nilfs,
char *buf)
{
struct nilfs_super_block **sbp = nilfs->ns_sbp;
u64 dev_size = le64_to_cpu(sbp[0]->s_dev_size);
struct nilfs_super_block *raw_sb;
u64 dev_size;
down_read(&nilfs->ns_sem);
raw_sb = nilfs->ns_sbp[0];
dev_size = le64_to_cpu(raw_sb->s_dev_size);
up_read(&nilfs->ns_sem);
return sysfs_emit(buf, "%llu\n", dev_size);
}
@ -879,9 +890,15 @@ ssize_t nilfs_dev_uuid_show(struct nilfs_dev_attr *attr,
struct the_nilfs *nilfs,
char *buf)
{
struct nilfs_super_block **sbp = nilfs->ns_sbp;
struct nilfs_super_block *raw_sb;
ssize_t len;
return sysfs_emit(buf, "%pUb\n", sbp[0]->s_uuid);
down_read(&nilfs->ns_sem);
raw_sb = nilfs->ns_sbp[0];
len = sysfs_emit(buf, "%pUb\n", raw_sb->s_uuid);
up_read(&nilfs->ns_sem);
return len;
}
static
@ -889,10 +906,16 @@ ssize_t nilfs_dev_volume_name_show(struct nilfs_dev_attr *attr,
struct the_nilfs *nilfs,
char *buf)
{
struct nilfs_super_block **sbp = nilfs->ns_sbp;
struct nilfs_super_block *raw_sb;
ssize_t len;
return scnprintf(buf, sizeof(sbp[0]->s_volume_name), "%s\n",
sbp[0]->s_volume_name);
down_read(&nilfs->ns_sem);
raw_sb = nilfs->ns_sbp[0];
len = scnprintf(buf, sizeof(raw_sb->s_volume_name), "%s\n",
raw_sb->s_volume_name);
up_read(&nilfs->ns_sem);
return len;
}
static const char dev_readme_str[] =