On Fri, 22 Sep 2017 12:26:42 +0200 Laurent Bigonville <bi...@debian.org> wrote:
On Sun, 03 Sep 2017 13:26:57 +0200 intrigeri <intrig...@debian.org> wrote:

> > As I am un-knowledgeable on this matter, can you list all the LSMs and
> > the way to identify any of them is running?
>
> A trivial way to discover AppArmor was proposed, and a bunch of
> options for SELinux were mentioned as well; no input from the Tomoyo
> maintainers AFAICT so let's skip that one ⇒ dropping the moreinfo tag.
>
> Next step is to actually implement this proposal in reportbug :)
>
> Sandro: at first glance this support could be added to
> /usr/lib/python3/dist-packages/reportbug/bugreport.py, with actual
> detection functions in utils.py, just like it's done for the init
> system. Would this approach suit you?

Regarding the way of detecting SELinux, like I said in my previous
mails, I see 4 ways:

1. Use existing SELinux tools like sestatus, sestatus is installed in
    policycoreutils package which has 95% of chances to be installed if
    SELinux is enabled on the machine. If reportbug doesn't need to
    parse the output, this is probably the easiest and the lower
    maintenance level, but it's quite verbose if we include that in all
    bug reports.
2. Use existing lower-level SELinux tools like selinuxenabled and
    getenforce, these tools will more than probably be installed in the
    case SELinux is enabled. Not sure if we can get the policy name in
    that way though.
3. If you don't want to shell out, you could use the python selinux
    module to retrieve and display the informations (see my little
    example attached) there is however no guarantee that the
    python-selinux module is installed if selinux is enabled though.
    That means that reportbug will have to Depends/Recommends it. IMHO
    this is the most flexible way.
4. Directly query the selinuxfs and selinux configuration
    (/sys/fs/selinux/...), this is maybe too low level.

I would probably for 3 if depending on the module is OK and we just a
one line telling: "LMS: SELinux: enabled - enforcing/permissive - Policy
name: foo"
Here a patch that implements the SELinux part

I'm not too sure how to do that for apparmor (or the other LSM)
>From c101e36e6cf952ac79c6298deb4b189623643c94 Mon Sep 17 00:00:00 2001
From: Laurent Bigonville <bi...@debian.org>
Date: Sat, 7 Oct 2017 16:59:01 +0200
Subject: [PATCH] Add SELinux status in the bug reports

This is the first step to add LSM information in the bug reports
---
 debian/control         |  1 +
 reportbug/bugreport.py |  3 +++
 reportbug/utils.py     | 25 +++++++++++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/debian/control b/debian/control
index 9afcc7d..800588a 100644
--- a/debian/control
+++ b/debian/control
@@ -37,6 +37,7 @@ Package: python3-reportbug
 Section: python
 Architecture: all
 Depends: ${misc:Depends}, ${python3:Depends}, apt, python3-debian, python3-debianbts (>= 1.13), file, python3-requests
+Recommends: python3-selinux
 Suggests: reportbug
 Description: Python modules for interacting with bug tracking systems
  reportbug is a tool designed to make the reporting of bugs in Debian
diff --git a/reportbug/bugreport.py b/reportbug/bugreport.py
index ea835fa..e178a1a 100644
--- a/reportbug/bugreport.py
+++ b/reportbug/bugreport.py
@@ -82,6 +82,7 @@ class bugreport(object):
         debinfo = ''
         shellpath = utils.realpath('/bin/sh')
         init = utils.get_init_system()
+        lsminfo = utils.get_lsm_info()
 
         locinfo = []
         langsetting = os.environ.get('LANG', 'C')
@@ -177,6 +178,8 @@ class bugreport(object):
             debinfo += 'Shell: /bin/sh linked to %s\n' % shellpath
         if init:
             debinfo += 'Init: %s\n' % init
+        if lsminfo:
+            debinfo += 'LSM: %s\n' % lsminfo
 
         # Don't include system info for certain packages
         if self.sysinfo:
diff --git a/reportbug/utils.py b/reportbug/utils.py
index 8139668..f09651a 100644
--- a/reportbug/utils.py
+++ b/reportbug/utils.py
@@ -43,6 +43,12 @@ import pipes
 from .urlutils import open_url
 from string import ascii_letters, digits
 
+try:
+    import selinux
+    selinux_module = True
+except ImportError:
+    selinux_module = False
+
 # Paths for dpkg
 DPKGLIB = '/var/lib/dpkg'
 AVAILDB = os.path.join(DPKGLIB, 'available')
@@ -1304,3 +1310,22 @@ def get_init_system():
         init = 'sysvinit (via /sbin/init)'
 
     return init
+
+def get_lsm_info():
+    """Determines the linux security module enabled on the current machine"""
+
+    lsminfo = 'SELinux: unknown'
+    if selinux_module:
+        is_selinux_enabled = selinux.is_selinux_enabled()
+        if (is_selinux_enabled == 0):
+            lsminfo = 'SELinux: disabled'
+        elif (is_selinux_enabled == 1):
+            lsminfo = 'SELinux: enabled - '
+            is_selinux_enforce = selinux.security_getenforce()
+            if (is_selinux_enforce == 0):
+                lsminfo += 'Mode: permissive - '
+            elif (is_selinux_enforce == 1):
+                lsminfo += 'Mode: enforcing - '
+            else:
+                lsminfo += 'Mode: unknown - '
+            lsminfo += 'Policy name: %s' % os.path.basename(selinux.selinux_policy_root())
-- 
2.14.2

Reply via email to