Blame SOURCES/0018-Fix-possible-UnicodeDecodeError-when-reading-sysfs-a.patch

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