Skip to contentSkip to conversation

python: Hide error message when modifying non-local fcontext

PATCH In review Vit Mojzis 2 participants 3 messages Sep 14, 2022 – Oct 20, 2022 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 python: Hide error message when modifying non-local fcontext

Fixes:

  # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd
  libsemanage.dbase_llist_query: could not query record value (No such file or directory).
---
 python/semanage/seobject.py | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
python/semanage/seobject.py +18 -1
index 0782c082..2d52f53c 100644
@@ -177,6 +177,22 @@ except (OSError, ImportError):
             for l in self.log_list:
                 syslog.syslog(syslog.LOG_INFO, message + l)
 
+# Define a context manager to suppress stderr.
+class suppress_stderr(object):
+    def __init__(self):
+        # Open a /dev/null file to be used as stderr
+        self.null =  os.open(os.devnull,os.O_RDWR)
+        self.save_fd = os.dup(2)
+
+    def __enter__(self):
+        # Set stderr to the null file
+        os.dup2(self.null,2)
+
+    def __exit__(self, *_):
+        # Restore stderr
+        os.dup2(self.save_fd,2)
+        os.close(self.null)
+
 
 class nulllogger:
 
@@ -2510,7 +2526,8 @@ class fcontextRecords(semanageRecords):
                 raise ValueError(_("File context for %s is not defined") % target)
 
         try:
-            (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
+            with suppress_stderr():
+                (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
         except OSError:
             try:
                 (rc, fcontext) = semanage_fcontext_query(self.sh, k)
Signature · 1 line
2.35.3
#1 lore raw
Quoted 48 lines · Vit Mojzis

Fixes:

  # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd
  libsemanage.dbase_llist_query: could not query record value (No such file or directory).
---
 python/semanage/seobject.py | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
python/semanage/seobject.py +18 -2
index 0782c082..2d52f53c 100644
@@ -177,6 +177,22 @@ except (OSError, ImportError):
             for l in self.log_list:
                 syslog.syslog(syslog.LOG_INFO, message + l)
 
+# Define a context manager to suppress stderr.
+class suppress_stderr(object):
+    def __init__(self):
+        # Open a /dev/null file to be used as stderr
+        self.null =  os.open(os.devnull,os.O_RDWR)
+        self.save_fd = os.dup(2)
+
+    def __enter__(self):
+        # Set stderr to the null file
+        os.dup2(self.null,2)
+
+    def __exit__(self, *_):
+        # Restore stderr
+        os.dup2(self.save_fd,2)
+        os.close(self.null)
+
 
 class nulllogger:
 
@@ -2510,7 +2526,8 @@ class fcontextRecords(semanageRecords):
                 raise ValueError(_("File context for %s is not defined") % target)
 
         try:
-            (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
+            with suppress_stderr():
+                (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
         except OSError:
             try:
                 (rc, fcontext) = semanage_fcontext_query(self.sh, k)
--
Quoted 2 lines

2.35.3

I think it is better to rework semanage/seobject.py so that there is no error. I sent a patch to the list that should fix this. Thanks for the report. Sorry that it took so long to get to. Jim

author
#2 lore raw
Quoted 41 lines · James Carter
Quoted 40 lines · Vit Mojzis

Fixes:

   # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd
   libsemanage.dbase_llist_query: could not query record value (No such file or directory).
---
  python/semanage/seobject.py | 19 ++++++++++++++++++-
  1 file changed, 18 insertions(+), 1 deletion(-)
python/semanage/seobject.py +16 -0
index 0782c082..2d52f53c 100644
@@ -177,6 +177,22 @@ except (OSError, ImportError):
              for l in self.log_list:
                  syslog.syslog(syslog.LOG_INFO, message + l)
 
+# Define a context manager to suppress stderr.
+class suppress_stderr(object):
+    def __init__(self):
+        # Open a /dev/null file to be used as stderr
+        self.null =  os.open(os.devnull,os.O_RDWR)
+        self.save_fd = os.dup(2)
+
+    def __enter__(self):
+        # Set stderr to the null file
+        os.dup2(self.null,2)
+
+    def __exit__(self, *_):
+        # Restore stderr
+        os.dup2(self.save_fd,2)
+        os.close(self.null)
+
 
  class nulllogger:
 
@@ -2510,7 +2526,8 @@ class fcontextRecords(semanageRecords):
                  raise ValueError(_("File context for %s is not defined") % target)
 
Quoted 15 lines
Quoted 10 lines
python/semanage/seobject.py +2 -2
          try:
-            (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
+            with suppress_stderr():
+                (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
          except OSError:
              try:
                  (rc, fcontext) = semanage_fcontext_query(self.sh, k)
--

2.35.3

I think it is better to rework semanage/seobject.py so that there is no error. I sent a patch to the list that should fix this. Thanks for the report. Sorry that it took so long to get to. Jim

Hi Jim,
thank you for looking into this. I agree that your solution is much more elegant and should be used instead.

Vit