resource: disk: Remove unnecessary pre-checking for opening file 36/280236/2
authorDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 25 Aug 2022 08:15:24 +0000 (17:15 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 30 Aug 2022 00:29:40 +0000 (09:29 +0900)
Instead of checking file existence before opening, now just try to
open file. If the file does not exist, it makes errors though.
This also fixes the vulnerability reports about TOCTOU race condition
(CWE-367).

Change-Id: I10affc264666566b635e1cd8b91ad34fe5613845
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/resource/resource-disk.c

index cc6a983..a28f216 100644 (file)
@@ -235,11 +235,12 @@ static int read_disk_stats(char *device_name, struct io_stats *ios)
 
        snprintf(filename, BUFF_MAX, "/sys/class/block/%s/stat", device_name);
 
-       if (access(filename, F_OK) == -1) {
-               _E("There is no block device(%s)\n", device_name);
-               return -ENOENT;
-       } else if ((fp = fopen(filename, "r")) == NULL) {
-               _E("failed to open block device(%s)\n", device_name);
+       fp = fopen(filename, "r");
+       if (!fp) {
+               char errstr[BUFF_MAX];
+
+               strerror_r(errno, errstr, BUFF_MAX);
+               _E("failed to open block device(%s):%s\n", device_name, errstr);
                return -ENOENT;
        }