Fix search for device ID in tst_find_backing_dev()
PATCH 1/1 Fix search for device ID in tst_find_backing_dev()
The current search using substrings can lead to false positives, e.g. search for device 0:5 could return device 10:50 instead. Parse device IDs in /proc/self/mountinfo properly using sscanf() and also check whether the device was really found.
--- lib/tst_device.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
lib/tst_device.c +12 -4
@@ -497,21 +497,26 @@ unsigned long tst_dev_bytes_written(const char *dev)void tst_find_backing_dev (const char *path,char *dev) { -char fmt[20 ];struct stat buf;FILE *file;char line[PATH_MAX ];char *pre =NULL ;char *next =NULL ; +unsigned int dev_major, dev_minor, line_mjr, line_mnr;if (stat (path, &buf) <0 )tst_brkm (TWARN |TERRNO ,NULL ,"stat() failed" ); -snprintf (fmt,sizeof (fmt),"%u:%u" ,major (buf.st_dev),minor (buf.st_dev)); + dev_major =major (buf.st_dev); + dev_minor =minor (buf.st_dev); file =SAFE_FOPEN (NULL ,"/proc/self/mountinfo" ,"r" ); + *dev ='\0' ;while (fgets (line,sizeof (line), file)) { -if (strstr (line, fmt) !=NULL ) { +if (sscanf (line,"%*d %*d %d:%d" , &line_mjr, &line_mnr) !=2 ) +continue ; + +if (line_mjr == dev_major && line_mnr == dev_minor) { pre =strstr (line," - " ); pre =strtok_r (pre," " , &next); pre =strtok_r (NULL ," " , &next);
@@ -523,8 +528,11 @@ void tst_find_backing_dev(const char *path, char *dev)SAFE_FCLOSE (NULL , file); +if (!*dev) +tst_brkm (TBROK ,NULL ,"Cannot find block device for %s" , path); +if (stat (dev, &buf) <0 ) -tst_brkm (TWARN |TERRNO ,NULL ,"stat(%s) failed" , dev); +tst_brkm (TWARN |TERRNO ,NULL ,"stat(%s) failed" , dev);if (S_ISBLK (buf.st_mode) !=1 )tst_brkm (TCONF ,NULL ,"dev(%s) isn't a block dev" , dev);
Signature · 1 line
2.28.0
PATCH 1/1 Fix search for device ID in tst_find_backing_dev()
Quoted 6 lines · Martin Doucha
The current search using substrings can lead to false positives, e.g. search for device 0:5 could return device 10:50 instead. Parse device IDs in
/proc/self/mountinfoproperly usingsscanf()and also check whether the device was really found.Signed-off-by Martin Doucha <[email protected]>
I forgot to add an extra comment that I still don't like that the search uses fixed-size buffer without properly handling lines that are too long but let's keep the fix simple for now. We can fix long line handling after the release.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 1/1 Fix search for device ID in tst_find_backing_dev()
In reply to Martin DouchaHi!
Applied, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]