seobject: Handle python error returns correctly
PATCH 1/1 seobject: Handle python error returns correctly
After 9406ace8 ("libsemanage: throw exceptions in python rather than return NULL"), calls to libsemanage functions return Python exceptions instead of returning negative error return codes. For systems that did not have the applicable headers installed prior to build, the difference was not seen. Following commit 9792099f ("Properly build the swig exception file even if the headers are missing"), that issue has been resolved and the underlying semanage_fcontext_query_local and semanage_fcontext_query calls now result in an OSError return. This results in the following error when attempting to modify a fcontext defined in the systems base policy.
libsemanage.dbase_llist_query: could not query record value (No such file or directory).
OSError: No such file or directory
To resolve the error, handle the OSError exception, but retain the previous query operation.
--- python/semanage/seobject.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
python/semanage/seobject.py +6 -4
@@ -1953,10 +1953,12 @@ class fcontextRecords(semanageRecords): if not exists: raise ValueError(_("File context for %s is not defined") % target) - (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) - if rc < 0: - (rc, fcontext) = semanage_fcontext_query(self.sh, k) - if rc < 0: + try: + (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) + except OSError: + try: + (rc, fcontext) = semanage_fcontext_query(self.sh, k) + except OSError: raise ValueError(_("Could not query file context for %s") % target) if setype != "<<none>>":
Signature · 1 line
2.5.5
Quoted 10 lines · Kyle Walker
After
9406ace8("libsemanage: throw exceptions in python rather than return NULL"), calls to libsemanage functions return Python exceptions instead of returning negative error return codes. For systems that did not have the applicable headers installed prior to build, the difference was not seen. Following commit9792099f("Properly build the swig exception file even if the headers are missing"), that issue has been resolved and the underlyingsemanage_fcontext_query_localandsemanage_fcontext_querycalls now result in an OSError return. This results in the following error when attempting to modify a fcontext defined in the systems base policy.
Quoted 7 lines
libsemanage.dbase_llist_query: could not query record value (No such file or directory).
OSError: No such file or directoryTo resolve the error, handle the OSError exception, but retain the previous query operation.
Hmm...even after this patch, we still get the error message from dbase_llist_query. Wondering if we should drop that or if that would hide anything useful for other errors. Doesn't seem overly useful at present.
Quoted 28 lines
Signed-off-by Kyle Walker <[email protected]>--- python/semanage/seobject.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)python/semanage/seobject.py +6
-4@@ -1953,10 +1953,12 @@ class fcontextRecords(semanageRecords): if not exists: raise ValueError(_("File context for %s is not defined") % target) - (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) - if rc < 0: - (rc, fcontext) = semanage_fcontext_query(self.sh, k) - if rc < 0: + try: + (rc, fcontext) = semanage_fcontext_query_local(self.sh, k) + except OSError: + try: + (rc, fcontext) = semanage_fcontext_query(self.sh, k) + except OSError: raise ValueError(_("Could not query file context for %s") % target) if setype != "<<none>>":
Quoted 4 lines · Stephen Smalley
Hmm...even after this patch, we still get the error message from
dbase_llist_query. Wondering if we should drop that or if that would hide anything useful for other errors. Doesn't seem overly useful at present.
It does still emit that warning, it stems from the dbase_llist_query_local not returning any entries and falling back to the dbase_llist_query. It is chatty, and not necessarily helpful. I can't really think of a use-case for it myself. Outside of being able to verify that a fcontext is not in the file_contexts.local, but in the underlying file_contexts database on modification.
I.E. If you see that message, you are modifying from the defaults. If you don't, you are modifying a fcontext that has already been modified on the system previously.
Quoted 13 lines · Kyle Walker
Quoted 4 lines · Stephen Smalley
Hmm...even after this patch, we still get the error message from
dbase_llist_query. Wondering if we should drop that or if that would hide anything useful for other errors. Doesn't seem overly useful at present.It does still emit that warning, it stems from the
dbase_llist_query_localnot returning any entries and falling back to thedbase_llist_query. It is chatty, and not necessarily helpful. I can't really think of a use-case for it myself. Outside of being able to verify that a fcontext is not in the file_contexts.local, but in the underlyingfile_contextsdatabase on modification.
Quoted 3 lines
I.E. If you see that message, you are modifying from the defaults. If you don't, you are modifying a fcontext that has already been modified on the system previously.
Yes, so I applied your patch, but I think it would make sense to drop that message from libsemanage too. Otherwise, users are likely to be confused by the error message and think the change did not take effect.
Quoted 3 lines · Stephen Smalley
Yes, so I applied your patch, but I think it would make sense to drop that message from libsemanage too. Otherwise, users are likely to be confused by the error message and think the change did not take effect.
Makes sense to me! I'll go ahead and put pulling that error it on my TODO list for today or tomorrow.
Thanks!