From fa1d73084769ea4a6d07434e12dcae519938e4c5 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sun, 8 Nov 2015 17:15:24 +0100 Subject: [PATCH] dev-type: fix TOCTOU order Doing 'stat' checking first and later opening is racy. And since we do not really care about any 'status' info here and we read 'sysfs' here - just drop whole 'stat()' call and directly handle error from failing 'fopen()'. --- WHATS_NEW | 1 + lib/device/dev-type.c | 13 +++---------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index 6368b3273..19f24f73c 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.135 - ==================================== + Drop unneeded stat() call when checking for sysfs file. Fix memory leak on error path of failing thin-pool percentage check. Add missing test for failing node allocation in lvmetad. Correct configure messages when enabling/disabling lvmlockd. diff --git a/lib/device/dev-type.c b/lib/device/dev-type.c index a66ef9512..e1243bd4c 100644 --- a/lib/device/dev-type.c +++ b/lib/device/dev-type.c @@ -462,7 +462,6 @@ int dev_get_primary_dev(struct dev_types *dt, struct device *dev, dev_t *result) *result = dev->dev; ret = 1; goto out; /* dev is not a partition! */ - } /* @@ -486,17 +485,11 @@ int dev_get_primary_dev(struct dev_types *dt, struct device *dev, dev_t *result) } /* finally, parse 'dev' attribute and create corresponding dev_t */ - if (stat(path, &info) == -1) { + if (!(fp = fopen(path, "r"))) { if (errno == ENOENT) - log_error("sysfs file %s does not exist", path); + log_error("sysfs file %s does not exist.", path); else - log_sys_error("stat", path); - goto out; - } - - fp = fopen(path, "r"); - if (!fp) { - log_sys_error("fopen", path); + log_sys_error("fopen", path); goto out; }