neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

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

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