Skip to contentSkip to conversation

chcat: don't crash if access to binary policy is prohibited

PATCH In review bauen1 4 participants 6 messages May 9, 2020 – Feb 22, 2021 SELinux security module

lore
Share & tools
lore

bauen1

author
#0 lore raw

PATCH 1/1 chcat: don't crash if access to binary policy is prohibited

sobject will crash if access to the binary policy is prohibited by selinux, e.g. refpolicy
this also breaks file operations that don't require seobject.

---
 python/chcat/chcat | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/chcat/chcat +4 -1
index fdd2e46e..55408577 100755
@@ -28,7 +28,6 @@ import os
 import pwd
 import getopt
 import selinux
-import seobject
 
 PROGNAME = "policycoreutils"
 try:
@@ -65,6 +64,7 @@ def verify_users(users):
 
 
 def chcat_user_add(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -144,6 +144,7 @@ def chcat_add(orig, newcat, objects, login_ind):
 
 
 def chcat_user_remove(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -233,6 +234,7 @@ def chcat_remove(orig, newcat, objects, login_ind):
 
 
 def chcat_user_replace(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -376,6 +378,7 @@ def listcats():
 
 
 def listusercats(users):
+    import seobject
     if len(users) == 0:
         try:
             users.append(os.getlogin())
Signature · 1 line
2.26.2
#1 lore raw
Quoted 6 lines · On Sat, May 9, 2020 at 4:06 PM bauen1 <[email protected]> wrote:

sobject will crash if access to the binary policy is prohibited by selinux, e.g. refpolicy
this also breaks file operations that don't require seobject.

Hello,
This patch looks very hackish. In fact, an underlying issue that exists with seobject is that "import seobject" raises an exception when it is used from an environment that is not allowed to read the policy:

Quoted 1 line
Quoted 1 line
Quoted 1 line

import seobject

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/site-packages/seobject.py", line 33, in <module>
import sepolicy
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
186, in <module>
raise e
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
182, in <module>
policy_file = get_installed_policy()
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
137, in get_installed_policy
raise ValueError(_("No SELinux Policy installed"))
ValueError: No SELinux Policy installed

Is this the issue you encountered when you write "seobject will crash"?

In my humble opinion, trying to hide such an issue by moving "import seobject" makes maintaining the project more difficult. I would prefer seeing a way to allow using "import seobject" without raising exceptions, but working on this is unfortunately quite time-consuming (I have not seen a straightforward way to deal with this, and there exist several ways to solve this in not-very-direct ways, for example with lazy loading of the policy when needed or with replacing some API with stub functions if the policy cannot be loaded).

Therefore I will not ack this patch, but I will not block ("Nack") it if another maintainer wants to include it.

Thanks,
Nicolas

Quoted 51 lines
---
 python/chcat/chcat | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/chcat/chcat +4 -2
index fdd2e46e..55408577 100755
@@ -28,7 +28,6 @@ import os
 import pwd
 import getopt
 import selinux
-import seobject
 
 PROGNAME = "policycoreutils"
 try:
@@ -65,6 +64,7 @@ def verify_users(users):
 
 
 def chcat_user_add(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -144,6 +144,7 @@ def chcat_add(orig, newcat, objects, login_ind):
 
 
 def chcat_user_remove(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -233,6 +234,7 @@ def chcat_remove(orig, newcat, objects, login_ind):
 
 
 def chcat_user_replace(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -376,6 +378,7 @@ def listcats():
 
 
 def listusercats(users):
+    import seobject
     if len(users) == 0:
         try:
             users.append(os.getlogin())
--

2.26.2

maintainer
#2 lore raw
Quoted 42 lines · Nicolas Iooss
Quoted 6 lines · On Sat, May 9, 2020 at 4:06 PM bauen1 <[email protected]> wrote:

sobject will crash if access to the binary policy is prohibited by selinux, e.g. refpolicy
this also breaks file operations that don't require seobject.

Hello,
This patch looks very hackish. In fact, an underlying issue that exists with seobject is that "import seobject" raises an exception when it is used from an environment that is not allowed to read the policy:

Quoted 1 line
Quoted 1 line
Quoted 1 line

import seobject

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/site-packages/seobject.py", line 33, in <module>
import sepolicy
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
186, in <module>
raise e
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
182, in <module>
policy_file = get_installed_policy()
File "/usr/lib/python3.8/site-packages/sepolicy/__init__.py", line
137, in get_installed_policy
raise ValueError(_("No SELinux Policy installed"))
ValueError: No SELinux Policy installed

Is this the issue you encountered when you write "seobject will crash"?

In my humble opinion, trying to hide such an issue by moving "import seobject" makes maintaining the project more difficult. I would prefer seeing a way to allow using "import seobject" without raising exceptions, but working on this is unfortunately quite time-consuming (I have not seen a straightforward way to deal with this, and there exist several ways to solve this in not-very-direct ways, for example with lazy loading of the policy when needed or with replacing some API with stub functions if the policy cannot be loaded).

Quoted 2 lines

Therefore I will not ack this patch, but I will not block ("Nack") it if another maintainer wants to include it.

I'm not opposed to the patch itself (I assume the current code breaks usage of chcat under MLS policy by regular users who lack access to the policy file), but your Signed-off-by line ought to be revised to contain your real name. Otherwise, it doesn't really serve its purpose. See the discussion of Signed-off-by in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html.

author
#3 lore raw

PATCH v2 1/1 chcat: allow usage if binary policy is inaccessible

Currently, chcat will crash when run as regular user, because import sepolicy throws an Exception when failing to access the binary policy under /etc/selinux/${POLICYNAME}/policy/ which is inaccessible to regular users.

---

v2:
Fix signed-off-by, improve commit message, but otherwise unchanged

 python/chcat/chcat | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/chcat/chcat +4 -1
index fdd2e46e..55408577 100755
@@ -28,7 +28,6 @@ import os
 import pwd
 import getopt
 import selinux
-import seobject
 
 PROGNAME = "policycoreutils"
 try:
@@ -65,6 +64,7 @@ def verify_users(users):
 
 
 def chcat_user_add(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -144,6 +144,7 @@ def chcat_add(orig, newcat, objects, login_ind):
 
 
 def chcat_user_remove(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -233,6 +234,7 @@ def chcat_remove(orig, newcat, objects, login_ind):
 
 
 def chcat_user_replace(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -376,6 +378,7 @@ def listcats():
 
 
 def listusercats(users):
+    import seobject
     if len(users) == 0:
         try:
             users.append(os.getlogin())

--
2.30.1

#4 lore raw

Re: [PATCH v2] chcat: allow usage if binary policy is inaccessible

Quoted 5 lines · bauen1 <[email protected]> writes:

Currently, chcat will crash when run as regular user, because import sepolicy throws an Exception when failing to access the binary policy under /etc/selinux/${POLICYNAME}/policy/ which is inaccessible to regular users.

I'd rather follow Nicolas suggestion so I've prepared a patch, see below, which moves the policy initialization in sepolicy module before it's used for the first time. It seems to solve the same problem in more generic way. I need to run some tests on that and then they pass I'll propose it here on the mailing list.

https://github.com/bachradsusi/SELinuxProject-selinux/commit/6a12939b613b273a6e96e1cc4cc096cdf7db5ac6

python/sepolicy/sepolicy/__init__.py +6 -5
@@ -178,15 +178,14 @@ def load_store_policy(store):
         return None
     policy(policy_file)
 
-try:
+def init_policy():
     policy_file = get_installed_policy()
     policy(policy_file)
-except ValueError as e:
-    if selinux.is_selinux_enabled() == 1:
-        raise e
-
 
 def info(setype, name=None):
+    if not _pol:
+        init_policy()
+
     if setype == TYPE:
         q = setools.TypeQuery(_pol)
         q.name = name
@@ -337,6 +336,8 @@ def _setools_rule_to_dict(rule):
 
 
 def search(types, seinfo=None):
+    if not _pol:
+        init_policy()
     if not seinfo:
         seinfo = {}
     valid_types = set([ALLOW, AUDITALLOW, NEVERALLOW, DONTAUDIT, TRANSITION, ROLE_ALLOW])
Quoted 55 lines

---

v2:
Fix signed-off-by, improve commit message, but otherwise unchanged

 python/chcat/chcat | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
python/chcat/chcat +4 -2
index fdd2e46e..55408577 100755
@@ -28,7 +28,6 @@ import os
 import pwd
 import getopt
 import selinux
-import seobject
 
 PROGNAME = "policycoreutils"
 try:
@@ -65,6 +64,7 @@ def verify_users(users):
 
 
 def chcat_user_add(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -144,6 +144,7 @@ def chcat_add(orig, newcat, objects, login_ind):
 
 
 def chcat_user_remove(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -233,6 +234,7 @@ def chcat_remove(orig, newcat, objects, login_ind):
 
 
 def chcat_user_replace(newcat, users):
+    import seobject
     errors = 0
     logins = seobject.loginRecords()
     seusers = logins.get_all()
@@ -376,6 +378,7 @@ def listcats():
 
 
 def listusercats(users):
+    import seobject
     if len(users) == 0:
         try:
             users.append(os.getlogin())
--

2.30.1

author
#5 lore raw

Re: [PATCH v2] chcat: allow usage if binary policy is inaccessible

Quoted 8 lines · Petr Lautrbach
Quoted 5 lines · bauen1 <[email protected]> writes:

Currently, chcat will crash when run as regular user, because import sepolicy throws an Exception when failing to access the binary policy under /etc/selinux/${POLICYNAME}/policy/ which is inaccessible to regular users.

Quoted 6 lines

I'd rather follow Nicolas suggestion so I've prepared a patch, see below, which moves the policy initialization in sepolicy module before it's used for the first time. It seems to solve the same problem in more generic way. I need to run some tests on that and then they pass I'll propose it here on the mailing list.

Yes, this is a much better approach.

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/