From 430cd2cdba8fba434b5bed2d2a7ed97803c62f6d Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Tue, 5 Jan 2021 16:56:52 +0100 Subject: [PATCH 1/3] Fix possible UnicodeDecodeError when reading sysfs attributes This is a follow-up for https://github.com/storaged-project/blivet/pull/861 where we fixed reading device model in "__is_blacklisted_blockdev" but we read the device model from other places too so it makes more sense to "fix" all sysfs attribute reads. --- blivet/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blivet/util.py b/blivet/util.py index 2fa9c8fc..48b7818f 100644 --- a/blivet/util.py +++ b/blivet/util.py @@ -379,7 +379,7 @@ def get_sysfs_attr(path, attr, root=None): log.warning("%s is not a valid attribute", attr) return None - f = open(fullattr, "r") + f = open(fullattr, "r", encoding="utf-8", errors="replace") data = f.read() f.close() sdata = "".join(["%02x" % (ord(x),) for x in data]) -- 2.29.2 From 15350b52f30910d4fadad92da0195710adcb69a0 Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Tue, 5 Jan 2021 16:59:14 +0100 Subject: [PATCH 2/3] Use util.get_sysfs_attr in __is_ignored_blockdev to read device mode --- blivet/udev.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/blivet/udev.py b/blivet/udev.py index 2c795225..25375459 100644 --- a/blivet/udev.py +++ b/blivet/udev.py @@ -185,9 +185,8 @@ def __is_blacklisted_blockdev(dev_name): if any(re.search(expr, dev_name) for expr in device_name_blacklist): return True - model_path = "/sys/class/block/%s/device/model" % dev_name - if os.path.exists(model_path): - model = open(model_path, encoding="utf-8", errors="replace").read() + model = util.get_sysfs_attr("/sys/class/block/%s" % dev_name, "device/model") + if model: for bad in ("IBM *STMF KERNEL", "SCEI Flash-5", "DGC LUNZ"): if model.find(bad) != -1: log.info("ignoring %s with model %s", dev_name, model) -- 2.29.2 From 64ece8c0dafb550bbde4798a766515fb04f44568 Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Wed, 6 Jan 2021 12:34:49 +0100 Subject: [PATCH 3/3] Add test for util.get_sysfs_attr --- tests/util_test.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/util_test.py b/tests/util_test.py index 9a2ff492..853b6166 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -2,7 +2,9 @@ import test_compat from six.moves import mock +import os import six +import tempfile import unittest from decimal import Decimal @@ -157,3 +159,24 @@ class DependencyGuardTestCase(unittest.TestCase): with mock.patch.object(_requires_something, '_check_avail', return_value=True): self.assertEqual(self._test_dependency_guard_non_critical(), True) self.assertEqual(self._test_dependency_guard_critical(), True) + + +class GetSysfsAttrTestCase(unittest.TestCase): + + def test_get_sysfs_attr(self): + + with tempfile.TemporaryDirectory() as sysfs: + model_file = os.path.join(sysfs, "model") + with open(model_file, "w") as f: + f.write("test model\n") + + model = util.get_sysfs_attr(sysfs, "model") + self.assertEqual(model, "test model") + + # now with some invalid byte in the model + with open(model_file, "wb") as f: + f.write(b"test model\xef\n") + + # the unicode replacement character (U+FFFD) should be used instead + model = util.get_sysfs_attr(sysfs, "model") + self.assertEqual(model, "test model\ufffd") -- 2.29.2