Skip to content

Commit

Permalink
Replace libselinux restorecon API with CLI
Browse files Browse the repository at this point in the history
The restorecon API in libselinux has a problem fixing the context if it
is update from the same script.

The bug is present in several RHEL releases and it is documented here:

https://issues.redhat.com/browse/RHEL-73348

Following the advice in the issue, the API call has been temporarily replaced
with the equivalent CLI.

Fix: 2338454
  • Loading branch information
fmarco76 committed Jan 20, 2025
1 parent 88b030d commit 8a89fe5
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from __future__ import absolute_import
import logging
import selinux
import subprocess
import sys
import time

Expand Down Expand Up @@ -48,10 +49,29 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
suffix = "(/.*)?"

def restore_context(self, mdict):
selinux.restorecon(mdict['pki_instance_path'], True)
selinux.restorecon(config.PKI_DEPLOYMENT_LOG_ROOT, True)
selinux.restorecon(mdict['pki_instance_log_path'], True)
selinux.restorecon(mdict['pki_instance_configuration_path'], True)
# The restocon API is not working in RHEL
# (see https://issues.redhat.com/browse/RHEL-73348).
#
#selinux.restorecon(mdict['pki_instance_path'], True)
#selinux.restorecon(config.PKI_DEPLOYMENT_LOG_ROOT, True)
#selinux.restorecon(mdict['pki_instance_log_path'], True)
#selinux.restorecon(mdict['pki_instance_configuration_path'], True)
folders = [
mdict['pki_instance_path'],
config.PKI_DEPLOYMENT_LOG_ROOT,
mdict['pki_instance_log_path'],
mdict['pki_instance_configuration_path']
]
for folder in folders:
cmd = [
'/usr/sbin/restorecon',
'-R'
]
if logger.isEnabledFor(logging.DEBUG):
cmd.append('-v')
cmd.append(folder)
logger.debug('Command: %s', ' '.join(cmd))
subprocess.run(cmd, check=True)

# Helper function to check if a given `context_value` exists in the given
# set of `records`. This method can process both port contexts and file contexts
Expand Down

0 comments on commit 8a89fe5

Please sign in to comment.