Skip to contentSkip to conversation

add some safety checks for label store interface of flash dev in sysfs

PATCH In review Dongbo Cao 2 participants 2 messages Sep 14, 2018 – Sep 15, 2018 bcache (block layer cache)

lore
Share & tools
lore

Dongbo Cao

author
#0 lore raw

PATCH 2/2 add some safety checks for label store interface of flash dev in sysfs

do some checks on the label's length and ending.

---
 drivers/md/bcache/sysfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/md/bcache/sysfs.c +8 -1
index e64c718f..cce793ef 100644
@@ -447,8 +447,15 @@ STORE(__bch_flash_dev)
 	}
 
 	if (attr == &sysfs_label) {
+		if (size > SB_LABEL_SIZE)
+			return -EINVAL;
 		memcpy(u->label, buf, SB_LABEL_SIZE);
-		bch_uuid_write(d->c);
+		if (size < SB_LABEL_SIZE)
+			u->label[size] = '\0';
+		if (size && u->label[size - 1] == '\n')
+			u->label[size - 1] = '\0';
+		if(d->c)
+			bch_uuid_write(d->c);
 	}
 
 	if (attr == &sysfs_unregister) {
Signature · 1 line
2.17.1
#1 lore raw

Seems something wrong
when size = SB_LABEL_SIZE and u->label[size - 1] != '\n'
please check again.

---------------------------------------------------------------------------
Tang Junhui
Forever me is me
---------------------------------------------------------------------------

Quoted 33 lines · Dongbo Cao <[email protected]> 于2018年9月14日周五 下午6:13写道:

do some checks on the label's length and ending.

---
 drivers/md/bcache/sysfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/md/bcache/sysfs.c +8 -2
index e64c718f..cce793ef 100644
@@ -447,8 +447,15 @@ STORE(__bch_flash_dev)
        }
 
        if (attr == &sysfs_label) {
+               if (size > SB_LABEL_SIZE)
+                       return -EINVAL;
                memcpy(u->label, buf, SB_LABEL_SIZE);
-               bch_uuid_write(d->c);
+               if (size < SB_LABEL_SIZE)
+                       u->label[size] = '\0';
+               if (size && u->label[size - 1] == '\n')
+                       u->label[size - 1] = '\0';
+               if(d->c)
+                       bch_uuid_write(d->c);
        }
 
        if (attr == &sysfs_unregister) {
--

2.17.1