Blame SOURCES/sos-bz1965001-fix-avc-copystating-proc-sys.patch

ecf6d6
From 206d65618f20995b168dcc63090d1e6871450e90 Mon Sep 17 00:00:00 2001
ecf6d6
From: Pavel Moravec <pmoravec@redhat.com>
ecf6d6
Date: Wed, 26 May 2021 15:45:26 +0200
ecf6d6
Subject: [PATCH] [archive] skip copying SELinux context for /proc and /sys
ecf6d6
 everytime
ecf6d6
ecf6d6
A supplement of #1399 fix, now also for adding strings or special
ecf6d6
device files.
ecf6d6
ecf6d6
Also adding a (vendor) test case for it.
ecf6d6
ecf6d6
Resolves: #2560
ecf6d6
ecf6d6
Signed-off-by: Pavel Moravec <pmoravec@redhat.com>
ecf6d6
---
ecf6d6
 sos/archive.py                           | 35 +++++++++++----------
ecf6d6
 tests/vendor_tests/redhat/rhbz1965001.py | 39 ++++++++++++++++++++++++
ecf6d6
 2 files changed, 56 insertions(+), 18 deletions(-)
ecf6d6
 create mode 100644 tests/vendor_tests/redhat/rhbz1965001.py
ecf6d6
ecf6d6
diff --git a/sos/archive.py b/sos/archive.py
ecf6d6
index 4dd31d75..b02b2475 100644
ecf6d6
--- a/sos/archive.py
ecf6d6
+++ b/sos/archive.py
ecf6d6
@@ -326,6 +326,20 @@ class FileCacheArchive(Archive):
ecf6d6
             return None
ecf6d6
         return dest
ecf6d6
 
ecf6d6
+    def _copy_attributes(self, src, dest):
ecf6d6
+        # copy file attributes, skip SELinux xattrs for /sys and /proc
ecf6d6
+        try:
ecf6d6
+            stat = os.stat(src)
ecf6d6
+            if src.startswith("/sys/") or src.startswith("/proc/"):
ecf6d6
+                shutil.copymode(src, dest)
ecf6d6
+                os.utime(dest, ns=(stat.st_atime_ns, stat.st_mtime_ns))
ecf6d6
+            else:
ecf6d6
+                shutil.copystat(src, dest)
ecf6d6
+            os.chown(dest, stat.st_uid, stat.st_gid)
ecf6d6
+        except Exception as e:
ecf6d6
+            self.log_debug("caught '%s' setting attributes of '%s'"
ecf6d6
+                           % (e, dest))
ecf6d6
+
ecf6d6
     def add_file(self, src, dest=None):
ecf6d6
         with self._path_lock:
ecf6d6
             if not dest:
ecf6d6
@@ -348,18 +362,7 @@ class FileCacheArchive(Archive):
ecf6d6
                     else:
ecf6d6
                         self.log_info("File %s not collected: '%s'" % (src, e))
ecf6d6
 
ecf6d6
-                # copy file attributes, skip SELinux xattrs for /sys and /proc
ecf6d6
-                try:
ecf6d6
-                    stat = os.stat(src)
ecf6d6
-                    if src.startswith("/sys/") or src.startswith("/proc/"):
ecf6d6
-                        shutil.copymode(src, dest)
ecf6d6
-                        os.utime(dest, ns=(stat.st_atime_ns, stat.st_mtime_ns))
ecf6d6
-                    else:
ecf6d6
-                        shutil.copystat(src, dest)
ecf6d6
-                    os.chown(dest, stat.st_uid, stat.st_gid)
ecf6d6
-                except Exception as e:
ecf6d6
-                    self.log_debug("caught '%s' setting attributes of '%s'"
ecf6d6
-                                   % (e, dest))
ecf6d6
+                self._copy_attributes(src, dest)
ecf6d6
                 file_name = "'%s'" % src
ecf6d6
             else:
ecf6d6
                 # Open file case: first rewind the file to obtain
ecf6d6
@@ -388,11 +391,7 @@ class FileCacheArchive(Archive):
ecf6d6
                 content = content.decode('utf8', 'ignore')
ecf6d6
             f.write(content)
ecf6d6
             if os.path.exists(src):
ecf6d6
-                try:
ecf6d6
-                    shutil.copystat(src, dest)
ecf6d6
-                except OSError as e:
ecf6d6
-                    self.log_error("Unable to add '%s' to archive: %s" %
ecf6d6
-                                   (dest, e))
ecf6d6
+                self._copy_attributes(src, dest)
ecf6d6
             self.log_debug("added string at '%s' to FileCacheArchive '%s'"
ecf6d6
                            % (src, self._archive_root))
ecf6d6
 
ecf6d6
@@ -501,7 +500,7 @@ class FileCacheArchive(Archive):
ecf6d6
                     self.log_info("add_node: %s - mknod '%s'" % (msg, dest))
ecf6d6
                     return
ecf6d6
                 raise e
ecf6d6
-            shutil.copystat(path, dest)
ecf6d6
+            self._copy_attributes(path, dest)
ecf6d6
 
ecf6d6
     def name_max(self):
ecf6d6
         if 'PC_NAME_MAX' in os.pathconf_names:
ecf6d6
diff --git a/tests/vendor_tests/redhat/rhbz1965001.py b/tests/vendor_tests/redhat/rhbz1965001.py
ecf6d6
new file mode 100644
ecf6d6
index 00000000..aa16ba81
ecf6d6
--- /dev/null
ecf6d6
+++ b/tests/vendor_tests/redhat/rhbz1965001.py
ecf6d6
@@ -0,0 +1,39 @@
ecf6d6
+# This file is part of the sos project: https://github.com/sosreport/sos
ecf6d6
+#
ecf6d6
+# This copyrighted material is made available to anyone wishing to use,
ecf6d6
+# modify, copy, or redistribute it subject to the terms and conditions of
ecf6d6
+# version 2 of the GNU General Public License.
ecf6d6
+#
ecf6d6
+# See the LICENSE file in the source distribution for further information.
ecf6d6
+
ecf6d6
+
ecf6d6
+import tempfile
ecf6d6
+import shutil
ecf6d6
+from sos_tests import StageOneReportTest
ecf6d6
+
ecf6d6
+
ecf6d6
+class rhbz1965001(StageOneReportTest):
ecf6d6
+    """
ecf6d6
+    Copying /proc/sys/vm/{compact_memory,drop_caches} must ignore SELinux
ecf6d6
+    context, otherwise an attempt to set the context to files under some
ecf6d6
+    directories like /tmp raises an AVC denial, and an ERROR
ecf6d6
+    "Unable to add '...' to archive: [Errno 13] Permission denied: '...'
ecf6d6
+    is raise.
ecf6d6
+
ecf6d6
+    https://bugzilla.redhat.com/show_bug.cgi?id=1965001
ecf6d6
+
ecf6d6
+    :avocado: enable
ecf6d6
+    :avocado: tags=stageone
ecf6d6
+    """
ecf6d6
+
ecf6d6
+    sos_cmd = '-o system'
ecf6d6
+    # it is crucial to run the test case with --tmp-dir=/tmp/... as that is
ecf6d6
+    # (an example of) directory exhibiting the relabel permission deny.
ecf6d6
+    # /var/tmp directory allows those relabels.
ecf6d6
+    #
ecf6d6
+    # the directory shouldn't exist at this moment, otherwise
ecf6d6
+    # "check to prevent multiple setUp() runs" in sos_tests.py would fail
ecf6d6
+    _tmpdir = '/tmp/rhbz1965001_avocado_test'
ecf6d6
+
ecf6d6
+    def test_no_permission_denied(self):
ecf6d6
+        self.assertSosLogNotContains("Permission denied")
ecf6d6
-- 
ecf6d6
2.26.3
ecf6d6