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

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