Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1818472

Summary: semodule -i sets wrong context in homedirs
Product: Red Hat Enterprise Linux 8 Reporter: Roger Sewell <roger.sewell>
Component: policycoreutilsAssignee: Vit Mojzis <vmojzis>
Status: CLOSED NOTABUG QA Contact: Milos Malik <mmalik>
Severity: high Docs Contact:
Priority: medium    
Version: 8.1CC: dwalsh, lvrabec, mmalik, plautrba, ssekidde, vmojzis
Target Milestone: rcKeywords: Triaged
Target Release: 8.3Flags: pm-rhel: mirror+
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-11-23 07:56:07 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1894575    
Attachments:
Description Flags
This attachment contains the module definitions and bad behaviour on CentOS 8.1
none
Corresponding correct behaviour on CentOS 7.7 none

Description Roger Sewell 2020-03-28 12:31:42 UTC
Created attachment 1674280 [details]
This attachment contains the module definitions and bad behaviour on CentOS 8.1

Description of problem:

On inserting a module containing an .fc section that sets contexts both in general and in homedirs, those in homedirs have the categories removed when they should not be, while those not in homedirs are correctly set.

Version-Release number of selected component (if applicable):

selinux-policy-3.14.3-20.el8.noarch         
selinux-policy-targeted-3.14.3-20.el8.noarch
selinux-policy-devel-3.14.3-20.el8.noarch
policycoreutils-2.9-3.el8.x86_64
policycoreutils-devel-2.9-3.el8.x86_64 
libselinux-2.9-2.1.el8.x86_64       
libselinux-utils-2.9-2.1.el8.x86_64 
libsemanage-2.9-1.el8.x86_64        
libsepol-2.9-1.el8.x86_64           

How reproducible: Always


Steps to Reproduce:
1. In the attachment bugshow.20200328b.8.1.tar.gz are module files duck.if, duck.te, duck.fc . Compile and install them as shown in duck.installation.

2. Look at the file_contexts.homedirs entries resulting as detailed in duck.result .

Actual results: As shown in duck.result the final :c0 is missing from the line 

/home/[^/]+/quack/quack	user_u:object_r:mozilla_home_t:s0

in /etc/selinux/targeted/contexts/files/file_contexts.homedirs .

However, the correct line is present in /var/lib/selinux/targeted/active/homedir_template:

HOME_DIR/quack/quack	system_u:object_r:mozilla_home_t:s0:c0

Meantime, in the corresponding non-homedir contexts file /etc/selinux/targeted/contexts/files/file_contexts, the c0 is correctly present in the line for /etc/shadow- .

Expected results: Immediately after submitting this I will attach a second attachment bugshow.20200328a.7.7.tar.gz indicating the correct behaviour in CentOS 7.7, showing that the final :c0 is present in filecontexts.homedirs on the line for quack/quack .

Comment 1 Roger Sewell 2020-03-28 12:32:56 UTC
Created attachment 1674281 [details]
Corresponding correct behaviour on CentOS 7.7

This attachment contains the corresponding correct behaviour on CentOS 7.7 .

Comment 2 Petr Lautrbach 2020-04-29 11:35:53 UTC
Thanks for the report. I've locally reproduced it and I'm investigating the problem.

Comment 8 Vit Mojzis 2020-11-05 18:55:18 UTC
The behaviour is deliberate.
Each homedir filecontext entry is evaluated against each SELinux login mapping (semanage login -l) and the context is changed to match given SELinux user (semanage user -l) to allow access.
[^/]+ is a fallback (which is assigned the __default__ login user and level, "user_u" and "s0" on your system)
Installing your policy on RHEL-8.3 yields 
# semanage fcontext -l | grep quack
/home/[^/]+/quack                                  all files          unconfined_u:object_r:mozilla_home_t:s0 
/home/[^/]+/quack/quack                            all files          unconfined_u:object_r:mozilla_home_t:s0 

# semanage login -l
Login Name           SELinux User         MLS/MCS Range        Service

__default__          unconfined_u         s0-s0:c0.c1023       *
root                 unconfined_u         s0-s0:c0.c1023       *
# semanage user -l
                Labeling   MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles
...
unconfined_u    user       s0      s0-s0:c0.c1023                 system_r unconfined_r
...

Adding a new SELinux user with different default level shows this better:
# semanage user -a duck_u -L s0:c0 -R "staff_r sysadm_r unconfined_r" -r s0-s0:c0.c1023
# useradd -Z duck_u duck
# semanage fcontext -l | grep quack
/home/[^/]+/quack                                  all files          unconfined_u:object_r:mozilla_home_t:s0 
/home/[^/]+/quack/quack                            all files          unconfined_u:object_r:mozilla_home_t:s0
/home/duck/quack                                   all files          duck_u:object_r:mozilla_home_t:s0:c0 
/home/duck/quack/quack                             all files          duck_u:object_r:mozilla_home_t:s0:c0

Not changing the home dir context definition could cause some users not to have access to files present in their home directories (in your example "user_u" with MLS/MCS Range of "s0" would not have access to a file labelled system_u:object_r:mozilla_home_t:s0:c0). What was your original use-case?

Comment 9 Roger Sewell 2020-11-06 15:53:49 UTC
Vit,

Thank you very much for giving attention to this, which still seems to me to be a bug. Apologies for taking a while to answer - I had to get my head around it again after an interval.

1. All worked well under CentOS 7.7 - so I don't understand why it was changed, given the following.

2. It seems I should have told you that I have the following settings different from what you assumed:

[root@revelation local]# semanage login -l

Login Name           SELinux User         MLS/MCS Range        Service

__default__          user_u               s0-s0:c0             *
rfs                  unconfined_u         s0-s0:c0.c1023       *
root                 unconfined_u         s0-s0:c0.c1023       *

but the same result as you expected for

[root@revelation local]# semanage user -l

                Labelling  MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

guest_u         user       s0         s0                             guest_r
root            user       s0         s0-s0:c0.c1023                 staff_r sysadm_r system_r unconfined_r
staff_u         user       s0         s0-s0:c0.c1023                 staff_r sysadm_r unconfined_r
sysadm_u        user       s0         s0-s0:c0.c1023                 sysadm_r
system_u        user       s0         s0-s0:c0.c1023                 system_r unconfined_r
unconfined_u    user       s0         s0-s0:c0.c1023                 system_r unconfined_r user_r
user_u          user       s0         s0-s0:c0.c1023                 user_r
xguest_u        user       s0         s0                             xguest_r

3. I agree that if I change the level for user_u by doing 

[root@revelation local]# semanage user -m -L s0:c0 user_u
[root@revelation local]# semanage user -l 

                Labelling  MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

...
user_u          user       s0:c0      s0-s0:c0.c1023                 user_r
...

and then reload the duck module I get

[root@revelation local]# semanage fcontext -l | grep quack
/home/[^/]+/quack                                  all files          user_u:object_r:mozilla_home_t:s0:c0 
/home/[^/]+/quack/quack                            all files          user_u:object_r:mozilla_home_t:s0:c0 
/home/rfs/quack                                    all files          unconfined_u:object_r:mozilla_home_t:s0 
/home/rfs/quack/quack                              all files          unconfined_u:object_r:mozilla_home_t:s0 

but now *both* quack and quack/quack have been given c0 qualifiers for user_u where only quack/quack should have had (and only quack/quack had under 7.7).

4. If I login as an unpriviledged user (from a gdm screen) (after restoring user_u to level s0), I get 

[root@revelation local]# semanage user -m -L s0 user_u
[root@revelation local]# semanage user -l

                Labelling  MLS/       MLS/                          
SELinux User    Prefix     MCS Level  MCS Range                      SELinux Roles

...
user_u          user       s0         s0-s0:c0.c1023                 user_r
...

(now login as ldds from gdm screen)

[ldds@revelation ~]$ ps -Z
LABEL                               PID TTY          TIME CMD
user_u:user_r:user_t:s0-s0:c0        20738 pts/5    00:00:00 bash
user_u:user_r:user_t:s0-s0:c0        20771 pts/5    00:00:00 ps
[ldds@revelation ~]$ ls -Z
...
user_u:object_r:mozilla_home_t:s0:c0 quack
   user_u:object_r:user_home_t:s0:c0 quackduckquack
...
[ldds@revelation ~]$ cat quackduckquack
quack

i.e. no problems reading a c0 file in the home directory, which as far as I can see is contrary to your last statement above.

But in any case, if a user sets a context which prevents him accessing a file in his own home directory, that's surely up to him, isn't it, and may be exactly what he intends and wants ?

5. The use case of interest is to be able to develop a program in ones home space and run it using

runcon -l s0 myprog

thereby ensuring that it has no access to anything carrying a c0 label.

The sandbox method is both extremely cumbersome and far too restrictive in the setup it permits, while this is quick and easy.

(I should perhaps also mention that my selinux policy is modified to ensure that an MCS-constrained process cannot call a non-MCS-constrained process and thereby escape this restriction: based on selinux-policy 3.14.3-41.el8_2.4:

diff -Naur a/policy/mcs b/policy/mcs
--- a/policy/mcs	2020-04-24 14:47:10.000000000 +0100
+++ b/policy/mcs	2020-07-28 14:50:21.810039144 +0100
@@ -114,10 +114,10 @@
 	(( h1 dom h2 ) or ( t1 != mcs_constrained_type ));
 
 mlsconstrain process { transition dyntransition }
-	(( h1 dom h2 ) or ( t1 != mcs_constrained_type ));
+	((( h1 dom h2 ) and (t2 == mcs_constrained_type)) or ( t1 != mcs_constrained_type ));
 
 mlsconstrain process { ptrace }
-	(( h1 dom h2 ) or ( t1 != mcs_constrained_type ));
+	((( h1 dom h2 ) and (t2 == mcs_constrained_type)) or ( t1 != mcs_constrained_type ));
 
 mlsconstrain process { sigkill sigstop }
 	(( h1 dom h2 ) or ( t1 != mcs_constrained_type ));

A few other minor mods are needed to ensure everything else can keep working despite this. But this all doesn't affect the issue in question, which you also observe despite an unmodified SELinux setup. (Indeed, the fact that without this mod a process can escape MCS constraint by calling a process of an unconstrained type might itself be perceived to be a bug (?).))

(Also, I note that the flag "blocks 1894575" has been added, referring to a later-registered bug that I have no access to. Without giving me info I can't have, are you able to tell me what "blocks bugN" would mean in general ?)

With thanks and best wishes,
Roger.

Comment 10 Vit Mojzis 2020-11-09 18:01:15 UTC
Sorry, I assumed you where using SELINUXTYPE=mls in which case "user_u" would not be allowed access to those files. With SELINUXTYPE=targeted you are right that the user can access the file just fine.
Anyway, this behaviour was deliberately chosen by SELinux upstream and is not a bug. I understand that the change breaks your setup, but I do not believe it should be reverted (in "mls" systems this ensures accessibility of home dir files to corresponding users).

Ad 1) See https://lore.kernel.org/selinux/201101051930.26082.russell@coker.com.au/ for reasoning behind the change
Ad 3) The "level" part of the file context definition is changed to match the default MLS/MCS level of given SELinux user (regardless of the level specified in the definition). 
Ad 5) It seems to me that what you are describing can easily be achieved using different file type for "/home/[^/]+/quack/quack" (and allowing all users access to it) and running "myprog" in a custom domain. Or, use file context definitions without "HOME_DIR".

All I can suggest is submitting an RFE to add an option in /etc/selinux/semanage.conf, which would allow user to decide which behaviour they want (the current one, or the one implemented in epel 7). But I don't believe it's worth it, given that there seems to be a more straight forward solution to what you need.

As for "blocks 1894575", it means that this bug needs to be resolved before ticket #1894575. In this case the "blocked" ticket is just used to track bugs with given characteristic (not an actual bug).

Comment 11 Vit Mojzis 2020-11-23 07:56:07 UTC
In light of the discussion above, I'm closing this as "NOT A BUG", since the userspace tools behave as intended in your example.
If you would like to pursue this matter, please file an RFE (as mentioned in Comment 10).