neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone

Blame SOURCES/0020-apply-directory-s-SELinux-context-to-freshly-created.patch

4cd9fd
From dabc5f8a29f34aebd9ea61405d822b5d5bef1ec2 Mon Sep 17 00:00:00 2001
4cd9fd
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
4cd9fd
Date: Tue, 2 Mar 2021 14:13:29 +0100
4cd9fd
Subject: [PATCH] apply directory's SELinux context to freshly created mount
4cd9fd
 points
4cd9fd
MIME-Version: 1.0
4cd9fd
Content-Type: text/plain; charset=UTF-8
4cd9fd
Content-Transfer-Encoding: 8bit
4cd9fd
4cd9fd
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
4cd9fd
4cd9fd
Resolves: rhbz#1934076
4cd9fd
---
4cd9fd
 blivet/formats/fs.py | 3 ++-
4cd9fd
 blivet/util.py       | 9 +++++----
4cd9fd
 2 files changed, 7 insertions(+), 5 deletions(-)
4cd9fd
4cd9fd
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
4cd9fd
index e61e5b86..a92d3485 100644
4cd9fd
--- a/blivet/formats/fs.py
4cd9fd
+++ b/blivet/formats/fs.py
4cd9fd
@@ -27,6 +27,7 @@ import os
4cd9fd
 import tempfile
4cd9fd
 import uuid as uuid_mod
4cd9fd
 import random
4cd9fd
+import stat
4cd9fd
 
4cd9fd
 from parted import fileSystemType, PARTITION_BOOT
4cd9fd
 
4cd9fd
@@ -582,7 +583,7 @@ class FS(DeviceFormat):
4cd9fd
         mountpoint = kwargs.get("mountpoint") or self.mountpoint
4cd9fd
 
4cd9fd
         if self._selinux_supported and flags.selinux and "ro" not in self._mount.mount_options(options).split(",") and flags.selinux_reset_fcon:
4cd9fd
-            ret = util.reset_file_context(mountpoint, chroot)
4cd9fd
+            ret = util.reset_file_context(mountpoint, chroot, stat.S_IFDIR)
4cd9fd
             if not ret:
4cd9fd
                 log.warning("Failed to reset SElinux context for newly mounted filesystem root directory to default.")
4cd9fd
 
4cd9fd
diff --git a/blivet/util.py b/blivet/util.py
4cd9fd
index 48b7818f..f5e0cc1a 100644
4cd9fd
--- a/blivet/util.py
4cd9fd
+++ b/blivet/util.py
4cd9fd
@@ -448,11 +448,11 @@ def get_cow_sysfs_path(dev_path, dev_sysfsPath):
4cd9fd
 ##
4cd9fd
 
4cd9fd
 
4cd9fd
-def match_path_context(path):
4cd9fd
+def match_path_context(path, mode=0):
4cd9fd
     """ Return the default SELinux context for the given path. """
4cd9fd
     context = None
4cd9fd
     try:
4cd9fd
-        context = selinux.matchpathcon(os.path.normpath(path), 0)[1]
4cd9fd
+        context = selinux.matchpathcon(os.path.normpath(path), mode)[1]
4cd9fd
     except OSError as e:
4cd9fd
         log.info("failed to get default SELinux context for %s: %s", path, e)
4cd9fd
 
4cd9fd
@@ -491,7 +491,7 @@ def set_file_context(path, context, root=None):
4cd9fd
     return rc
4cd9fd
 
4cd9fd
 
4cd9fd
-def reset_file_context(path, root=None):
4cd9fd
+def reset_file_context(path, root=None, mode=0):
4cd9fd
     """ Restore the SELinux context of a file to its default value.
4cd9fd
 
4cd9fd
         Arguments:
4cd9fd
@@ -501,12 +501,13 @@ def reset_file_context(path, root=None):
4cd9fd
         Keyword Arguments:
4cd9fd
 
4cd9fd
             root        an optional chroot string
4cd9fd
+            mode        an optional mode to use
4cd9fd
 
4cd9fd
         Return Value:
4cd9fd
 
4cd9fd
             If successful, returns the file's new/default context.
4cd9fd
     """
4cd9fd
-    context = match_path_context(path)
4cd9fd
+    context = match_path_context(path, mode)
4cd9fd
     if context:
4cd9fd
         if set_file_context(path, context, root=root):
4cd9fd
             return context
4cd9fd
-- 
4cd9fd
2.29.2
4cd9fd