Skip to contentSkip to conversation

sepolicy: do not fail when file_contexts.local or .subs do not exist

PATCH Superseded Nicolas Iooss 2 participants 4 messages Sep 18, 2017 – Sep 19, 2017 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sepolicy: do not fail when file_contexts.local or .subs do not exist

On a system without any file context customizations, "sepolicy gui" fails to load because it tries to read a non-existent file:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.local'

Once this issue is fixed, another one is triggered:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.subs

Use os.path.exists() to prevent trying to open non-existent files.

---
 python/sepolicy/sepolicy/__init__.py | 4 ++++
 1 file changed, 4 insertions(+)
python/sepolicy/sepolicy/__init__.py +4 -0
index 03742346caf0..14d2ad634d7d 100644
@@ -523,6 +523,8 @@ def find_entrypoint_path(exe, exclude_list=[]):
 
 
 def read_file_equiv(edict, fc_path, modify):
+    if not os.path.exists(fc_path):
+        return edict
     fd = open(fc_path, "r")
     fc = fd.readlines()
     fd.close()
@@ -555,6 +557,8 @@ def get_local_file_paths(fc_path=selinux.selinux_file_context_path()):
     if local_files:
         return local_files
     local_files = []
+    if not os.path.exists(fc_path + ".local"):
+        return []
     fd = open(fc_path + ".local", "r")
     fc = fd.readlines()
     fd.close()
Signature · 1 line
2.14.1
#1 lore raw
Quoted 33 lines · Nicolas Iooss

On a system without any file context customizations, "sepolicy gui" fails to load because it tries to read a non-existent file:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.local'

Once this issue is fixed, another one is triggered:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.subs

Use os.path.exists() to prevent trying to open non-existent files.

---
 python/sepolicy/sepolicy/__init__.py | 4 ++++
 1 file changed, 4 insertions(+)
python/sepolicy/sepolicy/__init__.py +2 -0
index 03742346caf0..14d2ad634d7d 100644
@@ -523,6 +523,8 @@ def find_entrypoint_path(exe, exclude_list=[]):
 
 
 def read_file_equiv(edict, fc_path, modify):
+    if not os.path.exists(fc_path):
+        return edict
     fd = open(fc_path, "r")
     fc = fd.readlines()
     fd.close()

python/sepolicy/sepolicy/__init__.py def get_local_file_paths(fc_path=selinux.selinux_file_context_path()): · line 562

Quoted 6 lines
python/sepolicy/sepolicy/__init__.py +2 -0
@@ -555,6 +557,8 @@ def get_local_file_paths(fc_path=selinux.selinux_file_context_path()):
     if local_files:
         return local_files
     local_files = []
+    if not os.path.exists(fc_path + ".local"):
+        return []
     fd = open(fc_path + ".local", "r")

Why not use Try/Except here with a pass here? While you're at it, maybe update this to use a with statement. instead of an explicit close call.

Quoted 5 lines
python/sepolicy/sepolicy/__init__.py +0 -1
     fc = fd.readlines()
     fd.close()
--

2.14.1

#2 lore raw
Quoted 27 lines · William Roberts
Quoted 26 lines · Nicolas Iooss

On a system without any file context customizations, "sepolicy gui" fails to load because it tries to read a non-existent file:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.local'

Once this issue is fixed, another one is triggered:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.subs

Use os.path.exists() to prevent trying to open non-existent files.

---
 python/sepolicy/sepolicy/__init__.py | 4 ++++
 1 file changed, 4 insertions(+)
python/sepolicy/sepolicy/__init__.py +0 -0
index 03742346caf0..14d2ad634d7d 100644
@@ -523,6 +523,8 @@ def find_entrypoint_path(exe, exclude_list=[]):
 
 
Quoted 15 lines

python/sepolicy/sepolicy/__init__.py def get_local_file_paths(fc_path=selinux.selinux_file_context_path()): · line 562

Quoted 13 lines
python/sepolicy/sepolicy/__init__.py +4 -0
 def read_file_equiv(edict, fc_path, modify):
+    if not os.path.exists(fc_path):
+        return edict
     fd = open(fc_path, "r")
     fc = fd.readlines()
     fd.close()
@@ -555,6 +557,8 @@ def get_local_file_paths(fc_path=selinux.selinux_file_context_path()):
     if local_files:
         return local_files
     local_files = []
+    if not os.path.exists(fc_path + ".local"):
+        return []
     fd = open(fc_path + ".local", "r")

Why not use Try/Except here with a pass here?

Wouldn't be a pass... but you get the idea.

Quoted 7 lines

While you're at it, maybe update this to use a with statement. instead of an explicit close call.

Quoted 5 lines
python/sepolicy/sepolicy/__init__.py +0 -1
     fc = fd.readlines()
     fd.close()
--

2.14.1

#3 lore raw
Quoted 45 lines · William Roberts
Quoted 27 lines · William Roberts
Quoted 26 lines · Nicolas Iooss

On a system without any file context customizations, "sepolicy gui" fails to load because it tries to read a non-existent file:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.local'

Once this issue is fixed, another one is triggered:

FileNotFoundError: [Errno 2] No such file or directory:
'/etc/selinux/refpolicy-git/contexts/files/file_contexts.subs

Use os.path.exists() to prevent trying to open non-existent files.

---
 python/sepolicy/sepolicy/__init__.py | 4 ++++
 1 file changed, 4 insertions(+)
python/sepolicy/sepolicy/__init__.py +0 -0
index 03742346caf0..14d2ad634d7d 100644
@@ -523,6 +523,8 @@ def find_entrypoint_path(exe, exclude_list=[]):
 
 
Quoted 15 lines

python/sepolicy/sepolicy/__init__.py def get_local_file_paths(fc_path=selinux.selinux_file_context_path()): · line 562

Quoted 13 lines
python/sepolicy/sepolicy/__init__.py +4 -0
 def read_file_equiv(edict, fc_path, modify):
+    if not os.path.exists(fc_path):
+        return edict
     fd = open(fc_path, "r")
     fc = fd.readlines()
     fd.close()
@@ -555,6 +557,8 @@ def get_local_file_paths(fc_path=selinux.selinux_file_context_path()):
     if local_files:
         return local_files
     local_files = []
+    if not os.path.exists(fc_path + ".local"):
+        return []
     fd = open(fc_path + ".local", "r")

Why not use Try/Except here with a pass here?

Quoted 1 line

Wouldn't be a pass... but you get the idea.

It modifies more lines, but as you suggested it I will send a v2 which uses try/except. In order to keep the code compatible with Python 2, it will be "except OSError" + errno checking to silently skip non-existing file.

Quoted 5 lines
Quoted 5 lines

While you're at it, maybe update this to use a with statement. instead of an explicit close call.

Quoted 2 lines
python/sepolicy/sepolicy/__init__.py +0 -0
     fc = fd.readlines()
     fd.close()

I will do it. Thanks for you suggestions.

Nicolas