ioctl_sg01: Skip USB devices
PATCH 1/2 ioctl_sg01: Skip USB devices
Some USB devices write hardware info and flags to the ioctl(SG_IO) response buffer which results in test failure. But the info is constant and doesn't represent any security risk. Skip USB devices to prevent false positives.
---
I've tested this patch on kernels v4.4 through v6.16. Non-USB generic SCSI block devices get correctly found and used, USB block device get skipped.
testcases/kernel/syscalls/ioctl/ioctl_sg01.c | 55 +++++++++++++++----- 1 file changed, 42 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +42 -13
@@ -29,6 +29,9 @@#include "tst_test.h" #include "tst_memutils.h" +#define SYSDIR "/sys/block" +#define BLOCKDIR "/sys/block/%s/device" +#define BUF_SIZE (128 * 4096)#define CMD_SIZE 6
@@ -38,42 +41,68 @@ static unsigned char command[CMD_SIZE];static struct sg_io_hdr query;/* TODO: split this off to a separate SCSI library? */ -static const char *find_generic_scsi_device (int access_flags) +static const char *find_generic_scsi_device (int access_flags,int skip_usb) { -DIR *devdir; +DIR *sysdir;struct dirent *ent;int tmpfd; -static char devpath[PATH_MAX ]; +ssize_t length; +char *filename; +static char devpath[PATH_MAX ], syspath[PATH_MAX ]; - errno =0 ; - devdir =opendir ("/dev" ); + sysdir =opendir (SYSDIR ); -if (!devdir) +if (!sysdir)return NULL ; -while ((ent =SAFE_READDIR (devdir))) { -/* The bug is most likely reproducible only on /dev/sg* */ -if (strncmp (ent->d_name,"sg" ,2 ) || !isdigit (ent->d_name[2 ])) +/* Scan block devices */ +while ((ent =SAFE_READDIR (sysdir))) { +if (ent->d_name[0 ] =='.' ) +continue ; + +snprintf (syspath,PATH_MAX ,BLOCKDIR , ent->d_name); + syspath[PATH_MAX -1 ] ='\0' ; + +/* Real device path matches the physical HW bus path */ +if (!realpath (syspath, devpath)) +continue ; + +strncat (devpath,"/generic" ,PATH_MAX -strlen (devpath) -1 ); + devpath[PATH_MAX -1 ] ='\0' ; + length =readlink (devpath, syspath,PATH_MAX -1 ); + +if (length <0 ) +continue ; + + syspath[length] ='\0' ; + filename =basename (syspath); + +/* USB devices often return HW info in SG_IO response buffer */ +if (skip_usb &&strstr (devpath,"/usb" )) { +tst_res (TINFO ,"Skipping USB device %s" , filename);continue ; + } -snprintf (devpath,PATH_MAX ,"/dev/%s" , ent->d_name); +snprintf (devpath,PATH_MAX ,"/dev/%s" , filename);/* access() makes incorrect assumptions about block devices */ tmpfd =open (devpath, access_flags);if (tmpfd >=0 ) {SAFE_CLOSE (tmpfd); -SAFE_CLOSEDIR (devdir); +SAFE_CLOSEDIR (sysdir);return devpath; } + +tst_res (TINFO |TERRNO ,"Cannot open device %s" , devpath); } -SAFE_CLOSEDIR (devdir); +SAFE_CLOSEDIR (sysdir);return NULL ; }static void setup (void ) { -const char *devpath =find_generic_scsi_device (O_RDONLY ); +const char *devpath =find_generic_scsi_device (O_RDONLY ,1 );if (!devpath)tst_brk (TCONF ,"Could not find any usable SCSI device" );
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/2 ioctl_sg01: Print buffer contents on failure
---
This patch is a convenience feature. Feel free to reject it.
testcases/kernel/syscalls/ioctl/ioctl_sg01.c | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +23 -1
@@ -36,7 +36,7 @@#define CMD_SIZE 6static int devfd =-1 ; -static char buffer[BUF_SIZE ]; +static char buffer[BUF_SIZE +1 ];static unsigned char command[CMD_SIZE ];static struct sg_io_hdr query;
@@ -100,6 +100,25 @@ static const char *find_generic_scsi_device(int access_flags, int skip_usb)return NULL ; } +static void dump_hex (const char *str,size_t size) +{ +size_t i; + +for (; size && !str[size -1 ]; size--) + ; + +for (i =0 ; i < size; i++) { +if (i && (i %32 ) ==0 ) +printf ("\n" ); +else if (i && (i %4 ) ==0 ) +printf (" " ); + +printf ("%02x" , (unsigned int )str[i]); + } + +printf ("\n" ); +} +static void setup (void ) {const char *devpath =find_generic_scsi_device (O_RDONLY ,1 );
@@ -135,6 +154,7 @@ static void run(void)for (i =0 ; i <100 ; i++) {TEST (ioctl (devfd,SG_IO , &query)); + buffer[BUF_SIZE ] ='\0' ;if (TST_RET !=0 &&TST_RET !=-1 )tst_brk (TBROK |TTERRNO ,"Invalid ioctl() return value" );
@@ -143,6 +163,8 @@ static void run(void)for (j =0 ; j <BUF_SIZE ; j++) {if (buffer[j]) {tst_res (TFAIL ,"Kernel memory leaked" ); +tst_res (TINFO ,"Buffer contents: %s" , buffer); +dump_hex (buffer,BUF_SIZE );return ; } }
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/2] ioctl_sg01: Print buffer contents on failure
Hi Martin,
Quoted 1 line
This patch is a convenience feature. Feel free to reject it.
IMHO it's useful to to print debug info.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/2] ioctl_sg01: Print buffer contents on failure
Hi Martin,
patchset merged, thank you for a great work.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/2] ioctl_sg01: Print buffer contents on failure
In reply to Martin DouchaHi!
Quoted 31 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +20
-0+static void dump_hex (const char *str,size_t size) +{ +size_t i; + +for (; size && !str[size -1 ]; size--) + ; + +for (i =0 ; i < size; i++) { +if (i && (i %32 ) ==0 ) +printf ("\n" ); +else if (i && (i %4 ) ==0 ) +printf (" " ); + +printf ("%02x" , (unsigned int )str[i]); + } + +printf ("\n" ); +} +static void setup (void ) {const char *devpath =find_generic_scsi_device (O_RDONLY ,1 );@@ -135,6 +154,7 @@ static void run(void)for (i =0 ; i <100 ; i++) {TEST (ioctl (devfd,SG_IO , &query)); + buffer[BUF_SIZE ] ='\0' ;if (TST_RET !=0 &&TST_RET !=-1 )tst_brk (TBROK |TTERRNO ,"Invalid ioctl() return value" );
testcases/kernel/syscalls/ioctl/ioctl_sg01.c static void run(void) · line 166
Quoted 4 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +1
-0@@ -143,6 +163,8 @@ static void run(void)for (j =0 ; j <BUF_SIZE ; j++) {if (buffer[j]) {tst_res (TFAIL ,"Kernel memory leaked" ); +tst_res (TINFO ,"Buffer contents: %s" , buffer);
Any reason why we don't use tst_res_hexd() instead?
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/2] ioctl_sg01: Print buffer contents on failure
Quoted 3 lines · Cyril Hrubis
Hi!
Any reason why we don't use
tst_res_hexd()instead?
Hi,
I didn't known about tst_res_hexd(). Please merge patch 1 and I'll send patch v2 for the hexdump afterwards.
Signature · 10 lines
Martin Doucha [email protected] SW Quality Engineer SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/2] ioctl_sg01: Print buffer contents on failure
Hi!
Quoted 2 lines
I didn't known about
tst_res_hexd(). Please merge patch 1 and I'll send patch v2 for the hexdump afterwards.
Too late for that, Petr already merged both, so please send a follow up that replaces the hexdump function with tst_res_hexd()
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin,
Quoted 9 lines
Some USB devices write hardware info and flags to the
ioctl(SG_IO)response buffer which results in test failure. But the info is constant and doesn't represent any security risk. Skip USB devices to prevent false positives.---
I've tested this patch on kernels v4.4 through v6.16. Non-USB generic SCSI block devices get correctly found and used, USB block device get skipped.
Thanks a lot for an extensive testing!
I was also verify on my machine with block device connected over USB that it's skipped (and indeed test was blocked on master).
Few notes below.
Quoted 34 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c | 55 +++++++++++++++----- 1 file changed, 42 insertions(+), 13 deletions(-)testcases/kernel/syscalls/ioctl/ioctl_sg01.c +8
-4@@ -29,6 +29,9 @@#include "tst_test.h" #include "tst_memutils.h" +#define SYSDIR "/sys/block" +#define BLOCKDIR "/sys/block/%s/device" +#define BUF_SIZE (128 * 4096)#define CMD_SIZE 6@@ -38,42 +41,68 @@ static unsigned char command[CMD_SIZE];static struct sg_io_hdr query;/* TODO: split this off to a separate SCSI library? */ -static const char *find_generic_scsi_device (int access_flags) +static const char *find_generic_scsi_device (int access_flags,int skip_usb) { -DIR *devdir; +DIR *sysdir;struct dirent *ent;int tmpfd; -static char devpath[PATH_MAX ]; +ssize_t length; +char *filename; +static char devpath[PATH_MAX ], syspath[PATH_MAX ]; - errno =0 ;
testcases/kernel/syscalls/ioctl/ioctl_sg01.c static unsigned char command[CMD_SIZE]; · line 56
Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +2
-5- devdir =opendir ("/dev" ); + sysdir =opendir (SYSDIR ); -if (!devdir) +if (!sysdir)return NULL ; -while ((ent =SAFE_READDIR (devdir))) { -/* The bug is most likely reproducible only on /dev/sg* */ -if (strncmp (ent->d_name,"sg" ,2 ) || !isdigit (ent->d_name[2 ]))
The kernel fix was done in drivers/scsi/sg.c, it made sense to check it.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a45b599ad808
testcases/kernel/syscalls/ioctl/ioctl_sg01.c static unsigned char command[CMD_SIZE]; · line 70
Quoted 13 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +13
-0+/* Scan block devices */ +while ((ent =SAFE_READDIR (sysdir))) { +if (ent->d_name[0 ] =='.' ) +continue ; + +snprintf (syspath,PATH_MAX ,BLOCKDIR , ent->d_name); + syspath[PATH_MAX -1 ] ='\0' ; + +/* Real device path matches the physical HW bus path */ +if (!realpath (syspath, devpath)) +continue ; + +strncat (devpath,"/generic" ,PATH_MAX -strlen (devpath) -1 );
On one baremetal machine and on VM with added SCSI device this approach really works (anything "/generic" was actually pointing to scsi_generic/sg*.
testcases/kernel/syscalls/ioctl/ioctl_sg01.c static unsigned char command[CMD_SIZE]; · line 81
Quoted 11 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +11
-0+ devpath[PATH_MAX -1 ] ='\0' ; + length =readlink (devpath, syspath,PATH_MAX -1 ); + +if (length <0 ) +continue ; + + syspath[length] ='\0' ; + filename =basename (syspath); + +/* USB devices often return HW info in SG_IO response buffer */ +if (skip_usb &&strstr (devpath,"/usb" )) {
very nit: I would personally avoid skip_usb variable because it is always 1 (skip unconditionally). Or actually allow to set it via getopts.
Kind regards,
Petr
...
Quoted 4 lines
testcases/kernel/syscalls/ioctl/ioctl_sg01.c +1
-1static void setup (void ) { -const char *devpath =find_generic_scsi_device (O_RDONLY ); +const char *devpath =find_generic_scsi_device (O_RDONLY ,1 );
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Martin,
Quoted 9 lines
Hi Martin,
Quoted 6 lines
Some USB devices write hardware info and flags to the
ioctl(SG_IO)response buffer which results in test failure. But the info is constant and doesn't represent any security risk. Skip USB devices to prevent false positives.---
Quoted 7 lines
Quoted 2 lines
I've tested this patch on kernels v4.4 through v6.16. Non-USB generic SCSI block devices get correctly found and used, USB block device get skipped.
Thanks a lot for an extensive testing!
I was also verify on my machine with block device connected over USB that it's skipped (and indeed test was blocked on master).
FYI running test with this HDD connected over USB no leaked info was printed (no false positive), but test runs very slow (10 iterations in ~5 min, it will likely manage below the limit, but 50x slower run is just waste of time given vast majority of testers runs tests sequentially).
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp