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

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