From fcc5170a108fe7ce7c1cd5e31e06fa4dc9c6d76c Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Thu, 9 Feb 2023 11:20:37 +0100 Subject: [PATCH] Disk(Linux): Better detection of physical drives --- src/detection/disk/disk_linux.c | 45 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index c15eea812..7e6a1bc0b 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -15,28 +15,35 @@ #define readdir readdir64 #endif -static bool isPhysicalDevice(const char* device) +static bool isPhysicalDevice(FFstrbuf* device) { + struct stat deviceStat; + if(stat(device->chars, &deviceStat) != 0) + return false; + + //Ignore all devices that are not block devices + if(!S_ISBLK(deviceStat.st_mode)) + return false; + + //Ignore all devices that are not physical devices + + //DrvFs is a filesystem plugin to WSL that was designed to support interop between WSL and the Windows filesystem. - if(strcmp(device, "drvfs") == 0) + if(ffStrbufEqualS(device, "drvfs")) return true; //ZFS root pool. The format is rpool/// - if(strncmp(device, "rpool/", 6) == 0) + if(ffStrbufStartsWithS(device, "rpool/")) return true; //Pseudo filesystems don't have a device in /dev - const char* devPrefix = "/dev/"; - if(strncmp(device, devPrefix, strlen(devPrefix)) != 0) + if(!ffStrbufStartsWithS(device, "/dev/")) return false; - //Skip /dev/ prefix - device += strlen(devPrefix); - if( - strncmp(device, "loop", 4) == 0 || //Ignore loop devices - strncmp(device, "ram", 3) == 0 || //Ignore ram devices - strncmp(device, "fd", 2) == 0 //Ignore fd devices + ffStrbufStartsWithS(device, "/dev/loop") || //Ignore loop devices + ffStrbufStartsWithS(device, "/dev/ram") || //Ignore ram devices + ffStrbufStartsWithS(device, "/dev/fd") //Ignore fd devices ) return false; return true; @@ -208,12 +215,6 @@ void ffDetectDisksImpl(FFDiskResult* disks) while(getline(&line, &len, mountsFile) != EOF) { - if(!isPhysicalDevice(line)) - continue; - - //We have a valid device, add it to the list - FFDisk* disk = ffListAdd(&disks->disks); - //Format of the file: " ..." (Same as fstab) char* currentPos = line; @@ -222,6 +223,16 @@ void ffDetectDisksImpl(FFDiskResult* disks) ffStrbufInit(device); appendNextEntry(device, ¤tPos); + if(!isPhysicalDevice(device)) + { + ffStrbufDestroy(device); + devices.length--; + continue; + } + + //We have a valid device, add it to the list + FFDisk* disk = ffListAdd(&disks->disks); + //detect mountpoint ffStrbufInit(&disk->mountpoint); appendNextEntry(&disk->mountpoint, ¤tPos);