Skip to contentSkip to conversation

libf2fs: quote non-printables when displaying "disk model"

PATCH In review Adam Borowski 3 participants 4 messages May 6, 2018 – May 28, 2018 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libf2fs: quote non-printables when displaying "disk model"

One offender I own has:
STORAGE DEVICE 1404x05xe3x07QGENEx00%x00x00x00x00x00x00x00x00x00x00x00x00x170x04

I limited it to pure ASCII only, but you might want to allow high-bit bytes, either validating UTF-8 or assuming that mojibake is not as bad as beep and zeroes as in the above example. Or, cutting off at the first control character might be a good alternative too.

---
 lib/libf2fs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/libf2fs.c +3 -2
index 5ef0214..8782afc 100644
@@ -884,8 +884,9 @@ int get_device_info(int i)
 
 			MSG(0, "Info: [%s] Disk Model: ",
 					dev->path);
-			while (reply_buffer[i] != '`' && i < 80)
-				printf("%c", reply_buffer[i++]);
+			for (; reply_buffer[i] != '`' && i < 80; i++)
+				printf((reply_buffer[i] >= ' ' && reply_buffer[i] < 127) ?
+				       "%c" : "x%02x", reply_buffer[i]);
 			printf("\n");
 		}
 #endif
Signature · 6 lines
2.17.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
#1 lore raw
Quoted 18 lines · Adam Borowski

One offender I own has:
STORAGE DEVICE 1404x05xe3x07QGENEx00%x00x00x00x00x00x00x00x00x00x00x00x00x170x04

I limited it to pure ASCII only, but you might want to allow high-bit bytes, either validating UTF-8 or assuming that mojibake is not as bad as beep and zeroes as in the above example. Or, cutting off at the first control character might be a good alternative too.

---
 lib/libf2fs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/libf2fs.c +0 -0
index 5ef0214..8782afc 100644

lib/libf2fs.c int get_device_info(int i) · line 888

Quoted 7 lines
lib/libf2fs.c +2 -2
@@ -884,8 +884,9 @@ int get_device_info(int i)
 
 			MSG(0, "Info: [%s] Disk Model: ",
 					dev->path);
-			while (reply_buffer[i] != '`' && i < 80)
-				printf("%c", reply_buffer[i++]);
+			for (; reply_buffer[i] != '`' && i < 80; i++)
+				printf((reply_buffer[i] >= ' ' && reply_buffer[i] < 127) ?

How about printing only valid characters here? In my virtualbox, this is showing garbage characters.

Thanks,

Quoted 6 lines
lib/libf2fs.c +1 -0
+				       "%c" : "x%02x", reply_buffer[i]);
 			printf("\n");
 		}
 #endif
Signature · 1 line
2.17.0

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot

#2 lore raw

PATCH v2 1/1 libf2fs: read "disk model" from SCSI ioctl the same way kernel does

Quoted 11 lines · Jaegeuk Kim
Quoted 7 lines · Adam Borowski

One offender I own has:
STORAGE DEVICE 1404x05xe3x07QGENEx00%x00x00x00x00x00x00x00x00x00x00x00x00x170x04

I limited it to pure ASCII only, but you might want to allow high-bit bytes, either validating UTF-8 or assuming that mojibake is not as bad as beep and zeroes as in the above example. Or, cutting off at the first control character might be a good alternative too.

How about printing only valid characters here? In my virtualbox, this is showing garbage characters.

I dug more, and it turns out that both the old way and what I had proposed are totally wrong. This field is not a real (ie, zero-terminated) string, and its length is 16 bytes rather than 64 as in current code. I went through all SD card readers and USB sticks I had at hand, and indeed the value is always padded with spaces. Stuff past byte 16 might be zeroes, spaces or garbage -- for real (SATA) disks it's clean, for USB stuff it tends to be junk. There's a four-byte "rev" field that's sometimes interesting, but even if we want to print it, it would require a separator.

So, here's another stab:

-- >8 --
Subject: libf2fs: read "disk model" from SCSI ioctl the same way kernel does

Ref: drivers/scsi/scsi_scan.c scsi_add_lun()

Ie, fixed-width 16 bytes, assumed to be filled with spaces -- NOT null-terminated; comments suggest that in some cases this field can be truncated and filled with nulls but printf is fine with that.

The old code did read up to 64 characters, which produced garbage for at least some USB-attached card readers. It also special-cased '`' character which the kernel does not.

---
 lib/libf2fs.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
lib/libf2fs.c +2 -7
index 5ef0214..38480c5 100644
@@ -880,13 +880,8 @@ int get_device_info(int i)
 		io_hdr.timeout = 1000;
 
 		if (!ioctl(fd, SG_IO, &io_hdr)) {
-			int i = 16;
-
-			MSG(0, "Info: [%s] Disk Model: ",
-					dev->path);
-			while (reply_buffer[i] != '`' && i < 80)
-				printf("%c", reply_buffer[i++]);
-			printf("\n");
+			MSG(0, "Info: [%s] Disk Model: %.16s\n",
+					dev->path, reply_buffer+16);
 		}
 #endif
 	} else {
Signature · 5 lines
2.17.0

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Reviewed-by on v2 1/1
#3 lore raw

Re: [PATCH v2] libf2fs: read "disk model" from SCSI ioctl the same way kernel does

Quoted 33 lines · Adam Borowski
Quoted 11 lines · Jaegeuk Kim
Quoted 7 lines · Adam Borowski

One offender I own has:
STORAGE DEVICE 1404x05xe3x07QGENEx00%x00x00x00x00x00x00x00x00x00x00x00x00x170x04

I limited it to pure ASCII only, but you might want to allow high-bit bytes, either validating UTF-8 or assuming that mojibake is not as bad as beep and zeroes as in the above example. Or, cutting off at the first control character might be a good alternative too.

How about printing only valid characters here? In my virtualbox, this is showing garbage characters.

I dug more, and it turns out that both the old way and what I had proposed are totally wrong. This field is not a real (ie, zero-terminated) string, and its length is 16 bytes rather than 64 as in current code. I went through all SD card readers and USB sticks I had at hand, and indeed the value is always padded with spaces. Stuff past byte 16 might be zeroes, spaces or garbage -- for real (SATA) disks it's clean, for USB stuff it tends to be junk. There's a four-byte "rev" field that's sometimes interesting, but even if we want to print it, it would require a separator.

So, here's another stab:

-- >8 --
Subject: libf2fs: read "disk model" from SCSI ioctl the same way kernel does

Ref: drivers/scsi/scsi_scan.c scsi_add_lun()

Ie, fixed-width 16 bytes, assumed to be filled with spaces -- NOT null-terminated; comments suggest that in some cases this field can be truncated and filled with nulls but printf is fine with that.

Quoted 5 lines

The old code did read up to 64 characters, which produced garbage for at least some USB-attached card readers. It also special-cased '`' character which the kernel does not.

Thanks,

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot