Blame SOURCES/0050-python-Do-not-query-the-local-database-if-the-fconte.patch

017072
From f33e40265d192e5d725e7b82e5f14f603e1fba48 Mon Sep 17 00:00:00 2001
017072
From: James Carter <jwcart2@gmail.com>
017072
Date: Wed, 19 Oct 2022 14:20:11 -0400
017072
Subject: [PATCH] python: Do not query the local database if the fcontext is
017072
 non-local
017072
017072
Vit Mojzis reports that an error message is produced when modifying
017072
a non-local fcontext.
017072
017072
He gives the following example:
017072
  # semanage fcontext -f f -m -t passwd_file_t /etc/security/opasswd
017072
  libsemanage.dbase_llist_query: could not query record value (No such file or directory).
017072
017072
When modifying an fcontext, the non-local database is checked for the
017072
key and then, if it is not found there, the local database is checked.
017072
If the key doesn't exist, then an error is raised. If the key exists
017072
then the local database is queried first and, if that fails, the non-
017072
local database is queried.
017072
017072
The error is from querying the local database when the fcontext is in
017072
the non-local database.
017072
017072
Instead, if the fcontext is in the non-local database, just query
017072
the non-local database. Only query the local database if the
017072
fcontext was found in it.
017072
017072
Reported-by: Vit Mojzis <vmojzis@redhat.com>
017072
Signed-off-by: James Carter <jwcart2@gmail.com>
017072
---
017072
 python/semanage/seobject.py | 15 +++++++++------
017072
 1 file changed, 9 insertions(+), 6 deletions(-)
017072
017072
diff --git a/python/semanage/seobject.py b/python/semanage/seobject.py
017072
index 70ebfd08..0e923a0d 100644
017072
--- a/python/semanage/seobject.py
017072
+++ b/python/semanage/seobject.py
017072
@@ -2490,16 +2490,19 @@ class fcontextRecords(semanageRecords):
017072
         (rc, exists) = semanage_fcontext_exists(self.sh, k)
017072
         if rc < 0:
017072
             raise ValueError(_("Could not check if file context for %s is defined") % target)
017072
-        if not exists:
017072
+        if exists:
017072
+            try:
017072
+                (rc, fcontext) = semanage_fcontext_query(self.sh, k)
017072
+            except OSError:
017072
+                raise ValueError(_("Could not query file context for %s") % target)
017072
+        else:
017072
             (rc, exists) = semanage_fcontext_exists_local(self.sh, k)
017072
+            if rc < 0:
017072
+                raise ValueError(_("Could not check if file context for %s is defined") % target)
017072
             if not exists:
017072
                 raise ValueError(_("File context for %s is not defined") % target)
017072
-
017072
-        try:
017072
-            (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
017072
-        except OSError:
017072
             try:
017072
-                (rc, fcontext) = semanage_fcontext_query(self.sh, k)
017072
+                (rc, fcontext) = semanage_fcontext_query_local(self.sh, k)
017072
             except OSError:
017072
                 raise ValueError(_("Could not query file context for %s") % target)
017072
 
017072
-- 
017072
2.37.3
017072