From fa83be69c926610add026a53b50714c707a8c965 Mon Sep 17 00:00:00 2001 From: Pradeep P V K Date: Fri, 25 Mar 2022 15:15:58 +0530 Subject: [PATCH] ubi: Use PAGE_SIZE as size field for ubi sysfs show attributes To avoid truncation and to show correct ubi sysfs show attribute values, use PAGE_SIZE as a buffer size value. Change-Id: I4cf84967437b7baae0f10f00fd3d8726f780af16 Signed-off-by: Pradeep P V K --- drivers/mtd/ubi/build.c | 34 +++++++++++++++++----------------- drivers/mtd/ubi/vmt.c | 25 +++++++++++-------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index 971c3b7a51c3..2eff13c24253 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -95,7 +95,7 @@ static DEFINE_SPINLOCK(ubi_devices_lock); static ssize_t version_show(struct class *class, struct class_attribute *attr, char *buf) { - return scnprintf(buf, sizeof(int), "%d\n", UBI_VERSION); + return scnprintf(buf, PAGE_SIZE, "%d\n", UBI_VERSION); } static CLASS_ATTR_RO(version); @@ -376,49 +376,49 @@ static ssize_t dev_attribute_show(struct device *dev, return -ENODEV; if (attr == &dev_eraseblock_size) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->leb_size); else if (attr == &dev_avail_eraseblocks) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->avail_pebs); else if (attr == &dev_total_eraseblocks) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->good_peb_count); else if (attr == &dev_volumes_count) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT); else if (attr == &dev_max_ec) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->max_ec); else if (attr == &dev_reserved_for_bad) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->beb_rsvd_pebs); else if (attr == &dev_bad_peb_count) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->bad_peb_count); else if (attr == &dev_max_vol_count) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->vtbl_slots); else if (attr == &dev_min_io_size) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->min_io_size); else if (attr == &dev_bgt_enabled) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->thread_enabled); else if (attr == &dev_mtd_num) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->mtd->index); else if (attr == &dev_ro_mode) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", ubi->ro_mode); else if (attr == &dev_mtd_trigger_scrub) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&ubi->scrub_work_count)); else if (attr == &dev_mtd_max_scrub_sqnum) - ret = scnprintf(buf, sizeof(unsigned long long), "%llu\n", + ret = scnprintf(buf, PAGE_SIZE, "%llu\n", get_max_sqnum(ubi)); else if (attr == &dev_mtd_min_scrub_sqnum) - ret = scnprintf(buf, sizeof(unsigned long long), "%llu\n", + ret = scnprintf(buf, PAGE_SIZE, "%llu\n", ubi_wl_scrub_get_min_sqnum(ubi)); else ret = -EINVAL; @@ -516,7 +516,7 @@ static int uif_init(struct ubi_device *ubi) int i, err; dev_t dev; - scnprintf(ubi->ubi_name, sizeof(UBI_NAME_STR) + 5, + scnprintf(ubi->ubi_name, sizeof(ubi->ubi_name), UBI_NAME_STR "%d", ubi->ubi_num); /* diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c index e1fa76b88469..c1d562f6b7e5 100644 --- a/drivers/mtd/ubi/vmt.c +++ b/drivers/mtd/ubi/vmt.c @@ -54,7 +54,7 @@ static struct device_attribute attr_vol_upd_marker = static ssize_t vol_attribute_show(struct device *dev, struct device_attribute *attr, char *buf) { - int ret, size; + int ret; struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev); struct ubi_device *ubi; @@ -73,35 +73,32 @@ static ssize_t vol_attribute_show(struct device *dev, spin_unlock(&ubi->volumes_lock); if (attr == &attr_vol_reserved_ebs) - ret = scnprintf(buf, sizeof(int), "%d\n", vol->reserved_pebs); + ret = scnprintf(buf, PAGE_SIZE, "%d\n", vol->reserved_pebs); else if (attr == &attr_vol_type) { const char *tp; - if (vol->vol_type == UBI_DYNAMIC_VOLUME) { + if (vol->vol_type == UBI_DYNAMIC_VOLUME) tp = "dynamic"; - size = 8; - } else { + else tp = "static"; - size = 7; - } - ret = scnprintf(buf, size, "%s\n", tp); + ret = scnprintf(buf, PAGE_SIZE, "%s\n", tp); } else if (attr == &attr_vol_name) - ret = scnprintf(buf, UBI_MAX_VOLUME_NAME + 1, "%s\n", + ret = scnprintf(buf, PAGE_SIZE, "%s\n", vol->name); else if (attr == &attr_vol_corrupted) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", vol->corrupted); else if (attr == &attr_vol_alignment) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", vol->alignment); else if (attr == &attr_vol_usable_eb_size) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", vol->usable_leb_size); else if (attr == &attr_vol_data_bytes) - ret = scnprintf(buf, sizeof(unsigned long long), "%lld\n", + ret = scnprintf(buf, PAGE_SIZE, "%lld\n", vol->used_bytes); else if (attr == &attr_vol_upd_marker) - ret = scnprintf(buf, sizeof(int), "%d\n", + ret = scnprintf(buf, PAGE_SIZE, "%d\n", vol->upd_marker); else /* This must be a bug */