Blame SOURCES/scap-security-guide-0.1.52-selinux_all_devicefiles_labeled_fix-PR_5911.patch

475544
From 60f82f8d33cef82f3ff5e90073803c199bad02fb Mon Sep 17 00:00:00 2001
475544
From: Vojtech Polasek <vpolasek@redhat.com>
475544
Date: Tue, 7 Jul 2020 11:31:59 +0200
475544
Subject: [PATCH 1/3] modify rule description and ocil
475544
475544
---
475544
 .../selinux_all_devicefiles_labeled/rule.yml  | 19 +++++++++++--------
475544
 1 file changed, 11 insertions(+), 8 deletions(-)
475544
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/rule.yml b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/rule.yml
475544
index 765fca583e..1667557740 100644
475544
--- a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/rule.yml
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/rule.yml
475544
@@ -6,18 +6,20 @@ title: 'Ensure No Device Files are Unlabeled by SELinux'
475544
 
475544
 description: |-
475544
     Device files, which are used for communication with important system
475544
-    resources, should be labeled with proper SELinux types. If any device
475544
-    files do not carry the SELinux type <tt>device_t</tt>, report the bug so
475544
-    that policy can be corrected. Supply information about what the device is
475544
-    and what programs use it.
475544
+    resources, should be labeled with proper SELinux types. If any device files
475544
+    carry the SELinux type <tt>device_t</tt> or <tt>unlabeled_t</tt>, report the
475544
+    bug so that policy can be corrected. Supply information about what the
475544
+    device is and what programs use it.
475544
     

475544
-    To check for unlabeled device files, run the following command:
475544
+    To check for incorrectly labeled device files, run following commands:
475544
     
$ sudo find /dev -context *:device_t:* \( -type c -o -type b \) -printf "%p %Z\n"
475544
+    
$ sudo find /dev -context *:unlabeled_t:* \( -type c -o -type b \) -printf "%p %Z\n"
475544
     It should produce no output in a well-configured system.
475544
 
475544
 rationale: |-
475544
-    If a device file carries the SELinux type <tt>device_t</tt>, then SELinux
475544
-    cannot properly restrict access to the device file.
475544
+    If a device file carries the SELinux type <tt>device_t</tt> or
475544
+    <tt>unlabeled_t</tt>, then SELinux cannot properly restrict access to the
475544
+    device file.
475544
 
475544
 severity: medium
475544
 
475544
@@ -45,8 +47,9 @@ references:
475544
 ocil_clause: 'there is output'
475544
 
475544
 ocil: |-
475544
-    To check for unlabeled device files, run the following command:
475544
+    To check for incorrectly labeled device files, run following commands:
475544
     
$ sudo find /dev -context *:device_t:* \( -type c -o -type b \) -printf "%p %Z\n"
475544
+    
$ sudo find /dev -context *:unlabeled_t:* \( -type c -o -type b \) -printf "%p %Z\n"
475544
     It should produce no output in a well-configured system.
475544
 
475544
 warnings:
475544
475544
From e0cb2d04a9d95967e4adb3e05cc93a4a834a90b5 Mon Sep 17 00:00:00 2001
475544
From: Vojtech Polasek <vpolasek@redhat.com>
475544
Date: Tue, 7 Jul 2020 11:32:57 +0200
475544
Subject: [PATCH 2/3] updated oval to check only device files
475544
475544
---
475544
 .../oval/shared.xml                           | 64 +++++++++++++------
475544
 1 file changed, 43 insertions(+), 21 deletions(-)
475544
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/oval/shared.xml b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/oval/shared.xml
475544
index 51b68008af..7dcfb98577 100644
475544
--- a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/oval/shared.xml
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/oval/shared.xml
475544
@@ -2,32 +2,54 @@
475544
   <definition class="compliance" id="selinux_all_devicefiles_labeled" version="1">
475544
     <metadata>
475544
       <title>Device Files Have Proper SELinux Context</title>
475544
-      <affected family="unix">
475544
-        <platform>Red Hat Enterprise Linux 6</platform>
475544
-        <platform>Red Hat Enterprise Linux 7</platform>
475544
-        <platform>Red Hat Enterprise Linux 8</platform>
475544
-        <platform>Red Hat Virtualization 4</platform>
475544
-        <platform>multi_platform_fedora</platform>
475544
-        <platform>multi_platform_ol</platform>
475544
-        <platform>multi_platform_wrlinux</platform>
475544
-      </affected>
475544
-      <description>All device files in /dev should be assigned an SELinux security context other than 'device_t'.</description>
475544
+      {{{- oval_affected(products) }}}
475544
+      <description>All device files in /dev should be assigned an SELinux security context other than 'device_t' and 'unlabeled_t'.</description>
475544
     </metadata>
475544
-    <criteria>
475544
-      <criterion comment="device_t in /dev" test_ref="test_selinux_all_devicefiles_labeled" />
475544
+    <criteria operator="AND">
475544
+      <criterion comment="device_t in /dev" test_ref="test_selinux_dev_device_t" />
475544
+      <criterion comment="unlabeled_t in /dev" test_ref="test_selinux_dev_unlabeled_t" />
475544
     </criteria>
475544
   </definition>
475544
-  <linux:selinuxsecuritycontext_test check="none satisfy" check_existence="any_exist" comment="device_t in /dev" id="test_selinux_all_devicefiles_labeled" version="2">
475544
-    <linux:object object_ref="object_selinux_all_devicefiles_labeled" />
475544
-    <linux:state state_ref="state_selinux_all_devicefiles_labeled" />
475544
+
475544
+  
475544
+  <unix:file_object id="object_dev_device_files" comment="device files within /dev directory" version="1">
475544
+    <unix:behaviors recurse_direction="down" />
475544
+    <unix:path operation="equals">/dev</unix:path>
475544
+    <unix:filename operation="pattern match">^.*$</unix:filename>
475544
+    <filter action="include">state_block_or_char_device_file</filter>
475544
+  </unix:file_object>
475544
+
475544
+  <unix:file_state id="state_block_or_char_device_file" version="1" comment="device files" >
475544
+    <unix:type operation="pattern match">^(block|character) special$</unix:type>
475544
+  </unix:file_state>
475544
+
475544
+  <local_variable id="variable_dev_device_files" comment="all device files within /dev directory" datatype="string" version="1">
475544
+    <object_component object_ref="object_dev_device_files" item_field="filepath" />
475544
+  </local_variable>
475544
+
475544
+
475544
+  <linux:selinuxsecuritycontext_test check="none satisfy" check_existence="any_exist" comment="device_t in /dev" id="test_selinux_dev_device_t" version="2">
475544
+    <linux:object object_ref="object_selinux_dev_device_t" />
475544
+    <linux:state state_ref="state_selinux_dev_device_t" />
475544
   </linux:selinuxsecuritycontext_test>
475544
-  <linux:selinuxsecuritycontext_object comment="device_t in /dev" id="object_selinux_all_devicefiles_labeled" version="1">
475544
-    <linux:behaviors recurse_direction="down" />
475544
-    <linux:path>/dev</linux:path>
475544
-    <linux:filename operation="pattern match">^.*$</linux:filename>
475544
-    <filter action="include">state_selinux_all_devicefiles_labeled</filter>
475544
+  <linux:selinuxsecuritycontext_object comment="device_t in /dev" id="object_selinux_dev_device_t" version="1">
475544
+    <linux:filepath operation="equals" var_ref="variable_dev_device_files"  var_check="at least one"/>
475544
+    <filter action="include">state_selinux_dev_device_t</filter>
475544
   </linux:selinuxsecuritycontext_object>
475544
-  <linux:selinuxsecuritycontext_state comment="do it" id="state_selinux_all_devicefiles_labeled" version="1">
475544
+  <linux:selinuxsecuritycontext_state comment="device_t label" id="state_selinux_dev_device_t" version="1">
475544
     <linux:type datatype="string" operation="equals">device_t</linux:type>
475544
   </linux:selinuxsecuritycontext_state>
475544
+
475544
+  <linux:selinuxsecuritycontext_test check="none satisfy" check_existence="any_exist" comment="unlabeled_t in /dev" id="test_selinux_dev_unlabeled_t" version="2">
475544
+    <linux:object object_ref="object_selinux_dev_unlabeled_t" />
475544
+    <linux:state state_ref="state_selinux_dev_unlabeled_t" />
475544
+  </linux:selinuxsecuritycontext_test>
475544
+  <linux:selinuxsecuritycontext_object comment="unlabeled_t in /dev" id="object_selinux_dev_unlabeled_t" version="1">
475544
+    <linux:filepath operation="equals" var_ref="variable_dev_device_files"  var_check="at least one"/>
475544
+    <filter action="include">state_selinux_dev_unlabeled_t</filter>
475544
+  </linux:selinuxsecuritycontext_object>
475544
+  <linux:selinuxsecuritycontext_state comment="unlabeled_t label" id="state_selinux_dev_unlabeled_t" version="1">
475544
+    <linux:type datatype="string" operation="equals">unlabeled_t</linux:type>
475544
+  </linux:selinuxsecuritycontext_state>
475544
+
475544
 </def-group>
475544
475544
From 0bd95e6dbe3684524c86150cdb6beb0af05ff119 Mon Sep 17 00:00:00 2001
475544
From: Vojtech Polasek <vpolasek@redhat.com>
475544
Date: Tue, 7 Jul 2020 11:33:26 +0200
475544
Subject: [PATCH 3/3] add tests
475544
475544
---
475544
 .../tests/block_device_device_t.fail.sh            |  4 ++++
475544
 .../tests/char_device_unlabeled_t.fail.sh          | 14 ++++++++++++++
475544
 .../tests/regular_file_device_t.pass.sh            |  4 ++++
475544
 .../tests/symlink_with_wrong_label.pass.sh         |  4 ++++
475544
 4 files changed, 26 insertions(+)
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/block_device_device_t.fail.sh
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/char_device_unlabeled_t.fail.sh
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/regular_file_device_t.pass.sh
475544
 create mode 100644 linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/symlink_with_wrong_label.pass.sh
475544
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/block_device_device_t.fail.sh b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/block_device_device_t.fail.sh
475544
new file mode 100644
475544
index 0000000000..08c4142e5b
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/block_device_device_t.fail.sh
475544
@@ -0,0 +1,4 @@
475544
+#!/bin/bash
475544
+
475544
+mknod /dev/foo b 1 5
475544
+chcon -t device_t /dev/foo
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/char_device_unlabeled_t.fail.sh b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/char_device_unlabeled_t.fail.sh
475544
new file mode 100644
475544
index 0000000000..1da85c2034
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/char_device_unlabeled_t.fail.sh
475544
@@ -0,0 +1,14 @@
475544
+#!/bin/bash
475544
+
475544
+# selinux does not allow unlabeled_t in /dev
475544
+# we have to modify the selinux policy to allow that
475544
+
475544
+echo '(allow unlabeled_t device_t (filesystem (associate)))' > /tmp/unlabeled_t.cil
475544
+semodule -i /tmp/unlabeled_t.cil
475544
+
475544
+mknod /dev/foo c 1 5
475544
+chcon -t unlabeled_t /dev/foo
475544
+
475544
+
475544
+mknod /dev/foo c 1 5
475544
+chcon -t device_t /dev/foo
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/regular_file_device_t.pass.sh b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/regular_file_device_t.pass.sh
475544
new file mode 100644
475544
index 0000000000..d161951d7a
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/regular_file_device_t.pass.sh
475544
@@ -0,0 +1,4 @@
475544
+#!/bin/bash
475544
+
475544
+touch /dev/foo
475544
+restorecon -F /dev/foo
475544
diff --git a/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/symlink_with_wrong_label.pass.sh b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/symlink_with_wrong_label.pass.sh
475544
new file mode 100644
475544
index 0000000000..a8280bf37e
475544
--- /dev/null
475544
+++ b/linux_os/guide/system/selinux/selinux_all_devicefiles_labeled/tests/symlink_with_wrong_label.pass.sh
475544
@@ -0,0 +1,4 @@
475544
+#!/bin/bash
475544
+
475544
+ln -s /dev/cpu /dev/foo
475544
+restorecon -F /dev/foo