Blame SOURCES/sos-bz1965002-skip-selinux-of-proc-everytime.patch

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